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 * Writes the RGN marker (Region Of Interest)
811 * @param p_tile_no the tile to output
812 * @param p_comp_no the component to output
813 * @param p_stream the stream to write data to.
814 * @param p_j2k J2K codec.
815 * @param p_manager the user event manager.
817 static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k,
818 OPJ_UINT32 p_tile_no,
819 OPJ_UINT32 p_comp_no,
820 opj_stream_private_t *p_stream,
821 opj_event_mgr_t * p_manager );
824 * Reads a RGN marker (Region Of Interest)
826 * @param p_header_data the data contained in the POC box.
827 * @param p_j2k the jpeg2000 codec.
828 * @param p_header_size the size of the data contained in the POC marker.
829 * @param p_manager the user event manager.
831 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
832 OPJ_BYTE * p_header_data,
833 OPJ_UINT32 p_header_size,
834 opj_event_mgr_t * p_manager );
837 * Writes the EOC marker (End of Codestream)
839 * @param p_stream the stream to write data to.
840 * @param p_j2k J2K codec.
841 * @param p_manager the user event manager.
843 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
844 struct opj_stream_private *p_stream,
845 struct opj_event_mgr * p_manager );
848 Write the EOC marker (end of codestream)
849 @param j2k J2K handle
851 static void j2k_write_eoc(opj_j2k_t *j2k);
853 Read the EOC marker (end of codestream)
854 @param j2k J2K handle
856 static void j2k_read_eoc(opj_j2k_t *j2k);
859 * Reads a EOC marker (End Of Codestream)
861 * @param p_header_data the data contained in the SOD box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the SOD marker.
864 * @param p_manager the user event manager.
867 static opj_bool j2k_read_eoc_v2 (
869 struct opj_stream_private *p_stream,
870 struct opj_event_mgr * p_manager
877 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
879 * @param p_stream the stream to write data to.
880 * @param p_j2k J2K codec.
881 * @param p_manager the user event manager.
883 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
884 struct opj_stream_private *p_stream,
885 struct opj_event_mgr * p_manager );
890 * @param p_stream the stream to write data to.
891 * @param p_j2k J2K codec.
892 * @param p_manager the user event manager.
894 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
895 struct opj_stream_private *p_stream,
896 struct opj_event_mgr * p_manager );
899 Add main header marker information
900 @param cstr_info Codestream information structure
901 @param type marker type
902 @param pos byte offset of marker segment
903 @param len length of marker segment
905 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
907 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
909 Add tile header marker information
910 @param tileno tile index number
911 @param cstr_info Codestream information structure
912 @param type marker type
913 @param pos byte offset of marker segment
914 @param len length of marker segment
916 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
918 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);
921 * Reads an unknown marker
923 * @param p_stream the stream object to read from.
924 * @param p_j2k the jpeg2000 codec.
925 * @param p_manager the user event manager.
927 * @return true if the marker could be deduced.
929 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
930 opj_stream_private_t *p_stream,
931 OPJ_UINT32 *output_marker,
932 opj_event_mgr_t * p_manager );
935 * Writes the MCT marker (Multiple Component Transform)
937 * @param p_stream the stream to write data to.
938 * @param p_j2k J2K codec.
939 * @param p_manager the user event manager.
941 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
942 opj_mct_data_t * p_mct_record,
943 struct opj_stream_private *p_stream,
944 struct opj_event_mgr * p_manager );
947 * Reads a MCT marker (Multiple Component Transform)
949 * @param p_header_data the data contained in the MCT box.
950 * @param p_j2k the jpeg2000 codec.
951 * @param p_header_size the size of the data contained in the MCT marker.
952 * @param p_manager the user event manager.
954 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
955 OPJ_BYTE * p_header_data,
956 OPJ_UINT32 p_header_size,
957 opj_event_mgr_t * p_manager );
960 * Writes the MCC marker (Multiple Component Collection)
962 * @param p_stream the stream to write data to.
963 * @param p_j2k J2K codec.
964 * @param p_manager the user event manager.
966 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
967 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
968 struct opj_stream_private *p_stream,
969 struct opj_event_mgr * p_manager );
973 * Reads a MCC marker (Multiple Component Collection)
975 * @param p_header_data the data contained in the MCC box.
976 * @param p_j2k the jpeg2000 codec.
977 * @param p_header_size the size of the data contained in the MCC marker.
978 * @param p_manager the user event manager.
980 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
981 OPJ_BYTE * p_header_data,
982 OPJ_UINT32 p_header_size,
983 opj_event_mgr_t * p_manager );
986 * Writes the MCO marker (Multiple component transformation ordering)
988 * @param p_stream the stream to write data to.
989 * @param p_j2k J2K codec.
990 * @param p_manager the user event manager.
992 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
993 struct opj_stream_private *p_stream,
994 struct opj_event_mgr * p_manager );
997 * Reads a MCO marker (Multiple Component Transform Ordering)
999 * @param p_header_data the data contained in the MCO box.
1000 * @param p_j2k the jpeg2000 codec.
1001 * @param p_header_size the size of the data contained in the MCO marker.
1002 * @param p_manager the user event manager.
1004 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1005 OPJ_BYTE * p_header_data,
1006 OPJ_UINT32 p_header_size,
1007 opj_event_mgr_t * p_manager );
1009 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1011 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1012 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1013 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1014 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1016 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1017 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1018 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1019 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1021 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1022 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1023 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1024 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1028 * Ends the encoding, i.e. frees memory.
1030 * @param p_stream the stream to write data to.
1031 * @param p_j2k J2K codec.
1032 * @param p_manager the user event manager.
1034 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1035 struct opj_stream_private *p_stream,
1036 struct opj_event_mgr * p_manager );
1039 * Writes the CBD marker (Component bit depth definition)
1041 * @param p_stream the stream to write data to.
1042 * @param p_j2k J2K codec.
1043 * @param p_manager the user event manager.
1045 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1046 struct opj_stream_private *p_stream,
1047 struct opj_event_mgr * p_manager );
1050 * Reads a CBD marker (Component bit depth definition)
1051 * @param p_header_data the data contained in the CBD box.
1052 * @param p_j2k the jpeg2000 codec.
1053 * @param p_header_size the size of the data contained in the CBD marker.
1054 * @param p_manager the user event manager.
1056 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1057 OPJ_BYTE * p_header_data,
1058 OPJ_UINT32 p_header_size,
1059 opj_event_mgr_t * p_manager);
1062 * Writes the image components.
1064 * @param p_stream the stream to write data to.
1065 * @param p_j2k J2K codec.
1066 * @param p_manager the user event manager.
1068 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1069 struct opj_stream_private *p_stream,
1070 struct opj_event_mgr * p_manager );
1073 * Writes regions of interests.
1075 * @param p_stream the stream to write data to.
1076 * @param p_j2k J2K codec.
1077 * @param p_manager the user event manager.
1079 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1080 struct opj_stream_private *p_stream,
1081 struct opj_event_mgr * p_manager );
1086 * @param p_stream the stream to write data to.
1087 * @param p_j2k J2K codec.
1088 * @param p_manager the user event manager.
1090 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1091 struct opj_stream_private *p_stream,
1092 struct opj_event_mgr * p_manager );
1095 * Checks the progression order changes values. Tells of the poc given as input are valid.
1096 * A nice message is outputted at errors.
1098 * @param p_pocs the progression order changes.
1099 * @param p_nb_pocs the number of progression order changes.
1100 * @param p_nb_resolutions the number of resolutions.
1101 * @param numcomps the number of components
1102 * @param numlayers the number of layers.
1104 * @return true if the pocs are valid.
1106 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1107 OPJ_UINT32 p_nb_pocs,
1108 OPJ_UINT32 p_nb_resolutions,
1109 OPJ_UINT32 numcomps,
1110 OPJ_UINT32 numlayers,
1111 opj_event_mgr_t * p_manager);
1114 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1116 * @param cp the coding parameters.
1117 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1118 * @param tileno the given tile.
1120 * @return the number of tile parts.
1122 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1124 /** mem allocation for TLM marker*/
1125 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1128 * Calculates the total number of tile parts needed by the encoder to
1129 * encode such an image. If not enough memory is available, then the function return false.
1131 * @param p_nb_tiles pointer that will hold the number of tile parts.
1132 * @param cp the coding parameters for the image.
1133 * @param image the image to encode.
1134 * @param p_j2k the p_j2k encoder.
1135 * @param p_manager the user event manager.
1137 * @return true if the function was successful, false else.
1139 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1141 OPJ_UINT32 * p_nb_tiles,
1143 opj_event_mgr_t * p_manager);
1145 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1147 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1149 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1155 /* ----------------------------------------------------------------------- */
1156 typedef struct j2k_prog_order{
1157 OPJ_PROG_ORDER enum_prog;
1161 j2k_prog_order_t j2k_prog_order_list[] = {
1167 {(OPJ_PROG_ORDER)-1, ""}
1175 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1183 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1185 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1187 opj_j2k_read_int16_to_float,
1188 opj_j2k_read_int32_to_float,
1189 opj_j2k_read_float32_to_float,
1190 opj_j2k_read_float64_to_float
1193 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1195 opj_j2k_read_int16_to_int32,
1196 opj_j2k_read_int32_to_int32,
1197 opj_j2k_read_float32_to_int32,
1198 opj_j2k_read_float64_to_int32
1201 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1203 opj_j2k_write_float_to_int16,
1204 opj_j2k_write_float_to_int32,
1205 opj_j2k_write_float_to_float,
1206 opj_j2k_write_float_to_float64
1209 typedef struct opj_dec_memory_marker_handler
1213 /** value of the state when the marker can appear */
1215 /** action linked to the marker */
1216 opj_bool (*handler) (
1217 opj_j2k_v2_t *p_j2k,
1218 OPJ_BYTE * p_header_data,
1219 OPJ_UINT32 p_header_size,
1220 struct opj_event_mgr * p_manager
1223 opj_dec_memory_marker_handler_t;
1225 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1228 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1229 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1230 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1231 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1232 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1233 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1234 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1235 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1236 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1237 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1238 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1239 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1240 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1242 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1243 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1244 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1245 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1246 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1247 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1249 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1250 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1251 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1252 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1253 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1254 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1255 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1256 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1257 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1258 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1259 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1260 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1261 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1263 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1264 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1265 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1266 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1267 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1268 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1270 #ifdef TODO_MS /* FIXME */
1271 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1272 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1273 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1274 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1276 #endif /* USE_JPWL */
1278 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1279 {J2K_MS_INSEC, 0, j2k_read_insec}
1280 #endif /* USE_JPSEC */
1281 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1286 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1288 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1289 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1293 for (i=0;i<p_nb_elem;++i) {
1294 opj_read_bytes(l_src_data,&l_temp,2);
1296 l_src_data+=sizeof(OPJ_INT16);
1298 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1302 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1304 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1305 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1309 for (i=0;i<p_nb_elem;++i) {
1310 opj_read_bytes(l_src_data,&l_temp,4);
1312 l_src_data+=sizeof(OPJ_INT32);
1314 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1318 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1320 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1321 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1325 for (i=0;i<p_nb_elem;++i) {
1326 opj_read_float(l_src_data,&l_temp);
1328 l_src_data+=sizeof(OPJ_FLOAT32);
1330 *(l_dest_data++) = l_temp;
1334 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1336 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1337 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1341 for (i=0;i<p_nb_elem;++i) {
1342 opj_read_double(l_src_data,&l_temp);
1344 l_src_data+=sizeof(OPJ_FLOAT64);
1346 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1350 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1352 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1353 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1357 for (i=0;i<p_nb_elem;++i) {
1358 opj_read_bytes(l_src_data,&l_temp,2);
1360 l_src_data+=sizeof(OPJ_INT16);
1362 *(l_dest_data++) = (OPJ_INT32) l_temp;
1366 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1368 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1369 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1373 for (i=0;i<p_nb_elem;++i) {
1374 opj_read_bytes(l_src_data,&l_temp,4);
1376 l_src_data+=sizeof(OPJ_INT32);
1378 *(l_dest_data++) = (OPJ_INT32) l_temp;
1382 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1384 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1385 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1389 for (i=0;i<p_nb_elem;++i) {
1390 opj_read_float(l_src_data,&l_temp);
1392 l_src_data+=sizeof(OPJ_FLOAT32);
1394 *(l_dest_data++) = (OPJ_INT32) l_temp;
1398 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1400 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1401 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1405 for (i=0;i<p_nb_elem;++i) {
1406 opj_read_double(l_src_data,&l_temp);
1408 l_src_data+=sizeof(OPJ_FLOAT64);
1410 *(l_dest_data++) = (OPJ_INT32) l_temp;
1414 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1416 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1417 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1421 for (i=0;i<p_nb_elem;++i) {
1422 l_temp = (OPJ_UINT32) *(l_src_data++);
1424 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1426 l_dest_data+=sizeof(OPJ_INT16);
1430 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1432 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1433 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1437 for (i=0;i<p_nb_elem;++i) {
1438 l_temp = (OPJ_UINT32) *(l_src_data++);
1440 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1442 l_dest_data+=sizeof(OPJ_INT32);
1446 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1448 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1449 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1453 for (i=0;i<p_nb_elem;++i) {
1454 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1456 opj_write_float(l_dest_data,l_temp);
1458 l_dest_data+=sizeof(OPJ_FLOAT32);
1462 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1464 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1465 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1469 for (i=0;i<p_nb_elem;++i) {
1470 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1472 opj_write_double(l_dest_data,l_temp);
1474 l_dest_data+=sizeof(OPJ_FLOAT64);
1480 * Converts an enum type progression order to string type.
1482 * @param prg_order the progression order to get.
1484 * @return the string representation of the given progression order.
1486 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1487 j2k_prog_order_t *po;
1488 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1489 if(po->enum_prog == prg_order){
1490 return po->str_prog;
1493 return po->str_prog;
1497 * Checks the progression order changes values. Tells if the poc given as input are valid.
1499 * @param p_pocs the progression order changes.
1500 * @param p_nb_pocs the number of progression order changes.
1501 * @param p_nb_resolutions the number of resolutions.
1502 * @param numcomps the number of components
1503 * @param numlayers the number of layers.
1504 * @param p_manager the user event manager.
1506 * @return true if the pocs are valid.
1508 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1509 OPJ_UINT32 p_nb_pocs,
1510 OPJ_UINT32 p_nb_resolutions,
1511 OPJ_UINT32 p_num_comps,
1512 OPJ_UINT32 p_num_layers,
1513 opj_event_mgr_t * p_manager)
1515 OPJ_UINT32* packet_array;
1516 OPJ_UINT32 index , resno, compno, layno;
1518 OPJ_UINT32 step_c = 1;
1519 OPJ_UINT32 step_r = p_num_comps * step_c;
1520 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1521 opj_bool loss = OPJ_FALSE;
1522 OPJ_UINT32 layno0 = 0;
1524 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1525 if (packet_array == 00) {
1526 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1529 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1531 if (p_nb_pocs == 0) {
1535 index = step_r * p_pocs->resno0;
1536 // take each resolution for each poc
1537 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1539 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1541 // take each comp of each resolution for each poc
1542 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1543 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1545 // and finally take each layer of each res of ...
1546 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1547 //index = step_r * resno + step_c * compno + step_l * layno;
1548 packet_array[comp_index] = 1;
1549 comp_index += step_l;
1552 res_index += step_c;
1559 // iterate through all the pocs
1560 for (i = 1; i < p_nb_pocs ; ++i) {
1561 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1563 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1564 index = step_r * p_pocs->resno0;
1566 // take each resolution for each poc
1567 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1568 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1570 // take each comp of each resolution for each poc
1571 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1572 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1574 // and finally take each layer of each res of ...
1575 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1576 //index = step_r * resno + step_c * compno + step_l * layno;
1577 packet_array[comp_index] = 1;
1578 comp_index += step_l;
1581 res_index += step_c;
1591 for (layno = 0; layno < p_num_layers ; ++layno) {
1592 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1593 for (compno = 0; compno < p_num_comps; ++compno) {
1594 loss |= (packet_array[index]!=1);
1595 //index = step_r * resno + step_c * compno + step_l * layno;
1602 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1605 opj_free(packet_array);
1610 /* ----------------------------------------------------------------------- */
1611 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1614 int tpnum=1,tpend=0;
1615 opj_tcp_t *tcp = &cp->tcps[tileno];
1616 prog = j2k_convert_progression_order(tcp->prg);
1621 if( cp->tp_flag == prog[i] ){
1622 tpend=1;cp->tp_pos=i;
1626 tpnum= tpnum * tcp->pocs[pino].compE;
1629 tpnum= tpnum * tcp->pocs[pino].resE;
1632 tpnum= tpnum * tcp->pocs[pino].prcE;
1635 tpnum= tpnum * tcp->pocs[pino].layE;
1647 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1649 * @param cp the coding parameters.
1650 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1651 * @param tileno the given tile.
1653 * @return the number of tile parts.
1655 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1657 const OPJ_CHAR *prog = 00;
1659 OPJ_UINT32 tpnum = 1;
1660 opj_tcp_v2_t *tcp = 00;
1661 opj_poc_t * l_current_poc = 00;
1664 assert(tileno < (cp->tw * cp->th));
1665 assert(pino < (cp->tcps[tileno].numpocs + 1));
1667 /* get the given tile coding parameter */
1668 tcp = &cp->tcps[tileno];
1671 l_current_poc = &(tcp->pocs[pino]);
1672 assert(l_current_poc != 0);
1674 /* get the progression order as a character string */
1675 prog = j2k_convert_progression_order(tcp->prg);
1676 assert(strlen(prog) > 0);
1678 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1682 /* component wise */
1684 tpnum *= l_current_poc->compE;
1686 /* resolution wise */
1688 tpnum *= l_current_poc->resE;
1692 tpnum *= l_current_poc->prcE;
1696 tpnum *= l_current_poc->layE;
1699 /* whould we split here ? */
1700 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1701 cp->m_specific_param.m_enc.m_tp_pos=i;
1713 /** mem allocation for TLM marker*/
1714 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1715 int pino,tileno,totnum_tp=0;
1717 OPJ_ARG_NOT_USED(img_numcomp);
1719 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1720 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1721 int cur_totnum_tp = 0;
1722 opj_tcp_t *tcp = &cp->tcps[tileno];
1723 for(pino = 0; pino <= tcp->numpocs; pino++) {
1725 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1726 if(!pi) { return -1;}
1727 tp_num = j2k_get_num_tp(cp,pino,tileno);
1728 totnum_tp = totnum_tp + tp_num;
1729 cur_totnum_tp = cur_totnum_tp + tp_num;
1730 pi_destroy(pi, cp, tileno);
1732 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1734 if (j2k->cstr_info) {
1735 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1736 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1744 * Calculates the total number of tile parts needed by the encoder to
1745 * encode such an image. If not enough memory is available, then the function return false.
1747 * @param p_nb_tiles pointer that will hold the number of tile parts.
1748 * @param cp the coding parameters for the image.
1749 * @param image the image to encode.
1750 * @param p_j2k the p_j2k encoder.
1751 * @param p_manager the user event manager.
1753 * @return true if the function was successful, false else.
1755 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1757 OPJ_UINT32 * p_nb_tiles,
1759 opj_event_mgr_t * p_manager)
1761 OPJ_UINT32 pino,tileno;
1762 OPJ_UINT32 l_nb_tiles;
1766 assert(p_nb_tiles != 00);
1768 assert(image != 00);
1769 assert(p_j2k != 00);
1770 assert(p_manager != 00);
1772 l_nb_tiles = cp->tw * cp->th;
1777 /* TODO mergeV2: check this part which use cstr_info */
1778 /*if (p_j2k->cstr_info) {
1779 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1781 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1782 OPJ_UINT32 cur_totnum_tp = 0;
1784 pi_update_encoding_parameters(image,cp,tileno);
1786 for (pino = 0; pino <= tcp->numpocs; ++pino)
1788 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1790 *p_nb_tiles = *p_nb_tiles + tp_num;
1792 cur_totnum_tp += tp_num;
1795 tcp->m_nb_tile_parts = cur_totnum_tp;
1797 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1798 if (l_info_tile_ptr->tp == 00) {
1802 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1804 l_info_tile_ptr->num_tps = cur_totnum_tp;
1811 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1812 OPJ_UINT32 cur_totnum_tp = 0;
1814 pi_update_encoding_parameters(image,cp,tileno);
1816 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1817 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1819 *p_nb_tiles = *p_nb_tiles + tp_num;
1821 cur_totnum_tp += tp_num;
1823 tcp->m_nb_tile_parts = cur_totnum_tp;
1833 * Writes the SOC marker (Start Of Codestream)
1835 * @param p_stream the stream to write data to.
1836 * @param p_j2k J2K codec.
1837 * @param p_manager the user event manager.
1839 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1840 opj_stream_private_t *p_stream,
1841 opj_event_mgr_t * p_manager )
1843 /* 2 bytes will be written */
1844 OPJ_BYTE * l_start_stream = 00;
1847 assert(p_stream != 00);
1848 assert(p_j2k != 00);
1849 assert(p_manager != 00);
1851 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1853 /* write SOC identifier */
1854 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1856 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1862 /* update markers struct */
1864 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1866 assert( 0 && "TODO" );
1867 #endif /* USE_JPWL */
1876 * Reads a SOC marker (Start of Codestream)
1877 * @param p_header_data the data contained in the SOC box.
1878 * @param jp2 the jpeg2000 file codec.
1879 * @param p_header_size the size of the data contained in the SOC marker.
1880 * @param p_manager the user event manager.
1882 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1883 opj_stream_private_t *p_stream,
1884 opj_event_mgr_t * p_manager
1887 OPJ_BYTE l_data [2];
1888 OPJ_UINT32 l_marker;
1891 assert(p_j2k != 00);
1892 assert(p_manager != 00);
1893 assert(p_stream != 00);
1895 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1899 opj_read_bytes(l_data,&l_marker,2);
1900 if (l_marker != J2K_MS_SOC) {
1904 /* Next marker should be a SIZ marker in the main header */
1905 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1907 /* FIXME move it in a index structure included in p_j2k*/
1908 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1910 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1912 /* Add the marker to the codestream index*/
1913 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1920 * Writes the SIZ marker (image and tile size)
1922 * @param p_stream the stream to write data to.
1923 * @param p_j2k J2K codec.
1924 * @param p_manager the user event manager.
1926 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1927 opj_stream_private_t *p_stream,
1928 opj_event_mgr_t * p_manager )
1931 OPJ_UINT32 l_size_len;
1932 OPJ_BYTE * l_current_ptr;
1933 opj_image_t * l_image = 00;
1934 opj_cp_v2_t *cp = 00;
1935 opj_image_comp_t * l_img_comp = 00;
1938 assert(p_stream != 00);
1939 assert(p_j2k != 00);
1940 assert(p_manager != 00);
1942 l_image = p_j2k->m_private_image;
1943 cp = &(p_j2k->m_cp);
1944 l_size_len = 40 + 3 * l_image->numcomps;
1945 l_img_comp = l_image->comps;
1947 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1949 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1950 = (OPJ_BYTE*)opj_realloc(
1951 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1953 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1957 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1960 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1962 /* write SOC identifier */
1963 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1966 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1969 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1972 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1975 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1978 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1981 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1984 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1987 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1990 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
1993 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
1996 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
1999 for (i = 0; i < l_image->numcomps; ++i) {
2000 /* TODO here with MCT ? */
2001 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2004 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2007 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2013 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) {
2023 * Reads a SIZ marker (image and tile size)
2024 * @param p_header_data the data contained in the SIZ box.
2025 * @param jp2 the jpeg2000 file codec.
2026 * @param p_header_size the size of the data contained in the SIZ marker.
2027 * @param p_manager the user event manager.
2029 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2030 OPJ_BYTE * p_header_data,
2031 OPJ_UINT32 p_header_size,
2032 opj_event_mgr_t * p_manager
2035 OPJ_UINT32 l_size, i;
2036 OPJ_UINT32 l_nb_comp;
2037 OPJ_UINT32 l_nb_comp_remain;
2038 OPJ_UINT32 l_remaining_size;
2039 OPJ_UINT32 l_nb_tiles;
2041 opj_image_t *l_image = 00;
2042 opj_cp_v2_t *l_cp = 00;
2043 opj_image_comp_t * l_img_comp = 00;
2044 opj_tcp_v2_t * l_current_tile_param = 00;
2047 assert(p_j2k != 00);
2048 assert(p_manager != 00);
2049 assert(p_header_data != 00);
2051 l_image = p_j2k->m_private_image;
2052 l_cp = &(p_j2k->m_cp);
2054 /* minimum size == 39 - 3 (= minimum component parameter) */
2055 if (p_header_size < 36) {
2056 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2060 l_remaining_size = p_header_size - 36;
2061 l_nb_comp = l_remaining_size / 3;
2062 l_nb_comp_remain = l_remaining_size % 3;
2063 if (l_nb_comp_remain != 0){
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2068 l_size = p_header_size + 2; /* Lsiz */
2070 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2072 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2073 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2075 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2077 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2079 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2081 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2083 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2085 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2087 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2089 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2092 l_image->numcomps = (OPJ_UINT16) l_tmp;
2094 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2098 if (l_image->numcomps != l_nb_comp) {
2099 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);
2104 if (l_cp->correct) {
2105 /* if JPWL is on, we check whether TX errors have damaged
2106 too much the SIZ parameters */
2107 if (!(l_image->x1 * l_image->y1)) {
2108 opj_event_msg_v2(p_manager, EVT_ERROR,
2109 "JPWL: bad image size (%d x %d)\n",
2110 l_image->x1, l_image->y1);
2111 if (!JPWL_ASSUME || JPWL_ASSUME) {
2112 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2117 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2118 if (l_image->numcomps != ((len - 38) / 3)) {
2119 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2120 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2121 l_image->numcomps, ((len - 38) / 3));
2123 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2126 */ /* we try to correct */
2127 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2128 if (l_image->numcomps < ((len - 38) / 3)) {
2129 len = 38 + 3 * l_image->numcomps;
2130 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2133 l_image->numcomps = ((len - 38) / 3);
2134 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2140 /* update components number in the jpwl_exp_comps filed */
2141 l_cp->exp_comps = l_image->numcomps;
2143 #endif /* USE_JPWL */
2145 /* Allocate the resulting image components */
2146 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2147 if (l_image->comps == 00){
2148 l_image->numcomps = 0;
2149 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2153 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2154 l_img_comp = l_image->comps;
2156 /* Read the component information */
2157 for (i = 0; i < l_image->numcomps; ++i){
2159 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2161 l_img_comp->prec = (tmp & 0x7f) + 1;
2162 l_img_comp->sgnd = tmp >> 7;
2163 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2165 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2166 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2168 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2171 if (l_cp->correct) {
2172 /* if JPWL is on, we check whether TX errors have damaged
2173 too much the SIZ parameters, again */
2174 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2175 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2176 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2177 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2179 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2182 /* we try to correct */
2183 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2184 if (!l_image->comps[i].dx) {
2185 l_image->comps[i].dx = 1;
2186 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2187 i, l_image->comps[i].dx);
2189 if (!l_image->comps[i].dy) {
2190 l_image->comps[i].dy = 1;
2191 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2192 i, l_image->comps[i].dy);
2196 #endif /* USE_JPWL */
2197 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2198 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2202 /* Compute the number of tiles */
2203 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2204 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2205 l_nb_tiles = l_cp->tw * l_cp->th;
2207 /* Define the tiles which will be decoded */
2208 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2209 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;
2210 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;
2211 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);
2212 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);
2215 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2216 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2217 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2218 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2222 if (l_cp->correct) {
2223 /* if JPWL is on, we check whether TX errors have damaged
2224 too much the SIZ parameters */
2225 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2226 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2227 "JPWL: bad number of tiles (%d x %d)\n",
2228 l_cp->tw, l_cp->th);
2230 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2233 /* we try to correct */
2234 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2237 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2240 if (l_cp->tw > l_cp->max_tiles) {
2242 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2243 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2244 l_cp->max_tiles, l_cp->tw);
2248 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2251 if (l_cp->th > l_cp->max_tiles) {
2253 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2254 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2255 l_cp->max_tiles, l_cp->th);
2259 #endif /* USE_JPWL */
2261 /* memory allocations */
2262 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2263 if (l_cp->tcps == 00) {
2264 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2267 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2270 if (l_cp->correct) {
2272 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2273 "JPWL: could not alloc tcps field of cp\n");
2274 if (!JPWL_ASSUME || JPWL_ASSUME) {
2275 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2280 #endif /* USE_JPWL */
2282 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2283 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2284 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2285 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2288 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2290 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2291 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2293 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2294 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2297 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));
2298 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2300 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2301 (opj_simple_mcc_decorrelation_data_t*)
2302 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2304 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2305 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2308 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));
2309 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2311 /* set up default dc level shift */
2312 for (i=0;i<l_image->numcomps;++i) {
2313 if (! l_image->comps[i].sgnd) {
2314 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2318 l_current_tile_param = l_cp->tcps;
2319 for (i = 0; i < l_nb_tiles; ++i) {
2320 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2321 if (l_current_tile_param->tccps == 00) {
2322 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2325 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2327 ++l_current_tile_param;
2330 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2331 opj_image_comp_header_update(l_image,l_cp);
2337 * Writes the COM marker (comment)
2339 * @param p_stream the stream to write data to.
2340 * @param p_j2k J2K codec.
2341 * @param p_manager the user event manager.
2343 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2344 opj_stream_private_t *p_stream,
2345 opj_event_mgr_t * p_manager
2348 OPJ_UINT32 l_comment_size;
2349 OPJ_UINT32 l_total_com_size;
2350 const OPJ_CHAR *l_comment;
2351 OPJ_BYTE * l_current_ptr = 00;
2354 assert(p_j2k != 00);
2355 assert(p_stream != 00);
2356 assert(p_manager != 00);
2358 l_comment = p_j2k->m_cp.comment;
2359 l_comment_size = strlen(l_comment);
2360 l_total_com_size = l_comment_size + 6;
2362 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2363 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2364 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2367 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2371 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2374 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2376 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2379 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2382 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2385 memcpy( l_current_ptr,l_comment,l_comment_size);
2387 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) {
2395 * Reads a COM marker (comments)
2396 * @param p_header_data the data contained in the COM box.
2397 * @param jp2 the jpeg2000 file codec.
2398 * @param p_header_size the size of the data contained in the COM marker.
2399 * @param p_manager the user event manager.
2401 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2402 OPJ_BYTE * p_header_data,
2403 OPJ_UINT32 p_header_size,
2404 opj_event_mgr_t * p_manager
2408 assert(p_j2k != 00);
2409 assert(p_manager != 00);
2410 assert(p_header_data != 00);
2411 (void)p_header_size;
2417 * Writes the COD marker (Coding style default)
2419 * @param p_stream the stream to write data to.
2420 * @param p_j2k J2K codec.
2421 * @param p_manager the user event manager.
2423 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2424 opj_stream_private_t *p_stream,
2425 opj_event_mgr_t * p_manager )
2427 opj_cp_v2_t *l_cp = 00;
2428 opj_tcp_v2_t *l_tcp = 00;
2429 OPJ_UINT32 l_code_size,l_remaining_size;
2430 OPJ_BYTE * l_current_data = 00;
2433 assert(p_j2k != 00);
2434 assert(p_manager != 00);
2435 assert(p_stream != 00);
2437 l_cp = &(p_j2k->m_cp);
2438 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2439 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2440 l_remaining_size = l_code_size;
2442 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2443 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2444 = (OPJ_BYTE*)opj_realloc(
2445 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2448 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2452 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2455 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2457 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2458 l_current_data += 2;
2460 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2461 l_current_data += 2;
2463 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2466 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2469 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2472 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2475 l_remaining_size -= 9;
2477 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2478 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2482 if (l_remaining_size != 0) {
2483 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2487 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) {
2496 * Reads a COD marker (Coding Styke defaults)
2497 * @param p_header_data the data contained in the COD box.
2498 * @param p_j2k the jpeg2000 codec.
2499 * @param p_header_size the size of the data contained in the COD marker.
2500 * @param p_manager the user event manager.
2502 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2503 OPJ_BYTE * p_header_data,
2504 OPJ_UINT32 p_header_size,
2505 opj_event_mgr_t * p_manager
2511 opj_cp_v2_t *l_cp = 00;
2512 opj_tcp_v2_t *l_tcp = 00;
2513 opj_image_t *l_image = 00;
2516 assert(p_header_data != 00);
2517 assert(p_j2k != 00);
2518 assert(p_manager != 00);
2520 l_image = p_j2k->m_private_image;
2521 l_cp = &(p_j2k->m_cp);
2523 /* If we are in the first tile-part header of the current tile */
2524 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2525 &l_cp->tcps[p_j2k->m_current_tile_number] :
2526 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2528 /* Make sure room is sufficient */
2529 if (p_header_size < 5) {
2530 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2534 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2536 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2538 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2539 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2542 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2543 if (l_cp->m_specific_param.m_dec.m_layer) {
2544 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2547 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2550 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2554 for (i = 0; i < l_image->numcomps; ++i) {
2555 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2558 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2559 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2563 if (p_header_size != 0) {
2564 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2568 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2569 opj_j2k_copy_tile_component_parameters(p_j2k);
2572 #ifdef WIP_REMOVE_MSD
2573 if (p_j2k->cstr_info) {
2574 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2575 p_j2k->cstr_info->prog = l_tcp->prg;
2576 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2577 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2578 for (i = 0; i < l_image->numcomps; ++i) {
2579 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2588 * Writes the COC marker (Coding style component)
2590 * @param p_comp_no the index of the component to output.
2591 * @param p_stream the stream to write data to.
2592 * @param p_j2k J2K codec.
2593 * @param p_manager the user event manager.
2595 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2596 OPJ_UINT32 p_comp_no,
2597 opj_stream_private_t *p_stream,
2598 opj_event_mgr_t * p_manager )
2600 OPJ_UINT32 l_coc_size,l_remaining_size;
2601 OPJ_UINT32 l_comp_room;
2604 assert(p_j2k != 00);
2605 assert(p_manager != 00);
2606 assert(p_stream != 00);
2608 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2610 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2612 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2613 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2614 = (OPJ_BYTE*)opj_realloc(
2615 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2617 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2621 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2624 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);
2626 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) {
2634 * Writes the COC marker (Coding style component)
2636 * @param p_comp_no the index of the component to output.
2637 * @param p_stream the stream to write data to.
2638 * @param p_j2k J2K codec.
2639 * @param p_manager the user event manager.
2641 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2642 OPJ_UINT32 p_comp_no,
2644 OPJ_UINT32 * p_data_written,
2645 opj_event_mgr_t * p_manager
2648 opj_cp_v2_t *l_cp = 00;
2649 opj_tcp_v2_t *l_tcp = 00;
2650 OPJ_UINT32 l_coc_size,l_remaining_size;
2651 OPJ_BYTE * l_current_data = 00;
2652 opj_image_t *l_image = 00;
2653 OPJ_UINT32 l_comp_room;
2656 assert(p_j2k != 00);
2657 assert(p_manager != 00);
2659 l_cp = &(p_j2k->m_cp);
2660 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2661 l_image = p_j2k->m_private_image;
2662 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2664 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2665 l_remaining_size = l_coc_size;
2667 l_current_data = p_data;
2669 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2670 l_current_data += 2;
2672 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2673 l_current_data += 2;
2675 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2676 l_current_data+=l_comp_room;
2678 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2681 l_remaining_size -= (5 + l_comp_room);
2682 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2683 * p_data_written = l_coc_size;
2687 * Gets the maximum size taken by a coc.
2689 * @param p_j2k the jpeg2000 codec to use.
2691 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2694 OPJ_UINT32 l_nb_comp;
2695 OPJ_UINT32 l_nb_tiles;
2696 OPJ_UINT32 l_max = 0;
2700 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2701 l_nb_comp = p_j2k->m_private_image->numcomps;
2703 for (i=0;i<l_nb_tiles;++i) {
2704 for (j=0;j<l_nb_comp;++j) {
2705 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2714 * Reads a COC marker (Coding Style Component)
2715 * @param p_header_data the data contained in the COC box.
2716 * @param p_j2k the jpeg2000 codec.
2717 * @param p_header_size the size of the data contained in the COC marker.
2718 * @param p_manager the user event manager.
2720 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2721 OPJ_BYTE * p_header_data,
2722 OPJ_UINT32 p_header_size,
2723 opj_event_mgr_t * p_manager
2726 opj_cp_v2_t *l_cp = NULL;
2727 opj_tcp_v2_t *l_tcp = NULL;
2728 opj_image_t *l_image = NULL;
2729 OPJ_UINT32 l_comp_room;
2730 OPJ_UINT32 l_comp_no;
2733 assert(p_header_data != 00);
2734 assert(p_j2k != 00);
2735 assert(p_manager != 00);
2737 l_cp = &(p_j2k->m_cp);
2738 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2739 &l_cp->tcps[p_j2k->m_current_tile_number] :
2740 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2741 l_image = p_j2k->m_private_image;
2743 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2745 /* make sure room is sufficient*/
2746 if (p_header_size < l_comp_room + 1) {
2747 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2750 p_header_size -= l_comp_room + 1;
2752 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2753 p_header_data += l_comp_room;
2754 if (l_comp_no >= l_image->numcomps) {
2755 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2759 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2762 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2763 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2767 if (p_header_size != 0) {
2768 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2775 * Writes the QCD marker (quantization default)
2777 * @param p_comp_number the index of the component to output.
2778 * @param p_stream the stream to write data to.
2779 * @param p_j2k J2K codec.
2780 * @param p_manager the user event manager.
2782 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2783 opj_stream_private_t *p_stream,
2784 opj_event_mgr_t * p_manager
2787 opj_cp_v2_t *l_cp = 00;
2788 opj_tcp_v2_t *l_tcp = 00;
2789 OPJ_UINT32 l_qcd_size,l_remaining_size;
2790 OPJ_BYTE * l_current_data = 00;
2793 assert(p_j2k != 00);
2794 assert(p_manager != 00);
2795 assert(p_stream != 00);
2797 l_cp = &(p_j2k->m_cp);
2798 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2799 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2800 l_remaining_size = l_qcd_size;
2802 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2803 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2804 = (OPJ_BYTE*)opj_realloc(
2805 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2808 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2812 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2815 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2817 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2818 l_current_data += 2;
2820 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2821 l_current_data += 2;
2823 l_remaining_size -= 4;
2825 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2826 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2830 if (l_remaining_size != 0) {
2831 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2835 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) {
2843 * Reads a QCD marker (Quantization defaults)
2844 * @param p_header_data the data contained in the QCD box.
2845 * @param p_j2k the jpeg2000 codec.
2846 * @param p_header_size the size of the data contained in the QCD marker.
2847 * @param p_manager the user event manager.
2849 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2850 OPJ_BYTE * p_header_data,
2851 OPJ_UINT32 p_header_size,
2852 opj_event_mgr_t * p_manager
2856 assert(p_header_data != 00);
2857 assert(p_j2k != 00);
2858 assert(p_manager != 00);
2860 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2861 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2865 if (p_header_size != 0) {
2866 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2870 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2871 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2877 * Writes the QCC marker (quantization component)
2879 * @param p_comp_no the index of the component to output.
2880 * @param p_stream the stream to write data to.
2881 * @param p_j2k J2K codec.
2882 * @param p_manager the user event manager.
2884 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2885 OPJ_UINT32 p_comp_no,
2886 opj_stream_private_t *p_stream,
2887 opj_event_mgr_t * p_manager
2890 OPJ_UINT32 l_qcc_size,l_remaining_size;
2893 assert(p_j2k != 00);
2894 assert(p_manager != 00);
2895 assert(p_stream != 00);
2897 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2898 l_remaining_size = l_qcc_size;
2900 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2901 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2902 = (OPJ_BYTE*)opj_realloc(
2903 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2905 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2909 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2912 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);
2914 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) {
2922 * Writes the QCC marker (quantization component)
2924 * @param p_comp_no the index of the component to output.
2925 * @param p_stream the stream to write data to.
2926 * @param p_j2k J2K codec.
2927 * @param p_manager the user event manager.
2929 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2930 OPJ_UINT32 p_comp_no,
2932 OPJ_UINT32 * p_data_written,
2933 opj_event_mgr_t * p_manager
2936 OPJ_UINT32 l_qcc_size,l_remaining_size;
2937 OPJ_BYTE * l_current_data = 00;
2940 assert(p_j2k != 00);
2941 assert(p_manager != 00);
2943 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2944 l_remaining_size = l_qcc_size;
2946 l_current_data = p_data;
2948 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2949 l_current_data += 2;
2951 if (p_j2k->m_private_image->numcomps <= 256) {
2954 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2955 l_current_data += 2;
2957 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2960 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2961 l_remaining_size -= 6;
2964 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2965 l_current_data += 2;
2967 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2970 l_remaining_size -= 6;
2973 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2975 *p_data_written = l_qcc_size;
2979 * Gets the maximum size taken by a qcc.
2981 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
2983 return opj_j2k_get_max_coc_size(p_j2k);
2987 * Reads a QCC marker (Quantization component)
2988 * @param p_header_data the data contained in the QCC box.
2989 * @param p_j2k the jpeg2000 codec.
2990 * @param p_header_size the size of the data contained in the QCC marker.
2991 * @param p_manager the user event manager.
2993 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
2994 OPJ_BYTE * p_header_data,
2995 OPJ_UINT32 p_header_size,
2996 opj_event_mgr_t * p_manager
2999 OPJ_UINT32 l_num_comp,l_comp_no;
3002 assert(p_header_data != 00);
3003 assert(p_j2k != 00);
3004 assert(p_manager != 00);
3006 l_num_comp = p_j2k->m_private_image->numcomps;
3008 if (l_num_comp <= 256) {
3009 if (p_header_size < 1) {
3010 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3013 opj_read_bytes(p_header_data,&l_comp_no,1);
3018 if (p_header_size < 2) {
3019 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3022 opj_read_bytes(p_header_data,&l_comp_no,2);
3028 if (p_j2k->m_cp.correct) {
3030 static OPJ_UINT32 backup_compno = 0;
3032 /* compno is negative or larger than the number of components!!! */
3033 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3034 opj_event_msg_v2(p_manager, EVT_ERROR,
3035 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3036 l_comp_no, l_num_comp);
3038 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3041 /* we try to correct */
3042 l_comp_no = backup_compno % l_num_comp;
3043 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3044 "- setting component number to %d\n",
3048 /* keep your private count of tiles */
3051 #endif /* USE_JPWL */
3053 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3054 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3058 if (p_header_size != 0) {
3059 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3067 * Writes the POC marker (Progression Order Change)
3069 * @param p_stream the stream to write data to.
3070 * @param p_j2k J2K codec.
3071 * @param p_manager the user event manager.
3073 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
3074 opj_stream_private_t *p_stream,
3075 opj_event_mgr_t * p_manager
3078 OPJ_UINT32 l_nb_comp;
3079 OPJ_UINT32 l_nb_poc;
3080 OPJ_UINT32 l_poc_size;
3081 OPJ_UINT32 l_written_size = 0;
3082 opj_tcp_v2_t *l_tcp = 00;
3083 opj_tccp_t *l_tccp = 00;
3084 OPJ_UINT32 l_poc_room;
3087 assert(p_j2k != 00);
3088 assert(p_manager != 00);
3089 assert(p_stream != 00);
3091 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3092 l_tccp = &l_tcp->tccps[0];
3093 l_nb_comp = p_j2k->m_private_image->numcomps;
3094 l_nb_poc = 1 + l_tcp->numpocs;
3096 if (l_nb_comp <= 256) {
3102 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3104 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3105 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3106 = (OPJ_BYTE*)opj_realloc(
3107 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3110 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3114 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3117 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3119 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) {
3128 * Writes the POC marker (Progression Order Change)
3130 * @param p_stream the stream to write data to.
3131 * @param p_j2k J2K codec.
3132 * @param p_manager the user event manager.
3134 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3136 OPJ_UINT32 * p_data_written,
3137 opj_event_mgr_t * p_manager
3141 OPJ_BYTE * l_current_data = 00;
3142 OPJ_UINT32 l_nb_comp;
3143 OPJ_UINT32 l_nb_poc;
3144 OPJ_UINT32 l_poc_size;
3145 opj_image_t *l_image = 00;
3146 opj_tcp_v2_t *l_tcp = 00;
3147 opj_tccp_t *l_tccp = 00;
3148 opj_poc_t *l_current_poc = 00;
3149 OPJ_UINT32 l_poc_room;
3152 assert(p_j2k != 00);
3153 assert(p_manager != 00);
3155 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3156 l_tccp = &l_tcp->tccps[0];
3157 l_image = p_j2k->m_private_image;
3158 l_nb_comp = l_image->numcomps;
3159 l_nb_poc = 1 + l_tcp->numpocs;
3161 if (l_nb_comp <= 256) {
3168 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3170 l_current_data = p_data;
3172 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3173 l_current_data += 2;
3175 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3176 l_current_data += 2;
3178 l_current_poc = l_tcp->pocs;
3179 for (i = 0; i < l_nb_poc; ++i) {
3180 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3183 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3184 l_current_data+=l_poc_room;
3186 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3189 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3192 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3193 l_current_data+=l_poc_room;
3195 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3198 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3199 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3200 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3201 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3206 *p_data_written = l_poc_size;
3210 * Gets the maximum size taken by the writing of a POC.
3212 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3214 opj_tcp_v2_t * l_tcp = 00;
3215 OPJ_UINT32 l_nb_tiles = 0;
3216 OPJ_UINT32 l_max_poc = 0;
3219 l_tcp = p_j2k->m_cp.tcps;
3220 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3222 for (i=0;i<l_nb_tiles;++i) {
3223 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3229 return 4 + 9 * l_max_poc;
3233 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3235 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3238 OPJ_UINT32 l_nb_tiles;
3239 OPJ_UINT32 l_max = 0;
3240 opj_tcp_v2_t * l_tcp = 00;
3242 l_tcp = p_j2k->m_cp.tcps;
3243 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3245 for (i=0;i<l_nb_tiles;++i) {
3246 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3256 * Gets the maximum size taken by the headers of the SOT.
3258 * @param p_j2k the jpeg2000 codec to use.
3260 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3262 OPJ_UINT32 l_nb_bytes = 0;
3263 OPJ_UINT32 l_nb_comps;
3264 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3266 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3267 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3269 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3270 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3271 l_nb_bytes += l_nb_comps * l_coc_bytes;
3273 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3274 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3277 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3279 /*** DEVELOPER CORNER, Add room for your headers ***/
3286 * Reads a POC marker (Progression Order Change)
3288 * @param p_header_data the data contained in the POC box.
3289 * @param p_j2k the jpeg2000 codec.
3290 * @param p_header_size the size of the data contained in the POC marker.
3291 * @param p_manager the user event manager.
3293 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3294 OPJ_BYTE * p_header_data,
3295 OPJ_UINT32 p_header_size,
3296 opj_event_mgr_t * p_manager
3299 OPJ_UINT32 i, l_nb_comp, l_tmp;
3300 opj_image_t * l_image = 00;
3301 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3302 OPJ_UINT32 l_chunk_size, l_comp_room;
3304 opj_cp_v2_t *l_cp = 00;
3305 opj_tcp_v2_t *l_tcp = 00;
3306 opj_poc_t *l_current_poc = 00;
3309 assert(p_header_data != 00);
3310 assert(p_j2k != 00);
3311 assert(p_manager != 00);
3313 l_image = p_j2k->m_private_image;
3314 l_nb_comp = l_image->numcomps;
3315 if (l_nb_comp <= 256) {
3321 l_chunk_size = 5 + 2 * l_comp_room;
3322 l_current_poc_nb = p_header_size / l_chunk_size;
3323 l_current_poc_remaining = p_header_size % l_chunk_size;
3325 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3326 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3330 l_cp = &(p_j2k->m_cp);
3331 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3332 &l_cp->tcps[p_j2k->m_current_tile_number] :
3333 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3334 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3335 l_current_poc_nb += l_old_poc_nb;
3337 assert(l_current_poc_nb < 32);
3339 /* now poc is in use.*/
3342 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3343 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3344 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3346 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3347 p_header_data+=l_comp_room;
3348 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3350 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3352 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3353 p_header_data+=l_comp_room;
3354 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3356 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3357 /* make sure comp is in acceptable bounds */
3358 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3362 l_tcp->numpocs = l_current_poc_nb - 1;
3367 * Reads a CRG marker (Component registration)
3369 * @param p_header_data the data contained in the TLM box.
3370 * @param p_j2k the jpeg2000 codec.
3371 * @param p_header_size the size of the data contained in the TLM marker.
3372 * @param p_manager the user event manager.
3374 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3375 OPJ_BYTE * p_header_data,
3376 OPJ_UINT32 p_header_size,
3377 opj_event_mgr_t * p_manager
3380 OPJ_UINT32 l_nb_comp;
3382 assert(p_header_data != 00);
3383 assert(p_j2k != 00);
3384 assert(p_manager != 00);
3386 l_nb_comp = p_j2k->m_private_image->numcomps;
3388 if (p_header_size != l_nb_comp *4) {
3389 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3392 /* Do not care of this at the moment since only local variables are set here */
3395 (i = 0; i < l_nb_comp; ++i)
3397 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3399 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3407 * Reads a TLM marker (Tile Length Marker)
3409 * @param p_header_data the data contained in the TLM box.
3410 * @param p_j2k the jpeg2000 codec.
3411 * @param p_header_size the size of the data contained in the TLM marker.
3412 * @param p_manager the user event manager.
3414 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3415 OPJ_BYTE * p_header_data,
3416 OPJ_UINT32 p_header_size,
3417 opj_event_mgr_t * p_manager
3420 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3422 assert(p_header_data != 00);
3423 assert(p_j2k != 00);
3424 assert(p_manager != 00);
3426 if (p_header_size < 2) {
3427 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3432 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3434 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3437 l_ST = ((l_Stlm >> 4) & 0x3);
3438 l_SP = (l_Stlm >> 6) & 0x1;
3440 l_Ptlm_size = (l_SP + 1) * 2;
3441 l_quotient = l_Ptlm_size + l_ST;
3443 l_tot_num_tp = p_header_size / l_quotient;
3444 l_tot_num_tp_remaining = p_header_size % l_quotient;
3446 if (l_tot_num_tp_remaining != 0) {
3447 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3450 /* FIXME Do not care of this at the moment since only local variables are set here */
3453 (i = 0; i < l_tot_num_tp; ++i)
3455 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3456 p_header_data += l_ST;
3457 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3458 p_header_data += l_Ptlm_size;
3465 * Reads a PLM marker (Packet length, main header marker)
3467 * @param p_header_data the data contained in the TLM box.
3468 * @param p_j2k the jpeg2000 codec.
3469 * @param p_header_size the size of the data contained in the TLM marker.
3470 * @param p_manager the user event manager.
3472 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3473 OPJ_BYTE * p_header_data,
3474 OPJ_UINT32 p_header_size,
3475 opj_event_mgr_t * p_manager
3479 assert(p_header_data != 00);
3480 assert(p_j2k != 00);
3481 assert(p_manager != 00);
3483 if (p_header_size < 1) {
3484 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3487 /* Do not care of this at the moment since only local variables are set here */
3489 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3496 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3498 p_header_size -= (1+l_Nplm);
3502 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3506 (i = 0; i < l_Nplm; ++i)
3508 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3510 // take only the last seven bytes
3511 l_packet_len |= (l_tmp & 0x7f);
3519 // store packet length and proceed to next packet
3526 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3535 * Reads a PLT marker (Packet length, tile-part header)
3537 * @param p_header_data the data contained in the PLT box.
3538 * @param p_j2k the jpeg2000 codec.
3539 * @param p_header_size the size of the data contained in the PLT marker.
3540 * @param p_manager the user event manager.
3542 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3543 OPJ_BYTE * p_header_data,
3544 OPJ_UINT32 p_header_size,
3545 opj_event_mgr_t * p_manager
3548 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3551 assert(p_header_data != 00);
3552 assert(p_j2k != 00);
3553 assert(p_manager != 00);
3555 if (p_header_size < 1) {
3556 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3560 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3564 for (i = 0; i < p_header_size; ++i) {
3565 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3567 /* take only the last seven bytes */
3568 l_packet_len |= (l_tmp & 0x7f);
3573 /* store packet length and proceed to next packet */
3578 if (l_packet_len != 0) {
3579 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3588 * Reads a PPM marker (Packed packet headers, main header)
3590 * @param p_header_data the data contained in the POC box.
3591 * @param p_j2k the jpeg2000 codec.
3592 * @param p_header_size the size of the data contained in the POC marker.
3593 * @param p_manager the user event manager.
3596 opj_bool j2k_read_ppm_v2 (
3597 opj_j2k_v2_t *p_j2k,
3598 OPJ_BYTE * p_header_data,
3599 OPJ_UINT32 p_header_size,
3600 struct opj_event_mgr * p_manager
3604 opj_cp_v2_t *l_cp = 00;
3605 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3608 assert(p_header_data != 00);
3609 assert(p_j2k != 00);
3610 assert(p_manager != 00);
3612 if (p_header_size < 1) {
3613 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3617 l_cp = &(p_j2k->m_cp);
3620 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3624 /* First PPM marker */
3626 if (p_header_size < 4) {
3627 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3631 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3635 /* First PPM marker: Initialization */
3636 l_cp->ppm_len = l_N_ppm;
3637 l_cp->ppm_data_size = 0;
3639 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3640 if (l_cp->ppm_buffer == 00) {
3641 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3644 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3646 l_cp->ppm_data = l_cp->ppm_buffer;
3650 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3651 if (p_header_size >= 4) {
3653 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3656 l_cp->ppm_len += l_N_ppm ;
3658 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3659 if (l_cp->ppm_buffer == 00) {
3660 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3663 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3665 l_cp->ppm_data = l_cp->ppm_buffer;
3672 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3674 if (l_remaining_data <= p_header_size) {
3675 /* we must store less information than available in the packet */
3676 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3677 l_cp->ppm_data_size = l_cp->ppm_len;
3678 p_header_size -= l_remaining_data;
3679 p_header_data += l_remaining_data;
3682 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3683 l_cp->ppm_data_size += p_header_size;
3684 p_header_data += p_header_size;
3697 * Reads a PPM marker (Packed packet headers, main header)
3699 * @param p_header_data the data contained in the POC box.
3700 * @param p_j2k the jpeg2000 codec.
3701 * @param p_header_size the size of the data contained in the POC marker.
3702 * @param p_manager the user event manager.
3704 opj_bool j2k_read_ppm_v3 (
3705 opj_j2k_v2_t *p_j2k,
3706 OPJ_BYTE * p_header_data,
3707 OPJ_UINT32 p_header_size,
3708 struct opj_event_mgr * p_manager
3711 opj_cp_v2_t *l_cp = 00;
3712 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3715 assert(p_header_data != 00);
3716 assert(p_j2k != 00);
3717 assert(p_manager != 00);
3719 /* Minimum size of PPM marker is equal to the size of Zppm element */
3720 if (p_header_size < 1) {
3721 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3725 l_cp = &(p_j2k->m_cp);
3728 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3732 /* First PPM marker */
3734 /* We need now at least the Nppm^0 element */
3735 if (p_header_size < 4) {
3736 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3740 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3744 /* First PPM marker: Initialization */
3745 l_cp->ppm_len = l_N_ppm;
3746 l_cp->ppm_data_read = 0;
3748 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3749 if (l_cp->ppm_data == 00) {
3750 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3753 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3755 l_cp->ppm_data_current = l_cp->ppm_data;
3757 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3760 if (p_header_size < 4) {
3761 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3765 /* Uncompleted Ippm series in the previous PPM marker?*/
3766 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3767 /* Get the place where add the remaining Ippm series*/
3768 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3769 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3772 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3776 /* Increase the size of ppm_data to add the new Ippm series*/
3777 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3779 /* Keep the position of the place where concatenate the new series*/
3780 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3781 l_cp->ppm_len += l_N_ppm;
3786 l_remaining_data = p_header_size;
3788 while (l_remaining_data >= l_N_ppm) {
3789 /* read a complete Ippm series*/
3790 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3791 p_header_size -= l_N_ppm;
3792 p_header_data += l_N_ppm;
3794 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3798 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3803 l_remaining_data = p_header_size;
3807 l_remaining_data = p_header_size;
3809 /* Next Ippm series is a complete series ?*/
3810 if (l_remaining_data > l_N_ppm) {
3811 /* Increase the size of ppm_data to add the new Ippm series*/
3812 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3814 /* Keep the position of the place where concatenate the new series */
3815 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3816 l_cp->ppm_len += l_N_ppm;
3821 /* Need to read an incomplete Ippm series*/
3822 if (l_remaining_data) {
3823 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3825 /* Keep the position of the place where concatenate the new series*/
3826 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3827 l_cp->ppm_len += l_N_ppm;
3829 /* Read incomplete Ippm series*/
3830 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3831 p_header_size -= l_remaining_data;
3832 p_header_data += l_remaining_data;
3834 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3839 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3840 if (p_header_size >= 4) {
3842 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3845 l_cp->ppm_len += l_N_ppm ;
3847 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3848 if (l_cp->ppm_buffer == 00) {
3849 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3852 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3854 l_cp->ppm_data = l_cp->ppm_buffer;
3861 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3863 if (l_remaining_data <= p_header_size) {
3864 /* we must store less information than available in the packet */
3865 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3866 l_cp->ppm_data_size = l_cp->ppm_len;
3867 p_header_size -= l_remaining_data;
3868 p_header_data += l_remaining_data;
3871 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3872 l_cp->ppm_data_size += p_header_size;
3873 p_header_data += p_header_size;
3883 * Reads a PPT marker (Packed packet headers, tile-part header)
3885 * @param p_header_data the data contained in the PPT box.
3886 * @param p_j2k the jpeg2000 codec.
3887 * @param p_header_size the size of the data contained in the PPT marker.
3888 * @param p_manager the user event manager.
3890 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3891 OPJ_BYTE * p_header_data,
3892 OPJ_UINT32 p_header_size,
3893 opj_event_mgr_t * p_manager
3896 opj_cp_v2_t *l_cp = 00;
3897 opj_tcp_v2_t *l_tcp = 00;
3901 assert(p_header_data != 00);
3902 assert(p_j2k != 00);
3903 assert(p_manager != 00);
3905 /* We need to have the Z_ppt element at minimum */
3906 if (p_header_size < 1) {
3907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3911 l_cp = &(p_j2k->m_cp);
3913 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");
3917 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3920 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3924 /* Allocate buffer to read the packet header */
3926 /* First PPT marker */
3927 l_tcp->ppt_data_size = 0;
3928 l_tcp->ppt_len = p_header_size;
3930 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3931 if (l_tcp->ppt_buffer == 00) {
3932 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3935 l_tcp->ppt_data = l_tcp->ppt_buffer;
3937 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3940 l_tcp->ppt_len += p_header_size;
3942 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3943 if (l_tcp->ppt_buffer == 00) {
3944 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3947 l_tcp->ppt_data = l_tcp->ppt_buffer;
3949 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3952 /* Read packet header from buffer */
3953 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3955 l_tcp->ppt_data_size += p_header_size;
3961 * Writes the TLM marker (Tile Length Marker)
3963 * @param p_stream the stream to write data to.
3964 * @param p_j2k J2K codec.
3965 * @param p_manager the user event manager.
3967 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3968 opj_stream_private_t *p_stream,
3969 opj_event_mgr_t * p_manager
3972 OPJ_BYTE * l_current_data = 00;
3973 OPJ_UINT32 l_tlm_size;
3976 assert(p_j2k != 00);
3977 assert(p_manager != 00);
3978 assert(p_stream != 00);
3980 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3982 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3983 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3984 = (OPJ_BYTE*)opj_realloc(
3985 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3988 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3992 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3995 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3997 /* change the way data is written to avoid seeking if possible */
3999 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4001 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4002 l_current_data += 2;
4004 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4005 l_current_data += 2;
4007 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4010 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4013 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4014 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) {
4022 * Writes the SOT marker (Start of tile-part)
4024 * @param p_stream the stream to write data to.
4025 * @param p_j2k J2K codec.
4026 * @param p_manager the user event manager.
4028 opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
4030 OPJ_UINT32 * p_data_written,
4031 const opj_stream_private_t *p_stream,
4032 opj_event_mgr_t * p_manager
4036 assert(p_j2k != 00);
4037 assert(p_manager != 00);
4038 assert(p_stream != 00);
4040 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4043 opj_write_bytes(p_data,10,2); /* Lsot */
4046 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4052 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4055 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4060 /* update markers struct */
4062 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4064 assert( 0 && "TODO" );
4065 #endif /* USE_JPWL */
4067 * p_data_written = 12;
4076 * Reads a PPT marker (Packed packet headers, tile-part header)
4078 * @param p_header_data the data contained in the PPT box.
4079 * @param p_j2k the jpeg2000 codec.
4080 * @param p_header_size the size of the data contained in the PPT marker.
4081 * @param p_manager the user event manager.
4083 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4084 OPJ_BYTE * p_header_data,
4085 OPJ_UINT32 p_header_size,
4086 opj_event_mgr_t * p_manager )
4088 opj_cp_v2_t *l_cp = 00;
4089 opj_tcp_v2_t *l_tcp = 00;
4090 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4091 OPJ_UINT32 l_current_part;
4092 OPJ_UINT32 l_tile_x,l_tile_y;
4095 assert(p_header_data != 00);
4096 assert(p_j2k != 00);
4097 assert(p_manager != 00);
4099 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4100 if (p_header_size != 8) {
4101 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4105 l_cp = &(p_j2k->m_cp);
4106 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4109 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4110 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4111 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4114 if (l_cp->correct) {
4116 int tileno = p_j2k->m_current_tile_number;
4117 static int backup_tileno = 0;
4119 /* tileno is negative or larger than the number of tiles!!! */
4120 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4121 opj_event_msg_v2(p_manager, EVT_ERROR,
4122 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4123 tileno, (l_cp->tw * l_cp->th));
4125 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4128 /* we try to correct */
4129 tileno = backup_tileno;
4130 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4131 "- setting tile number to %d\n",
4135 /* keep your private count of tiles */
4138 #endif /* USE_JPWL */
4140 /* look for the tile in the list of already processed tile (in parts). */
4141 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4142 /* since the time taken by this function can only grow at the time */
4144 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4147 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4148 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4150 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4156 if (l_cp->correct) {
4158 /* totlen is negative or larger than the bytes left!!! */
4159 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))) { */
4160 opj_event_msg_v2(p_manager, EVT_ERROR,
4161 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4162 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4164 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4167 /* we try to correct */
4169 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4170 "- setting Psot to %d => assuming it is the last tile\n",
4174 #endif /* USE_JPWL */
4176 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4178 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4179 "we assuming it is the last tile-part of the codestream.\n");
4180 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4183 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4186 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4189 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4190 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4191 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4192 if (l_tcp->m_nb_tile_parts) {
4193 if (l_current_part >= l_tcp->m_nb_tile_parts){
4194 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4195 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4196 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4200 l_tcp->m_nb_tile_parts = l_num_parts;
4203 /* If know the number of tile part header we will check if we didn't read the last*/
4204 if (l_tcp->m_nb_tile_parts) {
4205 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4206 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4210 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4211 /* Keep the size of data to skip after this marker */
4212 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4215 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4216 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4219 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4221 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4222 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4223 p_j2k->m_specific_param.m_decoder.m_skip_data =
4224 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4225 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4226 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4227 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4230 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4231 p_j2k->m_specific_param.m_decoder.m_skip_data =
4232 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4236 if (p_j2k->cstr_index)
4238 assert(p_j2k->cstr_index->tile_index != 00);
4239 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4240 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4242 if (l_num_parts != 0){
4243 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4244 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4247 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4248 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4249 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4251 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4252 (opj_tp_index_t*)opj_realloc(
4253 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4254 l_num_parts* sizeof(opj_tp_index_t));
4257 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4259 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4260 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4261 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4262 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4263 sizeof(opj_tp_index_t));
4266 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4267 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4268 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4269 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4270 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4271 * sizeof(opj_tp_index_t));
4280 /* 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 */
4281 /* if (p_j2k->cstr_info) {
4284 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4287 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4288 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4289 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4290 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4293 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4296 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4300 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4303 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4304 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4305 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4311 * Writes the SOD marker (Start of data)
4313 * @param p_stream the stream to write data to.
4314 * @param p_j2k J2K codec.
4315 * @param p_manager the user event manager.
4317 opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
4318 opj_tcd_v2_t * p_tile_coder,
4320 OPJ_UINT32 * p_data_written,
4321 OPJ_UINT32 p_total_data_size,
4322 const opj_stream_private_t *p_stream,
4323 opj_event_mgr_t * p_manager
4326 opj_tcp_v2_t *l_tcp = 00;
4327 opj_codestream_info_t *l_cstr_info = 00;
4328 opj_cp_v2_t *l_cp = 00;
4330 OPJ_UINT32 l_size_tile;
4331 OPJ_UINT32 l_remaining_data;
4334 assert(p_j2k != 00);
4335 assert(p_manager != 00);
4336 assert(p_stream != 00);
4338 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4341 /* make room for the EOF marker */
4342 l_remaining_data = p_total_data_size - 4;
4344 l_cp = &(p_j2k->m_cp);
4345 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4348 /* update tile coder */
4349 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4350 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4352 l_size_tile = l_cp->th * l_cp->tw;
4355 /* TODO mergeV2: check this part which use cstr_info */
4356 /*l_cstr_info = p_j2k->cstr_info;
4358 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4359 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4360 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4366 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4368 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4373 /* update markers struct */
4374 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4376 assert( 0 && "TODO" );
4377 #endif /* USE_JPWL */
4382 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4383 p_tile_coder->tcd_image->tiles->packno = 0;
4385 l_cstr_info->packno = 0;
4389 *p_data_written = 0;
4391 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)) {
4392 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4396 *p_data_written += 2;
4405 * Reads a SOD marker (Start Of Data)
4407 * @param p_header_data the data contained in the SOD box.
4408 * @param p_j2k the jpeg2000 codec.
4409 * @param p_header_size the size of the data contained in the SOD marker.
4410 * @param p_manager the user event manager.
4412 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4413 opj_stream_private_t *p_stream,
4414 opj_event_mgr_t * p_manager
4417 OPJ_UINT32 l_current_read_size;
4418 opj_codestream_index_t * l_cstr_index = 00;
4419 OPJ_BYTE ** l_current_data = 00;
4420 opj_tcp_v2_t * l_tcp = 00;
4421 OPJ_UINT32 * l_tile_len = 00;
4424 assert(p_j2k != 00);
4425 assert(p_manager != 00);
4426 assert(p_stream != 00);
4428 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4430 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4431 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4432 // but we are in the last tile part,
4433 // so its result will fit on OPJ_UINT32 unless we find
4434 // a file with a single tile part of more than 4 GB...*/
4435 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4438 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4440 l_current_data = &(l_tcp->m_data);
4441 l_tile_len = &l_tcp->m_data_size;
4443 if (! *l_current_data) {
4444 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4447 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4450 if (*l_current_data == 00) {
4451 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4457 l_cstr_index = p_j2k->cstr_index;
4459 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4461 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4462 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4464 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4465 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4467 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4471 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4473 /*l_cstr_index->packno = 0;*/
4476 l_current_read_size = opj_stream_read_data( p_stream,
4477 *l_current_data + *l_tile_len,
4478 p_j2k->m_specific_param.m_decoder.m_sot_length,
4481 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4482 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4485 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4488 *l_tile_len += l_current_read_size;
4494 * Writes the RGN marker (Region Of Interest)
4496 * @param p_tile_no the tile to output
4497 * @param p_comp_no the component to output
4498 * @param p_stream the stream to write data to.
4499 * @param p_j2k J2K codec.
4500 * @param p_manager the user event manager.
4502 opj_bool opj_j2k_write_rgn(opj_j2k_v2_t *p_j2k,
4503 OPJ_UINT32 p_tile_no,
4504 OPJ_UINT32 p_comp_no,
4505 opj_stream_private_t *p_stream,
4506 opj_event_mgr_t * p_manager
4509 OPJ_BYTE * l_current_data = 00;
4510 OPJ_UINT32 l_nb_comp;
4511 OPJ_UINT32 l_rgn_size;
4512 opj_image_t *l_image = 00;
4513 opj_cp_v2_t *l_cp = 00;
4514 opj_tcp_v2_t *l_tcp = 00;
4515 opj_tccp_t *l_tccp = 00;
4516 OPJ_UINT32 l_comp_room;
4519 assert(p_j2k != 00);
4520 assert(p_manager != 00);
4521 assert(p_stream != 00);
4523 l_cp = &(p_j2k->m_cp);
4524 l_tcp = &l_cp->tcps[p_tile_no];
4525 l_tccp = &l_tcp->tccps[p_comp_no];
4527 l_nb_comp = l_image->numcomps;
4529 if (l_nb_comp <= 256) {
4536 l_rgn_size = 6 + l_comp_room;
4538 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4540 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4541 l_current_data += 2;
4543 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4544 l_current_data += 2;
4546 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4547 l_current_data+=l_comp_room;
4549 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4552 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4555 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) {
4563 static void j2k_write_eoc(opj_j2k_t *j2k) {
4564 opj_cio_t *cio = j2k->cio;
4565 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4566 cio_write(cio, J2K_MS_EOC, 2);
4570 /* update markers struct */
4571 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4572 #endif /* USE_JPWL */
4577 * Writes the EOC marker (End of Codestream)
4579 * @param p_stream the stream to write data to.
4580 * @param p_j2k J2K codec.
4581 * @param p_manager the user event manager.
4583 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4584 struct opj_stream_private *p_stream,
4585 struct opj_event_mgr * p_manager )
4588 assert(p_j2k != 00);
4589 assert(p_manager != 00);
4590 assert(p_stream != 00);
4592 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4597 /* update markers struct */
4599 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4601 #endif /* USE_JPWL */
4603 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4607 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4616 * Reads a RGN marker (Region Of Interest)
4618 * @param p_header_data the data contained in the POC box.
4619 * @param p_j2k the jpeg2000 codec.
4620 * @param p_header_size the size of the data contained in the POC marker.
4621 * @param p_manager the user event manager.
4623 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4624 OPJ_BYTE * p_header_data,
4625 OPJ_UINT32 p_header_size,
4626 opj_event_mgr_t * p_manager
4629 OPJ_UINT32 l_nb_comp;
4630 opj_image_t * l_image = 00;
4632 opj_cp_v2_t *l_cp = 00;
4633 opj_tcp_v2_t *l_tcp = 00;
4634 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4637 assert(p_header_data != 00);
4638 assert(p_j2k != 00);
4639 assert(p_manager != 00);
4641 l_image = p_j2k->m_private_image;
4642 l_nb_comp = l_image->numcomps;
4644 if (l_nb_comp <= 256) {
4649 if (p_header_size != 2 + l_comp_room) {
4650 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4654 l_cp = &(p_j2k->m_cp);
4655 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4656 &l_cp->tcps[p_j2k->m_current_tile_number] :
4657 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4659 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4660 p_header_data+=l_comp_room;
4661 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4665 if (l_cp->correct) {
4666 /* totlen is negative or larger than the bytes left!!! */
4667 if (l_comp_room >= l_nb_comp) {
4668 opj_event_msg_v2(p_manager, EVT_ERROR,
4669 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4670 l_comp_room, l_nb_comp);
4671 if (!JPWL_ASSUME || JPWL_ASSUME) {
4672 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4677 #endif /* USE_JPWL */
4679 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4686 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
4688 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4691 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
4698 * Updates the rates of the tcp.
4700 * @param p_stream the stream to write data to.
4701 * @param p_j2k J2K codec.
4702 * @param p_manager the user event manager.
4704 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4705 struct opj_stream_private *p_stream,
4706 struct opj_event_mgr * p_manager )
4708 opj_cp_v2_t * l_cp = 00;
4709 opj_image_t * l_image = 00;
4710 opj_tcp_v2_t * l_tcp = 00;
4711 opj_image_comp_t * l_img_comp = 00;
4714 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4715 OPJ_FLOAT32 * l_rates = 0;
4716 OPJ_FLOAT32 l_sot_remove;
4717 OPJ_UINT32 l_bits_empty, l_size_pixel;
4718 OPJ_UINT32 l_tile_size = 0;
4719 OPJ_UINT32 l_last_res;
4720 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4723 assert(p_j2k != 00);
4724 assert(p_manager != 00);
4725 assert(p_stream != 00);
4728 l_cp = &(p_j2k->m_cp);
4729 l_image = p_j2k->m_private_image;
4732 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4733 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4734 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4736 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4737 l_tp_stride_func = get_tp_stride;
4740 l_tp_stride_func = get_default_stride;
4743 for (i=0;i<l_cp->th;++i) {
4744 for (j=0;j<l_cp->tw;++j) {
4745 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4747 /* 4 borders of the tile rescale on the image if necessary */
4748 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4749 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4750 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4751 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4753 l_rates = l_tcp->rates;
4755 /* Modification of the RATE >> */
4757 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4759 ((*l_rates) * l_bits_empty)
4767 for (k = 1; k < l_tcp->numlayers; ++k) {
4769 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4771 ((*l_rates) * l_bits_empty)
4787 for (i=0;i<l_cp->th;++i) {
4788 for (j=0;j<l_cp->tw;++j) {
4789 l_rates = l_tcp->rates;
4792 *l_rates -= l_sot_remove;
4794 if (*l_rates < 30) {
4801 l_last_res = l_tcp->numlayers - 1;
4803 for (k = 1; k < l_last_res; ++k) {
4806 *l_rates -= l_sot_remove;
4808 if (*l_rates < *(l_rates - 1) + 10) {
4809 *l_rates = (*(l_rates - 1)) + 20;
4817 *l_rates -= (l_sot_remove + 2.f);
4819 if (*l_rates < *(l_rates - 1) + 10) {
4820 *l_rates = (*(l_rates - 1)) + 20;
4828 l_img_comp = l_image->comps;
4831 for (i=0;i<l_image->numcomps;++i) {
4832 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4834 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4842 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4844 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4846 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4847 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4848 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4849 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4853 if (l_cp->m_specific_param.m_enc.m_cinema) {
4854 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4855 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4856 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4860 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4861 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4867 static void j2k_read_eoc(opj_j2k_t *j2k) {
4869 opj_bool success = OPJ_FALSE;
4871 /* if packets should be decoded */
4872 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4873 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4874 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4875 for (i = 0; i < j2k->cp->tileno_size; i++) {
4876 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4877 if (j2k->cp->tileno[i] != -1)
4879 tileno = j2k->cp->tileno[i];
4880 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4881 opj_free(j2k->tile_data[tileno]);
4882 j2k->tile_data[tileno] = NULL;
4883 tcd_free_decode_tile(tcd, i);
4886 success = OPJ_FALSE;
4887 if (success == OPJ_FALSE) {
4888 j2k->state |= J2K_STATE_ERR;
4892 tcd_free_decode(tcd);
4895 /* if packets should not be decoded */
4897 for (i = 0; i < j2k->cp->tileno_size; i++) {
4898 tileno = j2k->cp->tileno[i];
4899 opj_free(j2k->tile_data[tileno]);
4900 j2k->tile_data[tileno] = NULL;
4903 if (j2k->state & J2K_STATE_ERR)
4904 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4906 j2k->state = J2K_STATE_MT;
4910 * Reads a EOC marker (End Of Codestream)
4912 * @param p_header_data the data contained in the SOD box.
4913 * @param p_j2k the jpeg2000 codec.
4914 * @param p_header_size the size of the data contained in the SOD marker.
4915 * @param p_manager the user event manager.
4918 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4919 struct opj_stream_private *p_stream,
4920 struct opj_event_mgr * p_manager )
4923 opj_tcd_v2_t * l_tcd = 00;
4924 OPJ_UINT32 l_nb_tiles;
4925 opj_tcp_v2_t * l_tcp = 00;
4929 assert(p_j2k != 00);
4930 assert(p_manager != 00);
4931 assert(p_stream != 00);
4933 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4934 l_tcp = p_j2k->m_cp.tcps;
4936 l_tcd = tcd_create_v2(OPJ_TRUE);
4938 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4942 for (i = 0; i < l_nb_tiles; ++i) {
4943 if (l_tcp->m_data) {
4944 if (! tcd_init_decode_tile(l_tcd, i)) {
4945 tcd_destroy_v2(l_tcd);
4946 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4950 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4954 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4959 opj_j2k_tcp_destroy(l_tcp);
4963 tcd_destroy_v2(l_tcd);
4969 * Gets the offset of the header.
4971 * @param p_stream the stream to write data to.
4972 * @param p_j2k J2K codec.
4973 * @param p_manager the user event manager.
4975 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
4976 struct opj_stream_private *p_stream,
4977 struct opj_event_mgr * p_manager )
4980 assert(p_j2k != 00);
4981 assert(p_manager != 00);
4982 assert(p_stream != 00);
4984 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4990 * Writes the MCT marker (Multiple Component Transform)
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 j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
4997 struct opj_stream_private *p_stream,
4998 struct opj_event_mgr * p_manager )
5001 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5002 opj_mct_data_t * l_mct_record;
5003 opj_tcp_v2_t * l_tcp;
5006 assert(p_j2k != 00);
5007 assert(p_stream != 00);
5008 assert(p_manager != 00);
5010 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5014 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5015 l_mct_record = l_tcp->m_mct_records;
5017 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5019 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5026 l_mcc_record = l_tcp->m_mcc_records;
5028 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5030 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5037 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5045 * Writes the image components.
5047 * @param p_stream the stream to write data to.
5048 * @param p_j2k J2K codec.
5049 * @param p_manager the user event manager.
5051 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5052 struct opj_stream_private *p_stream,
5053 struct opj_event_mgr * p_manager )
5058 assert(p_j2k != 00);
5059 assert(p_manager != 00);
5060 assert(p_stream != 00);
5062 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5064 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
5068 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
5077 * Writes regions of interests.
5079 * @param p_stream the stream to write data to.
5080 * @param p_j2k J2K codec.
5081 * @param p_manager the user event manager.
5083 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5084 struct opj_stream_private *p_stream,
5085 struct opj_event_mgr * p_manager )
5088 const opj_tccp_t *l_tccp = 00;
5091 assert(p_j2k != 00);
5092 assert(p_manager != 00);
5093 assert(p_stream != 00);
5095 l_tccp = p_j2k->m_cp.tcps->tccps;
5097 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5098 if (l_tccp->roishift) {
5100 if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) {
5114 * @param p_stream the stream to write data to.
5115 * @param p_j2k J2K codec.
5116 * @param p_manager the user event manager.
5118 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5119 struct opj_stream_private *p_stream,
5120 struct opj_event_mgr * p_manager )
5122 opj_codestream_index_t * l_cstr_index = 00;
5125 assert(p_j2k != 00);
5126 assert(p_manager != 00);
5127 assert(p_stream != 00);
5129 l_cstr_index = p_j2k->cstr_index;
5131 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5133 /* The following adjustment is done to adjust the codestream size */
5134 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5135 /* the first bunch of bytes is not in the codestream */
5136 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5141 /* preparation of JPWL marker segments */
5145 /* encode according to JPWL */
5146 jpwl_encode(p_j2k, p_stream, image);
5150 assert( 0 && "TODO" );
5151 #endif /* USE_JPWL */
5156 typedef struct opj_dec_mstabent {
5159 /** value of the state when the marker can appear */
5161 /** action linked to the marker */
5162 void (*handler) (opj_j2k_t *j2k);
5163 } opj_dec_mstabent_t;
5165 opj_dec_mstabent_t j2k_dec_mstab[] = {
5166 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5167 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5168 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5169 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5170 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5171 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5172 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5173 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5174 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5175 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5176 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5177 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5178 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5179 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5180 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5181 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5183 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5184 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5187 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5188 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5189 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5190 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5191 #endif /* USE_JPWL */
5193 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5194 {J2K_MS_INSEC, 0, j2k_read_insec},
5195 #endif /* USE_JPSEC */
5197 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5203 * Reads an unknown marker
5205 * @param p_stream the stream object to read from.
5206 * @param p_j2k the jpeg2000 codec.
5207 * @param p_manager the user event manager.
5209 * @return true if the marker could be deduced.
5211 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5212 opj_stream_private_t *p_stream,
5213 OPJ_UINT32 *output_marker,
5214 opj_event_mgr_t * p_manager
5217 OPJ_UINT32 l_unknown_marker;
5218 const opj_dec_memory_marker_handler_t * l_marker_handler;
5219 OPJ_UINT32 l_size_unk = 2;
5222 assert(p_j2k != 00);
5223 assert(p_manager != 00);
5224 assert(p_stream != 00);
5226 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5229 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5230 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5231 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5235 /* read 2 bytes as the new marker ID*/
5236 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5238 if (!(l_unknown_marker < 0xff00)) {
5240 /* Get the marker handler from the marker ID*/
5241 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5243 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5244 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5248 if (l_marker_handler->id != J2K_MS_UNK) {
5249 /* Add the marker to the codestream index*/
5250 if (l_marker_handler->id != J2K_MS_SOT)
5251 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5252 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5254 break; /* next marker is known and well located */
5262 *output_marker = l_marker_handler->id ;
5268 Read the lookup table containing all the marker, status and action
5269 @param id Marker value
5271 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5272 opj_dec_mstabent_t *e;
5273 for (e = j2k_dec_mstab; e->id != 0; e++) {
5282 * Writes the MCT marker (Multiple Component Transform)
5284 * @param p_stream the stream to write data to.
5285 * @param p_j2k J2K codec.
5286 * @param p_manager the user event manager.
5288 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5289 opj_mct_data_t * p_mct_record,
5290 struct opj_stream_private *p_stream,
5291 struct opj_event_mgr * p_manager )
5293 OPJ_UINT32 l_mct_size;
5294 OPJ_BYTE * l_current_data = 00;
5298 assert(p_j2k != 00);
5299 assert(p_manager != 00);
5300 assert(p_stream != 00);
5302 l_mct_size = 10 + p_mct_record->m_data_size;
5304 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5305 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5306 = (OPJ_BYTE*)opj_realloc(
5307 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5310 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5314 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5317 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5319 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5320 l_current_data += 2;
5322 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5323 l_current_data += 2;
5325 opj_write_bytes(l_current_data,0,2); /* Zmct */
5326 l_current_data += 2;
5328 /* only one marker atm */
5329 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5331 opj_write_bytes(l_current_data,l_tmp,2);
5332 l_current_data += 2;
5334 opj_write_bytes(l_current_data,0,2); /* Ymct */
5337 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5339 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) {
5347 * Reads a MCT marker (Multiple Component Transform)
5349 * @param p_header_data the data contained in the MCT box.
5350 * @param p_j2k the jpeg2000 codec.
5351 * @param p_header_size the size of the data contained in the MCT marker.
5352 * @param p_manager the user event manager.
5354 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5355 OPJ_BYTE * p_header_data,
5356 OPJ_UINT32 p_header_size,
5357 opj_event_mgr_t * p_manager
5361 opj_tcp_v2_t *l_tcp = 00;
5364 opj_mct_data_t * l_mct_data;
5367 assert(p_header_data != 00);
5368 assert(p_j2k != 00);
5370 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5371 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5372 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5374 if (p_header_size < 2) {
5375 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5380 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5383 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5387 if(p_header_size <= 6) {
5388 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5392 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5393 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5396 l_indix = l_tmp & 0xff;
5397 l_mct_data = l_tcp->m_mct_records;
5399 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5400 if (l_mct_data->m_index == l_indix) {
5407 if (i == l_tcp->m_nb_mct_records) {
5408 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5409 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5411 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));
5412 if(! l_tcp->m_mct_records) {
5413 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5417 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5418 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5421 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5424 if (l_mct_data->m_data) {
5425 opj_free(l_mct_data->m_data);
5426 l_mct_data->m_data = 00;
5429 l_mct_data->m_index = l_indix;
5430 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5431 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5433 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5436 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5442 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5443 if (! l_mct_data->m_data) {
5444 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5447 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5449 l_mct_data->m_data_size = p_header_size;
5450 ++l_tcp->m_nb_mct_records;
5456 * Writes the MCC marker (Multiple Component Collection)
5458 * @param p_stream the stream to write data to.
5459 * @param p_j2k J2K codec.
5460 * @param p_manager the user event manager.
5462 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5463 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5464 struct opj_stream_private *p_stream,
5465 struct opj_event_mgr * p_manager )
5468 OPJ_UINT32 l_mcc_size;
5469 OPJ_BYTE * l_current_data = 00;
5470 OPJ_UINT32 l_nb_bytes_for_comp;
5475 assert(p_j2k != 00);
5476 assert(p_manager != 00);
5477 assert(p_stream != 00);
5479 if (p_mcc_record->m_nb_comps > 255 ) {
5480 l_nb_bytes_for_comp = 2;
5484 l_nb_bytes_for_comp = 1;
5488 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5489 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5491 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5492 = (OPJ_BYTE*)opj_realloc(
5493 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5495 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5499 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5502 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5504 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5505 l_current_data += 2;
5507 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5508 l_current_data += 2;
5511 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5512 l_current_data += 2;
5514 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5517 /* only one marker atm */
5518 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5521 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5524 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5527 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 */
5530 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5531 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5532 l_current_data+=l_nb_bytes_for_comp;
5535 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 */
5538 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5540 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5541 l_current_data+=l_nb_bytes_for_comp;
5544 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5546 if (p_mcc_record->m_decorrelation_array) {
5547 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5550 if (p_mcc_record->m_offset_array) {
5551 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5554 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5557 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) {
5565 * Reads a MCC marker (Multiple Component Collection)
5567 * @param p_header_data the data contained in the MCC box.
5568 * @param p_j2k the jpeg2000 codec.
5569 * @param p_header_size the size of the data contained in the MCC marker.
5570 * @param p_manager the user event manager.
5572 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5573 OPJ_BYTE * p_header_data,
5574 OPJ_UINT32 p_header_size,
5575 opj_event_mgr_t * p_manager )
5580 opj_tcp_v2_t * l_tcp;
5581 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5582 opj_mct_data_t * l_mct_data;
5583 OPJ_UINT32 l_nb_collections;
5584 OPJ_UINT32 l_nb_comps;
5585 OPJ_UINT32 l_nb_bytes_by_comp;
5589 assert(p_header_data != 00);
5590 assert(p_j2k != 00);
5591 assert(p_manager != 00);
5593 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5594 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5595 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5597 if (p_header_size < 2) {
5598 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5603 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5606 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5610 if (p_header_size < 7) {
5611 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5615 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5618 l_mcc_record = l_tcp->m_mcc_records;
5620 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5621 if (l_mcc_record->m_index == l_indix) {
5628 if (i == l_tcp->m_nb_mcc_records) {
5629 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5630 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5632 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5633 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5634 if (! l_tcp->m_mcc_records) {
5635 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5638 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5639 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));
5641 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5643 l_mcc_record->m_index = l_indix;
5645 /* only one marker atm */
5646 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5649 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5653 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5656 if (l_nb_collections > 1) {
5657 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5663 for (i=0;i<l_nb_collections;++i) {
5664 if (p_header_size < 3) {
5665 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5669 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5673 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5677 opj_read_bytes(p_header_data,&l_nb_comps,2);
5682 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5683 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5685 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5686 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5690 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5692 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5693 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5694 p_header_data+=l_nb_bytes_by_comp;
5697 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5702 opj_read_bytes(p_header_data,&l_nb_comps,2);
5705 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5706 l_nb_comps &= 0x7fff;
5708 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5709 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5713 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5714 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5718 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5720 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5721 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5722 p_header_data+=l_nb_bytes_by_comp;
5725 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5730 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5733 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5734 l_mcc_record->m_decorrelation_array = 00;
5735 l_mcc_record->m_offset_array = 00;
5737 l_indix = l_tmp & 0xff;
5739 l_mct_data = l_tcp->m_mct_records;
5740 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5741 if (l_mct_data->m_index == l_indix) {
5742 l_mcc_record->m_decorrelation_array = l_mct_data;
5748 if (l_mcc_record->m_decorrelation_array == 00) {
5749 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5754 l_indix = (l_tmp >> 8) & 0xff;
5756 l_mct_data = l_tcp->m_mct_records;
5757 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5758 if (l_mct_data->m_index == l_indix) {
5759 l_mcc_record->m_offset_array = l_mct_data;
5765 if (l_mcc_record->m_offset_array == 00) {
5766 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5772 if (p_header_size != 0) {
5773 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5777 ++l_tcp->m_nb_mcc_records;
5784 * Writes the MCO marker (Multiple component transformation ordering)
5786 * @param p_stream the stream to write data to.
5787 * @param p_j2k J2K codec.
5788 * @param p_manager the user event manager.
5790 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
5791 struct opj_stream_private *p_stream,
5792 struct opj_event_mgr * p_manager
5795 OPJ_BYTE * l_current_data = 00;
5796 OPJ_UINT32 l_mco_size;
5797 opj_tcp_v2_t * l_tcp = 00;
5798 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5802 assert(p_j2k != 00);
5803 assert(p_manager != 00);
5804 assert(p_stream != 00);
5806 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5807 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5809 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5810 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5812 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5813 = (OPJ_BYTE*)opj_realloc(
5814 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5816 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5821 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5824 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5825 l_current_data += 2;
5827 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5828 l_current_data += 2;
5830 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5833 l_mcc_record = l_tcp->m_mcc_records;
5834 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5835 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
5841 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) {
5849 * Reads a MCO marker (Multiple Component Transform Ordering)
5851 * @param p_header_data the data contained in the MCO box.
5852 * @param p_j2k the jpeg2000 codec.
5853 * @param p_header_size the size of the data contained in the MCO marker.
5854 * @param p_manager the user event manager.
5856 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5857 OPJ_BYTE * p_header_data,
5858 OPJ_UINT32 p_header_size,
5859 opj_event_mgr_t * p_manager
5862 OPJ_UINT32 l_tmp, i;
5863 OPJ_UINT32 l_nb_stages;
5864 opj_tcp_v2_t * l_tcp;
5865 opj_tccp_t * l_tccp;
5866 opj_image_t * l_image;
5867 opj_image_comp_t * l_img_comp;
5870 assert(p_header_data != 00);
5871 assert(p_j2k != 00);
5872 assert(p_manager != 00);
5874 l_image = p_j2k->m_private_image;
5875 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5876 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5877 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5879 if (p_header_size < 1) {
5880 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5884 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
5887 if (l_nb_stages > 1) {
5888 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5892 if (p_header_size != l_nb_stages + 1) {
5893 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5897 l_tccp = l_tcp->tccps;
5898 l_img_comp = l_image->comps;
5900 for (i=0;i<l_image->numcomps;++i) {
5901 l_tccp->m_dc_level_shift = 0;
5905 if (l_tcp->m_mct_decoding_matrix) {
5906 opj_free(l_tcp->m_mct_decoding_matrix);
5907 l_tcp->m_mct_decoding_matrix = 00;
5910 for (i=0;i<l_nb_stages;++i) {
5911 opj_read_bytes(p_header_data,&l_tmp,1);
5914 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5922 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5925 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5926 opj_mct_data_t * l_deco_array, * l_offset_array;
5927 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5928 OPJ_UINT32 l_nb_elem;
5929 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5930 opj_tccp_t * l_tccp;
5933 assert(p_tcp != 00);
5935 l_mcc_record = p_tcp->m_mcc_records;
5937 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5938 if (l_mcc_record->m_index == p_index) {
5943 if (i==p_tcp->m_nb_mcc_records) {
5944 /** element discarded **/
5948 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5949 /** do not support number of comps != image */
5953 l_deco_array = l_mcc_record->m_decorrelation_array;
5956 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5957 if (l_deco_array->m_data_size != l_data_size) {
5961 l_nb_elem = p_image->numcomps * p_image->numcomps;
5962 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5963 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5965 if (! p_tcp->m_mct_decoding_matrix ) {
5969 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);
5972 l_offset_array = l_mcc_record->m_offset_array;
5974 if (l_offset_array) {
5975 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5976 if (l_offset_array->m_data_size != l_data_size) {
5980 l_nb_elem = p_image->numcomps;
5981 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
5982 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5984 if (! l_offset_data ) {
5988 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5990 l_tccp = p_tcp->tccps;
5991 l_current_offset_data = l_offset_data;
5993 for (i=0;i<p_image->numcomps;++i) {
5994 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
5998 opj_free(l_offset_data);
6005 * Writes the CBD marker (Component bit depth definition)
6007 * @param p_stream the stream to write data to.
6008 * @param p_j2k J2K codec.
6009 * @param p_manager the user event manager.
6011 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6012 struct opj_stream_private *p_stream,
6013 struct opj_event_mgr * p_manager )
6016 OPJ_UINT32 l_cbd_size;
6017 OPJ_BYTE * l_current_data = 00;
6018 opj_image_t *l_image = 00;
6019 opj_image_comp_t * l_comp = 00;
6022 assert(p_j2k != 00);
6023 assert(p_manager != 00);
6024 assert(p_stream != 00);
6026 l_image = p_j2k->m_private_image;
6027 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6029 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6030 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6031 = (OPJ_BYTE*)opj_realloc(
6032 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6035 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6039 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6042 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6044 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6045 l_current_data += 2;
6047 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6048 l_current_data += 2;
6050 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6053 l_comp = l_image->comps;
6055 for (i=0;i<l_image->numcomps;++i) {
6056 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6062 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) {
6070 * Reads a CBD marker (Component bit depth definition)
6071 * @param p_header_data the data contained in the CBD box.
6072 * @param p_j2k the jpeg2000 codec.
6073 * @param p_header_size the size of the data contained in the CBD marker.
6074 * @param p_manager the user event manager.
6076 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6077 OPJ_BYTE * p_header_data,
6078 OPJ_UINT32 p_header_size,
6079 opj_event_mgr_t * p_manager
6082 OPJ_UINT32 l_nb_comp,l_num_comp;
6083 OPJ_UINT32 l_comp_def;
6085 opj_image_comp_t * l_comp = 00;
6088 assert(p_header_data != 00);
6089 assert(p_j2k != 00);
6090 assert(p_manager != 00);
6092 l_num_comp = p_j2k->m_private_image->numcomps;
6094 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6095 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6099 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6102 if (l_nb_comp != l_num_comp) {
6103 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6107 l_comp = p_j2k->m_private_image->comps;
6108 for (i=0;i<l_num_comp;++i) {
6109 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6111 l_comp->sgnd = (l_comp_def>>7) & 1;
6112 l_comp->prec = (l_comp_def&0x7f) + 1;
6120 /* ----------------------------------------------------------------------- */
6121 /* J2K / JPT decoder interface */
6122 /* ----------------------------------------------------------------------- */
6124 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6126 if(j2k && parameters) {
6127 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6128 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6131 j2k->m_cp.correct = parameters->jpwl_correct;
6132 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6133 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6134 #endif /* USE_JPWL */
6139 /* ----------------------------------------------------------------------- */
6140 /* J2K encoder interface */
6141 /* ----------------------------------------------------------------------- */
6143 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6144 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6151 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6153 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6158 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6160 l_j2k->m_is_decoder = 0;
6161 l_j2k->m_cp.m_is_decoder = 0;
6163 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6164 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6165 opj_j2k_destroy(l_j2k);
6169 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6171 /* validation list creation*/
6172 l_j2k->m_validation_list = opj_procedure_list_create();
6173 if (! l_j2k->m_validation_list) {
6174 opj_j2k_destroy(l_j2k);
6178 /* execution list creation*/
6179 l_j2k->m_procedure_list = opj_procedure_list_create();
6180 if (! l_j2k->m_procedure_list) {
6181 opj_j2k_destroy(l_j2k);
6188 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6190 int tileno, numpocs_tile;
6191 opj_cp_t *cp = NULL;
6193 if(!j2k || !parameters || ! image) {
6197 /* create and initialize the coding parameters structure */
6198 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6200 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6203 /* set default values for cp */
6208 copy user encoding parameters
6210 cp->cinema = parameters->cp_cinema;
6211 cp->max_comp_size = parameters->max_comp_size;
6212 cp->rsiz = parameters->cp_rsiz;
6213 cp->disto_alloc = parameters->cp_disto_alloc;
6214 cp->fixed_alloc = parameters->cp_fixed_alloc;
6215 cp->fixed_quality = parameters->cp_fixed_quality;
6217 /* mod fixed_quality */
6218 if(parameters->cp_matrice) {
6219 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6220 cp->matrice = (int *) opj_malloc(array_size);
6221 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6225 cp->tdx = parameters->cp_tdx;
6226 cp->tdy = parameters->cp_tdy;
6229 cp->tx0 = parameters->cp_tx0;
6230 cp->ty0 = parameters->cp_ty0;
6232 /* comment string */
6233 if(parameters->cp_comment) {
6234 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6236 strcpy(cp->comment, parameters->cp_comment);
6241 calculate other encoding parameters
6244 if (parameters->tile_size_on) {
6245 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6246 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6248 cp->tdx = image->x1 - cp->tx0;
6249 cp->tdy = image->y1 - cp->ty0;
6252 if(parameters->tp_on){
6253 cp->tp_flag = parameters->tp_flag;
6258 for(i=0;i<image->numcomps ;i++){
6259 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6265 calculate JPWL encoding parameters
6268 if (parameters->jpwl_epc_on) {
6272 cp->epc_on = OPJ_TRUE;
6273 cp->info_on = OPJ_FALSE; /* no informative technique */
6276 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6277 cp->epb_on = OPJ_TRUE;
6279 cp->hprot_MH = parameters->jpwl_hprot_MH;
6280 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6281 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6282 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6284 /* if tile specs are not specified, copy MH specs */
6285 if (cp->hprot_TPH[0] == -1) {
6286 cp->hprot_TPH_tileno[0] = 0;
6287 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6289 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6290 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6291 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6292 cp->pprot[i] = parameters->jpwl_pprot[i];
6296 /* set ESD writing */
6297 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6298 cp->esd_on = OPJ_TRUE;
6300 cp->sens_size = parameters->jpwl_sens_size;
6301 cp->sens_addr = parameters->jpwl_sens_addr;
6302 cp->sens_range = parameters->jpwl_sens_range;
6304 cp->sens_MH = parameters->jpwl_sens_MH;
6305 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6306 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6307 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6311 /* always set RED writing to false: we are at the encoder */
6312 cp->red_on = OPJ_FALSE;
6315 cp->epc_on = OPJ_FALSE;
6317 #endif /* USE_JPWL */
6320 /* initialize the mutiple tiles */
6321 /* ---------------------------- */
6322 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6324 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6325 opj_tcp_t *tcp = &cp->tcps[tileno];
6326 tcp->numlayers = parameters->tcp_numlayers;
6327 assert ( tcp->numlayers >= 0 );
6328 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6330 if (cp->fixed_quality) {
6331 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6333 tcp->rates[j] = parameters->tcp_rates[j];
6335 if (cp->fixed_quality) { /* add fixed_quality */
6336 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6338 tcp->rates[j] = parameters->tcp_rates[j];
6342 tcp->csty = parameters->csty;
6343 tcp->prg = parameters->prog_order;
6344 tcp->mct = parameters->tcp_mct;
6348 if (parameters->numpocs) {
6349 /* initialisation of POC */
6351 assert( parameters->numpocs >= 0 );
6352 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6353 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6354 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6355 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6356 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6357 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6358 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6359 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6360 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6361 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6365 tcp->numpocs = numpocs_tile -1 ;
6370 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6372 for (i = 0; i < image->numcomps; i++) {
6373 opj_tccp_t *tccp = &tcp->tccps[i];
6374 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6375 tccp->numresolutions = parameters->numresolution;
6376 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6377 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6378 tccp->cblksty = parameters->mode;
6379 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6380 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6382 assert(parameters->roi_compno >= 0);
6383 if (i == (OPJ_UINT32)parameters->roi_compno) {
6384 tccp->roishift = parameters->roi_shift;
6389 if(parameters->cp_cinema)
6391 /*Precinct size for lowest frequency subband=128*/
6394 /*Precinct size at all other resolutions = 256*/
6395 for (j = 1; j < tccp->numresolutions; j++) {
6400 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6402 assert(tccp->numresolutions > 0);
6403 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6404 if (p < parameters->res_spec) {
6406 if (parameters->prcw_init[p] < 1) {
6409 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6412 if (parameters->prch_init[p] < 1) {
6415 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6419 int res_spec = parameters->res_spec;
6420 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6421 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6423 if (size_prcw < 1) {
6426 tccp->prcw[j] = int_floorlog2(size_prcw);
6429 if (size_prch < 1) {
6432 tccp->prch[j] = int_floorlog2(size_prch);
6436 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6439 for (j = 0; j < tccp->numresolutions; j++) {
6446 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6451 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6452 opj_cparameters_t *parameters,
6454 struct opj_event_mgr * p_manager)
6456 OPJ_UINT32 i, j, tileno, numpocs_tile;
6457 opj_cp_v2_t *cp = 00;
6460 if(!p_j2k || !parameters || ! image) {
6464 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6465 cp = &(p_j2k->m_cp);
6467 /* set default values for cp */
6472 copy user encoding parameters
6474 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6475 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6476 cp->rsiz = parameters->cp_rsiz;
6477 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6478 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6479 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6481 /* mod fixed_quality */
6482 if (parameters->cp_matrice) {
6483 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6484 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6485 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6489 cp->tdx = parameters->cp_tdx;
6490 cp->tdy = parameters->cp_tdy;
6493 cp->tx0 = parameters->cp_tx0;
6494 cp->ty0 = parameters->cp_ty0;
6496 /* comment string */
6497 if(parameters->cp_comment) {
6498 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6500 strcpy(cp->comment, parameters->cp_comment);
6505 calculate other encoding parameters
6508 if (parameters->tile_size_on) {
6509 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6510 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6512 cp->tdx = image->x1 - cp->tx0;
6513 cp->tdy = image->y1 - cp->ty0;
6516 if (parameters->tp_on) {
6517 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6518 cp->m_specific_param.m_enc.m_tp_on = 1;
6523 calculate JPWL encoding parameters
6526 if (parameters->jpwl_epc_on) {
6530 cp->epc_on = OPJ_TRUE;
6531 cp->info_on = OPJ_FALSE; /* no informative technique */
6534 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6535 cp->epb_on = OPJ_TRUE;
6537 cp->hprot_MH = parameters->jpwl_hprot_MH;
6538 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6539 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6540 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6542 /* if tile specs are not specified, copy MH specs */
6543 if (cp->hprot_TPH[0] == -1) {
6544 cp->hprot_TPH_tileno[0] = 0;
6545 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6547 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6548 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6549 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6550 cp->pprot[i] = parameters->jpwl_pprot[i];
6554 /* set ESD writing */
6555 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6556 cp->esd_on = OPJ_TRUE;
6558 cp->sens_size = parameters->jpwl_sens_size;
6559 cp->sens_addr = parameters->jpwl_sens_addr;
6560 cp->sens_range = parameters->jpwl_sens_range;
6562 cp->sens_MH = parameters->jpwl_sens_MH;
6563 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6564 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6565 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6569 /* always set RED writing to false: we are at the encoder */
6570 cp->red_on = OPJ_FALSE;
6573 cp->epc_on = OPJ_FALSE;
6575 #endif /* USE_JPWL */
6578 /* initialize the mutiple tiles */
6579 /* ---------------------------- */
6580 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6581 if (parameters->numpocs) {
6582 /* initialisation of POC */
6583 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6587 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6588 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6589 tcp->numlayers = parameters->tcp_numlayers;
6591 for (j = 0; j < tcp->numlayers; j++) {
6592 if(cp->m_specific_param.m_enc.m_cinema){
6593 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6594 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6596 tcp->rates[j] = parameters->tcp_rates[j];
6598 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6599 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6601 tcp->rates[j] = parameters->tcp_rates[j];
6606 tcp->csty = parameters->csty;
6607 tcp->prg = parameters->prog_order;
6608 tcp->mct = parameters->tcp_mct;
6613 if (parameters->numpocs) {
6614 /* initialisation of POC */
6617 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6618 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6619 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6621 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6622 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6623 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6624 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6625 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6626 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6627 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6633 tcp->numpocs = numpocs_tile -1 ;
6638 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6640 if (parameters->mct_data) {
6642 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6645 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6646 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6647 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6648 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6651 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6652 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6653 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6655 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6656 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6658 tcp->mct_norms = (OPJ_FLOAT64*)
6659 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6661 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6664 for (i = 0; i < image->numcomps; i++) {
6665 opj_tccp_t *tccp = &tcp->tccps[i];
6666 tccp->m_dc_level_shift = l_dc_shift[i];
6669 opj_j2k_setup_mct_encoding(tcp,image);
6673 for (i = 0; i < image->numcomps; i++) {
6674 opj_tccp_t *tccp = &tcp->tccps[i];
6675 opj_image_comp_t * l_comp = &(image->comps[i]);
6677 if (! l_comp->sgnd) {
6678 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6683 for (i = 0; i < image->numcomps; i++) {
6684 opj_tccp_t *tccp = &tcp->tccps[i];
6686 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6687 tccp->numresolutions = parameters->numresolution;
6688 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6689 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6690 tccp->cblksty = parameters->mode;
6691 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6692 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6695 if (i == parameters->roi_compno) {
6696 tccp->roishift = parameters->roi_shift;
6701 if(parameters->cp_cinema) {
6702 //Precinct size for lowest frequency subband=128
6705 //Precinct size at all other resolutions = 256
6706 for (j = 1; j < tccp->numresolutions; j++) {
6711 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6712 OPJ_INT32 p = 0, it_res;
6713 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6714 if (p < parameters->res_spec) {
6716 if (parameters->prcw_init[p] < 1) {
6717 tccp->prcw[it_res] = 1;
6719 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6722 if (parameters->prch_init[p] < 1) {
6723 tccp->prch[it_res] = 1;
6725 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6729 int res_spec = parameters->res_spec;
6730 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6731 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6733 if (size_prcw < 1) {
6734 tccp->prcw[it_res] = 1;
6736 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6739 if (size_prch < 1) {
6740 tccp->prch[it_res] = 1;
6742 tccp->prch[it_res] = int_floorlog2(size_prch);
6746 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6749 for (j = 0; j < tccp->numresolutions; j++) {
6756 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6760 if (parameters->mct_data) {
6761 opj_free(parameters->mct_data);
6762 parameters->mct_data = 00;
6769 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6771 assert(cstr_info != 00);
6773 /* expand the list? */
6774 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
6775 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
6776 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
6779 /* add the marker */
6780 cstr_info->marker[cstr_info->marknum].type = type;
6781 cstr_info->marker[cstr_info->marknum].pos = pos;
6782 cstr_info->marker[cstr_info->marknum].len = len;
6783 cstr_info->marknum++;
6787 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6789 assert(cstr_index != 00);
6791 /* expand the list? */
6792 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6793 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6794 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6797 /* add the marker */
6798 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6799 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6800 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6801 cstr_index->marknum++;
6805 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6807 opj_marker_info_t *marker;
6809 assert(cstr_info != 00);
6811 /* expand the list? */
6812 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
6813 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
6814 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
6817 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
6819 /* add the marker */
6820 marker->type = type;
6823 cstr_info->tile[tileno].marknum++;
6826 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)
6828 assert(cstr_index != 00);
6829 assert(cstr_index->tile_index != 00);
6831 /* expand the list? */
6832 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6833 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6834 cstr_index->tile_index[tileno].marker =
6835 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
6836 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6839 /* add the marker */
6840 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6841 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6842 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6843 cstr_index->tile_index[tileno].marknum++;
6845 if (type == J2K_MS_SOT) {
6846 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6848 if (cstr_index->tile_index[tileno].tp_index)
6849 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6856 * -----------------------------------------------------------------------
6857 * -----------------------------------------------------------------------
6858 * -----------------------------------------------------------------------
6862 * Ends the decompression procedures and possibiliy add data to be read after the
6865 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
6866 opj_stream_private_t *p_stream,
6867 opj_event_mgr_t * p_manager
6877 * Reads a jpeg2000 codestream header structure.
6880 * @param p_stream the stream to read data from.
6881 * @param p_j2k the jpeg2000 codec.
6882 * @param p_manager the user event manager.
6884 * @return true if the box is valid.
6886 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
6887 opj_j2k_v2_t* p_j2k,
6888 opj_image_t** p_image,
6889 opj_event_mgr_t* p_manager )
6892 assert(p_j2k != 00);
6893 assert(p_stream != 00);
6894 assert(p_manager != 00);
6896 /* create an empty image header */
6897 p_j2k->m_private_image = opj_image_create0();
6898 if (! p_j2k->m_private_image) {
6902 /* customization of the validation */
6903 opj_j2k_setup_decoding_validation(p_j2k);
6905 /* validation of the parameters codec */
6906 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6907 opj_image_destroy(p_j2k->m_private_image);
6908 p_j2k->m_private_image = NULL;
6912 /* customization of the encoding */
6913 opj_j2k_setup_header_reading(p_j2k);
6916 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6917 opj_image_destroy(p_j2k->m_private_image);
6918 p_j2k->m_private_image = NULL;
6922 *p_image = opj_image_create0();
6927 /* Copy codestream image information to the output image */
6928 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6930 /*Allocate and initialize some elements of codestrem index*/
6931 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6939 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
6941 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
6944 assert(p_j2k != 00);
6946 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6948 /* DEVELOPER CORNER, add your custom procedures */
6949 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6954 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
6955 * are valid. Developpers wanting to extend the library can add their own validation procedures.
6957 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
6960 assert(p_j2k != 00);
6962 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6963 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6964 /* DEVELOPER CORNER, add your custom validation procedure */
6970 * The mct encoding validation procedure.
6972 * @param p_j2k the jpeg2000 codec to validate.
6973 * @param p_stream the input stream to validate.
6974 * @param p_manager the user event manager.
6976 * @return true if the parameters are correct.
6978 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
6979 opj_stream_private_t *p_stream,
6980 opj_event_mgr_t * p_manager )
6982 opj_bool l_is_valid = OPJ_TRUE;
6986 assert(p_j2k != 00);
6987 assert(p_stream != 00);
6988 assert(p_manager != 00);
6990 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6991 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6992 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
6994 for (i=0;i<l_nb_tiles;++i) {
6995 if (l_tcp->mct == 2) {
6996 opj_tccp_t * l_tccp = l_tcp->tccps;
6997 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6999 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7000 l_is_valid &= ! (l_tccp->qmfbid & 1);
7011 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7014 OPJ_UINT32 l_indix = 1;
7015 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7016 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7017 OPJ_UINT32 l_mct_size,l_nb_elem;
7018 OPJ_FLOAT32 * l_data, * l_current_data;
7019 opj_tccp_t * l_tccp;
7022 assert(p_tcp != 00);
7024 if (p_tcp->mct != 2) {
7028 if (p_tcp->m_mct_decoding_matrix) {
7029 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7030 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7032 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));
7033 if (! p_tcp->m_mct_records) {
7036 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7038 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7040 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7042 if (l_mct_deco_data->m_data) {
7043 opj_free(l_mct_deco_data->m_data);
7044 l_mct_deco_data->m_data = 00;
7047 l_mct_deco_data->m_index = l_indix++;
7048 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7049 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7050 l_nb_elem = p_image->numcomps * p_image->numcomps;
7051 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7052 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7054 if (! l_mct_deco_data->m_data) {
7058 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);
7060 l_mct_deco_data->m_data_size = l_mct_size;
7061 ++p_tcp->m_nb_mct_records;
7064 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7065 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7066 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));
7068 if (! p_tcp->m_mct_records) {
7072 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7073 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7075 if (l_mct_deco_data) {
7076 l_mct_deco_data = l_mct_offset_data - 1;
7080 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7082 if (l_mct_offset_data->m_data) {
7083 opj_free(l_mct_offset_data->m_data);
7084 l_mct_offset_data->m_data = 00;
7087 l_mct_offset_data->m_index = l_indix++;
7088 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7089 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7090 l_nb_elem = p_image->numcomps;
7091 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7092 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7094 if (! l_mct_offset_data->m_data) {
7098 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7100 opj_free(l_mct_offset_data->m_data);
7101 l_mct_offset_data->m_data = 00;
7105 l_tccp = p_tcp->tccps;
7106 l_current_data = l_data;
7108 for (i=0;i<l_nb_elem;++i) {
7109 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7113 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7117 l_mct_offset_data->m_data_size = l_mct_size;
7119 ++p_tcp->m_nb_mct_records;
7121 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7122 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7123 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7124 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7126 if (! p_tcp->m_mcc_records) {
7129 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7130 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));
7134 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7135 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7136 l_mcc_data->m_is_irreversible = 1;
7137 l_mcc_data->m_nb_comps = p_image->numcomps;
7138 l_mcc_data->m_index = l_indix++;
7139 l_mcc_data->m_offset_array = l_mct_offset_data;
7140 ++p_tcp->m_nb_mcc_records;
7146 * Builds the cp decoder parameters to use to decode tile.
7148 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7149 opj_stream_private_t *p_stream,
7150 opj_event_mgr_t * p_manager )
7152 /* add here initialization of cp
7153 copy paste of setup_decoder */
7161 * Builds the cp encoder parameters to use to encode tile.
7163 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7164 opj_stream_private_t *p_stream,
7165 opj_event_mgr_t * p_manager )
7167 /* add here initialization of cp
7168 copy paste of setup_encoder */
7176 * The default encoding validation procedure without any extension.
7178 * @param p_j2k the jpeg2000 codec to validate.
7179 * @param p_stream the input stream to validate.
7180 * @param p_manager the user event manager.
7182 * @return true if the parameters are correct.
7184 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7185 opj_stream_private_t *p_stream,
7186 opj_event_mgr_t * p_manager )
7188 opj_bool l_is_valid = OPJ_TRUE;
7191 assert(p_j2k != 00);
7192 assert(p_stream != 00);
7193 assert(p_manager != 00);
7195 /* STATE checking */
7196 /* make sure the state is at 0 */
7197 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7199 /* POINTER validation */
7200 /* make sure a p_j2k codec is present */
7201 l_is_valid &= (p_j2k->m_procedure_list != 00);
7202 /* make sure a validation list is present */
7203 l_is_valid &= (p_j2k->m_validation_list != 00);
7205 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7206 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7210 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7211 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7215 /* PARAMETER VALIDATION */
7220 * The default decoding validation procedure without any extension.
7222 * @param p_j2k the jpeg2000 codec to validate.
7223 * @param p_stream the input stream to validate.
7224 * @param p_manager the user event manager.
7226 * @return true if the parameters are correct.
7228 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7229 opj_stream_private_t *p_stream,
7230 opj_event_mgr_t * p_manager
7233 opj_bool l_is_valid = OPJ_TRUE;
7236 assert(p_j2k != 00);
7237 assert(p_stream != 00);
7238 assert(p_manager != 00);
7241 /* STATE checking */
7242 /* make sure the state is at 0 */
7244 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7246 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7248 /* POINTER validation */
7249 /* make sure a p_j2k codec is present */
7250 /* make sure a procedure list is present */
7251 l_is_valid &= (p_j2k->m_procedure_list != 00);
7252 /* make sure a validation list is present */
7253 l_is_valid &= (p_j2k->m_validation_list != 00);
7255 /* PARAMETER VALIDATION */
7259 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7260 opj_stream_private_t *p_stream,
7261 opj_event_mgr_t * p_manager)
7263 OPJ_UINT32 l_current_marker;
7264 OPJ_UINT32 l_marker_size;
7265 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7268 assert(p_stream != 00);
7269 assert(p_j2k != 00);
7270 assert(p_manager != 00);
7272 /* We enter in the main header */
7273 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7275 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7276 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7277 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7281 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7282 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7283 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7287 /* Read 2 bytes as the new marker ID */
7288 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7290 /* Try to read until the SOT is detected */
7291 while (l_current_marker != J2K_MS_SOT) {
7293 /* Check if the current marker ID is valid */
7294 if (l_current_marker < 0xff00) {
7295 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7299 /* Get the marker handler from the marker ID */
7300 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7302 /* Manage case where marker is unknown */
7303 if (l_marker_handler->id == J2K_MS_UNK) {
7304 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7305 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7309 if (l_current_marker == J2K_MS_SOT)
7310 break; /* SOT marker is detected main header is completely read */
7311 else /* Get the marker handler from the marker ID */
7312 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7315 /* Check if the marker is known and if it is the right place to find it */
7316 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7317 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7321 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7322 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7323 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7327 /* read 2 bytes as the marker size */
7328 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7329 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7331 /* Check if the marker size is compatible with the header data size */
7332 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7333 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7334 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7335 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7338 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7341 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7342 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) {
7343 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7347 /* Read the marker segment with the correct marker handler */
7348 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7349 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7353 /* Add the marker to the codestream index*/
7354 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7355 l_marker_handler->id,
7356 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7357 l_marker_size + 4 );
7359 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7360 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7361 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7365 /* read 2 bytes as the new marker ID */
7366 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7369 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7371 /* Position of the last element if the main header */
7372 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7374 /* Next step: read a tile-part header */
7375 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7381 * Excutes the given procedures on the given codec.
7383 * @param p_procedure_list the list of procedures to execute
7384 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7385 * @param p_stream the stream to execute the procedures on.
7386 * @param p_manager the user manager.
7388 * @return true if all the procedures were successfully executed.
7390 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7391 opj_procedure_list_t * p_procedure_list,
7392 opj_stream_private_t *p_stream,
7393 opj_event_mgr_t * p_manager )
7395 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7396 opj_bool l_result = OPJ_TRUE;
7397 OPJ_UINT32 l_nb_proc, i;
7400 assert(p_procedure_list != 00);
7401 assert(p_j2k != 00);
7402 assert(p_stream != 00);
7403 assert(p_manager != 00);
7406 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7407 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);
7409 for (i=0;i<l_nb_proc;++i) {
7410 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7414 /* and clear the procedure list at the end.*/
7415 opj_procedure_list_clear(p_procedure_list);
7420 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7421 opj_stream_private_t *p_stream,
7422 opj_event_mgr_t * p_manager
7425 opj_tcp_v2_t * l_tcp = 00;
7426 opj_tcp_v2_t * l_default_tcp = 00;
7427 OPJ_UINT32 l_nb_tiles;
7429 opj_tccp_t *l_current_tccp = 00;
7430 OPJ_UINT32 l_tccp_size;
7431 OPJ_UINT32 l_mct_size;
7432 opj_image_t * l_image;
7433 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7434 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7435 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7436 OPJ_UINT32 l_offset;
7439 assert(p_j2k != 00);
7440 assert(p_stream != 00);
7441 assert(p_manager != 00);
7443 l_image = p_j2k->m_private_image;
7444 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7445 l_tcp = p_j2k->m_cp.tcps;
7446 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7447 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7448 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7451 for (i=0; i<l_nb_tiles; ++i) {
7452 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7453 l_current_tccp = l_tcp->tccps;
7454 /*Copy default coding parameters into the current tile coding parameters*/
7455 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7456 /* Initialize some values of the current tile coding parameters*/
7458 l_tcp->ppt_data = 00;
7459 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7460 l_tcp->tccps = l_current_tccp;
7462 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7463 if (l_default_tcp->m_mct_decoding_matrix) {
7464 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7465 if (! l_tcp->m_mct_decoding_matrix ) {
7468 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7471 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7472 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7473 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7474 if (! l_tcp->m_mct_records) {
7477 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7479 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7480 l_src_mct_rec = l_default_tcp->m_mct_records;
7481 l_dest_mct_rec = l_tcp->m_mct_records;
7483 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7485 if (l_src_mct_rec->m_data) {
7487 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7488 if(! l_dest_mct_rec->m_data) {
7491 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7498 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7499 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7500 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7501 if (! l_tcp->m_mcc_records) {
7504 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7506 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7507 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7508 l_dest_mcc_rec = l_tcp->m_mcc_records;
7510 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7512 if (l_src_mcc_rec->m_decorrelation_array) {
7513 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7514 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7517 if (l_src_mcc_rec->m_offset_array) {
7518 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7519 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7526 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7527 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7529 /* Move to next tile cp*/
7533 /* Create the current tile decoder*/
7534 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7535 if (! p_j2k->m_tcd ) {
7539 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7540 tcd_destroy_v2(p_j2k->m_tcd);
7542 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7550 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7551 * with the marker value.
7552 * @param p_id Marker value to look up
7554 * @return the handler associated with the id.
7556 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7558 const opj_dec_memory_marker_handler_t *e;
7559 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7560 if (e->id == p_id) {
7561 break; /* we find a handler corresponding to the marker ID*/
7569 * Destroys a jpeg2000 codec.
7571 * @param p_j2k the jpeg20000 structure to destroy.
7573 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7579 if (p_j2k->m_is_decoder) {
7581 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7582 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7583 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7584 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7587 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7588 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7589 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7590 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7595 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7596 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7597 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7600 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7601 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7602 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7603 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7606 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7607 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7608 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7609 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7613 tcd_destroy_v2(p_j2k->m_tcd);
7615 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7616 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7618 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7619 p_j2k->m_procedure_list = 00;
7621 opj_procedure_list_destroy(p_j2k->m_validation_list);
7622 p_j2k->m_procedure_list = 00;
7624 j2k_destroy_cstr_index(p_j2k->cstr_index);
7625 p_j2k->cstr_index = NULL;
7627 opj_image_destroy(p_j2k->m_private_image);
7628 p_j2k->m_private_image = NULL;
7630 opj_image_destroy(p_j2k->m_output_image);
7631 p_j2k->m_output_image = NULL;
7636 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7640 if (p_cstr_ind->marker) {
7641 opj_free(p_cstr_ind->marker);
7642 p_cstr_ind->marker = NULL;
7645 if (p_cstr_ind->tile_index) {
7646 OPJ_UINT32 it_tile = 0;
7648 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7650 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7651 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7652 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7655 if(p_cstr_ind->tile_index[it_tile].tp_index){
7656 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7657 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7660 if(p_cstr_ind->tile_index[it_tile].marker){
7661 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7662 p_cstr_ind->tile_index[it_tile].marker = NULL;
7667 opj_free( p_cstr_ind->tile_index);
7668 p_cstr_ind->tile_index = NULL;
7671 opj_free(p_cstr_ind);
7678 * Destroys a tile coding parameter structure.
7680 * @param p_tcp the tile coding parameter to destroy.
7682 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7688 if (p_tcp->ppt_buffer != 00) {
7689 opj_free(p_tcp->ppt_buffer);
7690 p_tcp->ppt_buffer = 00;
7693 if (p_tcp->tccps != 00) {
7694 opj_free(p_tcp->tccps);
7698 if (p_tcp->m_mct_coding_matrix != 00) {
7699 opj_free(p_tcp->m_mct_coding_matrix);
7700 p_tcp->m_mct_coding_matrix = 00;
7703 if (p_tcp->m_mct_decoding_matrix != 00) {
7704 opj_free(p_tcp->m_mct_decoding_matrix);
7705 p_tcp->m_mct_decoding_matrix = 00;
7708 if (p_tcp->m_mcc_records) {
7709 opj_free(p_tcp->m_mcc_records);
7710 p_tcp->m_mcc_records = 00;
7711 p_tcp->m_nb_max_mcc_records = 0;
7712 p_tcp->m_nb_mcc_records = 0;
7715 if (p_tcp->m_mct_records) {
7716 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7719 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7720 if (l_mct_data->m_data) {
7721 opj_free(l_mct_data->m_data);
7722 l_mct_data->m_data = 00;
7728 opj_free(p_tcp->m_mct_records);
7729 p_tcp->m_mct_records = 00;
7732 if (p_tcp->mct_norms != 00) {
7733 opj_free(p_tcp->mct_norms);
7734 p_tcp->mct_norms = 00;
7737 opj_j2k_tcp_data_destroy(p_tcp);
7742 * Destroys the data inside a tile coding parameter structure.
7744 * @param p_tcp the tile coding parameter which contain data to destroy.
7746 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7748 if (p_tcp->m_data) {
7749 opj_free(p_tcp->m_data);
7750 p_tcp->m_data = NULL;
7751 p_tcp->m_data_size = 0;
7756 * Destroys a coding parameter structure.
7758 * @param p_cp the coding parameter to destroy.
7760 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7762 OPJ_UINT32 l_nb_tiles;
7763 opj_tcp_v2_t * l_current_tile = 00;
7774 l_current_tile = p_cp->tcps;
7775 l_nb_tiles = p_cp->th * p_cp->tw;
7778 (i = 0; i < l_nb_tiles; ++i)
7780 opj_j2k_tcp_destroy(l_current_tile);
7783 opj_free(p_cp->tcps);
7787 (p_cp->ppm_buffer != 00)
7789 opj_free(p_cp->ppm_buffer);
7790 p_cp->ppm_buffer = 00;
7793 (p_cp->comment != 00)
7795 opj_free(p_cp->comment);
7799 (! p_cp->m_is_decoder)
7802 (p_cp->m_specific_param.m_enc.m_matrice)
7804 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7805 p_cp->m_specific_param.m_enc.m_matrice = 00;
7813 * Reads a tile header.
7814 * @param p_j2k the jpeg2000 codec.
7815 * @param p_stream the stream to write data to.
7816 * @param p_manager the user event manager.
7818 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7819 OPJ_UINT32 * p_tile_index,
7820 OPJ_UINT32 * p_data_size,
7821 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7822 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7823 OPJ_UINT32 * p_nb_comps,
7825 opj_stream_private_t *p_stream,
7826 opj_event_mgr_t * p_manager )
7828 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7829 OPJ_UINT32 l_marker_size;
7830 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7831 opj_tcp_v2_t * l_tcp = NULL;
7832 OPJ_UINT32 l_nb_tiles;
7835 assert(p_stream != 00);
7836 assert(p_j2k != 00);
7837 assert(p_manager != 00);
7839 /* Reach the End Of Codestream ?*/
7840 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7841 l_current_marker = J2K_MS_EOC;
7843 /* We need to encounter a SOT marker (a new tile-part header) */
7844 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7848 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7849 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7851 /* Try to read until the Start Of Data is detected */
7852 while (l_current_marker != J2K_MS_SOD) {
7854 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7855 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7856 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7860 /* Read 2 bytes from the buffer as the marker size */
7861 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7863 /* Why this condition? FIXME */
7864 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7865 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7867 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7869 /* Get the marker handler from the marker ID */
7870 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7872 /* Check if the marker is known and if it is the right place to find it */
7873 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7874 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7877 /* FIXME manage case of unknown marker as in the main header ? */
7879 /* Check if the marker size is compatible with the header data size */
7880 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7881 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7882 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7883 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7886 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7889 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7890 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) {
7891 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7895 /* Read the marker segment with the correct marker handler */
7896 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7897 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7901 /* Add the marker to the codestream index*/
7902 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
7904 l_marker_handler->id,
7905 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7906 l_marker_size + 4 );
7908 /* Keep the position of the last SOT marker read */
7909 if ( l_marker_handler->id == J2K_MS_SOT ) {
7910 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7911 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7913 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7918 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7919 /* Skip the rest of the tile part header*/
7920 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) {
7921 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7924 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7927 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7928 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7929 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7932 /* Read 2 bytes from the buffer as the new marker ID */
7933 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7937 /* If we didn't skip data before, we need to read the SOD marker*/
7938 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7939 /* Try to read the SOD marker and skip data ? FIXME */
7940 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7946 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7947 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7948 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7949 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7953 /* Read 2 bytes from 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 /* Indicate we will try to read a new tile-part header*/
7959 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7960 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7961 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7963 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7964 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7965 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7969 /* Read 2 bytes from buffer as the new marker ID */
7970 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7974 /* Current marker is the EOC marker ?*/
7975 if (l_current_marker == J2K_MS_EOC) {
7976 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7977 p_j2k->m_current_tile_number = 0;
7978 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
7983 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
7984 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
7985 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7987 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
7988 ++p_j2k->m_current_tile_number;
7992 if (p_j2k->m_current_tile_number == l_nb_tiles) {
7993 *p_go_on = OPJ_FALSE;
7999 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8000 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8004 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8005 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8007 *p_tile_index = p_j2k->m_current_tile_number;
8008 *p_go_on = OPJ_TRUE;
8009 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8010 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8011 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8012 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8013 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8014 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8016 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8022 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8023 OPJ_UINT32 p_tile_index,
8025 OPJ_UINT32 p_data_size,
8026 opj_stream_private_t *p_stream,
8027 opj_event_mgr_t * p_manager )
8029 OPJ_UINT32 l_current_marker;
8030 OPJ_BYTE l_data [2];
8031 opj_tcp_v2_t * l_tcp;
8034 assert(p_stream != 00);
8035 assert(p_j2k != 00);
8036 assert(p_manager != 00);
8038 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8039 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8043 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8044 if (! l_tcp->m_data) {
8045 opj_j2k_tcp_destroy(l_tcp);
8049 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8053 p_j2k->cstr_index) ) {
8054 opj_j2k_tcp_destroy(l_tcp);
8055 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8059 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8063 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8064 * we destroy just the data which will be re-read in read_tile_header*/
8065 /*opj_j2k_tcp_destroy(l_tcp);
8066 p_j2k->m_tcd->tcp = 0;*/
8067 opj_j2k_tcp_data_destroy(l_tcp);
8069 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8070 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8072 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8073 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8074 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8078 opj_read_bytes(l_data,&l_current_marker,2);
8080 if (l_current_marker == J2K_MS_EOC) {
8081 p_j2k->m_current_tile_number = 0;
8082 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8084 else if (l_current_marker != J2K_MS_SOT)
8086 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8095 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8097 OPJ_UINT32 i,j,k = 0;
8098 OPJ_UINT32 l_width_src,l_height_src;
8099 OPJ_UINT32 l_width_dest,l_height_dest;
8100 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8101 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8102 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8103 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8104 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8106 opj_image_comp_t * l_img_comp_src = 00;
8107 opj_image_comp_t * l_img_comp_dest = 00;
8109 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8110 opj_image_t * l_image_src = 00;
8111 OPJ_UINT32 l_size_comp, l_remaining;
8112 OPJ_INT32 * l_dest_ptr;
8113 opj_tcd_resolution_v2_t* l_res= 00;
8115 l_tilec = p_tcd->tcd_image->tiles->comps;
8116 l_image_src = p_tcd->image;
8117 l_img_comp_src = l_image_src->comps;
8119 l_img_comp_dest = p_output_image->comps;
8121 for (i=0; i<l_image_src->numcomps; i++) {
8123 /* Allocate output component buffer if necessary */
8124 if (!l_img_comp_dest->data) {
8126 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8127 if (! l_img_comp_dest->data) {
8132 /* Copy info from decoded comp image to output image */
8133 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8136 /* Compute the precision of the output buffer */
8137 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8138 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8139 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8145 if (l_size_comp == 3) {
8150 /* Current tile component size*/
8152 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8153 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8156 l_width_src = (l_res->x1 - l_res->x0);
8157 l_height_src = (l_res->y1 - l_res->y0);
8159 /* Border of the current output component*/
8160 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8161 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8162 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8163 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8166 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8167 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8171 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8172 * of the input buffer (decoded tile component) which will be move
8173 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8174 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8175 * by this input area.
8177 assert( l_res->x0 >= 0);
8178 assert( l_res->x1 >= 0);
8179 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8180 l_start_x_dest = l_res->x0 - l_x0_dest;
8181 l_offset_x0_src = 0;
8183 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8184 l_width_dest = l_width_src;
8185 l_offset_x1_src = 0;
8188 l_width_dest = l_x1_dest - l_res->x0 ;
8189 l_offset_x1_src = l_width_src - l_width_dest;
8193 l_start_x_dest = 0 ;
8194 l_offset_x0_src = l_x0_dest - l_res->x0;
8196 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8197 l_width_dest = l_width_src - l_offset_x0_src;
8198 l_offset_x1_src = 0;
8201 l_width_dest = l_img_comp_dest->w ;
8202 l_offset_x1_src = l_res->x1 - l_x1_dest;
8206 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8207 l_start_y_dest = l_res->y0 - l_y0_dest;
8208 l_offset_y0_src = 0;
8210 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8211 l_height_dest = l_height_src;
8212 l_offset_y1_src = 0;
8215 l_height_dest = l_y1_dest - l_res->y0 ;
8216 l_offset_y1_src = l_height_src - l_height_dest;
8220 l_start_y_dest = 0 ;
8221 l_offset_y0_src = l_y0_dest - l_res->y0;
8223 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8224 l_height_dest = l_height_src - l_offset_y0_src;
8225 l_offset_y1_src = 0;
8228 l_height_dest = l_img_comp_dest->h ;
8229 l_offset_y1_src = l_res->y1 - l_y1_dest;
8233 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8238 /* Compute the input buffer offset */
8239 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8240 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8241 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8243 /* Compute the output buffer offset */
8244 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8245 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8247 /* Move the output buffer to the first place where we will write*/
8248 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8251 fprintf(stdout, "COMPO[%d]:\n",i);
8252 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8253 "\t tile offset:%d, %d, %d, %d\n"
8254 "\t buffer offset: %d; %d, %d\n",
8255 l_res->x0, l_res->y0, l_width_src, l_height_src,
8256 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8257 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8259 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8260 "\t start offset: %d, line offset= %d\n",
8261 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8265 switch (l_size_comp) {
8268 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8269 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8271 if (l_img_comp_src->sgnd) {
8272 for (j = 0 ; j < l_height_dest ; ++j) {
8273 for ( k = 0 ; k < l_width_dest ; ++k) {
8274 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8277 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8278 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8282 for ( j = 0 ; j < l_height_dest ; ++j ) {
8283 for ( k = 0 ; k < l_width_dest ; ++k) {
8284 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8287 l_dest_ptr+= l_line_offset_dest;
8288 l_src_ptr += l_line_offset_src;
8292 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8293 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8298 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8299 l_src_ptr += l_start_offset_src;
8301 if (l_img_comp_src->sgnd) {
8302 for (j=0;j<l_height_dest;++j) {
8303 for (k=0;k<l_width_dest;++k) {
8304 *(l_dest_ptr++) = *(l_src_ptr++);
8307 l_dest_ptr+= l_line_offset_dest;
8308 l_src_ptr += l_line_offset_src ;
8312 for (j=0;j<l_height_dest;++j) {
8313 for (k=0;k<l_width_dest;++k) {
8314 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8317 l_dest_ptr+= l_line_offset_dest;
8318 l_src_ptr += l_line_offset_src ;
8322 l_src_ptr += l_end_offset_src;
8323 p_data = (OPJ_BYTE*) l_src_ptr;
8328 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8329 l_src_ptr += l_start_offset_src;
8331 for (j=0;j<l_height_dest;++j) {
8332 for (k=0;k<l_width_dest;++k) {
8333 *(l_dest_ptr++) = (*(l_src_ptr++));
8336 l_dest_ptr+= l_line_offset_dest;
8337 l_src_ptr += l_line_offset_src ;
8340 l_src_ptr += l_end_offset_src;
8341 p_data = (OPJ_BYTE*) l_src_ptr;
8355 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8357 * @param p_j2k the jpeg2000 codec.
8358 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8359 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8360 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8361 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8362 * @param p_manager the user event manager
8364 * @return true if the area could be set.
8366 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8367 opj_image_t* p_image,
8368 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8369 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8370 opj_event_mgr_t * p_manager )
8372 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8373 opj_image_t * l_image = p_j2k->m_private_image;
8376 OPJ_INT32 l_comp_x1, l_comp_y1;
8377 opj_image_comp_t* l_img_comp = NULL;
8379 /* Check if we are read the main header */
8380 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8381 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8385 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8386 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8388 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8389 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8390 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8391 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8397 /* Check if the positions provided by the user are correct */
8400 assert(p_start_x >= 0 );
8401 assert(p_start_y >= 0 );
8403 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8404 opj_event_msg_v2(p_manager, EVT_ERROR,
8405 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8406 p_start_x, l_image->x1);
8409 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8410 opj_event_msg_v2(p_manager, EVT_WARNING,
8411 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8412 p_start_x, l_image->x0);
8413 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8414 p_image->x0 = l_image->x0;
8417 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8418 p_image->x0 = p_start_x;
8422 if ((OPJ_UINT32)p_start_y > l_image->y1){
8423 opj_event_msg_v2(p_manager, EVT_ERROR,
8424 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8425 p_start_y, l_image->y1);
8428 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8429 opj_event_msg_v2(p_manager, EVT_WARNING,
8430 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8431 p_start_y, l_image->y0);
8432 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8433 p_image->y0 = l_image->y0;
8436 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8437 p_image->y0 = p_start_y;
8441 assert((OPJ_UINT32)p_end_x > 0);
8442 assert((OPJ_UINT32)p_end_y > 0);
8443 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8444 opj_event_msg_v2(p_manager, EVT_ERROR,
8445 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8446 p_end_x, l_image->x0);
8449 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8450 opj_event_msg_v2(p_manager, EVT_WARNING,
8451 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8452 p_end_x, l_image->x1);
8453 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8454 p_image->x1 = l_image->x1;
8457 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8458 p_image->x1 = p_end_x;
8462 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8463 opj_event_msg_v2(p_manager, EVT_ERROR,
8464 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8465 p_end_y, l_image->y0);
8468 if ((OPJ_UINT32)p_end_y > l_image->y1){
8469 opj_event_msg_v2(p_manager, EVT_WARNING,
8470 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8471 p_end_y, l_image->y1);
8472 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8473 p_image->y1 = l_image->y1;
8476 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8477 p_image->y1 = p_end_y;
8481 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8483 l_img_comp = p_image->comps;
8484 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8488 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8489 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8490 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8491 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8493 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8494 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8496 opj_event_msg_v2(p_manager, EVT_ERROR,
8497 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8501 l_img_comp->w = l_w;
8503 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8504 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8506 opj_event_msg_v2(p_manager, EVT_ERROR,
8507 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8511 l_img_comp->h = l_h;
8516 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8517 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8524 /* ----------------------------------------------------------------------- */
8525 /* J2K / JPT decoder interface */
8526 /* ----------------------------------------------------------------------- */
8528 * Creates a J2K decompression structure.
8530 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8532 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8534 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8538 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8540 l_j2k->m_is_decoder = 1;
8541 l_j2k->m_cp.m_is_decoder = 1;
8543 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8544 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8545 opj_j2k_destroy(l_j2k);
8548 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8550 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8551 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8552 opj_j2k_destroy(l_j2k);
8556 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8558 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8560 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8562 /* codestream index creation */
8563 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8565 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8566 if (!l_j2k->cstr_index){
8567 opj_j2k_destroy(l_j2k);
8571 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8574 /* validation list creation */
8575 l_j2k->m_validation_list = opj_procedure_list_create();
8576 if (! l_j2k->m_validation_list) {
8577 opj_j2k_destroy(l_j2k);
8581 /* execution list creation */
8582 l_j2k->m_procedure_list = opj_procedure_list_create();
8583 if (! l_j2k->m_procedure_list) {
8584 opj_j2k_destroy(l_j2k);
8592 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8594 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8595 opj_calloc(1,sizeof(opj_codestream_index_t));
8599 cstr_index->maxmarknum = 100;
8600 cstr_index->marknum = 0;
8601 cstr_index->marker = (opj_marker_info_t*)
8602 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8603 if (!cstr_index-> marker)
8606 cstr_index->tile_index = NULL;
8613 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8615 * @param p_tile_no the tile index.
8616 * @param p_comp_no the component being outputted.
8617 * @param p_j2k the J2K codec.
8619 * @return the number of bytes taken by the SPCod element.
8621 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8622 OPJ_UINT32 p_tile_no,
8623 OPJ_UINT32 p_comp_no )
8625 opj_cp_v2_t *l_cp = 00;
8626 opj_tcp_v2_t *l_tcp = 00;
8627 opj_tccp_t *l_tccp = 00;
8630 assert(p_j2k != 00);
8632 l_cp = &(p_j2k->m_cp);
8633 l_tcp = &l_cp->tcps[p_tile_no];
8634 l_tccp = &l_tcp->tccps[p_comp_no];
8636 /* preconditions again */
8637 assert(p_tile_no < (l_cp->tw * l_cp->th));
8638 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8640 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8641 return 5 + l_tccp->numresolutions;
8649 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8651 * @param p_comp_no the component number to output.
8652 * @param p_stream the stream to write data to.
8653 * @param p_j2k J2K codec.
8654 * @param p_manager the user event manager.
8657 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8658 OPJ_UINT32 p_tile_no,
8659 OPJ_UINT32 p_comp_no,
8661 OPJ_UINT32 * p_header_size,
8662 struct opj_event_mgr * p_manager )
8665 opj_cp_v2_t *l_cp = 00;
8666 opj_tcp_v2_t *l_tcp = 00;
8667 opj_tccp_t *l_tccp = 00;
8670 assert(p_j2k != 00);
8671 assert(p_header_size != 00);
8672 assert(p_manager != 00);
8673 assert(p_data != 00);
8675 l_cp = &(p_j2k->m_cp);
8676 l_tcp = &l_cp->tcps[p_tile_no];
8677 l_tccp = &l_tcp->tccps[p_comp_no];
8679 /* preconditions again */
8680 assert(p_tile_no < (l_cp->tw * l_cp->th));
8681 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8683 if (*p_header_size < 5) {
8684 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8688 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8691 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8694 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8697 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8700 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8703 *p_header_size = *p_header_size - 5;
8705 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8707 if (*p_header_size < l_tccp->numresolutions) {
8708 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8712 for (i = 0; i < l_tccp->numresolutions; ++i) {
8713 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8717 *p_header_size = *p_header_size - l_tccp->numresolutions;
8724 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8725 * @param p_header_data the data contained in the COM box.
8726 * @param p_j2k the jpeg2000 codec.
8727 * @param p_header_size the size of the data contained in the COM marker.
8728 * @param p_manager the user event manager.
8730 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8732 OPJ_BYTE * p_header_data,
8733 OPJ_UINT32 * p_header_size,
8734 opj_event_mgr_t * p_manager)
8736 OPJ_UINT32 i, l_tmp;
8737 opj_cp_v2_t *l_cp = NULL;
8738 opj_tcp_v2_t *l_tcp = NULL;
8739 opj_tccp_t *l_tccp = NULL;
8740 OPJ_BYTE * l_current_ptr = NULL;
8743 assert(p_j2k != 00);
8744 assert(p_manager != 00);
8745 assert(p_header_data != 00);
8747 l_cp = &(p_j2k->m_cp);
8748 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8749 &l_cp->tcps[p_j2k->m_current_tile_number] :
8750 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8752 /* precondition again */
8753 assert(compno < p_j2k->m_private_image->numcomps);
8755 l_tccp = &l_tcp->tccps[compno];
8756 l_current_ptr = p_header_data;
8758 /* make sure room is sufficient */
8759 if (*p_header_size < 5) {
8760 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8764 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8765 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8768 /* If user wants to remove more resolutions than the codestream contains, return error */
8769 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8770 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8771 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8772 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8776 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8780 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8784 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8787 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8790 *p_header_size = *p_header_size - 5;
8792 /* use custom precinct size ? */
8793 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8794 if (*p_header_size < l_tccp->numresolutions) {
8795 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8799 for (i = 0; i < l_tccp->numresolutions; ++i) {
8800 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8802 l_tccp->prcw[i] = l_tmp & 0xf;
8803 l_tccp->prch[i] = l_tmp >> 4;
8806 *p_header_size = *p_header_size - l_tccp->numresolutions;
8809 /* set default size for the precinct width and height */
8810 for (i = 0; i < l_tccp->numresolutions; ++i) {
8811 l_tccp->prcw[i] = 15;
8812 l_tccp->prch[i] = 15;
8816 #ifdef WIP_REMOVE_MSD
8818 if (p_j2k->cstr_info && compno == 0) {
8819 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8821 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8822 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8823 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8824 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8825 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8828 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8829 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8838 * Copies the tile component parameters of all the component from the first tile component.
8840 * @param p_j2k the J2k codec.
8842 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8846 opj_cp_v2_t *l_cp = NULL;
8847 opj_tcp_v2_t *l_tcp = NULL;
8848 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8849 OPJ_UINT32 l_prc_size;
8852 assert(p_j2k != 00);
8854 l_cp = &(p_j2k->m_cp);
8855 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8856 &l_cp->tcps[p_j2k->m_current_tile_number] :
8857 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8859 l_ref_tccp = &l_tcp->tccps[0];
8860 l_copied_tccp = l_ref_tccp + 1;
8861 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8863 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8864 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8865 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8866 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8867 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8868 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8869 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8870 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8876 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
8878 * @param p_tile_no the tile index.
8879 * @param p_comp_no the component being outputted.
8880 * @param p_j2k the J2K codec.
8882 * @return the number of bytes taken by the SPCod element.
8884 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
8885 OPJ_UINT32 p_tile_no,
8886 OPJ_UINT32 p_comp_no )
8888 OPJ_UINT32 l_num_bands;
8890 opj_cp_v2_t *l_cp = 00;
8891 opj_tcp_v2_t *l_tcp = 00;
8892 opj_tccp_t *l_tccp = 00;
8895 assert(p_j2k != 00);
8897 l_cp = &(p_j2k->m_cp);
8898 l_tcp = &l_cp->tcps[p_tile_no];
8899 l_tccp = &l_tcp->tccps[p_comp_no];
8901 /* preconditions again */
8902 assert(p_tile_no < l_cp->tw * l_cp->th);
8903 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8905 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8907 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8908 return 1 + l_num_bands;
8911 return 1 + 2*l_num_bands;
8916 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
8918 * @param p_tile_no the tile to output.
8919 * @param p_comp_no the component number to output.
8920 * @param p_data the data buffer.
8921 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
8922 * @param p_j2k J2K codec.
8923 * @param p_manager the user event manager.
8926 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
8927 OPJ_UINT32 p_tile_no,
8928 OPJ_UINT32 p_comp_no,
8930 OPJ_UINT32 * p_header_size,
8931 struct opj_event_mgr * p_manager )
8933 OPJ_UINT32 l_header_size;
8934 OPJ_UINT32 l_band_no, l_num_bands;
8935 OPJ_UINT32 l_expn,l_mant;
8937 opj_cp_v2_t *l_cp = 00;
8938 opj_tcp_v2_t *l_tcp = 00;
8939 opj_tccp_t *l_tccp = 00;
8942 assert(p_j2k != 00);
8943 assert(p_header_size != 00);
8944 assert(p_manager != 00);
8945 assert(p_data != 00);
8947 l_cp = &(p_j2k->m_cp);
8948 l_tcp = &l_cp->tcps[p_tile_no];
8949 l_tccp = &l_tcp->tccps[p_comp_no];
8951 /* preconditions again */
8952 assert(p_tile_no < l_cp->tw * l_cp->th);
8953 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8955 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8957 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8958 l_header_size = 1 + l_num_bands;
8960 if (*p_header_size < l_header_size) {
8961 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8965 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8968 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8969 l_expn = l_tccp->stepsizes[l_band_no].expn;
8970 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8975 l_header_size = 1 + 2*l_num_bands;
8977 if (*p_header_size < l_header_size) {
8978 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8982 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8985 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8986 l_expn = l_tccp->stepsizes[l_band_no].expn;
8987 l_mant = l_tccp->stepsizes[l_band_no].mant;
8989 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
8994 *p_header_size = *p_header_size - l_header_size;
9000 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9002 * @param p_comp_no the component being targeted.
9003 * @param p_header_data the data contained in the COM box.
9004 * @param p_j2k the jpeg2000 codec.
9005 * @param p_header_size the size of the data contained in the COM marker.
9006 * @param p_manager the user event manager.
9008 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9009 OPJ_UINT32 p_comp_no,
9010 OPJ_BYTE* p_header_data,
9011 OPJ_UINT32 * p_header_size,
9012 opj_event_mgr_t * p_manager
9016 OPJ_UINT32 l_band_no;
9017 opj_cp_v2_t *l_cp = 00;
9018 opj_tcp_v2_t *l_tcp = 00;
9019 opj_tccp_t *l_tccp = 00;
9020 OPJ_BYTE * l_current_ptr = 00;
9021 OPJ_UINT32 l_tmp, l_num_band;
9024 assert(p_j2k != 00);
9025 assert(p_manager != 00);
9026 assert(p_header_data != 00);
9028 l_cp = &(p_j2k->m_cp);
9029 /* come from tile part header or main header ?*/
9030 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9031 &l_cp->tcps[p_j2k->m_current_tile_number] :
9032 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9034 /* precondition again*/
9035 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9037 l_tccp = &l_tcp->tccps[p_comp_no];
9038 l_current_ptr = p_header_data;
9040 if (*p_header_size < 1) {
9041 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9044 *p_header_size -= 1;
9046 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9049 l_tccp->qntsty = l_tmp & 0x1f;
9050 l_tccp->numgbits = l_tmp >> 5;
9051 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9055 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9057 (*p_header_size) / 2;
9059 if( l_num_band > J2K_MAXBANDS ) {
9060 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9061 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9062 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9063 /*return OPJ_FALSE;*/
9068 if (l_cp->correct) {
9070 /* if JPWL is on, we check whether there are too many subbands */
9071 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9072 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9073 "JPWL: bad number of subbands in Sqcx (%d)\n",
9076 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9079 /* we try to correct */
9081 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9082 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9087 #endif /* USE_JPWL */
9089 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9090 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9091 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9093 if (l_band_no < J2K_MAXBANDS){
9094 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9095 l_tccp->stepsizes[l_band_no].mant = 0;
9098 *p_header_size = *p_header_size - l_num_band;
9101 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9102 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9104 if (l_band_no < J2K_MAXBANDS){
9105 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9106 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9109 *p_header_size = *p_header_size - 2*l_num_band;
9112 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9113 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9114 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9115 l_tccp->stepsizes[l_band_no].expn =
9116 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9117 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9118 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9126 * Copies the tile component parameters of all the component from the first tile component.
9128 * @param p_j2k the J2k codec.
9130 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9133 opj_cp_v2_t *l_cp = NULL;
9134 opj_tcp_v2_t *l_tcp = NULL;
9135 opj_tccp_t *l_ref_tccp = NULL;
9136 opj_tccp_t *l_copied_tccp = NULL;
9140 assert(p_j2k != 00);
9142 l_cp = &(p_j2k->m_cp);
9143 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9144 &l_cp->tcps[p_j2k->m_current_tile_number] :
9145 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9147 l_ref_tccp = &l_tcp->tccps[0];
9148 l_copied_tccp = l_ref_tccp + 1;
9149 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9151 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9152 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9153 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9154 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9160 * Dump some elements from the J2K decompression structure .
9162 *@param p_j2k the jpeg2000 codec.
9163 *@param flag flag to describe what elments are dump.
9164 *@param out_stream output stream where dump the elements.
9167 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9169 /* Check if the flag is compatible with j2k file*/
9170 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9171 fprintf(out_stream, "Wrong flag\n");
9175 /* Dump the image_header */
9176 if (flag & OPJ_IMG_INFO){
9177 if (p_j2k->m_private_image)
9178 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9181 /* Dump the codestream info from main header */
9182 if (flag & OPJ_J2K_MH_INFO){
9183 opj_j2k_dump_MH_info(p_j2k, out_stream);
9187 /* Dump the codestream info of the current tile */
9188 if (flag & OPJ_J2K_TH_INFO){
9192 /* Dump the codestream index from main header */
9193 if (flag & OPJ_J2K_MH_IND){
9194 opj_j2k_dump_MH_index(p_j2k, out_stream);
9197 /* Dump the codestream index of the current tile */
9198 if (flag & OPJ_J2K_TH_IND){
9205 * Dump index elements of the codestream extract from the main header.
9207 *@param p_j2k the jpeg2000 codec.
9208 *@param out_stream output stream where dump the elements.
9211 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9213 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9214 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9216 fprintf(out_stream, "Codestream index from main header: {\n");
9218 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9219 "\t Main header end position=%" PRIi64 "\n",
9220 cstr_index->main_head_start, cstr_index->main_head_end);
9222 fprintf(out_stream, "\t Marker list: {\n");
9224 if (cstr_index->marker){
9225 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9226 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9227 cstr_index->marker[it_marker].type,
9228 cstr_index->marker[it_marker].pos,
9229 cstr_index->marker[it_marker].len );
9233 fprintf(out_stream, "\t }\n");
9236 if (cstr_index->tile_index){
9238 /* Simple test to avoid to write empty information*/
9239 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9240 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9241 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9244 if (l_acc_nb_of_tile_part)
9246 fprintf(out_stream, "\t Tile index: {\n");
9248 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9249 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9251 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9253 if (cstr_index->tile_index[it_tile].tp_index){
9254 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9255 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9257 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9258 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9259 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9263 if (cstr_index->tile_index[it_tile].marker){
9264 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9265 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9266 cstr_index->tile_index[it_tile].marker[it_marker].type,
9267 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9268 cstr_index->tile_index[it_tile].marker[it_marker].len );
9272 fprintf(out_stream,"\t }\n");
9276 fprintf(out_stream,"}\n");
9281 * Dump info elements of the codestream extract from the main header.
9283 *@param p_j2k the jpeg2000 codec.
9284 *@param out_stream output stream where dump the elements.
9287 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9289 opj_tcp_v2_t * l_default_tile=NULL;
9291 fprintf(out_stream, "Codestream info from main header: {\n");
9293 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9294 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9295 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9297 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9301 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9303 fprintf(out_stream, "\t default tile {\n");
9304 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9305 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9306 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9307 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9309 for (compno = 0; compno < numcomps; compno++) {
9310 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9312 OPJ_INT32 bandno, numbands;
9315 fprintf(out_stream, "\t\t comp %d {\n", compno);
9316 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9317 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9318 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9319 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9320 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9321 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9323 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9324 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9325 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9327 fprintf(out_stream, "\n");
9329 /* quantization style*/
9330 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9331 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9332 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9333 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9334 for (bandno = 0; bandno < numbands; bandno++) {
9335 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9336 l_tccp->stepsizes[bandno].expn);
9338 fprintf(out_stream, "\n");
9341 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9343 fprintf(out_stream, "\t\t }\n");
9344 } /*end of component of default tile*/
9345 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9349 fprintf(out_stream, "}\n");
9354 * Dump an image header structure.
9356 *@param img_header the image header to dump.
9357 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9358 *@param out_stream output stream where dump the elements.
9360 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9365 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9369 fprintf(out_stream, "Image info {\n");
9370 tab[0] = '\t';tab[1] = '\0';
9373 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9374 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9375 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9377 if (img_header->comps){
9379 for (compno = 0; compno < img_header->numcomps; compno++) {
9380 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9381 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9382 fprintf(out_stream,"%s}\n",tab);
9386 fprintf(out_stream, "}\n");
9390 * Dump a component image header structure.
9392 *@param comp_header the component image header to dump.
9393 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9394 *@param out_stream output stream where dump the elements.
9396 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9401 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9404 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9407 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9408 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9409 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9412 fprintf(out_stream, "}\n");
9417 * Get the codestream info from a JPEG2000 codec.
9419 *@param p_j2k the component image header to dump.
9421 *@return the codestream information extract from the jpg2000 codec
9423 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9426 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9427 opj_tcp_v2_t *l_default_tile;
9428 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9430 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9432 cstr_info->tx0 = p_j2k->m_cp.tx0;
9433 cstr_info->ty0 = p_j2k->m_cp.ty0;
9434 cstr_info->tdx = p_j2k->m_cp.tdx;
9435 cstr_info->tdy = p_j2k->m_cp.tdy;
9436 cstr_info->tw = p_j2k->m_cp.tw;
9437 cstr_info->th = p_j2k->m_cp.th;
9439 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9441 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9443 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9444 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9445 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9446 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9448 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9450 for (compno = 0; compno < numcomps; compno++) {
9451 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9452 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9453 OPJ_INT32 bandno, numbands;
9456 l_tccp_info->csty = l_tccp->csty;
9457 l_tccp_info->numresolutions = l_tccp->numresolutions;
9458 l_tccp_info->cblkw = l_tccp->cblkw;
9459 l_tccp_info->cblkh = l_tccp->cblkh;
9460 l_tccp_info->cblksty = l_tccp->cblksty;
9461 l_tccp_info->qmfbid = l_tccp->qmfbid;
9462 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9464 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9465 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9468 /* quantization style*/
9469 l_tccp_info->qntsty = l_tccp->qntsty;
9470 l_tccp_info->numgbits = l_tccp->numgbits;
9472 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9473 if (numbands < J2K_MAXBANDS) {
9474 for (bandno = 0; bandno < numbands; bandno++) {
9475 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9476 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9481 l_tccp_info->roishift = l_tccp->roishift;
9489 * Get the codestream index from a JPEG2000 codec.
9491 *@param p_j2k the component image header to dump.
9493 *@return the codestream index extract from the jpg2000 codec
9495 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9497 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9498 opj_calloc(1,sizeof(opj_codestream_index_t));
9502 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9503 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9504 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9506 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9507 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9508 if (!l_cstr_index->marker){
9509 opj_free( l_cstr_index);
9513 if (p_j2k->cstr_index->marker)
9514 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9516 opj_free(l_cstr_index->marker);
9517 l_cstr_index->marker = NULL;
9520 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9521 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9522 if (!l_cstr_index->tile_index){
9523 opj_free( l_cstr_index->marker);
9524 opj_free( l_cstr_index);
9528 if (!p_j2k->cstr_index->tile_index){
9529 opj_free(l_cstr_index->tile_index);
9530 l_cstr_index->tile_index = NULL;
9533 OPJ_UINT32 it_tile = 0;
9534 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9537 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9539 l_cstr_index->tile_index[it_tile].marker =
9540 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9542 if (!l_cstr_index->tile_index[it_tile].marker) {
9543 OPJ_UINT32 it_tile_free;
9545 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9546 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9549 opj_free( l_cstr_index->tile_index);
9550 opj_free( l_cstr_index->marker);
9551 opj_free( l_cstr_index);
9555 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9556 memcpy( l_cstr_index->tile_index[it_tile].marker,
9557 p_j2k->cstr_index->tile_index[it_tile].marker,
9558 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9560 opj_free(l_cstr_index->tile_index[it_tile].marker);
9561 l_cstr_index->tile_index[it_tile].marker = NULL;
9564 /* Tile part index*/
9565 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9567 l_cstr_index->tile_index[it_tile].tp_index =
9568 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9570 if(!l_cstr_index->tile_index[it_tile].tp_index){
9571 OPJ_UINT32 it_tile_free;
9573 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9574 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9575 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9578 opj_free( l_cstr_index->tile_index);
9579 opj_free( l_cstr_index->marker);
9580 opj_free( l_cstr_index);
9584 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9585 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9586 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9587 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9590 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9591 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9594 /* Packet index (NOT USED)*/
9595 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9596 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9601 return l_cstr_index;
9604 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9606 OPJ_UINT32 it_tile=0;
9608 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9609 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9610 if (!p_j2k->cstr_index->tile_index)
9613 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9614 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9615 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9616 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9617 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9618 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9628 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9629 opj_stream_private_t *p_stream,
9630 opj_event_mgr_t * p_manager)
9632 opj_bool l_go_on = OPJ_TRUE;
9633 OPJ_UINT32 l_current_tile_no;
9634 OPJ_UINT32 l_data_size,l_max_data_size;
9635 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9636 OPJ_UINT32 l_nb_comps;
9637 OPJ_BYTE * l_current_data;
9639 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9640 if (! l_current_data) {
9643 l_max_data_size = 1000;
9648 if (! opj_j2k_read_tile_header( p_j2k,
9651 &l_tile_x0, &l_tile_y0,
9652 &l_tile_x1, &l_tile_y1,
9657 opj_free(l_current_data);
9665 if (l_data_size > l_max_data_size) {
9666 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9667 if (! l_current_data) {
9668 opj_free(l_current_data);
9672 l_max_data_size = l_data_size;
9675 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9676 opj_free(l_current_data);
9679 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);
9681 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9682 opj_free(l_current_data);
9685 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9689 opj_free(l_current_data);
9695 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9697 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9700 assert(p_j2k != 00);
9702 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9703 /* DEVELOPER CORNER, add your custom procedures */
9708 * Read and decode one tile.
9710 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9711 opj_stream_private_t *p_stream,
9712 opj_event_mgr_t * p_manager)
9714 opj_bool l_go_on = OPJ_TRUE;
9715 OPJ_UINT32 l_current_tile_no;
9716 OPJ_UINT32 l_tile_no_to_dec;
9717 OPJ_UINT32 l_data_size,l_max_data_size;
9718 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9719 OPJ_UINT32 l_nb_comps;
9720 OPJ_BYTE * l_current_data;
9722 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9723 if (! l_current_data) {
9726 l_max_data_size = 1000;
9728 /*Allocate and initialize some elements of codestrem index if not already done*/
9729 if( !p_j2k->cstr_index->tile_index)
9731 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9732 opj_free(l_current_data);
9736 /* Move into the codestream to the first SOT used to decode the desired tile */
9737 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9738 if (p_j2k->cstr_index->tile_index)
9739 if(p_j2k->cstr_index->tile_index->tp_index)
9741 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9742 /* the index for this tile has not been built,
9743 * so move to the last SOT read */
9744 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9745 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9750 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)) ) {
9751 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9755 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9756 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9757 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9761 if (! opj_j2k_read_tile_header( p_j2k,
9764 &l_tile_x0, &l_tile_y0,
9765 &l_tile_x1, &l_tile_y1,
9770 opj_free(l_current_data);
9779 if (l_data_size > l_max_data_size) {
9780 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9781 if (! l_current_data) {
9782 opj_free(l_current_data);
9786 l_max_data_size = l_data_size;
9791 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9792 opj_free(l_current_data);
9795 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);
9797 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9798 opj_free(l_current_data);
9801 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9803 if(l_current_tile_no == l_tile_no_to_dec)
9805 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9806 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9807 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9813 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);
9818 opj_free(l_current_data);
9825 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9827 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9830 assert(p_j2k != 00);
9832 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9833 /* DEVELOPER CORNER, add your custom procedures */
9839 * Decodes the tiles of the stream.
9841 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9842 opj_stream_private_t * p_stream,
9843 opj_image_t * p_image,
9844 opj_event_mgr_t * p_manager)
9851 p_j2k->m_output_image = opj_image_create0();
9852 if (! (p_j2k->m_output_image)) {
9855 opj_copy_image_header(p_image, p_j2k->m_output_image);
9857 /* customization of the decoding */
9858 opj_j2k_setup_decoding(p_j2k);
9860 /* Decode the codestream */
9861 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9862 opj_image_destroy(p_j2k->m_private_image);
9863 p_j2k->m_private_image = NULL;
9867 /* Move data and copy one information from codec to output image*/
9868 for (compno = 0; compno < p_image->numcomps; compno++) {
9869 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9870 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9871 p_j2k->m_output_image->comps[compno].data = NULL;
9879 * Get the decoded tile.
9881 * @param p_j2k the jpeg2000 codestream codec.
9882 * @param p_stream input_stream
9883 * @param p_image output image. .
9884 * @param p_manager the user event manager
9885 * @param tile_index index of the tile we want decode
9887 * @return true if succeed.
9889 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
9890 opj_stream_private_t *p_stream,
9891 opj_image_t* p_image,
9892 opj_event_mgr_t * p_manager,
9893 OPJ_UINT32 tile_index )
9896 OPJ_UINT32 l_tile_x, l_tile_y;
9897 opj_image_comp_t* l_img_comp;
9900 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
9904 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9905 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);
9909 /* Compute the dimension of the desired tile*/
9910 l_tile_x = tile_index % p_j2k->m_cp.tw;
9911 l_tile_y = tile_index / p_j2k->m_cp.tw;
9913 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9914 if (p_image->x0 < p_j2k->m_private_image->x0)
9915 p_image->x0 = p_j2k->m_private_image->x0;
9916 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9917 if (p_image->x1 > p_j2k->m_private_image->x1)
9918 p_image->x1 = p_j2k->m_private_image->x1;
9920 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9921 if (p_image->y0 < p_j2k->m_private_image->y0)
9922 p_image->y0 = p_j2k->m_private_image->y0;
9923 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9924 if (p_image->y1 > p_j2k->m_private_image->y1)
9925 p_image->y1 = p_j2k->m_private_image->y1;
9927 l_img_comp = p_image->comps;
9928 for (compno=0; compno < p_image->numcomps; ++compno)
9930 OPJ_INT32 l_comp_x1, l_comp_y1;
9932 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9934 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9935 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9936 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9937 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9939 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9940 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9945 /* Destroy the previous output image*/
9946 if (p_j2k->m_output_image)
9947 opj_image_destroy(p_j2k->m_output_image);
9949 /* Create the ouput image from the information previously computed*/
9950 p_j2k->m_output_image = opj_image_create0();
9951 if (! (p_j2k->m_output_image)) {
9954 opj_copy_image_header(p_image, p_j2k->m_output_image);
9956 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9958 /* customization of the decoding */
9959 opj_j2k_setup_decoding_tile(p_j2k);
9961 /* Decode the codestream */
9962 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9963 opj_image_destroy(p_j2k->m_private_image);
9964 p_j2k->m_private_image = NULL;
9968 /* Move data and copy one information from codec to output image*/
9969 for (compno = 0; compno < p_image->numcomps; compno++) {
9970 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9972 if (p_image->comps[compno].data)
9973 opj_free(p_image->comps[compno].data);
9975 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9977 p_j2k->m_output_image->comps[compno].data = NULL;
9983 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
9984 OPJ_UINT32 res_factor,
9985 opj_event_mgr_t * p_manager)
9989 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9991 if (p_j2k->m_private_image) {
9992 if (p_j2k->m_private_image->comps) {
9993 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9994 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9995 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9996 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9997 if ( res_factor >= max_res){
9998 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10001 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10014 * Encodes all the tiles in a row.
10016 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10017 opj_stream_private_t *p_stream,
10018 opj_event_mgr_t * p_manager )
10021 OPJ_UINT32 l_nb_tiles;
10022 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10023 OPJ_BYTE * l_current_data;
10025 /* preconditions */
10026 assert(p_j2k != 00);
10027 assert(p_stream != 00);
10028 assert(p_manager != 00);
10030 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10031 if (! l_current_data) {
10034 l_max_tile_size = 1000;
10036 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10037 for (i=0;i<l_nb_tiles;++i) {
10038 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10039 opj_free(l_current_data);
10043 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10044 if (l_current_tile_size > l_max_tile_size) {
10045 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10046 if (! l_current_data) {
10049 l_max_tile_size = l_current_tile_size;
10052 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10054 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10059 opj_free(l_current_data);
10064 * Ends the compression procedures and possibility add data to be read after the
10067 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10068 opj_stream_private_t *p_stream,
10069 opj_event_mgr_t * p_manager)
10071 /* customization of the encoding */
10072 opj_j2k_setup_end_compress(p_j2k);
10074 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10084 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10086 * @param p_j2k the jpeg2000 codec.
10087 * @param p_stream the stream object.
10088 * @param p_manager the user event manager.
10090 * @return true if the codec is valid.
10092 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10093 opj_stream_private_t *p_stream,
10094 opj_image_t * p_image,
10095 opj_event_mgr_t * p_manager)
10097 /* preconditions */
10098 assert(p_j2k != 00);
10099 assert(p_stream != 00);
10100 assert(p_manager != 00);
10102 p_j2k->m_private_image = opj_image_create0();
10103 opj_copy_image_header(p_image, p_j2k->m_private_image);
10105 // TODO_MSD: Find a better way
10106 if (p_image->comps) {
10107 OPJ_UINT32 it_comp;
10108 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10109 if (p_image->comps[it_comp].data) {
10110 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10111 p_image->comps[it_comp].data = NULL;
10117 /* customization of the validation */
10118 opj_j2k_setup_encoding_validation (p_j2k);
10120 /* validation of the parameters codec */
10121 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10125 /* customization of the encoding */
10126 opj_j2k_setup_header_writting(p_j2k);
10129 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10139 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10140 OPJ_UINT32 p_tile_index,
10141 opj_stream_private_t *p_stream,
10142 opj_event_mgr_t * p_manager )
10145 if (p_tile_index != p_j2k->m_current_tile_number) {
10146 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10150 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);
10152 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10153 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10154 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10156 /* initialisation before tile encoding */
10157 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10167 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10169 OPJ_UINT32 i,j,k = 0;
10170 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10171 opj_image_comp_t * l_img_comp = 00;
10172 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10173 opj_image_t * l_image = 00;
10174 OPJ_UINT32 l_size_comp, l_remaining;
10175 OPJ_INT32 * l_src_ptr;
10176 l_tilec = p_tcd->tcd_image->tiles->comps;
10177 l_image = p_tcd->image;
10178 l_img_comp = l_image->comps;
10180 for (i=0;i<p_tcd->image->numcomps;++i) {
10181 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10182 l_remaining = l_img_comp->prec & 7; /* (%8) */
10187 if (l_size_comp == 3) {
10191 l_width = (l_tilec->x1 - l_tilec->x0);
10192 l_height = (l_tilec->y1 - l_tilec->y0);
10193 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10194 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10195 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10196 l_stride = l_image_width - l_width;
10197 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10199 switch (l_size_comp) {
10202 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10203 if (l_img_comp->sgnd) {
10204 for (j=0;j<l_height;++j) {
10205 for (k=0;k<l_width;++k) {
10206 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10210 l_src_ptr += l_stride;
10214 for (j=0;j<l_height;++j) {
10215 for (k=0;k<l_width;++k) {
10216 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10220 l_src_ptr += l_stride;
10224 p_data = (OPJ_BYTE*) l_dest_ptr;
10229 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10230 if (l_img_comp->sgnd) {
10231 for (j=0;j<l_height;++j) {
10232 for (k=0;k<l_width;++k) {
10233 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10235 l_src_ptr += l_stride;
10239 for (j=0;j<l_height;++j) {
10240 for (k=0;k<l_width;++k) {
10241 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10243 l_src_ptr += l_stride;
10247 p_data = (OPJ_BYTE*) l_dest_ptr;
10252 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10253 for (j=0;j<l_height;++j) {
10254 for (k=0;k<l_width;++k) {
10255 *(l_dest_ptr++) = *(l_src_ptr++);
10257 l_src_ptr += l_stride;
10260 p_data = (OPJ_BYTE*) l_dest_ptr;
10273 * @param p_j2k the jpeg2000 codec.
10274 * @param p_stream the stream to write data to.
10275 * @param p_manager the user event manager.
10277 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10279 OPJ_UINT32 p_data_size,
10280 opj_stream_private_t *p_stream,
10281 opj_event_mgr_t * p_manager )
10283 opj_tcd_v2_t * l_tcd = 00;
10284 opj_cp_v2_t * l_cp = 00;
10285 opj_tcp_v2_t * l_tcp = 00;
10286 OPJ_UINT32 l_nb_bytes_written;
10287 OPJ_BYTE * l_current_data = 00;
10288 OPJ_UINT32 l_tile_size = 0;
10289 OPJ_UINT32 l_available_data;
10291 /* preconditions */
10292 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10294 l_tcd = p_j2k->m_tcd;
10295 l_cp = &(p_j2k->m_cp);
10296 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10298 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10299 l_available_data = l_tile_size;
10300 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10302 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10303 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10307 l_nb_bytes_written = 0;
10308 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10311 l_current_data += l_nb_bytes_written;
10312 l_available_data -= l_nb_bytes_written;
10314 l_nb_bytes_written = 0;
10315 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10319 l_available_data -= l_nb_bytes_written;
10320 l_nb_bytes_written = l_tile_size - l_available_data;
10322 if ( opj_stream_write_data( p_stream,
10323 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10324 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10328 ++p_j2k->m_current_tile_number;
10335 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10336 * are valid. Developers wanting to extend the library can add their own validation procedures.
10338 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10340 /* preconditions */
10341 assert(p_j2k != 00);
10343 /* DEVELOPER CORNER, insert your custom procedures */
10344 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10346 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10347 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
10350 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
10351 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
10352 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
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_encoding_validation (opj_j2k_v2_t *p_j2k)
10361 /* preconditions */
10362 assert(p_j2k != 00);
10364 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10365 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10367 /* DEVELOPER CORNER, add your custom validation procedure */
10368 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10373 * Sets up the procedures to do on writing header.
10374 * Developers wanting to extend the library can add their own writing procedures.
10376 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10378 /* preconditions */
10379 assert(p_j2k != 00);
10381 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
10382 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10383 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10384 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10385 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10388 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10389 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
10390 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
10392 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10393 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
10397 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
10399 if (p_j2k->m_cp.comment != 00) {
10400 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10403 /* DEVELOPER CORNER, insert your custom procedures */
10404 if (p_j2k->m_cp.rsiz & MCT) {
10405 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
10407 /* End of Developer Corner */
10409 if (p_j2k->cstr_index) {
10410 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10413 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10414 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10418 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10420 OPJ_UINT32 * p_data_written,
10421 OPJ_UINT32 p_total_data_size,
10422 opj_stream_private_t *p_stream,
10423 struct opj_event_mgr * p_manager )
10426 OPJ_UINT32 l_nb_bytes_written = 0;
10427 OPJ_UINT32 l_current_nb_bytes_written;
10428 OPJ_BYTE * l_begin_data = 00;
10430 opj_tcp_v2_t *l_tcp = 00;
10431 opj_tcd_v2_t * l_tcd = 00;
10432 opj_cp_v2_t * l_cp = 00;
10434 l_tcd = p_j2k->m_tcd;
10435 l_cp = &(p_j2k->m_cp);
10436 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10438 l_tcd->cur_pino = 0;
10440 /*Get number of tile parts*/
10441 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10446 l_current_nb_bytes_written = 0;
10447 l_begin_data = p_data;
10448 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10453 l_nb_bytes_written += l_current_nb_bytes_written;
10454 p_data += l_current_nb_bytes_written;
10455 p_total_data_size -= l_current_nb_bytes_written;
10457 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10458 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10459 l_current_nb_bytes_written = 0;
10460 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10461 l_nb_bytes_written += l_current_nb_bytes_written;
10462 p_data += l_current_nb_bytes_written;
10463 p_total_data_size -= l_current_nb_bytes_written;
10465 l_current_nb_bytes_written = 0;
10466 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10467 l_nb_bytes_written += l_current_nb_bytes_written;
10468 p_data += l_current_nb_bytes_written;
10469 p_total_data_size -= l_current_nb_bytes_written;
10472 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10473 l_current_nb_bytes_written = 0;
10474 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10475 l_nb_bytes_written += l_current_nb_bytes_written;
10476 p_data += l_current_nb_bytes_written;
10477 p_total_data_size -= l_current_nb_bytes_written;
10481 l_current_nb_bytes_written = 0;
10482 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10486 l_nb_bytes_written += l_current_nb_bytes_written;
10487 * p_data_written = l_nb_bytes_written;
10489 /* Writing Psot in SOT marker */
10490 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10492 if (l_cp->m_specific_param.m_enc.m_cinema){
10493 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10499 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10501 OPJ_UINT32 * p_data_written,
10502 OPJ_UINT32 p_total_data_size,
10503 opj_stream_private_t *p_stream,
10504 struct opj_event_mgr * p_manager
10507 OPJ_UINT32 tilepartno=0;
10508 OPJ_UINT32 l_nb_bytes_written = 0;
10509 OPJ_UINT32 l_current_nb_bytes_written;
10510 OPJ_UINT32 l_part_tile_size;
10511 OPJ_UINT32 tot_num_tp;
10514 OPJ_BYTE * l_begin_data;
10515 opj_tcp_v2_t *l_tcp = 00;
10516 opj_tcd_v2_t * l_tcd = 00;
10517 opj_cp_v2_t * l_cp = 00;
10520 l_tcd = p_j2k->m_tcd;
10521 l_cp = &(p_j2k->m_cp);
10522 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10524 /*Get number of tile parts*/
10525 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
10527 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10528 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10529 l_current_nb_bytes_written = 0;
10530 l_part_tile_size = 0;
10531 l_begin_data = p_data;
10533 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10537 l_nb_bytes_written += l_current_nb_bytes_written;
10538 p_data += l_current_nb_bytes_written;
10539 p_total_data_size -= l_current_nb_bytes_written;
10540 l_part_tile_size += l_nb_bytes_written;
10542 l_current_nb_bytes_written = 0;
10543 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10547 p_data += l_current_nb_bytes_written;
10548 l_nb_bytes_written += l_current_nb_bytes_written;
10549 p_total_data_size -= l_current_nb_bytes_written;
10550 l_part_tile_size += l_nb_bytes_written;
10552 /* Writing Psot in SOT marker */
10553 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10555 if (l_cp->m_specific_param.m_enc.m_cinema) {
10556 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10559 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10562 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10563 l_tcd->cur_pino = pino;
10565 /*Get number of tile parts*/
10566 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
10567 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10568 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10569 l_current_nb_bytes_written = 0;
10570 l_part_tile_size = 0;
10571 l_begin_data = p_data;
10573 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10577 l_nb_bytes_written += l_current_nb_bytes_written;
10578 p_data += l_current_nb_bytes_written;
10579 p_total_data_size -= l_current_nb_bytes_written;
10580 l_part_tile_size += l_current_nb_bytes_written;
10582 l_current_nb_bytes_written = 0;
10584 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10588 l_nb_bytes_written += l_current_nb_bytes_written;
10589 p_data += l_current_nb_bytes_written;
10590 p_total_data_size -= l_current_nb_bytes_written;
10591 l_part_tile_size += l_current_nb_bytes_written;
10593 /* Writing Psot in SOT marker */
10594 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10596 if (l_cp->m_specific_param.m_enc.m_cinema) {
10597 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10600 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10604 *p_data_written = l_nb_bytes_written;
10610 * Writes the updated tlm.
10612 * @param p_stream the stream to write data to.
10613 * @param p_j2k J2K codec.
10614 * @param p_manager the user event manager.
10616 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10617 struct opj_stream_private *p_stream,
10618 struct opj_event_mgr * p_manager )
10620 OPJ_UINT32 l_tlm_size;
10621 OPJ_SIZE_T l_tlm_position, l_current_position;
10623 /* preconditions */
10624 assert(p_j2k != 00);
10625 assert(p_manager != 00);
10626 assert(p_stream != 00);
10628 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10629 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10630 l_current_position = opj_stream_tell(p_stream);
10632 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10636 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) {
10640 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10649 * Ends the encoding, i.e. frees memory.
10651 * @param p_stream the stream to write data to.
10652 * @param p_j2k J2K codec.
10653 * @param p_manager the user event manager.
10655 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10656 struct opj_stream_private *p_stream,
10657 struct opj_event_mgr * p_manager )
10659 /* preconditions */
10660 assert(p_j2k != 00);
10661 assert(p_manager != 00);
10662 assert(p_stream != 00);
10664 tcd_destroy_v2(p_j2k->m_tcd);
10667 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10668 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10669 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10670 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10673 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10674 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10675 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10678 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10684 * Destroys the memory associated with the decoding of headers.
10686 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10687 opj_stream_private_t *p_stream,
10688 opj_event_mgr_t * p_manager
10691 /* preconditions */
10692 assert(p_j2k != 00);
10693 assert(p_stream != 00);
10694 assert(p_manager != 00);
10696 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10697 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10698 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10701 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10710 * @param p_stream the stream to write data to.
10711 * @param p_j2k J2K codec.
10712 * @param p_manager the user event manager.
10714 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
10715 struct opj_stream_private *p_stream,
10716 struct opj_event_mgr * p_manager )
10718 opj_codestream_info_t * l_cstr_info = 00;
10720 /* preconditions */
10721 assert(p_j2k != 00);
10722 assert(p_manager != 00);
10723 assert(p_stream != 00);
10726 /* TODO mergeV2: check this part which use cstr_info */
10727 /*l_cstr_info = p_j2k->cstr_info;
10731 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));
10733 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10734 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10736 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10738 l_cstr_info->tw = p_j2k->m_cp.tw;
10739 l_cstr_info->th = p_j2k->m_cp.th;
10741 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10742 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10743 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10744 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10746 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10748 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10750 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10752 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10753 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10756 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10758 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10760 /*l_cstr_info->maxmarknum = 100;
10761 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10762 l_cstr_info->marknum = 0;
10765 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);
10769 * Creates a tile-coder decoder.
10771 * @param p_stream the stream to write data to.
10772 * @param p_j2k J2K codec.
10773 * @param p_manager the user event manager.
10775 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10776 opj_stream_private_t *p_stream,
10777 opj_event_mgr_t * p_manager
10780 /* preconditions */
10781 assert(p_j2k != 00);
10782 assert(p_manager != 00);
10783 assert(p_stream != 00);
10785 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10787 if (! p_j2k->m_tcd) {
10788 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10792 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10793 tcd_destroy_v2(p_j2k->m_tcd);
10804 * @param p_j2k the jpeg2000 codec.
10805 * @param p_stream the stream to write data to.
10806 * @param p_manager the user event manager.
10808 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10809 OPJ_UINT32 p_tile_index,
10811 OPJ_UINT32 p_data_size,
10812 opj_stream_private_t *p_stream,
10813 opj_event_mgr_t * p_manager )
10815 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10816 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10820 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10821 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);