2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 static void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool opj_j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool opj_j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool opj_j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 opj_stream_private_t *p_stream,
135 opj_event_mgr_t * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool opj_j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void opj_j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool opj_j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool opj_j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
372 static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 * Writes the SOC marker (Start Of Codestream)
383 * @param p_stream the stream to write data to.
384 * @param p_j2k J2K codec.
385 * @param p_manager the user event manager.
387 static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
388 opj_stream_private_t *p_stream,
389 opj_event_mgr_t * p_manager );
392 * Reads a SOC marker (Start of Codestream)
393 * @param p_header_data the data contained in the SOC box.
394 * @param jp2 the jpeg2000 file codec.
395 * @param p_header_size the size of the data contained in the SOC marker.
396 * @param p_manager the user event manager.
398 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
399 opj_stream_private_t *p_stream,
400 opj_event_mgr_t * p_manager );
403 * Writes the SIZ marker (image and tile size)
405 * @param p_stream the stream to write data to.
406 * @param p_j2k J2K codec.
407 * @param p_manager the user event manager.
409 static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
410 opj_stream_private_t *p_stream,
411 opj_event_mgr_t * p_manager );
414 * Reads a SIZ marker (image and tile size)
415 * @param p_header_data the data contained in the SIZ box.
416 * @param jp2 the jpeg2000 file codec.
417 * @param p_header_size the size of the data contained in the SIZ marker.
418 * @param p_manager the user event manager.
420 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
421 OPJ_BYTE * p_header_data,
422 OPJ_UINT32 p_header_size,
423 opj_event_mgr_t * p_manager);
426 * Writes the COM marker (comment)
428 * @param p_stream the stream to write data to.
429 * @param p_j2k J2K codec.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
433 opj_stream_private_t *p_stream,
434 opj_event_mgr_t * p_manager );
437 * Reads a COM marker (comments)
438 * @param p_header_data the data contained in the COM box.
439 * @param jp2 the jpeg2000 file codec.
440 * @param p_header_size the size of the data contained in the COM marker.
441 * @param p_manager the user event manager.
443 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
444 OPJ_BYTE * p_header_data,
445 OPJ_UINT32 p_header_size,
446 opj_event_mgr_t * p_manager );
448 * Writes the COD marker (Coding style default)
450 * @param p_stream the stream to write data to.
451 * @param p_j2k J2K codec.
452 * @param p_manager the user event manager.
454 static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
455 opj_stream_private_t *p_stream,
456 opj_event_mgr_t * p_manager );
459 * Reads a COD marker (Coding Styke defaults)
460 * @param p_header_data the data contained in the COD box.
461 * @param p_j2k the jpeg2000 codec.
462 * @param p_header_size the size of the data contained in the COD marker.
463 * @param p_manager the user event manager.
465 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
466 OPJ_BYTE * p_header_data,
467 OPJ_UINT32 p_header_size,
468 opj_event_mgr_t * p_manager);
471 * Writes the COC marker (Coding style component)
473 * @param p_comp_number the index of the component to output.
474 * @param p_stream the stream to write data to.
475 * @param p_j2k J2K codec.
476 * @param p_manager the user event manager.
478 static opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
479 OPJ_UINT32 p_comp_no,
480 opj_stream_private_t *p_stream,
481 opj_event_mgr_t * p_manager );
483 * Writes the COC marker (Coding style component)
485 * @param p_comp_no the index of the component to output.
486 * @param p_stream the stream to write data to.
487 * @param p_j2k J2K codec.
488 * @param p_manager the user event manager.
490 static void opj_j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
491 OPJ_UINT32 p_comp_no,
493 OPJ_UINT32 * p_data_written,
494 opj_event_mgr_t * p_manager );
497 * Gets the maximum size taken by a coc.
499 * @param p_j2k the jpeg2000 codec to use.
501 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
505 * Reads a COC marker (Coding Style Component)
506 * @param p_header_data the data contained in the COC box.
507 * @param p_j2k the jpeg2000 codec.
508 * @param p_header_size the size of the data contained in the COC marker.
509 * @param p_manager the user event manager.
511 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
512 OPJ_BYTE * p_header_data,
513 OPJ_UINT32 p_header_size,
514 opj_event_mgr_t * p_manager );
517 * Writes the QCD marker (quantization default)
519 * @param p_comp_number the index of the component to output.
520 * @param p_stream the stream to write data to.
521 * @param p_j2k J2K codec.
522 * @param p_manager the user event manager.
524 static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
525 opj_stream_private_t *p_stream,
526 opj_event_mgr_t * p_manager );
529 * Reads a QCD marker (Quantization defaults)
530 * @param p_header_data the data contained in the QCD box.
531 * @param p_j2k the jpeg2000 codec.
532 * @param p_header_size the size of the data contained in the QCD marker.
533 * @param p_manager the user event manager.
535 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
536 OPJ_BYTE * p_header_data,
537 OPJ_UINT32 p_header_size,
538 opj_event_mgr_t * p_manager );
540 * Writes the QCC marker (quantization component)
542 * @param p_comp_no the index of the component to output.
543 * @param p_stream the stream to write data to.
544 * @param p_j2k J2K codec.
545 * @param p_manager the user event manager.
547 static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
548 OPJ_UINT32 p_comp_no,
549 opj_stream_private_t *p_stream,
550 opj_event_mgr_t * p_manager );
553 * Writes the QCC marker (quantization component)
555 * @param p_comp_no the index of the component to output.
556 * @param p_stream the stream to write data to.
557 * @param p_j2k J2K codec.
558 * @param p_manager the user event manager.
560 static void opj_j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
561 OPJ_UINT32 p_comp_no,
563 OPJ_UINT32 * p_data_written,
564 opj_event_mgr_t * p_manager );
567 * Gets the maximum size taken by a qcc.
569 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
572 * Reads a QCC marker (Quantization component)
573 * @param p_header_data the data contained in the QCC box.
574 * @param p_j2k the jpeg2000 codec.
575 * @param p_header_size the size of the data contained in the QCC marker.
576 * @param p_manager the user event manager.
578 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 opj_event_mgr_t * p_manager);
583 * Writes the POC marker (Progression Order Change)
585 * @param p_stream the stream to write data to.
586 * @param p_j2k J2K codec.
587 * @param p_manager the user event manager.
589 static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
590 opj_stream_private_t *p_stream,
591 opj_event_mgr_t * p_manager );
593 * Writes the POC marker (Progression Order Change)
595 * @param p_stream the stream to write data to.
596 * @param p_j2k J2K codec.
597 * @param p_manager the user event manager.
599 static void opj_j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
601 OPJ_UINT32 * p_data_written,
602 opj_event_mgr_t * p_manager );
604 * Gets the maximum size taken by the writting of a POC.
606 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
609 * Reads a POC marker (Progression Order Change)
611 * @param p_header_data the data contained in the POC box.
612 * @param p_j2k the jpeg2000 codec.
613 * @param p_header_size the size of the data contained in the POC marker.
614 * @param p_manager the user event manager.
616 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
617 OPJ_BYTE * p_header_data,
618 OPJ_UINT32 p_header_size,
619 opj_event_mgr_t * p_manager );
622 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
624 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
627 * Gets the maximum size taken by the headers of the SOT.
629 * @param p_j2k the jpeg2000 codec to use.
631 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
635 * Reads a CRG marker (Component registration)
637 * @param p_header_data the data contained in the TLM box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the TLM marker.
640 * @param p_manager the user event manager.
642 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
643 OPJ_BYTE * p_header_data,
644 OPJ_UINT32 p_header_size,
645 opj_event_mgr_t * p_manager );
647 * Reads a TLM marker (Tile Length Marker)
649 * @param p_header_data the data contained in the TLM box.
650 * @param p_j2k the jpeg2000 codec.
651 * @param p_header_size the size of the data contained in the TLM marker.
652 * @param p_manager the user event manager.
654 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
655 OPJ_BYTE * p_header_data,
656 OPJ_UINT32 p_header_size,
657 opj_event_mgr_t * p_manager);
660 * Writes the updated tlm.
662 * @param p_stream the stream to write data to.
663 * @param p_j2k J2K codec.
664 * @param p_manager the user event manager.
666 static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
667 struct opj_stream_private *p_stream,
668 struct opj_event_mgr * p_manager );
671 * Reads a PLM marker (Packet length, main header marker)
673 * @param p_header_data the data contained in the TLM box.
674 * @param p_j2k the jpeg2000 codec.
675 * @param p_header_size the size of the data contained in the TLM marker.
676 * @param p_manager the user event manager.
678 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
679 OPJ_BYTE * p_header_data,
680 OPJ_UINT32 p_header_size,
681 opj_event_mgr_t * p_manager);
683 * Reads a PLT marker (Packet length, tile-part header)
685 * @param p_header_data the data contained in the PLT box.
686 * @param p_j2k the jpeg2000 codec.
687 * @param p_header_size the size of the data contained in the PLT marker.
688 * @param p_manager the user event manager.
690 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
691 OPJ_BYTE * p_header_data,
692 OPJ_UINT32 p_header_size,
693 opj_event_mgr_t * p_manager );
697 * Reads a PPM marker (Packed packet headers, main header)
699 * @param p_header_data the data contained in the POC box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the POC marker.
702 * @param p_manager the user event manager.
705 static opj_bool j2k_read_ppm_v2 (
707 OPJ_BYTE * p_header_data,
708 OPJ_UINT32 p_header_size,
709 struct opj_event_mgr * p_manager
713 static opj_bool j2k_read_ppm_v3 (
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 struct opj_event_mgr * p_manager
722 * Reads a PPT marker (Packed packet headers, tile-part header)
724 * @param p_header_data the data contained in the PPT box.
725 * @param p_j2k the jpeg2000 codec.
726 * @param p_header_size the size of the data contained in the PPT marker.
727 * @param p_manager the user event manager.
729 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
730 OPJ_BYTE * p_header_data,
731 OPJ_UINT32 p_header_size,
732 opj_event_mgr_t * p_manager );
734 * Writes the TLM marker (Tile Length Marker)
736 * @param p_stream the stream to write data to.
737 * @param p_j2k J2K codec.
738 * @param p_manager the user event manager.
740 static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
741 opj_stream_private_t *p_stream,
742 opj_event_mgr_t * p_manager );
745 Write the SOT marker (start of tile-part)
746 @param j2k J2K handle
748 static void j2k_write_sot(opj_j2k_t *j2k);
751 * Writes the SOT marker (Start of tile-part)
753 * @param p_stream the stream to write data to.
754 * @param p_j2k J2K codec.
755 * @param p_manager the user event manager.
757 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
759 OPJ_UINT32 * p_data_written,
760 const struct opj_stream_private *p_stream,
761 struct opj_event_mgr * p_manager );
764 * Reads a PPT marker (Packed packet headers, tile-part header)
766 * @param p_header_data the data contained in the PPT box.
767 * @param p_j2k the jpeg2000 codec.
768 * @param p_header_size the size of the data contained in the PPT marker.
769 * @param p_manager the user event manager.
771 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
772 OPJ_BYTE * p_header_data,
773 OPJ_UINT32 p_header_size,
774 opj_event_mgr_t * p_manager );
777 Write the SOD marker (start of data)
778 @param j2k J2K handle
779 @param tile_coder Pointer to a TCD handle
781 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
784 * Writes the SOD marker (Start of data)
786 * @param p_stream the stream to write data to.
787 * @param p_j2k J2K codec.
788 * @param p_manager the user event manager.
790 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
791 struct opj_tcd_v2 * p_tile_coder,
793 OPJ_UINT32 * p_data_written,
794 OPJ_UINT32 p_total_data_size,
795 const struct opj_stream_private *p_stream,
796 struct opj_event_mgr * p_manager );
799 * Reads a SOD marker (Start Of Data)
801 * @param p_header_data the data contained in the SOD box.
802 * @param p_j2k the jpeg2000 codec.
803 * @param p_header_size the size of the data contained in the SOD marker.
804 * @param p_manager the user event manager.
806 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
807 opj_stream_private_t *p_stream,
808 opj_event_mgr_t * p_manager );
811 * Updates the Tile Length Marker.
813 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
815 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
816 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
818 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
819 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
823 Write the RGN marker (region-of-interest)
824 @param j2k J2K handle
825 @param compno Number of the component concerned by the information written
826 @param tileno Number of the tile concerned by the information written
828 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
831 * Writes the RGN marker (Region Of Interest)
833 * @param p_tile_no the tile to output
834 * @param p_comp_no the component to output
835 * @param p_stream the stream to write data to.
836 * @param p_j2k J2K codec.
837 * @param p_manager the user event manager.
839 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
840 OPJ_UINT32 p_tile_no,
841 OPJ_UINT32 p_comp_no,
842 struct opj_stream_private *p_stream,
843 struct opj_event_mgr * p_manager );
846 * Reads a RGN marker (Region Of Interest)
848 * @param p_header_data the data contained in the POC box.
849 * @param p_j2k the jpeg2000 codec.
850 * @param p_header_size the size of the data contained in the POC marker.
851 * @param p_manager the user event manager.
853 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
854 OPJ_BYTE * p_header_data,
855 OPJ_UINT32 p_header_size,
856 opj_event_mgr_t * p_manager );
859 * Writes the EOC marker (End of Codestream)
861 * @param p_stream the stream to write data to.
862 * @param p_j2k J2K codec.
863 * @param p_manager the user event manager.
865 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
866 struct opj_stream_private *p_stream,
867 struct opj_event_mgr * p_manager );
870 Write the EOC marker (end of codestream)
871 @param j2k J2K handle
873 static void j2k_write_eoc(opj_j2k_t *j2k);
875 Read the EOC marker (end of codestream)
876 @param j2k J2K handle
878 static void j2k_read_eoc(opj_j2k_t *j2k);
881 * Reads a EOC marker (End Of Codestream)
883 * @param p_header_data the data contained in the SOD box.
884 * @param p_j2k the jpeg2000 codec.
885 * @param p_header_size the size of the data contained in the SOD marker.
886 * @param p_manager the user event manager.
889 static opj_bool j2k_read_eoc_v2 (
891 struct opj_stream_private *p_stream,
892 struct opj_event_mgr * p_manager
899 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
901 * @param p_stream the stream to write data to.
902 * @param p_j2k J2K codec.
903 * @param p_manager the user event manager.
905 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
906 struct opj_stream_private *p_stream,
907 struct opj_event_mgr * p_manager );
912 * @param p_stream the stream to write data to.
913 * @param p_j2k J2K codec.
914 * @param p_manager the user event manager.
916 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
917 struct opj_stream_private *p_stream,
918 struct opj_event_mgr * p_manager );
921 Add main header marker information
922 @param cstr_info Codestream information structure
923 @param type marker type
924 @param pos byte offset of marker segment
925 @param len length of marker segment
927 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
929 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
931 Add tile header marker information
932 @param tileno tile index number
933 @param cstr_info Codestream information structure
934 @param type marker type
935 @param pos byte offset of marker segment
936 @param len length of marker segment
938 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
940 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);
943 * Reads an unknown marker
945 * @param p_stream the stream object to read from.
946 * @param p_j2k the jpeg2000 codec.
947 * @param p_manager the user event manager.
949 * @return true if the marker could be deduced.
951 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
952 opj_stream_private_t *p_stream,
953 OPJ_UINT32 *output_marker,
954 opj_event_mgr_t * p_manager );
957 * Writes the MCT marker (Multiple Component Transform)
959 * @param p_stream the stream to write data to.
960 * @param p_j2k J2K codec.
961 * @param p_manager the user event manager.
963 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
964 opj_mct_data_t * p_mct_record,
965 struct opj_stream_private *p_stream,
966 struct opj_event_mgr * p_manager );
969 * Reads a MCT marker (Multiple Component Transform)
971 * @param p_header_data the data contained in the MCT box.
972 * @param p_j2k the jpeg2000 codec.
973 * @param p_header_size the size of the data contained in the MCT marker.
974 * @param p_manager the user event manager.
976 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
977 OPJ_BYTE * p_header_data,
978 OPJ_UINT32 p_header_size,
979 opj_event_mgr_t * p_manager );
982 * Writes the MCC marker (Multiple Component Collection)
984 * @param p_stream the stream to write data to.
985 * @param p_j2k J2K codec.
986 * @param p_manager the user event manager.
988 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
989 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
990 struct opj_stream_private *p_stream,
991 struct opj_event_mgr * p_manager );
995 * Reads a MCC marker (Multiple Component Collection)
997 * @param p_header_data the data contained in the MCC box.
998 * @param p_j2k the jpeg2000 codec.
999 * @param p_header_size the size of the data contained in the MCC marker.
1000 * @param p_manager the user event manager.
1002 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1003 OPJ_BYTE * p_header_data,
1004 OPJ_UINT32 p_header_size,
1005 opj_event_mgr_t * p_manager );
1008 * Writes the MCO marker (Multiple component transformation ordering)
1010 * @param p_stream the stream to write data to.
1011 * @param p_j2k J2K codec.
1012 * @param p_manager the user event manager.
1014 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1015 struct opj_stream_private *p_stream,
1016 struct opj_event_mgr * p_manager );
1019 * Reads a MCO marker (Multiple Component Transform Ordering)
1021 * @param p_header_data the data contained in the MCO box.
1022 * @param p_j2k the jpeg2000 codec.
1023 * @param p_header_size the size of the data contained in the MCO marker.
1024 * @param p_manager the user event manager.
1026 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1027 OPJ_BYTE * p_header_data,
1028 OPJ_UINT32 p_header_size,
1029 opj_event_mgr_t * p_manager );
1031 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1033 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1034 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1035 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1036 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1038 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1039 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1040 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1041 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1043 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1044 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1045 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1046 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1050 * Ends the encoding, i.e. frees memory.
1052 * @param p_stream the stream to write data to.
1053 * @param p_j2k J2K codec.
1054 * @param p_manager the user event manager.
1056 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1057 struct opj_stream_private *p_stream,
1058 struct opj_event_mgr * p_manager );
1061 * Writes the CBD marker (Component bit depth definition)
1063 * @param p_stream the stream to write data to.
1064 * @param p_j2k J2K codec.
1065 * @param p_manager the user event manager.
1067 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1068 struct opj_stream_private *p_stream,
1069 struct opj_event_mgr * p_manager );
1072 * Reads a CBD marker (Component bit depth definition)
1073 * @param p_header_data the data contained in the CBD box.
1074 * @param p_j2k the jpeg2000 codec.
1075 * @param p_header_size the size of the data contained in the CBD marker.
1076 * @param p_manager the user event manager.
1078 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1079 OPJ_BYTE * p_header_data,
1080 OPJ_UINT32 p_header_size,
1081 opj_event_mgr_t * p_manager);
1084 * Writes the image components.
1086 * @param p_stream the stream to write data to.
1087 * @param p_j2k J2K codec.
1088 * @param p_manager the user event manager.
1090 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1091 struct opj_stream_private *p_stream,
1092 struct opj_event_mgr * p_manager );
1095 * Writes regions of interests.
1097 * @param p_stream the stream to write data to.
1098 * @param p_j2k J2K codec.
1099 * @param p_manager the user event manager.
1101 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1102 struct opj_stream_private *p_stream,
1103 struct opj_event_mgr * p_manager );
1108 * @param p_stream the stream to write data to.
1109 * @param p_j2k J2K codec.
1110 * @param p_manager the user event manager.
1112 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1113 struct opj_stream_private *p_stream,
1114 struct opj_event_mgr * p_manager );
1117 * Checks the progression order changes values. Tells of the poc given as input are valid.
1118 * A nice message is outputted at errors.
1120 * @param p_pocs the progression order changes.
1121 * @param p_nb_pocs the number of progression order changes.
1122 * @param p_nb_resolutions the number of resolutions.
1123 * @param numcomps the number of components
1124 * @param numlayers the number of layers.
1126 * @return true if the pocs are valid.
1128 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1129 OPJ_UINT32 p_nb_pocs,
1130 OPJ_UINT32 p_nb_resolutions,
1131 OPJ_UINT32 numcomps,
1132 OPJ_UINT32 numlayers,
1133 opj_event_mgr_t * p_manager);
1136 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1138 * @param cp the coding parameters.
1139 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1140 * @param tileno the given tile.
1142 * @return the number of tile parts.
1144 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1146 /** mem allocation for TLM marker*/
1147 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1150 * Calculates the total number of tile parts needed by the encoder to
1151 * encode such an image. If not enough memory is available, then the function return false.
1153 * @param p_nb_tiles pointer that will hold the number of tile parts.
1154 * @param cp the coding parameters for the image.
1155 * @param image the image to encode.
1156 * @param p_j2k the p_j2k encoder.
1157 * @param p_manager the user event manager.
1159 * @return true if the function was successful, false else.
1161 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1163 OPJ_UINT32 * p_nb_tiles,
1165 opj_event_mgr_t * p_manager);
1167 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1169 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1171 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1177 /* ----------------------------------------------------------------------- */
1178 typedef struct j2k_prog_order{
1179 OPJ_PROG_ORDER enum_prog;
1183 j2k_prog_order_t j2k_prog_order_list[] = {
1189 {(OPJ_PROG_ORDER)-1, ""}
1197 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1205 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1207 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1209 opj_j2k_read_int16_to_float,
1210 opj_j2k_read_int32_to_float,
1211 opj_j2k_read_float32_to_float,
1212 opj_j2k_read_float64_to_float
1215 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1217 opj_j2k_read_int16_to_int32,
1218 opj_j2k_read_int32_to_int32,
1219 opj_j2k_read_float32_to_int32,
1220 opj_j2k_read_float64_to_int32
1223 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1225 opj_j2k_write_float_to_int16,
1226 opj_j2k_write_float_to_int32,
1227 opj_j2k_write_float_to_float,
1228 opj_j2k_write_float_to_float64
1231 typedef struct opj_dec_memory_marker_handler
1235 /** value of the state when the marker can appear */
1237 /** action linked to the marker */
1238 opj_bool (*handler) (
1239 opj_j2k_v2_t *p_j2k,
1240 OPJ_BYTE * p_header_data,
1241 OPJ_UINT32 p_header_size,
1242 struct opj_event_mgr * p_manager
1245 opj_dec_memory_marker_handler_t;
1247 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1250 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1251 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1252 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1253 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1254 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1255 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1256 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1257 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1258 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1259 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1260 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1261 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1262 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1264 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1265 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1266 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1267 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1268 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1269 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1271 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1272 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1273 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1274 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1275 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1276 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1277 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1278 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1279 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1280 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1281 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1282 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1283 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1285 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1286 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1287 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1288 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1289 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1290 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1292 #ifdef TODO_MS /* FIXME */
1293 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1294 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1295 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1296 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1298 #endif /* USE_JPWL */
1300 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1301 {J2K_MS_INSEC, 0, j2k_read_insec}
1302 #endif /* USE_JPSEC */
1303 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1308 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1310 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1311 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1315 for (i=0;i<p_nb_elem;++i) {
1316 opj_read_bytes(l_src_data,&l_temp,2);
1318 l_src_data+=sizeof(OPJ_INT16);
1320 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1324 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1326 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1327 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1331 for (i=0;i<p_nb_elem;++i) {
1332 opj_read_bytes(l_src_data,&l_temp,4);
1334 l_src_data+=sizeof(OPJ_INT32);
1336 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1340 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1342 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1343 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1347 for (i=0;i<p_nb_elem;++i) {
1348 opj_read_float(l_src_data,&l_temp);
1350 l_src_data+=sizeof(OPJ_FLOAT32);
1352 *(l_dest_data++) = l_temp;
1356 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1358 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1359 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1363 for (i=0;i<p_nb_elem;++i) {
1364 opj_read_double(l_src_data,&l_temp);
1366 l_src_data+=sizeof(OPJ_FLOAT64);
1368 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1372 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1374 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1375 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1379 for (i=0;i<p_nb_elem;++i) {
1380 opj_read_bytes(l_src_data,&l_temp,2);
1382 l_src_data+=sizeof(OPJ_INT16);
1384 *(l_dest_data++) = (OPJ_INT32) l_temp;
1388 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1390 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1391 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1395 for (i=0;i<p_nb_elem;++i) {
1396 opj_read_bytes(l_src_data,&l_temp,4);
1398 l_src_data+=sizeof(OPJ_INT32);
1400 *(l_dest_data++) = (OPJ_INT32) l_temp;
1404 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1406 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1407 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1411 for (i=0;i<p_nb_elem;++i) {
1412 opj_read_float(l_src_data,&l_temp);
1414 l_src_data+=sizeof(OPJ_FLOAT32);
1416 *(l_dest_data++) = (OPJ_INT32) l_temp;
1420 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1422 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1423 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1427 for (i=0;i<p_nb_elem;++i) {
1428 opj_read_double(l_src_data,&l_temp);
1430 l_src_data+=sizeof(OPJ_FLOAT64);
1432 *(l_dest_data++) = (OPJ_INT32) l_temp;
1436 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1438 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1439 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1443 for (i=0;i<p_nb_elem;++i) {
1444 l_temp = (OPJ_UINT32) *(l_src_data++);
1446 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1448 l_dest_data+=sizeof(OPJ_INT16);
1452 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1454 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1455 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1459 for (i=0;i<p_nb_elem;++i) {
1460 l_temp = (OPJ_UINT32) *(l_src_data++);
1462 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1464 l_dest_data+=sizeof(OPJ_INT32);
1468 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1470 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1471 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1475 for (i=0;i<p_nb_elem;++i) {
1476 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1478 opj_write_float(l_dest_data,l_temp);
1480 l_dest_data+=sizeof(OPJ_FLOAT32);
1484 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1486 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1487 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1491 for (i=0;i<p_nb_elem;++i) {
1492 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1494 opj_write_double(l_dest_data,l_temp);
1496 l_dest_data+=sizeof(OPJ_FLOAT64);
1502 * Converts an enum type progression order to string type.
1504 * @param prg_order the progression order to get.
1506 * @return the string representation of the given progression order.
1508 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1509 j2k_prog_order_t *po;
1510 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1511 if(po->enum_prog == prg_order){
1512 return po->str_prog;
1515 return po->str_prog;
1519 * Checks the progression order changes values. Tells if the poc given as input are valid.
1521 * @param p_pocs the progression order changes.
1522 * @param p_nb_pocs the number of progression order changes.
1523 * @param p_nb_resolutions the number of resolutions.
1524 * @param numcomps the number of components
1525 * @param numlayers the number of layers.
1526 * @param p_manager the user event manager.
1528 * @return true if the pocs are valid.
1530 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1531 OPJ_UINT32 p_nb_pocs,
1532 OPJ_UINT32 p_nb_resolutions,
1533 OPJ_UINT32 p_num_comps,
1534 OPJ_UINT32 p_num_layers,
1535 opj_event_mgr_t * p_manager)
1537 OPJ_UINT32* packet_array;
1538 OPJ_UINT32 index , resno, compno, layno;
1540 OPJ_UINT32 step_c = 1;
1541 OPJ_UINT32 step_r = p_num_comps * step_c;
1542 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1543 opj_bool loss = OPJ_FALSE;
1544 OPJ_UINT32 layno0 = 0;
1546 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1547 if (packet_array == 00) {
1548 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1551 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1553 if (p_nb_pocs == 0) {
1557 index = step_r * p_pocs->resno0;
1558 // take each resolution for each poc
1559 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1561 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1563 // take each comp of each resolution for each poc
1564 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1565 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1567 // and finally take each layer of each res of ...
1568 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1569 //index = step_r * resno + step_c * compno + step_l * layno;
1570 packet_array[comp_index] = 1;
1571 comp_index += step_l;
1574 res_index += step_c;
1581 // iterate through all the pocs
1582 for (i = 1; i < p_nb_pocs ; ++i) {
1583 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1585 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1586 index = step_r * p_pocs->resno0;
1588 // take each resolution for each poc
1589 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1590 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1592 // take each comp of each resolution for each poc
1593 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1594 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1596 // and finally take each layer of each res of ...
1597 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1598 //index = step_r * resno + step_c * compno + step_l * layno;
1599 packet_array[comp_index] = 1;
1600 comp_index += step_l;
1603 res_index += step_c;
1613 for (layno = 0; layno < p_num_layers ; ++layno) {
1614 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1615 for (compno = 0; compno < p_num_comps; ++compno) {
1616 loss |= (packet_array[index]!=1);
1617 //index = step_r * resno + step_c * compno + step_l * layno;
1624 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1627 opj_free(packet_array);
1632 /* ----------------------------------------------------------------------- */
1633 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1636 int tpnum=1,tpend=0;
1637 opj_tcp_t *tcp = &cp->tcps[tileno];
1638 prog = j2k_convert_progression_order(tcp->prg);
1643 if( cp->tp_flag == prog[i] ){
1644 tpend=1;cp->tp_pos=i;
1648 tpnum= tpnum * tcp->pocs[pino].compE;
1651 tpnum= tpnum * tcp->pocs[pino].resE;
1654 tpnum= tpnum * tcp->pocs[pino].prcE;
1657 tpnum= tpnum * tcp->pocs[pino].layE;
1669 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1671 * @param cp the coding parameters.
1672 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1673 * @param tileno the given tile.
1675 * @return the number of tile parts.
1677 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1679 const OPJ_CHAR *prog = 00;
1681 OPJ_UINT32 tpnum = 1;
1682 opj_tcp_v2_t *tcp = 00;
1683 opj_poc_t * l_current_poc = 00;
1686 assert(tileno < (cp->tw * cp->th));
1687 assert(pino < (cp->tcps[tileno].numpocs + 1));
1689 /* get the given tile coding parameter */
1690 tcp = &cp->tcps[tileno];
1693 l_current_poc = &(tcp->pocs[pino]);
1694 assert(l_current_poc != 0);
1696 /* get the progression order as a character string */
1697 prog = j2k_convert_progression_order(tcp->prg);
1698 assert(strlen(prog) > 0);
1700 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1704 /* component wise */
1706 tpnum *= l_current_poc->compE;
1708 /* resolution wise */
1710 tpnum *= l_current_poc->resE;
1714 tpnum *= l_current_poc->prcE;
1718 tpnum *= l_current_poc->layE;
1721 /* whould we split here ? */
1722 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1723 cp->m_specific_param.m_enc.m_tp_pos=i;
1735 /** mem allocation for TLM marker*/
1736 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1737 int pino,tileno,totnum_tp=0;
1739 OPJ_ARG_NOT_USED(img_numcomp);
1741 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1742 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1743 int cur_totnum_tp = 0;
1744 opj_tcp_t *tcp = &cp->tcps[tileno];
1745 for(pino = 0; pino <= tcp->numpocs; pino++) {
1747 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1748 if(!pi) { return -1;}
1749 tp_num = j2k_get_num_tp(cp,pino,tileno);
1750 totnum_tp = totnum_tp + tp_num;
1751 cur_totnum_tp = cur_totnum_tp + tp_num;
1752 pi_destroy(pi, cp, tileno);
1754 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1756 if (j2k->cstr_info) {
1757 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1758 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1766 * Calculates the total number of tile parts needed by the encoder to
1767 * encode such an image. If not enough memory is available, then the function return false.
1769 * @param p_nb_tiles pointer that will hold the number of tile parts.
1770 * @param cp the coding parameters for the image.
1771 * @param image the image to encode.
1772 * @param p_j2k the p_j2k encoder.
1773 * @param p_manager the user event manager.
1775 * @return true if the function was successful, false else.
1777 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1779 OPJ_UINT32 * p_nb_tiles,
1781 opj_event_mgr_t * p_manager)
1783 OPJ_UINT32 pino,tileno;
1784 OPJ_UINT32 l_nb_tiles;
1788 assert(p_nb_tiles != 00);
1790 assert(image != 00);
1791 assert(p_j2k != 00);
1792 assert(p_manager != 00);
1794 l_nb_tiles = cp->tw * cp->th;
1799 /* TODO mergeV2: check this part which use cstr_info */
1800 /*if (p_j2k->cstr_info) {
1801 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1803 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1804 OPJ_UINT32 cur_totnum_tp = 0;
1806 pi_update_encoding_parameters(image,cp,tileno);
1808 for (pino = 0; pino <= tcp->numpocs; ++pino)
1810 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1812 *p_nb_tiles = *p_nb_tiles + tp_num;
1814 cur_totnum_tp += tp_num;
1817 tcp->m_nb_tile_parts = cur_totnum_tp;
1819 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1820 if (l_info_tile_ptr->tp == 00) {
1824 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1826 l_info_tile_ptr->num_tps = cur_totnum_tp;
1833 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1834 OPJ_UINT32 cur_totnum_tp = 0;
1836 pi_update_encoding_parameters(image,cp,tileno);
1838 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1839 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1841 *p_nb_tiles = *p_nb_tiles + tp_num;
1843 cur_totnum_tp += tp_num;
1845 tcp->m_nb_tile_parts = cur_totnum_tp;
1855 * Writes the SOC marker (Start Of Codestream)
1857 * @param p_stream the stream to write data to.
1858 * @param p_j2k J2K codec.
1859 * @param p_manager the user event manager.
1861 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1862 opj_stream_private_t *p_stream,
1863 opj_event_mgr_t * p_manager )
1865 /* 2 bytes will be written */
1866 OPJ_BYTE * l_start_stream = 00;
1869 assert(p_stream != 00);
1870 assert(p_j2k != 00);
1871 assert(p_manager != 00);
1873 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1875 /* write SOC identifier */
1876 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1878 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1884 /* update markers struct */
1886 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1888 assert( 0 && "TODO" );
1889 #endif /* USE_JPWL */
1898 * Reads a SOC marker (Start of Codestream)
1899 * @param p_header_data the data contained in the SOC box.
1900 * @param jp2 the jpeg2000 file codec.
1901 * @param p_header_size the size of the data contained in the SOC marker.
1902 * @param p_manager the user event manager.
1904 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1905 opj_stream_private_t *p_stream,
1906 opj_event_mgr_t * p_manager
1909 OPJ_BYTE l_data [2];
1910 OPJ_UINT32 l_marker;
1913 assert(p_j2k != 00);
1914 assert(p_manager != 00);
1915 assert(p_stream != 00);
1917 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1921 opj_read_bytes(l_data,&l_marker,2);
1922 if (l_marker != J2K_MS_SOC) {
1926 /* Next marker should be a SIZ marker in the main header */
1927 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1929 /* FIXME move it in a index structure included in p_j2k*/
1930 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1932 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1934 /* Add the marker to the codestream index*/
1935 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1942 * Writes the SIZ marker (image and tile size)
1944 * @param p_stream the stream to write data to.
1945 * @param p_j2k J2K codec.
1946 * @param p_manager the user event manager.
1948 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1949 opj_stream_private_t *p_stream,
1950 opj_event_mgr_t * p_manager )
1953 OPJ_UINT32 l_size_len;
1954 OPJ_BYTE * l_current_ptr;
1955 opj_image_t * l_image = 00;
1956 opj_cp_v2_t *cp = 00;
1957 opj_image_comp_t * l_img_comp = 00;
1960 assert(p_stream != 00);
1961 assert(p_j2k != 00);
1962 assert(p_manager != 00);
1964 l_image = p_j2k->m_private_image;
1965 cp = &(p_j2k->m_cp);
1966 l_size_len = 40 + 3 * l_image->numcomps;
1967 l_img_comp = l_image->comps;
1969 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1971 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1972 = (OPJ_BYTE*)opj_realloc(
1973 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1975 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1979 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1982 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1984 /* write SOC identifier */
1985 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1988 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1991 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1994 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1997 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2000 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2003 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2006 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2009 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2012 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2015 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2018 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2021 for (i = 0; i < l_image->numcomps; ++i) {
2022 /* TODO here with MCT ? */
2023 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2026 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2029 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2035 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) {
2045 * Reads a SIZ marker (image and tile size)
2046 * @param p_header_data the data contained in the SIZ box.
2047 * @param jp2 the jpeg2000 file codec.
2048 * @param p_header_size the size of the data contained in the SIZ marker.
2049 * @param p_manager the user event manager.
2051 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2052 OPJ_BYTE * p_header_data,
2053 OPJ_UINT32 p_header_size,
2054 opj_event_mgr_t * p_manager
2057 OPJ_UINT32 l_size, i;
2058 OPJ_UINT32 l_nb_comp;
2059 OPJ_UINT32 l_nb_comp_remain;
2060 OPJ_UINT32 l_remaining_size;
2061 OPJ_UINT32 l_nb_tiles;
2063 opj_image_t *l_image = 00;
2064 opj_cp_v2_t *l_cp = 00;
2065 opj_image_comp_t * l_img_comp = 00;
2066 opj_tcp_v2_t * l_current_tile_param = 00;
2069 assert(p_j2k != 00);
2070 assert(p_manager != 00);
2071 assert(p_header_data != 00);
2073 l_image = p_j2k->m_private_image;
2074 l_cp = &(p_j2k->m_cp);
2076 /* minimum size == 39 - 3 (= minimum component parameter) */
2077 if (p_header_size < 36) {
2078 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2082 l_remaining_size = p_header_size - 36;
2083 l_nb_comp = l_remaining_size / 3;
2084 l_nb_comp_remain = l_remaining_size % 3;
2085 if (l_nb_comp_remain != 0){
2086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2090 l_size = p_header_size + 2; /* Lsiz */
2092 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2094 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2095 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2097 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2099 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2101 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2103 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2105 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2107 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2109 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2111 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2114 l_image->numcomps = (OPJ_UINT16) l_tmp;
2116 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2120 if (l_image->numcomps != l_nb_comp) {
2121 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);
2126 if (l_cp->correct) {
2127 /* if JPWL is on, we check whether TX errors have damaged
2128 too much the SIZ parameters */
2129 if (!(l_image->x1 * l_image->y1)) {
2130 opj_event_msg_v2(p_manager, EVT_ERROR,
2131 "JPWL: bad image size (%d x %d)\n",
2132 l_image->x1, l_image->y1);
2133 if (!JPWL_ASSUME || JPWL_ASSUME) {
2134 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2139 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2140 if (l_image->numcomps != ((len - 38) / 3)) {
2141 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2142 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2143 l_image->numcomps, ((len - 38) / 3));
2145 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2148 */ /* we try to correct */
2149 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2150 if (l_image->numcomps < ((len - 38) / 3)) {
2151 len = 38 + 3 * l_image->numcomps;
2152 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2155 l_image->numcomps = ((len - 38) / 3);
2156 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2162 /* update components number in the jpwl_exp_comps filed */
2163 l_cp->exp_comps = l_image->numcomps;
2165 #endif /* USE_JPWL */
2167 /* Allocate the resulting image components */
2168 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2169 if (l_image->comps == 00){
2170 l_image->numcomps = 0;
2171 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2175 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2176 l_img_comp = l_image->comps;
2178 /* Read the component information */
2179 for (i = 0; i < l_image->numcomps; ++i){
2181 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2183 l_img_comp->prec = (tmp & 0x7f) + 1;
2184 l_img_comp->sgnd = tmp >> 7;
2185 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2187 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2188 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2190 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2193 if (l_cp->correct) {
2194 /* if JPWL is on, we check whether TX errors have damaged
2195 too much the SIZ parameters, again */
2196 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2197 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2198 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2199 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2201 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2204 /* we try to correct */
2205 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2206 if (!l_image->comps[i].dx) {
2207 l_image->comps[i].dx = 1;
2208 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2209 i, l_image->comps[i].dx);
2211 if (!l_image->comps[i].dy) {
2212 l_image->comps[i].dy = 1;
2213 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2214 i, l_image->comps[i].dy);
2218 #endif /* USE_JPWL */
2219 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2220 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2224 /* Compute the number of tiles */
2225 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2226 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2227 l_nb_tiles = l_cp->tw * l_cp->th;
2229 /* Define the tiles which will be decoded */
2230 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2231 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;
2232 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;
2233 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);
2234 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);
2237 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2238 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2239 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2240 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2244 if (l_cp->correct) {
2245 /* if JPWL is on, we check whether TX errors have damaged
2246 too much the SIZ parameters */
2247 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2248 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2249 "JPWL: bad number of tiles (%d x %d)\n",
2250 l_cp->tw, l_cp->th);
2252 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2255 /* we try to correct */
2256 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2259 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2262 if (l_cp->tw > l_cp->max_tiles) {
2264 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2265 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2266 l_cp->max_tiles, l_cp->tw);
2270 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2273 if (l_cp->th > l_cp->max_tiles) {
2275 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2276 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2277 l_cp->max_tiles, l_cp->th);
2281 #endif /* USE_JPWL */
2283 /* memory allocations */
2284 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2285 if (l_cp->tcps == 00) {
2286 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2289 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2292 if (l_cp->correct) {
2294 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2295 "JPWL: could not alloc tcps field of cp\n");
2296 if (!JPWL_ASSUME || JPWL_ASSUME) {
2297 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2302 #endif /* USE_JPWL */
2304 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2305 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2306 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2307 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2310 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2312 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2313 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2315 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2316 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2319 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));
2320 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2322 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2323 (opj_simple_mcc_decorrelation_data_t*)
2324 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2326 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2327 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2330 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));
2331 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2333 /* set up default dc level shift */
2334 for (i=0;i<l_image->numcomps;++i) {
2335 if (! l_image->comps[i].sgnd) {
2336 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2340 l_current_tile_param = l_cp->tcps;
2341 for (i = 0; i < l_nb_tiles; ++i) {
2342 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2343 if (l_current_tile_param->tccps == 00) {
2344 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2347 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2349 ++l_current_tile_param;
2352 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2353 opj_image_comp_header_update(l_image,l_cp);
2359 * Writes the COM marker (comment)
2361 * @param p_stream the stream to write data to.
2362 * @param p_j2k J2K codec.
2363 * @param p_manager the user event manager.
2365 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2366 opj_stream_private_t *p_stream,
2367 opj_event_mgr_t * p_manager
2370 OPJ_UINT32 l_comment_size;
2371 OPJ_UINT32 l_total_com_size;
2372 const OPJ_CHAR *l_comment;
2373 OPJ_BYTE * l_current_ptr = 00;
2376 assert(p_j2k != 00);
2377 assert(p_stream != 00);
2378 assert(p_manager != 00);
2380 l_comment = p_j2k->m_cp.comment;
2381 l_comment_size = strlen(l_comment);
2382 l_total_com_size = l_comment_size + 6;
2384 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2385 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2386 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2389 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2393 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2396 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2398 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2401 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2404 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2407 memcpy( l_current_ptr,l_comment,l_comment_size);
2409 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) {
2417 * Reads a COM marker (comments)
2418 * @param p_header_data the data contained in the COM box.
2419 * @param jp2 the jpeg2000 file codec.
2420 * @param p_header_size the size of the data contained in the COM marker.
2421 * @param p_manager the user event manager.
2423 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2424 OPJ_BYTE * p_header_data,
2425 OPJ_UINT32 p_header_size,
2426 opj_event_mgr_t * p_manager
2430 assert(p_j2k != 00);
2431 assert(p_manager != 00);
2432 assert(p_header_data != 00);
2433 (void)p_header_size;
2439 * Writes the COD marker (Coding style default)
2441 * @param p_stream the stream to write data to.
2442 * @param p_j2k J2K codec.
2443 * @param p_manager the user event manager.
2445 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2446 opj_stream_private_t *p_stream,
2447 opj_event_mgr_t * p_manager )
2449 opj_cp_v2_t *l_cp = 00;
2450 opj_tcp_v2_t *l_tcp = 00;
2451 OPJ_UINT32 l_code_size,l_remaining_size;
2452 OPJ_BYTE * l_current_data = 00;
2455 assert(p_j2k != 00);
2456 assert(p_manager != 00);
2457 assert(p_stream != 00);
2459 l_cp = &(p_j2k->m_cp);
2460 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2461 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2462 l_remaining_size = l_code_size;
2464 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2465 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2466 = (OPJ_BYTE*)opj_realloc(
2467 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2470 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2474 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2477 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2479 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2480 l_current_data += 2;
2482 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2483 l_current_data += 2;
2485 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2488 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2491 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2494 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2497 l_remaining_size -= 9;
2499 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2500 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2504 if (l_remaining_size != 0) {
2505 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2509 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) {
2518 * Reads a COD marker (Coding Styke defaults)
2519 * @param p_header_data the data contained in the COD box.
2520 * @param p_j2k the jpeg2000 codec.
2521 * @param p_header_size the size of the data contained in the COD marker.
2522 * @param p_manager the user event manager.
2524 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2525 OPJ_BYTE * p_header_data,
2526 OPJ_UINT32 p_header_size,
2527 opj_event_mgr_t * p_manager
2533 opj_cp_v2_t *l_cp = 00;
2534 opj_tcp_v2_t *l_tcp = 00;
2535 opj_image_t *l_image = 00;
2538 assert(p_header_data != 00);
2539 assert(p_j2k != 00);
2540 assert(p_manager != 00);
2542 l_image = p_j2k->m_private_image;
2543 l_cp = &(p_j2k->m_cp);
2545 /* If we are in the first tile-part header of the current tile */
2546 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2547 &l_cp->tcps[p_j2k->m_current_tile_number] :
2548 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2550 /* Make sure room is sufficient */
2551 if (p_header_size < 5) {
2552 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2556 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2558 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2560 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2561 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2564 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2565 if (l_cp->m_specific_param.m_dec.m_layer) {
2566 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2569 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2572 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2576 for (i = 0; i < l_image->numcomps; ++i) {
2577 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2580 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2581 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2585 if (p_header_size != 0) {
2586 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2590 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2591 opj_j2k_copy_tile_component_parameters(p_j2k);
2594 #ifdef WIP_REMOVE_MSD
2595 if (p_j2k->cstr_info) {
2596 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2597 p_j2k->cstr_info->prog = l_tcp->prg;
2598 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2599 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2600 for (i = 0; i < l_image->numcomps; ++i) {
2601 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2610 * Writes the COC marker (Coding style component)
2612 * @param p_comp_no the index of the component to output.
2613 * @param p_stream the stream to write data to.
2614 * @param p_j2k J2K codec.
2615 * @param p_manager the user event manager.
2617 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2618 OPJ_UINT32 p_comp_no,
2619 opj_stream_private_t *p_stream,
2620 opj_event_mgr_t * p_manager )
2622 OPJ_UINT32 l_coc_size,l_remaining_size;
2623 OPJ_UINT32 l_comp_room;
2626 assert(p_j2k != 00);
2627 assert(p_manager != 00);
2628 assert(p_stream != 00);
2630 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2632 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2634 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2635 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2636 = (OPJ_BYTE*)opj_realloc(
2637 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2639 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2643 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2646 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);
2648 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) {
2656 * Writes the COC marker (Coding style component)
2658 * @param p_comp_no the index of the component to output.
2659 * @param p_stream the stream to write data to.
2660 * @param p_j2k J2K codec.
2661 * @param p_manager the user event manager.
2663 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2664 OPJ_UINT32 p_comp_no,
2666 OPJ_UINT32 * p_data_written,
2667 opj_event_mgr_t * p_manager
2670 opj_cp_v2_t *l_cp = 00;
2671 opj_tcp_v2_t *l_tcp = 00;
2672 OPJ_UINT32 l_coc_size,l_remaining_size;
2673 OPJ_BYTE * l_current_data = 00;
2674 opj_image_t *l_image = 00;
2675 OPJ_UINT32 l_comp_room;
2678 assert(p_j2k != 00);
2679 assert(p_manager != 00);
2681 l_cp = &(p_j2k->m_cp);
2682 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2683 l_image = p_j2k->m_private_image;
2684 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2686 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2687 l_remaining_size = l_coc_size;
2689 l_current_data = p_data;
2691 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2692 l_current_data += 2;
2694 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2695 l_current_data += 2;
2697 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2698 l_current_data+=l_comp_room;
2700 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2703 l_remaining_size -= (5 + l_comp_room);
2704 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2705 * p_data_written = l_coc_size;
2709 * Gets the maximum size taken by a coc.
2711 * @param p_j2k the jpeg2000 codec to use.
2713 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2716 OPJ_UINT32 l_nb_comp;
2717 OPJ_UINT32 l_nb_tiles;
2718 OPJ_UINT32 l_max = 0;
2722 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2723 l_nb_comp = p_j2k->m_private_image->numcomps;
2725 for (i=0;i<l_nb_tiles;++i) {
2726 for (j=0;j<l_nb_comp;++j) {
2727 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2736 * Reads a COC marker (Coding Style Component)
2737 * @param p_header_data the data contained in the COC box.
2738 * @param p_j2k the jpeg2000 codec.
2739 * @param p_header_size the size of the data contained in the COC marker.
2740 * @param p_manager the user event manager.
2742 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2743 OPJ_BYTE * p_header_data,
2744 OPJ_UINT32 p_header_size,
2745 opj_event_mgr_t * p_manager
2748 opj_cp_v2_t *l_cp = NULL;
2749 opj_tcp_v2_t *l_tcp = NULL;
2750 opj_image_t *l_image = NULL;
2751 OPJ_UINT32 l_comp_room;
2752 OPJ_UINT32 l_comp_no;
2755 assert(p_header_data != 00);
2756 assert(p_j2k != 00);
2757 assert(p_manager != 00);
2759 l_cp = &(p_j2k->m_cp);
2760 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2761 &l_cp->tcps[p_j2k->m_current_tile_number] :
2762 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2763 l_image = p_j2k->m_private_image;
2765 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2767 /* make sure room is sufficient*/
2768 if (p_header_size < l_comp_room + 1) {
2769 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2772 p_header_size -= l_comp_room + 1;
2774 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2775 p_header_data += l_comp_room;
2776 if (l_comp_no >= l_image->numcomps) {
2777 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2781 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2784 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2785 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2789 if (p_header_size != 0) {
2790 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2797 * Writes the QCD marker (quantization default)
2799 * @param p_comp_number the index of the component to output.
2800 * @param p_stream the stream to write data to.
2801 * @param p_j2k J2K codec.
2802 * @param p_manager the user event manager.
2804 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2805 opj_stream_private_t *p_stream,
2806 opj_event_mgr_t * p_manager
2809 opj_cp_v2_t *l_cp = 00;
2810 opj_tcp_v2_t *l_tcp = 00;
2811 OPJ_UINT32 l_qcd_size,l_remaining_size;
2812 OPJ_BYTE * l_current_data = 00;
2815 assert(p_j2k != 00);
2816 assert(p_manager != 00);
2817 assert(p_stream != 00);
2819 l_cp = &(p_j2k->m_cp);
2820 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2821 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2822 l_remaining_size = l_qcd_size;
2824 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2825 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2826 = (OPJ_BYTE*)opj_realloc(
2827 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2830 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2834 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2837 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2839 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2840 l_current_data += 2;
2842 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2843 l_current_data += 2;
2845 l_remaining_size -= 4;
2847 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2848 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2852 if (l_remaining_size != 0) {
2853 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2857 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) {
2865 * Reads a QCD marker (Quantization defaults)
2866 * @param p_header_data the data contained in the QCD box.
2867 * @param p_j2k the jpeg2000 codec.
2868 * @param p_header_size the size of the data contained in the QCD marker.
2869 * @param p_manager the user event manager.
2871 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2872 OPJ_BYTE * p_header_data,
2873 OPJ_UINT32 p_header_size,
2874 opj_event_mgr_t * p_manager
2878 assert(p_header_data != 00);
2879 assert(p_j2k != 00);
2880 assert(p_manager != 00);
2882 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2883 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2887 if (p_header_size != 0) {
2888 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2892 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2893 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2899 * Writes the QCC marker (quantization component)
2901 * @param p_comp_no the index of the component to output.
2902 * @param p_stream the stream to write data to.
2903 * @param p_j2k J2K codec.
2904 * @param p_manager the user event manager.
2906 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2907 OPJ_UINT32 p_comp_no,
2908 opj_stream_private_t *p_stream,
2909 opj_event_mgr_t * p_manager
2912 OPJ_UINT32 l_qcc_size,l_remaining_size;
2915 assert(p_j2k != 00);
2916 assert(p_manager != 00);
2917 assert(p_stream != 00);
2919 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2920 l_remaining_size = l_qcc_size;
2922 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2923 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2924 = (OPJ_BYTE*)opj_realloc(
2925 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2927 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2931 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2934 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);
2936 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) {
2944 * Writes the QCC marker (quantization component)
2946 * @param p_comp_no the index of the component to output.
2947 * @param p_stream the stream to write data to.
2948 * @param p_j2k J2K codec.
2949 * @param p_manager the user event manager.
2951 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2952 OPJ_UINT32 p_comp_no,
2954 OPJ_UINT32 * p_data_written,
2955 opj_event_mgr_t * p_manager
2958 OPJ_UINT32 l_qcc_size,l_remaining_size;
2959 OPJ_BYTE * l_current_data = 00;
2962 assert(p_j2k != 00);
2963 assert(p_manager != 00);
2965 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2966 l_remaining_size = l_qcc_size;
2968 l_current_data = p_data;
2970 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2971 l_current_data += 2;
2973 if (p_j2k->m_private_image->numcomps <= 256) {
2976 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2977 l_current_data += 2;
2979 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2982 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2983 l_remaining_size -= 6;
2986 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2987 l_current_data += 2;
2989 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2992 l_remaining_size -= 6;
2995 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2997 *p_data_written = l_qcc_size;
3001 * Gets the maximum size taken by a qcc.
3003 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3005 return opj_j2k_get_max_coc_size(p_j2k);
3009 * Reads a QCC marker (Quantization component)
3010 * @param p_header_data the data contained in the QCC box.
3011 * @param p_j2k the jpeg2000 codec.
3012 * @param p_header_size the size of the data contained in the QCC marker.
3013 * @param p_manager the user event manager.
3015 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3016 OPJ_BYTE * p_header_data,
3017 OPJ_UINT32 p_header_size,
3018 opj_event_mgr_t * p_manager
3021 OPJ_UINT32 l_num_comp,l_comp_no;
3024 assert(p_header_data != 00);
3025 assert(p_j2k != 00);
3026 assert(p_manager != 00);
3028 l_num_comp = p_j2k->m_private_image->numcomps;
3030 if (l_num_comp <= 256) {
3031 if (p_header_size < 1) {
3032 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3035 opj_read_bytes(p_header_data,&l_comp_no,1);
3040 if (p_header_size < 2) {
3041 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3044 opj_read_bytes(p_header_data,&l_comp_no,2);
3050 if (p_j2k->m_cp.correct) {
3052 static OPJ_UINT32 backup_compno = 0;
3054 /* compno is negative or larger than the number of components!!! */
3055 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3056 opj_event_msg_v2(p_manager, EVT_ERROR,
3057 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3058 l_comp_no, l_num_comp);
3060 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3063 /* we try to correct */
3064 l_comp_no = backup_compno % l_num_comp;
3065 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3066 "- setting component number to %d\n",
3070 /* keep your private count of tiles */
3073 #endif /* USE_JPWL */
3075 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3076 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3080 if (p_header_size != 0) {
3081 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3089 * Writes the POC marker (Progression Order Change)
3091 * @param p_stream the stream to write data to.
3092 * @param p_j2k J2K codec.
3093 * @param p_manager the user event manager.
3095 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
3096 opj_stream_private_t *p_stream,
3097 opj_event_mgr_t * p_manager
3100 OPJ_UINT32 l_nb_comp;
3101 OPJ_UINT32 l_nb_poc;
3102 OPJ_UINT32 l_poc_size;
3103 OPJ_UINT32 l_written_size = 0;
3104 opj_tcp_v2_t *l_tcp = 00;
3105 opj_tccp_t *l_tccp = 00;
3106 OPJ_UINT32 l_poc_room;
3109 assert(p_j2k != 00);
3110 assert(p_manager != 00);
3111 assert(p_stream != 00);
3113 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3114 l_tccp = &l_tcp->tccps[0];
3115 l_nb_comp = p_j2k->m_private_image->numcomps;
3116 l_nb_poc = 1 + l_tcp->numpocs;
3118 if (l_nb_comp <= 256) {
3124 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3126 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3127 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3128 = (OPJ_BYTE*)opj_realloc(
3129 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3132 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3136 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3139 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3141 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) {
3150 * Writes the POC marker (Progression Order Change)
3152 * @param p_stream the stream to write data to.
3153 * @param p_j2k J2K codec.
3154 * @param p_manager the user event manager.
3156 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3158 OPJ_UINT32 * p_data_written,
3159 opj_event_mgr_t * p_manager
3163 OPJ_BYTE * l_current_data = 00;
3164 OPJ_UINT32 l_nb_comp;
3165 OPJ_UINT32 l_nb_poc;
3166 OPJ_UINT32 l_poc_size;
3167 opj_image_t *l_image = 00;
3168 opj_tcp_v2_t *l_tcp = 00;
3169 opj_tccp_t *l_tccp = 00;
3170 opj_poc_t *l_current_poc = 00;
3171 OPJ_UINT32 l_poc_room;
3174 assert(p_j2k != 00);
3175 assert(p_manager != 00);
3177 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3178 l_tccp = &l_tcp->tccps[0];
3179 l_image = p_j2k->m_private_image;
3180 l_nb_comp = l_image->numcomps;
3181 l_nb_poc = 1 + l_tcp->numpocs;
3183 if (l_nb_comp <= 256) {
3190 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3192 l_current_data = p_data;
3194 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3195 l_current_data += 2;
3197 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3198 l_current_data += 2;
3200 l_current_poc = l_tcp->pocs;
3201 for (i = 0; i < l_nb_poc; ++i) {
3202 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3205 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3206 l_current_data+=l_poc_room;
3208 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3211 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3214 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3215 l_current_data+=l_poc_room;
3217 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3220 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3221 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3222 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3223 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3228 *p_data_written = l_poc_size;
3232 * Gets the maximum size taken by the writing of a POC.
3234 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3236 opj_tcp_v2_t * l_tcp = 00;
3237 OPJ_UINT32 l_nb_tiles = 0;
3238 OPJ_UINT32 l_max_poc = 0;
3241 l_tcp = p_j2k->m_cp.tcps;
3242 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3244 for (i=0;i<l_nb_tiles;++i) {
3245 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3251 return 4 + 9 * l_max_poc;
3255 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3257 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3260 OPJ_UINT32 l_nb_tiles;
3261 OPJ_UINT32 l_max = 0;
3262 opj_tcp_v2_t * l_tcp = 00;
3264 l_tcp = p_j2k->m_cp.tcps;
3265 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3267 for (i=0;i<l_nb_tiles;++i) {
3268 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3278 * Gets the maximum size taken by the headers of the SOT.
3280 * @param p_j2k the jpeg2000 codec to use.
3282 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3284 OPJ_UINT32 l_nb_bytes = 0;
3285 OPJ_UINT32 l_nb_comps;
3286 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3288 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3289 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3291 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3292 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3293 l_nb_bytes += l_nb_comps * l_coc_bytes;
3295 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3296 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3299 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3301 /*** DEVELOPER CORNER, Add room for your headers ***/
3308 * Reads a POC marker (Progression Order Change)
3310 * @param p_header_data the data contained in the POC box.
3311 * @param p_j2k the jpeg2000 codec.
3312 * @param p_header_size the size of the data contained in the POC marker.
3313 * @param p_manager the user event manager.
3315 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3316 OPJ_BYTE * p_header_data,
3317 OPJ_UINT32 p_header_size,
3318 opj_event_mgr_t * p_manager
3321 OPJ_UINT32 i, l_nb_comp, l_tmp;
3322 opj_image_t * l_image = 00;
3323 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3324 OPJ_UINT32 l_chunk_size, l_comp_room;
3326 opj_cp_v2_t *l_cp = 00;
3327 opj_tcp_v2_t *l_tcp = 00;
3328 opj_poc_t *l_current_poc = 00;
3331 assert(p_header_data != 00);
3332 assert(p_j2k != 00);
3333 assert(p_manager != 00);
3335 l_image = p_j2k->m_private_image;
3336 l_nb_comp = l_image->numcomps;
3337 if (l_nb_comp <= 256) {
3343 l_chunk_size = 5 + 2 * l_comp_room;
3344 l_current_poc_nb = p_header_size / l_chunk_size;
3345 l_current_poc_remaining = p_header_size % l_chunk_size;
3347 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3348 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3352 l_cp = &(p_j2k->m_cp);
3353 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3354 &l_cp->tcps[p_j2k->m_current_tile_number] :
3355 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3356 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3357 l_current_poc_nb += l_old_poc_nb;
3359 assert(l_current_poc_nb < 32);
3361 /* now poc is in use.*/
3364 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3365 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3366 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3368 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3369 p_header_data+=l_comp_room;
3370 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3372 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3374 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3375 p_header_data+=l_comp_room;
3376 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3378 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3379 /* make sure comp is in acceptable bounds */
3380 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3384 l_tcp->numpocs = l_current_poc_nb - 1;
3389 * Reads a CRG marker (Component registration)
3391 * @param p_header_data the data contained in the TLM box.
3392 * @param p_j2k the jpeg2000 codec.
3393 * @param p_header_size the size of the data contained in the TLM marker.
3394 * @param p_manager the user event manager.
3396 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3397 OPJ_BYTE * p_header_data,
3398 OPJ_UINT32 p_header_size,
3399 opj_event_mgr_t * p_manager
3402 OPJ_UINT32 l_nb_comp;
3404 assert(p_header_data != 00);
3405 assert(p_j2k != 00);
3406 assert(p_manager != 00);
3408 l_nb_comp = p_j2k->m_private_image->numcomps;
3410 if (p_header_size != l_nb_comp *4) {
3411 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3414 /* Do not care of this at the moment since only local variables are set here */
3417 (i = 0; i < l_nb_comp; ++i)
3419 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3421 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3429 * Reads a TLM marker (Tile Length Marker)
3431 * @param p_header_data the data contained in the TLM box.
3432 * @param p_j2k the jpeg2000 codec.
3433 * @param p_header_size the size of the data contained in the TLM marker.
3434 * @param p_manager the user event manager.
3436 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3437 OPJ_BYTE * p_header_data,
3438 OPJ_UINT32 p_header_size,
3439 opj_event_mgr_t * p_manager
3442 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3444 assert(p_header_data != 00);
3445 assert(p_j2k != 00);
3446 assert(p_manager != 00);
3448 if (p_header_size < 2) {
3449 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3454 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3456 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3459 l_ST = ((l_Stlm >> 4) & 0x3);
3460 l_SP = (l_Stlm >> 6) & 0x1;
3462 l_Ptlm_size = (l_SP + 1) * 2;
3463 l_quotient = l_Ptlm_size + l_ST;
3465 l_tot_num_tp = p_header_size / l_quotient;
3466 l_tot_num_tp_remaining = p_header_size % l_quotient;
3468 if (l_tot_num_tp_remaining != 0) {
3469 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3472 /* FIXME Do not care of this at the moment since only local variables are set here */
3475 (i = 0; i < l_tot_num_tp; ++i)
3477 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3478 p_header_data += l_ST;
3479 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3480 p_header_data += l_Ptlm_size;
3487 * Reads a PLM marker (Packet length, main header marker)
3489 * @param p_header_data the data contained in the TLM box.
3490 * @param p_j2k the jpeg2000 codec.
3491 * @param p_header_size the size of the data contained in the TLM marker.
3492 * @param p_manager the user event manager.
3494 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3495 OPJ_BYTE * p_header_data,
3496 OPJ_UINT32 p_header_size,
3497 opj_event_mgr_t * p_manager
3501 assert(p_header_data != 00);
3502 assert(p_j2k != 00);
3503 assert(p_manager != 00);
3505 if (p_header_size < 1) {
3506 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3509 /* Do not care of this at the moment since only local variables are set here */
3511 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3518 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3520 p_header_size -= (1+l_Nplm);
3524 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3528 (i = 0; i < l_Nplm; ++i)
3530 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3532 // take only the last seven bytes
3533 l_packet_len |= (l_tmp & 0x7f);
3541 // store packet length and proceed to next packet
3548 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3557 * Reads a PLT marker (Packet length, tile-part header)
3559 * @param p_header_data the data contained in the PLT box.
3560 * @param p_j2k the jpeg2000 codec.
3561 * @param p_header_size the size of the data contained in the PLT marker.
3562 * @param p_manager the user event manager.
3564 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3565 OPJ_BYTE * p_header_data,
3566 OPJ_UINT32 p_header_size,
3567 opj_event_mgr_t * p_manager
3570 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3573 assert(p_header_data != 00);
3574 assert(p_j2k != 00);
3575 assert(p_manager != 00);
3577 if (p_header_size < 1) {
3578 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3582 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3586 for (i = 0; i < p_header_size; ++i) {
3587 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3589 /* take only the last seven bytes */
3590 l_packet_len |= (l_tmp & 0x7f);
3595 /* store packet length and proceed to next packet */
3600 if (l_packet_len != 0) {
3601 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3610 * Reads a PPM marker (Packed packet headers, main header)
3612 * @param p_header_data the data contained in the POC box.
3613 * @param p_j2k the jpeg2000 codec.
3614 * @param p_header_size the size of the data contained in the POC marker.
3615 * @param p_manager the user event manager.
3618 opj_bool j2k_read_ppm_v2 (
3619 opj_j2k_v2_t *p_j2k,
3620 OPJ_BYTE * p_header_data,
3621 OPJ_UINT32 p_header_size,
3622 struct opj_event_mgr * p_manager
3626 opj_cp_v2_t *l_cp = 00;
3627 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3630 assert(p_header_data != 00);
3631 assert(p_j2k != 00);
3632 assert(p_manager != 00);
3634 if (p_header_size < 1) {
3635 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3639 l_cp = &(p_j2k->m_cp);
3642 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3646 /* First PPM marker */
3648 if (p_header_size < 4) {
3649 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3653 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3657 /* First PPM marker: Initialization */
3658 l_cp->ppm_len = l_N_ppm;
3659 l_cp->ppm_data_size = 0;
3661 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3662 if (l_cp->ppm_buffer == 00) {
3663 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3666 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3668 l_cp->ppm_data = l_cp->ppm_buffer;
3672 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3673 if (p_header_size >= 4) {
3675 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3678 l_cp->ppm_len += l_N_ppm ;
3680 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3681 if (l_cp->ppm_buffer == 00) {
3682 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3685 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3687 l_cp->ppm_data = l_cp->ppm_buffer;
3694 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3696 if (l_remaining_data <= p_header_size) {
3697 /* we must store less information than available in the packet */
3698 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3699 l_cp->ppm_data_size = l_cp->ppm_len;
3700 p_header_size -= l_remaining_data;
3701 p_header_data += l_remaining_data;
3704 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3705 l_cp->ppm_data_size += p_header_size;
3706 p_header_data += p_header_size;
3719 * Reads a PPM marker (Packed packet headers, main header)
3721 * @param p_header_data the data contained in the POC box.
3722 * @param p_j2k the jpeg2000 codec.
3723 * @param p_header_size the size of the data contained in the POC marker.
3724 * @param p_manager the user event manager.
3726 opj_bool j2k_read_ppm_v3 (
3727 opj_j2k_v2_t *p_j2k,
3728 OPJ_BYTE * p_header_data,
3729 OPJ_UINT32 p_header_size,
3730 struct opj_event_mgr * p_manager
3733 opj_cp_v2_t *l_cp = 00;
3734 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3737 assert(p_header_data != 00);
3738 assert(p_j2k != 00);
3739 assert(p_manager != 00);
3741 /* Minimum size of PPM marker is equal to the size of Zppm element */
3742 if (p_header_size < 1) {
3743 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3747 l_cp = &(p_j2k->m_cp);
3750 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3754 /* First PPM marker */
3756 /* We need now at least the Nppm^0 element */
3757 if (p_header_size < 4) {
3758 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3762 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3766 /* First PPM marker: Initialization */
3767 l_cp->ppm_len = l_N_ppm;
3768 l_cp->ppm_data_read = 0;
3770 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3771 if (l_cp->ppm_data == 00) {
3772 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3775 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3777 l_cp->ppm_data_current = l_cp->ppm_data;
3779 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3782 if (p_header_size < 4) {
3783 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3787 /* Uncompleted Ippm series in the previous PPM marker?*/
3788 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3789 /* Get the place where add the remaining Ippm series*/
3790 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3791 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3794 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3798 /* Increase the size of ppm_data to add the new Ippm series*/
3799 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3801 /* Keep the position of the place where concatenate the new series*/
3802 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3803 l_cp->ppm_len += l_N_ppm;
3808 l_remaining_data = p_header_size;
3810 while (l_remaining_data >= l_N_ppm) {
3811 /* read a complete Ippm series*/
3812 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3813 p_header_size -= l_N_ppm;
3814 p_header_data += l_N_ppm;
3816 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3820 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3825 l_remaining_data = p_header_size;
3829 l_remaining_data = p_header_size;
3831 /* Next Ippm series is a complete series ?*/
3832 if (l_remaining_data > l_N_ppm) {
3833 /* Increase the size of ppm_data to add the new Ippm series*/
3834 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3836 /* Keep the position of the place where concatenate the new series */
3837 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3838 l_cp->ppm_len += l_N_ppm;
3843 /* Need to read an incomplete Ippm series*/
3844 if (l_remaining_data) {
3845 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3847 /* Keep the position of the place where concatenate the new series*/
3848 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3849 l_cp->ppm_len += l_N_ppm;
3851 /* Read incomplete Ippm series*/
3852 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3853 p_header_size -= l_remaining_data;
3854 p_header_data += l_remaining_data;
3856 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3861 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3862 if (p_header_size >= 4) {
3864 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3867 l_cp->ppm_len += l_N_ppm ;
3869 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3870 if (l_cp->ppm_buffer == 00) {
3871 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3874 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3876 l_cp->ppm_data = l_cp->ppm_buffer;
3883 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3885 if (l_remaining_data <= p_header_size) {
3886 /* we must store less information than available in the packet */
3887 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3888 l_cp->ppm_data_size = l_cp->ppm_len;
3889 p_header_size -= l_remaining_data;
3890 p_header_data += l_remaining_data;
3893 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3894 l_cp->ppm_data_size += p_header_size;
3895 p_header_data += p_header_size;
3905 * Reads a PPT marker (Packed packet headers, tile-part header)
3907 * @param p_header_data the data contained in the PPT box.
3908 * @param p_j2k the jpeg2000 codec.
3909 * @param p_header_size the size of the data contained in the PPT marker.
3910 * @param p_manager the user event manager.
3912 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3913 OPJ_BYTE * p_header_data,
3914 OPJ_UINT32 p_header_size,
3915 opj_event_mgr_t * p_manager
3918 opj_cp_v2_t *l_cp = 00;
3919 opj_tcp_v2_t *l_tcp = 00;
3923 assert(p_header_data != 00);
3924 assert(p_j2k != 00);
3925 assert(p_manager != 00);
3927 /* We need to have the Z_ppt element at minimum */
3928 if (p_header_size < 1) {
3929 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3933 l_cp = &(p_j2k->m_cp);
3935 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");
3939 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3942 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3946 /* Allocate buffer to read the packet header */
3948 /* First PPT marker */
3949 l_tcp->ppt_data_size = 0;
3950 l_tcp->ppt_len = p_header_size;
3952 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3953 if (l_tcp->ppt_buffer == 00) {
3954 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3957 l_tcp->ppt_data = l_tcp->ppt_buffer;
3959 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3962 l_tcp->ppt_len += p_header_size;
3964 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3965 if (l_tcp->ppt_buffer == 00) {
3966 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3969 l_tcp->ppt_data = l_tcp->ppt_buffer;
3971 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3974 /* Read packet header from buffer */
3975 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3977 l_tcp->ppt_data_size += p_header_size;
3983 * Writes the TLM marker (Tile Length Marker)
3985 * @param p_stream the stream to write data to.
3986 * @param p_j2k J2K codec.
3987 * @param p_manager the user event manager.
3989 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3990 opj_stream_private_t *p_stream,
3991 opj_event_mgr_t * p_manager
3994 OPJ_BYTE * l_current_data = 00;
3995 OPJ_UINT32 l_tlm_size;
3998 assert(p_j2k != 00);
3999 assert(p_manager != 00);
4000 assert(p_stream != 00);
4002 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4004 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4005 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4006 = (OPJ_BYTE*)opj_realloc(
4007 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4010 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4014 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4017 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4019 /* change the way data is written to avoid seeking if possible */
4021 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4023 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4024 l_current_data += 2;
4026 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4027 l_current_data += 2;
4029 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4032 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4035 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4036 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) {
4043 static void j2k_write_sot(opj_j2k_t *j2k) {
4046 opj_cio_t *cio = j2k->cio;
4048 j2k->sot_start = cio_tell(cio);
4049 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4050 lenp = cio_tell(cio);
4051 cio_skip(cio, 2); /* Lsot (further) */
4052 cio_write(cio, j2k->curtileno, 2); /* Isot */
4053 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4054 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4055 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4056 len = cio_tell(cio) - lenp;
4057 cio_seek(cio, lenp);
4058 cio_write(cio, len, 2); /* Lsot */
4059 cio_seek(cio, lenp + len);
4063 /* update markers struct */
4064 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4065 #endif /* USE_JPWL */
4068 if( j2k->cstr_info && j2k->cur_tp_num==0){
4069 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4074 * Writes the SOT marker (Start of tile-part)
4076 * @param p_stream the stream to write data to.
4077 * @param p_j2k J2K codec.
4078 * @param p_manager the user event manager.
4080 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4082 OPJ_UINT32 * p_data_written,
4083 const struct opj_stream_private *p_stream,
4084 struct opj_event_mgr * p_manager )
4087 assert(p_j2k != 00);
4088 assert(p_manager != 00);
4089 assert(p_stream != 00);
4091 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4094 opj_write_bytes(p_data,10,2); /* Lsot */
4097 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4103 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4106 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4111 /* update markers struct */
4113 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4115 assert( 0 && "TODO" );
4116 #endif /* USE_JPWL */
4118 * p_data_written = 12;
4127 * Reads a PPT marker (Packed packet headers, tile-part header)
4129 * @param p_header_data the data contained in the PPT box.
4130 * @param p_j2k the jpeg2000 codec.
4131 * @param p_header_size the size of the data contained in the PPT marker.
4132 * @param p_manager the user event manager.
4134 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4135 OPJ_BYTE * p_header_data,
4136 OPJ_UINT32 p_header_size,
4137 opj_event_mgr_t * p_manager )
4139 opj_cp_v2_t *l_cp = 00;
4140 opj_tcp_v2_t *l_tcp = 00;
4141 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4142 OPJ_UINT32 l_current_part;
4143 OPJ_UINT32 l_tile_x,l_tile_y;
4146 assert(p_header_data != 00);
4147 assert(p_j2k != 00);
4148 assert(p_manager != 00);
4150 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4151 if (p_header_size != 8) {
4152 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4156 l_cp = &(p_j2k->m_cp);
4157 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4160 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4161 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4162 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4165 if (l_cp->correct) {
4167 int tileno = p_j2k->m_current_tile_number;
4168 static int backup_tileno = 0;
4170 /* tileno is negative or larger than the number of tiles!!! */
4171 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4172 opj_event_msg_v2(p_manager, EVT_ERROR,
4173 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4174 tileno, (l_cp->tw * l_cp->th));
4176 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4179 /* we try to correct */
4180 tileno = backup_tileno;
4181 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4182 "- setting tile number to %d\n",
4186 /* keep your private count of tiles */
4189 #endif /* USE_JPWL */
4191 /* look for the tile in the list of already processed tile (in parts). */
4192 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4193 /* since the time taken by this function can only grow at the time */
4195 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4198 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4199 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4201 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4207 if (l_cp->correct) {
4209 /* totlen is negative or larger than the bytes left!!! */
4210 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))) { */
4211 opj_event_msg_v2(p_manager, EVT_ERROR,
4212 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4213 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4215 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4218 /* we try to correct */
4220 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4221 "- setting Psot to %d => assuming it is the last tile\n",
4225 #endif /* USE_JPWL */
4227 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4229 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4230 "we assuming it is the last tile-part of the codestream.\n");
4231 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4234 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4237 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4240 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4241 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4242 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4243 if (l_tcp->m_nb_tile_parts) {
4244 if (l_current_part >= l_tcp->m_nb_tile_parts){
4245 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4246 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4247 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4251 l_tcp->m_nb_tile_parts = l_num_parts;
4254 /* If know the number of tile part header we will check if we didn't read the last*/
4255 if (l_tcp->m_nb_tile_parts) {
4256 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4257 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4261 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4262 /* Keep the size of data to skip after this marker */
4263 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4266 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4267 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4270 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4272 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4273 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4274 p_j2k->m_specific_param.m_decoder.m_skip_data =
4275 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4276 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4277 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4278 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4281 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4282 p_j2k->m_specific_param.m_decoder.m_skip_data =
4283 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4287 if (p_j2k->cstr_index)
4289 assert(p_j2k->cstr_index->tile_index != 00);
4290 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4291 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4293 if (l_num_parts != 0){
4294 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4295 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4298 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4299 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4300 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4302 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4303 (opj_tp_index_t*)opj_realloc(
4304 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4305 l_num_parts* sizeof(opj_tp_index_t));
4308 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4310 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4311 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4312 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4313 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4314 sizeof(opj_tp_index_t));
4317 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4318 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4319 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4320 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4321 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4322 * sizeof(opj_tp_index_t));
4331 /* 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 */
4332 /* if (p_j2k->cstr_info) {
4335 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4338 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4339 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4340 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4341 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4344 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4347 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4351 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4354 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4355 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4356 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4361 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4364 opj_tcp_t *tcp = NULL;
4365 opj_codestream_info_t *cstr_info = NULL;
4367 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4368 opj_cp_t *cp = j2k->cp;
4369 opj_cio_t *cio = j2k->cio;
4371 tcd->tp_num = j2k->tp_num ;
4372 tcd->cur_tp_num = j2k->cur_tp_num;
4374 cio_write(cio, J2K_MS_SOD, 2);
4376 if( j2k->cstr_info && j2k->cur_tp_num==0){
4377 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4380 if (j2k->curtileno == 0) {
4381 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4385 cstr_info = j2k->cstr_info;
4387 if (!j2k->cur_tp_num ) {
4388 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4389 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4392 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4393 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4397 /* update markers struct */
4398 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4399 #endif /* USE_JPWL */
4404 tcp = &cp->tcps[j2k->curtileno];
4405 for (layno = 0; layno < tcp->numlayers; layno++) {
4406 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4407 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4408 } else if (tcp->rates[layno]) {
4409 tcp->rates[layno]=1;
4412 if(j2k->cur_tp_num == 0){
4413 tcd->tcd_image->tiles->packno = 0;
4415 cstr_info->packno = 0;
4418 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4420 /* Writing Psot in SOT marker */
4421 totlen = cio_tell(cio) + l - j2k->sot_start;
4422 cio_seek(cio, j2k->sot_start + 6);
4423 cio_write(cio, totlen, 4);
4424 cio_seek(cio, j2k->sot_start + totlen);
4425 /* Writing Ttlm and Ptlm in TLM marker */
4427 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4428 cio_write(cio, j2k->curtileno, 1);
4429 cio_write(cio, totlen, 4);
4431 cio_seek(cio, j2k->sot_start + totlen);
4435 * Writes the SOD marker (Start of data)
4437 * @param p_stream the stream to write data to.
4438 * @param p_j2k J2K codec.
4439 * @param p_manager the user event manager.
4441 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4442 struct opj_tcd_v2 * p_tile_coder,
4444 OPJ_UINT32 * p_data_written,
4445 OPJ_UINT32 p_total_data_size,
4446 const struct opj_stream_private *p_stream,
4447 struct opj_event_mgr * p_manager )
4449 opj_tcp_v2_t *l_tcp = 00;
4450 opj_codestream_info_t *l_cstr_info = 00;
4451 opj_cp_v2_t *l_cp = 00;
4453 OPJ_UINT32 l_size_tile;
4454 OPJ_UINT32 l_remaining_data;
4457 assert(p_j2k != 00);
4458 assert(p_manager != 00);
4459 assert(p_stream != 00);
4461 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4464 /* make room for the EOF marker */
4465 l_remaining_data = p_total_data_size - 4;
4467 l_cp = &(p_j2k->m_cp);
4468 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4471 /* update tile coder */
4472 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4473 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4475 l_size_tile = l_cp->th * l_cp->tw;
4478 /* TODO mergeV2: check this part which use cstr_info */
4479 /*l_cstr_info = p_j2k->cstr_info;
4481 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4482 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4483 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4489 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4491 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4496 /* update markers struct */
4497 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4499 assert( 0 && "TODO" );
4500 #endif /* USE_JPWL */
4505 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4506 p_tile_coder->tcd_image->tiles->packno = 0;
4508 l_cstr_info->packno = 0;
4512 *p_data_written = 0;
4514 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)) {
4515 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4519 *p_data_written += 2;
4528 * Reads a SOD marker (Start Of Data)
4530 * @param p_header_data the data contained in the SOD box.
4531 * @param p_j2k the jpeg2000 codec.
4532 * @param p_header_size the size of the data contained in the SOD marker.
4533 * @param p_manager the user event manager.
4535 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4536 opj_stream_private_t *p_stream,
4537 opj_event_mgr_t * p_manager
4540 OPJ_UINT32 l_current_read_size;
4541 opj_codestream_index_t * l_cstr_index = 00;
4542 OPJ_BYTE ** l_current_data = 00;
4543 opj_tcp_v2_t * l_tcp = 00;
4544 OPJ_UINT32 * l_tile_len = 00;
4547 assert(p_j2k != 00);
4548 assert(p_manager != 00);
4549 assert(p_stream != 00);
4551 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4553 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4554 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4555 // but we are in the last tile part,
4556 // so its result will fit on OPJ_UINT32 unless we find
4557 // a file with a single tile part of more than 4 GB...*/
4558 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4561 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4563 l_current_data = &(l_tcp->m_data);
4564 l_tile_len = &l_tcp->m_data_size;
4566 if (! *l_current_data) {
4567 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4570 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4573 if (*l_current_data == 00) {
4574 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4580 l_cstr_index = p_j2k->cstr_index;
4582 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4584 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4585 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4587 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4588 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4590 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4594 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4596 /*l_cstr_index->packno = 0;*/
4599 l_current_read_size = opj_stream_read_data( p_stream,
4600 *l_current_data + *l_tile_len,
4601 p_j2k->m_specific_param.m_decoder.m_sot_length,
4604 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4605 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4608 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4611 *l_tile_len += l_current_read_size;
4617 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4618 opj_cp_t *cp = j2k->cp;
4619 opj_tcp_t *tcp = &cp->tcps[tileno];
4620 opj_cio_t *cio = j2k->cio;
4621 int numcomps = j2k->image->numcomps;
4623 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4624 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4625 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4626 cio_write(cio, 0, 1); /* Srgn */
4627 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4631 * Writes the RGN marker (Region Of Interest)
4633 * @param p_tile_no the tile to output
4634 * @param p_comp_no the component to output
4635 * @param p_stream the stream to write data to.
4636 * @param p_j2k J2K codec.
4637 * @param p_manager the user event manager.
4639 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4640 OPJ_UINT32 p_tile_no,
4641 OPJ_UINT32 p_comp_no,
4642 struct opj_stream_private *p_stream,
4643 struct opj_event_mgr * p_manager )
4645 OPJ_BYTE * l_current_data = 00;
4646 OPJ_UINT32 l_nb_comp;
4647 OPJ_UINT32 l_rgn_size;
4648 opj_image_t *l_image = 00;
4649 opj_cp_v2_t *l_cp = 00;
4650 opj_tcp_v2_t *l_tcp = 00;
4651 opj_tccp_t *l_tccp = 00;
4652 OPJ_UINT32 l_comp_room;
4655 assert(p_j2k != 00);
4656 assert(p_manager != 00);
4657 assert(p_stream != 00);
4659 l_cp = &(p_j2k->m_cp);
4660 l_tcp = &l_cp->tcps[p_tile_no];
4661 l_tccp = &l_tcp->tccps[p_comp_no];
4663 l_nb_comp = l_image->numcomps;
4665 if (l_nb_comp <= 256) {
4672 l_rgn_size = 6 + l_comp_room;
4674 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4676 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4677 l_current_data += 2;
4679 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4680 l_current_data += 2;
4682 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4683 l_current_data+=l_comp_room;
4685 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4688 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4691 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) {
4699 static void j2k_write_eoc(opj_j2k_t *j2k) {
4700 opj_cio_t *cio = j2k->cio;
4701 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4702 cio_write(cio, J2K_MS_EOC, 2);
4706 /* update markers struct */
4707 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4708 #endif /* USE_JPWL */
4713 * Writes the EOC marker (End of Codestream)
4715 * @param p_stream the stream to write data to.
4716 * @param p_j2k J2K codec.
4717 * @param p_manager the user event manager.
4719 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4720 struct opj_stream_private *p_stream,
4721 struct opj_event_mgr * p_manager )
4724 assert(p_j2k != 00);
4725 assert(p_manager != 00);
4726 assert(p_stream != 00);
4728 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4733 /* update markers struct */
4735 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4737 #endif /* USE_JPWL */
4739 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4743 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4752 * Reads a RGN marker (Region Of Interest)
4754 * @param p_header_data the data contained in the POC box.
4755 * @param p_j2k the jpeg2000 codec.
4756 * @param p_header_size the size of the data contained in the POC marker.
4757 * @param p_manager the user event manager.
4759 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4760 OPJ_BYTE * p_header_data,
4761 OPJ_UINT32 p_header_size,
4762 opj_event_mgr_t * p_manager
4765 OPJ_UINT32 l_nb_comp;
4766 opj_image_t * l_image = 00;
4768 opj_cp_v2_t *l_cp = 00;
4769 opj_tcp_v2_t *l_tcp = 00;
4770 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4773 assert(p_header_data != 00);
4774 assert(p_j2k != 00);
4775 assert(p_manager != 00);
4777 l_image = p_j2k->m_private_image;
4778 l_nb_comp = l_image->numcomps;
4780 if (l_nb_comp <= 256) {
4785 if (p_header_size != 2 + l_comp_room) {
4786 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4790 l_cp = &(p_j2k->m_cp);
4791 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4792 &l_cp->tcps[p_j2k->m_current_tile_number] :
4793 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4795 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4796 p_header_data+=l_comp_room;
4797 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4801 if (l_cp->correct) {
4802 /* totlen is negative or larger than the bytes left!!! */
4803 if (l_comp_room >= l_nb_comp) {
4804 opj_event_msg_v2(p_manager, EVT_ERROR,
4805 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4806 l_comp_room, l_nb_comp);
4807 if (!JPWL_ASSUME || JPWL_ASSUME) {
4808 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4813 #endif /* USE_JPWL */
4815 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4822 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
4824 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4827 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
4834 * Updates the rates of the tcp.
4836 * @param p_stream the stream to write data to.
4837 * @param p_j2k J2K codec.
4838 * @param p_manager the user event manager.
4840 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4841 struct opj_stream_private *p_stream,
4842 struct opj_event_mgr * p_manager )
4844 opj_cp_v2_t * l_cp = 00;
4845 opj_image_t * l_image = 00;
4846 opj_tcp_v2_t * l_tcp = 00;
4847 opj_image_comp_t * l_img_comp = 00;
4850 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4851 OPJ_FLOAT32 * l_rates = 0;
4852 OPJ_FLOAT32 l_sot_remove;
4853 OPJ_UINT32 l_bits_empty, l_size_pixel;
4854 OPJ_UINT32 l_tile_size = 0;
4855 OPJ_UINT32 l_last_res;
4856 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4859 assert(p_j2k != 00);
4860 assert(p_manager != 00);
4861 assert(p_stream != 00);
4864 l_cp = &(p_j2k->m_cp);
4865 l_image = p_j2k->m_private_image;
4868 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4869 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4870 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4872 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4873 l_tp_stride_func = get_tp_stride;
4876 l_tp_stride_func = get_default_stride;
4879 for (i=0;i<l_cp->th;++i) {
4880 for (j=0;j<l_cp->tw;++j) {
4881 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4883 /* 4 borders of the tile rescale on the image if necessary */
4884 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4885 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4886 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4887 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4889 l_rates = l_tcp->rates;
4891 /* Modification of the RATE >> */
4893 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4895 ((*l_rates) * l_bits_empty)
4903 for (k = 1; k < l_tcp->numlayers; ++k) {
4905 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4907 ((*l_rates) * l_bits_empty)
4923 for (i=0;i<l_cp->th;++i) {
4924 for (j=0;j<l_cp->tw;++j) {
4925 l_rates = l_tcp->rates;
4928 *l_rates -= l_sot_remove;
4930 if (*l_rates < 30) {
4937 l_last_res = l_tcp->numlayers - 1;
4939 for (k = 1; k < l_last_res; ++k) {
4942 *l_rates -= l_sot_remove;
4944 if (*l_rates < *(l_rates - 1) + 10) {
4945 *l_rates = (*(l_rates - 1)) + 20;
4953 *l_rates -= (l_sot_remove + 2.f);
4955 if (*l_rates < *(l_rates - 1) + 10) {
4956 *l_rates = (*(l_rates - 1)) + 20;
4964 l_img_comp = l_image->comps;
4967 for (i=0;i<l_image->numcomps;++i) {
4968 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4970 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4978 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4980 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4982 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4983 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4984 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4985 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4989 if (l_cp->m_specific_param.m_enc.m_cinema) {
4990 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4991 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4992 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4996 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4997 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5003 static void j2k_read_eoc(opj_j2k_t *j2k) {
5005 opj_bool success = OPJ_FALSE;
5007 /* if packets should be decoded */
5008 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5009 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5010 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5011 for (i = 0; i < j2k->cp->tileno_size; i++) {
5012 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5013 if (j2k->cp->tileno[i] != -1)
5015 tileno = j2k->cp->tileno[i];
5016 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5017 opj_free(j2k->tile_data[tileno]);
5018 j2k->tile_data[tileno] = NULL;
5019 tcd_free_decode_tile(tcd, i);
5022 success = OPJ_FALSE;
5023 if (success == OPJ_FALSE) {
5024 j2k->state |= J2K_STATE_ERR;
5028 tcd_free_decode(tcd);
5031 /* if packets should not be decoded */
5033 for (i = 0; i < j2k->cp->tileno_size; i++) {
5034 tileno = j2k->cp->tileno[i];
5035 opj_free(j2k->tile_data[tileno]);
5036 j2k->tile_data[tileno] = NULL;
5039 if (j2k->state & J2K_STATE_ERR)
5040 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5042 j2k->state = J2K_STATE_MT;
5046 * Reads a EOC marker (End Of Codestream)
5048 * @param p_header_data the data contained in the SOD box.
5049 * @param p_j2k the jpeg2000 codec.
5050 * @param p_header_size the size of the data contained in the SOD marker.
5051 * @param p_manager the user event manager.
5054 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5055 struct opj_stream_private *p_stream,
5056 struct opj_event_mgr * p_manager )
5059 opj_tcd_v2_t * l_tcd = 00;
5060 OPJ_UINT32 l_nb_tiles;
5061 opj_tcp_v2_t * l_tcp = 00;
5065 assert(p_j2k != 00);
5066 assert(p_manager != 00);
5067 assert(p_stream != 00);
5069 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5070 l_tcp = p_j2k->m_cp.tcps;
5072 l_tcd = tcd_create_v2(OPJ_TRUE);
5074 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5078 for (i = 0; i < l_nb_tiles; ++i) {
5079 if (l_tcp->m_data) {
5080 if (! tcd_init_decode_tile(l_tcd, i)) {
5081 tcd_destroy_v2(l_tcd);
5082 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5086 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5090 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5095 opj_j2k_tcp_destroy(l_tcp);
5099 tcd_destroy_v2(l_tcd);
5105 * Gets the offset of the header.
5107 * @param p_stream the stream to write data to.
5108 * @param p_j2k J2K codec.
5109 * @param p_manager the user event manager.
5111 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5112 struct opj_stream_private *p_stream,
5113 struct opj_event_mgr * p_manager )
5116 assert(p_j2k != 00);
5117 assert(p_manager != 00);
5118 assert(p_stream != 00);
5120 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5126 * Writes the MCT marker (Multiple Component Transform)
5128 * @param p_stream the stream to write data to.
5129 * @param p_j2k J2K codec.
5130 * @param p_manager the user event manager.
5132 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5133 struct opj_stream_private *p_stream,
5134 struct opj_event_mgr * p_manager )
5137 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5138 opj_mct_data_t * l_mct_record;
5139 opj_tcp_v2_t * l_tcp;
5142 assert(p_j2k != 00);
5143 assert(p_stream != 00);
5144 assert(p_manager != 00);
5146 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5150 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5151 l_mct_record = l_tcp->m_mct_records;
5153 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5155 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5162 l_mcc_record = l_tcp->m_mcc_records;
5164 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5166 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5173 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5181 * Writes the image components.
5183 * @param p_stream the stream to write data to.
5184 * @param p_j2k J2K codec.
5185 * @param p_manager the user event manager.
5187 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5188 struct opj_stream_private *p_stream,
5189 struct opj_event_mgr * p_manager )
5194 assert(p_j2k != 00);
5195 assert(p_manager != 00);
5196 assert(p_stream != 00);
5198 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5200 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
5204 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
5213 * Writes regions of interests.
5215 * @param p_stream the stream to write data to.
5216 * @param p_j2k J2K codec.
5217 * @param p_manager the user event manager.
5219 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5220 struct opj_stream_private *p_stream,
5221 struct opj_event_mgr * p_manager )
5224 const opj_tccp_t *l_tccp = 00;
5227 assert(p_j2k != 00);
5228 assert(p_manager != 00);
5229 assert(p_stream != 00);
5231 l_tccp = p_j2k->m_cp.tcps->tccps;
5233 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5234 if (l_tccp->roishift) {
5236 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5250 * @param p_stream the stream to write data to.
5251 * @param p_j2k J2K codec.
5252 * @param p_manager the user event manager.
5254 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5255 struct opj_stream_private *p_stream,
5256 struct opj_event_mgr * p_manager )
5258 opj_codestream_index_t * l_cstr_index = 00;
5261 assert(p_j2k != 00);
5262 assert(p_manager != 00);
5263 assert(p_stream != 00);
5265 l_cstr_index = p_j2k->cstr_index;
5267 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5269 /* The following adjustment is done to adjust the codestream size */
5270 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5271 /* the first bunch of bytes is not in the codestream */
5272 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5277 /* preparation of JPWL marker segments */
5281 /* encode according to JPWL */
5282 jpwl_encode(p_j2k, p_stream, image);
5286 assert( 0 && "TODO" );
5287 #endif /* USE_JPWL */
5292 typedef struct opj_dec_mstabent {
5295 /** value of the state when the marker can appear */
5297 /** action linked to the marker */
5298 void (*handler) (opj_j2k_t *j2k);
5299 } opj_dec_mstabent_t;
5301 opj_dec_mstabent_t j2k_dec_mstab[] = {
5302 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5303 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5304 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5305 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5306 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5307 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5308 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5309 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5310 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5311 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5312 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5313 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5314 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5315 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5316 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5317 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5319 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5320 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5323 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5324 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5325 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5326 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5327 #endif /* USE_JPWL */
5329 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5330 {J2K_MS_INSEC, 0, j2k_read_insec},
5331 #endif /* USE_JPSEC */
5333 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5339 * Reads an unknown marker
5341 * @param p_stream the stream object to read from.
5342 * @param p_j2k the jpeg2000 codec.
5343 * @param p_manager the user event manager.
5345 * @return true if the marker could be deduced.
5347 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5348 opj_stream_private_t *p_stream,
5349 OPJ_UINT32 *output_marker,
5350 opj_event_mgr_t * p_manager
5353 OPJ_UINT32 l_unknown_marker;
5354 const opj_dec_memory_marker_handler_t * l_marker_handler;
5355 OPJ_UINT32 l_size_unk = 2;
5358 assert(p_j2k != 00);
5359 assert(p_manager != 00);
5360 assert(p_stream != 00);
5362 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5365 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5366 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5367 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5371 /* read 2 bytes as the new marker ID*/
5372 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5374 if (!(l_unknown_marker < 0xff00)) {
5376 /* Get the marker handler from the marker ID*/
5377 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5379 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5380 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5384 if (l_marker_handler->id != J2K_MS_UNK) {
5385 /* Add the marker to the codestream index*/
5386 if (l_marker_handler->id != J2K_MS_SOT)
5387 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5388 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5390 break; /* next marker is known and well located */
5398 *output_marker = l_marker_handler->id ;
5404 Read the lookup table containing all the marker, status and action
5405 @param id Marker value
5407 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5408 opj_dec_mstabent_t *e;
5409 for (e = j2k_dec_mstab; e->id != 0; e++) {
5418 * Writes the MCT marker (Multiple Component Transform)
5420 * @param p_stream the stream to write data to.
5421 * @param p_j2k J2K codec.
5422 * @param p_manager the user event manager.
5424 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5425 opj_mct_data_t * p_mct_record,
5426 struct opj_stream_private *p_stream,
5427 struct opj_event_mgr * p_manager )
5429 OPJ_UINT32 l_mct_size;
5430 OPJ_BYTE * l_current_data = 00;
5434 assert(p_j2k != 00);
5435 assert(p_manager != 00);
5436 assert(p_stream != 00);
5438 l_mct_size = 10 + p_mct_record->m_data_size;
5440 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5441 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5442 = (OPJ_BYTE*)opj_realloc(
5443 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5446 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5450 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5453 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5455 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5456 l_current_data += 2;
5458 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5459 l_current_data += 2;
5461 opj_write_bytes(l_current_data,0,2); /* Zmct */
5462 l_current_data += 2;
5464 /* only one marker atm */
5465 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5467 opj_write_bytes(l_current_data,l_tmp,2);
5468 l_current_data += 2;
5470 opj_write_bytes(l_current_data,0,2); /* Ymct */
5473 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5475 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) {
5483 * Reads a MCT marker (Multiple Component Transform)
5485 * @param p_header_data the data contained in the MCT box.
5486 * @param p_j2k the jpeg2000 codec.
5487 * @param p_header_size the size of the data contained in the MCT marker.
5488 * @param p_manager the user event manager.
5490 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5491 OPJ_BYTE * p_header_data,
5492 OPJ_UINT32 p_header_size,
5493 opj_event_mgr_t * p_manager
5497 opj_tcp_v2_t *l_tcp = 00;
5500 opj_mct_data_t * l_mct_data;
5503 assert(p_header_data != 00);
5504 assert(p_j2k != 00);
5506 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5507 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5508 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5510 if (p_header_size < 2) {
5511 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5516 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5519 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5523 if(p_header_size <= 6) {
5524 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5528 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5529 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5532 l_indix = l_tmp & 0xff;
5533 l_mct_data = l_tcp->m_mct_records;
5535 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5536 if (l_mct_data->m_index == l_indix) {
5543 if (i == l_tcp->m_nb_mct_records) {
5544 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5545 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5547 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));
5548 if(! l_tcp->m_mct_records) {
5549 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5553 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5554 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5557 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5560 if (l_mct_data->m_data) {
5561 opj_free(l_mct_data->m_data);
5562 l_mct_data->m_data = 00;
5565 l_mct_data->m_index = l_indix;
5566 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5567 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5569 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5572 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5578 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5579 if (! l_mct_data->m_data) {
5580 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5583 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5585 l_mct_data->m_data_size = p_header_size;
5586 ++l_tcp->m_nb_mct_records;
5592 * Writes the MCC marker (Multiple Component Collection)
5594 * @param p_stream the stream to write data to.
5595 * @param p_j2k J2K codec.
5596 * @param p_manager the user event manager.
5598 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5599 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5600 struct opj_stream_private *p_stream,
5601 struct opj_event_mgr * p_manager )
5604 OPJ_UINT32 l_mcc_size;
5605 OPJ_BYTE * l_current_data = 00;
5606 OPJ_UINT32 l_nb_bytes_for_comp;
5611 assert(p_j2k != 00);
5612 assert(p_manager != 00);
5613 assert(p_stream != 00);
5615 if (p_mcc_record->m_nb_comps > 255 ) {
5616 l_nb_bytes_for_comp = 2;
5620 l_nb_bytes_for_comp = 1;
5624 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5625 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5627 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5628 = (OPJ_BYTE*)opj_realloc(
5629 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5631 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5635 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5638 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5640 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5641 l_current_data += 2;
5643 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5644 l_current_data += 2;
5647 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5648 l_current_data += 2;
5650 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5653 /* only one marker atm */
5654 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5657 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5660 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5663 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 */
5666 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5667 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5668 l_current_data+=l_nb_bytes_for_comp;
5671 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 */
5674 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5676 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5677 l_current_data+=l_nb_bytes_for_comp;
5680 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5682 if (p_mcc_record->m_decorrelation_array) {
5683 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5686 if (p_mcc_record->m_offset_array) {
5687 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5690 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5693 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) {
5701 * Reads a MCC marker (Multiple Component Collection)
5703 * @param p_header_data the data contained in the MCC box.
5704 * @param p_j2k the jpeg2000 codec.
5705 * @param p_header_size the size of the data contained in the MCC marker.
5706 * @param p_manager the user event manager.
5708 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5709 OPJ_BYTE * p_header_data,
5710 OPJ_UINT32 p_header_size,
5711 opj_event_mgr_t * p_manager )
5716 opj_tcp_v2_t * l_tcp;
5717 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5718 opj_mct_data_t * l_mct_data;
5719 OPJ_UINT32 l_nb_collections;
5720 OPJ_UINT32 l_nb_comps;
5721 OPJ_UINT32 l_nb_bytes_by_comp;
5725 assert(p_header_data != 00);
5726 assert(p_j2k != 00);
5727 assert(p_manager != 00);
5729 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5730 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5731 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5733 if (p_header_size < 2) {
5734 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5739 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5742 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5746 if (p_header_size < 7) {
5747 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5751 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5754 l_mcc_record = l_tcp->m_mcc_records;
5756 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5757 if (l_mcc_record->m_index == l_indix) {
5764 if (i == l_tcp->m_nb_mcc_records) {
5765 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5766 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5768 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5769 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5770 if (! l_tcp->m_mcc_records) {
5771 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5774 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5775 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));
5777 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5779 l_mcc_record->m_index = l_indix;
5781 /* only one marker atm */
5782 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5785 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5789 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5792 if (l_nb_collections > 1) {
5793 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5799 for (i=0;i<l_nb_collections;++i) {
5800 if (p_header_size < 3) {
5801 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5805 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5809 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5813 opj_read_bytes(p_header_data,&l_nb_comps,2);
5818 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5819 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5821 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5822 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5826 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5828 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5829 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5830 p_header_data+=l_nb_bytes_by_comp;
5833 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5838 opj_read_bytes(p_header_data,&l_nb_comps,2);
5841 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5842 l_nb_comps &= 0x7fff;
5844 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5845 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5849 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5850 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5854 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5856 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5857 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5858 p_header_data+=l_nb_bytes_by_comp;
5861 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5866 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5869 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5870 l_mcc_record->m_decorrelation_array = 00;
5871 l_mcc_record->m_offset_array = 00;
5873 l_indix = l_tmp & 0xff;
5875 l_mct_data = l_tcp->m_mct_records;
5876 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5877 if (l_mct_data->m_index == l_indix) {
5878 l_mcc_record->m_decorrelation_array = l_mct_data;
5884 if (l_mcc_record->m_decorrelation_array == 00) {
5885 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5890 l_indix = (l_tmp >> 8) & 0xff;
5892 l_mct_data = l_tcp->m_mct_records;
5893 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5894 if (l_mct_data->m_index == l_indix) {
5895 l_mcc_record->m_offset_array = l_mct_data;
5901 if (l_mcc_record->m_offset_array == 00) {
5902 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5908 if (p_header_size != 0) {
5909 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5913 ++l_tcp->m_nb_mcc_records;
5920 * Writes the MCO marker (Multiple component transformation ordering)
5922 * @param p_stream the stream to write data to.
5923 * @param p_j2k J2K codec.
5924 * @param p_manager the user event manager.
5926 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
5927 struct opj_stream_private *p_stream,
5928 struct opj_event_mgr * p_manager
5931 OPJ_BYTE * l_current_data = 00;
5932 OPJ_UINT32 l_mco_size;
5933 opj_tcp_v2_t * l_tcp = 00;
5934 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5938 assert(p_j2k != 00);
5939 assert(p_manager != 00);
5940 assert(p_stream != 00);
5942 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5943 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5945 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5946 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5948 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5949 = (OPJ_BYTE*)opj_realloc(
5950 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5952 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5957 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5960 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5961 l_current_data += 2;
5963 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5964 l_current_data += 2;
5966 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5969 l_mcc_record = l_tcp->m_mcc_records;
5970 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5971 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
5977 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) {
5985 * Reads a MCO marker (Multiple Component Transform Ordering)
5987 * @param p_header_data the data contained in the MCO box.
5988 * @param p_j2k the jpeg2000 codec.
5989 * @param p_header_size the size of the data contained in the MCO marker.
5990 * @param p_manager the user event manager.
5992 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5993 OPJ_BYTE * p_header_data,
5994 OPJ_UINT32 p_header_size,
5995 opj_event_mgr_t * p_manager
5998 OPJ_UINT32 l_tmp, i;
5999 OPJ_UINT32 l_nb_stages;
6000 opj_tcp_v2_t * l_tcp;
6001 opj_tccp_t * l_tccp;
6002 opj_image_t * l_image;
6003 opj_image_comp_t * l_img_comp;
6006 assert(p_header_data != 00);
6007 assert(p_j2k != 00);
6008 assert(p_manager != 00);
6010 l_image = p_j2k->m_private_image;
6011 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6012 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6013 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6015 if (p_header_size < 1) {
6016 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6020 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6023 if (l_nb_stages > 1) {
6024 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6028 if (p_header_size != l_nb_stages + 1) {
6029 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6033 l_tccp = l_tcp->tccps;
6034 l_img_comp = l_image->comps;
6036 for (i=0;i<l_image->numcomps;++i) {
6037 l_tccp->m_dc_level_shift = 0;
6041 if (l_tcp->m_mct_decoding_matrix) {
6042 opj_free(l_tcp->m_mct_decoding_matrix);
6043 l_tcp->m_mct_decoding_matrix = 00;
6046 for (i=0;i<l_nb_stages;++i) {
6047 opj_read_bytes(p_header_data,&l_tmp,1);
6050 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6058 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6061 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6062 opj_mct_data_t * l_deco_array, * l_offset_array;
6063 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6064 OPJ_UINT32 l_nb_elem;
6065 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6066 opj_tccp_t * l_tccp;
6069 assert(p_tcp != 00);
6071 l_mcc_record = p_tcp->m_mcc_records;
6073 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6074 if (l_mcc_record->m_index == p_index) {
6079 if (i==p_tcp->m_nb_mcc_records) {
6080 /** element discarded **/
6084 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6085 /** do not support number of comps != image */
6089 l_deco_array = l_mcc_record->m_decorrelation_array;
6092 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6093 if (l_deco_array->m_data_size != l_data_size) {
6097 l_nb_elem = p_image->numcomps * p_image->numcomps;
6098 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6099 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6101 if (! p_tcp->m_mct_decoding_matrix ) {
6105 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);
6108 l_offset_array = l_mcc_record->m_offset_array;
6110 if (l_offset_array) {
6111 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6112 if (l_offset_array->m_data_size != l_data_size) {
6116 l_nb_elem = p_image->numcomps;
6117 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6118 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6120 if (! l_offset_data ) {
6124 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6126 l_tccp = p_tcp->tccps;
6127 l_current_offset_data = l_offset_data;
6129 for (i=0;i<p_image->numcomps;++i) {
6130 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6134 opj_free(l_offset_data);
6141 * Writes the CBD marker (Component bit depth definition)
6143 * @param p_stream the stream to write data to.
6144 * @param p_j2k J2K codec.
6145 * @param p_manager the user event manager.
6147 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6148 struct opj_stream_private *p_stream,
6149 struct opj_event_mgr * p_manager )
6152 OPJ_UINT32 l_cbd_size;
6153 OPJ_BYTE * l_current_data = 00;
6154 opj_image_t *l_image = 00;
6155 opj_image_comp_t * l_comp = 00;
6158 assert(p_j2k != 00);
6159 assert(p_manager != 00);
6160 assert(p_stream != 00);
6162 l_image = p_j2k->m_private_image;
6163 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6165 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6166 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6167 = (OPJ_BYTE*)opj_realloc(
6168 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6171 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6175 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6178 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6180 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6181 l_current_data += 2;
6183 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6184 l_current_data += 2;
6186 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6189 l_comp = l_image->comps;
6191 for (i=0;i<l_image->numcomps;++i) {
6192 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6198 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) {
6206 * Reads a CBD marker (Component bit depth definition)
6207 * @param p_header_data the data contained in the CBD box.
6208 * @param p_j2k the jpeg2000 codec.
6209 * @param p_header_size the size of the data contained in the CBD marker.
6210 * @param p_manager the user event manager.
6212 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6213 OPJ_BYTE * p_header_data,
6214 OPJ_UINT32 p_header_size,
6215 opj_event_mgr_t * p_manager
6218 OPJ_UINT32 l_nb_comp,l_num_comp;
6219 OPJ_UINT32 l_comp_def;
6221 opj_image_comp_t * l_comp = 00;
6224 assert(p_header_data != 00);
6225 assert(p_j2k != 00);
6226 assert(p_manager != 00);
6228 l_num_comp = p_j2k->m_private_image->numcomps;
6230 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6231 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6235 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6238 if (l_nb_comp != l_num_comp) {
6239 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6243 l_comp = p_j2k->m_private_image->comps;
6244 for (i=0;i<l_num_comp;++i) {
6245 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6247 l_comp->sgnd = (l_comp_def>>7) & 1;
6248 l_comp->prec = (l_comp_def&0x7f) + 1;
6256 /* ----------------------------------------------------------------------- */
6257 /* J2K / JPT decoder interface */
6258 /* ----------------------------------------------------------------------- */
6260 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6262 if(j2k && parameters) {
6263 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6264 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6267 j2k->m_cp.correct = parameters->jpwl_correct;
6268 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6269 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6270 #endif /* USE_JPWL */
6275 /* ----------------------------------------------------------------------- */
6276 /* J2K encoder interface */
6277 /* ----------------------------------------------------------------------- */
6279 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6280 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6287 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6289 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6294 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6296 l_j2k->m_is_decoder = 0;
6297 l_j2k->m_cp.m_is_decoder = 0;
6299 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6300 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6301 opj_j2k_destroy(l_j2k);
6305 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6307 /* validation list creation*/
6308 l_j2k->m_validation_list = opj_procedure_list_create();
6309 if (! l_j2k->m_validation_list) {
6310 opj_j2k_destroy(l_j2k);
6314 /* execution list creation*/
6315 l_j2k->m_procedure_list = opj_procedure_list_create();
6316 if (! l_j2k->m_procedure_list) {
6317 opj_j2k_destroy(l_j2k);
6324 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6326 int tileno, numpocs_tile;
6327 opj_cp_t *cp = NULL;
6329 if(!j2k || !parameters || ! image) {
6333 /* create and initialize the coding parameters structure */
6334 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6336 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6339 /* set default values for cp */
6344 copy user encoding parameters
6346 cp->cinema = parameters->cp_cinema;
6347 cp->max_comp_size = parameters->max_comp_size;
6348 cp->rsiz = parameters->cp_rsiz;
6349 cp->disto_alloc = parameters->cp_disto_alloc;
6350 cp->fixed_alloc = parameters->cp_fixed_alloc;
6351 cp->fixed_quality = parameters->cp_fixed_quality;
6353 /* mod fixed_quality */
6354 if(parameters->cp_matrice) {
6355 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6356 cp->matrice = (int *) opj_malloc(array_size);
6357 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6361 cp->tdx = parameters->cp_tdx;
6362 cp->tdy = parameters->cp_tdy;
6365 cp->tx0 = parameters->cp_tx0;
6366 cp->ty0 = parameters->cp_ty0;
6368 /* comment string */
6369 if(parameters->cp_comment) {
6370 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6372 strcpy(cp->comment, parameters->cp_comment);
6377 calculate other encoding parameters
6380 if (parameters->tile_size_on) {
6381 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6382 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6384 cp->tdx = image->x1 - cp->tx0;
6385 cp->tdy = image->y1 - cp->ty0;
6388 if(parameters->tp_on){
6389 cp->tp_flag = parameters->tp_flag;
6394 for(i=0;i<image->numcomps ;i++){
6395 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6401 calculate JPWL encoding parameters
6404 if (parameters->jpwl_epc_on) {
6408 cp->epc_on = OPJ_TRUE;
6409 cp->info_on = OPJ_FALSE; /* no informative technique */
6412 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6413 cp->epb_on = OPJ_TRUE;
6415 cp->hprot_MH = parameters->jpwl_hprot_MH;
6416 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6417 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6418 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6420 /* if tile specs are not specified, copy MH specs */
6421 if (cp->hprot_TPH[0] == -1) {
6422 cp->hprot_TPH_tileno[0] = 0;
6423 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6425 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6426 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6427 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6428 cp->pprot[i] = parameters->jpwl_pprot[i];
6432 /* set ESD writing */
6433 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6434 cp->esd_on = OPJ_TRUE;
6436 cp->sens_size = parameters->jpwl_sens_size;
6437 cp->sens_addr = parameters->jpwl_sens_addr;
6438 cp->sens_range = parameters->jpwl_sens_range;
6440 cp->sens_MH = parameters->jpwl_sens_MH;
6441 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6442 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6443 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6447 /* always set RED writing to false: we are at the encoder */
6448 cp->red_on = OPJ_FALSE;
6451 cp->epc_on = OPJ_FALSE;
6453 #endif /* USE_JPWL */
6456 /* initialize the mutiple tiles */
6457 /* ---------------------------- */
6458 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6460 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6461 opj_tcp_t *tcp = &cp->tcps[tileno];
6462 tcp->numlayers = parameters->tcp_numlayers;
6463 assert ( tcp->numlayers >= 0 );
6464 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6466 if (cp->fixed_quality) {
6467 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6469 tcp->rates[j] = parameters->tcp_rates[j];
6471 if (cp->fixed_quality) { /* add fixed_quality */
6472 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6474 tcp->rates[j] = parameters->tcp_rates[j];
6478 tcp->csty = parameters->csty;
6479 tcp->prg = parameters->prog_order;
6480 tcp->mct = parameters->tcp_mct;
6484 if (parameters->numpocs) {
6485 /* initialisation of POC */
6487 assert( parameters->numpocs >= 0 );
6488 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6489 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6490 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6491 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6492 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6493 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6494 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6495 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6496 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6497 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6501 tcp->numpocs = numpocs_tile -1 ;
6506 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6508 for (i = 0; i < image->numcomps; i++) {
6509 opj_tccp_t *tccp = &tcp->tccps[i];
6510 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6511 tccp->numresolutions = parameters->numresolution;
6512 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6513 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6514 tccp->cblksty = parameters->mode;
6515 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6516 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6518 assert(parameters->roi_compno >= 0);
6519 if (i == (OPJ_UINT32)parameters->roi_compno) {
6520 tccp->roishift = parameters->roi_shift;
6525 if(parameters->cp_cinema)
6527 /*Precinct size for lowest frequency subband=128*/
6530 /*Precinct size at all other resolutions = 256*/
6531 for (j = 1; j < tccp->numresolutions; j++) {
6536 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6538 assert(tccp->numresolutions > 0);
6539 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6540 if (p < parameters->res_spec) {
6542 if (parameters->prcw_init[p] < 1) {
6545 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6548 if (parameters->prch_init[p] < 1) {
6551 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6555 int res_spec = parameters->res_spec;
6556 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6557 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6559 if (size_prcw < 1) {
6562 tccp->prcw[j] = int_floorlog2(size_prcw);
6565 if (size_prch < 1) {
6568 tccp->prch[j] = int_floorlog2(size_prch);
6572 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6575 for (j = 0; j < tccp->numresolutions; j++) {
6582 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6587 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6588 opj_cparameters_t *parameters,
6590 struct opj_event_mgr * p_manager)
6592 OPJ_UINT32 i, j, tileno, numpocs_tile;
6593 opj_cp_v2_t *cp = 00;
6596 if(!p_j2k || !parameters || ! image) {
6600 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6601 cp = &(p_j2k->m_cp);
6603 /* set default values for cp */
6608 copy user encoding parameters
6610 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6611 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6612 cp->rsiz = parameters->cp_rsiz;
6613 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6614 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6615 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6617 /* mod fixed_quality */
6618 if (parameters->cp_matrice) {
6619 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6620 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6621 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6625 cp->tdx = parameters->cp_tdx;
6626 cp->tdy = parameters->cp_tdy;
6629 cp->tx0 = parameters->cp_tx0;
6630 cp->ty0 = parameters->cp_ty0;
6632 /* comment string */
6633 if(parameters->cp_comment) {
6634 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6636 strcpy(cp->comment, parameters->cp_comment);
6641 calculate other encoding parameters
6644 if (parameters->tile_size_on) {
6645 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6646 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6648 cp->tdx = image->x1 - cp->tx0;
6649 cp->tdy = image->y1 - cp->ty0;
6652 if (parameters->tp_on) {
6653 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6654 cp->m_specific_param.m_enc.m_tp_on = 1;
6659 calculate JPWL encoding parameters
6662 if (parameters->jpwl_epc_on) {
6666 cp->epc_on = OPJ_TRUE;
6667 cp->info_on = OPJ_FALSE; /* no informative technique */
6670 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6671 cp->epb_on = OPJ_TRUE;
6673 cp->hprot_MH = parameters->jpwl_hprot_MH;
6674 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6675 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6676 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6678 /* if tile specs are not specified, copy MH specs */
6679 if (cp->hprot_TPH[0] == -1) {
6680 cp->hprot_TPH_tileno[0] = 0;
6681 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6683 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6684 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6685 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6686 cp->pprot[i] = parameters->jpwl_pprot[i];
6690 /* set ESD writing */
6691 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6692 cp->esd_on = OPJ_TRUE;
6694 cp->sens_size = parameters->jpwl_sens_size;
6695 cp->sens_addr = parameters->jpwl_sens_addr;
6696 cp->sens_range = parameters->jpwl_sens_range;
6698 cp->sens_MH = parameters->jpwl_sens_MH;
6699 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6700 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6701 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6705 /* always set RED writing to false: we are at the encoder */
6706 cp->red_on = OPJ_FALSE;
6709 cp->epc_on = OPJ_FALSE;
6711 #endif /* USE_JPWL */
6714 /* initialize the mutiple tiles */
6715 /* ---------------------------- */
6716 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6717 if (parameters->numpocs) {
6718 /* initialisation of POC */
6719 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6723 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6724 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6725 tcp->numlayers = parameters->tcp_numlayers;
6727 for (j = 0; j < tcp->numlayers; j++) {
6728 if(cp->m_specific_param.m_enc.m_cinema){
6729 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6730 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6732 tcp->rates[j] = parameters->tcp_rates[j];
6734 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6735 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6737 tcp->rates[j] = parameters->tcp_rates[j];
6742 tcp->csty = parameters->csty;
6743 tcp->prg = parameters->prog_order;
6744 tcp->mct = parameters->tcp_mct;
6749 if (parameters->numpocs) {
6750 /* initialisation of POC */
6753 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6754 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6755 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6757 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6758 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6759 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6760 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6761 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6762 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6763 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6769 tcp->numpocs = numpocs_tile -1 ;
6774 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6776 if (parameters->mct_data) {
6778 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6781 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6782 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6783 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6784 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6787 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6788 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6789 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6791 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6792 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6794 tcp->mct_norms = (OPJ_FLOAT64*)
6795 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6797 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6800 for (i = 0; i < image->numcomps; i++) {
6801 opj_tccp_t *tccp = &tcp->tccps[i];
6802 tccp->m_dc_level_shift = l_dc_shift[i];
6805 opj_j2k_setup_mct_encoding(tcp,image);
6809 for (i = 0; i < image->numcomps; i++) {
6810 opj_tccp_t *tccp = &tcp->tccps[i];
6811 opj_image_comp_t * l_comp = &(image->comps[i]);
6813 if (! l_comp->sgnd) {
6814 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6819 for (i = 0; i < image->numcomps; i++) {
6820 opj_tccp_t *tccp = &tcp->tccps[i];
6822 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6823 tccp->numresolutions = parameters->numresolution;
6824 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6825 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6826 tccp->cblksty = parameters->mode;
6827 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6828 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6831 if (i == parameters->roi_compno) {
6832 tccp->roishift = parameters->roi_shift;
6837 if(parameters->cp_cinema) {
6838 //Precinct size for lowest frequency subband=128
6841 //Precinct size at all other resolutions = 256
6842 for (j = 1; j < tccp->numresolutions; j++) {
6847 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6848 OPJ_INT32 p = 0, it_res;
6849 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6850 if (p < parameters->res_spec) {
6852 if (parameters->prcw_init[p] < 1) {
6853 tccp->prcw[it_res] = 1;
6855 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6858 if (parameters->prch_init[p] < 1) {
6859 tccp->prch[it_res] = 1;
6861 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6865 int res_spec = parameters->res_spec;
6866 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6867 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6869 if (size_prcw < 1) {
6870 tccp->prcw[it_res] = 1;
6872 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6875 if (size_prch < 1) {
6876 tccp->prch[it_res] = 1;
6878 tccp->prch[it_res] = int_floorlog2(size_prch);
6882 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6885 for (j = 0; j < tccp->numresolutions; j++) {
6892 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6896 if (parameters->mct_data) {
6897 opj_free(parameters->mct_data);
6898 parameters->mct_data = 00;
6905 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6907 assert(cstr_info != 00);
6909 /* expand the list? */
6910 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
6911 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
6912 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
6915 /* add the marker */
6916 cstr_info->marker[cstr_info->marknum].type = type;
6917 cstr_info->marker[cstr_info->marknum].pos = pos;
6918 cstr_info->marker[cstr_info->marknum].len = len;
6919 cstr_info->marknum++;
6923 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6925 assert(cstr_index != 00);
6927 /* expand the list? */
6928 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6929 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6930 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6933 /* add the marker */
6934 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6935 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6936 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6937 cstr_index->marknum++;
6941 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6943 opj_marker_info_t *marker;
6945 assert(cstr_info != 00);
6947 /* expand the list? */
6948 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
6949 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
6950 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
6953 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
6955 /* add the marker */
6956 marker->type = type;
6959 cstr_info->tile[tileno].marknum++;
6962 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)
6964 assert(cstr_index != 00);
6965 assert(cstr_index->tile_index != 00);
6967 /* expand the list? */
6968 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6969 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6970 cstr_index->tile_index[tileno].marker =
6971 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
6972 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6975 /* add the marker */
6976 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6977 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6978 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6979 cstr_index->tile_index[tileno].marknum++;
6981 if (type == J2K_MS_SOT) {
6982 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6984 if (cstr_index->tile_index[tileno].tp_index)
6985 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6992 * -----------------------------------------------------------------------
6993 * -----------------------------------------------------------------------
6994 * -----------------------------------------------------------------------
6998 * Ends the decompression procedures and possibiliy add data to be read after the
7001 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
7002 opj_stream_private_t *p_stream,
7003 opj_event_mgr_t * p_manager
7013 * Reads a jpeg2000 codestream header structure.
7016 * @param p_stream the stream to read data from.
7017 * @param p_j2k the jpeg2000 codec.
7018 * @param p_manager the user event manager.
7020 * @return true if the box is valid.
7022 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
7023 opj_j2k_v2_t* p_j2k,
7024 opj_image_t** p_image,
7025 opj_event_mgr_t* p_manager )
7028 assert(p_j2k != 00);
7029 assert(p_stream != 00);
7030 assert(p_manager != 00);
7032 /* create an empty image header */
7033 p_j2k->m_private_image = opj_image_create0();
7034 if (! p_j2k->m_private_image) {
7038 /* customization of the validation */
7039 opj_j2k_setup_decoding_validation(p_j2k);
7041 /* validation of the parameters codec */
7042 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7043 opj_image_destroy(p_j2k->m_private_image);
7044 p_j2k->m_private_image = NULL;
7048 /* customization of the encoding */
7049 opj_j2k_setup_header_reading(p_j2k);
7052 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7053 opj_image_destroy(p_j2k->m_private_image);
7054 p_j2k->m_private_image = NULL;
7058 *p_image = opj_image_create0();
7063 /* Copy codestream image information to the output image */
7064 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7066 /*Allocate and initialize some elements of codestrem index*/
7067 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
7075 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7077 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7080 assert(p_j2k != 00);
7082 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
7084 /* DEVELOPER CORNER, add your custom procedures */
7085 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
7090 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7091 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7093 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7096 assert(p_j2k != 00);
7098 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
7099 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
7100 /* DEVELOPER CORNER, add your custom validation procedure */
7106 * The mct encoding validation procedure.
7108 * @param p_j2k the jpeg2000 codec to validate.
7109 * @param p_stream the input stream to validate.
7110 * @param p_manager the user event manager.
7112 * @return true if the parameters are correct.
7114 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7115 opj_stream_private_t *p_stream,
7116 opj_event_mgr_t * p_manager )
7118 opj_bool l_is_valid = OPJ_TRUE;
7122 assert(p_j2k != 00);
7123 assert(p_stream != 00);
7124 assert(p_manager != 00);
7126 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7127 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7128 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7130 for (i=0;i<l_nb_tiles;++i) {
7131 if (l_tcp->mct == 2) {
7132 opj_tccp_t * l_tccp = l_tcp->tccps;
7133 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7135 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7136 l_is_valid &= ! (l_tccp->qmfbid & 1);
7147 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7150 OPJ_UINT32 l_indix = 1;
7151 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7152 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7153 OPJ_UINT32 l_mct_size,l_nb_elem;
7154 OPJ_FLOAT32 * l_data, * l_current_data;
7155 opj_tccp_t * l_tccp;
7158 assert(p_tcp != 00);
7160 if (p_tcp->mct != 2) {
7164 if (p_tcp->m_mct_decoding_matrix) {
7165 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7166 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7168 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));
7169 if (! p_tcp->m_mct_records) {
7172 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7174 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7176 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7178 if (l_mct_deco_data->m_data) {
7179 opj_free(l_mct_deco_data->m_data);
7180 l_mct_deco_data->m_data = 00;
7183 l_mct_deco_data->m_index = l_indix++;
7184 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7185 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7186 l_nb_elem = p_image->numcomps * p_image->numcomps;
7187 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7188 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7190 if (! l_mct_deco_data->m_data) {
7194 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);
7196 l_mct_deco_data->m_data_size = l_mct_size;
7197 ++p_tcp->m_nb_mct_records;
7200 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7201 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7202 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));
7204 if (! p_tcp->m_mct_records) {
7208 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7209 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7211 if (l_mct_deco_data) {
7212 l_mct_deco_data = l_mct_offset_data - 1;
7216 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7218 if (l_mct_offset_data->m_data) {
7219 opj_free(l_mct_offset_data->m_data);
7220 l_mct_offset_data->m_data = 00;
7223 l_mct_offset_data->m_index = l_indix++;
7224 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7225 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7226 l_nb_elem = p_image->numcomps;
7227 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7228 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7230 if (! l_mct_offset_data->m_data) {
7234 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7236 opj_free(l_mct_offset_data->m_data);
7237 l_mct_offset_data->m_data = 00;
7241 l_tccp = p_tcp->tccps;
7242 l_current_data = l_data;
7244 for (i=0;i<l_nb_elem;++i) {
7245 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7249 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7253 l_mct_offset_data->m_data_size = l_mct_size;
7255 ++p_tcp->m_nb_mct_records;
7257 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7258 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7259 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7260 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7262 if (! p_tcp->m_mcc_records) {
7265 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7266 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));
7270 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7271 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7272 l_mcc_data->m_is_irreversible = 1;
7273 l_mcc_data->m_nb_comps = p_image->numcomps;
7274 l_mcc_data->m_index = l_indix++;
7275 l_mcc_data->m_offset_array = l_mct_offset_data;
7276 ++p_tcp->m_nb_mcc_records;
7282 * Builds the cp decoder parameters to use to decode tile.
7284 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7285 opj_stream_private_t *p_stream,
7286 opj_event_mgr_t * p_manager )
7288 /* add here initialization of cp
7289 copy paste of setup_decoder */
7297 * Builds the cp encoder parameters to use to encode tile.
7299 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7300 opj_stream_private_t *p_stream,
7301 opj_event_mgr_t * p_manager )
7303 /* add here initialization of cp
7304 copy paste of setup_encoder */
7312 * The default encoding validation procedure without any extension.
7314 * @param p_j2k the jpeg2000 codec to validate.
7315 * @param p_stream the input stream to validate.
7316 * @param p_manager the user event manager.
7318 * @return true if the parameters are correct.
7320 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7321 opj_stream_private_t *p_stream,
7322 opj_event_mgr_t * p_manager )
7324 opj_bool l_is_valid = OPJ_TRUE;
7327 assert(p_j2k != 00);
7328 assert(p_stream != 00);
7329 assert(p_manager != 00);
7331 /* STATE checking */
7332 /* make sure the state is at 0 */
7333 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7335 /* POINTER validation */
7336 /* make sure a p_j2k codec is present */
7337 l_is_valid &= (p_j2k->m_procedure_list != 00);
7338 /* make sure a validation list is present */
7339 l_is_valid &= (p_j2k->m_validation_list != 00);
7341 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7342 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7346 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7347 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7351 /* PARAMETER VALIDATION */
7356 * The default decoding validation procedure without any extension.
7358 * @param p_j2k the jpeg2000 codec to validate.
7359 * @param p_stream the input stream to validate.
7360 * @param p_manager the user event manager.
7362 * @return true if the parameters are correct.
7364 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7365 opj_stream_private_t *p_stream,
7366 opj_event_mgr_t * p_manager
7369 opj_bool l_is_valid = OPJ_TRUE;
7372 assert(p_j2k != 00);
7373 assert(p_stream != 00);
7374 assert(p_manager != 00);
7377 /* STATE checking */
7378 /* make sure the state is at 0 */
7380 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7382 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7384 /* POINTER validation */
7385 /* make sure a p_j2k codec is present */
7386 /* make sure a procedure list is present */
7387 l_is_valid &= (p_j2k->m_procedure_list != 00);
7388 /* make sure a validation list is present */
7389 l_is_valid &= (p_j2k->m_validation_list != 00);
7391 /* PARAMETER VALIDATION */
7395 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7396 opj_stream_private_t *p_stream,
7397 opj_event_mgr_t * p_manager)
7399 OPJ_UINT32 l_current_marker;
7400 OPJ_UINT32 l_marker_size;
7401 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7404 assert(p_stream != 00);
7405 assert(p_j2k != 00);
7406 assert(p_manager != 00);
7408 /* We enter in the main header */
7409 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7411 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7412 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7413 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7417 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7418 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7419 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7423 /* Read 2 bytes as the new marker ID */
7424 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7426 /* Try to read until the SOT is detected */
7427 while (l_current_marker != J2K_MS_SOT) {
7429 /* Check if the current marker ID is valid */
7430 if (l_current_marker < 0xff00) {
7431 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7435 /* Get the marker handler from the marker ID */
7436 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7438 /* Manage case where marker is unknown */
7439 if (l_marker_handler->id == J2K_MS_UNK) {
7440 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7441 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7445 if (l_current_marker == J2K_MS_SOT)
7446 break; /* SOT marker is detected main header is completely read */
7447 else /* Get the marker handler from the marker ID */
7448 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7451 /* Check if the marker is known and if it is the right place to find it */
7452 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7453 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7457 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7458 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7459 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7463 /* read 2 bytes as the marker size */
7464 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7465 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7467 /* Check if the marker size is compatible with the header data size */
7468 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7469 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7470 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7471 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7474 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7477 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7478 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) {
7479 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7483 /* Read the marker segment with the correct marker handler */
7484 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7485 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7489 /* Add the marker to the codestream index*/
7490 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7491 l_marker_handler->id,
7492 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7493 l_marker_size + 4 );
7495 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7496 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7497 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7501 /* read 2 bytes as the new marker ID */
7502 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7505 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7507 /* Position of the last element if the main header */
7508 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7510 /* Next step: read a tile-part header */
7511 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7517 * Excutes the given procedures on the given codec.
7519 * @param p_procedure_list the list of procedures to execute
7520 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7521 * @param p_stream the stream to execute the procedures on.
7522 * @param p_manager the user manager.
7524 * @return true if all the procedures were successfully executed.
7526 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7527 opj_procedure_list_t * p_procedure_list,
7528 opj_stream_private_t *p_stream,
7529 opj_event_mgr_t * p_manager )
7531 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7532 opj_bool l_result = OPJ_TRUE;
7533 OPJ_UINT32 l_nb_proc, i;
7536 assert(p_procedure_list != 00);
7537 assert(p_j2k != 00);
7538 assert(p_stream != 00);
7539 assert(p_manager != 00);
7542 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7543 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);
7545 for (i=0;i<l_nb_proc;++i) {
7546 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7550 /* and clear the procedure list at the end.*/
7551 opj_procedure_list_clear(p_procedure_list);
7556 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7557 opj_stream_private_t *p_stream,
7558 opj_event_mgr_t * p_manager
7561 opj_tcp_v2_t * l_tcp = 00;
7562 opj_tcp_v2_t * l_default_tcp = 00;
7563 OPJ_UINT32 l_nb_tiles;
7565 opj_tccp_t *l_current_tccp = 00;
7566 OPJ_UINT32 l_tccp_size;
7567 OPJ_UINT32 l_mct_size;
7568 opj_image_t * l_image;
7569 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7570 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7571 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7572 OPJ_UINT32 l_offset;
7575 assert(p_j2k != 00);
7576 assert(p_stream != 00);
7577 assert(p_manager != 00);
7579 l_image = p_j2k->m_private_image;
7580 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7581 l_tcp = p_j2k->m_cp.tcps;
7582 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7583 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7584 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7587 for (i=0; i<l_nb_tiles; ++i) {
7588 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7589 l_current_tccp = l_tcp->tccps;
7590 /*Copy default coding parameters into the current tile coding parameters*/
7591 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7592 /* Initialize some values of the current tile coding parameters*/
7594 l_tcp->ppt_data = 00;
7595 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7596 l_tcp->tccps = l_current_tccp;
7598 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7599 if (l_default_tcp->m_mct_decoding_matrix) {
7600 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7601 if (! l_tcp->m_mct_decoding_matrix ) {
7604 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7607 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7608 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7609 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7610 if (! l_tcp->m_mct_records) {
7613 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7615 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7616 l_src_mct_rec = l_default_tcp->m_mct_records;
7617 l_dest_mct_rec = l_tcp->m_mct_records;
7619 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7621 if (l_src_mct_rec->m_data) {
7623 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7624 if(! l_dest_mct_rec->m_data) {
7627 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7634 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7635 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7636 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7637 if (! l_tcp->m_mcc_records) {
7640 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7642 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7643 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7644 l_dest_mcc_rec = l_tcp->m_mcc_records;
7646 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7648 if (l_src_mcc_rec->m_decorrelation_array) {
7649 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7650 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7653 if (l_src_mcc_rec->m_offset_array) {
7654 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7655 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7662 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7663 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7665 /* Move to next tile cp*/
7669 /* Create the current tile decoder*/
7670 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7671 if (! p_j2k->m_tcd ) {
7675 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7676 tcd_destroy_v2(p_j2k->m_tcd);
7678 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7686 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7687 * with the marker value.
7688 * @param p_id Marker value to look up
7690 * @return the handler associated with the id.
7692 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7694 const opj_dec_memory_marker_handler_t *e;
7695 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7696 if (e->id == p_id) {
7697 break; /* we find a handler corresponding to the marker ID*/
7705 * Destroys a jpeg2000 codec.
7707 * @param p_j2k the jpeg20000 structure to destroy.
7709 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7715 if (p_j2k->m_is_decoder) {
7717 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7718 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7719 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7720 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7723 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7724 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7725 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7726 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7731 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7732 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7733 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7736 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7737 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7738 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7739 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7742 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7743 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7744 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7745 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7749 tcd_destroy_v2(p_j2k->m_tcd);
7751 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7752 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7754 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7755 p_j2k->m_procedure_list = 00;
7757 opj_procedure_list_destroy(p_j2k->m_validation_list);
7758 p_j2k->m_procedure_list = 00;
7760 j2k_destroy_cstr_index(p_j2k->cstr_index);
7761 p_j2k->cstr_index = NULL;
7763 opj_image_destroy(p_j2k->m_private_image);
7764 p_j2k->m_private_image = NULL;
7766 opj_image_destroy(p_j2k->m_output_image);
7767 p_j2k->m_output_image = NULL;
7772 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7776 if (p_cstr_ind->marker) {
7777 opj_free(p_cstr_ind->marker);
7778 p_cstr_ind->marker = NULL;
7781 if (p_cstr_ind->tile_index) {
7782 OPJ_UINT32 it_tile = 0;
7784 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7786 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7787 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7788 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7791 if(p_cstr_ind->tile_index[it_tile].tp_index){
7792 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7793 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7796 if(p_cstr_ind->tile_index[it_tile].marker){
7797 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7798 p_cstr_ind->tile_index[it_tile].marker = NULL;
7803 opj_free( p_cstr_ind->tile_index);
7804 p_cstr_ind->tile_index = NULL;
7807 opj_free(p_cstr_ind);
7814 * Destroys a tile coding parameter structure.
7816 * @param p_tcp the tile coding parameter to destroy.
7818 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7824 if (p_tcp->ppt_buffer != 00) {
7825 opj_free(p_tcp->ppt_buffer);
7826 p_tcp->ppt_buffer = 00;
7829 if (p_tcp->tccps != 00) {
7830 opj_free(p_tcp->tccps);
7834 if (p_tcp->m_mct_coding_matrix != 00) {
7835 opj_free(p_tcp->m_mct_coding_matrix);
7836 p_tcp->m_mct_coding_matrix = 00;
7839 if (p_tcp->m_mct_decoding_matrix != 00) {
7840 opj_free(p_tcp->m_mct_decoding_matrix);
7841 p_tcp->m_mct_decoding_matrix = 00;
7844 if (p_tcp->m_mcc_records) {
7845 opj_free(p_tcp->m_mcc_records);
7846 p_tcp->m_mcc_records = 00;
7847 p_tcp->m_nb_max_mcc_records = 0;
7848 p_tcp->m_nb_mcc_records = 0;
7851 if (p_tcp->m_mct_records) {
7852 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7855 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7856 if (l_mct_data->m_data) {
7857 opj_free(l_mct_data->m_data);
7858 l_mct_data->m_data = 00;
7864 opj_free(p_tcp->m_mct_records);
7865 p_tcp->m_mct_records = 00;
7868 if (p_tcp->mct_norms != 00) {
7869 opj_free(p_tcp->mct_norms);
7870 p_tcp->mct_norms = 00;
7873 opj_j2k_tcp_data_destroy(p_tcp);
7878 * Destroys the data inside a tile coding parameter structure.
7880 * @param p_tcp the tile coding parameter which contain data to destroy.
7882 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7884 if (p_tcp->m_data) {
7885 opj_free(p_tcp->m_data);
7886 p_tcp->m_data = NULL;
7887 p_tcp->m_data_size = 0;
7892 * Destroys a coding parameter structure.
7894 * @param p_cp the coding parameter to destroy.
7896 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7898 OPJ_UINT32 l_nb_tiles;
7899 opj_tcp_v2_t * l_current_tile = 00;
7910 l_current_tile = p_cp->tcps;
7911 l_nb_tiles = p_cp->th * p_cp->tw;
7914 (i = 0; i < l_nb_tiles; ++i)
7916 opj_j2k_tcp_destroy(l_current_tile);
7919 opj_free(p_cp->tcps);
7923 (p_cp->ppm_buffer != 00)
7925 opj_free(p_cp->ppm_buffer);
7926 p_cp->ppm_buffer = 00;
7929 (p_cp->comment != 00)
7931 opj_free(p_cp->comment);
7935 (! p_cp->m_is_decoder)
7938 (p_cp->m_specific_param.m_enc.m_matrice)
7940 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7941 p_cp->m_specific_param.m_enc.m_matrice = 00;
7949 * Reads a tile header.
7950 * @param p_j2k the jpeg2000 codec.
7951 * @param p_stream the stream to write data to.
7952 * @param p_manager the user event manager.
7954 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7955 OPJ_UINT32 * p_tile_index,
7956 OPJ_UINT32 * p_data_size,
7957 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7958 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7959 OPJ_UINT32 * p_nb_comps,
7961 opj_stream_private_t *p_stream,
7962 opj_event_mgr_t * p_manager )
7964 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7965 OPJ_UINT32 l_marker_size;
7966 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7967 opj_tcp_v2_t * l_tcp = NULL;
7968 OPJ_UINT32 l_nb_tiles;
7971 assert(p_stream != 00);
7972 assert(p_j2k != 00);
7973 assert(p_manager != 00);
7975 /* Reach the End Of Codestream ?*/
7976 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7977 l_current_marker = J2K_MS_EOC;
7979 /* We need to encounter a SOT marker (a new tile-part header) */
7980 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7984 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7985 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7987 /* Try to read until the Start Of Data is detected */
7988 while (l_current_marker != J2K_MS_SOD) {
7990 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7991 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7992 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7996 /* Read 2 bytes from the buffer as the marker size */
7997 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7999 /* Why this condition? FIXME */
8000 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8001 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8003 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8005 /* Get the marker handler from the marker ID */
8006 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8008 /* Check if the marker is known and if it is the right place to find it */
8009 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8010 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8013 /* FIXME manage case of unknown marker as in the main header ? */
8015 /* Check if the marker size is compatible with the header data size */
8016 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8017 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8018 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8019 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8022 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8025 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8026 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) {
8027 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8031 /* Read the marker segment with the correct marker handler */
8032 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8033 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8037 /* Add the marker to the codestream index*/
8038 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8040 l_marker_handler->id,
8041 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8042 l_marker_size + 4 );
8044 /* Keep the position of the last SOT marker read */
8045 if ( l_marker_handler->id == J2K_MS_SOT ) {
8046 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8047 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8049 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8054 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8055 /* Skip the rest of the tile part header*/
8056 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) {
8057 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8060 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8063 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8064 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8065 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8068 /* Read 2 bytes from the buffer as the new marker ID */
8069 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8073 /* If we didn't skip data before, we need to read the SOD marker*/
8074 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8075 /* Try to read the SOD marker and skip data ? FIXME */
8076 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8082 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8083 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8084 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8085 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8089 /* Read 2 bytes from buffer as the new marker ID */
8090 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8094 /* Indicate we will try to read a new tile-part header*/
8095 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8096 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8097 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8099 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8100 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8101 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8105 /* Read 2 bytes from buffer as the new marker ID */
8106 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8110 /* Current marker is the EOC marker ?*/
8111 if (l_current_marker == J2K_MS_EOC) {
8112 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8113 p_j2k->m_current_tile_number = 0;
8114 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8119 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8120 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8121 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8123 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8124 ++p_j2k->m_current_tile_number;
8128 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8129 *p_go_on = OPJ_FALSE;
8135 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8136 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8140 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8141 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8143 *p_tile_index = p_j2k->m_current_tile_number;
8144 *p_go_on = OPJ_TRUE;
8145 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8146 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8147 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8148 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8149 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8150 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8152 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8158 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8159 OPJ_UINT32 p_tile_index,
8161 OPJ_UINT32 p_data_size,
8162 opj_stream_private_t *p_stream,
8163 opj_event_mgr_t * p_manager )
8165 OPJ_UINT32 l_current_marker;
8166 OPJ_BYTE l_data [2];
8167 opj_tcp_v2_t * l_tcp;
8170 assert(p_stream != 00);
8171 assert(p_j2k != 00);
8172 assert(p_manager != 00);
8174 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8175 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8179 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8180 if (! l_tcp->m_data) {
8181 opj_j2k_tcp_destroy(l_tcp);
8185 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8189 p_j2k->cstr_index) ) {
8190 opj_j2k_tcp_destroy(l_tcp);
8191 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8195 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8199 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8200 * we destroy just the data which will be re-read in read_tile_header*/
8201 /*opj_j2k_tcp_destroy(l_tcp);
8202 p_j2k->m_tcd->tcp = 0;*/
8203 opj_j2k_tcp_data_destroy(l_tcp);
8205 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8206 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8208 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8209 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8210 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8214 opj_read_bytes(l_data,&l_current_marker,2);
8216 if (l_current_marker == J2K_MS_EOC) {
8217 p_j2k->m_current_tile_number = 0;
8218 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8220 else if (l_current_marker != J2K_MS_SOT)
8222 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8231 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8233 OPJ_UINT32 i,j,k = 0;
8234 OPJ_UINT32 l_width_src,l_height_src;
8235 OPJ_UINT32 l_width_dest,l_height_dest;
8236 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8237 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8238 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8239 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8240 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8242 opj_image_comp_t * l_img_comp_src = 00;
8243 opj_image_comp_t * l_img_comp_dest = 00;
8245 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8246 opj_image_t * l_image_src = 00;
8247 OPJ_UINT32 l_size_comp, l_remaining;
8248 OPJ_INT32 * l_dest_ptr;
8249 opj_tcd_resolution_v2_t* l_res= 00;
8251 l_tilec = p_tcd->tcd_image->tiles->comps;
8252 l_image_src = p_tcd->image;
8253 l_img_comp_src = l_image_src->comps;
8255 l_img_comp_dest = p_output_image->comps;
8257 for (i=0; i<l_image_src->numcomps; i++) {
8259 /* Allocate output component buffer if necessary */
8260 if (!l_img_comp_dest->data) {
8262 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8263 if (! l_img_comp_dest->data) {
8268 /* Copy info from decoded comp image to output image */
8269 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8272 /* Compute the precision of the output buffer */
8273 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8274 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8275 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8281 if (l_size_comp == 3) {
8286 /* Current tile component size*/
8288 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8289 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8292 l_width_src = (l_res->x1 - l_res->x0);
8293 l_height_src = (l_res->y1 - l_res->y0);
8295 /* Border of the current output component*/
8296 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8297 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8298 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8299 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8302 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8303 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8307 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8308 * of the input buffer (decoded tile component) which will be move
8309 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8310 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8311 * by this input area.
8313 assert( l_res->x0 >= 0);
8314 assert( l_res->x1 >= 0);
8315 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8316 l_start_x_dest = l_res->x0 - l_x0_dest;
8317 l_offset_x0_src = 0;
8319 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8320 l_width_dest = l_width_src;
8321 l_offset_x1_src = 0;
8324 l_width_dest = l_x1_dest - l_res->x0 ;
8325 l_offset_x1_src = l_width_src - l_width_dest;
8329 l_start_x_dest = 0 ;
8330 l_offset_x0_src = l_x0_dest - l_res->x0;
8332 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8333 l_width_dest = l_width_src - l_offset_x0_src;
8334 l_offset_x1_src = 0;
8337 l_width_dest = l_img_comp_dest->w ;
8338 l_offset_x1_src = l_res->x1 - l_x1_dest;
8342 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8343 l_start_y_dest = l_res->y0 - l_y0_dest;
8344 l_offset_y0_src = 0;
8346 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8347 l_height_dest = l_height_src;
8348 l_offset_y1_src = 0;
8351 l_height_dest = l_y1_dest - l_res->y0 ;
8352 l_offset_y1_src = l_height_src - l_height_dest;
8356 l_start_y_dest = 0 ;
8357 l_offset_y0_src = l_y0_dest - l_res->y0;
8359 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8360 l_height_dest = l_height_src - l_offset_y0_src;
8361 l_offset_y1_src = 0;
8364 l_height_dest = l_img_comp_dest->h ;
8365 l_offset_y1_src = l_res->y1 - l_y1_dest;
8369 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8374 /* Compute the input buffer offset */
8375 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8376 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8377 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8379 /* Compute the output buffer offset */
8380 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8381 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8383 /* Move the output buffer to the first place where we will write*/
8384 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8387 fprintf(stdout, "COMPO[%d]:\n",i);
8388 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8389 "\t tile offset:%d, %d, %d, %d\n"
8390 "\t buffer offset: %d; %d, %d\n",
8391 l_res->x0, l_res->y0, l_width_src, l_height_src,
8392 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8393 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8395 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8396 "\t start offset: %d, line offset= %d\n",
8397 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8401 switch (l_size_comp) {
8404 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8405 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8407 if (l_img_comp_src->sgnd) {
8408 for (j = 0 ; j < l_height_dest ; ++j) {
8409 for ( k = 0 ; k < l_width_dest ; ++k) {
8410 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8413 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8414 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8418 for ( j = 0 ; j < l_height_dest ; ++j ) {
8419 for ( k = 0 ; k < l_width_dest ; ++k) {
8420 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8423 l_dest_ptr+= l_line_offset_dest;
8424 l_src_ptr += l_line_offset_src;
8428 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8429 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8434 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8435 l_src_ptr += l_start_offset_src;
8437 if (l_img_comp_src->sgnd) {
8438 for (j=0;j<l_height_dest;++j) {
8439 for (k=0;k<l_width_dest;++k) {
8440 *(l_dest_ptr++) = *(l_src_ptr++);
8443 l_dest_ptr+= l_line_offset_dest;
8444 l_src_ptr += l_line_offset_src ;
8448 for (j=0;j<l_height_dest;++j) {
8449 for (k=0;k<l_width_dest;++k) {
8450 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8453 l_dest_ptr+= l_line_offset_dest;
8454 l_src_ptr += l_line_offset_src ;
8458 l_src_ptr += l_end_offset_src;
8459 p_data = (OPJ_BYTE*) l_src_ptr;
8464 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8465 l_src_ptr += l_start_offset_src;
8467 for (j=0;j<l_height_dest;++j) {
8468 for (k=0;k<l_width_dest;++k) {
8469 *(l_dest_ptr++) = (*(l_src_ptr++));
8472 l_dest_ptr+= l_line_offset_dest;
8473 l_src_ptr += l_line_offset_src ;
8476 l_src_ptr += l_end_offset_src;
8477 p_data = (OPJ_BYTE*) l_src_ptr;
8491 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8493 * @param p_j2k the jpeg2000 codec.
8494 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8495 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8496 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8497 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8498 * @param p_manager the user event manager
8500 * @return true if the area could be set.
8502 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8503 opj_image_t* p_image,
8504 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8505 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8506 opj_event_mgr_t * p_manager )
8508 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8509 opj_image_t * l_image = p_j2k->m_private_image;
8512 OPJ_INT32 l_comp_x1, l_comp_y1;
8513 opj_image_comp_t* l_img_comp = NULL;
8515 /* Check if we are read the main header */
8516 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8517 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8521 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8522 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8524 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8525 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8526 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8527 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8533 /* Check if the positions provided by the user are correct */
8536 assert(p_start_x >= 0 );
8537 assert(p_start_y >= 0 );
8539 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8540 opj_event_msg_v2(p_manager, EVT_ERROR,
8541 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8542 p_start_x, l_image->x1);
8545 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8546 opj_event_msg_v2(p_manager, EVT_WARNING,
8547 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8548 p_start_x, l_image->x0);
8549 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8550 p_image->x0 = l_image->x0;
8553 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8554 p_image->x0 = p_start_x;
8558 if ((OPJ_UINT32)p_start_y > l_image->y1){
8559 opj_event_msg_v2(p_manager, EVT_ERROR,
8560 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8561 p_start_y, l_image->y1);
8564 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8565 opj_event_msg_v2(p_manager, EVT_WARNING,
8566 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8567 p_start_y, l_image->y0);
8568 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8569 p_image->y0 = l_image->y0;
8572 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8573 p_image->y0 = p_start_y;
8577 assert((OPJ_UINT32)p_end_x > 0);
8578 assert((OPJ_UINT32)p_end_y > 0);
8579 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8580 opj_event_msg_v2(p_manager, EVT_ERROR,
8581 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8582 p_end_x, l_image->x0);
8585 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8586 opj_event_msg_v2(p_manager, EVT_WARNING,
8587 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8588 p_end_x, l_image->x1);
8589 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8590 p_image->x1 = l_image->x1;
8593 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8594 p_image->x1 = p_end_x;
8598 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8599 opj_event_msg_v2(p_manager, EVT_ERROR,
8600 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8601 p_end_y, l_image->y0);
8604 if ((OPJ_UINT32)p_end_y > l_image->y1){
8605 opj_event_msg_v2(p_manager, EVT_WARNING,
8606 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8607 p_end_y, l_image->y1);
8608 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8609 p_image->y1 = l_image->y1;
8612 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8613 p_image->y1 = p_end_y;
8617 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8619 l_img_comp = p_image->comps;
8620 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8624 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8625 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8626 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8627 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8629 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8630 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8632 opj_event_msg_v2(p_manager, EVT_ERROR,
8633 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8637 l_img_comp->w = l_w;
8639 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8640 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8642 opj_event_msg_v2(p_manager, EVT_ERROR,
8643 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8647 l_img_comp->h = l_h;
8652 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8653 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8660 /* ----------------------------------------------------------------------- */
8661 /* J2K / JPT decoder interface */
8662 /* ----------------------------------------------------------------------- */
8664 * Creates a J2K decompression structure.
8666 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8668 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8670 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8674 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8676 l_j2k->m_is_decoder = 1;
8677 l_j2k->m_cp.m_is_decoder = 1;
8679 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8680 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8681 opj_j2k_destroy(l_j2k);
8684 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8686 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8687 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8688 opj_j2k_destroy(l_j2k);
8692 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8694 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8696 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8698 /* codestream index creation */
8699 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8701 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8702 if (!l_j2k->cstr_index){
8703 opj_j2k_destroy(l_j2k);
8707 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8710 /* validation list creation */
8711 l_j2k->m_validation_list = opj_procedure_list_create();
8712 if (! l_j2k->m_validation_list) {
8713 opj_j2k_destroy(l_j2k);
8717 /* execution list creation */
8718 l_j2k->m_procedure_list = opj_procedure_list_create();
8719 if (! l_j2k->m_procedure_list) {
8720 opj_j2k_destroy(l_j2k);
8728 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8730 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8731 opj_calloc(1,sizeof(opj_codestream_index_t));
8735 cstr_index->maxmarknum = 100;
8736 cstr_index->marknum = 0;
8737 cstr_index->marker = (opj_marker_info_t*)
8738 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8739 if (!cstr_index-> marker)
8742 cstr_index->tile_index = NULL;
8749 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8751 * @param p_tile_no the tile index.
8752 * @param p_comp_no the component being outputted.
8753 * @param p_j2k the J2K codec.
8755 * @return the number of bytes taken by the SPCod element.
8757 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8758 OPJ_UINT32 p_tile_no,
8759 OPJ_UINT32 p_comp_no )
8761 opj_cp_v2_t *l_cp = 00;
8762 opj_tcp_v2_t *l_tcp = 00;
8763 opj_tccp_t *l_tccp = 00;
8766 assert(p_j2k != 00);
8768 l_cp = &(p_j2k->m_cp);
8769 l_tcp = &l_cp->tcps[p_tile_no];
8770 l_tccp = &l_tcp->tccps[p_comp_no];
8772 /* preconditions again */
8773 assert(p_tile_no < (l_cp->tw * l_cp->th));
8774 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8776 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8777 return 5 + l_tccp->numresolutions;
8785 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8787 * @param p_comp_no the component number to output.
8788 * @param p_stream the stream to write data to.
8789 * @param p_j2k J2K codec.
8790 * @param p_manager the user event manager.
8793 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8794 OPJ_UINT32 p_tile_no,
8795 OPJ_UINT32 p_comp_no,
8797 OPJ_UINT32 * p_header_size,
8798 struct opj_event_mgr * p_manager )
8801 opj_cp_v2_t *l_cp = 00;
8802 opj_tcp_v2_t *l_tcp = 00;
8803 opj_tccp_t *l_tccp = 00;
8806 assert(p_j2k != 00);
8807 assert(p_header_size != 00);
8808 assert(p_manager != 00);
8809 assert(p_data != 00);
8811 l_cp = &(p_j2k->m_cp);
8812 l_tcp = &l_cp->tcps[p_tile_no];
8813 l_tccp = &l_tcp->tccps[p_comp_no];
8815 /* preconditions again */
8816 assert(p_tile_no < (l_cp->tw * l_cp->th));
8817 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8819 if (*p_header_size < 5) {
8820 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8824 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8827 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8830 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8833 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8836 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8839 *p_header_size = *p_header_size - 5;
8841 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8843 if (*p_header_size < l_tccp->numresolutions) {
8844 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8848 for (i = 0; i < l_tccp->numresolutions; ++i) {
8849 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8853 *p_header_size = *p_header_size - l_tccp->numresolutions;
8860 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8861 * @param p_header_data the data contained in the COM box.
8862 * @param p_j2k the jpeg2000 codec.
8863 * @param p_header_size the size of the data contained in the COM marker.
8864 * @param p_manager the user event manager.
8866 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8868 OPJ_BYTE * p_header_data,
8869 OPJ_UINT32 * p_header_size,
8870 opj_event_mgr_t * p_manager)
8872 OPJ_UINT32 i, l_tmp;
8873 opj_cp_v2_t *l_cp = NULL;
8874 opj_tcp_v2_t *l_tcp = NULL;
8875 opj_tccp_t *l_tccp = NULL;
8876 OPJ_BYTE * l_current_ptr = NULL;
8879 assert(p_j2k != 00);
8880 assert(p_manager != 00);
8881 assert(p_header_data != 00);
8883 l_cp = &(p_j2k->m_cp);
8884 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8885 &l_cp->tcps[p_j2k->m_current_tile_number] :
8886 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8888 /* precondition again */
8889 assert(compno < p_j2k->m_private_image->numcomps);
8891 l_tccp = &l_tcp->tccps[compno];
8892 l_current_ptr = p_header_data;
8894 /* make sure room is sufficient */
8895 if (*p_header_size < 5) {
8896 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8900 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8901 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8904 /* If user wants to remove more resolutions than the codestream contains, return error */
8905 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8906 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8907 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8908 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8912 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8916 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8920 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8923 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8926 *p_header_size = *p_header_size - 5;
8928 /* use custom precinct size ? */
8929 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8930 if (*p_header_size < l_tccp->numresolutions) {
8931 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8935 for (i = 0; i < l_tccp->numresolutions; ++i) {
8936 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8938 l_tccp->prcw[i] = l_tmp & 0xf;
8939 l_tccp->prch[i] = l_tmp >> 4;
8942 *p_header_size = *p_header_size - l_tccp->numresolutions;
8945 /* set default size for the precinct width and height */
8946 for (i = 0; i < l_tccp->numresolutions; ++i) {
8947 l_tccp->prcw[i] = 15;
8948 l_tccp->prch[i] = 15;
8952 #ifdef WIP_REMOVE_MSD
8954 if (p_j2k->cstr_info && compno == 0) {
8955 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8957 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8958 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8959 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8960 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8961 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8964 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8965 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8974 * Copies the tile component parameters of all the component from the first tile component.
8976 * @param p_j2k the J2k codec.
8978 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8982 opj_cp_v2_t *l_cp = NULL;
8983 opj_tcp_v2_t *l_tcp = NULL;
8984 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8985 OPJ_UINT32 l_prc_size;
8988 assert(p_j2k != 00);
8990 l_cp = &(p_j2k->m_cp);
8991 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8992 &l_cp->tcps[p_j2k->m_current_tile_number] :
8993 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8995 l_ref_tccp = &l_tcp->tccps[0];
8996 l_copied_tccp = l_ref_tccp + 1;
8997 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8999 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9000 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9001 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9002 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9003 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9004 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9005 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9006 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9012 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9014 * @param p_tile_no the tile index.
9015 * @param p_comp_no the component being outputted.
9016 * @param p_j2k the J2K codec.
9018 * @return the number of bytes taken by the SPCod element.
9020 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9021 OPJ_UINT32 p_tile_no,
9022 OPJ_UINT32 p_comp_no )
9024 OPJ_UINT32 l_num_bands;
9026 opj_cp_v2_t *l_cp = 00;
9027 opj_tcp_v2_t *l_tcp = 00;
9028 opj_tccp_t *l_tccp = 00;
9031 assert(p_j2k != 00);
9033 l_cp = &(p_j2k->m_cp);
9034 l_tcp = &l_cp->tcps[p_tile_no];
9035 l_tccp = &l_tcp->tccps[p_comp_no];
9037 /* preconditions again */
9038 assert(p_tile_no < l_cp->tw * l_cp->th);
9039 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9041 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9043 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9044 return 1 + l_num_bands;
9047 return 1 + 2*l_num_bands;
9052 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9054 * @param p_tile_no the tile to output.
9055 * @param p_comp_no the component number to output.
9056 * @param p_data the data buffer.
9057 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9058 * @param p_j2k J2K codec.
9059 * @param p_manager the user event manager.
9062 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9063 OPJ_UINT32 p_tile_no,
9064 OPJ_UINT32 p_comp_no,
9066 OPJ_UINT32 * p_header_size,
9067 struct opj_event_mgr * p_manager )
9069 OPJ_UINT32 l_header_size;
9070 OPJ_UINT32 l_band_no, l_num_bands;
9071 OPJ_UINT32 l_expn,l_mant;
9073 opj_cp_v2_t *l_cp = 00;
9074 opj_tcp_v2_t *l_tcp = 00;
9075 opj_tccp_t *l_tccp = 00;
9078 assert(p_j2k != 00);
9079 assert(p_header_size != 00);
9080 assert(p_manager != 00);
9081 assert(p_data != 00);
9083 l_cp = &(p_j2k->m_cp);
9084 l_tcp = &l_cp->tcps[p_tile_no];
9085 l_tccp = &l_tcp->tccps[p_comp_no];
9087 /* preconditions again */
9088 assert(p_tile_no < l_cp->tw * l_cp->th);
9089 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9091 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9093 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9094 l_header_size = 1 + l_num_bands;
9096 if (*p_header_size < l_header_size) {
9097 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9101 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9104 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9105 l_expn = l_tccp->stepsizes[l_band_no].expn;
9106 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9111 l_header_size = 1 + 2*l_num_bands;
9113 if (*p_header_size < l_header_size) {
9114 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9118 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9121 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9122 l_expn = l_tccp->stepsizes[l_band_no].expn;
9123 l_mant = l_tccp->stepsizes[l_band_no].mant;
9125 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9130 *p_header_size = *p_header_size - l_header_size;
9136 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9138 * @param p_comp_no the component being targeted.
9139 * @param p_header_data the data contained in the COM box.
9140 * @param p_j2k the jpeg2000 codec.
9141 * @param p_header_size the size of the data contained in the COM marker.
9142 * @param p_manager the user event manager.
9144 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9145 OPJ_UINT32 p_comp_no,
9146 OPJ_BYTE* p_header_data,
9147 OPJ_UINT32 * p_header_size,
9148 opj_event_mgr_t * p_manager
9152 OPJ_UINT32 l_band_no;
9153 opj_cp_v2_t *l_cp = 00;
9154 opj_tcp_v2_t *l_tcp = 00;
9155 opj_tccp_t *l_tccp = 00;
9156 OPJ_BYTE * l_current_ptr = 00;
9157 OPJ_UINT32 l_tmp, l_num_band;
9160 assert(p_j2k != 00);
9161 assert(p_manager != 00);
9162 assert(p_header_data != 00);
9164 l_cp = &(p_j2k->m_cp);
9165 /* come from tile part header or main header ?*/
9166 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9167 &l_cp->tcps[p_j2k->m_current_tile_number] :
9168 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9170 /* precondition again*/
9171 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9173 l_tccp = &l_tcp->tccps[p_comp_no];
9174 l_current_ptr = p_header_data;
9176 if (*p_header_size < 1) {
9177 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9180 *p_header_size -= 1;
9182 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9185 l_tccp->qntsty = l_tmp & 0x1f;
9186 l_tccp->numgbits = l_tmp >> 5;
9187 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9191 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9193 (*p_header_size) / 2;
9195 if( l_num_band > J2K_MAXBANDS ) {
9196 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9197 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9198 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9199 /*return OPJ_FALSE;*/
9204 if (l_cp->correct) {
9206 /* if JPWL is on, we check whether there are too many subbands */
9207 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9208 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9209 "JPWL: bad number of subbands in Sqcx (%d)\n",
9212 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9215 /* we try to correct */
9217 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9218 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9223 #endif /* USE_JPWL */
9225 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9226 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9227 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9229 if (l_band_no < J2K_MAXBANDS){
9230 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9231 l_tccp->stepsizes[l_band_no].mant = 0;
9234 *p_header_size = *p_header_size - l_num_band;
9237 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9238 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9240 if (l_band_no < J2K_MAXBANDS){
9241 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9242 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9245 *p_header_size = *p_header_size - 2*l_num_band;
9248 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9249 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9250 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9251 l_tccp->stepsizes[l_band_no].expn =
9252 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9253 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9254 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9262 * Copies the tile component parameters of all the component from the first tile component.
9264 * @param p_j2k the J2k codec.
9266 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9269 opj_cp_v2_t *l_cp = NULL;
9270 opj_tcp_v2_t *l_tcp = NULL;
9271 opj_tccp_t *l_ref_tccp = NULL;
9272 opj_tccp_t *l_copied_tccp = NULL;
9276 assert(p_j2k != 00);
9278 l_cp = &(p_j2k->m_cp);
9279 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9280 &l_cp->tcps[p_j2k->m_current_tile_number] :
9281 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9283 l_ref_tccp = &l_tcp->tccps[0];
9284 l_copied_tccp = l_ref_tccp + 1;
9285 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9287 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9288 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9289 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9290 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9296 * Dump some elements from the J2K decompression structure .
9298 *@param p_j2k the jpeg2000 codec.
9299 *@param flag flag to describe what elments are dump.
9300 *@param out_stream output stream where dump the elements.
9303 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9305 /* Check if the flag is compatible with j2k file*/
9306 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9307 fprintf(out_stream, "Wrong flag\n");
9311 /* Dump the image_header */
9312 if (flag & OPJ_IMG_INFO){
9313 if (p_j2k->m_private_image)
9314 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9317 /* Dump the codestream info from main header */
9318 if (flag & OPJ_J2K_MH_INFO){
9319 opj_j2k_dump_MH_info(p_j2k, out_stream);
9323 /* Dump the codestream info of the current tile */
9324 if (flag & OPJ_J2K_TH_INFO){
9328 /* Dump the codestream index from main header */
9329 if (flag & OPJ_J2K_MH_IND){
9330 opj_j2k_dump_MH_index(p_j2k, out_stream);
9333 /* Dump the codestream index of the current tile */
9334 if (flag & OPJ_J2K_TH_IND){
9341 * Dump index elements of the codestream extract from the main header.
9343 *@param p_j2k the jpeg2000 codec.
9344 *@param out_stream output stream where dump the elements.
9347 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9349 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9350 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9352 fprintf(out_stream, "Codestream index from main header: {\n");
9354 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9355 "\t Main header end position=%" PRIi64 "\n",
9356 cstr_index->main_head_start, cstr_index->main_head_end);
9358 fprintf(out_stream, "\t Marker list: {\n");
9360 if (cstr_index->marker){
9361 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9362 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9363 cstr_index->marker[it_marker].type,
9364 cstr_index->marker[it_marker].pos,
9365 cstr_index->marker[it_marker].len );
9369 fprintf(out_stream, "\t }\n");
9372 if (cstr_index->tile_index){
9374 /* Simple test to avoid to write empty information*/
9375 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9376 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9377 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9380 if (l_acc_nb_of_tile_part)
9382 fprintf(out_stream, "\t Tile index: {\n");
9384 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9385 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9387 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9389 if (cstr_index->tile_index[it_tile].tp_index){
9390 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9391 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9393 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9394 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9395 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9399 if (cstr_index->tile_index[it_tile].marker){
9400 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9401 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9402 cstr_index->tile_index[it_tile].marker[it_marker].type,
9403 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9404 cstr_index->tile_index[it_tile].marker[it_marker].len );
9408 fprintf(out_stream,"\t }\n");
9412 fprintf(out_stream,"}\n");
9417 * Dump info elements of the codestream extract from the main header.
9419 *@param p_j2k the jpeg2000 codec.
9420 *@param out_stream output stream where dump the elements.
9423 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9425 opj_tcp_v2_t * l_default_tile=NULL;
9427 fprintf(out_stream, "Codestream info from main header: {\n");
9429 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9430 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9431 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9433 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9437 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9439 fprintf(out_stream, "\t default tile {\n");
9440 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9441 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9442 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9443 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9445 for (compno = 0; compno < numcomps; compno++) {
9446 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9448 OPJ_INT32 bandno, numbands;
9451 fprintf(out_stream, "\t\t comp %d {\n", compno);
9452 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9453 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9454 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9455 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9456 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9457 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9459 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9460 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9461 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9463 fprintf(out_stream, "\n");
9465 /* quantization style*/
9466 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9467 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9468 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9469 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9470 for (bandno = 0; bandno < numbands; bandno++) {
9471 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9472 l_tccp->stepsizes[bandno].expn);
9474 fprintf(out_stream, "\n");
9477 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9479 fprintf(out_stream, "\t\t }\n");
9480 } /*end of component of default tile*/
9481 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9485 fprintf(out_stream, "}\n");
9490 * Dump an image header structure.
9492 *@param img_header the image header to dump.
9493 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9494 *@param out_stream output stream where dump the elements.
9496 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9501 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9505 fprintf(out_stream, "Image info {\n");
9506 tab[0] = '\t';tab[1] = '\0';
9509 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9510 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9511 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9513 if (img_header->comps){
9515 for (compno = 0; compno < img_header->numcomps; compno++) {
9516 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9517 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9518 fprintf(out_stream,"%s}\n",tab);
9522 fprintf(out_stream, "}\n");
9526 * Dump a component image header structure.
9528 *@param comp_header the component image header to dump.
9529 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9530 *@param out_stream output stream where dump the elements.
9532 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9537 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9540 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9543 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9544 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9545 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9548 fprintf(out_stream, "}\n");
9553 * Get the codestream info from a JPEG2000 codec.
9555 *@param p_j2k the component image header to dump.
9557 *@return the codestream information extract from the jpg2000 codec
9559 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9562 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9563 opj_tcp_v2_t *l_default_tile;
9564 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9566 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9568 cstr_info->tx0 = p_j2k->m_cp.tx0;
9569 cstr_info->ty0 = p_j2k->m_cp.ty0;
9570 cstr_info->tdx = p_j2k->m_cp.tdx;
9571 cstr_info->tdy = p_j2k->m_cp.tdy;
9572 cstr_info->tw = p_j2k->m_cp.tw;
9573 cstr_info->th = p_j2k->m_cp.th;
9575 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9577 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9579 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9580 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9581 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9582 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9584 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9586 for (compno = 0; compno < numcomps; compno++) {
9587 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9588 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9589 OPJ_INT32 bandno, numbands;
9592 l_tccp_info->csty = l_tccp->csty;
9593 l_tccp_info->numresolutions = l_tccp->numresolutions;
9594 l_tccp_info->cblkw = l_tccp->cblkw;
9595 l_tccp_info->cblkh = l_tccp->cblkh;
9596 l_tccp_info->cblksty = l_tccp->cblksty;
9597 l_tccp_info->qmfbid = l_tccp->qmfbid;
9598 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9600 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9601 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9604 /* quantization style*/
9605 l_tccp_info->qntsty = l_tccp->qntsty;
9606 l_tccp_info->numgbits = l_tccp->numgbits;
9608 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9609 if (numbands < J2K_MAXBANDS) {
9610 for (bandno = 0; bandno < numbands; bandno++) {
9611 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9612 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9617 l_tccp_info->roishift = l_tccp->roishift;
9625 * Get the codestream index from a JPEG2000 codec.
9627 *@param p_j2k the component image header to dump.
9629 *@return the codestream index extract from the jpg2000 codec
9631 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9633 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9634 opj_calloc(1,sizeof(opj_codestream_index_t));
9638 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9639 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9640 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9642 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9643 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9644 if (!l_cstr_index->marker){
9645 opj_free( l_cstr_index);
9649 if (p_j2k->cstr_index->marker)
9650 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9652 opj_free(l_cstr_index->marker);
9653 l_cstr_index->marker = NULL;
9656 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9657 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9658 if (!l_cstr_index->tile_index){
9659 opj_free( l_cstr_index->marker);
9660 opj_free( l_cstr_index);
9664 if (!p_j2k->cstr_index->tile_index){
9665 opj_free(l_cstr_index->tile_index);
9666 l_cstr_index->tile_index = NULL;
9669 OPJ_UINT32 it_tile = 0;
9670 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9673 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9675 l_cstr_index->tile_index[it_tile].marker =
9676 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9678 if (!l_cstr_index->tile_index[it_tile].marker) {
9679 OPJ_UINT32 it_tile_free;
9681 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9682 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9685 opj_free( l_cstr_index->tile_index);
9686 opj_free( l_cstr_index->marker);
9687 opj_free( l_cstr_index);
9691 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9692 memcpy( l_cstr_index->tile_index[it_tile].marker,
9693 p_j2k->cstr_index->tile_index[it_tile].marker,
9694 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9696 opj_free(l_cstr_index->tile_index[it_tile].marker);
9697 l_cstr_index->tile_index[it_tile].marker = NULL;
9700 /* Tile part index*/
9701 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9703 l_cstr_index->tile_index[it_tile].tp_index =
9704 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9706 if(!l_cstr_index->tile_index[it_tile].tp_index){
9707 OPJ_UINT32 it_tile_free;
9709 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9710 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9711 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9714 opj_free( l_cstr_index->tile_index);
9715 opj_free( l_cstr_index->marker);
9716 opj_free( l_cstr_index);
9720 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9721 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9722 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9723 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9726 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9727 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9730 /* Packet index (NOT USED)*/
9731 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9732 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9737 return l_cstr_index;
9740 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9742 OPJ_UINT32 it_tile=0;
9744 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9745 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9746 if (!p_j2k->cstr_index->tile_index)
9749 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9750 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9751 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9752 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9753 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9754 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9764 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9765 opj_stream_private_t *p_stream,
9766 opj_event_mgr_t * p_manager)
9768 opj_bool l_go_on = OPJ_TRUE;
9769 OPJ_UINT32 l_current_tile_no;
9770 OPJ_UINT32 l_data_size,l_max_data_size;
9771 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9772 OPJ_UINT32 l_nb_comps;
9773 OPJ_BYTE * l_current_data;
9775 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9776 if (! l_current_data) {
9779 l_max_data_size = 1000;
9784 if (! opj_j2k_read_tile_header( p_j2k,
9787 &l_tile_x0, &l_tile_y0,
9788 &l_tile_x1, &l_tile_y1,
9793 opj_free(l_current_data);
9801 if (l_data_size > l_max_data_size) {
9802 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9803 if (! l_current_data) {
9804 opj_free(l_current_data);
9808 l_max_data_size = l_data_size;
9811 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9812 opj_free(l_current_data);
9815 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);
9817 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9818 opj_free(l_current_data);
9821 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9825 opj_free(l_current_data);
9831 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9833 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9836 assert(p_j2k != 00);
9838 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9839 /* DEVELOPER CORNER, add your custom procedures */
9844 * Read and decode one tile.
9846 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9847 opj_stream_private_t *p_stream,
9848 opj_event_mgr_t * p_manager)
9850 opj_bool l_go_on = OPJ_TRUE;
9851 OPJ_UINT32 l_current_tile_no;
9852 OPJ_UINT32 l_tile_no_to_dec;
9853 OPJ_UINT32 l_data_size,l_max_data_size;
9854 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9855 OPJ_UINT32 l_nb_comps;
9856 OPJ_BYTE * l_current_data;
9858 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9859 if (! l_current_data) {
9862 l_max_data_size = 1000;
9864 /*Allocate and initialize some elements of codestrem index if not already done*/
9865 if( !p_j2k->cstr_index->tile_index)
9867 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9868 opj_free(l_current_data);
9872 /* Move into the codestream to the first SOT used to decode the desired tile */
9873 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9874 if (p_j2k->cstr_index->tile_index)
9875 if(p_j2k->cstr_index->tile_index->tp_index)
9877 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9878 /* the index for this tile has not been built,
9879 * so move to the last SOT read */
9880 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9881 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9886 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)) ) {
9887 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9891 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9892 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9893 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9897 if (! opj_j2k_read_tile_header( p_j2k,
9900 &l_tile_x0, &l_tile_y0,
9901 &l_tile_x1, &l_tile_y1,
9906 opj_free(l_current_data);
9915 if (l_data_size > l_max_data_size) {
9916 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9917 if (! l_current_data) {
9918 opj_free(l_current_data);
9922 l_max_data_size = l_data_size;
9927 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9928 opj_free(l_current_data);
9931 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);
9933 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9934 opj_free(l_current_data);
9937 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9939 if(l_current_tile_no == l_tile_no_to_dec)
9941 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9942 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9943 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9949 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);
9954 opj_free(l_current_data);
9961 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9963 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9966 assert(p_j2k != 00);
9968 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9969 /* DEVELOPER CORNER, add your custom procedures */
9975 * Decodes the tiles of the stream.
9977 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9978 opj_stream_private_t * p_stream,
9979 opj_image_t * p_image,
9980 opj_event_mgr_t * p_manager)
9987 p_j2k->m_output_image = opj_image_create0();
9988 if (! (p_j2k->m_output_image)) {
9991 opj_copy_image_header(p_image, p_j2k->m_output_image);
9993 /* customization of the decoding */
9994 opj_j2k_setup_decoding(p_j2k);
9996 /* Decode the codestream */
9997 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9998 opj_image_destroy(p_j2k->m_private_image);
9999 p_j2k->m_private_image = NULL;
10003 /* Move data and copy one information from codec to output image*/
10004 for (compno = 0; compno < p_image->numcomps; compno++) {
10005 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10006 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10007 p_j2k->m_output_image->comps[compno].data = NULL;
10015 * Get the decoded tile.
10017 * @param p_j2k the jpeg2000 codestream codec.
10018 * @param p_stream input_stream
10019 * @param p_image output image. .
10020 * @param p_manager the user event manager
10021 * @param tile_index index of the tile we want decode
10023 * @return true if succeed.
10025 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
10026 opj_stream_private_t *p_stream,
10027 opj_image_t* p_image,
10028 opj_event_mgr_t * p_manager,
10029 OPJ_UINT32 tile_index )
10032 OPJ_UINT32 l_tile_x, l_tile_y;
10033 opj_image_comp_t* l_img_comp;
10036 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10040 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10041 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);
10045 /* Compute the dimension of the desired tile*/
10046 l_tile_x = tile_index % p_j2k->m_cp.tw;
10047 l_tile_y = tile_index / p_j2k->m_cp.tw;
10049 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10050 if (p_image->x0 < p_j2k->m_private_image->x0)
10051 p_image->x0 = p_j2k->m_private_image->x0;
10052 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10053 if (p_image->x1 > p_j2k->m_private_image->x1)
10054 p_image->x1 = p_j2k->m_private_image->x1;
10056 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10057 if (p_image->y0 < p_j2k->m_private_image->y0)
10058 p_image->y0 = p_j2k->m_private_image->y0;
10059 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10060 if (p_image->y1 > p_j2k->m_private_image->y1)
10061 p_image->y1 = p_j2k->m_private_image->y1;
10063 l_img_comp = p_image->comps;
10064 for (compno=0; compno < p_image->numcomps; ++compno)
10066 OPJ_INT32 l_comp_x1, l_comp_y1;
10068 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10070 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10071 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10072 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10073 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10075 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10076 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10081 /* Destroy the previous output image*/
10082 if (p_j2k->m_output_image)
10083 opj_image_destroy(p_j2k->m_output_image);
10085 /* Create the ouput image from the information previously computed*/
10086 p_j2k->m_output_image = opj_image_create0();
10087 if (! (p_j2k->m_output_image)) {
10090 opj_copy_image_header(p_image, p_j2k->m_output_image);
10092 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10094 /* customization of the decoding */
10095 opj_j2k_setup_decoding_tile(p_j2k);
10097 /* Decode the codestream */
10098 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10099 opj_image_destroy(p_j2k->m_private_image);
10100 p_j2k->m_private_image = NULL;
10104 /* Move data and copy one information from codec to output image*/
10105 for (compno = 0; compno < p_image->numcomps; compno++) {
10106 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10108 if (p_image->comps[compno].data)
10109 opj_free(p_image->comps[compno].data);
10111 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10113 p_j2k->m_output_image->comps[compno].data = NULL;
10119 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
10120 OPJ_UINT32 res_factor,
10121 opj_event_mgr_t * p_manager)
10123 OPJ_UINT32 it_comp;
10125 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10127 if (p_j2k->m_private_image) {
10128 if (p_j2k->m_private_image->comps) {
10129 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10130 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10131 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10132 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10133 if ( res_factor >= max_res){
10134 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10137 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10150 * Encodes all the tiles in a row.
10152 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10153 opj_stream_private_t *p_stream,
10154 opj_event_mgr_t * p_manager )
10157 OPJ_UINT32 l_nb_tiles;
10158 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10159 OPJ_BYTE * l_current_data;
10161 /* preconditions */
10162 assert(p_j2k != 00);
10163 assert(p_stream != 00);
10164 assert(p_manager != 00);
10166 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10167 if (! l_current_data) {
10170 l_max_tile_size = 1000;
10172 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10173 for (i=0;i<l_nb_tiles;++i) {
10174 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10175 opj_free(l_current_data);
10179 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10180 if (l_current_tile_size > l_max_tile_size) {
10181 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10182 if (! l_current_data) {
10185 l_max_tile_size = l_current_tile_size;
10188 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10190 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10195 opj_free(l_current_data);
10200 * Ends the compression procedures and possibility add data to be read after the
10203 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10204 opj_stream_private_t *p_stream,
10205 opj_event_mgr_t * p_manager)
10207 /* customization of the encoding */
10208 opj_j2k_setup_end_compress(p_j2k);
10210 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10220 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10222 * @param p_j2k the jpeg2000 codec.
10223 * @param p_stream the stream object.
10224 * @param p_manager the user event manager.
10226 * @return true if the codec is valid.
10228 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10229 opj_stream_private_t *p_stream,
10230 opj_image_t * p_image,
10231 opj_event_mgr_t * p_manager)
10233 /* preconditions */
10234 assert(p_j2k != 00);
10235 assert(p_stream != 00);
10236 assert(p_manager != 00);
10238 p_j2k->m_private_image = opj_image_create0();
10239 opj_copy_image_header(p_image, p_j2k->m_private_image);
10241 // TODO_MSD: Find a better way
10242 if (p_image->comps) {
10243 OPJ_UINT32 it_comp;
10244 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10245 if (p_image->comps[it_comp].data) {
10246 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10247 p_image->comps[it_comp].data = NULL;
10253 /* customization of the validation */
10254 opj_j2k_setup_encoding_validation (p_j2k);
10256 /* validation of the parameters codec */
10257 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10261 /* customization of the encoding */
10262 opj_j2k_setup_header_writting(p_j2k);
10265 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10275 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10276 OPJ_UINT32 p_tile_index,
10277 opj_stream_private_t *p_stream,
10278 opj_event_mgr_t * p_manager )
10281 if (p_tile_index != p_j2k->m_current_tile_number) {
10282 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10286 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);
10288 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10289 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10290 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10292 /* initialisation before tile encoding */
10293 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10303 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10305 OPJ_UINT32 i,j,k = 0;
10306 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10307 opj_image_comp_t * l_img_comp = 00;
10308 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10309 opj_image_t * l_image = 00;
10310 OPJ_UINT32 l_size_comp, l_remaining;
10311 OPJ_INT32 * l_src_ptr;
10312 l_tilec = p_tcd->tcd_image->tiles->comps;
10313 l_image = p_tcd->image;
10314 l_img_comp = l_image->comps;
10316 for (i=0;i<p_tcd->image->numcomps;++i) {
10317 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10318 l_remaining = l_img_comp->prec & 7; /* (%8) */
10323 if (l_size_comp == 3) {
10327 l_width = (l_tilec->x1 - l_tilec->x0);
10328 l_height = (l_tilec->y1 - l_tilec->y0);
10329 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10330 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10331 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10332 l_stride = l_image_width - l_width;
10333 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10335 switch (l_size_comp) {
10338 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10339 if (l_img_comp->sgnd) {
10340 for (j=0;j<l_height;++j) {
10341 for (k=0;k<l_width;++k) {
10342 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10346 l_src_ptr += l_stride;
10350 for (j=0;j<l_height;++j) {
10351 for (k=0;k<l_width;++k) {
10352 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10356 l_src_ptr += l_stride;
10360 p_data = (OPJ_BYTE*) l_dest_ptr;
10365 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10366 if (l_img_comp->sgnd) {
10367 for (j=0;j<l_height;++j) {
10368 for (k=0;k<l_width;++k) {
10369 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10371 l_src_ptr += l_stride;
10375 for (j=0;j<l_height;++j) {
10376 for (k=0;k<l_width;++k) {
10377 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10379 l_src_ptr += l_stride;
10383 p_data = (OPJ_BYTE*) l_dest_ptr;
10388 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10389 for (j=0;j<l_height;++j) {
10390 for (k=0;k<l_width;++k) {
10391 *(l_dest_ptr++) = *(l_src_ptr++);
10393 l_src_ptr += l_stride;
10396 p_data = (OPJ_BYTE*) l_dest_ptr;
10409 * @param p_j2k the jpeg2000 codec.
10410 * @param p_stream the stream to write data to.
10411 * @param p_manager the user event manager.
10413 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10415 OPJ_UINT32 p_data_size,
10416 opj_stream_private_t *p_stream,
10417 opj_event_mgr_t * p_manager )
10419 opj_tcd_v2_t * l_tcd = 00;
10420 opj_cp_v2_t * l_cp = 00;
10421 opj_tcp_v2_t * l_tcp = 00;
10422 OPJ_UINT32 l_nb_bytes_written;
10423 OPJ_BYTE * l_current_data = 00;
10424 OPJ_UINT32 l_tile_size = 0;
10425 OPJ_UINT32 l_available_data;
10427 /* preconditions */
10428 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10430 l_tcd = p_j2k->m_tcd;
10431 l_cp = &(p_j2k->m_cp);
10432 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10434 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10435 l_available_data = l_tile_size;
10436 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10438 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10439 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10443 l_nb_bytes_written = 0;
10444 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10447 l_current_data += l_nb_bytes_written;
10448 l_available_data -= l_nb_bytes_written;
10450 l_nb_bytes_written = 0;
10451 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10455 l_available_data -= l_nb_bytes_written;
10456 l_nb_bytes_written = l_tile_size - l_available_data;
10458 if ( opj_stream_write_data( p_stream,
10459 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10460 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10464 ++p_j2k->m_current_tile_number;
10471 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10472 * are valid. Developers wanting to extend the library can add their own validation procedures.
10474 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10476 /* preconditions */
10477 assert(p_j2k != 00);
10479 /* DEVELOPER CORNER, insert your custom procedures */
10480 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10482 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10483 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
10486 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
10487 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
10488 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
10492 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10493 * are valid. Developers wanting to extend the library can add their own validation procedures.
10495 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
10497 /* preconditions */
10498 assert(p_j2k != 00);
10500 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10501 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10503 /* DEVELOPER CORNER, add your custom validation procedure */
10504 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10509 * Sets up the procedures to do on writing header.
10510 * Developers wanting to extend the library can add their own writing procedures.
10512 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10514 /* preconditions */
10515 assert(p_j2k != 00);
10517 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
10518 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10519 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10520 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10521 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10524 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10525 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
10526 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
10528 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10529 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
10533 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
10535 if (p_j2k->m_cp.comment != 00) {
10536 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10539 /* DEVELOPER CORNER, insert your custom procedures */
10540 if (p_j2k->m_cp.rsiz & MCT) {
10541 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
10543 /* End of Developer Corner */
10545 if (p_j2k->cstr_index) {
10546 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10549 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10550 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10554 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10556 OPJ_UINT32 * p_data_written,
10557 OPJ_UINT32 p_total_data_size,
10558 opj_stream_private_t *p_stream,
10559 struct opj_event_mgr * p_manager )
10562 OPJ_UINT32 l_nb_bytes_written = 0;
10563 OPJ_UINT32 l_current_nb_bytes_written;
10564 OPJ_BYTE * l_begin_data = 00;
10566 opj_tcp_v2_t *l_tcp = 00;
10567 opj_tcd_v2_t * l_tcd = 00;
10568 opj_cp_v2_t * l_cp = 00;
10570 l_tcd = p_j2k->m_tcd;
10571 l_cp = &(p_j2k->m_cp);
10572 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10574 l_tcd->cur_pino = 0;
10576 /*Get number of tile parts*/
10577 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10582 l_current_nb_bytes_written = 0;
10583 l_begin_data = p_data;
10584 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10589 l_nb_bytes_written += l_current_nb_bytes_written;
10590 p_data += l_current_nb_bytes_written;
10591 p_total_data_size -= l_current_nb_bytes_written;
10593 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10594 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10595 l_current_nb_bytes_written = 0;
10596 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10597 l_nb_bytes_written += l_current_nb_bytes_written;
10598 p_data += l_current_nb_bytes_written;
10599 p_total_data_size -= l_current_nb_bytes_written;
10601 l_current_nb_bytes_written = 0;
10602 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10603 l_nb_bytes_written += l_current_nb_bytes_written;
10604 p_data += l_current_nb_bytes_written;
10605 p_total_data_size -= l_current_nb_bytes_written;
10608 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10609 l_current_nb_bytes_written = 0;
10610 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10611 l_nb_bytes_written += l_current_nb_bytes_written;
10612 p_data += l_current_nb_bytes_written;
10613 p_total_data_size -= l_current_nb_bytes_written;
10617 l_current_nb_bytes_written = 0;
10618 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10622 l_nb_bytes_written += l_current_nb_bytes_written;
10623 * p_data_written = l_nb_bytes_written;
10625 /* Writing Psot in SOT marker */
10626 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10628 if (l_cp->m_specific_param.m_enc.m_cinema){
10629 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10635 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10637 OPJ_UINT32 * p_data_written,
10638 OPJ_UINT32 p_total_data_size,
10639 opj_stream_private_t *p_stream,
10640 struct opj_event_mgr * p_manager
10643 OPJ_UINT32 tilepartno=0;
10644 OPJ_UINT32 l_nb_bytes_written = 0;
10645 OPJ_UINT32 l_current_nb_bytes_written;
10646 OPJ_UINT32 l_part_tile_size;
10647 OPJ_UINT32 tot_num_tp;
10650 OPJ_BYTE * l_begin_data;
10651 opj_tcp_v2_t *l_tcp = 00;
10652 opj_tcd_v2_t * l_tcd = 00;
10653 opj_cp_v2_t * l_cp = 00;
10656 l_tcd = p_j2k->m_tcd;
10657 l_cp = &(p_j2k->m_cp);
10658 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10660 /*Get number of tile parts*/
10661 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
10663 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10664 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10665 l_current_nb_bytes_written = 0;
10666 l_part_tile_size = 0;
10667 l_begin_data = p_data;
10669 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10673 l_nb_bytes_written += l_current_nb_bytes_written;
10674 p_data += l_current_nb_bytes_written;
10675 p_total_data_size -= l_current_nb_bytes_written;
10676 l_part_tile_size += l_nb_bytes_written;
10678 l_current_nb_bytes_written = 0;
10679 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10683 p_data += l_current_nb_bytes_written;
10684 l_nb_bytes_written += l_current_nb_bytes_written;
10685 p_total_data_size -= l_current_nb_bytes_written;
10686 l_part_tile_size += l_nb_bytes_written;
10688 /* Writing Psot in SOT marker */
10689 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10691 if (l_cp->m_specific_param.m_enc.m_cinema) {
10692 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10695 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10698 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10699 l_tcd->cur_pino = pino;
10701 /*Get number of tile parts*/
10702 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
10703 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10704 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10705 l_current_nb_bytes_written = 0;
10706 l_part_tile_size = 0;
10707 l_begin_data = p_data;
10709 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10713 l_nb_bytes_written += l_current_nb_bytes_written;
10714 p_data += l_current_nb_bytes_written;
10715 p_total_data_size -= l_current_nb_bytes_written;
10716 l_part_tile_size += l_current_nb_bytes_written;
10718 l_current_nb_bytes_written = 0;
10720 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10724 l_nb_bytes_written += l_current_nb_bytes_written;
10725 p_data += l_current_nb_bytes_written;
10726 p_total_data_size -= l_current_nb_bytes_written;
10727 l_part_tile_size += l_current_nb_bytes_written;
10729 /* Writing Psot in SOT marker */
10730 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10732 if (l_cp->m_specific_param.m_enc.m_cinema) {
10733 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10736 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10740 *p_data_written = l_nb_bytes_written;
10746 * Writes the updated tlm.
10748 * @param p_stream the stream to write data to.
10749 * @param p_j2k J2K codec.
10750 * @param p_manager the user event manager.
10752 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10753 struct opj_stream_private *p_stream,
10754 struct opj_event_mgr * p_manager )
10756 OPJ_UINT32 l_tlm_size;
10757 OPJ_SIZE_T l_tlm_position, l_current_position;
10759 /* preconditions */
10760 assert(p_j2k != 00);
10761 assert(p_manager != 00);
10762 assert(p_stream != 00);
10764 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10765 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10766 l_current_position = opj_stream_tell(p_stream);
10768 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10772 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) {
10776 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10785 * Ends the encoding, i.e. frees memory.
10787 * @param p_stream the stream to write data to.
10788 * @param p_j2k J2K codec.
10789 * @param p_manager the user event manager.
10791 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10792 struct opj_stream_private *p_stream,
10793 struct opj_event_mgr * p_manager )
10795 /* preconditions */
10796 assert(p_j2k != 00);
10797 assert(p_manager != 00);
10798 assert(p_stream != 00);
10800 tcd_destroy_v2(p_j2k->m_tcd);
10803 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10804 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10805 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10806 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10809 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10810 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10811 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10814 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10820 * Destroys the memory associated with the decoding of headers.
10822 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10823 opj_stream_private_t *p_stream,
10824 opj_event_mgr_t * p_manager
10827 /* preconditions */
10828 assert(p_j2k != 00);
10829 assert(p_stream != 00);
10830 assert(p_manager != 00);
10832 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10833 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10834 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10837 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10846 * @param p_stream the stream to write data to.
10847 * @param p_j2k J2K codec.
10848 * @param p_manager the user event manager.
10850 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
10851 struct opj_stream_private *p_stream,
10852 struct opj_event_mgr * p_manager )
10854 opj_codestream_info_t * l_cstr_info = 00;
10856 /* preconditions */
10857 assert(p_j2k != 00);
10858 assert(p_manager != 00);
10859 assert(p_stream != 00);
10862 /* TODO mergeV2: check this part which use cstr_info */
10863 /*l_cstr_info = p_j2k->cstr_info;
10867 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));
10869 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10870 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10872 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10874 l_cstr_info->tw = p_j2k->m_cp.tw;
10875 l_cstr_info->th = p_j2k->m_cp.th;
10877 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10878 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10879 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10880 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10882 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10884 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10886 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10888 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10889 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10892 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10894 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10896 /*l_cstr_info->maxmarknum = 100;
10897 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10898 l_cstr_info->marknum = 0;
10901 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);
10905 * Creates a tile-coder decoder.
10907 * @param p_stream the stream to write data to.
10908 * @param p_j2k J2K codec.
10909 * @param p_manager the user event manager.
10911 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10912 opj_stream_private_t *p_stream,
10913 opj_event_mgr_t * p_manager
10916 /* preconditions */
10917 assert(p_j2k != 00);
10918 assert(p_manager != 00);
10919 assert(p_stream != 00);
10921 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10923 if (! p_j2k->m_tcd) {
10924 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10928 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10929 tcd_destroy_v2(p_j2k->m_tcd);
10940 * @param p_j2k the jpeg2000 codec.
10941 * @param p_stream the stream to write data to.
10942 * @param p_manager the user event manager.
10944 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10945 OPJ_UINT32 p_tile_index,
10947 OPJ_UINT32 p_data_size,
10948 opj_stream_private_t *p_stream,
10949 opj_event_mgr_t * p_manager )
10951 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10952 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10956 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10957 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);