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);
584 Write the POC marker (progression order change)
585 @param j2k J2K handle
587 static void j2k_write_poc(opj_j2k_t *j2k);
590 * Writes the POC marker (Progression Order Change)
592 * @param p_stream the stream to write data to.
593 * @param p_j2k J2K codec.
594 * @param p_manager the user event manager.
596 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
597 struct opj_stream_private *p_stream,
598 struct opj_event_mgr * p_manager );
601 * Writes the POC marker (Progression Order Change)
603 * @param p_stream the stream to write data to.
604 * @param p_j2k J2K codec.
605 * @param p_manager the user event manager.
607 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
609 OPJ_UINT32 * p_data_written,
610 struct opj_event_mgr * p_manager );
613 * Gets the maximum size taken by the writting of a POC.
615 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
618 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
620 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
623 * Gets the maximum size taken by the headers of the SOT.
625 * @param p_j2k the jpeg2000 codec to use.
627 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
630 * Reads a POC marker (Progression Order Change)
632 * @param p_header_data the data contained in the POC box.
633 * @param p_j2k the jpeg2000 codec.
634 * @param p_header_size the size of the data contained in the POC marker.
635 * @param p_manager the user event manager.
637 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
638 OPJ_BYTE * p_header_data,
639 OPJ_UINT32 p_header_size,
640 opj_event_mgr_t * p_manager );
643 * Reads a CRG marker (Component registration)
645 * @param p_header_data the data contained in the TLM box.
646 * @param p_j2k the jpeg2000 codec.
647 * @param p_header_size the size of the data contained in the TLM marker.
648 * @param p_manager the user event manager.
650 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
651 OPJ_BYTE * p_header_data,
652 OPJ_UINT32 p_header_size,
653 opj_event_mgr_t * p_manager );
655 * Reads a TLM marker (Tile Length Marker)
657 * @param p_header_data the data contained in the TLM box.
658 * @param p_j2k the jpeg2000 codec.
659 * @param p_header_size the size of the data contained in the TLM marker.
660 * @param p_manager the user event manager.
662 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
663 OPJ_BYTE * p_header_data,
664 OPJ_UINT32 p_header_size,
665 opj_event_mgr_t * p_manager);
668 * Writes the updated tlm.
670 * @param p_stream the stream to write data to.
671 * @param p_j2k J2K codec.
672 * @param p_manager the user event manager.
674 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
675 struct opj_stream_private *p_stream,
676 struct opj_event_mgr * p_manager );
679 * Reads a PLM marker (Packet length, main header marker)
681 * @param p_header_data the data contained in the TLM box.
682 * @param p_j2k the jpeg2000 codec.
683 * @param p_header_size the size of the data contained in the TLM marker.
684 * @param p_manager the user event manager.
686 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
687 OPJ_BYTE * p_header_data,
688 OPJ_UINT32 p_header_size,
689 opj_event_mgr_t * p_manager);
691 * Reads a PLT marker (Packet length, tile-part header)
693 * @param p_header_data the data contained in the PLT box.
694 * @param p_j2k the jpeg2000 codec.
695 * @param p_header_size the size of the data contained in the PLT marker.
696 * @param p_manager the user event manager.
698 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
699 OPJ_BYTE * p_header_data,
700 OPJ_UINT32 p_header_size,
701 opj_event_mgr_t * p_manager );
705 * Reads a PPM marker (Packed packet headers, main header)
707 * @param p_header_data the data contained in the POC box.
708 * @param p_j2k the jpeg2000 codec.
709 * @param p_header_size the size of the data contained in the POC marker.
710 * @param p_manager the user event manager.
713 static opj_bool j2k_read_ppm_v2 (
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 struct opj_event_mgr * p_manager
721 static opj_bool j2k_read_ppm_v3 (
723 OPJ_BYTE * p_header_data,
724 OPJ_UINT32 p_header_size,
725 struct opj_event_mgr * p_manager
730 * Reads a PPT marker (Packed packet headers, tile-part header)
732 * @param p_header_data the data contained in the PPT box.
733 * @param p_j2k the jpeg2000 codec.
734 * @param p_header_size the size of the data contained in the PPT marker.
735 * @param p_manager the user event manager.
737 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
738 OPJ_BYTE * p_header_data,
739 OPJ_UINT32 p_header_size,
740 opj_event_mgr_t * p_manager );
742 Write the TLM marker (Mainheader)
743 @param j2k J2K handle
745 static void j2k_write_tlm(opj_j2k_t *j2k);
748 * Writes the TLM marker (Tile Length Marker)
750 * @param p_stream the stream to write data to.
751 * @param p_j2k J2K codec.
752 * @param p_manager the user event manager.
754 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
755 struct opj_stream_private *p_stream,
756 struct opj_event_mgr * p_manager );
759 Write the SOT marker (start of tile-part)
760 @param j2k J2K handle
762 static void j2k_write_sot(opj_j2k_t *j2k);
765 * Writes the SOT marker (Start of tile-part)
767 * @param p_stream the stream to write data to.
768 * @param p_j2k J2K codec.
769 * @param p_manager the user event manager.
771 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
773 OPJ_UINT32 * p_data_written,
774 const struct opj_stream_private *p_stream,
775 struct opj_event_mgr * p_manager );
778 * Reads a PPT marker (Packed packet headers, tile-part header)
780 * @param p_header_data the data contained in the PPT box.
781 * @param p_j2k the jpeg2000 codec.
782 * @param p_header_size the size of the data contained in the PPT marker.
783 * @param p_manager the user event manager.
785 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
786 OPJ_BYTE * p_header_data,
787 OPJ_UINT32 p_header_size,
788 opj_event_mgr_t * p_manager );
791 Write the SOD marker (start of data)
792 @param j2k J2K handle
793 @param tile_coder Pointer to a TCD handle
795 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
798 * Writes the SOD marker (Start of data)
800 * @param p_stream the stream to write data to.
801 * @param p_j2k J2K codec.
802 * @param p_manager the user event manager.
804 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
805 struct opj_tcd_v2 * p_tile_coder,
807 OPJ_UINT32 * p_data_written,
808 OPJ_UINT32 p_total_data_size,
809 const struct opj_stream_private *p_stream,
810 struct opj_event_mgr * p_manager );
813 * Reads a SOD marker (Start Of Data)
815 * @param p_header_data the data contained in the SOD box.
816 * @param p_j2k the jpeg2000 codec.
817 * @param p_header_size the size of the data contained in the SOD marker.
818 * @param p_manager the user event manager.
820 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
821 opj_stream_private_t *p_stream,
822 opj_event_mgr_t * p_manager );
825 * Updates the Tile Length Marker.
827 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
829 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
830 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
832 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
833 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
837 Write the RGN marker (region-of-interest)
838 @param j2k J2K handle
839 @param compno Number of the component concerned by the information written
840 @param tileno Number of the tile concerned by the information written
842 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
845 * Writes the RGN marker (Region Of Interest)
847 * @param p_tile_no the tile to output
848 * @param p_comp_no the component to output
849 * @param p_stream the stream to write data to.
850 * @param p_j2k J2K codec.
851 * @param p_manager the user event manager.
853 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
854 OPJ_UINT32 p_tile_no,
855 OPJ_UINT32 p_comp_no,
856 struct opj_stream_private *p_stream,
857 struct opj_event_mgr * p_manager );
860 * Reads a RGN marker (Region Of Interest)
862 * @param p_header_data the data contained in the POC box.
863 * @param p_j2k the jpeg2000 codec.
864 * @param p_header_size the size of the data contained in the POC marker.
865 * @param p_manager the user event manager.
867 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
868 OPJ_BYTE * p_header_data,
869 OPJ_UINT32 p_header_size,
870 opj_event_mgr_t * p_manager );
873 * Writes the EOC marker (End of Codestream)
875 * @param p_stream the stream to write data to.
876 * @param p_j2k J2K codec.
877 * @param p_manager the user event manager.
879 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
880 struct opj_stream_private *p_stream,
881 struct opj_event_mgr * p_manager );
884 Write the EOC marker (end of codestream)
885 @param j2k J2K handle
887 static void j2k_write_eoc(opj_j2k_t *j2k);
889 Read the EOC marker (end of codestream)
890 @param j2k J2K handle
892 static void j2k_read_eoc(opj_j2k_t *j2k);
895 * Reads a EOC marker (End Of Codestream)
897 * @param p_header_data the data contained in the SOD box.
898 * @param p_j2k the jpeg2000 codec.
899 * @param p_header_size the size of the data contained in the SOD marker.
900 * @param p_manager the user event manager.
903 static opj_bool j2k_read_eoc_v2 (
905 struct opj_stream_private *p_stream,
906 struct opj_event_mgr * p_manager
913 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
915 * @param p_stream the stream to write data to.
916 * @param p_j2k J2K codec.
917 * @param p_manager the user event manager.
919 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
920 struct opj_stream_private *p_stream,
921 struct opj_event_mgr * p_manager );
926 * @param p_stream the stream to write data to.
927 * @param p_j2k J2K codec.
928 * @param p_manager the user event manager.
930 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
931 struct opj_stream_private *p_stream,
932 struct opj_event_mgr * p_manager );
935 Add main header marker information
936 @param cstr_info Codestream information structure
937 @param type marker type
938 @param pos byte offset of marker segment
939 @param len length of marker segment
941 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
943 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
945 Add tile header marker information
946 @param tileno tile index number
947 @param cstr_info Codestream information structure
948 @param type marker type
949 @param pos byte offset of marker segment
950 @param len length of marker segment
952 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
954 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);
957 * Reads an unknown marker
959 * @param p_stream the stream object to read from.
960 * @param p_j2k the jpeg2000 codec.
961 * @param p_manager the user event manager.
963 * @return true if the marker could be deduced.
965 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
966 opj_stream_private_t *p_stream,
967 OPJ_UINT32 *output_marker,
968 opj_event_mgr_t * p_manager );
971 * Writes the MCT marker (Multiple Component Transform)
973 * @param p_stream the stream to write data to.
974 * @param p_j2k J2K codec.
975 * @param p_manager the user event manager.
977 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
978 opj_mct_data_t * p_mct_record,
979 struct opj_stream_private *p_stream,
980 struct opj_event_mgr * p_manager );
983 * Reads a MCT marker (Multiple Component Transform)
985 * @param p_header_data the data contained in the MCT box.
986 * @param p_j2k the jpeg2000 codec.
987 * @param p_header_size the size of the data contained in the MCT marker.
988 * @param p_manager the user event manager.
990 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
991 OPJ_BYTE * p_header_data,
992 OPJ_UINT32 p_header_size,
993 opj_event_mgr_t * p_manager );
996 * Writes the MCC marker (Multiple Component Collection)
998 * @param p_stream the stream to write data to.
999 * @param p_j2k J2K codec.
1000 * @param p_manager the user event manager.
1002 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1003 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1004 struct opj_stream_private *p_stream,
1005 struct opj_event_mgr * p_manager );
1009 * Reads a MCC marker (Multiple Component Collection)
1011 * @param p_header_data the data contained in the MCC box.
1012 * @param p_j2k the jpeg2000 codec.
1013 * @param p_header_size the size of the data contained in the MCC marker.
1014 * @param p_manager the user event manager.
1016 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1017 OPJ_BYTE * p_header_data,
1018 OPJ_UINT32 p_header_size,
1019 opj_event_mgr_t * p_manager );
1022 * Writes the MCO marker (Multiple component transformation ordering)
1024 * @param p_stream the stream to write data to.
1025 * @param p_j2k J2K codec.
1026 * @param p_manager the user event manager.
1028 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1029 struct opj_stream_private *p_stream,
1030 struct opj_event_mgr * p_manager );
1033 * Reads a MCO marker (Multiple Component Transform Ordering)
1035 * @param p_header_data the data contained in the MCO box.
1036 * @param p_j2k the jpeg2000 codec.
1037 * @param p_header_size the size of the data contained in the MCO marker.
1038 * @param p_manager the user event manager.
1040 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1041 OPJ_BYTE * p_header_data,
1042 OPJ_UINT32 p_header_size,
1043 opj_event_mgr_t * p_manager );
1045 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1047 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1048 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1049 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1050 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1052 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1053 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1054 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1055 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1057 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1058 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1059 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1060 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1064 * Ends the encoding, i.e. frees memory.
1066 * @param p_stream the stream to write data to.
1067 * @param p_j2k J2K codec.
1068 * @param p_manager the user event manager.
1070 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1071 struct opj_stream_private *p_stream,
1072 struct opj_event_mgr * p_manager );
1075 * Writes the CBD marker (Component bit depth definition)
1077 * @param p_stream the stream to write data to.
1078 * @param p_j2k J2K codec.
1079 * @param p_manager the user event manager.
1081 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1082 struct opj_stream_private *p_stream,
1083 struct opj_event_mgr * p_manager );
1086 * Reads a CBD marker (Component bit depth definition)
1087 * @param p_header_data the data contained in the CBD box.
1088 * @param p_j2k the jpeg2000 codec.
1089 * @param p_header_size the size of the data contained in the CBD marker.
1090 * @param p_manager the user event manager.
1092 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1093 OPJ_BYTE * p_header_data,
1094 OPJ_UINT32 p_header_size,
1095 opj_event_mgr_t * p_manager);
1098 * Writes the image components.
1100 * @param p_stream the stream to write data to.
1101 * @param p_j2k J2K codec.
1102 * @param p_manager the user event manager.
1104 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1105 struct opj_stream_private *p_stream,
1106 struct opj_event_mgr * p_manager );
1109 * Writes regions of interests.
1111 * @param p_stream the stream to write data to.
1112 * @param p_j2k J2K codec.
1113 * @param p_manager the user event manager.
1115 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1116 struct opj_stream_private *p_stream,
1117 struct opj_event_mgr * p_manager );
1122 * @param p_stream the stream to write data to.
1123 * @param p_j2k J2K codec.
1124 * @param p_manager the user event manager.
1126 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1127 struct opj_stream_private *p_stream,
1128 struct opj_event_mgr * p_manager );
1131 * Checks the progression order changes values. Tells of the poc given as input are valid.
1132 * A nice message is outputted at errors.
1134 * @param p_pocs the progression order changes.
1135 * @param p_nb_pocs the number of progression order changes.
1136 * @param p_nb_resolutions the number of resolutions.
1137 * @param numcomps the number of components
1138 * @param numlayers the number of layers.
1140 * @return true if the pocs are valid.
1142 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1143 OPJ_UINT32 p_nb_pocs,
1144 OPJ_UINT32 p_nb_resolutions,
1145 OPJ_UINT32 numcomps,
1146 OPJ_UINT32 numlayers,
1147 opj_event_mgr_t * p_manager);
1150 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1152 * @param cp the coding parameters.
1153 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1154 * @param tileno the given tile.
1156 * @return the number of tile parts.
1158 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1160 /** mem allocation for TLM marker*/
1161 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1164 * Calculates the total number of tile parts needed by the encoder to
1165 * encode such an image. If not enough memory is available, then the function return false.
1167 * @param p_nb_tiles pointer that will hold the number of tile parts.
1168 * @param cp the coding parameters for the image.
1169 * @param image the image to encode.
1170 * @param p_j2k the p_j2k encoder.
1171 * @param p_manager the user event manager.
1173 * @return true if the function was successful, false else.
1175 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1177 OPJ_UINT32 * p_nb_tiles,
1179 opj_event_mgr_t * p_manager);
1181 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1183 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1185 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1191 /* ----------------------------------------------------------------------- */
1192 typedef struct j2k_prog_order{
1193 OPJ_PROG_ORDER enum_prog;
1197 j2k_prog_order_t j2k_prog_order_list[] = {
1203 {(OPJ_PROG_ORDER)-1, ""}
1211 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1219 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1223 opj_j2k_read_int16_to_float,
1224 opj_j2k_read_int32_to_float,
1225 opj_j2k_read_float32_to_float,
1226 opj_j2k_read_float64_to_float
1229 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1231 opj_j2k_read_int16_to_int32,
1232 opj_j2k_read_int32_to_int32,
1233 opj_j2k_read_float32_to_int32,
1234 opj_j2k_read_float64_to_int32
1237 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1239 opj_j2k_write_float_to_int16,
1240 opj_j2k_write_float_to_int32,
1241 opj_j2k_write_float_to_float,
1242 opj_j2k_write_float_to_float64
1245 typedef struct opj_dec_memory_marker_handler
1249 /** value of the state when the marker can appear */
1251 /** action linked to the marker */
1252 opj_bool (*handler) (
1253 opj_j2k_v2_t *p_j2k,
1254 OPJ_BYTE * p_header_data,
1255 OPJ_UINT32 p_header_size,
1256 struct opj_event_mgr * p_manager
1259 opj_dec_memory_marker_handler_t;
1261 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1264 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1265 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1266 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1267 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1268 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1269 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1270 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1271 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1272 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1273 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1274 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1275 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1276 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1278 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1279 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1280 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1281 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1282 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1283 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1285 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1286 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1287 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1288 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1289 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1290 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1291 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1292 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1293 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1294 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1295 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1296 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1297 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1299 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1300 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1301 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1302 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1303 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1304 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1306 #ifdef TODO_MS /* FIXME */
1307 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1308 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1309 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1310 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1312 #endif /* USE_JPWL */
1314 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1315 {J2K_MS_INSEC, 0, j2k_read_insec}
1316 #endif /* USE_JPSEC */
1317 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1322 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1324 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1325 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1329 for (i=0;i<p_nb_elem;++i) {
1330 opj_read_bytes(l_src_data,&l_temp,2);
1332 l_src_data+=sizeof(OPJ_INT16);
1334 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1338 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1340 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1341 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1345 for (i=0;i<p_nb_elem;++i) {
1346 opj_read_bytes(l_src_data,&l_temp,4);
1348 l_src_data+=sizeof(OPJ_INT32);
1350 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1354 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1356 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1357 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1361 for (i=0;i<p_nb_elem;++i) {
1362 opj_read_float(l_src_data,&l_temp);
1364 l_src_data+=sizeof(OPJ_FLOAT32);
1366 *(l_dest_data++) = l_temp;
1370 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1372 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1373 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1377 for (i=0;i<p_nb_elem;++i) {
1378 opj_read_double(l_src_data,&l_temp);
1380 l_src_data+=sizeof(OPJ_FLOAT64);
1382 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1386 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1388 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1389 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1393 for (i=0;i<p_nb_elem;++i) {
1394 opj_read_bytes(l_src_data,&l_temp,2);
1396 l_src_data+=sizeof(OPJ_INT16);
1398 *(l_dest_data++) = (OPJ_INT32) l_temp;
1402 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1404 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1405 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1409 for (i=0;i<p_nb_elem;++i) {
1410 opj_read_bytes(l_src_data,&l_temp,4);
1412 l_src_data+=sizeof(OPJ_INT32);
1414 *(l_dest_data++) = (OPJ_INT32) l_temp;
1418 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1420 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1421 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1425 for (i=0;i<p_nb_elem;++i) {
1426 opj_read_float(l_src_data,&l_temp);
1428 l_src_data+=sizeof(OPJ_FLOAT32);
1430 *(l_dest_data++) = (OPJ_INT32) l_temp;
1434 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1436 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1437 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1441 for (i=0;i<p_nb_elem;++i) {
1442 opj_read_double(l_src_data,&l_temp);
1444 l_src_data+=sizeof(OPJ_FLOAT64);
1446 *(l_dest_data++) = (OPJ_INT32) l_temp;
1450 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1452 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1453 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1457 for (i=0;i<p_nb_elem;++i) {
1458 l_temp = (OPJ_UINT32) *(l_src_data++);
1460 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1462 l_dest_data+=sizeof(OPJ_INT16);
1466 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1468 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1469 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1473 for (i=0;i<p_nb_elem;++i) {
1474 l_temp = (OPJ_UINT32) *(l_src_data++);
1476 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1478 l_dest_data+=sizeof(OPJ_INT32);
1482 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1484 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1485 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1489 for (i=0;i<p_nb_elem;++i) {
1490 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1492 opj_write_float(l_dest_data,l_temp);
1494 l_dest_data+=sizeof(OPJ_FLOAT32);
1498 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1500 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1501 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1505 for (i=0;i<p_nb_elem;++i) {
1506 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1508 opj_write_double(l_dest_data,l_temp);
1510 l_dest_data+=sizeof(OPJ_FLOAT64);
1516 * Converts an enum type progression order to string type.
1518 * @param prg_order the progression order to get.
1520 * @return the string representation of the given progression order.
1522 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1523 j2k_prog_order_t *po;
1524 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1525 if(po->enum_prog == prg_order){
1526 return po->str_prog;
1529 return po->str_prog;
1533 * Checks the progression order changes values. Tells if the poc given as input are valid.
1535 * @param p_pocs the progression order changes.
1536 * @param p_nb_pocs the number of progression order changes.
1537 * @param p_nb_resolutions the number of resolutions.
1538 * @param numcomps the number of components
1539 * @param numlayers the number of layers.
1540 * @param p_manager the user event manager.
1542 * @return true if the pocs are valid.
1544 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1545 OPJ_UINT32 p_nb_pocs,
1546 OPJ_UINT32 p_nb_resolutions,
1547 OPJ_UINT32 p_num_comps,
1548 OPJ_UINT32 p_num_layers,
1549 opj_event_mgr_t * p_manager)
1551 OPJ_UINT32* packet_array;
1552 OPJ_UINT32 index , resno, compno, layno;
1554 OPJ_UINT32 step_c = 1;
1555 OPJ_UINT32 step_r = p_num_comps * step_c;
1556 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1557 opj_bool loss = OPJ_FALSE;
1558 OPJ_UINT32 layno0 = 0;
1560 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1561 if (packet_array == 00) {
1562 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1565 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1567 if (p_nb_pocs == 0) {
1571 index = step_r * p_pocs->resno0;
1572 // take each resolution for each poc
1573 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1575 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1577 // take each comp of each resolution for each poc
1578 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1579 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1581 // and finally take each layer of each res of ...
1582 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1583 //index = step_r * resno + step_c * compno + step_l * layno;
1584 packet_array[comp_index] = 1;
1585 comp_index += step_l;
1588 res_index += step_c;
1595 // iterate through all the pocs
1596 for (i = 1; i < p_nb_pocs ; ++i) {
1597 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1599 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1600 index = step_r * p_pocs->resno0;
1602 // take each resolution for each poc
1603 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1604 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1606 // take each comp of each resolution for each poc
1607 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1608 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1610 // and finally take each layer of each res of ...
1611 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1612 //index = step_r * resno + step_c * compno + step_l * layno;
1613 packet_array[comp_index] = 1;
1614 comp_index += step_l;
1617 res_index += step_c;
1627 for (layno = 0; layno < p_num_layers ; ++layno) {
1628 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1629 for (compno = 0; compno < p_num_comps; ++compno) {
1630 loss |= (packet_array[index]!=1);
1631 //index = step_r * resno + step_c * compno + step_l * layno;
1638 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1641 opj_free(packet_array);
1646 /* ----------------------------------------------------------------------- */
1647 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1650 int tpnum=1,tpend=0;
1651 opj_tcp_t *tcp = &cp->tcps[tileno];
1652 prog = j2k_convert_progression_order(tcp->prg);
1657 if( cp->tp_flag == prog[i] ){
1658 tpend=1;cp->tp_pos=i;
1662 tpnum= tpnum * tcp->pocs[pino].compE;
1665 tpnum= tpnum * tcp->pocs[pino].resE;
1668 tpnum= tpnum * tcp->pocs[pino].prcE;
1671 tpnum= tpnum * tcp->pocs[pino].layE;
1683 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1685 * @param cp the coding parameters.
1686 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1687 * @param tileno the given tile.
1689 * @return the number of tile parts.
1691 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1693 const OPJ_CHAR *prog = 00;
1695 OPJ_UINT32 tpnum = 1;
1696 opj_tcp_v2_t *tcp = 00;
1697 opj_poc_t * l_current_poc = 00;
1700 assert(tileno < (cp->tw * cp->th));
1701 assert(pino < (cp->tcps[tileno].numpocs + 1));
1703 /* get the given tile coding parameter */
1704 tcp = &cp->tcps[tileno];
1707 l_current_poc = &(tcp->pocs[pino]);
1708 assert(l_current_poc != 0);
1710 /* get the progression order as a character string */
1711 prog = j2k_convert_progression_order(tcp->prg);
1712 assert(strlen(prog) > 0);
1714 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1718 /* component wise */
1720 tpnum *= l_current_poc->compE;
1722 /* resolution wise */
1724 tpnum *= l_current_poc->resE;
1728 tpnum *= l_current_poc->prcE;
1732 tpnum *= l_current_poc->layE;
1735 /* whould we split here ? */
1736 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1737 cp->m_specific_param.m_enc.m_tp_pos=i;
1749 /** mem allocation for TLM marker*/
1750 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1751 int pino,tileno,totnum_tp=0;
1753 OPJ_ARG_NOT_USED(img_numcomp);
1755 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1756 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1757 int cur_totnum_tp = 0;
1758 opj_tcp_t *tcp = &cp->tcps[tileno];
1759 for(pino = 0; pino <= tcp->numpocs; pino++) {
1761 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1762 if(!pi) { return -1;}
1763 tp_num = j2k_get_num_tp(cp,pino,tileno);
1764 totnum_tp = totnum_tp + tp_num;
1765 cur_totnum_tp = cur_totnum_tp + tp_num;
1766 pi_destroy(pi, cp, tileno);
1768 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1770 if (j2k->cstr_info) {
1771 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1772 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1780 * Calculates the total number of tile parts needed by the encoder to
1781 * encode such an image. If not enough memory is available, then the function return false.
1783 * @param p_nb_tiles pointer that will hold the number of tile parts.
1784 * @param cp the coding parameters for the image.
1785 * @param image the image to encode.
1786 * @param p_j2k the p_j2k encoder.
1787 * @param p_manager the user event manager.
1789 * @return true if the function was successful, false else.
1791 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1793 OPJ_UINT32 * p_nb_tiles,
1795 opj_event_mgr_t * p_manager)
1797 OPJ_UINT32 pino,tileno;
1798 OPJ_UINT32 l_nb_tiles;
1802 assert(p_nb_tiles != 00);
1804 assert(image != 00);
1805 assert(p_j2k != 00);
1806 assert(p_manager != 00);
1808 l_nb_tiles = cp->tw * cp->th;
1813 /* TODO mergeV2: check this part which use cstr_info */
1814 /*if (p_j2k->cstr_info) {
1815 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1817 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1818 OPJ_UINT32 cur_totnum_tp = 0;
1820 pi_update_encoding_parameters(image,cp,tileno);
1822 for (pino = 0; pino <= tcp->numpocs; ++pino)
1824 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1826 *p_nb_tiles = *p_nb_tiles + tp_num;
1828 cur_totnum_tp += tp_num;
1831 tcp->m_nb_tile_parts = cur_totnum_tp;
1833 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1834 if (l_info_tile_ptr->tp == 00) {
1838 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1840 l_info_tile_ptr->num_tps = cur_totnum_tp;
1847 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1848 OPJ_UINT32 cur_totnum_tp = 0;
1850 pi_update_encoding_parameters(image,cp,tileno);
1852 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1853 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1855 *p_nb_tiles = *p_nb_tiles + tp_num;
1857 cur_totnum_tp += tp_num;
1859 tcp->m_nb_tile_parts = cur_totnum_tp;
1869 * Writes the SOC marker (Start Of Codestream)
1871 * @param p_stream the stream to write data to.
1872 * @param p_j2k J2K codec.
1873 * @param p_manager the user event manager.
1875 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1876 opj_stream_private_t *p_stream,
1877 opj_event_mgr_t * p_manager )
1879 /* 2 bytes will be written */
1880 OPJ_BYTE * l_start_stream = 00;
1883 assert(p_stream != 00);
1884 assert(p_j2k != 00);
1885 assert(p_manager != 00);
1887 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1889 /* write SOC identifier */
1890 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1892 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1898 /* update markers struct */
1900 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1902 assert( 0 && "TODO" );
1903 #endif /* USE_JPWL */
1912 * Reads a SOC marker (Start of Codestream)
1913 * @param p_header_data the data contained in the SOC box.
1914 * @param jp2 the jpeg2000 file codec.
1915 * @param p_header_size the size of the data contained in the SOC marker.
1916 * @param p_manager the user event manager.
1918 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1919 opj_stream_private_t *p_stream,
1920 opj_event_mgr_t * p_manager
1923 OPJ_BYTE l_data [2];
1924 OPJ_UINT32 l_marker;
1927 assert(p_j2k != 00);
1928 assert(p_manager != 00);
1929 assert(p_stream != 00);
1931 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1935 opj_read_bytes(l_data,&l_marker,2);
1936 if (l_marker != J2K_MS_SOC) {
1940 /* Next marker should be a SIZ marker in the main header */
1941 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1943 /* FIXME move it in a index structure included in p_j2k*/
1944 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1946 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1948 /* Add the marker to the codestream index*/
1949 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1956 * Writes the SIZ marker (image and tile size)
1958 * @param p_stream the stream to write data to.
1959 * @param p_j2k J2K codec.
1960 * @param p_manager the user event manager.
1962 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1963 opj_stream_private_t *p_stream,
1964 opj_event_mgr_t * p_manager )
1967 OPJ_UINT32 l_size_len;
1968 OPJ_BYTE * l_current_ptr;
1969 opj_image_t * l_image = 00;
1970 opj_cp_v2_t *cp = 00;
1971 opj_image_comp_t * l_img_comp = 00;
1974 assert(p_stream != 00);
1975 assert(p_j2k != 00);
1976 assert(p_manager != 00);
1978 l_image = p_j2k->m_private_image;
1979 cp = &(p_j2k->m_cp);
1980 l_size_len = 40 + 3 * l_image->numcomps;
1981 l_img_comp = l_image->comps;
1983 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1985 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1986 = (OPJ_BYTE*)opj_realloc(
1987 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1989 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1993 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1996 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1998 /* write SOC identifier */
1999 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2002 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2005 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2008 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2011 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2014 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2017 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2020 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2023 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2026 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2029 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2032 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2035 for (i = 0; i < l_image->numcomps; ++i) {
2036 /* TODO here with MCT ? */
2037 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2040 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2043 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2049 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) {
2059 * Reads a SIZ marker (image and tile size)
2060 * @param p_header_data the data contained in the SIZ box.
2061 * @param jp2 the jpeg2000 file codec.
2062 * @param p_header_size the size of the data contained in the SIZ marker.
2063 * @param p_manager the user event manager.
2065 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2066 OPJ_BYTE * p_header_data,
2067 OPJ_UINT32 p_header_size,
2068 opj_event_mgr_t * p_manager
2071 OPJ_UINT32 l_size, i;
2072 OPJ_UINT32 l_nb_comp;
2073 OPJ_UINT32 l_nb_comp_remain;
2074 OPJ_UINT32 l_remaining_size;
2075 OPJ_UINT32 l_nb_tiles;
2077 opj_image_t *l_image = 00;
2078 opj_cp_v2_t *l_cp = 00;
2079 opj_image_comp_t * l_img_comp = 00;
2080 opj_tcp_v2_t * l_current_tile_param = 00;
2083 assert(p_j2k != 00);
2084 assert(p_manager != 00);
2085 assert(p_header_data != 00);
2087 l_image = p_j2k->m_private_image;
2088 l_cp = &(p_j2k->m_cp);
2090 /* minimum size == 39 - 3 (= minimum component parameter) */
2091 if (p_header_size < 36) {
2092 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2096 l_remaining_size = p_header_size - 36;
2097 l_nb_comp = l_remaining_size / 3;
2098 l_nb_comp_remain = l_remaining_size % 3;
2099 if (l_nb_comp_remain != 0){
2100 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2104 l_size = p_header_size + 2; /* Lsiz */
2106 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2108 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2109 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2111 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2113 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2115 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2117 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2119 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2121 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2123 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2125 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2128 l_image->numcomps = (OPJ_UINT16) l_tmp;
2130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2134 if (l_image->numcomps != l_nb_comp) {
2135 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);
2140 if (l_cp->correct) {
2141 /* if JPWL is on, we check whether TX errors have damaged
2142 too much the SIZ parameters */
2143 if (!(l_image->x1 * l_image->y1)) {
2144 opj_event_msg_v2(p_manager, EVT_ERROR,
2145 "JPWL: bad image size (%d x %d)\n",
2146 l_image->x1, l_image->y1);
2147 if (!JPWL_ASSUME || JPWL_ASSUME) {
2148 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2153 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2154 if (l_image->numcomps != ((len - 38) / 3)) {
2155 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2156 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2157 l_image->numcomps, ((len - 38) / 3));
2159 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2162 */ /* we try to correct */
2163 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2164 if (l_image->numcomps < ((len - 38) / 3)) {
2165 len = 38 + 3 * l_image->numcomps;
2166 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2169 l_image->numcomps = ((len - 38) / 3);
2170 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2176 /* update components number in the jpwl_exp_comps filed */
2177 l_cp->exp_comps = l_image->numcomps;
2179 #endif /* USE_JPWL */
2181 /* Allocate the resulting image components */
2182 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2183 if (l_image->comps == 00){
2184 l_image->numcomps = 0;
2185 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2189 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2190 l_img_comp = l_image->comps;
2192 /* Read the component information */
2193 for (i = 0; i < l_image->numcomps; ++i){
2195 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2197 l_img_comp->prec = (tmp & 0x7f) + 1;
2198 l_img_comp->sgnd = tmp >> 7;
2199 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2201 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2202 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2204 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2207 if (l_cp->correct) {
2208 /* if JPWL is on, we check whether TX errors have damaged
2209 too much the SIZ parameters, again */
2210 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2211 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2212 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2213 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2215 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2218 /* we try to correct */
2219 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2220 if (!l_image->comps[i].dx) {
2221 l_image->comps[i].dx = 1;
2222 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2223 i, l_image->comps[i].dx);
2225 if (!l_image->comps[i].dy) {
2226 l_image->comps[i].dy = 1;
2227 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2228 i, l_image->comps[i].dy);
2232 #endif /* USE_JPWL */
2233 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2234 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2238 /* Compute the number of tiles */
2239 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2240 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2241 l_nb_tiles = l_cp->tw * l_cp->th;
2243 /* Define the tiles which will be decoded */
2244 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2245 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;
2246 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;
2247 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);
2248 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);
2251 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2252 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2253 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2254 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2258 if (l_cp->correct) {
2259 /* if JPWL is on, we check whether TX errors have damaged
2260 too much the SIZ parameters */
2261 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2262 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2263 "JPWL: bad number of tiles (%d x %d)\n",
2264 l_cp->tw, l_cp->th);
2266 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2269 /* we try to correct */
2270 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2273 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2276 if (l_cp->tw > l_cp->max_tiles) {
2278 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2279 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2280 l_cp->max_tiles, l_cp->tw);
2284 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2287 if (l_cp->th > l_cp->max_tiles) {
2289 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2290 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2291 l_cp->max_tiles, l_cp->th);
2295 #endif /* USE_JPWL */
2297 /* memory allocations */
2298 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2299 if (l_cp->tcps == 00) {
2300 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2303 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2306 if (l_cp->correct) {
2308 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2309 "JPWL: could not alloc tcps field of cp\n");
2310 if (!JPWL_ASSUME || JPWL_ASSUME) {
2311 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2316 #endif /* USE_JPWL */
2318 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2319 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2320 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2321 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2324 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2326 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2327 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2329 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2330 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2333 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));
2334 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2336 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2337 (opj_simple_mcc_decorrelation_data_t*)
2338 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2340 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2341 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2344 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));
2345 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2347 /* set up default dc level shift */
2348 for (i=0;i<l_image->numcomps;++i) {
2349 if (! l_image->comps[i].sgnd) {
2350 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2354 l_current_tile_param = l_cp->tcps;
2355 for (i = 0; i < l_nb_tiles; ++i) {
2356 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2357 if (l_current_tile_param->tccps == 00) {
2358 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2361 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2363 ++l_current_tile_param;
2366 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2367 opj_image_comp_header_update(l_image,l_cp);
2373 * Writes the COM marker (comment)
2375 * @param p_stream the stream to write data to.
2376 * @param p_j2k J2K codec.
2377 * @param p_manager the user event manager.
2379 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2380 opj_stream_private_t *p_stream,
2381 opj_event_mgr_t * p_manager
2384 OPJ_UINT32 l_comment_size;
2385 OPJ_UINT32 l_total_com_size;
2386 const OPJ_CHAR *l_comment;
2387 OPJ_BYTE * l_current_ptr = 00;
2390 assert(p_j2k != 00);
2391 assert(p_stream != 00);
2392 assert(p_manager != 00);
2394 l_comment = p_j2k->m_cp.comment;
2395 l_comment_size = strlen(l_comment);
2396 l_total_com_size = l_comment_size + 6;
2398 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2399 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2400 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2403 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2407 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2410 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2412 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2415 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2418 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2421 memcpy( l_current_ptr,l_comment,l_comment_size);
2423 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) {
2431 * Reads a COM marker (comments)
2432 * @param p_header_data the data contained in the COM box.
2433 * @param jp2 the jpeg2000 file codec.
2434 * @param p_header_size the size of the data contained in the COM marker.
2435 * @param p_manager the user event manager.
2437 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2438 OPJ_BYTE * p_header_data,
2439 OPJ_UINT32 p_header_size,
2440 opj_event_mgr_t * p_manager
2444 assert(p_j2k != 00);
2445 assert(p_manager != 00);
2446 assert(p_header_data != 00);
2447 (void)p_header_size;
2453 * Writes the COD marker (Coding style default)
2455 * @param p_stream the stream to write data to.
2456 * @param p_j2k J2K codec.
2457 * @param p_manager the user event manager.
2459 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2460 opj_stream_private_t *p_stream,
2461 opj_event_mgr_t * p_manager )
2463 opj_cp_v2_t *l_cp = 00;
2464 opj_tcp_v2_t *l_tcp = 00;
2465 OPJ_UINT32 l_code_size,l_remaining_size;
2466 OPJ_BYTE * l_current_data = 00;
2469 assert(p_j2k != 00);
2470 assert(p_manager != 00);
2471 assert(p_stream != 00);
2473 l_cp = &(p_j2k->m_cp);
2474 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2475 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2476 l_remaining_size = l_code_size;
2478 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2479 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2480 = (OPJ_BYTE*)opj_realloc(
2481 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2484 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2488 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2491 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2493 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2494 l_current_data += 2;
2496 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2497 l_current_data += 2;
2499 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2502 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2505 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2508 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2511 l_remaining_size -= 9;
2513 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2514 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2518 if (l_remaining_size != 0) {
2519 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2523 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) {
2532 * Reads a COD marker (Coding Styke defaults)
2533 * @param p_header_data the data contained in the COD box.
2534 * @param p_j2k the jpeg2000 codec.
2535 * @param p_header_size the size of the data contained in the COD marker.
2536 * @param p_manager the user event manager.
2538 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2539 OPJ_BYTE * p_header_data,
2540 OPJ_UINT32 p_header_size,
2541 opj_event_mgr_t * p_manager
2547 opj_cp_v2_t *l_cp = 00;
2548 opj_tcp_v2_t *l_tcp = 00;
2549 opj_image_t *l_image = 00;
2552 assert(p_header_data != 00);
2553 assert(p_j2k != 00);
2554 assert(p_manager != 00);
2556 l_image = p_j2k->m_private_image;
2557 l_cp = &(p_j2k->m_cp);
2559 /* If we are in the first tile-part header of the current tile */
2560 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2561 &l_cp->tcps[p_j2k->m_current_tile_number] :
2562 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2564 /* Make sure room is sufficient */
2565 if (p_header_size < 5) {
2566 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2570 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2572 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2574 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2575 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2578 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2579 if (l_cp->m_specific_param.m_dec.m_layer) {
2580 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2583 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2586 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2590 for (i = 0; i < l_image->numcomps; ++i) {
2591 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2594 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2595 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2599 if (p_header_size != 0) {
2600 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2604 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2605 opj_j2k_copy_tile_component_parameters(p_j2k);
2608 #ifdef WIP_REMOVE_MSD
2609 if (p_j2k->cstr_info) {
2610 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2611 p_j2k->cstr_info->prog = l_tcp->prg;
2612 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2613 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2614 for (i = 0; i < l_image->numcomps; ++i) {
2615 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2624 * Writes the COC marker (Coding style component)
2626 * @param p_comp_no the index of the component to output.
2627 * @param p_stream the stream to write data to.
2628 * @param p_j2k J2K codec.
2629 * @param p_manager the user event manager.
2631 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2632 OPJ_UINT32 p_comp_no,
2633 opj_stream_private_t *p_stream,
2634 opj_event_mgr_t * p_manager )
2636 OPJ_UINT32 l_coc_size,l_remaining_size;
2637 OPJ_UINT32 l_comp_room;
2640 assert(p_j2k != 00);
2641 assert(p_manager != 00);
2642 assert(p_stream != 00);
2644 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2646 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2648 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2649 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2650 = (OPJ_BYTE*)opj_realloc(
2651 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2653 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2657 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2660 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);
2662 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) {
2670 * Writes the COC marker (Coding style component)
2672 * @param p_comp_no the index of the component to output.
2673 * @param p_stream the stream to write data to.
2674 * @param p_j2k J2K codec.
2675 * @param p_manager the user event manager.
2677 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2678 OPJ_UINT32 p_comp_no,
2680 OPJ_UINT32 * p_data_written,
2681 opj_event_mgr_t * p_manager
2684 opj_cp_v2_t *l_cp = 00;
2685 opj_tcp_v2_t *l_tcp = 00;
2686 OPJ_UINT32 l_coc_size,l_remaining_size;
2687 OPJ_BYTE * l_current_data = 00;
2688 opj_image_t *l_image = 00;
2689 OPJ_UINT32 l_comp_room;
2692 assert(p_j2k != 00);
2693 assert(p_manager != 00);
2695 l_cp = &(p_j2k->m_cp);
2696 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2697 l_image = p_j2k->m_private_image;
2698 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2700 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2701 l_remaining_size = l_coc_size;
2703 l_current_data = p_data;
2705 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2706 l_current_data += 2;
2708 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2709 l_current_data += 2;
2711 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2712 l_current_data+=l_comp_room;
2714 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2717 l_remaining_size -= (5 + l_comp_room);
2718 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2719 * p_data_written = l_coc_size;
2723 * Gets the maximum size taken by a coc.
2725 * @param p_j2k the jpeg2000 codec to use.
2727 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2730 OPJ_UINT32 l_nb_comp;
2731 OPJ_UINT32 l_nb_tiles;
2732 OPJ_UINT32 l_max = 0;
2736 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2737 l_nb_comp = p_j2k->m_private_image->numcomps;
2739 for (i=0;i<l_nb_tiles;++i) {
2740 for (j=0;j<l_nb_comp;++j) {
2741 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2750 * Reads a COC marker (Coding Style Component)
2751 * @param p_header_data the data contained in the COC box.
2752 * @param p_j2k the jpeg2000 codec.
2753 * @param p_header_size the size of the data contained in the COC marker.
2754 * @param p_manager the user event manager.
2756 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2757 OPJ_BYTE * p_header_data,
2758 OPJ_UINT32 p_header_size,
2759 opj_event_mgr_t * p_manager
2762 opj_cp_v2_t *l_cp = NULL;
2763 opj_tcp_v2_t *l_tcp = NULL;
2764 opj_image_t *l_image = NULL;
2765 OPJ_UINT32 l_comp_room;
2766 OPJ_UINT32 l_comp_no;
2769 assert(p_header_data != 00);
2770 assert(p_j2k != 00);
2771 assert(p_manager != 00);
2773 l_cp = &(p_j2k->m_cp);
2774 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2775 &l_cp->tcps[p_j2k->m_current_tile_number] :
2776 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2777 l_image = p_j2k->m_private_image;
2779 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2781 /* make sure room is sufficient*/
2782 if (p_header_size < l_comp_room + 1) {
2783 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2786 p_header_size -= l_comp_room + 1;
2788 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2789 p_header_data += l_comp_room;
2790 if (l_comp_no >= l_image->numcomps) {
2791 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2795 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2798 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2799 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2803 if (p_header_size != 0) {
2804 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2811 * Writes the QCD marker (quantization default)
2813 * @param p_comp_number the index of the component to output.
2814 * @param p_stream the stream to write data to.
2815 * @param p_j2k J2K codec.
2816 * @param p_manager the user event manager.
2818 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2819 opj_stream_private_t *p_stream,
2820 opj_event_mgr_t * p_manager
2823 opj_cp_v2_t *l_cp = 00;
2824 opj_tcp_v2_t *l_tcp = 00;
2825 OPJ_UINT32 l_qcd_size,l_remaining_size;
2826 OPJ_BYTE * l_current_data = 00;
2829 assert(p_j2k != 00);
2830 assert(p_manager != 00);
2831 assert(p_stream != 00);
2833 l_cp = &(p_j2k->m_cp);
2834 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2835 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2836 l_remaining_size = l_qcd_size;
2838 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2839 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2840 = (OPJ_BYTE*)opj_realloc(
2841 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2844 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2848 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2851 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2853 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2854 l_current_data += 2;
2856 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2857 l_current_data += 2;
2859 l_remaining_size -= 4;
2861 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2862 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2866 if (l_remaining_size != 0) {
2867 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2871 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) {
2879 * Reads a QCD marker (Quantization defaults)
2880 * @param p_header_data the data contained in the QCD box.
2881 * @param p_j2k the jpeg2000 codec.
2882 * @param p_header_size the size of the data contained in the QCD marker.
2883 * @param p_manager the user event manager.
2885 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2886 OPJ_BYTE * p_header_data,
2887 OPJ_UINT32 p_header_size,
2888 opj_event_mgr_t * p_manager
2892 assert(p_header_data != 00);
2893 assert(p_j2k != 00);
2894 assert(p_manager != 00);
2896 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2897 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2901 if (p_header_size != 0) {
2902 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2906 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2907 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2913 * Writes the QCC marker (quantization component)
2915 * @param p_comp_no the index of the component to output.
2916 * @param p_stream the stream to write data to.
2917 * @param p_j2k J2K codec.
2918 * @param p_manager the user event manager.
2920 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2921 OPJ_UINT32 p_comp_no,
2922 opj_stream_private_t *p_stream,
2923 opj_event_mgr_t * p_manager
2926 OPJ_UINT32 l_qcc_size,l_remaining_size;
2929 assert(p_j2k != 00);
2930 assert(p_manager != 00);
2931 assert(p_stream != 00);
2933 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2934 l_remaining_size = l_qcc_size;
2936 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2937 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2938 = (OPJ_BYTE*)opj_realloc(
2939 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2941 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2945 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2948 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);
2950 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) {
2958 * Writes the QCC marker (quantization component)
2960 * @param p_comp_no the index of the component to output.
2961 * @param p_stream the stream to write data to.
2962 * @param p_j2k J2K codec.
2963 * @param p_manager the user event manager.
2965 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2966 OPJ_UINT32 p_comp_no,
2968 OPJ_UINT32 * p_data_written,
2969 opj_event_mgr_t * p_manager
2972 OPJ_UINT32 l_qcc_size,l_remaining_size;
2973 OPJ_BYTE * l_current_data = 00;
2976 assert(p_j2k != 00);
2977 assert(p_manager != 00);
2979 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2980 l_remaining_size = l_qcc_size;
2982 l_current_data = p_data;
2984 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2985 l_current_data += 2;
2987 if (p_j2k->m_private_image->numcomps <= 256) {
2990 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2991 l_current_data += 2;
2993 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2996 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2997 l_remaining_size -= 6;
3000 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3001 l_current_data += 2;
3003 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3006 l_remaining_size -= 6;
3009 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3011 *p_data_written = l_qcc_size;
3015 * Gets the maximum size taken by a qcc.
3017 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3019 return opj_j2k_get_max_coc_size(p_j2k);
3023 * Reads a QCC marker (Quantization component)
3024 * @param p_header_data the data contained in the QCC box.
3025 * @param p_j2k the jpeg2000 codec.
3026 * @param p_header_size the size of the data contained in the QCC marker.
3027 * @param p_manager the user event manager.
3029 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3030 OPJ_BYTE * p_header_data,
3031 OPJ_UINT32 p_header_size,
3032 opj_event_mgr_t * p_manager
3035 OPJ_UINT32 l_num_comp,l_comp_no;
3038 assert(p_header_data != 00);
3039 assert(p_j2k != 00);
3040 assert(p_manager != 00);
3042 l_num_comp = p_j2k->m_private_image->numcomps;
3044 if (l_num_comp <= 256) {
3045 if (p_header_size < 1) {
3046 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3049 opj_read_bytes(p_header_data,&l_comp_no,1);
3054 if (p_header_size < 2) {
3055 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3058 opj_read_bytes(p_header_data,&l_comp_no,2);
3064 if (p_j2k->m_cp.correct) {
3066 static OPJ_UINT32 backup_compno = 0;
3068 /* compno is negative or larger than the number of components!!! */
3069 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3070 opj_event_msg_v2(p_manager, EVT_ERROR,
3071 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3072 l_comp_no, l_num_comp);
3074 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3077 /* we try to correct */
3078 l_comp_no = backup_compno % l_num_comp;
3079 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3080 "- setting component number to %d\n",
3084 /* keep your private count of tiles */
3087 #endif /* USE_JPWL */
3089 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3090 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3094 if (p_header_size != 0) {
3095 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3103 static void j2k_write_poc(opj_j2k_t *j2k) {
3104 int len, numpchgs, i;
3106 int numcomps = j2k->image->numcomps;
3108 opj_cp_t *cp = j2k->cp;
3109 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3110 opj_tccp_t *tccp = &tcp->tccps[0];
3111 opj_cio_t *cio = j2k->cio;
3113 numpchgs = 1 + tcp->numpocs;
3114 cio_write(cio, J2K_MS_POC, 2); /* POC */
3115 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3116 cio_write(cio, len, 2); /* Lpoc */
3117 for (i = 0; i < numpchgs; i++) {
3118 opj_poc_t *poc = &tcp->pocs[i];
3119 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3120 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3121 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3122 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3123 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3124 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3125 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3126 poc->compno1 = int_min(poc->compno1, numcomps);
3127 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3132 * Writes the POC marker (Progression Order Change)
3134 * @param p_stream the stream to write data to.
3135 * @param p_j2k J2K codec.
3136 * @param p_manager the user event manager.
3138 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3139 struct opj_stream_private *p_stream,
3140 struct opj_event_mgr * p_manager )
3142 OPJ_UINT32 l_nb_comp;
3143 OPJ_UINT32 l_nb_poc;
3144 OPJ_UINT32 l_poc_size;
3145 OPJ_UINT32 l_written_size = 0;
3146 opj_tcp_v2_t *l_tcp = 00;
3147 opj_tccp_t *l_tccp = 00;
3148 OPJ_UINT32 l_poc_room;
3151 assert(p_j2k != 00);
3152 assert(p_manager != 00);
3153 assert(p_stream != 00);
3155 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3156 l_tccp = &l_tcp->tccps[0];
3157 l_nb_comp = p_j2k->m_private_image->numcomps;
3158 l_nb_poc = 1 + l_tcp->numpocs;
3160 if (l_nb_comp <= 256) {
3166 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3168 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3169 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3170 = (OPJ_BYTE*)opj_realloc(
3171 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3174 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3178 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3181 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3183 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) {
3192 * Writes the POC marker (Progression Order Change)
3194 * @param p_stream the stream to write data to.
3195 * @param p_j2k J2K codec.
3196 * @param p_manager the user event manager.
3198 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3200 OPJ_UINT32 * p_data_written,
3201 struct opj_event_mgr * p_manager )
3204 OPJ_BYTE * l_current_data = 00;
3205 OPJ_UINT32 l_nb_comp;
3206 OPJ_UINT32 l_nb_poc;
3207 OPJ_UINT32 l_poc_size;
3208 opj_image_t *l_image = 00;
3209 opj_tcp_v2_t *l_tcp = 00;
3210 opj_tccp_t *l_tccp = 00;
3211 opj_poc_t *l_current_poc = 00;
3212 OPJ_UINT32 l_poc_room;
3215 assert(p_j2k != 00);
3216 assert(p_manager != 00);
3218 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3219 l_tccp = &l_tcp->tccps[0];
3220 l_image = p_j2k->m_private_image;
3221 l_nb_comp = l_image->numcomps;
3222 l_nb_poc = 1 + l_tcp->numpocs;
3224 if (l_nb_comp <= 256) {
3231 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3233 l_current_data = p_data;
3235 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3236 l_current_data += 2;
3238 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3239 l_current_data += 2;
3241 l_current_poc = l_tcp->pocs;
3242 for (i = 0; i < l_nb_poc; ++i) {
3243 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3246 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3247 l_current_data+=l_poc_room;
3249 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3252 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3255 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3256 l_current_data+=l_poc_room;
3258 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3261 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3262 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3263 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3264 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3269 *p_data_written = l_poc_size;
3273 * Gets the maximum size taken by the writing of a POC.
3275 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3277 opj_tcp_v2_t * l_tcp = 00;
3278 OPJ_UINT32 l_nb_tiles = 0;
3279 OPJ_UINT32 l_max_poc = 0;
3282 l_tcp = p_j2k->m_cp.tcps;
3283 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3285 for (i=0;i<l_nb_tiles;++i) {
3286 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3292 return 4 + 9 * l_max_poc;
3296 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3298 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3301 OPJ_UINT32 l_nb_tiles;
3302 OPJ_UINT32 l_max = 0;
3303 opj_tcp_v2_t * l_tcp = 00;
3305 l_tcp = p_j2k->m_cp.tcps;
3306 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3308 for (i=0;i<l_nb_tiles;++i) {
3309 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3319 * Gets the maximum size taken by the headers of the SOT.
3321 * @param p_j2k the jpeg2000 codec to use.
3323 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3325 OPJ_UINT32 l_nb_bytes = 0;
3326 OPJ_UINT32 l_nb_comps;
3327 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3329 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3330 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3332 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3333 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3334 l_nb_bytes += l_nb_comps * l_coc_bytes;
3336 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3337 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3340 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3342 /*** DEVELOPER CORNER, Add room for your headers ***/
3349 * Reads a POC marker (Progression Order Change)
3351 * @param p_header_data the data contained in the POC box.
3352 * @param p_j2k the jpeg2000 codec.
3353 * @param p_header_size the size of the data contained in the POC marker.
3354 * @param p_manager the user event manager.
3356 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3357 OPJ_BYTE * p_header_data,
3358 OPJ_UINT32 p_header_size,
3359 opj_event_mgr_t * p_manager
3362 OPJ_UINT32 i, l_nb_comp, l_tmp;
3363 opj_image_t * l_image = 00;
3364 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3365 OPJ_UINT32 l_chunk_size, l_comp_room;
3367 opj_cp_v2_t *l_cp = 00;
3368 opj_tcp_v2_t *l_tcp = 00;
3369 opj_poc_t *l_current_poc = 00;
3372 assert(p_header_data != 00);
3373 assert(p_j2k != 00);
3374 assert(p_manager != 00);
3376 l_image = p_j2k->m_private_image;
3377 l_nb_comp = l_image->numcomps;
3378 if (l_nb_comp <= 256) {
3384 l_chunk_size = 5 + 2 * l_comp_room;
3385 l_current_poc_nb = p_header_size / l_chunk_size;
3386 l_current_poc_remaining = p_header_size % l_chunk_size;
3388 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3389 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3393 l_cp = &(p_j2k->m_cp);
3394 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3395 &l_cp->tcps[p_j2k->m_current_tile_number] :
3396 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3397 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3398 l_current_poc_nb += l_old_poc_nb;
3400 assert(l_current_poc_nb < 32);
3402 /* now poc is in use.*/
3405 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3406 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3407 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3409 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3410 p_header_data+=l_comp_room;
3411 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3413 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3415 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3416 p_header_data+=l_comp_room;
3417 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3419 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3420 /* make sure comp is in acceptable bounds */
3421 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3425 l_tcp->numpocs = l_current_poc_nb - 1;
3430 * Reads a CRG marker (Component registration)
3432 * @param p_header_data the data contained in the TLM box.
3433 * @param p_j2k the jpeg2000 codec.
3434 * @param p_header_size the size of the data contained in the TLM marker.
3435 * @param p_manager the user event manager.
3437 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3438 OPJ_BYTE * p_header_data,
3439 OPJ_UINT32 p_header_size,
3440 opj_event_mgr_t * p_manager
3443 OPJ_UINT32 l_nb_comp;
3445 assert(p_header_data != 00);
3446 assert(p_j2k != 00);
3447 assert(p_manager != 00);
3449 l_nb_comp = p_j2k->m_private_image->numcomps;
3451 if (p_header_size != l_nb_comp *4) {
3452 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3455 /* Do not care of this at the moment since only local variables are set here */
3458 (i = 0; i < l_nb_comp; ++i)
3460 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3462 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3470 * Reads a TLM marker (Tile Length Marker)
3472 * @param p_header_data the data contained in the TLM box.
3473 * @param p_j2k the jpeg2000 codec.
3474 * @param p_header_size the size of the data contained in the TLM marker.
3475 * @param p_manager the user event manager.
3477 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3478 OPJ_BYTE * p_header_data,
3479 OPJ_UINT32 p_header_size,
3480 opj_event_mgr_t * p_manager
3483 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3485 assert(p_header_data != 00);
3486 assert(p_j2k != 00);
3487 assert(p_manager != 00);
3489 if (p_header_size < 2) {
3490 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3495 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3497 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3500 l_ST = ((l_Stlm >> 4) & 0x3);
3501 l_SP = (l_Stlm >> 6) & 0x1;
3503 l_Ptlm_size = (l_SP + 1) * 2;
3504 l_quotient = l_Ptlm_size + l_ST;
3506 l_tot_num_tp = p_header_size / l_quotient;
3507 l_tot_num_tp_remaining = p_header_size % l_quotient;
3509 if (l_tot_num_tp_remaining != 0) {
3510 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3513 /* FIXME Do not care of this at the moment since only local variables are set here */
3516 (i = 0; i < l_tot_num_tp; ++i)
3518 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3519 p_header_data += l_ST;
3520 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3521 p_header_data += l_Ptlm_size;
3528 * Reads a PLM marker (Packet length, main header marker)
3530 * @param p_header_data the data contained in the TLM box.
3531 * @param p_j2k the jpeg2000 codec.
3532 * @param p_header_size the size of the data contained in the TLM marker.
3533 * @param p_manager the user event manager.
3535 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3536 OPJ_BYTE * p_header_data,
3537 OPJ_UINT32 p_header_size,
3538 opj_event_mgr_t * p_manager
3542 assert(p_header_data != 00);
3543 assert(p_j2k != 00);
3544 assert(p_manager != 00);
3546 if (p_header_size < 1) {
3547 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3550 /* Do not care of this at the moment since only local variables are set here */
3552 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3559 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3561 p_header_size -= (1+l_Nplm);
3565 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3569 (i = 0; i < l_Nplm; ++i)
3571 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3573 // take only the last seven bytes
3574 l_packet_len |= (l_tmp & 0x7f);
3582 // store packet length and proceed to next packet
3589 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3598 * Reads a PLT marker (Packet length, tile-part header)
3600 * @param p_header_data the data contained in the PLT box.
3601 * @param p_j2k the jpeg2000 codec.
3602 * @param p_header_size the size of the data contained in the PLT marker.
3603 * @param p_manager the user event manager.
3605 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3606 OPJ_BYTE * p_header_data,
3607 OPJ_UINT32 p_header_size,
3608 opj_event_mgr_t * p_manager
3611 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3614 assert(p_header_data != 00);
3615 assert(p_j2k != 00);
3616 assert(p_manager != 00);
3618 if (p_header_size < 1) {
3619 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3623 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3627 for (i = 0; i < p_header_size; ++i) {
3628 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3630 /* take only the last seven bytes */
3631 l_packet_len |= (l_tmp & 0x7f);
3636 /* store packet length and proceed to next packet */
3641 if (l_packet_len != 0) {
3642 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3651 * Reads a PPM marker (Packed packet headers, main header)
3653 * @param p_header_data the data contained in the POC box.
3654 * @param p_j2k the jpeg2000 codec.
3655 * @param p_header_size the size of the data contained in the POC marker.
3656 * @param p_manager the user event manager.
3659 opj_bool j2k_read_ppm_v2 (
3660 opj_j2k_v2_t *p_j2k,
3661 OPJ_BYTE * p_header_data,
3662 OPJ_UINT32 p_header_size,
3663 struct opj_event_mgr * p_manager
3667 opj_cp_v2_t *l_cp = 00;
3668 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3671 assert(p_header_data != 00);
3672 assert(p_j2k != 00);
3673 assert(p_manager != 00);
3675 if (p_header_size < 1) {
3676 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3680 l_cp = &(p_j2k->m_cp);
3683 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3687 /* First PPM marker */
3689 if (p_header_size < 4) {
3690 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3694 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3698 /* First PPM marker: Initialization */
3699 l_cp->ppm_len = l_N_ppm;
3700 l_cp->ppm_data_size = 0;
3702 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3703 if (l_cp->ppm_buffer == 00) {
3704 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3707 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3709 l_cp->ppm_data = l_cp->ppm_buffer;
3713 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3714 if (p_header_size >= 4) {
3716 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3719 l_cp->ppm_len += l_N_ppm ;
3721 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3722 if (l_cp->ppm_buffer == 00) {
3723 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3726 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3728 l_cp->ppm_data = l_cp->ppm_buffer;
3735 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3737 if (l_remaining_data <= p_header_size) {
3738 /* we must store less information than available in the packet */
3739 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3740 l_cp->ppm_data_size = l_cp->ppm_len;
3741 p_header_size -= l_remaining_data;
3742 p_header_data += l_remaining_data;
3745 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3746 l_cp->ppm_data_size += p_header_size;
3747 p_header_data += p_header_size;
3760 * Reads a PPM marker (Packed packet headers, main header)
3762 * @param p_header_data the data contained in the POC box.
3763 * @param p_j2k the jpeg2000 codec.
3764 * @param p_header_size the size of the data contained in the POC marker.
3765 * @param p_manager the user event manager.
3767 opj_bool j2k_read_ppm_v3 (
3768 opj_j2k_v2_t *p_j2k,
3769 OPJ_BYTE * p_header_data,
3770 OPJ_UINT32 p_header_size,
3771 struct opj_event_mgr * p_manager
3774 opj_cp_v2_t *l_cp = 00;
3775 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3778 assert(p_header_data != 00);
3779 assert(p_j2k != 00);
3780 assert(p_manager != 00);
3782 /* Minimum size of PPM marker is equal to the size of Zppm element */
3783 if (p_header_size < 1) {
3784 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3788 l_cp = &(p_j2k->m_cp);
3791 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3795 /* First PPM marker */
3797 /* We need now at least the Nppm^0 element */
3798 if (p_header_size < 4) {
3799 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3803 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3807 /* First PPM marker: Initialization */
3808 l_cp->ppm_len = l_N_ppm;
3809 l_cp->ppm_data_read = 0;
3811 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3812 if (l_cp->ppm_data == 00) {
3813 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3816 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3818 l_cp->ppm_data_current = l_cp->ppm_data;
3820 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3823 if (p_header_size < 4) {
3824 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3828 /* Uncompleted Ippm series in the previous PPM marker?*/
3829 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3830 /* Get the place where add the remaining Ippm series*/
3831 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3832 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3835 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3839 /* Increase the size of ppm_data to add the new Ippm series*/
3840 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3842 /* Keep the position of the place where concatenate the new series*/
3843 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3844 l_cp->ppm_len += l_N_ppm;
3849 l_remaining_data = p_header_size;
3851 while (l_remaining_data >= l_N_ppm) {
3852 /* read a complete Ippm series*/
3853 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3854 p_header_size -= l_N_ppm;
3855 p_header_data += l_N_ppm;
3857 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3861 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3866 l_remaining_data = p_header_size;
3870 l_remaining_data = p_header_size;
3872 /* Next Ippm series is a complete series ?*/
3873 if (l_remaining_data > l_N_ppm) {
3874 /* Increase the size of ppm_data to add the new Ippm series*/
3875 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3877 /* Keep the position of the place where concatenate the new series */
3878 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3879 l_cp->ppm_len += l_N_ppm;
3884 /* Need to read an incomplete Ippm series*/
3885 if (l_remaining_data) {
3886 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3888 /* Keep the position of the place where concatenate the new series*/
3889 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3890 l_cp->ppm_len += l_N_ppm;
3892 /* Read incomplete Ippm series*/
3893 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3894 p_header_size -= l_remaining_data;
3895 p_header_data += l_remaining_data;
3897 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3902 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3903 if (p_header_size >= 4) {
3905 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3908 l_cp->ppm_len += l_N_ppm ;
3910 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3911 if (l_cp->ppm_buffer == 00) {
3912 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3915 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3917 l_cp->ppm_data = l_cp->ppm_buffer;
3924 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3926 if (l_remaining_data <= p_header_size) {
3927 /* we must store less information than available in the packet */
3928 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3929 l_cp->ppm_data_size = l_cp->ppm_len;
3930 p_header_size -= l_remaining_data;
3931 p_header_data += l_remaining_data;
3934 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3935 l_cp->ppm_data_size += p_header_size;
3936 p_header_data += p_header_size;
3946 * Reads a PPT marker (Packed packet headers, tile-part header)
3948 * @param p_header_data the data contained in the PPT box.
3949 * @param p_j2k the jpeg2000 codec.
3950 * @param p_header_size the size of the data contained in the PPT marker.
3951 * @param p_manager the user event manager.
3953 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3954 OPJ_BYTE * p_header_data,
3955 OPJ_UINT32 p_header_size,
3956 opj_event_mgr_t * p_manager
3959 opj_cp_v2_t *l_cp = 00;
3960 opj_tcp_v2_t *l_tcp = 00;
3964 assert(p_header_data != 00);
3965 assert(p_j2k != 00);
3966 assert(p_manager != 00);
3968 /* We need to have the Z_ppt element at minimum */
3969 if (p_header_size < 1) {
3970 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3974 l_cp = &(p_j2k->m_cp);
3976 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");
3980 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3983 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3987 /* Allocate buffer to read the packet header */
3989 /* First PPT marker */
3990 l_tcp->ppt_data_size = 0;
3991 l_tcp->ppt_len = p_header_size;
3993 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3994 if (l_tcp->ppt_buffer == 00) {
3995 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3998 l_tcp->ppt_data = l_tcp->ppt_buffer;
4000 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4003 l_tcp->ppt_len += p_header_size;
4005 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4006 if (l_tcp->ppt_buffer == 00) {
4007 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4010 l_tcp->ppt_data = l_tcp->ppt_buffer;
4012 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4015 /* Read packet header from buffer */
4016 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4018 l_tcp->ppt_data_size += p_header_size;
4024 * Writes the TLM marker (Tile Length Marker)
4026 * @param p_stream the stream to write data to.
4027 * @param p_j2k J2K codec.
4028 * @param p_manager the user event manager.
4030 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4031 struct opj_stream_private *p_stream,
4032 struct opj_event_mgr * p_manager )
4034 OPJ_BYTE * l_current_data = 00;
4035 OPJ_UINT32 l_tlm_size;
4038 assert(p_j2k != 00);
4039 assert(p_manager != 00);
4040 assert(p_stream != 00);
4042 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4044 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4045 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4046 = (OPJ_BYTE*)opj_realloc(
4047 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4050 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4054 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4057 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4059 /* change the way data is written to avoid seeking if possible */
4061 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4063 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4064 l_current_data += 2;
4066 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4067 l_current_data += 2;
4069 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4072 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4075 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4076 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) {
4083 static void j2k_write_tlm(opj_j2k_t *j2k){
4085 opj_cio_t *cio = j2k->cio;
4086 j2k->tlm_start = cio_tell(cio);
4087 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4088 lenp = 4 + (5*j2k->totnum_tp);
4089 cio_write(cio,lenp,2); /* Ltlm */
4090 cio_write(cio, 0,1); /* Ztlm=0*/
4091 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4092 cio_skip(cio,5*j2k->totnum_tp);
4095 static void j2k_write_sot(opj_j2k_t *j2k) {
4098 opj_cio_t *cio = j2k->cio;
4100 j2k->sot_start = cio_tell(cio);
4101 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4102 lenp = cio_tell(cio);
4103 cio_skip(cio, 2); /* Lsot (further) */
4104 cio_write(cio, j2k->curtileno, 2); /* Isot */
4105 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4106 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4107 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4108 len = cio_tell(cio) - lenp;
4109 cio_seek(cio, lenp);
4110 cio_write(cio, len, 2); /* Lsot */
4111 cio_seek(cio, lenp + len);
4115 /* update markers struct */
4116 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4117 #endif /* USE_JPWL */
4120 if( j2k->cstr_info && j2k->cur_tp_num==0){
4121 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4126 * Writes the SOT marker (Start of tile-part)
4128 * @param p_stream the stream to write data to.
4129 * @param p_j2k J2K codec.
4130 * @param p_manager the user event manager.
4132 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4134 OPJ_UINT32 * p_data_written,
4135 const struct opj_stream_private *p_stream,
4136 struct opj_event_mgr * p_manager )
4139 assert(p_j2k != 00);
4140 assert(p_manager != 00);
4141 assert(p_stream != 00);
4143 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4146 opj_write_bytes(p_data,10,2); /* Lsot */
4149 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4155 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4158 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4163 /* update markers struct */
4165 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4167 assert( 0 && "TODO" );
4168 #endif /* USE_JPWL */
4170 * p_data_written = 12;
4179 * Reads a PPT marker (Packed packet headers, tile-part header)
4181 * @param p_header_data the data contained in the PPT box.
4182 * @param p_j2k the jpeg2000 codec.
4183 * @param p_header_size the size of the data contained in the PPT marker.
4184 * @param p_manager the user event manager.
4186 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4187 OPJ_BYTE * p_header_data,
4188 OPJ_UINT32 p_header_size,
4189 opj_event_mgr_t * p_manager )
4191 opj_cp_v2_t *l_cp = 00;
4192 opj_tcp_v2_t *l_tcp = 00;
4193 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4194 OPJ_UINT32 l_current_part;
4195 OPJ_UINT32 l_tile_x,l_tile_y;
4198 assert(p_header_data != 00);
4199 assert(p_j2k != 00);
4200 assert(p_manager != 00);
4202 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4203 if (p_header_size != 8) {
4204 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4208 l_cp = &(p_j2k->m_cp);
4209 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4212 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4213 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4214 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4217 if (l_cp->correct) {
4219 int tileno = p_j2k->m_current_tile_number;
4220 static int backup_tileno = 0;
4222 /* tileno is negative or larger than the number of tiles!!! */
4223 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4224 opj_event_msg_v2(p_manager, EVT_ERROR,
4225 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4226 tileno, (l_cp->tw * l_cp->th));
4228 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4231 /* we try to correct */
4232 tileno = backup_tileno;
4233 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4234 "- setting tile number to %d\n",
4238 /* keep your private count of tiles */
4241 #endif /* USE_JPWL */
4243 /* look for the tile in the list of already processed tile (in parts). */
4244 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4245 /* since the time taken by this function can only grow at the time */
4247 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4250 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4251 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4253 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4259 if (l_cp->correct) {
4261 /* totlen is negative or larger than the bytes left!!! */
4262 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))) { */
4263 opj_event_msg_v2(p_manager, EVT_ERROR,
4264 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4265 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4267 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4270 /* we try to correct */
4272 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4273 "- setting Psot to %d => assuming it is the last tile\n",
4277 #endif /* USE_JPWL */
4279 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4281 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4282 "we assuming it is the last tile-part of the codestream.\n");
4283 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4286 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4289 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4292 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4293 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4294 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4295 if (l_tcp->m_nb_tile_parts) {
4296 if (l_current_part >= l_tcp->m_nb_tile_parts){
4297 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4298 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4299 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4303 l_tcp->m_nb_tile_parts = l_num_parts;
4306 /* If know the number of tile part header we will check if we didn't read the last*/
4307 if (l_tcp->m_nb_tile_parts) {
4308 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4309 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4313 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4314 /* Keep the size of data to skip after this marker */
4315 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4318 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4319 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4322 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4324 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4325 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4326 p_j2k->m_specific_param.m_decoder.m_skip_data =
4327 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4328 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4329 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4330 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4333 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4334 p_j2k->m_specific_param.m_decoder.m_skip_data =
4335 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4339 if (p_j2k->cstr_index)
4341 assert(p_j2k->cstr_index->tile_index != 00);
4342 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4343 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4345 if (l_num_parts != 0){
4346 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4347 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4350 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4351 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4352 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4354 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4355 (opj_tp_index_t*)opj_realloc(
4356 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4357 l_num_parts* sizeof(opj_tp_index_t));
4360 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4362 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4363 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4364 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4365 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4366 sizeof(opj_tp_index_t));
4369 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4370 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4371 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4372 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4373 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4374 * sizeof(opj_tp_index_t));
4383 /* 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 */
4384 /* if (p_j2k->cstr_info) {
4387 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4390 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4391 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4392 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4393 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4396 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4399 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4403 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4406 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4407 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4408 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4413 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4416 opj_tcp_t *tcp = NULL;
4417 opj_codestream_info_t *cstr_info = NULL;
4419 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4420 opj_cp_t *cp = j2k->cp;
4421 opj_cio_t *cio = j2k->cio;
4423 tcd->tp_num = j2k->tp_num ;
4424 tcd->cur_tp_num = j2k->cur_tp_num;
4426 cio_write(cio, J2K_MS_SOD, 2);
4428 if( j2k->cstr_info && j2k->cur_tp_num==0){
4429 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4432 if (j2k->curtileno == 0) {
4433 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4437 cstr_info = j2k->cstr_info;
4439 if (!j2k->cur_tp_num ) {
4440 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4441 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4444 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4445 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4449 /* update markers struct */
4450 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4451 #endif /* USE_JPWL */
4456 tcp = &cp->tcps[j2k->curtileno];
4457 for (layno = 0; layno < tcp->numlayers; layno++) {
4458 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4459 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4460 } else if (tcp->rates[layno]) {
4461 tcp->rates[layno]=1;
4464 if(j2k->cur_tp_num == 0){
4465 tcd->tcd_image->tiles->packno = 0;
4467 cstr_info->packno = 0;
4470 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4472 /* Writing Psot in SOT marker */
4473 totlen = cio_tell(cio) + l - j2k->sot_start;
4474 cio_seek(cio, j2k->sot_start + 6);
4475 cio_write(cio, totlen, 4);
4476 cio_seek(cio, j2k->sot_start + totlen);
4477 /* Writing Ttlm and Ptlm in TLM marker */
4479 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4480 cio_write(cio, j2k->curtileno, 1);
4481 cio_write(cio, totlen, 4);
4483 cio_seek(cio, j2k->sot_start + totlen);
4487 * Writes the SOD marker (Start of data)
4489 * @param p_stream the stream to write data to.
4490 * @param p_j2k J2K codec.
4491 * @param p_manager the user event manager.
4493 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4494 struct opj_tcd_v2 * p_tile_coder,
4496 OPJ_UINT32 * p_data_written,
4497 OPJ_UINT32 p_total_data_size,
4498 const struct opj_stream_private *p_stream,
4499 struct opj_event_mgr * p_manager )
4501 opj_tcp_v2_t *l_tcp = 00;
4502 opj_codestream_info_t *l_cstr_info = 00;
4503 opj_cp_v2_t *l_cp = 00;
4505 OPJ_UINT32 l_size_tile;
4506 OPJ_UINT32 l_remaining_data;
4509 assert(p_j2k != 00);
4510 assert(p_manager != 00);
4511 assert(p_stream != 00);
4513 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4516 /* make room for the EOF marker */
4517 l_remaining_data = p_total_data_size - 4;
4519 l_cp = &(p_j2k->m_cp);
4520 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4523 /* update tile coder */
4524 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4525 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4527 l_size_tile = l_cp->th * l_cp->tw;
4530 /* TODO mergeV2: check this part which use cstr_info */
4531 /*l_cstr_info = p_j2k->cstr_info;
4533 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4534 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4535 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4541 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4543 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4548 /* update markers struct */
4549 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4551 assert( 0 && "TODO" );
4552 #endif /* USE_JPWL */
4557 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4558 p_tile_coder->tcd_image->tiles->packno = 0;
4560 l_cstr_info->packno = 0;
4564 *p_data_written = 0;
4566 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)) {
4567 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4571 *p_data_written += 2;
4580 * Reads a SOD marker (Start Of Data)
4582 * @param p_header_data the data contained in the SOD box.
4583 * @param p_j2k the jpeg2000 codec.
4584 * @param p_header_size the size of the data contained in the SOD marker.
4585 * @param p_manager the user event manager.
4587 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4588 opj_stream_private_t *p_stream,
4589 opj_event_mgr_t * p_manager
4592 OPJ_UINT32 l_current_read_size;
4593 opj_codestream_index_t * l_cstr_index = 00;
4594 OPJ_BYTE ** l_current_data = 00;
4595 opj_tcp_v2_t * l_tcp = 00;
4596 OPJ_UINT32 * l_tile_len = 00;
4599 assert(p_j2k != 00);
4600 assert(p_manager != 00);
4601 assert(p_stream != 00);
4603 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4605 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4606 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4607 // but we are in the last tile part,
4608 // so its result will fit on OPJ_UINT32 unless we find
4609 // a file with a single tile part of more than 4 GB...*/
4610 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4613 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4615 l_current_data = &(l_tcp->m_data);
4616 l_tile_len = &l_tcp->m_data_size;
4618 if (! *l_current_data) {
4619 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4622 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4625 if (*l_current_data == 00) {
4626 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4632 l_cstr_index = p_j2k->cstr_index;
4634 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4636 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4637 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4639 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4640 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4642 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4646 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4648 /*l_cstr_index->packno = 0;*/
4651 l_current_read_size = opj_stream_read_data( p_stream,
4652 *l_current_data + *l_tile_len,
4653 p_j2k->m_specific_param.m_decoder.m_sot_length,
4656 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4657 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4660 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4663 *l_tile_len += l_current_read_size;
4669 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4670 opj_cp_t *cp = j2k->cp;
4671 opj_tcp_t *tcp = &cp->tcps[tileno];
4672 opj_cio_t *cio = j2k->cio;
4673 int numcomps = j2k->image->numcomps;
4675 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4676 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4677 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4678 cio_write(cio, 0, 1); /* Srgn */
4679 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4683 * Writes the RGN marker (Region Of Interest)
4685 * @param p_tile_no the tile to output
4686 * @param p_comp_no the component to output
4687 * @param p_stream the stream to write data to.
4688 * @param p_j2k J2K codec.
4689 * @param p_manager the user event manager.
4691 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4692 OPJ_UINT32 p_tile_no,
4693 OPJ_UINT32 p_comp_no,
4694 struct opj_stream_private *p_stream,
4695 struct opj_event_mgr * p_manager )
4697 OPJ_BYTE * l_current_data = 00;
4698 OPJ_UINT32 l_nb_comp;
4699 OPJ_UINT32 l_rgn_size;
4700 opj_image_t *l_image = 00;
4701 opj_cp_v2_t *l_cp = 00;
4702 opj_tcp_v2_t *l_tcp = 00;
4703 opj_tccp_t *l_tccp = 00;
4704 OPJ_UINT32 l_comp_room;
4707 assert(p_j2k != 00);
4708 assert(p_manager != 00);
4709 assert(p_stream != 00);
4711 l_cp = &(p_j2k->m_cp);
4712 l_tcp = &l_cp->tcps[p_tile_no];
4713 l_tccp = &l_tcp->tccps[p_comp_no];
4715 l_nb_comp = l_image->numcomps;
4717 if (l_nb_comp <= 256) {
4724 l_rgn_size = 6 + l_comp_room;
4726 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4728 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4729 l_current_data += 2;
4731 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4732 l_current_data += 2;
4734 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4735 l_current_data+=l_comp_room;
4737 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4740 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4743 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) {
4751 static void j2k_write_eoc(opj_j2k_t *j2k) {
4752 opj_cio_t *cio = j2k->cio;
4753 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4754 cio_write(cio, J2K_MS_EOC, 2);
4758 /* update markers struct */
4759 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4760 #endif /* USE_JPWL */
4765 * Writes the EOC marker (End of Codestream)
4767 * @param p_stream the stream to write data to.
4768 * @param p_j2k J2K codec.
4769 * @param p_manager the user event manager.
4771 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4772 struct opj_stream_private *p_stream,
4773 struct opj_event_mgr * p_manager )
4776 assert(p_j2k != 00);
4777 assert(p_manager != 00);
4778 assert(p_stream != 00);
4780 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4785 /* update markers struct */
4787 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4789 #endif /* USE_JPWL */
4791 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4795 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4804 * Reads a RGN marker (Region Of Interest)
4806 * @param p_header_data the data contained in the POC box.
4807 * @param p_j2k the jpeg2000 codec.
4808 * @param p_header_size the size of the data contained in the POC marker.
4809 * @param p_manager the user event manager.
4811 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4812 OPJ_BYTE * p_header_data,
4813 OPJ_UINT32 p_header_size,
4814 opj_event_mgr_t * p_manager
4817 OPJ_UINT32 l_nb_comp;
4818 opj_image_t * l_image = 00;
4820 opj_cp_v2_t *l_cp = 00;
4821 opj_tcp_v2_t *l_tcp = 00;
4822 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4825 assert(p_header_data != 00);
4826 assert(p_j2k != 00);
4827 assert(p_manager != 00);
4829 l_image = p_j2k->m_private_image;
4830 l_nb_comp = l_image->numcomps;
4832 if (l_nb_comp <= 256) {
4837 if (p_header_size != 2 + l_comp_room) {
4838 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4842 l_cp = &(p_j2k->m_cp);
4843 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4844 &l_cp->tcps[p_j2k->m_current_tile_number] :
4845 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4847 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4848 p_header_data+=l_comp_room;
4849 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4853 if (l_cp->correct) {
4854 /* totlen is negative or larger than the bytes left!!! */
4855 if (l_comp_room >= l_nb_comp) {
4856 opj_event_msg_v2(p_manager, EVT_ERROR,
4857 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4858 l_comp_room, l_nb_comp);
4859 if (!JPWL_ASSUME || JPWL_ASSUME) {
4860 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4865 #endif /* USE_JPWL */
4867 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4874 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
4876 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4879 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
4886 * Updates the rates of the tcp.
4888 * @param p_stream the stream to write data to.
4889 * @param p_j2k J2K codec.
4890 * @param p_manager the user event manager.
4892 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4893 struct opj_stream_private *p_stream,
4894 struct opj_event_mgr * p_manager )
4896 opj_cp_v2_t * l_cp = 00;
4897 opj_image_t * l_image = 00;
4898 opj_tcp_v2_t * l_tcp = 00;
4899 opj_image_comp_t * l_img_comp = 00;
4902 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4903 OPJ_FLOAT32 * l_rates = 0;
4904 OPJ_FLOAT32 l_sot_remove;
4905 OPJ_UINT32 l_bits_empty, l_size_pixel;
4906 OPJ_UINT32 l_tile_size = 0;
4907 OPJ_UINT32 l_last_res;
4908 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4911 assert(p_j2k != 00);
4912 assert(p_manager != 00);
4913 assert(p_stream != 00);
4916 l_cp = &(p_j2k->m_cp);
4917 l_image = p_j2k->m_private_image;
4920 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4921 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4922 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4924 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4925 l_tp_stride_func = get_tp_stride;
4928 l_tp_stride_func = get_default_stride;
4931 for (i=0;i<l_cp->th;++i) {
4932 for (j=0;j<l_cp->tw;++j) {
4933 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4935 /* 4 borders of the tile rescale on the image if necessary */
4936 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4937 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4938 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4939 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4941 l_rates = l_tcp->rates;
4943 /* Modification of the RATE >> */
4945 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4947 ((*l_rates) * l_bits_empty)
4955 for (k = 1; k < l_tcp->numlayers; ++k) {
4957 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4959 ((*l_rates) * l_bits_empty)
4975 for (i=0;i<l_cp->th;++i) {
4976 for (j=0;j<l_cp->tw;++j) {
4977 l_rates = l_tcp->rates;
4980 *l_rates -= l_sot_remove;
4982 if (*l_rates < 30) {
4989 l_last_res = l_tcp->numlayers - 1;
4991 for (k = 1; k < l_last_res; ++k) {
4994 *l_rates -= l_sot_remove;
4996 if (*l_rates < *(l_rates - 1) + 10) {
4997 *l_rates = (*(l_rates - 1)) + 20;
5005 *l_rates -= (l_sot_remove + 2.f);
5007 if (*l_rates < *(l_rates - 1) + 10) {
5008 *l_rates = (*(l_rates - 1)) + 20;
5016 l_img_comp = l_image->comps;
5019 for (i=0;i<l_image->numcomps;++i) {
5020 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5022 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5030 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5032 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5034 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5035 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5036 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5037 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5041 if (l_cp->m_specific_param.m_enc.m_cinema) {
5042 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5043 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5044 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5048 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5049 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5055 static void j2k_read_eoc(opj_j2k_t *j2k) {
5057 opj_bool success = OPJ_FALSE;
5059 /* if packets should be decoded */
5060 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5061 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5062 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5063 for (i = 0; i < j2k->cp->tileno_size; i++) {
5064 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5065 if (j2k->cp->tileno[i] != -1)
5067 tileno = j2k->cp->tileno[i];
5068 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5069 opj_free(j2k->tile_data[tileno]);
5070 j2k->tile_data[tileno] = NULL;
5071 tcd_free_decode_tile(tcd, i);
5074 success = OPJ_FALSE;
5075 if (success == OPJ_FALSE) {
5076 j2k->state |= J2K_STATE_ERR;
5080 tcd_free_decode(tcd);
5083 /* if packets should not be decoded */
5085 for (i = 0; i < j2k->cp->tileno_size; i++) {
5086 tileno = j2k->cp->tileno[i];
5087 opj_free(j2k->tile_data[tileno]);
5088 j2k->tile_data[tileno] = NULL;
5091 if (j2k->state & J2K_STATE_ERR)
5092 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5094 j2k->state = J2K_STATE_MT;
5098 * Reads a EOC marker (End Of Codestream)
5100 * @param p_header_data the data contained in the SOD box.
5101 * @param p_j2k the jpeg2000 codec.
5102 * @param p_header_size the size of the data contained in the SOD marker.
5103 * @param p_manager the user event manager.
5106 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5107 struct opj_stream_private *p_stream,
5108 struct opj_event_mgr * p_manager )
5111 opj_tcd_v2_t * l_tcd = 00;
5112 OPJ_UINT32 l_nb_tiles;
5113 opj_tcp_v2_t * l_tcp = 00;
5117 assert(p_j2k != 00);
5118 assert(p_manager != 00);
5119 assert(p_stream != 00);
5121 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5122 l_tcp = p_j2k->m_cp.tcps;
5124 l_tcd = tcd_create_v2(OPJ_TRUE);
5126 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5130 for (i = 0; i < l_nb_tiles; ++i) {
5131 if (l_tcp->m_data) {
5132 if (! tcd_init_decode_tile(l_tcd, i)) {
5133 tcd_destroy_v2(l_tcd);
5134 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5138 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5142 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5147 opj_j2k_tcp_destroy(l_tcp);
5151 tcd_destroy_v2(l_tcd);
5157 * Gets the offset of the header.
5159 * @param p_stream the stream to write data to.
5160 * @param p_j2k J2K codec.
5161 * @param p_manager the user event manager.
5163 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5164 struct opj_stream_private *p_stream,
5165 struct opj_event_mgr * p_manager )
5168 assert(p_j2k != 00);
5169 assert(p_manager != 00);
5170 assert(p_stream != 00);
5172 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5178 * Writes the MCT marker (Multiple Component Transform)
5180 * @param p_stream the stream to write data to.
5181 * @param p_j2k J2K codec.
5182 * @param p_manager the user event manager.
5184 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5185 struct opj_stream_private *p_stream,
5186 struct opj_event_mgr * p_manager )
5189 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5190 opj_mct_data_t * l_mct_record;
5191 opj_tcp_v2_t * l_tcp;
5194 assert(p_j2k != 00);
5195 assert(p_stream != 00);
5196 assert(p_manager != 00);
5198 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5202 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5203 l_mct_record = l_tcp->m_mct_records;
5205 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5207 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5214 l_mcc_record = l_tcp->m_mcc_records;
5216 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5218 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5225 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5233 * Writes the image components.
5235 * @param p_stream the stream to write data to.
5236 * @param p_j2k J2K codec.
5237 * @param p_manager the user event manager.
5239 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5240 struct opj_stream_private *p_stream,
5241 struct opj_event_mgr * p_manager )
5246 assert(p_j2k != 00);
5247 assert(p_manager != 00);
5248 assert(p_stream != 00);
5250 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5252 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
5256 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
5265 * Writes regions of interests.
5267 * @param p_stream the stream to write data to.
5268 * @param p_j2k J2K codec.
5269 * @param p_manager the user event manager.
5271 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5272 struct opj_stream_private *p_stream,
5273 struct opj_event_mgr * p_manager )
5276 const opj_tccp_t *l_tccp = 00;
5279 assert(p_j2k != 00);
5280 assert(p_manager != 00);
5281 assert(p_stream != 00);
5283 l_tccp = p_j2k->m_cp.tcps->tccps;
5285 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5286 if (l_tccp->roishift) {
5288 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5302 * @param p_stream the stream to write data to.
5303 * @param p_j2k J2K codec.
5304 * @param p_manager the user event manager.
5306 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5307 struct opj_stream_private *p_stream,
5308 struct opj_event_mgr * p_manager )
5310 opj_codestream_index_t * l_cstr_index = 00;
5313 assert(p_j2k != 00);
5314 assert(p_manager != 00);
5315 assert(p_stream != 00);
5317 l_cstr_index = p_j2k->cstr_index;
5319 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5321 /* The following adjustment is done to adjust the codestream size */
5322 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5323 /* the first bunch of bytes is not in the codestream */
5324 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5329 /* preparation of JPWL marker segments */
5333 /* encode according to JPWL */
5334 jpwl_encode(p_j2k, p_stream, image);
5338 assert( 0 && "TODO" );
5339 #endif /* USE_JPWL */
5344 typedef struct opj_dec_mstabent {
5347 /** value of the state when the marker can appear */
5349 /** action linked to the marker */
5350 void (*handler) (opj_j2k_t *j2k);
5351 } opj_dec_mstabent_t;
5353 opj_dec_mstabent_t j2k_dec_mstab[] = {
5354 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5355 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5356 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5357 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5358 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5359 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5360 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5361 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5362 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5363 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5364 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5365 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5366 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5367 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5368 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5369 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5371 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5372 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5375 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5376 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5377 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5378 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5379 #endif /* USE_JPWL */
5381 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5382 {J2K_MS_INSEC, 0, j2k_read_insec},
5383 #endif /* USE_JPSEC */
5385 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5391 * Reads an unknown marker
5393 * @param p_stream the stream object to read from.
5394 * @param p_j2k the jpeg2000 codec.
5395 * @param p_manager the user event manager.
5397 * @return true if the marker could be deduced.
5399 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5400 opj_stream_private_t *p_stream,
5401 OPJ_UINT32 *output_marker,
5402 opj_event_mgr_t * p_manager
5405 OPJ_UINT32 l_unknown_marker;
5406 const opj_dec_memory_marker_handler_t * l_marker_handler;
5407 OPJ_UINT32 l_size_unk = 2;
5410 assert(p_j2k != 00);
5411 assert(p_manager != 00);
5412 assert(p_stream != 00);
5414 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5417 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5418 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5419 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5423 /* read 2 bytes as the new marker ID*/
5424 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5426 if (!(l_unknown_marker < 0xff00)) {
5428 /* Get the marker handler from the marker ID*/
5429 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5431 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5432 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5436 if (l_marker_handler->id != J2K_MS_UNK) {
5437 /* Add the marker to the codestream index*/
5438 if (l_marker_handler->id != J2K_MS_SOT)
5439 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5440 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5442 break; /* next marker is known and well located */
5450 *output_marker = l_marker_handler->id ;
5456 Read the lookup table containing all the marker, status and action
5457 @param id Marker value
5459 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5460 opj_dec_mstabent_t *e;
5461 for (e = j2k_dec_mstab; e->id != 0; e++) {
5470 * Writes the MCT marker (Multiple Component Transform)
5472 * @param p_stream the stream to write data to.
5473 * @param p_j2k J2K codec.
5474 * @param p_manager the user event manager.
5476 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5477 opj_mct_data_t * p_mct_record,
5478 struct opj_stream_private *p_stream,
5479 struct opj_event_mgr * p_manager )
5481 OPJ_UINT32 l_mct_size;
5482 OPJ_BYTE * l_current_data = 00;
5486 assert(p_j2k != 00);
5487 assert(p_manager != 00);
5488 assert(p_stream != 00);
5490 l_mct_size = 10 + p_mct_record->m_data_size;
5492 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5493 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5494 = (OPJ_BYTE*)opj_realloc(
5495 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5498 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5502 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5505 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5507 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5508 l_current_data += 2;
5510 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5511 l_current_data += 2;
5513 opj_write_bytes(l_current_data,0,2); /* Zmct */
5514 l_current_data += 2;
5516 /* only one marker atm */
5517 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5519 opj_write_bytes(l_current_data,l_tmp,2);
5520 l_current_data += 2;
5522 opj_write_bytes(l_current_data,0,2); /* Ymct */
5525 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5527 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) {
5535 * Reads a MCT marker (Multiple Component Transform)
5537 * @param p_header_data the data contained in the MCT box.
5538 * @param p_j2k the jpeg2000 codec.
5539 * @param p_header_size the size of the data contained in the MCT marker.
5540 * @param p_manager the user event manager.
5542 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5543 OPJ_BYTE * p_header_data,
5544 OPJ_UINT32 p_header_size,
5545 opj_event_mgr_t * p_manager
5549 opj_tcp_v2_t *l_tcp = 00;
5552 opj_mct_data_t * l_mct_data;
5555 assert(p_header_data != 00);
5556 assert(p_j2k != 00);
5558 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5559 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5560 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5562 if (p_header_size < 2) {
5563 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5568 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5571 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5575 if(p_header_size <= 6) {
5576 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5580 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5581 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5584 l_indix = l_tmp & 0xff;
5585 l_mct_data = l_tcp->m_mct_records;
5587 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5588 if (l_mct_data->m_index == l_indix) {
5595 if (i == l_tcp->m_nb_mct_records) {
5596 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5597 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5599 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));
5600 if(! l_tcp->m_mct_records) {
5601 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5605 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5606 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5609 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5612 if (l_mct_data->m_data) {
5613 opj_free(l_mct_data->m_data);
5614 l_mct_data->m_data = 00;
5617 l_mct_data->m_index = l_indix;
5618 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5619 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5621 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5624 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5630 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5631 if (! l_mct_data->m_data) {
5632 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5635 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5637 l_mct_data->m_data_size = p_header_size;
5638 ++l_tcp->m_nb_mct_records;
5644 * Writes the MCC marker (Multiple Component Collection)
5646 * @param p_stream the stream to write data to.
5647 * @param p_j2k J2K codec.
5648 * @param p_manager the user event manager.
5650 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5651 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5652 struct opj_stream_private *p_stream,
5653 struct opj_event_mgr * p_manager )
5656 OPJ_UINT32 l_mcc_size;
5657 OPJ_BYTE * l_current_data = 00;
5658 OPJ_UINT32 l_nb_bytes_for_comp;
5663 assert(p_j2k != 00);
5664 assert(p_manager != 00);
5665 assert(p_stream != 00);
5667 if (p_mcc_record->m_nb_comps > 255 ) {
5668 l_nb_bytes_for_comp = 2;
5672 l_nb_bytes_for_comp = 1;
5676 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5677 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5679 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5680 = (OPJ_BYTE*)opj_realloc(
5681 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5683 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5687 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5690 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5692 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5693 l_current_data += 2;
5695 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5696 l_current_data += 2;
5699 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5700 l_current_data += 2;
5702 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5705 /* only one marker atm */
5706 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5709 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5712 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5715 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 */
5718 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5719 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5720 l_current_data+=l_nb_bytes_for_comp;
5723 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 */
5726 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5728 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5729 l_current_data+=l_nb_bytes_for_comp;
5732 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5734 if (p_mcc_record->m_decorrelation_array) {
5735 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5738 if (p_mcc_record->m_offset_array) {
5739 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5742 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5745 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) {
5753 * Reads a MCC marker (Multiple Component Collection)
5755 * @param p_header_data the data contained in the MCC box.
5756 * @param p_j2k the jpeg2000 codec.
5757 * @param p_header_size the size of the data contained in the MCC marker.
5758 * @param p_manager the user event manager.
5760 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5761 OPJ_BYTE * p_header_data,
5762 OPJ_UINT32 p_header_size,
5763 opj_event_mgr_t * p_manager )
5768 opj_tcp_v2_t * l_tcp;
5769 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5770 opj_mct_data_t * l_mct_data;
5771 OPJ_UINT32 l_nb_collections;
5772 OPJ_UINT32 l_nb_comps;
5773 OPJ_UINT32 l_nb_bytes_by_comp;
5777 assert(p_header_data != 00);
5778 assert(p_j2k != 00);
5779 assert(p_manager != 00);
5781 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5782 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5783 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5785 if (p_header_size < 2) {
5786 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5791 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5794 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5798 if (p_header_size < 7) {
5799 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5803 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5806 l_mcc_record = l_tcp->m_mcc_records;
5808 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5809 if (l_mcc_record->m_index == l_indix) {
5816 if (i == l_tcp->m_nb_mcc_records) {
5817 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5818 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5820 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5821 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5822 if (! l_tcp->m_mcc_records) {
5823 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5826 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5827 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));
5829 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5831 l_mcc_record->m_index = l_indix;
5833 /* only one marker atm */
5834 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5837 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5841 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5844 if (l_nb_collections > 1) {
5845 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5851 for (i=0;i<l_nb_collections;++i) {
5852 if (p_header_size < 3) {
5853 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5857 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5861 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5865 opj_read_bytes(p_header_data,&l_nb_comps,2);
5870 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5871 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5873 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5874 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5878 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5880 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5881 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5882 p_header_data+=l_nb_bytes_by_comp;
5885 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5890 opj_read_bytes(p_header_data,&l_nb_comps,2);
5893 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5894 l_nb_comps &= 0x7fff;
5896 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5897 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5901 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5902 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5906 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5908 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5909 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5910 p_header_data+=l_nb_bytes_by_comp;
5913 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5918 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5921 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5922 l_mcc_record->m_decorrelation_array = 00;
5923 l_mcc_record->m_offset_array = 00;
5925 l_indix = l_tmp & 0xff;
5927 l_mct_data = l_tcp->m_mct_records;
5928 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5929 if (l_mct_data->m_index == l_indix) {
5930 l_mcc_record->m_decorrelation_array = l_mct_data;
5936 if (l_mcc_record->m_decorrelation_array == 00) {
5937 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5942 l_indix = (l_tmp >> 8) & 0xff;
5944 l_mct_data = l_tcp->m_mct_records;
5945 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5946 if (l_mct_data->m_index == l_indix) {
5947 l_mcc_record->m_offset_array = l_mct_data;
5953 if (l_mcc_record->m_offset_array == 00) {
5954 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5960 if (p_header_size != 0) {
5961 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5965 ++l_tcp->m_nb_mcc_records;
5972 * Writes the MCO marker (Multiple component transformation ordering)
5974 * @param p_stream the stream to write data to.
5975 * @param p_j2k J2K codec.
5976 * @param p_manager the user event manager.
5978 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
5979 struct opj_stream_private *p_stream,
5980 struct opj_event_mgr * p_manager
5983 OPJ_BYTE * l_current_data = 00;
5984 OPJ_UINT32 l_mco_size;
5985 opj_tcp_v2_t * l_tcp = 00;
5986 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5990 assert(p_j2k != 00);
5991 assert(p_manager != 00);
5992 assert(p_stream != 00);
5994 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5995 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5997 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5998 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6000 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6001 = (OPJ_BYTE*)opj_realloc(
6002 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6004 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6009 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6012 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6013 l_current_data += 2;
6015 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6016 l_current_data += 2;
6018 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6021 l_mcc_record = l_tcp->m_mcc_records;
6022 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6023 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6029 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) {
6037 * Reads a MCO marker (Multiple Component Transform Ordering)
6039 * @param p_header_data the data contained in the MCO box.
6040 * @param p_j2k the jpeg2000 codec.
6041 * @param p_header_size the size of the data contained in the MCO marker.
6042 * @param p_manager the user event manager.
6044 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6045 OPJ_BYTE * p_header_data,
6046 OPJ_UINT32 p_header_size,
6047 opj_event_mgr_t * p_manager
6050 OPJ_UINT32 l_tmp, i;
6051 OPJ_UINT32 l_nb_stages;
6052 opj_tcp_v2_t * l_tcp;
6053 opj_tccp_t * l_tccp;
6054 opj_image_t * l_image;
6055 opj_image_comp_t * l_img_comp;
6058 assert(p_header_data != 00);
6059 assert(p_j2k != 00);
6060 assert(p_manager != 00);
6062 l_image = p_j2k->m_private_image;
6063 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6064 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6065 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6067 if (p_header_size < 1) {
6068 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6072 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6075 if (l_nb_stages > 1) {
6076 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6080 if (p_header_size != l_nb_stages + 1) {
6081 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6085 l_tccp = l_tcp->tccps;
6086 l_img_comp = l_image->comps;
6088 for (i=0;i<l_image->numcomps;++i) {
6089 l_tccp->m_dc_level_shift = 0;
6093 if (l_tcp->m_mct_decoding_matrix) {
6094 opj_free(l_tcp->m_mct_decoding_matrix);
6095 l_tcp->m_mct_decoding_matrix = 00;
6098 for (i=0;i<l_nb_stages;++i) {
6099 opj_read_bytes(p_header_data,&l_tmp,1);
6102 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6110 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6113 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6114 opj_mct_data_t * l_deco_array, * l_offset_array;
6115 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6116 OPJ_UINT32 l_nb_elem;
6117 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6118 opj_tccp_t * l_tccp;
6121 assert(p_tcp != 00);
6123 l_mcc_record = p_tcp->m_mcc_records;
6125 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6126 if (l_mcc_record->m_index == p_index) {
6131 if (i==p_tcp->m_nb_mcc_records) {
6132 /** element discarded **/
6136 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6137 /** do not support number of comps != image */
6141 l_deco_array = l_mcc_record->m_decorrelation_array;
6144 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6145 if (l_deco_array->m_data_size != l_data_size) {
6149 l_nb_elem = p_image->numcomps * p_image->numcomps;
6150 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6151 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6153 if (! p_tcp->m_mct_decoding_matrix ) {
6157 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);
6160 l_offset_array = l_mcc_record->m_offset_array;
6162 if (l_offset_array) {
6163 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6164 if (l_offset_array->m_data_size != l_data_size) {
6168 l_nb_elem = p_image->numcomps;
6169 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6170 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6172 if (! l_offset_data ) {
6176 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6178 l_tccp = p_tcp->tccps;
6179 l_current_offset_data = l_offset_data;
6181 for (i=0;i<p_image->numcomps;++i) {
6182 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6186 opj_free(l_offset_data);
6193 * Writes the CBD marker (Component bit depth definition)
6195 * @param p_stream the stream to write data to.
6196 * @param p_j2k J2K codec.
6197 * @param p_manager the user event manager.
6199 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6200 struct opj_stream_private *p_stream,
6201 struct opj_event_mgr * p_manager )
6204 OPJ_UINT32 l_cbd_size;
6205 OPJ_BYTE * l_current_data = 00;
6206 opj_image_t *l_image = 00;
6207 opj_image_comp_t * l_comp = 00;
6210 assert(p_j2k != 00);
6211 assert(p_manager != 00);
6212 assert(p_stream != 00);
6214 l_image = p_j2k->m_private_image;
6215 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6217 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6218 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6219 = (OPJ_BYTE*)opj_realloc(
6220 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6223 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6227 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6230 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6232 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6233 l_current_data += 2;
6235 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6236 l_current_data += 2;
6238 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6241 l_comp = l_image->comps;
6243 for (i=0;i<l_image->numcomps;++i) {
6244 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6250 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) {
6258 * Reads a CBD marker (Component bit depth definition)
6259 * @param p_header_data the data contained in the CBD box.
6260 * @param p_j2k the jpeg2000 codec.
6261 * @param p_header_size the size of the data contained in the CBD marker.
6262 * @param p_manager the user event manager.
6264 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6265 OPJ_BYTE * p_header_data,
6266 OPJ_UINT32 p_header_size,
6267 opj_event_mgr_t * p_manager
6270 OPJ_UINT32 l_nb_comp,l_num_comp;
6271 OPJ_UINT32 l_comp_def;
6273 opj_image_comp_t * l_comp = 00;
6276 assert(p_header_data != 00);
6277 assert(p_j2k != 00);
6278 assert(p_manager != 00);
6280 l_num_comp = p_j2k->m_private_image->numcomps;
6282 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6283 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6287 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6290 if (l_nb_comp != l_num_comp) {
6291 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6295 l_comp = p_j2k->m_private_image->comps;
6296 for (i=0;i<l_num_comp;++i) {
6297 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6299 l_comp->sgnd = (l_comp_def>>7) & 1;
6300 l_comp->prec = (l_comp_def&0x7f) + 1;
6308 /* ----------------------------------------------------------------------- */
6309 /* J2K / JPT decoder interface */
6310 /* ----------------------------------------------------------------------- */
6312 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6314 if(j2k && parameters) {
6315 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6316 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6319 j2k->m_cp.correct = parameters->jpwl_correct;
6320 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6321 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6322 #endif /* USE_JPWL */
6327 /* ----------------------------------------------------------------------- */
6328 /* J2K encoder interface */
6329 /* ----------------------------------------------------------------------- */
6331 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6332 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6339 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6341 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6346 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6348 l_j2k->m_is_decoder = 0;
6349 l_j2k->m_cp.m_is_decoder = 0;
6351 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6352 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6353 opj_j2k_destroy(l_j2k);
6357 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6359 /* validation list creation*/
6360 l_j2k->m_validation_list = opj_procedure_list_create();
6361 if (! l_j2k->m_validation_list) {
6362 opj_j2k_destroy(l_j2k);
6366 /* execution list creation*/
6367 l_j2k->m_procedure_list = opj_procedure_list_create();
6368 if (! l_j2k->m_procedure_list) {
6369 opj_j2k_destroy(l_j2k);
6376 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6378 int tileno, numpocs_tile;
6379 opj_cp_t *cp = NULL;
6381 if(!j2k || !parameters || ! image) {
6385 /* create and initialize the coding parameters structure */
6386 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6388 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6391 /* set default values for cp */
6396 copy user encoding parameters
6398 cp->cinema = parameters->cp_cinema;
6399 cp->max_comp_size = parameters->max_comp_size;
6400 cp->rsiz = parameters->cp_rsiz;
6401 cp->disto_alloc = parameters->cp_disto_alloc;
6402 cp->fixed_alloc = parameters->cp_fixed_alloc;
6403 cp->fixed_quality = parameters->cp_fixed_quality;
6405 /* mod fixed_quality */
6406 if(parameters->cp_matrice) {
6407 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6408 cp->matrice = (int *) opj_malloc(array_size);
6409 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6413 cp->tdx = parameters->cp_tdx;
6414 cp->tdy = parameters->cp_tdy;
6417 cp->tx0 = parameters->cp_tx0;
6418 cp->ty0 = parameters->cp_ty0;
6420 /* comment string */
6421 if(parameters->cp_comment) {
6422 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6424 strcpy(cp->comment, parameters->cp_comment);
6429 calculate other encoding parameters
6432 if (parameters->tile_size_on) {
6433 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6434 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6436 cp->tdx = image->x1 - cp->tx0;
6437 cp->tdy = image->y1 - cp->ty0;
6440 if(parameters->tp_on){
6441 cp->tp_flag = parameters->tp_flag;
6446 for(i=0;i<image->numcomps ;i++){
6447 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6453 calculate JPWL encoding parameters
6456 if (parameters->jpwl_epc_on) {
6460 cp->epc_on = OPJ_TRUE;
6461 cp->info_on = OPJ_FALSE; /* no informative technique */
6464 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6465 cp->epb_on = OPJ_TRUE;
6467 cp->hprot_MH = parameters->jpwl_hprot_MH;
6468 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6469 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6470 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6472 /* if tile specs are not specified, copy MH specs */
6473 if (cp->hprot_TPH[0] == -1) {
6474 cp->hprot_TPH_tileno[0] = 0;
6475 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6477 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6478 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6479 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6480 cp->pprot[i] = parameters->jpwl_pprot[i];
6484 /* set ESD writing */
6485 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6486 cp->esd_on = OPJ_TRUE;
6488 cp->sens_size = parameters->jpwl_sens_size;
6489 cp->sens_addr = parameters->jpwl_sens_addr;
6490 cp->sens_range = parameters->jpwl_sens_range;
6492 cp->sens_MH = parameters->jpwl_sens_MH;
6493 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6494 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6495 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6499 /* always set RED writing to false: we are at the encoder */
6500 cp->red_on = OPJ_FALSE;
6503 cp->epc_on = OPJ_FALSE;
6505 #endif /* USE_JPWL */
6508 /* initialize the mutiple tiles */
6509 /* ---------------------------- */
6510 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6512 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6513 opj_tcp_t *tcp = &cp->tcps[tileno];
6514 tcp->numlayers = parameters->tcp_numlayers;
6515 assert ( tcp->numlayers >= 0 );
6516 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6518 if (cp->fixed_quality) {
6519 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6521 tcp->rates[j] = parameters->tcp_rates[j];
6523 if (cp->fixed_quality) { /* add fixed_quality */
6524 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6526 tcp->rates[j] = parameters->tcp_rates[j];
6530 tcp->csty = parameters->csty;
6531 tcp->prg = parameters->prog_order;
6532 tcp->mct = parameters->tcp_mct;
6536 if (parameters->numpocs) {
6537 /* initialisation of POC */
6539 assert( parameters->numpocs >= 0 );
6540 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6541 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6542 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6543 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6544 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6545 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6546 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6547 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6548 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6549 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6553 tcp->numpocs = numpocs_tile -1 ;
6558 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6560 for (i = 0; i < image->numcomps; i++) {
6561 opj_tccp_t *tccp = &tcp->tccps[i];
6562 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6563 tccp->numresolutions = parameters->numresolution;
6564 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6565 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6566 tccp->cblksty = parameters->mode;
6567 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6568 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6570 assert(parameters->roi_compno >= 0);
6571 if (i == (OPJ_UINT32)parameters->roi_compno) {
6572 tccp->roishift = parameters->roi_shift;
6577 if(parameters->cp_cinema)
6579 /*Precinct size for lowest frequency subband=128*/
6582 /*Precinct size at all other resolutions = 256*/
6583 for (j = 1; j < tccp->numresolutions; j++) {
6588 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6590 assert(tccp->numresolutions > 0);
6591 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6592 if (p < parameters->res_spec) {
6594 if (parameters->prcw_init[p] < 1) {
6597 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6600 if (parameters->prch_init[p] < 1) {
6603 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6607 int res_spec = parameters->res_spec;
6608 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6609 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6611 if (size_prcw < 1) {
6614 tccp->prcw[j] = int_floorlog2(size_prcw);
6617 if (size_prch < 1) {
6620 tccp->prch[j] = int_floorlog2(size_prch);
6624 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6627 for (j = 0; j < tccp->numresolutions; j++) {
6634 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6639 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6640 opj_cparameters_t *parameters,
6642 struct opj_event_mgr * p_manager)
6644 OPJ_UINT32 i, j, tileno, numpocs_tile;
6645 opj_cp_v2_t *cp = 00;
6648 if(!p_j2k || !parameters || ! image) {
6652 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6653 cp = &(p_j2k->m_cp);
6655 /* set default values for cp */
6660 copy user encoding parameters
6662 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6663 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6664 cp->rsiz = parameters->cp_rsiz;
6665 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6666 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6667 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6669 /* mod fixed_quality */
6670 if (parameters->cp_matrice) {
6671 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6672 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6673 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6677 cp->tdx = parameters->cp_tdx;
6678 cp->tdy = parameters->cp_tdy;
6681 cp->tx0 = parameters->cp_tx0;
6682 cp->ty0 = parameters->cp_ty0;
6684 /* comment string */
6685 if(parameters->cp_comment) {
6686 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6688 strcpy(cp->comment, parameters->cp_comment);
6693 calculate other encoding parameters
6696 if (parameters->tile_size_on) {
6697 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6698 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6700 cp->tdx = image->x1 - cp->tx0;
6701 cp->tdy = image->y1 - cp->ty0;
6704 if (parameters->tp_on) {
6705 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6706 cp->m_specific_param.m_enc.m_tp_on = 1;
6711 calculate JPWL encoding parameters
6714 if (parameters->jpwl_epc_on) {
6718 cp->epc_on = OPJ_TRUE;
6719 cp->info_on = OPJ_FALSE; /* no informative technique */
6722 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6723 cp->epb_on = OPJ_TRUE;
6725 cp->hprot_MH = parameters->jpwl_hprot_MH;
6726 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6727 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6728 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6730 /* if tile specs are not specified, copy MH specs */
6731 if (cp->hprot_TPH[0] == -1) {
6732 cp->hprot_TPH_tileno[0] = 0;
6733 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6735 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6736 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6737 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6738 cp->pprot[i] = parameters->jpwl_pprot[i];
6742 /* set ESD writing */
6743 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6744 cp->esd_on = OPJ_TRUE;
6746 cp->sens_size = parameters->jpwl_sens_size;
6747 cp->sens_addr = parameters->jpwl_sens_addr;
6748 cp->sens_range = parameters->jpwl_sens_range;
6750 cp->sens_MH = parameters->jpwl_sens_MH;
6751 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6752 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6753 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6757 /* always set RED writing to false: we are at the encoder */
6758 cp->red_on = OPJ_FALSE;
6761 cp->epc_on = OPJ_FALSE;
6763 #endif /* USE_JPWL */
6766 /* initialize the mutiple tiles */
6767 /* ---------------------------- */
6768 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6769 if (parameters->numpocs) {
6770 /* initialisation of POC */
6771 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6775 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6776 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6777 tcp->numlayers = parameters->tcp_numlayers;
6779 for (j = 0; j < tcp->numlayers; j++) {
6780 if(cp->m_specific_param.m_enc.m_cinema){
6781 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6782 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6784 tcp->rates[j] = parameters->tcp_rates[j];
6786 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6787 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6789 tcp->rates[j] = parameters->tcp_rates[j];
6794 tcp->csty = parameters->csty;
6795 tcp->prg = parameters->prog_order;
6796 tcp->mct = parameters->tcp_mct;
6801 if (parameters->numpocs) {
6802 /* initialisation of POC */
6805 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6806 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6807 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6809 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6810 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6811 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6812 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6813 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6814 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6815 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6821 tcp->numpocs = numpocs_tile -1 ;
6826 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6828 if (parameters->mct_data) {
6830 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6833 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6834 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6835 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6836 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6839 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6840 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6841 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6843 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6844 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6846 tcp->mct_norms = (OPJ_FLOAT64*)
6847 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6849 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6852 for (i = 0; i < image->numcomps; i++) {
6853 opj_tccp_t *tccp = &tcp->tccps[i];
6854 tccp->m_dc_level_shift = l_dc_shift[i];
6857 opj_j2k_setup_mct_encoding(tcp,image);
6861 for (i = 0; i < image->numcomps; i++) {
6862 opj_tccp_t *tccp = &tcp->tccps[i];
6863 opj_image_comp_t * l_comp = &(image->comps[i]);
6865 if (! l_comp->sgnd) {
6866 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6871 for (i = 0; i < image->numcomps; i++) {
6872 opj_tccp_t *tccp = &tcp->tccps[i];
6874 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6875 tccp->numresolutions = parameters->numresolution;
6876 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6877 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6878 tccp->cblksty = parameters->mode;
6879 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6880 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6883 if (i == parameters->roi_compno) {
6884 tccp->roishift = parameters->roi_shift;
6889 if(parameters->cp_cinema) {
6890 //Precinct size for lowest frequency subband=128
6893 //Precinct size at all other resolutions = 256
6894 for (j = 1; j < tccp->numresolutions; j++) {
6899 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6900 OPJ_INT32 p = 0, it_res;
6901 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6902 if (p < parameters->res_spec) {
6904 if (parameters->prcw_init[p] < 1) {
6905 tccp->prcw[it_res] = 1;
6907 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6910 if (parameters->prch_init[p] < 1) {
6911 tccp->prch[it_res] = 1;
6913 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6917 int res_spec = parameters->res_spec;
6918 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6919 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6921 if (size_prcw < 1) {
6922 tccp->prcw[it_res] = 1;
6924 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6927 if (size_prch < 1) {
6928 tccp->prch[it_res] = 1;
6930 tccp->prch[it_res] = int_floorlog2(size_prch);
6934 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6937 for (j = 0; j < tccp->numresolutions; j++) {
6944 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6948 if (parameters->mct_data) {
6949 opj_free(parameters->mct_data);
6950 parameters->mct_data = 00;
6957 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6959 assert(cstr_info != 00);
6961 /* expand the list? */
6962 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
6963 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
6964 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
6967 /* add the marker */
6968 cstr_info->marker[cstr_info->marknum].type = type;
6969 cstr_info->marker[cstr_info->marknum].pos = pos;
6970 cstr_info->marker[cstr_info->marknum].len = len;
6971 cstr_info->marknum++;
6975 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6977 assert(cstr_index != 00);
6979 /* expand the list? */
6980 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6981 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6982 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6985 /* add the marker */
6986 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6987 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6988 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6989 cstr_index->marknum++;
6993 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6995 opj_marker_info_t *marker;
6997 assert(cstr_info != 00);
6999 /* expand the list? */
7000 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7001 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7002 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7005 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7007 /* add the marker */
7008 marker->type = type;
7011 cstr_info->tile[tileno].marknum++;
7014 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)
7016 assert(cstr_index != 00);
7017 assert(cstr_index->tile_index != 00);
7019 /* expand the list? */
7020 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7021 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7022 cstr_index->tile_index[tileno].marker =
7023 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7024 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7027 /* add the marker */
7028 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7029 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7030 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7031 cstr_index->tile_index[tileno].marknum++;
7033 if (type == J2K_MS_SOT) {
7034 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7036 if (cstr_index->tile_index[tileno].tp_index)
7037 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7044 * -----------------------------------------------------------------------
7045 * -----------------------------------------------------------------------
7046 * -----------------------------------------------------------------------
7050 * Ends the decompression procedures and possibiliy add data to be read after the
7053 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
7054 opj_stream_private_t *p_stream,
7055 opj_event_mgr_t * p_manager
7065 * Reads a jpeg2000 codestream header structure.
7068 * @param p_stream the stream to read data from.
7069 * @param p_j2k the jpeg2000 codec.
7070 * @param p_manager the user event manager.
7072 * @return true if the box is valid.
7074 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
7075 opj_j2k_v2_t* p_j2k,
7076 opj_image_t** p_image,
7077 opj_event_mgr_t* p_manager )
7080 assert(p_j2k != 00);
7081 assert(p_stream != 00);
7082 assert(p_manager != 00);
7084 /* create an empty image header */
7085 p_j2k->m_private_image = opj_image_create0();
7086 if (! p_j2k->m_private_image) {
7090 /* customization of the validation */
7091 opj_j2k_setup_decoding_validation(p_j2k);
7093 /* validation of the parameters codec */
7094 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7095 opj_image_destroy(p_j2k->m_private_image);
7096 p_j2k->m_private_image = NULL;
7100 /* customization of the encoding */
7101 opj_j2k_setup_header_reading(p_j2k);
7104 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7105 opj_image_destroy(p_j2k->m_private_image);
7106 p_j2k->m_private_image = NULL;
7110 *p_image = opj_image_create0();
7115 /* Copy codestream image information to the output image */
7116 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7118 /*Allocate and initialize some elements of codestrem index*/
7119 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
7127 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7129 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7132 assert(p_j2k != 00);
7134 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
7136 /* DEVELOPER CORNER, add your custom procedures */
7137 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
7142 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7143 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7145 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7148 assert(p_j2k != 00);
7150 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
7151 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
7152 /* DEVELOPER CORNER, add your custom validation procedure */
7158 * The mct encoding validation procedure.
7160 * @param p_j2k the jpeg2000 codec to validate.
7161 * @param p_stream the input stream to validate.
7162 * @param p_manager the user event manager.
7164 * @return true if the parameters are correct.
7166 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7167 opj_stream_private_t *p_stream,
7168 opj_event_mgr_t * p_manager )
7170 opj_bool l_is_valid = OPJ_TRUE;
7174 assert(p_j2k != 00);
7175 assert(p_stream != 00);
7176 assert(p_manager != 00);
7178 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7179 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7180 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7182 for (i=0;i<l_nb_tiles;++i) {
7183 if (l_tcp->mct == 2) {
7184 opj_tccp_t * l_tccp = l_tcp->tccps;
7185 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7187 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7188 l_is_valid &= ! (l_tccp->qmfbid & 1);
7199 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7202 OPJ_UINT32 l_indix = 1;
7203 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7204 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7205 OPJ_UINT32 l_mct_size,l_nb_elem;
7206 OPJ_FLOAT32 * l_data, * l_current_data;
7207 opj_tccp_t * l_tccp;
7210 assert(p_tcp != 00);
7212 if (p_tcp->mct != 2) {
7216 if (p_tcp->m_mct_decoding_matrix) {
7217 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7218 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7220 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));
7221 if (! p_tcp->m_mct_records) {
7224 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7226 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7228 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7230 if (l_mct_deco_data->m_data) {
7231 opj_free(l_mct_deco_data->m_data);
7232 l_mct_deco_data->m_data = 00;
7235 l_mct_deco_data->m_index = l_indix++;
7236 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7237 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7238 l_nb_elem = p_image->numcomps * p_image->numcomps;
7239 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7240 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7242 if (! l_mct_deco_data->m_data) {
7246 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);
7248 l_mct_deco_data->m_data_size = l_mct_size;
7249 ++p_tcp->m_nb_mct_records;
7252 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7253 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7254 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));
7256 if (! p_tcp->m_mct_records) {
7260 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7261 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7263 if (l_mct_deco_data) {
7264 l_mct_deco_data = l_mct_offset_data - 1;
7268 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7270 if (l_mct_offset_data->m_data) {
7271 opj_free(l_mct_offset_data->m_data);
7272 l_mct_offset_data->m_data = 00;
7275 l_mct_offset_data->m_index = l_indix++;
7276 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7277 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7278 l_nb_elem = p_image->numcomps;
7279 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7280 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7282 if (! l_mct_offset_data->m_data) {
7286 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7288 opj_free(l_mct_offset_data->m_data);
7289 l_mct_offset_data->m_data = 00;
7293 l_tccp = p_tcp->tccps;
7294 l_current_data = l_data;
7296 for (i=0;i<l_nb_elem;++i) {
7297 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7301 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7305 l_mct_offset_data->m_data_size = l_mct_size;
7307 ++p_tcp->m_nb_mct_records;
7309 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7310 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7311 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7312 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7314 if (! p_tcp->m_mcc_records) {
7317 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7318 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));
7322 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7323 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7324 l_mcc_data->m_is_irreversible = 1;
7325 l_mcc_data->m_nb_comps = p_image->numcomps;
7326 l_mcc_data->m_index = l_indix++;
7327 l_mcc_data->m_offset_array = l_mct_offset_data;
7328 ++p_tcp->m_nb_mcc_records;
7334 * Builds the cp decoder parameters to use to decode tile.
7336 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7337 opj_stream_private_t *p_stream,
7338 opj_event_mgr_t * p_manager )
7340 /* add here initialization of cp
7341 copy paste of setup_decoder */
7349 * Builds the cp encoder parameters to use to encode tile.
7351 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7352 opj_stream_private_t *p_stream,
7353 opj_event_mgr_t * p_manager )
7355 /* add here initialization of cp
7356 copy paste of setup_encoder */
7364 * The default encoding validation procedure without any extension.
7366 * @param p_j2k the jpeg2000 codec to validate.
7367 * @param p_stream the input stream to validate.
7368 * @param p_manager the user event manager.
7370 * @return true if the parameters are correct.
7372 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7373 opj_stream_private_t *p_stream,
7374 opj_event_mgr_t * p_manager )
7376 opj_bool l_is_valid = OPJ_TRUE;
7379 assert(p_j2k != 00);
7380 assert(p_stream != 00);
7381 assert(p_manager != 00);
7383 /* STATE checking */
7384 /* make sure the state is at 0 */
7385 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7387 /* POINTER validation */
7388 /* make sure a p_j2k codec is present */
7389 l_is_valid &= (p_j2k->m_procedure_list != 00);
7390 /* make sure a validation list is present */
7391 l_is_valid &= (p_j2k->m_validation_list != 00);
7393 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7394 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7398 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7399 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7403 /* PARAMETER VALIDATION */
7408 * The default decoding validation procedure without any extension.
7410 * @param p_j2k the jpeg2000 codec to validate.
7411 * @param p_stream the input stream to validate.
7412 * @param p_manager the user event manager.
7414 * @return true if the parameters are correct.
7416 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7417 opj_stream_private_t *p_stream,
7418 opj_event_mgr_t * p_manager
7421 opj_bool l_is_valid = OPJ_TRUE;
7424 assert(p_j2k != 00);
7425 assert(p_stream != 00);
7426 assert(p_manager != 00);
7429 /* STATE checking */
7430 /* make sure the state is at 0 */
7432 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7434 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7436 /* POINTER validation */
7437 /* make sure a p_j2k codec is present */
7438 /* make sure a procedure list is present */
7439 l_is_valid &= (p_j2k->m_procedure_list != 00);
7440 /* make sure a validation list is present */
7441 l_is_valid &= (p_j2k->m_validation_list != 00);
7443 /* PARAMETER VALIDATION */
7447 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7448 opj_stream_private_t *p_stream,
7449 opj_event_mgr_t * p_manager)
7451 OPJ_UINT32 l_current_marker;
7452 OPJ_UINT32 l_marker_size;
7453 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7456 assert(p_stream != 00);
7457 assert(p_j2k != 00);
7458 assert(p_manager != 00);
7460 /* We enter in the main header */
7461 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7463 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7464 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7465 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7469 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7470 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7471 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7475 /* Read 2 bytes as the new marker ID */
7476 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7478 /* Try to read until the SOT is detected */
7479 while (l_current_marker != J2K_MS_SOT) {
7481 /* Check if the current marker ID is valid */
7482 if (l_current_marker < 0xff00) {
7483 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7487 /* Get the marker handler from the marker ID */
7488 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7490 /* Manage case where marker is unknown */
7491 if (l_marker_handler->id == J2K_MS_UNK) {
7492 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7493 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7497 if (l_current_marker == J2K_MS_SOT)
7498 break; /* SOT marker is detected main header is completely read */
7499 else /* Get the marker handler from the marker ID */
7500 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7503 /* Check if the marker is known and if it is the right place to find it */
7504 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7505 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7509 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7510 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7511 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7515 /* read 2 bytes as the marker size */
7516 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7517 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7519 /* Check if the marker size is compatible with the header data size */
7520 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7521 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7522 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7523 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7526 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7529 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7530 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) {
7531 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7535 /* Read the marker segment with the correct marker handler */
7536 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7537 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7541 /* Add the marker to the codestream index*/
7542 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7543 l_marker_handler->id,
7544 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7545 l_marker_size + 4 );
7547 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7548 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7549 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7553 /* read 2 bytes as the new marker ID */
7554 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7557 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7559 /* Position of the last element if the main header */
7560 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7562 /* Next step: read a tile-part header */
7563 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7569 * Excutes the given procedures on the given codec.
7571 * @param p_procedure_list the list of procedures to execute
7572 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7573 * @param p_stream the stream to execute the procedures on.
7574 * @param p_manager the user manager.
7576 * @return true if all the procedures were successfully executed.
7578 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7579 opj_procedure_list_t * p_procedure_list,
7580 opj_stream_private_t *p_stream,
7581 opj_event_mgr_t * p_manager )
7583 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7584 opj_bool l_result = OPJ_TRUE;
7585 OPJ_UINT32 l_nb_proc, i;
7588 assert(p_procedure_list != 00);
7589 assert(p_j2k != 00);
7590 assert(p_stream != 00);
7591 assert(p_manager != 00);
7594 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7595 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);
7597 for (i=0;i<l_nb_proc;++i) {
7598 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7602 /* and clear the procedure list at the end.*/
7603 opj_procedure_list_clear(p_procedure_list);
7608 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7609 opj_stream_private_t *p_stream,
7610 opj_event_mgr_t * p_manager
7613 opj_tcp_v2_t * l_tcp = 00;
7614 opj_tcp_v2_t * l_default_tcp = 00;
7615 OPJ_UINT32 l_nb_tiles;
7617 opj_tccp_t *l_current_tccp = 00;
7618 OPJ_UINT32 l_tccp_size;
7619 OPJ_UINT32 l_mct_size;
7620 opj_image_t * l_image;
7621 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7622 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7623 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7624 OPJ_UINT32 l_offset;
7627 assert(p_j2k != 00);
7628 assert(p_stream != 00);
7629 assert(p_manager != 00);
7631 l_image = p_j2k->m_private_image;
7632 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7633 l_tcp = p_j2k->m_cp.tcps;
7634 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7635 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7636 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7639 for (i=0; i<l_nb_tiles; ++i) {
7640 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7641 l_current_tccp = l_tcp->tccps;
7642 /*Copy default coding parameters into the current tile coding parameters*/
7643 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7644 /* Initialize some values of the current tile coding parameters*/
7646 l_tcp->ppt_data = 00;
7647 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7648 l_tcp->tccps = l_current_tccp;
7650 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7651 if (l_default_tcp->m_mct_decoding_matrix) {
7652 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7653 if (! l_tcp->m_mct_decoding_matrix ) {
7656 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7659 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7660 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7661 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7662 if (! l_tcp->m_mct_records) {
7665 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7667 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7668 l_src_mct_rec = l_default_tcp->m_mct_records;
7669 l_dest_mct_rec = l_tcp->m_mct_records;
7671 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7673 if (l_src_mct_rec->m_data) {
7675 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7676 if(! l_dest_mct_rec->m_data) {
7679 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7686 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7687 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7688 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7689 if (! l_tcp->m_mcc_records) {
7692 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7694 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7695 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7696 l_dest_mcc_rec = l_tcp->m_mcc_records;
7698 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7700 if (l_src_mcc_rec->m_decorrelation_array) {
7701 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7702 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7705 if (l_src_mcc_rec->m_offset_array) {
7706 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7707 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7714 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7715 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7717 /* Move to next tile cp*/
7721 /* Create the current tile decoder*/
7722 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7723 if (! p_j2k->m_tcd ) {
7727 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7728 tcd_destroy_v2(p_j2k->m_tcd);
7730 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7738 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7739 * with the marker value.
7740 * @param p_id Marker value to look up
7742 * @return the handler associated with the id.
7744 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7746 const opj_dec_memory_marker_handler_t *e;
7747 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7748 if (e->id == p_id) {
7749 break; /* we find a handler corresponding to the marker ID*/
7757 * Destroys a jpeg2000 codec.
7759 * @param p_j2k the jpeg20000 structure to destroy.
7761 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7767 if (p_j2k->m_is_decoder) {
7769 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7770 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7771 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7772 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7775 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7776 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7777 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7778 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7783 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7784 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7785 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7788 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7789 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7790 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7791 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7794 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7795 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7796 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7797 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7801 tcd_destroy_v2(p_j2k->m_tcd);
7803 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7804 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7806 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7807 p_j2k->m_procedure_list = 00;
7809 opj_procedure_list_destroy(p_j2k->m_validation_list);
7810 p_j2k->m_procedure_list = 00;
7812 j2k_destroy_cstr_index(p_j2k->cstr_index);
7813 p_j2k->cstr_index = NULL;
7815 opj_image_destroy(p_j2k->m_private_image);
7816 p_j2k->m_private_image = NULL;
7818 opj_image_destroy(p_j2k->m_output_image);
7819 p_j2k->m_output_image = NULL;
7824 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7828 if (p_cstr_ind->marker) {
7829 opj_free(p_cstr_ind->marker);
7830 p_cstr_ind->marker = NULL;
7833 if (p_cstr_ind->tile_index) {
7834 OPJ_UINT32 it_tile = 0;
7836 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7838 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7839 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7840 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7843 if(p_cstr_ind->tile_index[it_tile].tp_index){
7844 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7845 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7848 if(p_cstr_ind->tile_index[it_tile].marker){
7849 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7850 p_cstr_ind->tile_index[it_tile].marker = NULL;
7855 opj_free( p_cstr_ind->tile_index);
7856 p_cstr_ind->tile_index = NULL;
7859 opj_free(p_cstr_ind);
7866 * Destroys a tile coding parameter structure.
7868 * @param p_tcp the tile coding parameter to destroy.
7870 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7876 if (p_tcp->ppt_buffer != 00) {
7877 opj_free(p_tcp->ppt_buffer);
7878 p_tcp->ppt_buffer = 00;
7881 if (p_tcp->tccps != 00) {
7882 opj_free(p_tcp->tccps);
7886 if (p_tcp->m_mct_coding_matrix != 00) {
7887 opj_free(p_tcp->m_mct_coding_matrix);
7888 p_tcp->m_mct_coding_matrix = 00;
7891 if (p_tcp->m_mct_decoding_matrix != 00) {
7892 opj_free(p_tcp->m_mct_decoding_matrix);
7893 p_tcp->m_mct_decoding_matrix = 00;
7896 if (p_tcp->m_mcc_records) {
7897 opj_free(p_tcp->m_mcc_records);
7898 p_tcp->m_mcc_records = 00;
7899 p_tcp->m_nb_max_mcc_records = 0;
7900 p_tcp->m_nb_mcc_records = 0;
7903 if (p_tcp->m_mct_records) {
7904 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7907 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7908 if (l_mct_data->m_data) {
7909 opj_free(l_mct_data->m_data);
7910 l_mct_data->m_data = 00;
7916 opj_free(p_tcp->m_mct_records);
7917 p_tcp->m_mct_records = 00;
7920 if (p_tcp->mct_norms != 00) {
7921 opj_free(p_tcp->mct_norms);
7922 p_tcp->mct_norms = 00;
7925 opj_j2k_tcp_data_destroy(p_tcp);
7930 * Destroys the data inside a tile coding parameter structure.
7932 * @param p_tcp the tile coding parameter which contain data to destroy.
7934 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7936 if (p_tcp->m_data) {
7937 opj_free(p_tcp->m_data);
7938 p_tcp->m_data = NULL;
7939 p_tcp->m_data_size = 0;
7944 * Destroys a coding parameter structure.
7946 * @param p_cp the coding parameter to destroy.
7948 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7950 OPJ_UINT32 l_nb_tiles;
7951 opj_tcp_v2_t * l_current_tile = 00;
7962 l_current_tile = p_cp->tcps;
7963 l_nb_tiles = p_cp->th * p_cp->tw;
7966 (i = 0; i < l_nb_tiles; ++i)
7968 opj_j2k_tcp_destroy(l_current_tile);
7971 opj_free(p_cp->tcps);
7975 (p_cp->ppm_buffer != 00)
7977 opj_free(p_cp->ppm_buffer);
7978 p_cp->ppm_buffer = 00;
7981 (p_cp->comment != 00)
7983 opj_free(p_cp->comment);
7987 (! p_cp->m_is_decoder)
7990 (p_cp->m_specific_param.m_enc.m_matrice)
7992 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7993 p_cp->m_specific_param.m_enc.m_matrice = 00;
8001 * Reads a tile header.
8002 * @param p_j2k the jpeg2000 codec.
8003 * @param p_stream the stream to write data to.
8004 * @param p_manager the user event manager.
8006 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8007 OPJ_UINT32 * p_tile_index,
8008 OPJ_UINT32 * p_data_size,
8009 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8010 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8011 OPJ_UINT32 * p_nb_comps,
8013 opj_stream_private_t *p_stream,
8014 opj_event_mgr_t * p_manager )
8016 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8017 OPJ_UINT32 l_marker_size;
8018 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8019 opj_tcp_v2_t * l_tcp = NULL;
8020 OPJ_UINT32 l_nb_tiles;
8023 assert(p_stream != 00);
8024 assert(p_j2k != 00);
8025 assert(p_manager != 00);
8027 /* Reach the End Of Codestream ?*/
8028 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8029 l_current_marker = J2K_MS_EOC;
8031 /* We need to encounter a SOT marker (a new tile-part header) */
8032 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8036 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8037 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8039 /* Try to read until the Start Of Data is detected */
8040 while (l_current_marker != J2K_MS_SOD) {
8042 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8043 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8044 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8048 /* Read 2 bytes from the buffer as the marker size */
8049 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8051 /* Why this condition? FIXME */
8052 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8053 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8055 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8057 /* Get the marker handler from the marker ID */
8058 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8060 /* Check if the marker is known and if it is the right place to find it */
8061 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8062 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8065 /* FIXME manage case of unknown marker as in the main header ? */
8067 /* Check if the marker size is compatible with the header data size */
8068 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8069 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8070 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8071 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8074 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8077 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8078 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) {
8079 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8083 /* Read the marker segment with the correct marker handler */
8084 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8085 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8089 /* Add the marker to the codestream index*/
8090 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8092 l_marker_handler->id,
8093 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8094 l_marker_size + 4 );
8096 /* Keep the position of the last SOT marker read */
8097 if ( l_marker_handler->id == J2K_MS_SOT ) {
8098 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8099 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8101 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8106 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8107 /* Skip the rest of the tile part header*/
8108 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) {
8109 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8112 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8115 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8116 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8117 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8120 /* Read 2 bytes from the buffer as the new marker ID */
8121 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8125 /* If we didn't skip data before, we need to read the SOD marker*/
8126 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8127 /* Try to read the SOD marker and skip data ? FIXME */
8128 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8134 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8135 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8136 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8137 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8141 /* Read 2 bytes from buffer as the new marker ID */
8142 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8146 /* Indicate we will try to read a new tile-part header*/
8147 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8148 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8149 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8151 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8152 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8153 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8157 /* Read 2 bytes from buffer as the new marker ID */
8158 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8162 /* Current marker is the EOC marker ?*/
8163 if (l_current_marker == J2K_MS_EOC) {
8164 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8165 p_j2k->m_current_tile_number = 0;
8166 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8171 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8172 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8173 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8175 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8176 ++p_j2k->m_current_tile_number;
8180 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8181 *p_go_on = OPJ_FALSE;
8187 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8188 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8192 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8193 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8195 *p_tile_index = p_j2k->m_current_tile_number;
8196 *p_go_on = OPJ_TRUE;
8197 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8198 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8199 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8200 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8201 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8202 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8204 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8210 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8211 OPJ_UINT32 p_tile_index,
8213 OPJ_UINT32 p_data_size,
8214 opj_stream_private_t *p_stream,
8215 opj_event_mgr_t * p_manager )
8217 OPJ_UINT32 l_current_marker;
8218 OPJ_BYTE l_data [2];
8219 opj_tcp_v2_t * l_tcp;
8222 assert(p_stream != 00);
8223 assert(p_j2k != 00);
8224 assert(p_manager != 00);
8226 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8227 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8231 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8232 if (! l_tcp->m_data) {
8233 opj_j2k_tcp_destroy(l_tcp);
8237 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8241 p_j2k->cstr_index) ) {
8242 opj_j2k_tcp_destroy(l_tcp);
8243 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8247 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8251 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8252 * we destroy just the data which will be re-read in read_tile_header*/
8253 /*opj_j2k_tcp_destroy(l_tcp);
8254 p_j2k->m_tcd->tcp = 0;*/
8255 opj_j2k_tcp_data_destroy(l_tcp);
8257 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8258 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8260 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8261 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8262 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8266 opj_read_bytes(l_data,&l_current_marker,2);
8268 if (l_current_marker == J2K_MS_EOC) {
8269 p_j2k->m_current_tile_number = 0;
8270 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8272 else if (l_current_marker != J2K_MS_SOT)
8274 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8283 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8285 OPJ_UINT32 i,j,k = 0;
8286 OPJ_UINT32 l_width_src,l_height_src;
8287 OPJ_UINT32 l_width_dest,l_height_dest;
8288 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8289 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8290 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8291 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8292 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8294 opj_image_comp_t * l_img_comp_src = 00;
8295 opj_image_comp_t * l_img_comp_dest = 00;
8297 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8298 opj_image_t * l_image_src = 00;
8299 OPJ_UINT32 l_size_comp, l_remaining;
8300 OPJ_INT32 * l_dest_ptr;
8301 opj_tcd_resolution_v2_t* l_res= 00;
8303 l_tilec = p_tcd->tcd_image->tiles->comps;
8304 l_image_src = p_tcd->image;
8305 l_img_comp_src = l_image_src->comps;
8307 l_img_comp_dest = p_output_image->comps;
8309 for (i=0; i<l_image_src->numcomps; i++) {
8311 /* Allocate output component buffer if necessary */
8312 if (!l_img_comp_dest->data) {
8314 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8315 if (! l_img_comp_dest->data) {
8320 /* Copy info from decoded comp image to output image */
8321 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8324 /* Compute the precision of the output buffer */
8325 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8326 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8327 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8333 if (l_size_comp == 3) {
8338 /* Current tile component size*/
8340 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8341 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8344 l_width_src = (l_res->x1 - l_res->x0);
8345 l_height_src = (l_res->y1 - l_res->y0);
8347 /* Border of the current output component*/
8348 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8349 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8350 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8351 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8354 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8355 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8359 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8360 * of the input buffer (decoded tile component) which will be move
8361 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8362 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8363 * by this input area.
8365 assert( l_res->x0 >= 0);
8366 assert( l_res->x1 >= 0);
8367 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8368 l_start_x_dest = l_res->x0 - l_x0_dest;
8369 l_offset_x0_src = 0;
8371 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8372 l_width_dest = l_width_src;
8373 l_offset_x1_src = 0;
8376 l_width_dest = l_x1_dest - l_res->x0 ;
8377 l_offset_x1_src = l_width_src - l_width_dest;
8381 l_start_x_dest = 0 ;
8382 l_offset_x0_src = l_x0_dest - l_res->x0;
8384 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8385 l_width_dest = l_width_src - l_offset_x0_src;
8386 l_offset_x1_src = 0;
8389 l_width_dest = l_img_comp_dest->w ;
8390 l_offset_x1_src = l_res->x1 - l_x1_dest;
8394 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8395 l_start_y_dest = l_res->y0 - l_y0_dest;
8396 l_offset_y0_src = 0;
8398 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8399 l_height_dest = l_height_src;
8400 l_offset_y1_src = 0;
8403 l_height_dest = l_y1_dest - l_res->y0 ;
8404 l_offset_y1_src = l_height_src - l_height_dest;
8408 l_start_y_dest = 0 ;
8409 l_offset_y0_src = l_y0_dest - l_res->y0;
8411 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8412 l_height_dest = l_height_src - l_offset_y0_src;
8413 l_offset_y1_src = 0;
8416 l_height_dest = l_img_comp_dest->h ;
8417 l_offset_y1_src = l_res->y1 - l_y1_dest;
8421 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8426 /* Compute the input buffer offset */
8427 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8428 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8429 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8431 /* Compute the output buffer offset */
8432 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8433 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8435 /* Move the output buffer to the first place where we will write*/
8436 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8439 fprintf(stdout, "COMPO[%d]:\n",i);
8440 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8441 "\t tile offset:%d, %d, %d, %d\n"
8442 "\t buffer offset: %d; %d, %d\n",
8443 l_res->x0, l_res->y0, l_width_src, l_height_src,
8444 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8445 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8447 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8448 "\t start offset: %d, line offset= %d\n",
8449 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8453 switch (l_size_comp) {
8456 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8457 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8459 if (l_img_comp_src->sgnd) {
8460 for (j = 0 ; j < l_height_dest ; ++j) {
8461 for ( k = 0 ; k < l_width_dest ; ++k) {
8462 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8465 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8466 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8470 for ( j = 0 ; j < l_height_dest ; ++j ) {
8471 for ( k = 0 ; k < l_width_dest ; ++k) {
8472 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8475 l_dest_ptr+= l_line_offset_dest;
8476 l_src_ptr += l_line_offset_src;
8480 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8481 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8486 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8487 l_src_ptr += l_start_offset_src;
8489 if (l_img_comp_src->sgnd) {
8490 for (j=0;j<l_height_dest;++j) {
8491 for (k=0;k<l_width_dest;++k) {
8492 *(l_dest_ptr++) = *(l_src_ptr++);
8495 l_dest_ptr+= l_line_offset_dest;
8496 l_src_ptr += l_line_offset_src ;
8500 for (j=0;j<l_height_dest;++j) {
8501 for (k=0;k<l_width_dest;++k) {
8502 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8505 l_dest_ptr+= l_line_offset_dest;
8506 l_src_ptr += l_line_offset_src ;
8510 l_src_ptr += l_end_offset_src;
8511 p_data = (OPJ_BYTE*) l_src_ptr;
8516 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8517 l_src_ptr += l_start_offset_src;
8519 for (j=0;j<l_height_dest;++j) {
8520 for (k=0;k<l_width_dest;++k) {
8521 *(l_dest_ptr++) = (*(l_src_ptr++));
8524 l_dest_ptr+= l_line_offset_dest;
8525 l_src_ptr += l_line_offset_src ;
8528 l_src_ptr += l_end_offset_src;
8529 p_data = (OPJ_BYTE*) l_src_ptr;
8543 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8545 * @param p_j2k the jpeg2000 codec.
8546 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8547 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8548 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8549 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8550 * @param p_manager the user event manager
8552 * @return true if the area could be set.
8554 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8555 opj_image_t* p_image,
8556 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8557 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8558 opj_event_mgr_t * p_manager )
8560 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8561 opj_image_t * l_image = p_j2k->m_private_image;
8564 OPJ_INT32 l_comp_x1, l_comp_y1;
8565 opj_image_comp_t* l_img_comp = NULL;
8567 /* Check if we are read the main header */
8568 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8569 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8573 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8574 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8576 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8577 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8578 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8579 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8585 /* Check if the positions provided by the user are correct */
8588 assert(p_start_x >= 0 );
8589 assert(p_start_y >= 0 );
8591 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8592 opj_event_msg_v2(p_manager, EVT_ERROR,
8593 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8594 p_start_x, l_image->x1);
8597 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8598 opj_event_msg_v2(p_manager, EVT_WARNING,
8599 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8600 p_start_x, l_image->x0);
8601 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8602 p_image->x0 = l_image->x0;
8605 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8606 p_image->x0 = p_start_x;
8610 if ((OPJ_UINT32)p_start_y > l_image->y1){
8611 opj_event_msg_v2(p_manager, EVT_ERROR,
8612 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8613 p_start_y, l_image->y1);
8616 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8617 opj_event_msg_v2(p_manager, EVT_WARNING,
8618 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8619 p_start_y, l_image->y0);
8620 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8621 p_image->y0 = l_image->y0;
8624 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8625 p_image->y0 = p_start_y;
8629 assert((OPJ_UINT32)p_end_x > 0);
8630 assert((OPJ_UINT32)p_end_y > 0);
8631 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8632 opj_event_msg_v2(p_manager, EVT_ERROR,
8633 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8634 p_end_x, l_image->x0);
8637 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8638 opj_event_msg_v2(p_manager, EVT_WARNING,
8639 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8640 p_end_x, l_image->x1);
8641 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8642 p_image->x1 = l_image->x1;
8645 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8646 p_image->x1 = p_end_x;
8650 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8651 opj_event_msg_v2(p_manager, EVT_ERROR,
8652 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8653 p_end_y, l_image->y0);
8656 if ((OPJ_UINT32)p_end_y > l_image->y1){
8657 opj_event_msg_v2(p_manager, EVT_WARNING,
8658 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8659 p_end_y, l_image->y1);
8660 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8661 p_image->y1 = l_image->y1;
8664 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8665 p_image->y1 = p_end_y;
8669 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8671 l_img_comp = p_image->comps;
8672 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8676 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8677 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8678 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8679 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8681 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8682 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8684 opj_event_msg_v2(p_manager, EVT_ERROR,
8685 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8689 l_img_comp->w = l_w;
8691 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8692 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8694 opj_event_msg_v2(p_manager, EVT_ERROR,
8695 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8699 l_img_comp->h = l_h;
8704 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8705 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8712 /* ----------------------------------------------------------------------- */
8713 /* J2K / JPT decoder interface */
8714 /* ----------------------------------------------------------------------- */
8716 * Creates a J2K decompression structure.
8718 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8720 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8722 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8726 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8728 l_j2k->m_is_decoder = 1;
8729 l_j2k->m_cp.m_is_decoder = 1;
8731 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8732 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8733 opj_j2k_destroy(l_j2k);
8736 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8738 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8739 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8740 opj_j2k_destroy(l_j2k);
8744 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8746 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8748 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8750 /* codestream index creation */
8751 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8753 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8754 if (!l_j2k->cstr_index){
8755 opj_j2k_destroy(l_j2k);
8759 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8762 /* validation list creation */
8763 l_j2k->m_validation_list = opj_procedure_list_create();
8764 if (! l_j2k->m_validation_list) {
8765 opj_j2k_destroy(l_j2k);
8769 /* execution list creation */
8770 l_j2k->m_procedure_list = opj_procedure_list_create();
8771 if (! l_j2k->m_procedure_list) {
8772 opj_j2k_destroy(l_j2k);
8780 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8782 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8783 opj_calloc(1,sizeof(opj_codestream_index_t));
8787 cstr_index->maxmarknum = 100;
8788 cstr_index->marknum = 0;
8789 cstr_index->marker = (opj_marker_info_t*)
8790 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8791 if (!cstr_index-> marker)
8794 cstr_index->tile_index = NULL;
8801 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8803 * @param p_tile_no the tile index.
8804 * @param p_comp_no the component being outputted.
8805 * @param p_j2k the J2K codec.
8807 * @return the number of bytes taken by the SPCod element.
8809 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8810 OPJ_UINT32 p_tile_no,
8811 OPJ_UINT32 p_comp_no )
8813 opj_cp_v2_t *l_cp = 00;
8814 opj_tcp_v2_t *l_tcp = 00;
8815 opj_tccp_t *l_tccp = 00;
8818 assert(p_j2k != 00);
8820 l_cp = &(p_j2k->m_cp);
8821 l_tcp = &l_cp->tcps[p_tile_no];
8822 l_tccp = &l_tcp->tccps[p_comp_no];
8824 /* preconditions again */
8825 assert(p_tile_no < (l_cp->tw * l_cp->th));
8826 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8828 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8829 return 5 + l_tccp->numresolutions;
8837 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8839 * @param p_comp_no the component number to output.
8840 * @param p_stream the stream to write data to.
8841 * @param p_j2k J2K codec.
8842 * @param p_manager the user event manager.
8845 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8846 OPJ_UINT32 p_tile_no,
8847 OPJ_UINT32 p_comp_no,
8849 OPJ_UINT32 * p_header_size,
8850 struct opj_event_mgr * p_manager )
8853 opj_cp_v2_t *l_cp = 00;
8854 opj_tcp_v2_t *l_tcp = 00;
8855 opj_tccp_t *l_tccp = 00;
8858 assert(p_j2k != 00);
8859 assert(p_header_size != 00);
8860 assert(p_manager != 00);
8861 assert(p_data != 00);
8863 l_cp = &(p_j2k->m_cp);
8864 l_tcp = &l_cp->tcps[p_tile_no];
8865 l_tccp = &l_tcp->tccps[p_comp_no];
8867 /* preconditions again */
8868 assert(p_tile_no < (l_cp->tw * l_cp->th));
8869 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8871 if (*p_header_size < 5) {
8872 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8876 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8879 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8882 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8885 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8888 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8891 *p_header_size = *p_header_size - 5;
8893 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8895 if (*p_header_size < l_tccp->numresolutions) {
8896 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8900 for (i = 0; i < l_tccp->numresolutions; ++i) {
8901 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8905 *p_header_size = *p_header_size - l_tccp->numresolutions;
8912 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8913 * @param p_header_data the data contained in the COM box.
8914 * @param p_j2k the jpeg2000 codec.
8915 * @param p_header_size the size of the data contained in the COM marker.
8916 * @param p_manager the user event manager.
8918 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8920 OPJ_BYTE * p_header_data,
8921 OPJ_UINT32 * p_header_size,
8922 opj_event_mgr_t * p_manager)
8924 OPJ_UINT32 i, l_tmp;
8925 opj_cp_v2_t *l_cp = NULL;
8926 opj_tcp_v2_t *l_tcp = NULL;
8927 opj_tccp_t *l_tccp = NULL;
8928 OPJ_BYTE * l_current_ptr = NULL;
8931 assert(p_j2k != 00);
8932 assert(p_manager != 00);
8933 assert(p_header_data != 00);
8935 l_cp = &(p_j2k->m_cp);
8936 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8937 &l_cp->tcps[p_j2k->m_current_tile_number] :
8938 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8940 /* precondition again */
8941 assert(compno < p_j2k->m_private_image->numcomps);
8943 l_tccp = &l_tcp->tccps[compno];
8944 l_current_ptr = p_header_data;
8946 /* make sure room is sufficient */
8947 if (*p_header_size < 5) {
8948 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8952 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8953 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8956 /* If user wants to remove more resolutions than the codestream contains, return error */
8957 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8958 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8959 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8960 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8964 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8968 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8972 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8975 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8978 *p_header_size = *p_header_size - 5;
8980 /* use custom precinct size ? */
8981 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8982 if (*p_header_size < l_tccp->numresolutions) {
8983 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8987 for (i = 0; i < l_tccp->numresolutions; ++i) {
8988 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8990 l_tccp->prcw[i] = l_tmp & 0xf;
8991 l_tccp->prch[i] = l_tmp >> 4;
8994 *p_header_size = *p_header_size - l_tccp->numresolutions;
8997 /* set default size for the precinct width and height */
8998 for (i = 0; i < l_tccp->numresolutions; ++i) {
8999 l_tccp->prcw[i] = 15;
9000 l_tccp->prch[i] = 15;
9004 #ifdef WIP_REMOVE_MSD
9006 if (p_j2k->cstr_info && compno == 0) {
9007 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9009 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9010 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9011 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9012 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9013 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9016 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9017 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9026 * Copies the tile component parameters of all the component from the first tile component.
9028 * @param p_j2k the J2k codec.
9030 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9034 opj_cp_v2_t *l_cp = NULL;
9035 opj_tcp_v2_t *l_tcp = NULL;
9036 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9037 OPJ_UINT32 l_prc_size;
9040 assert(p_j2k != 00);
9042 l_cp = &(p_j2k->m_cp);
9043 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9044 &l_cp->tcps[p_j2k->m_current_tile_number] :
9045 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9047 l_ref_tccp = &l_tcp->tccps[0];
9048 l_copied_tccp = l_ref_tccp + 1;
9049 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9051 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9052 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9053 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9054 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9055 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9056 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9057 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9058 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9064 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9066 * @param p_tile_no the tile index.
9067 * @param p_comp_no the component being outputted.
9068 * @param p_j2k the J2K codec.
9070 * @return the number of bytes taken by the SPCod element.
9072 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9073 OPJ_UINT32 p_tile_no,
9074 OPJ_UINT32 p_comp_no )
9076 OPJ_UINT32 l_num_bands;
9078 opj_cp_v2_t *l_cp = 00;
9079 opj_tcp_v2_t *l_tcp = 00;
9080 opj_tccp_t *l_tccp = 00;
9083 assert(p_j2k != 00);
9085 l_cp = &(p_j2k->m_cp);
9086 l_tcp = &l_cp->tcps[p_tile_no];
9087 l_tccp = &l_tcp->tccps[p_comp_no];
9089 /* preconditions again */
9090 assert(p_tile_no < l_cp->tw * l_cp->th);
9091 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9093 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9095 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9096 return 1 + l_num_bands;
9099 return 1 + 2*l_num_bands;
9104 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9106 * @param p_tile_no the tile to output.
9107 * @param p_comp_no the component number to output.
9108 * @param p_data the data buffer.
9109 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9110 * @param p_j2k J2K codec.
9111 * @param p_manager the user event manager.
9114 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9115 OPJ_UINT32 p_tile_no,
9116 OPJ_UINT32 p_comp_no,
9118 OPJ_UINT32 * p_header_size,
9119 struct opj_event_mgr * p_manager )
9121 OPJ_UINT32 l_header_size;
9122 OPJ_UINT32 l_band_no, l_num_bands;
9123 OPJ_UINT32 l_expn,l_mant;
9125 opj_cp_v2_t *l_cp = 00;
9126 opj_tcp_v2_t *l_tcp = 00;
9127 opj_tccp_t *l_tccp = 00;
9130 assert(p_j2k != 00);
9131 assert(p_header_size != 00);
9132 assert(p_manager != 00);
9133 assert(p_data != 00);
9135 l_cp = &(p_j2k->m_cp);
9136 l_tcp = &l_cp->tcps[p_tile_no];
9137 l_tccp = &l_tcp->tccps[p_comp_no];
9139 /* preconditions again */
9140 assert(p_tile_no < l_cp->tw * l_cp->th);
9141 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9143 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9145 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9146 l_header_size = 1 + l_num_bands;
9148 if (*p_header_size < l_header_size) {
9149 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9153 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9156 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9157 l_expn = l_tccp->stepsizes[l_band_no].expn;
9158 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9163 l_header_size = 1 + 2*l_num_bands;
9165 if (*p_header_size < l_header_size) {
9166 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9170 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9173 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9174 l_expn = l_tccp->stepsizes[l_band_no].expn;
9175 l_mant = l_tccp->stepsizes[l_band_no].mant;
9177 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9182 *p_header_size = *p_header_size - l_header_size;
9188 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9190 * @param p_comp_no the component being targeted.
9191 * @param p_header_data the data contained in the COM box.
9192 * @param p_j2k the jpeg2000 codec.
9193 * @param p_header_size the size of the data contained in the COM marker.
9194 * @param p_manager the user event manager.
9196 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9197 OPJ_UINT32 p_comp_no,
9198 OPJ_BYTE* p_header_data,
9199 OPJ_UINT32 * p_header_size,
9200 opj_event_mgr_t * p_manager
9204 OPJ_UINT32 l_band_no;
9205 opj_cp_v2_t *l_cp = 00;
9206 opj_tcp_v2_t *l_tcp = 00;
9207 opj_tccp_t *l_tccp = 00;
9208 OPJ_BYTE * l_current_ptr = 00;
9209 OPJ_UINT32 l_tmp, l_num_band;
9212 assert(p_j2k != 00);
9213 assert(p_manager != 00);
9214 assert(p_header_data != 00);
9216 l_cp = &(p_j2k->m_cp);
9217 /* come from tile part header or main header ?*/
9218 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9219 &l_cp->tcps[p_j2k->m_current_tile_number] :
9220 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9222 /* precondition again*/
9223 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9225 l_tccp = &l_tcp->tccps[p_comp_no];
9226 l_current_ptr = p_header_data;
9228 if (*p_header_size < 1) {
9229 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9232 *p_header_size -= 1;
9234 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9237 l_tccp->qntsty = l_tmp & 0x1f;
9238 l_tccp->numgbits = l_tmp >> 5;
9239 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9243 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9245 (*p_header_size) / 2;
9247 if( l_num_band > J2K_MAXBANDS ) {
9248 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9249 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9250 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9251 /*return OPJ_FALSE;*/
9256 if (l_cp->correct) {
9258 /* if JPWL is on, we check whether there are too many subbands */
9259 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9260 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9261 "JPWL: bad number of subbands in Sqcx (%d)\n",
9264 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9267 /* we try to correct */
9269 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9270 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9275 #endif /* USE_JPWL */
9277 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9278 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9279 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9281 if (l_band_no < J2K_MAXBANDS){
9282 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9283 l_tccp->stepsizes[l_band_no].mant = 0;
9286 *p_header_size = *p_header_size - l_num_band;
9289 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9290 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9292 if (l_band_no < J2K_MAXBANDS){
9293 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9294 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9297 *p_header_size = *p_header_size - 2*l_num_band;
9300 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9301 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9302 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9303 l_tccp->stepsizes[l_band_no].expn =
9304 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9305 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9306 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9314 * Copies the tile component parameters of all the component from the first tile component.
9316 * @param p_j2k the J2k codec.
9318 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9321 opj_cp_v2_t *l_cp = NULL;
9322 opj_tcp_v2_t *l_tcp = NULL;
9323 opj_tccp_t *l_ref_tccp = NULL;
9324 opj_tccp_t *l_copied_tccp = NULL;
9328 assert(p_j2k != 00);
9330 l_cp = &(p_j2k->m_cp);
9331 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9332 &l_cp->tcps[p_j2k->m_current_tile_number] :
9333 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9335 l_ref_tccp = &l_tcp->tccps[0];
9336 l_copied_tccp = l_ref_tccp + 1;
9337 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9339 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9340 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9341 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9342 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9348 * Dump some elements from the J2K decompression structure .
9350 *@param p_j2k the jpeg2000 codec.
9351 *@param flag flag to describe what elments are dump.
9352 *@param out_stream output stream where dump the elements.
9355 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9357 /* Check if the flag is compatible with j2k file*/
9358 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9359 fprintf(out_stream, "Wrong flag\n");
9363 /* Dump the image_header */
9364 if (flag & OPJ_IMG_INFO){
9365 if (p_j2k->m_private_image)
9366 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9369 /* Dump the codestream info from main header */
9370 if (flag & OPJ_J2K_MH_INFO){
9371 opj_j2k_dump_MH_info(p_j2k, out_stream);
9375 /* Dump the codestream info of the current tile */
9376 if (flag & OPJ_J2K_TH_INFO){
9380 /* Dump the codestream index from main header */
9381 if (flag & OPJ_J2K_MH_IND){
9382 opj_j2k_dump_MH_index(p_j2k, out_stream);
9385 /* Dump the codestream index of the current tile */
9386 if (flag & OPJ_J2K_TH_IND){
9393 * Dump index elements of the codestream extract from the main header.
9395 *@param p_j2k the jpeg2000 codec.
9396 *@param out_stream output stream where dump the elements.
9399 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9401 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9402 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9404 fprintf(out_stream, "Codestream index from main header: {\n");
9406 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9407 "\t Main header end position=%" PRIi64 "\n",
9408 cstr_index->main_head_start, cstr_index->main_head_end);
9410 fprintf(out_stream, "\t Marker list: {\n");
9412 if (cstr_index->marker){
9413 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9414 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9415 cstr_index->marker[it_marker].type,
9416 cstr_index->marker[it_marker].pos,
9417 cstr_index->marker[it_marker].len );
9421 fprintf(out_stream, "\t }\n");
9424 if (cstr_index->tile_index){
9426 /* Simple test to avoid to write empty information*/
9427 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9428 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9429 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9432 if (l_acc_nb_of_tile_part)
9434 fprintf(out_stream, "\t Tile index: {\n");
9436 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9437 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9439 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9441 if (cstr_index->tile_index[it_tile].tp_index){
9442 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9443 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9445 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9446 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9447 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9451 if (cstr_index->tile_index[it_tile].marker){
9452 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9453 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9454 cstr_index->tile_index[it_tile].marker[it_marker].type,
9455 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9456 cstr_index->tile_index[it_tile].marker[it_marker].len );
9460 fprintf(out_stream,"\t }\n");
9464 fprintf(out_stream,"}\n");
9469 * Dump info elements of the codestream extract from the main header.
9471 *@param p_j2k the jpeg2000 codec.
9472 *@param out_stream output stream where dump the elements.
9475 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9477 opj_tcp_v2_t * l_default_tile=NULL;
9479 fprintf(out_stream, "Codestream info from main header: {\n");
9481 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9482 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9483 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9485 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9489 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9491 fprintf(out_stream, "\t default tile {\n");
9492 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9493 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9494 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9495 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9497 for (compno = 0; compno < numcomps; compno++) {
9498 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9500 OPJ_INT32 bandno, numbands;
9503 fprintf(out_stream, "\t\t comp %d {\n", compno);
9504 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9505 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9506 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9507 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9508 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9509 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9511 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9512 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9513 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9515 fprintf(out_stream, "\n");
9517 /* quantization style*/
9518 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9519 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9520 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9521 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9522 for (bandno = 0; bandno < numbands; bandno++) {
9523 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9524 l_tccp->stepsizes[bandno].expn);
9526 fprintf(out_stream, "\n");
9529 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9531 fprintf(out_stream, "\t\t }\n");
9532 } /*end of component of default tile*/
9533 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9537 fprintf(out_stream, "}\n");
9542 * Dump an image header structure.
9544 *@param img_header the image header to dump.
9545 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9546 *@param out_stream output stream where dump the elements.
9548 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9553 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9557 fprintf(out_stream, "Image info {\n");
9558 tab[0] = '\t';tab[1] = '\0';
9561 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9562 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9563 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9565 if (img_header->comps){
9567 for (compno = 0; compno < img_header->numcomps; compno++) {
9568 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9569 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9570 fprintf(out_stream,"%s}\n",tab);
9574 fprintf(out_stream, "}\n");
9578 * Dump a component image header structure.
9580 *@param comp_header the component image header to dump.
9581 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9582 *@param out_stream output stream where dump the elements.
9584 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9589 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9592 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9595 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9596 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9597 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9600 fprintf(out_stream, "}\n");
9605 * Get the codestream info from a JPEG2000 codec.
9607 *@param p_j2k the component image header to dump.
9609 *@return the codestream information extract from the jpg2000 codec
9611 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9614 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9615 opj_tcp_v2_t *l_default_tile;
9616 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9618 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9620 cstr_info->tx0 = p_j2k->m_cp.tx0;
9621 cstr_info->ty0 = p_j2k->m_cp.ty0;
9622 cstr_info->tdx = p_j2k->m_cp.tdx;
9623 cstr_info->tdy = p_j2k->m_cp.tdy;
9624 cstr_info->tw = p_j2k->m_cp.tw;
9625 cstr_info->th = p_j2k->m_cp.th;
9627 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9629 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9631 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9632 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9633 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9634 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9636 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9638 for (compno = 0; compno < numcomps; compno++) {
9639 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9640 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9641 OPJ_INT32 bandno, numbands;
9644 l_tccp_info->csty = l_tccp->csty;
9645 l_tccp_info->numresolutions = l_tccp->numresolutions;
9646 l_tccp_info->cblkw = l_tccp->cblkw;
9647 l_tccp_info->cblkh = l_tccp->cblkh;
9648 l_tccp_info->cblksty = l_tccp->cblksty;
9649 l_tccp_info->qmfbid = l_tccp->qmfbid;
9650 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9652 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9653 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9656 /* quantization style*/
9657 l_tccp_info->qntsty = l_tccp->qntsty;
9658 l_tccp_info->numgbits = l_tccp->numgbits;
9660 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9661 if (numbands < J2K_MAXBANDS) {
9662 for (bandno = 0; bandno < numbands; bandno++) {
9663 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9664 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9669 l_tccp_info->roishift = l_tccp->roishift;
9677 * Get the codestream index from a JPEG2000 codec.
9679 *@param p_j2k the component image header to dump.
9681 *@return the codestream index extract from the jpg2000 codec
9683 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9685 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9686 opj_calloc(1,sizeof(opj_codestream_index_t));
9690 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9691 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9692 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9694 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9695 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9696 if (!l_cstr_index->marker){
9697 opj_free( l_cstr_index);
9701 if (p_j2k->cstr_index->marker)
9702 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9704 opj_free(l_cstr_index->marker);
9705 l_cstr_index->marker = NULL;
9708 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9709 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9710 if (!l_cstr_index->tile_index){
9711 opj_free( l_cstr_index->marker);
9712 opj_free( l_cstr_index);
9716 if (!p_j2k->cstr_index->tile_index){
9717 opj_free(l_cstr_index->tile_index);
9718 l_cstr_index->tile_index = NULL;
9721 OPJ_UINT32 it_tile = 0;
9722 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9725 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9727 l_cstr_index->tile_index[it_tile].marker =
9728 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9730 if (!l_cstr_index->tile_index[it_tile].marker) {
9731 OPJ_UINT32 it_tile_free;
9733 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9734 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9737 opj_free( l_cstr_index->tile_index);
9738 opj_free( l_cstr_index->marker);
9739 opj_free( l_cstr_index);
9743 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9744 memcpy( l_cstr_index->tile_index[it_tile].marker,
9745 p_j2k->cstr_index->tile_index[it_tile].marker,
9746 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9748 opj_free(l_cstr_index->tile_index[it_tile].marker);
9749 l_cstr_index->tile_index[it_tile].marker = NULL;
9752 /* Tile part index*/
9753 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9755 l_cstr_index->tile_index[it_tile].tp_index =
9756 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9758 if(!l_cstr_index->tile_index[it_tile].tp_index){
9759 OPJ_UINT32 it_tile_free;
9761 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9762 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9763 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9766 opj_free( l_cstr_index->tile_index);
9767 opj_free( l_cstr_index->marker);
9768 opj_free( l_cstr_index);
9772 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9773 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9774 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9775 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9778 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9779 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9782 /* Packet index (NOT USED)*/
9783 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9784 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9789 return l_cstr_index;
9792 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9794 OPJ_UINT32 it_tile=0;
9796 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9797 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9798 if (!p_j2k->cstr_index->tile_index)
9801 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9802 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9803 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9804 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9805 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9806 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9816 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9817 opj_stream_private_t *p_stream,
9818 opj_event_mgr_t * p_manager)
9820 opj_bool l_go_on = OPJ_TRUE;
9821 OPJ_UINT32 l_current_tile_no;
9822 OPJ_UINT32 l_data_size,l_max_data_size;
9823 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9824 OPJ_UINT32 l_nb_comps;
9825 OPJ_BYTE * l_current_data;
9827 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9828 if (! l_current_data) {
9831 l_max_data_size = 1000;
9836 if (! opj_j2k_read_tile_header( p_j2k,
9839 &l_tile_x0, &l_tile_y0,
9840 &l_tile_x1, &l_tile_y1,
9845 opj_free(l_current_data);
9853 if (l_data_size > l_max_data_size) {
9854 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9855 if (! l_current_data) {
9856 opj_free(l_current_data);
9860 l_max_data_size = l_data_size;
9863 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9864 opj_free(l_current_data);
9867 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);
9869 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9870 opj_free(l_current_data);
9873 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9877 opj_free(l_current_data);
9883 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9885 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9888 assert(p_j2k != 00);
9890 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9891 /* DEVELOPER CORNER, add your custom procedures */
9896 * Read and decode one tile.
9898 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9899 opj_stream_private_t *p_stream,
9900 opj_event_mgr_t * p_manager)
9902 opj_bool l_go_on = OPJ_TRUE;
9903 OPJ_UINT32 l_current_tile_no;
9904 OPJ_UINT32 l_tile_no_to_dec;
9905 OPJ_UINT32 l_data_size,l_max_data_size;
9906 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9907 OPJ_UINT32 l_nb_comps;
9908 OPJ_BYTE * l_current_data;
9910 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9911 if (! l_current_data) {
9914 l_max_data_size = 1000;
9916 /*Allocate and initialize some elements of codestrem index if not already done*/
9917 if( !p_j2k->cstr_index->tile_index)
9919 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9920 opj_free(l_current_data);
9924 /* Move into the codestream to the first SOT used to decode the desired tile */
9925 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9926 if (p_j2k->cstr_index->tile_index)
9927 if(p_j2k->cstr_index->tile_index->tp_index)
9929 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9930 /* the index for this tile has not been built,
9931 * so move to the last SOT read */
9932 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9933 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9938 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)) ) {
9939 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9943 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9944 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9945 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9949 if (! opj_j2k_read_tile_header( p_j2k,
9952 &l_tile_x0, &l_tile_y0,
9953 &l_tile_x1, &l_tile_y1,
9958 opj_free(l_current_data);
9967 if (l_data_size > l_max_data_size) {
9968 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9969 if (! l_current_data) {
9970 opj_free(l_current_data);
9974 l_max_data_size = l_data_size;
9979 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9980 opj_free(l_current_data);
9983 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);
9985 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9986 opj_free(l_current_data);
9989 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9991 if(l_current_tile_no == l_tile_no_to_dec)
9993 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9994 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9995 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10001 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);
10006 opj_free(l_current_data);
10013 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10015 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10018 assert(p_j2k != 00);
10020 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
10021 /* DEVELOPER CORNER, add your custom procedures */
10027 * Decodes the tiles of the stream.
10029 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10030 opj_stream_private_t * p_stream,
10031 opj_image_t * p_image,
10032 opj_event_mgr_t * p_manager)
10039 p_j2k->m_output_image = opj_image_create0();
10040 if (! (p_j2k->m_output_image)) {
10043 opj_copy_image_header(p_image, p_j2k->m_output_image);
10045 /* customization of the decoding */
10046 opj_j2k_setup_decoding(p_j2k);
10048 /* Decode the codestream */
10049 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10050 opj_image_destroy(p_j2k->m_private_image);
10051 p_j2k->m_private_image = NULL;
10055 /* Move data and copy one information from codec to output image*/
10056 for (compno = 0; compno < p_image->numcomps; compno++) {
10057 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10058 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10059 p_j2k->m_output_image->comps[compno].data = NULL;
10067 * Get the decoded tile.
10069 * @param p_j2k the jpeg2000 codestream codec.
10070 * @param p_stream input_stream
10071 * @param p_image output image. .
10072 * @param p_manager the user event manager
10073 * @param tile_index index of the tile we want decode
10075 * @return true if succeed.
10077 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
10078 opj_stream_private_t *p_stream,
10079 opj_image_t* p_image,
10080 opj_event_mgr_t * p_manager,
10081 OPJ_UINT32 tile_index )
10084 OPJ_UINT32 l_tile_x, l_tile_y;
10085 opj_image_comp_t* l_img_comp;
10088 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10092 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10093 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);
10097 /* Compute the dimension of the desired tile*/
10098 l_tile_x = tile_index % p_j2k->m_cp.tw;
10099 l_tile_y = tile_index / p_j2k->m_cp.tw;
10101 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10102 if (p_image->x0 < p_j2k->m_private_image->x0)
10103 p_image->x0 = p_j2k->m_private_image->x0;
10104 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10105 if (p_image->x1 > p_j2k->m_private_image->x1)
10106 p_image->x1 = p_j2k->m_private_image->x1;
10108 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10109 if (p_image->y0 < p_j2k->m_private_image->y0)
10110 p_image->y0 = p_j2k->m_private_image->y0;
10111 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10112 if (p_image->y1 > p_j2k->m_private_image->y1)
10113 p_image->y1 = p_j2k->m_private_image->y1;
10115 l_img_comp = p_image->comps;
10116 for (compno=0; compno < p_image->numcomps; ++compno)
10118 OPJ_INT32 l_comp_x1, l_comp_y1;
10120 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10122 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10123 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10124 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10125 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10127 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10128 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10133 /* Destroy the previous output image*/
10134 if (p_j2k->m_output_image)
10135 opj_image_destroy(p_j2k->m_output_image);
10137 /* Create the ouput image from the information previously computed*/
10138 p_j2k->m_output_image = opj_image_create0();
10139 if (! (p_j2k->m_output_image)) {
10142 opj_copy_image_header(p_image, p_j2k->m_output_image);
10144 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10146 /* customization of the decoding */
10147 opj_j2k_setup_decoding_tile(p_j2k);
10149 /* Decode the codestream */
10150 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10151 opj_image_destroy(p_j2k->m_private_image);
10152 p_j2k->m_private_image = NULL;
10156 /* Move data and copy one information from codec to output image*/
10157 for (compno = 0; compno < p_image->numcomps; compno++) {
10158 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10160 if (p_image->comps[compno].data)
10161 opj_free(p_image->comps[compno].data);
10163 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10165 p_j2k->m_output_image->comps[compno].data = NULL;
10171 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
10172 OPJ_UINT32 res_factor,
10173 opj_event_mgr_t * p_manager)
10175 OPJ_UINT32 it_comp;
10177 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10179 if (p_j2k->m_private_image) {
10180 if (p_j2k->m_private_image->comps) {
10181 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10182 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10183 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10184 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10185 if ( res_factor >= max_res){
10186 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10189 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10202 * Encodes all the tiles in a row.
10204 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10205 opj_stream_private_t *p_stream,
10206 opj_event_mgr_t * p_manager )
10209 OPJ_UINT32 l_nb_tiles;
10210 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10211 OPJ_BYTE * l_current_data;
10213 /* preconditions */
10214 assert(p_j2k != 00);
10215 assert(p_stream != 00);
10216 assert(p_manager != 00);
10218 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10219 if (! l_current_data) {
10222 l_max_tile_size = 1000;
10224 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10225 for (i=0;i<l_nb_tiles;++i) {
10226 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10227 opj_free(l_current_data);
10231 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10232 if (l_current_tile_size > l_max_tile_size) {
10233 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10234 if (! l_current_data) {
10237 l_max_tile_size = l_current_tile_size;
10240 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10242 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10247 opj_free(l_current_data);
10252 * Ends the compression procedures and possibility add data to be read after the
10255 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10256 opj_stream_private_t *p_stream,
10257 opj_event_mgr_t * p_manager)
10259 /* customization of the encoding */
10260 opj_j2k_setup_end_compress(p_j2k);
10262 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10272 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10274 * @param p_j2k the jpeg2000 codec.
10275 * @param p_stream the stream object.
10276 * @param p_manager the user event manager.
10278 * @return true if the codec is valid.
10280 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10281 opj_stream_private_t *p_stream,
10282 opj_image_t * p_image,
10283 opj_event_mgr_t * p_manager)
10285 /* preconditions */
10286 assert(p_j2k != 00);
10287 assert(p_stream != 00);
10288 assert(p_manager != 00);
10290 p_j2k->m_private_image = opj_image_create0();
10291 opj_copy_image_header(p_image, p_j2k->m_private_image);
10293 // TODO_MSD: Find a better way
10294 if (p_image->comps) {
10295 OPJ_UINT32 it_comp;
10296 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10297 if (p_image->comps[it_comp].data) {
10298 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10299 p_image->comps[it_comp].data = NULL;
10305 /* customization of the validation */
10306 opj_j2k_setup_encoding_validation (p_j2k);
10308 /* validation of the parameters codec */
10309 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10313 /* customization of the encoding */
10314 opj_j2k_setup_header_writting(p_j2k);
10317 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10327 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10328 OPJ_UINT32 p_tile_index,
10329 opj_stream_private_t *p_stream,
10330 opj_event_mgr_t * p_manager )
10333 if (p_tile_index != p_j2k->m_current_tile_number) {
10334 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10338 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);
10340 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10341 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10342 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10344 /* initialisation before tile encoding */
10345 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10355 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10357 OPJ_UINT32 i,j,k = 0;
10358 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10359 opj_image_comp_t * l_img_comp = 00;
10360 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10361 opj_image_t * l_image = 00;
10362 OPJ_UINT32 l_size_comp, l_remaining;
10363 OPJ_INT32 * l_src_ptr;
10364 l_tilec = p_tcd->tcd_image->tiles->comps;
10365 l_image = p_tcd->image;
10366 l_img_comp = l_image->comps;
10368 for (i=0;i<p_tcd->image->numcomps;++i) {
10369 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10370 l_remaining = l_img_comp->prec & 7; /* (%8) */
10375 if (l_size_comp == 3) {
10379 l_width = (l_tilec->x1 - l_tilec->x0);
10380 l_height = (l_tilec->y1 - l_tilec->y0);
10381 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10382 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10383 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10384 l_stride = l_image_width - l_width;
10385 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10387 switch (l_size_comp) {
10390 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10391 if (l_img_comp->sgnd) {
10392 for (j=0;j<l_height;++j) {
10393 for (k=0;k<l_width;++k) {
10394 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10398 l_src_ptr += l_stride;
10402 for (j=0;j<l_height;++j) {
10403 for (k=0;k<l_width;++k) {
10404 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10408 l_src_ptr += l_stride;
10412 p_data = (OPJ_BYTE*) l_dest_ptr;
10417 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10418 if (l_img_comp->sgnd) {
10419 for (j=0;j<l_height;++j) {
10420 for (k=0;k<l_width;++k) {
10421 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10423 l_src_ptr += l_stride;
10427 for (j=0;j<l_height;++j) {
10428 for (k=0;k<l_width;++k) {
10429 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10431 l_src_ptr += l_stride;
10435 p_data = (OPJ_BYTE*) l_dest_ptr;
10440 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10441 for (j=0;j<l_height;++j) {
10442 for (k=0;k<l_width;++k) {
10443 *(l_dest_ptr++) = *(l_src_ptr++);
10445 l_src_ptr += l_stride;
10448 p_data = (OPJ_BYTE*) l_dest_ptr;
10461 * @param p_j2k the jpeg2000 codec.
10462 * @param p_stream the stream to write data to.
10463 * @param p_manager the user event manager.
10465 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10467 OPJ_UINT32 p_data_size,
10468 opj_stream_private_t *p_stream,
10469 opj_event_mgr_t * p_manager )
10471 opj_tcd_v2_t * l_tcd = 00;
10472 opj_cp_v2_t * l_cp = 00;
10473 opj_tcp_v2_t * l_tcp = 00;
10474 OPJ_UINT32 l_nb_bytes_written;
10475 OPJ_BYTE * l_current_data = 00;
10476 OPJ_UINT32 l_tile_size = 0;
10477 OPJ_UINT32 l_available_data;
10479 /* preconditions */
10480 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10482 l_tcd = p_j2k->m_tcd;
10483 l_cp = &(p_j2k->m_cp);
10484 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10486 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10487 l_available_data = l_tile_size;
10488 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10490 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10491 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10495 l_nb_bytes_written = 0;
10496 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10499 l_current_data += l_nb_bytes_written;
10500 l_available_data -= l_nb_bytes_written;
10502 l_nb_bytes_written = 0;
10503 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10507 l_available_data -= l_nb_bytes_written;
10508 l_nb_bytes_written = l_tile_size - l_available_data;
10510 if ( opj_stream_write_data( p_stream,
10511 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10512 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10516 ++p_j2k->m_current_tile_number;
10523 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10524 * are valid. Developers wanting to extend the library can add their own validation procedures.
10526 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10528 /* preconditions */
10529 assert(p_j2k != 00);
10531 /* DEVELOPER CORNER, insert your custom procedures */
10532 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10534 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10535 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
10538 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
10539 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
10540 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
10544 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10545 * are valid. Developers wanting to extend the library can add their own validation procedures.
10547 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
10549 /* preconditions */
10550 assert(p_j2k != 00);
10552 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10553 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10555 /* DEVELOPER CORNER, add your custom validation procedure */
10556 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10561 * Sets up the procedures to do on writing header.
10562 * Developers wanting to extend the library can add their own writing procedures.
10564 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10566 /* preconditions */
10567 assert(p_j2k != 00);
10569 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
10570 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10571 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10572 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10573 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10576 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10577 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
10578 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
10580 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10581 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
10585 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
10587 if (p_j2k->m_cp.comment != 00) {
10588 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10591 /* DEVELOPER CORNER, insert your custom procedures */
10592 if (p_j2k->m_cp.rsiz & MCT) {
10593 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
10595 /* End of Developer Corner */
10597 if (p_j2k->cstr_index) {
10598 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10601 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10602 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10606 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10608 OPJ_UINT32 * p_data_written,
10609 OPJ_UINT32 p_total_data_size,
10610 opj_stream_private_t *p_stream,
10611 struct opj_event_mgr * p_manager )
10614 OPJ_UINT32 l_nb_bytes_written = 0;
10615 OPJ_UINT32 l_current_nb_bytes_written;
10616 OPJ_BYTE * l_begin_data = 00;
10618 opj_tcp_v2_t *l_tcp = 00;
10619 opj_tcd_v2_t * l_tcd = 00;
10620 opj_cp_v2_t * l_cp = 00;
10622 l_tcd = p_j2k->m_tcd;
10623 l_cp = &(p_j2k->m_cp);
10624 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10626 l_tcd->cur_pino = 0;
10628 /*Get number of tile parts*/
10629 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10634 l_current_nb_bytes_written = 0;
10635 l_begin_data = p_data;
10636 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10641 l_nb_bytes_written += l_current_nb_bytes_written;
10642 p_data += l_current_nb_bytes_written;
10643 p_total_data_size -= l_current_nb_bytes_written;
10645 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10646 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10647 l_current_nb_bytes_written = 0;
10648 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10649 l_nb_bytes_written += l_current_nb_bytes_written;
10650 p_data += l_current_nb_bytes_written;
10651 p_total_data_size -= l_current_nb_bytes_written;
10653 l_current_nb_bytes_written = 0;
10654 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10655 l_nb_bytes_written += l_current_nb_bytes_written;
10656 p_data += l_current_nb_bytes_written;
10657 p_total_data_size -= l_current_nb_bytes_written;
10660 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10661 l_current_nb_bytes_written = 0;
10662 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10663 l_nb_bytes_written += l_current_nb_bytes_written;
10664 p_data += l_current_nb_bytes_written;
10665 p_total_data_size -= l_current_nb_bytes_written;
10669 l_current_nb_bytes_written = 0;
10670 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10674 l_nb_bytes_written += l_current_nb_bytes_written;
10675 * p_data_written = l_nb_bytes_written;
10677 /* Writing Psot in SOT marker */
10678 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10680 if (l_cp->m_specific_param.m_enc.m_cinema){
10681 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10687 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10689 OPJ_UINT32 * p_data_written,
10690 OPJ_UINT32 p_total_data_size,
10691 opj_stream_private_t *p_stream,
10692 struct opj_event_mgr * p_manager
10695 OPJ_UINT32 tilepartno=0;
10696 OPJ_UINT32 l_nb_bytes_written = 0;
10697 OPJ_UINT32 l_current_nb_bytes_written;
10698 OPJ_UINT32 l_part_tile_size;
10699 OPJ_UINT32 tot_num_tp;
10702 OPJ_BYTE * l_begin_data;
10703 opj_tcp_v2_t *l_tcp = 00;
10704 opj_tcd_v2_t * l_tcd = 00;
10705 opj_cp_v2_t * l_cp = 00;
10708 l_tcd = p_j2k->m_tcd;
10709 l_cp = &(p_j2k->m_cp);
10710 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10712 /*Get number of tile parts*/
10713 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
10715 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10716 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10717 l_current_nb_bytes_written = 0;
10718 l_part_tile_size = 0;
10719 l_begin_data = p_data;
10721 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10725 l_nb_bytes_written += l_current_nb_bytes_written;
10726 p_data += l_current_nb_bytes_written;
10727 p_total_data_size -= l_current_nb_bytes_written;
10728 l_part_tile_size += l_nb_bytes_written;
10730 l_current_nb_bytes_written = 0;
10731 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10735 p_data += l_current_nb_bytes_written;
10736 l_nb_bytes_written += l_current_nb_bytes_written;
10737 p_total_data_size -= l_current_nb_bytes_written;
10738 l_part_tile_size += l_nb_bytes_written;
10740 /* Writing Psot in SOT marker */
10741 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10743 if (l_cp->m_specific_param.m_enc.m_cinema) {
10744 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10747 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10750 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10751 l_tcd->cur_pino = pino;
10753 /*Get number of tile parts*/
10754 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
10755 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10756 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10757 l_current_nb_bytes_written = 0;
10758 l_part_tile_size = 0;
10759 l_begin_data = p_data;
10761 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10765 l_nb_bytes_written += l_current_nb_bytes_written;
10766 p_data += l_current_nb_bytes_written;
10767 p_total_data_size -= l_current_nb_bytes_written;
10768 l_part_tile_size += l_current_nb_bytes_written;
10770 l_current_nb_bytes_written = 0;
10772 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10776 l_nb_bytes_written += l_current_nb_bytes_written;
10777 p_data += l_current_nb_bytes_written;
10778 p_total_data_size -= l_current_nb_bytes_written;
10779 l_part_tile_size += l_current_nb_bytes_written;
10781 /* Writing Psot in SOT marker */
10782 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10784 if (l_cp->m_specific_param.m_enc.m_cinema) {
10785 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10788 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10792 *p_data_written = l_nb_bytes_written;
10798 * Writes the updated tlm.
10800 * @param p_stream the stream to write data to.
10801 * @param p_j2k J2K codec.
10802 * @param p_manager the user event manager.
10804 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10805 struct opj_stream_private *p_stream,
10806 struct opj_event_mgr * p_manager )
10808 OPJ_UINT32 l_tlm_size;
10809 OPJ_SIZE_T l_tlm_position, l_current_position;
10811 /* preconditions */
10812 assert(p_j2k != 00);
10813 assert(p_manager != 00);
10814 assert(p_stream != 00);
10816 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10817 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10818 l_current_position = opj_stream_tell(p_stream);
10820 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10824 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) {
10828 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10837 * Ends the encoding, i.e. frees memory.
10839 * @param p_stream the stream to write data to.
10840 * @param p_j2k J2K codec.
10841 * @param p_manager the user event manager.
10843 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10844 struct opj_stream_private *p_stream,
10845 struct opj_event_mgr * p_manager )
10847 /* preconditions */
10848 assert(p_j2k != 00);
10849 assert(p_manager != 00);
10850 assert(p_stream != 00);
10852 tcd_destroy_v2(p_j2k->m_tcd);
10855 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10856 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10857 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10858 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10861 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10862 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10863 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10866 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10872 * Destroys the memory associated with the decoding of headers.
10874 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10875 opj_stream_private_t *p_stream,
10876 opj_event_mgr_t * p_manager
10879 /* preconditions */
10880 assert(p_j2k != 00);
10881 assert(p_stream != 00);
10882 assert(p_manager != 00);
10884 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10885 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10886 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10889 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10898 * @param p_stream the stream to write data to.
10899 * @param p_j2k J2K codec.
10900 * @param p_manager the user event manager.
10902 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
10903 struct opj_stream_private *p_stream,
10904 struct opj_event_mgr * p_manager )
10906 opj_codestream_info_t * l_cstr_info = 00;
10908 /* preconditions */
10909 assert(p_j2k != 00);
10910 assert(p_manager != 00);
10911 assert(p_stream != 00);
10914 /* TODO mergeV2: check this part which use cstr_info */
10915 /*l_cstr_info = p_j2k->cstr_info;
10919 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));
10921 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10922 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10924 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10926 l_cstr_info->tw = p_j2k->m_cp.tw;
10927 l_cstr_info->th = p_j2k->m_cp.th;
10929 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10930 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10931 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10932 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10934 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10936 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10938 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10940 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10941 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10944 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10946 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10948 /*l_cstr_info->maxmarknum = 100;
10949 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10950 l_cstr_info->marknum = 0;
10953 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);
10957 * Creates a tile-coder decoder.
10959 * @param p_stream the stream to write data to.
10960 * @param p_j2k J2K codec.
10961 * @param p_manager the user event manager.
10963 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10964 opj_stream_private_t *p_stream,
10965 opj_event_mgr_t * p_manager
10968 /* preconditions */
10969 assert(p_j2k != 00);
10970 assert(p_manager != 00);
10971 assert(p_stream != 00);
10973 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10975 if (! p_j2k->m_tcd) {
10976 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10980 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10981 tcd_destroy_v2(p_j2k->m_tcd);
10992 * @param p_j2k the jpeg2000 codec.
10993 * @param p_stream the stream to write data to.
10994 * @param p_manager the user event manager.
10996 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10997 OPJ_UINT32 p_tile_index,
10999 OPJ_UINT32 p_data_size,
11000 opj_stream_private_t *p_stream,
11001 opj_event_mgr_t * p_manager )
11003 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11004 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11008 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11009 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);