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 opj_event_mgr_t * 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 opj_stream_private_t *p_stream,
370 opj_event_mgr_t * 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 opj_stream_private_t *p_stream,
668 opj_event_mgr_t * 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 opj_event_mgr_t * p_manager );
721 * Reads a PPT marker (Packed packet headers, tile-part header)
723 * @param p_header_data the data contained in the PPT box.
724 * @param p_j2k the jpeg2000 codec.
725 * @param p_header_size the size of the data contained in the PPT marker.
726 * @param p_manager the user event manager.
728 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
729 OPJ_BYTE * p_header_data,
730 OPJ_UINT32 p_header_size,
731 opj_event_mgr_t * p_manager );
733 * Writes the TLM marker (Tile Length Marker)
735 * @param p_stream the stream to write data to.
736 * @param p_j2k J2K codec.
737 * @param p_manager the user event manager.
739 static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
740 opj_stream_private_t *p_stream,
741 opj_event_mgr_t * p_manager );
744 * Writes the SOT marker (Start of tile-part)
746 * @param p_stream the stream to write data to.
747 * @param p_j2k J2K codec.
748 * @param p_manager the user event manager.
750 static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
752 OPJ_UINT32 * p_data_written,
753 const opj_stream_private_t *p_stream,
754 opj_event_mgr_t * p_manager );
757 * Reads a PPT marker (Packed packet headers, tile-part header)
759 * @param p_header_data the data contained in the PPT box.
760 * @param p_j2k the jpeg2000 codec.
761 * @param p_header_size the size of the data contained in the PPT marker.
762 * @param p_manager the user event manager.
764 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
765 OPJ_BYTE * p_header_data,
766 OPJ_UINT32 p_header_size,
767 opj_event_mgr_t * p_manager );
769 * Writes the SOD marker (Start of data)
771 * @param p_stream the stream to write data to.
772 * @param p_j2k J2K codec.
773 * @param p_manager the user event manager.
775 static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
776 opj_tcd_v2_t * p_tile_coder,
778 OPJ_UINT32 * p_data_written,
779 OPJ_UINT32 p_total_data_size,
780 const opj_stream_private_t *p_stream,
781 opj_event_mgr_t * p_manager );
784 * Reads a SOD marker (Start Of Data)
786 * @param p_header_data the data contained in the SOD box.
787 * @param p_j2k the jpeg2000 codec.
788 * @param p_header_size the size of the data contained in the SOD marker.
789 * @param p_manager the user event manager.
791 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
792 opj_stream_private_t *p_stream,
793 opj_event_mgr_t * p_manager );
796 * Updates the Tile Length Marker.
798 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
800 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
801 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
803 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
804 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
808 * Writes the RGN marker (Region Of Interest)
810 * @param p_tile_no the tile to output
811 * @param p_comp_no the component to output
812 * @param p_stream the stream to write data to.
813 * @param p_j2k J2K codec.
814 * @param p_manager the user event manager.
816 static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k,
817 OPJ_UINT32 p_tile_no,
818 OPJ_UINT32 p_comp_no,
819 opj_stream_private_t *p_stream,
820 opj_event_mgr_t * p_manager );
823 * Reads a RGN marker (Region Of Interest)
825 * @param p_header_data the data contained in the POC box.
826 * @param p_j2k the jpeg2000 codec.
827 * @param p_header_size the size of the data contained in the POC marker.
828 * @param p_manager the user event manager.
830 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
831 OPJ_BYTE * p_header_data,
832 OPJ_UINT32 p_header_size,
833 opj_event_mgr_t * p_manager );
836 * Writes the EOC marker (End of Codestream)
838 * @param p_stream the stream to write data to.
839 * @param p_j2k J2K codec.
840 * @param p_manager the user event manager.
842 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
843 opj_stream_private_t *p_stream,
844 opj_event_mgr_t * p_manager );
847 Write the EOC marker (end of codestream)
848 @param j2k J2K handle
850 static void j2k_write_eoc(opj_j2k_t *j2k);
852 Read the EOC marker (end of codestream)
853 @param j2k J2K handle
855 static void j2k_read_eoc(opj_j2k_t *j2k);
858 * Reads a EOC marker (End Of Codestream)
860 * @param p_header_data the data contained in the SOD box.
861 * @param p_j2k the jpeg2000 codec.
862 * @param p_header_size the size of the data contained in the SOD marker.
863 * @param p_manager the user event manager.
866 static opj_bool j2k_read_eoc_v2 (
868 struct opj_stream_private *p_stream,
869 struct opj_event_mgr * p_manager
876 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
878 * @param p_stream the stream to write data to.
879 * @param p_j2k J2K codec.
880 * @param p_manager the user event manager.
882 static opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
883 opj_stream_private_t *p_stream,
884 opj_event_mgr_t * p_manager );
889 * @param p_stream the stream to write data to.
890 * @param p_j2k J2K codec.
891 * @param p_manager the user event manager.
893 static opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
894 opj_stream_private_t *p_stream,
895 opj_event_mgr_t * p_manager );
898 Add main header marker information
899 @param cstr_info Codestream information structure
900 @param type marker type
901 @param pos byte offset of marker segment
902 @param len length of marker segment
904 static void opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
906 Add tile header marker information
907 @param tileno tile index number
908 @param cstr_info Codestream information structure
909 @param type marker type
910 @param pos byte offset of marker segment
911 @param len length of marker segment
913 static void opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
916 * Reads an unknown marker
918 * @param p_stream the stream object to read from.
919 * @param p_j2k the jpeg2000 codec.
920 * @param p_manager the user event manager.
922 * @return true if the marker could be deduced.
924 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
925 opj_stream_private_t *p_stream,
926 OPJ_UINT32 *output_marker,
927 opj_event_mgr_t * p_manager );
930 * Writes the MCT marker (Multiple Component Transform)
932 * @param p_stream the stream to write data to.
933 * @param p_j2k J2K codec.
934 * @param p_manager the user event manager.
936 static opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
937 opj_mct_data_t * p_mct_record,
938 opj_stream_private_t *p_stream,
939 opj_event_mgr_t * p_manager );
942 * Reads a MCT marker (Multiple Component Transform)
944 * @param p_header_data the data contained in the MCT box.
945 * @param p_j2k the jpeg2000 codec.
946 * @param p_header_size the size of the data contained in the MCT marker.
947 * @param p_manager the user event manager.
949 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
950 OPJ_BYTE * p_header_data,
951 OPJ_UINT32 p_header_size,
952 opj_event_mgr_t * p_manager );
955 * Writes the MCC marker (Multiple Component Collection)
957 * @param p_stream the stream to write data to.
958 * @param p_j2k J2K codec.
959 * @param p_manager the user event manager.
961 static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
962 opj_simple_mcc_decorrelation_data_t * p_mcc_record,
963 opj_stream_private_t *p_stream,
964 opj_event_mgr_t * p_manager );
968 * Reads a MCC marker (Multiple Component Collection)
970 * @param p_header_data the data contained in the MCC box.
971 * @param p_j2k the jpeg2000 codec.
972 * @param p_header_size the size of the data contained in the MCC marker.
973 * @param p_manager the user event manager.
975 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
976 OPJ_BYTE * p_header_data,
977 OPJ_UINT32 p_header_size,
978 opj_event_mgr_t * p_manager );
981 * Writes the MCO marker (Multiple component transformation ordering)
983 * @param p_stream the stream to write data to.
984 * @param p_j2k J2K codec.
985 * @param p_manager the user event manager.
987 static opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
988 opj_stream_private_t *p_stream,
989 opj_event_mgr_t * p_manager );
992 * Reads a MCO marker (Multiple Component Transform Ordering)
994 * @param p_header_data the data contained in the MCO box.
995 * @param p_j2k the jpeg2000 codec.
996 * @param p_header_size the size of the data contained in the MCO marker.
997 * @param p_manager the user event manager.
999 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1000 OPJ_BYTE * p_header_data,
1001 OPJ_UINT32 p_header_size,
1002 opj_event_mgr_t * p_manager );
1004 static opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1006 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1007 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1008 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1009 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1011 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1012 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1013 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1014 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1016 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1017 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1018 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1019 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1023 * Ends the encoding, i.e. frees memory.
1025 * @param p_stream the stream to write data to.
1026 * @param p_j2k J2K codec.
1027 * @param p_manager the user event manager.
1029 static opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1030 opj_stream_private_t *p_stream,
1031 opj_event_mgr_t * p_manager );
1034 * Writes the CBD marker (Component bit depth definition)
1036 * @param p_stream the stream to write data to.
1037 * @param p_j2k J2K codec.
1038 * @param p_manager the user event manager.
1040 static opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1041 opj_stream_private_t *p_stream,
1042 opj_event_mgr_t * p_manager );
1045 * Reads a CBD marker (Component bit depth definition)
1046 * @param p_header_data the data contained in the CBD box.
1047 * @param p_j2k the jpeg2000 codec.
1048 * @param p_header_size the size of the data contained in the CBD marker.
1049 * @param p_manager the user event manager.
1051 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1052 OPJ_BYTE * p_header_data,
1053 OPJ_UINT32 p_header_size,
1054 opj_event_mgr_t * p_manager);
1057 * Writes the image components.
1059 * @param p_stream the stream to write data to.
1060 * @param p_j2k J2K codec.
1061 * @param p_manager the user event manager.
1063 static opj_bool opj_j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1064 opj_stream_private_t *p_stream,
1065 opj_event_mgr_t * p_manager );
1068 * Writes regions of interests.
1070 * @param p_stream the stream to write data to.
1071 * @param p_j2k J2K codec.
1072 * @param p_manager the user event manager.
1074 static opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
1075 opj_stream_private_t *p_stream,
1076 opj_event_mgr_t * p_manager );
1081 * @param p_stream the stream to write data to.
1082 * @param p_j2k J2K codec.
1083 * @param p_manager the user event manager.
1085 static opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k,
1086 opj_stream_private_t *p_stream,
1087 opj_event_mgr_t * p_manager );
1090 * Checks the progression order changes values. Tells of the poc given as input are valid.
1091 * A nice message is outputted at errors.
1093 * @param p_pocs the progression order changes.
1094 * @param p_nb_pocs the number of progression order changes.
1095 * @param p_nb_resolutions the number of resolutions.
1096 * @param numcomps the number of components
1097 * @param numlayers the number of layers.
1099 * @return true if the pocs are valid.
1101 static opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1102 OPJ_UINT32 p_nb_pocs,
1103 OPJ_UINT32 p_nb_resolutions,
1104 OPJ_UINT32 numcomps,
1105 OPJ_UINT32 numlayers,
1106 opj_event_mgr_t * p_manager);
1109 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1111 * @param cp the coding parameters.
1112 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1113 * @param tileno the given tile.
1115 * @return the number of tile parts.
1117 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1120 * Calculates the total number of tile parts needed by the encoder to
1121 * encode such an image. If not enough memory is available, then the function return false.
1123 * @param p_nb_tiles pointer that will hold the number of tile parts.
1124 * @param cp the coding parameters for the image.
1125 * @param image the image to encode.
1126 * @param p_j2k the p_j2k encoder.
1127 * @param p_manager the user event manager.
1129 * @return true if the function was successful, false else.
1131 static opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1133 OPJ_UINT32 * p_nb_tiles,
1135 opj_event_mgr_t * p_manager);
1137 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1139 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1141 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1143 static OPJ_FLOAT32 opj_get_tp_stride (opj_tcp_v2_t * p_tcp);
1145 static OPJ_FLOAT32 opj_get_default_stride (opj_tcp_v2_t * p_tcp);
1151 /* ----------------------------------------------------------------------- */
1152 typedef struct j2k_prog_order{
1153 OPJ_PROG_ORDER enum_prog;
1157 j2k_prog_order_t j2k_prog_order_list[] = {
1163 {(OPJ_PROG_ORDER)-1, ""}
1171 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1179 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1181 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1183 opj_j2k_read_int16_to_float,
1184 opj_j2k_read_int32_to_float,
1185 opj_j2k_read_float32_to_float,
1186 opj_j2k_read_float64_to_float
1189 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1191 opj_j2k_read_int16_to_int32,
1192 opj_j2k_read_int32_to_int32,
1193 opj_j2k_read_float32_to_int32,
1194 opj_j2k_read_float64_to_int32
1197 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1199 opj_j2k_write_float_to_int16,
1200 opj_j2k_write_float_to_int32,
1201 opj_j2k_write_float_to_float,
1202 opj_j2k_write_float_to_float64
1205 typedef struct opj_dec_memory_marker_handler
1209 /** value of the state when the marker can appear */
1211 /** action linked to the marker */
1212 opj_bool (*handler) ( opj_j2k_v2_t *p_j2k,
1213 OPJ_BYTE * p_header_data,
1214 OPJ_UINT32 p_header_size,
1215 opj_event_mgr_t * p_manager );
1217 opj_dec_memory_marker_handler_t;
1219 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1221 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1222 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1223 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1224 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1225 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1226 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1227 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1228 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1229 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1230 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1231 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1232 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1233 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1235 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1236 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1237 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1238 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1239 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1240 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1242 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1243 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1244 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1245 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1246 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1248 #endif /* USE_JPWL */
1250 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1251 {J2K_MS_INSEC, 0, j2k_read_insec}
1252 #endif /* USE_JPSEC */
1253 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1256 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1258 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1259 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1263 for (i=0;i<p_nb_elem;++i) {
1264 opj_read_bytes(l_src_data,&l_temp,2);
1266 l_src_data+=sizeof(OPJ_INT16);
1268 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1272 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1274 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1275 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1279 for (i=0;i<p_nb_elem;++i) {
1280 opj_read_bytes(l_src_data,&l_temp,4);
1282 l_src_data+=sizeof(OPJ_INT32);
1284 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1288 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1290 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1291 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1295 for (i=0;i<p_nb_elem;++i) {
1296 opj_read_float(l_src_data,&l_temp);
1298 l_src_data+=sizeof(OPJ_FLOAT32);
1300 *(l_dest_data++) = l_temp;
1304 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1306 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1307 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1311 for (i=0;i<p_nb_elem;++i) {
1312 opj_read_double(l_src_data,&l_temp);
1314 l_src_data+=sizeof(OPJ_FLOAT64);
1316 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1320 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1322 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1323 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1327 for (i=0;i<p_nb_elem;++i) {
1328 opj_read_bytes(l_src_data,&l_temp,2);
1330 l_src_data+=sizeof(OPJ_INT16);
1332 *(l_dest_data++) = (OPJ_INT32) l_temp;
1336 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1338 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1339 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1343 for (i=0;i<p_nb_elem;++i) {
1344 opj_read_bytes(l_src_data,&l_temp,4);
1346 l_src_data+=sizeof(OPJ_INT32);
1348 *(l_dest_data++) = (OPJ_INT32) l_temp;
1352 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1354 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1355 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1359 for (i=0;i<p_nb_elem;++i) {
1360 opj_read_float(l_src_data,&l_temp);
1362 l_src_data+=sizeof(OPJ_FLOAT32);
1364 *(l_dest_data++) = (OPJ_INT32) l_temp;
1368 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1370 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1371 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1375 for (i=0;i<p_nb_elem;++i) {
1376 opj_read_double(l_src_data,&l_temp);
1378 l_src_data+=sizeof(OPJ_FLOAT64);
1380 *(l_dest_data++) = (OPJ_INT32) l_temp;
1384 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1386 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1387 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1391 for (i=0;i<p_nb_elem;++i) {
1392 l_temp = (OPJ_UINT32) *(l_src_data++);
1394 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1396 l_dest_data+=sizeof(OPJ_INT16);
1400 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1402 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1403 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1407 for (i=0;i<p_nb_elem;++i) {
1408 l_temp = (OPJ_UINT32) *(l_src_data++);
1410 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1412 l_dest_data+=sizeof(OPJ_INT32);
1416 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1418 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1419 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1423 for (i=0;i<p_nb_elem;++i) {
1424 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1426 opj_write_float(l_dest_data,l_temp);
1428 l_dest_data+=sizeof(OPJ_FLOAT32);
1432 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1434 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1435 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1439 for (i=0;i<p_nb_elem;++i) {
1440 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1442 opj_write_double(l_dest_data,l_temp);
1444 l_dest_data+=sizeof(OPJ_FLOAT64);
1450 * Converts an enum type progression order to string type.
1452 * @param prg_order the progression order to get.
1454 * @return the string representation of the given progression order.
1456 char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1457 j2k_prog_order_t *po;
1458 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1459 if(po->enum_prog == prg_order){
1460 return po->str_prog;
1463 return po->str_prog;
1467 * Checks the progression order changes values. Tells if the poc given as input are valid.
1469 * @param p_pocs the progression order changes.
1470 * @param p_nb_pocs the number of progression order changes.
1471 * @param p_nb_resolutions the number of resolutions.
1472 * @param numcomps the number of components
1473 * @param numlayers the number of layers.
1474 * @param p_manager the user event manager.
1476 * @return true if the pocs are valid.
1478 opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1479 OPJ_UINT32 p_nb_pocs,
1480 OPJ_UINT32 p_nb_resolutions,
1481 OPJ_UINT32 p_num_comps,
1482 OPJ_UINT32 p_num_layers,
1483 opj_event_mgr_t * p_manager)
1485 OPJ_UINT32* packet_array;
1486 OPJ_UINT32 index , resno, compno, layno;
1488 OPJ_UINT32 step_c = 1;
1489 OPJ_UINT32 step_r = p_num_comps * step_c;
1490 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1491 opj_bool loss = OPJ_FALSE;
1492 OPJ_UINT32 layno0 = 0;
1494 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1495 if (packet_array == 00) {
1496 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1499 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1501 if (p_nb_pocs == 0) {
1505 index = step_r * p_pocs->resno0;
1506 // take each resolution for each poc
1507 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1509 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1511 // take each comp of each resolution for each poc
1512 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1513 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1515 // and finally take each layer of each res of ...
1516 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1517 //index = step_r * resno + step_c * compno + step_l * layno;
1518 packet_array[comp_index] = 1;
1519 comp_index += step_l;
1522 res_index += step_c;
1529 // iterate through all the pocs
1530 for (i = 1; i < p_nb_pocs ; ++i) {
1531 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1533 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1534 index = step_r * p_pocs->resno0;
1536 // take each resolution for each poc
1537 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1538 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1540 // take each comp of each resolution for each poc
1541 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1542 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1544 // and finally take each layer of each res of ...
1545 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1546 //index = step_r * resno + step_c * compno + step_l * layno;
1547 packet_array[comp_index] = 1;
1548 comp_index += step_l;
1551 res_index += step_c;
1561 for (layno = 0; layno < p_num_layers ; ++layno) {
1562 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1563 for (compno = 0; compno < p_num_comps; ++compno) {
1564 loss |= (packet_array[index]!=1);
1565 //index = step_r * resno + step_c * compno + step_l * layno;
1572 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1575 opj_free(packet_array);
1580 /* ----------------------------------------------------------------------- */
1583 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1585 * @param cp the coding parameters.
1586 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1587 * @param tileno the given tile.
1589 * @return the number of tile parts.
1591 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1593 const OPJ_CHAR *prog = 00;
1595 OPJ_UINT32 tpnum = 1;
1596 opj_tcp_v2_t *tcp = 00;
1597 opj_poc_t * l_current_poc = 00;
1600 assert(tileno < (cp->tw * cp->th));
1601 assert(pino < (cp->tcps[tileno].numpocs + 1));
1603 /* get the given tile coding parameter */
1604 tcp = &cp->tcps[tileno];
1607 l_current_poc = &(tcp->pocs[pino]);
1608 assert(l_current_poc != 0);
1610 /* get the progression order as a character string */
1611 prog = opj_j2k_convert_progression_order(tcp->prg);
1612 assert(strlen(prog) > 0);
1614 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1618 /* component wise */
1620 tpnum *= l_current_poc->compE;
1622 /* resolution wise */
1624 tpnum *= l_current_poc->resE;
1628 tpnum *= l_current_poc->prcE;
1632 tpnum *= l_current_poc->layE;
1635 /* whould we split here ? */
1636 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1637 cp->m_specific_param.m_enc.m_tp_pos=i;
1650 * Calculates the total number of tile parts needed by the encoder to
1651 * encode such an image. If not enough memory is available, then the function return false.
1653 * @param p_nb_tiles pointer that will hold the number of tile parts.
1654 * @param cp the coding parameters for the image.
1655 * @param image the image to encode.
1656 * @param p_j2k the p_j2k encoder.
1657 * @param p_manager the user event manager.
1659 * @return true if the function was successful, false else.
1661 opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1663 OPJ_UINT32 * p_nb_tiles,
1665 opj_event_mgr_t * p_manager
1668 OPJ_UINT32 pino,tileno;
1669 OPJ_UINT32 l_nb_tiles;
1673 assert(p_nb_tiles != 00);
1675 assert(image != 00);
1676 assert(p_j2k != 00);
1677 assert(p_manager != 00);
1679 l_nb_tiles = cp->tw * cp->th;
1684 /* TODO mergeV2: check this part which use cstr_info */
1685 /*if (p_j2k->cstr_info) {
1686 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1688 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1689 OPJ_UINT32 cur_totnum_tp = 0;
1691 pi_update_encoding_parameters(image,cp,tileno);
1693 for (pino = 0; pino <= tcp->numpocs; ++pino)
1695 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1697 *p_nb_tiles = *p_nb_tiles + tp_num;
1699 cur_totnum_tp += tp_num;
1702 tcp->m_nb_tile_parts = cur_totnum_tp;
1704 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1705 if (l_info_tile_ptr->tp == 00) {
1709 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1711 l_info_tile_ptr->num_tps = cur_totnum_tp;
1718 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1719 OPJ_UINT32 cur_totnum_tp = 0;
1721 pi_update_encoding_parameters(image,cp,tileno);
1723 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1724 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1726 *p_nb_tiles = *p_nb_tiles + tp_num;
1728 cur_totnum_tp += tp_num;
1730 tcp->m_nb_tile_parts = cur_totnum_tp;
1740 * Writes the SOC marker (Start Of Codestream)
1742 * @param p_stream the stream to write data to.
1743 * @param p_j2k J2K codec.
1744 * @param p_manager the user event manager.
1746 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1747 opj_stream_private_t *p_stream,
1748 opj_event_mgr_t * p_manager )
1750 /* 2 bytes will be written */
1751 OPJ_BYTE * l_start_stream = 00;
1754 assert(p_stream != 00);
1755 assert(p_j2k != 00);
1756 assert(p_manager != 00);
1758 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1760 /* write SOC identifier */
1761 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1763 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1769 /* update markers struct */
1771 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1773 assert( 0 && "TODO" );
1774 #endif /* USE_JPWL */
1783 * Reads a SOC marker (Start of Codestream)
1784 * @param p_header_data the data contained in the SOC box.
1785 * @param jp2 the jpeg2000 file codec.
1786 * @param p_header_size the size of the data contained in the SOC marker.
1787 * @param p_manager the user event manager.
1789 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1790 opj_stream_private_t *p_stream,
1791 opj_event_mgr_t * p_manager
1794 OPJ_BYTE l_data [2];
1795 OPJ_UINT32 l_marker;
1798 assert(p_j2k != 00);
1799 assert(p_manager != 00);
1800 assert(p_stream != 00);
1802 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1806 opj_read_bytes(l_data,&l_marker,2);
1807 if (l_marker != J2K_MS_SOC) {
1811 /* Next marker should be a SIZ marker in the main header */
1812 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1814 /* FIXME move it in a index structure included in p_j2k*/
1815 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1817 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1819 /* Add the marker to the codestream index*/
1820 opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1827 * Writes the SIZ marker (image and tile size)
1829 * @param p_stream the stream to write data to.
1830 * @param p_j2k J2K codec.
1831 * @param p_manager the user event manager.
1833 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1834 opj_stream_private_t *p_stream,
1835 opj_event_mgr_t * p_manager )
1838 OPJ_UINT32 l_size_len;
1839 OPJ_BYTE * l_current_ptr;
1840 opj_image_t * l_image = 00;
1841 opj_cp_v2_t *cp = 00;
1842 opj_image_comp_t * l_img_comp = 00;
1845 assert(p_stream != 00);
1846 assert(p_j2k != 00);
1847 assert(p_manager != 00);
1849 l_image = p_j2k->m_private_image;
1850 cp = &(p_j2k->m_cp);
1851 l_size_len = 40 + 3 * l_image->numcomps;
1852 l_img_comp = l_image->comps;
1854 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1856 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1857 = (OPJ_BYTE*)opj_realloc(
1858 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1860 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1864 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1867 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1869 /* write SOC identifier */
1870 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1873 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1876 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1879 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1882 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1885 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1888 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1891 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1894 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1897 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
1900 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
1903 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
1906 for (i = 0; i < l_image->numcomps; ++i) {
1907 /* TODO here with MCT ? */
1908 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
1911 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
1914 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
1920 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) {
1930 * Reads a SIZ marker (image and tile size)
1931 * @param p_header_data the data contained in the SIZ box.
1932 * @param jp2 the jpeg2000 file codec.
1933 * @param p_header_size the size of the data contained in the SIZ marker.
1934 * @param p_manager the user event manager.
1936 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
1937 OPJ_BYTE * p_header_data,
1938 OPJ_UINT32 p_header_size,
1939 opj_event_mgr_t * p_manager
1942 OPJ_UINT32 l_size, i;
1943 OPJ_UINT32 l_nb_comp;
1944 OPJ_UINT32 l_nb_comp_remain;
1945 OPJ_UINT32 l_remaining_size;
1946 OPJ_UINT32 l_nb_tiles;
1948 opj_image_t *l_image = 00;
1949 opj_cp_v2_t *l_cp = 00;
1950 opj_image_comp_t * l_img_comp = 00;
1951 opj_tcp_v2_t * l_current_tile_param = 00;
1954 assert(p_j2k != 00);
1955 assert(p_manager != 00);
1956 assert(p_header_data != 00);
1958 l_image = p_j2k->m_private_image;
1959 l_cp = &(p_j2k->m_cp);
1961 /* minimum size == 39 - 3 (= minimum component parameter) */
1962 if (p_header_size < 36) {
1963 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1967 l_remaining_size = p_header_size - 36;
1968 l_nb_comp = l_remaining_size / 3;
1969 l_nb_comp_remain = l_remaining_size % 3;
1970 if (l_nb_comp_remain != 0){
1971 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1975 l_size = p_header_size + 2; /* Lsiz */
1977 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1979 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1980 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1982 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1984 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1986 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1988 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1990 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1992 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1994 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1996 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1999 l_image->numcomps = (OPJ_UINT16) l_tmp;
2001 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2005 if (l_image->numcomps != l_nb_comp) {
2006 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);
2011 if (l_cp->correct) {
2012 /* if JPWL is on, we check whether TX errors have damaged
2013 too much the SIZ parameters */
2014 if (!(l_image->x1 * l_image->y1)) {
2015 opj_event_msg_v2(p_manager, EVT_ERROR,
2016 "JPWL: bad image size (%d x %d)\n",
2017 l_image->x1, l_image->y1);
2018 if (!JPWL_ASSUME || JPWL_ASSUME) {
2019 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2024 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2025 if (l_image->numcomps != ((len - 38) / 3)) {
2026 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2027 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2028 l_image->numcomps, ((len - 38) / 3));
2030 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2033 */ /* we try to correct */
2034 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2035 if (l_image->numcomps < ((len - 38) / 3)) {
2036 len = 38 + 3 * l_image->numcomps;
2037 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2040 l_image->numcomps = ((len - 38) / 3);
2041 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2047 /* update components number in the jpwl_exp_comps filed */
2048 l_cp->exp_comps = l_image->numcomps;
2050 #endif /* USE_JPWL */
2052 /* Allocate the resulting image components */
2053 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2054 if (l_image->comps == 00){
2055 l_image->numcomps = 0;
2056 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2060 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2061 l_img_comp = l_image->comps;
2063 /* Read the component information */
2064 for (i = 0; i < l_image->numcomps; ++i){
2066 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2068 l_img_comp->prec = (tmp & 0x7f) + 1;
2069 l_img_comp->sgnd = tmp >> 7;
2070 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2072 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2073 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2075 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2078 if (l_cp->correct) {
2079 /* if JPWL is on, we check whether TX errors have damaged
2080 too much the SIZ parameters, again */
2081 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2082 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2083 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2084 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2086 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2089 /* we try to correct */
2090 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2091 if (!l_image->comps[i].dx) {
2092 l_image->comps[i].dx = 1;
2093 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2094 i, l_image->comps[i].dx);
2096 if (!l_image->comps[i].dy) {
2097 l_image->comps[i].dy = 1;
2098 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2099 i, l_image->comps[i].dy);
2103 #endif /* USE_JPWL */
2104 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2105 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2109 /* Compute the number of tiles */
2110 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2111 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2112 l_nb_tiles = l_cp->tw * l_cp->th;
2114 /* Define the tiles which will be decoded */
2115 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2116 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;
2117 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;
2118 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);
2119 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);
2122 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2123 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2124 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2125 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2129 if (l_cp->correct) {
2130 /* if JPWL is on, we check whether TX errors have damaged
2131 too much the SIZ parameters */
2132 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2133 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2134 "JPWL: bad number of tiles (%d x %d)\n",
2135 l_cp->tw, l_cp->th);
2137 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2140 /* we try to correct */
2141 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2144 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2147 if (l_cp->tw > l_cp->max_tiles) {
2149 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2150 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2151 l_cp->max_tiles, l_cp->tw);
2155 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2158 if (l_cp->th > l_cp->max_tiles) {
2160 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2161 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2162 l_cp->max_tiles, l_cp->th);
2166 #endif /* USE_JPWL */
2168 /* memory allocations */
2169 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2170 if (l_cp->tcps == 00) {
2171 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2174 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2177 if (l_cp->correct) {
2179 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2180 "JPWL: could not alloc tcps field of cp\n");
2181 if (!JPWL_ASSUME || JPWL_ASSUME) {
2182 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2187 #endif /* USE_JPWL */
2189 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2190 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2191 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2192 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2195 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2197 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2198 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2200 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2201 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2204 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));
2205 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2207 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2208 (opj_simple_mcc_decorrelation_data_t*)
2209 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2211 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2212 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2215 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));
2216 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2218 /* set up default dc level shift */
2219 for (i=0;i<l_image->numcomps;++i) {
2220 if (! l_image->comps[i].sgnd) {
2221 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2225 l_current_tile_param = l_cp->tcps;
2226 for (i = 0; i < l_nb_tiles; ++i) {
2227 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2228 if (l_current_tile_param->tccps == 00) {
2229 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2232 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2234 ++l_current_tile_param;
2237 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2238 opj_image_comp_header_update(l_image,l_cp);
2244 * Writes the COM marker (comment)
2246 * @param p_stream the stream to write data to.
2247 * @param p_j2k J2K codec.
2248 * @param p_manager the user event manager.
2250 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2251 opj_stream_private_t *p_stream,
2252 opj_event_mgr_t * p_manager
2255 OPJ_UINT32 l_comment_size;
2256 OPJ_UINT32 l_total_com_size;
2257 const OPJ_CHAR *l_comment;
2258 OPJ_BYTE * l_current_ptr = 00;
2261 assert(p_j2k != 00);
2262 assert(p_stream != 00);
2263 assert(p_manager != 00);
2265 l_comment = p_j2k->m_cp.comment;
2266 l_comment_size = strlen(l_comment);
2267 l_total_com_size = l_comment_size + 6;
2269 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2270 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2271 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2274 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2278 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2281 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2283 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2286 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2289 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2292 memcpy( l_current_ptr,l_comment,l_comment_size);
2294 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) {
2302 * Reads a COM marker (comments)
2303 * @param p_header_data the data contained in the COM box.
2304 * @param jp2 the jpeg2000 file codec.
2305 * @param p_header_size the size of the data contained in the COM marker.
2306 * @param p_manager the user event manager.
2308 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2309 OPJ_BYTE * p_header_data,
2310 OPJ_UINT32 p_header_size,
2311 opj_event_mgr_t * p_manager
2315 assert(p_j2k != 00);
2316 assert(p_manager != 00);
2317 assert(p_header_data != 00);
2318 (void)p_header_size;
2324 * Writes the COD marker (Coding style default)
2326 * @param p_stream the stream to write data to.
2327 * @param p_j2k J2K codec.
2328 * @param p_manager the user event manager.
2330 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2331 opj_stream_private_t *p_stream,
2332 opj_event_mgr_t * p_manager )
2334 opj_cp_v2_t *l_cp = 00;
2335 opj_tcp_v2_t *l_tcp = 00;
2336 OPJ_UINT32 l_code_size,l_remaining_size;
2337 OPJ_BYTE * l_current_data = 00;
2340 assert(p_j2k != 00);
2341 assert(p_manager != 00);
2342 assert(p_stream != 00);
2344 l_cp = &(p_j2k->m_cp);
2345 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2346 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2347 l_remaining_size = l_code_size;
2349 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2350 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2351 = (OPJ_BYTE*)opj_realloc(
2352 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2355 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2359 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2362 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2364 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2365 l_current_data += 2;
2367 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2368 l_current_data += 2;
2370 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2373 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2376 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2379 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2382 l_remaining_size -= 9;
2384 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2385 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2389 if (l_remaining_size != 0) {
2390 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2394 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) {
2403 * Reads a COD marker (Coding Styke defaults)
2404 * @param p_header_data the data contained in the COD box.
2405 * @param p_j2k the jpeg2000 codec.
2406 * @param p_header_size the size of the data contained in the COD marker.
2407 * @param p_manager the user event manager.
2409 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2410 OPJ_BYTE * p_header_data,
2411 OPJ_UINT32 p_header_size,
2412 opj_event_mgr_t * p_manager
2418 opj_cp_v2_t *l_cp = 00;
2419 opj_tcp_v2_t *l_tcp = 00;
2420 opj_image_t *l_image = 00;
2423 assert(p_header_data != 00);
2424 assert(p_j2k != 00);
2425 assert(p_manager != 00);
2427 l_image = p_j2k->m_private_image;
2428 l_cp = &(p_j2k->m_cp);
2430 /* If we are in the first tile-part header of the current tile */
2431 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2432 &l_cp->tcps[p_j2k->m_current_tile_number] :
2433 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2435 /* Make sure room is sufficient */
2436 if (p_header_size < 5) {
2437 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2441 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2443 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2445 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2446 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2449 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2450 if (l_cp->m_specific_param.m_dec.m_layer) {
2451 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2454 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2457 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2461 for (i = 0; i < l_image->numcomps; ++i) {
2462 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2465 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2466 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2470 if (p_header_size != 0) {
2471 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2475 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2476 opj_j2k_copy_tile_component_parameters(p_j2k);
2479 #ifdef WIP_REMOVE_MSD
2480 if (p_j2k->cstr_info) {
2481 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2482 p_j2k->cstr_info->prog = l_tcp->prg;
2483 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2484 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2485 for (i = 0; i < l_image->numcomps; ++i) {
2486 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2495 * Writes the COC marker (Coding style component)
2497 * @param p_comp_no the index of the component to output.
2498 * @param p_stream the stream to write data to.
2499 * @param p_j2k J2K codec.
2500 * @param p_manager the user event manager.
2502 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2503 OPJ_UINT32 p_comp_no,
2504 opj_stream_private_t *p_stream,
2505 opj_event_mgr_t * p_manager )
2507 OPJ_UINT32 l_coc_size,l_remaining_size;
2508 OPJ_UINT32 l_comp_room;
2511 assert(p_j2k != 00);
2512 assert(p_manager != 00);
2513 assert(p_stream != 00);
2515 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2517 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2519 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2520 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2521 = (OPJ_BYTE*)opj_realloc(
2522 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2524 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2528 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2531 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);
2533 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) {
2541 * Writes the COC marker (Coding style component)
2543 * @param p_comp_no the index of the component to output.
2544 * @param p_stream the stream to write data to.
2545 * @param p_j2k J2K codec.
2546 * @param p_manager the user event manager.
2548 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2549 OPJ_UINT32 p_comp_no,
2551 OPJ_UINT32 * p_data_written,
2552 opj_event_mgr_t * p_manager
2555 opj_cp_v2_t *l_cp = 00;
2556 opj_tcp_v2_t *l_tcp = 00;
2557 OPJ_UINT32 l_coc_size,l_remaining_size;
2558 OPJ_BYTE * l_current_data = 00;
2559 opj_image_t *l_image = 00;
2560 OPJ_UINT32 l_comp_room;
2563 assert(p_j2k != 00);
2564 assert(p_manager != 00);
2566 l_cp = &(p_j2k->m_cp);
2567 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2568 l_image = p_j2k->m_private_image;
2569 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2571 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2572 l_remaining_size = l_coc_size;
2574 l_current_data = p_data;
2576 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2577 l_current_data += 2;
2579 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2580 l_current_data += 2;
2582 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2583 l_current_data+=l_comp_room;
2585 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2588 l_remaining_size -= (5 + l_comp_room);
2589 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2590 * p_data_written = l_coc_size;
2594 * Gets the maximum size taken by a coc.
2596 * @param p_j2k the jpeg2000 codec to use.
2598 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2601 OPJ_UINT32 l_nb_comp;
2602 OPJ_UINT32 l_nb_tiles;
2603 OPJ_UINT32 l_max = 0;
2607 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2608 l_nb_comp = p_j2k->m_private_image->numcomps;
2610 for (i=0;i<l_nb_tiles;++i) {
2611 for (j=0;j<l_nb_comp;++j) {
2612 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2621 * Reads a COC marker (Coding Style Component)
2622 * @param p_header_data the data contained in the COC box.
2623 * @param p_j2k the jpeg2000 codec.
2624 * @param p_header_size the size of the data contained in the COC marker.
2625 * @param p_manager the user event manager.
2627 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2628 OPJ_BYTE * p_header_data,
2629 OPJ_UINT32 p_header_size,
2630 opj_event_mgr_t * p_manager
2633 opj_cp_v2_t *l_cp = NULL;
2634 opj_tcp_v2_t *l_tcp = NULL;
2635 opj_image_t *l_image = NULL;
2636 OPJ_UINT32 l_comp_room;
2637 OPJ_UINT32 l_comp_no;
2640 assert(p_header_data != 00);
2641 assert(p_j2k != 00);
2642 assert(p_manager != 00);
2644 l_cp = &(p_j2k->m_cp);
2645 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2646 &l_cp->tcps[p_j2k->m_current_tile_number] :
2647 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2648 l_image = p_j2k->m_private_image;
2650 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2652 /* make sure room is sufficient*/
2653 if (p_header_size < l_comp_room + 1) {
2654 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2657 p_header_size -= l_comp_room + 1;
2659 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2660 p_header_data += l_comp_room;
2661 if (l_comp_no >= l_image->numcomps) {
2662 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2666 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2669 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2670 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2674 if (p_header_size != 0) {
2675 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2682 * Writes the QCD marker (quantization default)
2684 * @param p_comp_number the index of the component to output.
2685 * @param p_stream the stream to write data to.
2686 * @param p_j2k J2K codec.
2687 * @param p_manager the user event manager.
2689 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2690 opj_stream_private_t *p_stream,
2691 opj_event_mgr_t * p_manager
2694 opj_cp_v2_t *l_cp = 00;
2695 opj_tcp_v2_t *l_tcp = 00;
2696 OPJ_UINT32 l_qcd_size,l_remaining_size;
2697 OPJ_BYTE * l_current_data = 00;
2700 assert(p_j2k != 00);
2701 assert(p_manager != 00);
2702 assert(p_stream != 00);
2704 l_cp = &(p_j2k->m_cp);
2705 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2706 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2707 l_remaining_size = l_qcd_size;
2709 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2710 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2711 = (OPJ_BYTE*)opj_realloc(
2712 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2715 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2719 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2722 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2724 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2725 l_current_data += 2;
2727 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2728 l_current_data += 2;
2730 l_remaining_size -= 4;
2732 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2733 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2737 if (l_remaining_size != 0) {
2738 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2742 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) {
2750 * Reads a QCD marker (Quantization defaults)
2751 * @param p_header_data the data contained in the QCD box.
2752 * @param p_j2k the jpeg2000 codec.
2753 * @param p_header_size the size of the data contained in the QCD marker.
2754 * @param p_manager the user event manager.
2756 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2757 OPJ_BYTE * p_header_data,
2758 OPJ_UINT32 p_header_size,
2759 opj_event_mgr_t * p_manager
2763 assert(p_header_data != 00);
2764 assert(p_j2k != 00);
2765 assert(p_manager != 00);
2767 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2768 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2772 if (p_header_size != 0) {
2773 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2777 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2778 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2784 * Writes the QCC marker (quantization component)
2786 * @param p_comp_no the index of the component to output.
2787 * @param p_stream the stream to write data to.
2788 * @param p_j2k J2K codec.
2789 * @param p_manager the user event manager.
2791 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2792 OPJ_UINT32 p_comp_no,
2793 opj_stream_private_t *p_stream,
2794 opj_event_mgr_t * p_manager
2797 OPJ_UINT32 l_qcc_size,l_remaining_size;
2800 assert(p_j2k != 00);
2801 assert(p_manager != 00);
2802 assert(p_stream != 00);
2804 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2805 l_remaining_size = l_qcc_size;
2807 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2808 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2809 = (OPJ_BYTE*)opj_realloc(
2810 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2812 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2816 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2819 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);
2821 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) {
2829 * Writes the QCC marker (quantization component)
2831 * @param p_comp_no the index of the component to output.
2832 * @param p_stream the stream to write data to.
2833 * @param p_j2k J2K codec.
2834 * @param p_manager the user event manager.
2836 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2837 OPJ_UINT32 p_comp_no,
2839 OPJ_UINT32 * p_data_written,
2840 opj_event_mgr_t * p_manager
2843 OPJ_UINT32 l_qcc_size,l_remaining_size;
2844 OPJ_BYTE * l_current_data = 00;
2847 assert(p_j2k != 00);
2848 assert(p_manager != 00);
2850 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2851 l_remaining_size = l_qcc_size;
2853 l_current_data = p_data;
2855 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2856 l_current_data += 2;
2858 if (p_j2k->m_private_image->numcomps <= 256) {
2861 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2862 l_current_data += 2;
2864 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2867 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2868 l_remaining_size -= 6;
2871 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2872 l_current_data += 2;
2874 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2877 l_remaining_size -= 6;
2880 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2882 *p_data_written = l_qcc_size;
2886 * Gets the maximum size taken by a qcc.
2888 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
2890 return opj_j2k_get_max_coc_size(p_j2k);
2894 * Reads a QCC marker (Quantization component)
2895 * @param p_header_data the data contained in the QCC box.
2896 * @param p_j2k the jpeg2000 codec.
2897 * @param p_header_size the size of the data contained in the QCC marker.
2898 * @param p_manager the user event manager.
2900 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
2901 OPJ_BYTE * p_header_data,
2902 OPJ_UINT32 p_header_size,
2903 opj_event_mgr_t * p_manager
2906 OPJ_UINT32 l_num_comp,l_comp_no;
2909 assert(p_header_data != 00);
2910 assert(p_j2k != 00);
2911 assert(p_manager != 00);
2913 l_num_comp = p_j2k->m_private_image->numcomps;
2915 if (l_num_comp <= 256) {
2916 if (p_header_size < 1) {
2917 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2920 opj_read_bytes(p_header_data,&l_comp_no,1);
2925 if (p_header_size < 2) {
2926 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2929 opj_read_bytes(p_header_data,&l_comp_no,2);
2935 if (p_j2k->m_cp.correct) {
2937 static OPJ_UINT32 backup_compno = 0;
2939 /* compno is negative or larger than the number of components!!! */
2940 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2941 opj_event_msg_v2(p_manager, EVT_ERROR,
2942 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2943 l_comp_no, l_num_comp);
2945 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2948 /* we try to correct */
2949 l_comp_no = backup_compno % l_num_comp;
2950 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2951 "- setting component number to %d\n",
2955 /* keep your private count of tiles */
2958 #endif /* USE_JPWL */
2960 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2961 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2965 if (p_header_size != 0) {
2966 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2974 * Writes the POC marker (Progression Order Change)
2976 * @param p_stream the stream to write data to.
2977 * @param p_j2k J2K codec.
2978 * @param p_manager the user event manager.
2980 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
2981 opj_stream_private_t *p_stream,
2982 opj_event_mgr_t * p_manager
2985 OPJ_UINT32 l_nb_comp;
2986 OPJ_UINT32 l_nb_poc;
2987 OPJ_UINT32 l_poc_size;
2988 OPJ_UINT32 l_written_size = 0;
2989 opj_tcp_v2_t *l_tcp = 00;
2990 opj_tccp_t *l_tccp = 00;
2991 OPJ_UINT32 l_poc_room;
2994 assert(p_j2k != 00);
2995 assert(p_manager != 00);
2996 assert(p_stream != 00);
2998 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2999 l_tccp = &l_tcp->tccps[0];
3000 l_nb_comp = p_j2k->m_private_image->numcomps;
3001 l_nb_poc = 1 + l_tcp->numpocs;
3003 if (l_nb_comp <= 256) {
3009 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3011 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3012 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3013 = (OPJ_BYTE*)opj_realloc(
3014 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3017 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3021 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3024 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3026 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) {
3035 * Writes the POC marker (Progression Order Change)
3037 * @param p_stream the stream to write data to.
3038 * @param p_j2k J2K codec.
3039 * @param p_manager the user event manager.
3041 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3043 OPJ_UINT32 * p_data_written,
3044 opj_event_mgr_t * p_manager
3048 OPJ_BYTE * l_current_data = 00;
3049 OPJ_UINT32 l_nb_comp;
3050 OPJ_UINT32 l_nb_poc;
3051 OPJ_UINT32 l_poc_size;
3052 opj_image_t *l_image = 00;
3053 opj_tcp_v2_t *l_tcp = 00;
3054 opj_tccp_t *l_tccp = 00;
3055 opj_poc_t *l_current_poc = 00;
3056 OPJ_UINT32 l_poc_room;
3059 assert(p_j2k != 00);
3060 assert(p_manager != 00);
3062 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3063 l_tccp = &l_tcp->tccps[0];
3064 l_image = p_j2k->m_private_image;
3065 l_nb_comp = l_image->numcomps;
3066 l_nb_poc = 1 + l_tcp->numpocs;
3068 if (l_nb_comp <= 256) {
3075 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3077 l_current_data = p_data;
3079 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3080 l_current_data += 2;
3082 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3083 l_current_data += 2;
3085 l_current_poc = l_tcp->pocs;
3086 for (i = 0; i < l_nb_poc; ++i) {
3087 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3090 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3091 l_current_data+=l_poc_room;
3093 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3096 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3099 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3100 l_current_data+=l_poc_room;
3102 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3105 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3106 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3107 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3108 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3113 *p_data_written = l_poc_size;
3117 * Gets the maximum size taken by the writing of a POC.
3119 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3121 opj_tcp_v2_t * l_tcp = 00;
3122 OPJ_UINT32 l_nb_tiles = 0;
3123 OPJ_UINT32 l_max_poc = 0;
3126 l_tcp = p_j2k->m_cp.tcps;
3127 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3129 for (i=0;i<l_nb_tiles;++i) {
3130 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3136 return 4 + 9 * l_max_poc;
3140 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3142 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3145 OPJ_UINT32 l_nb_tiles;
3146 OPJ_UINT32 l_max = 0;
3147 opj_tcp_v2_t * l_tcp = 00;
3149 l_tcp = p_j2k->m_cp.tcps;
3150 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3152 for (i=0;i<l_nb_tiles;++i) {
3153 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3163 * Gets the maximum size taken by the headers of the SOT.
3165 * @param p_j2k the jpeg2000 codec to use.
3167 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3169 OPJ_UINT32 l_nb_bytes = 0;
3170 OPJ_UINT32 l_nb_comps;
3171 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3173 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3174 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3176 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3177 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3178 l_nb_bytes += l_nb_comps * l_coc_bytes;
3180 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3181 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3184 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3186 /*** DEVELOPER CORNER, Add room for your headers ***/
3193 * Reads a POC marker (Progression Order Change)
3195 * @param p_header_data the data contained in the POC box.
3196 * @param p_j2k the jpeg2000 codec.
3197 * @param p_header_size the size of the data contained in the POC marker.
3198 * @param p_manager the user event manager.
3200 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3201 OPJ_BYTE * p_header_data,
3202 OPJ_UINT32 p_header_size,
3203 opj_event_mgr_t * p_manager
3206 OPJ_UINT32 i, l_nb_comp, l_tmp;
3207 opj_image_t * l_image = 00;
3208 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3209 OPJ_UINT32 l_chunk_size, l_comp_room;
3211 opj_cp_v2_t *l_cp = 00;
3212 opj_tcp_v2_t *l_tcp = 00;
3213 opj_poc_t *l_current_poc = 00;
3216 assert(p_header_data != 00);
3217 assert(p_j2k != 00);
3218 assert(p_manager != 00);
3220 l_image = p_j2k->m_private_image;
3221 l_nb_comp = l_image->numcomps;
3222 if (l_nb_comp <= 256) {
3228 l_chunk_size = 5 + 2 * l_comp_room;
3229 l_current_poc_nb = p_header_size / l_chunk_size;
3230 l_current_poc_remaining = p_header_size % l_chunk_size;
3232 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3233 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3237 l_cp = &(p_j2k->m_cp);
3238 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3239 &l_cp->tcps[p_j2k->m_current_tile_number] :
3240 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3241 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3242 l_current_poc_nb += l_old_poc_nb;
3244 assert(l_current_poc_nb < 32);
3246 /* now poc is in use.*/
3249 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3250 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3251 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3253 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3254 p_header_data+=l_comp_room;
3255 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3257 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3259 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3260 p_header_data+=l_comp_room;
3261 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3263 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3264 /* make sure comp is in acceptable bounds */
3265 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3269 l_tcp->numpocs = l_current_poc_nb - 1;
3274 * Reads a CRG marker (Component registration)
3276 * @param p_header_data the data contained in the TLM box.
3277 * @param p_j2k the jpeg2000 codec.
3278 * @param p_header_size the size of the data contained in the TLM marker.
3279 * @param p_manager the user event manager.
3281 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3282 OPJ_BYTE * p_header_data,
3283 OPJ_UINT32 p_header_size,
3284 opj_event_mgr_t * p_manager
3287 OPJ_UINT32 l_nb_comp;
3289 assert(p_header_data != 00);
3290 assert(p_j2k != 00);
3291 assert(p_manager != 00);
3293 l_nb_comp = p_j2k->m_private_image->numcomps;
3295 if (p_header_size != l_nb_comp *4) {
3296 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3299 /* Do not care of this at the moment since only local variables are set here */
3302 (i = 0; i < l_nb_comp; ++i)
3304 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3306 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3314 * Reads a TLM marker (Tile Length Marker)
3316 * @param p_header_data the data contained in the TLM box.
3317 * @param p_j2k the jpeg2000 codec.
3318 * @param p_header_size the size of the data contained in the TLM marker.
3319 * @param p_manager the user event manager.
3321 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3322 OPJ_BYTE * p_header_data,
3323 OPJ_UINT32 p_header_size,
3324 opj_event_mgr_t * p_manager
3327 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3329 assert(p_header_data != 00);
3330 assert(p_j2k != 00);
3331 assert(p_manager != 00);
3333 if (p_header_size < 2) {
3334 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3339 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3341 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3344 l_ST = ((l_Stlm >> 4) & 0x3);
3345 l_SP = (l_Stlm >> 6) & 0x1;
3347 l_Ptlm_size = (l_SP + 1) * 2;
3348 l_quotient = l_Ptlm_size + l_ST;
3350 l_tot_num_tp = p_header_size / l_quotient;
3351 l_tot_num_tp_remaining = p_header_size % l_quotient;
3353 if (l_tot_num_tp_remaining != 0) {
3354 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3357 /* FIXME Do not care of this at the moment since only local variables are set here */
3360 (i = 0; i < l_tot_num_tp; ++i)
3362 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3363 p_header_data += l_ST;
3364 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3365 p_header_data += l_Ptlm_size;
3372 * Reads a PLM marker (Packet length, main header marker)
3374 * @param p_header_data the data contained in the TLM box.
3375 * @param p_j2k the jpeg2000 codec.
3376 * @param p_header_size the size of the data contained in the TLM marker.
3377 * @param p_manager the user event manager.
3379 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3380 OPJ_BYTE * p_header_data,
3381 OPJ_UINT32 p_header_size,
3382 opj_event_mgr_t * p_manager
3386 assert(p_header_data != 00);
3387 assert(p_j2k != 00);
3388 assert(p_manager != 00);
3390 if (p_header_size < 1) {
3391 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3394 /* Do not care of this at the moment since only local variables are set here */
3396 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3403 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3405 p_header_size -= (1+l_Nplm);
3409 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3413 (i = 0; i < l_Nplm; ++i)
3415 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3417 // take only the last seven bytes
3418 l_packet_len |= (l_tmp & 0x7f);
3426 // store packet length and proceed to next packet
3433 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3442 * Reads a PLT marker (Packet length, tile-part header)
3444 * @param p_header_data the data contained in the PLT box.
3445 * @param p_j2k the jpeg2000 codec.
3446 * @param p_header_size the size of the data contained in the PLT marker.
3447 * @param p_manager the user event manager.
3449 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3450 OPJ_BYTE * p_header_data,
3451 OPJ_UINT32 p_header_size,
3452 opj_event_mgr_t * p_manager
3455 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3458 assert(p_header_data != 00);
3459 assert(p_j2k != 00);
3460 assert(p_manager != 00);
3462 if (p_header_size < 1) {
3463 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3467 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3471 for (i = 0; i < p_header_size; ++i) {
3472 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3474 /* take only the last seven bytes */
3475 l_packet_len |= (l_tmp & 0x7f);
3480 /* store packet length and proceed to next packet */
3485 if (l_packet_len != 0) {
3486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3495 * Reads a PPM marker (Packed packet headers, main header)
3497 * @param p_header_data the data contained in the POC box.
3498 * @param p_j2k the jpeg2000 codec.
3499 * @param p_header_size the size of the data contained in the POC marker.
3500 * @param p_manager the user event manager.
3503 opj_bool j2k_read_ppm_v2 (
3504 opj_j2k_v2_t *p_j2k,
3505 OPJ_BYTE * p_header_data,
3506 OPJ_UINT32 p_header_size,
3507 struct opj_event_mgr * p_manager
3511 opj_cp_v2_t *l_cp = 00;
3512 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3515 assert(p_header_data != 00);
3516 assert(p_j2k != 00);
3517 assert(p_manager != 00);
3519 if (p_header_size < 1) {
3520 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3524 l_cp = &(p_j2k->m_cp);
3527 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3531 /* First PPM marker */
3533 if (p_header_size < 4) {
3534 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3538 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3542 /* First PPM marker: Initialization */
3543 l_cp->ppm_len = l_N_ppm;
3544 l_cp->ppm_data_size = 0;
3546 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3547 if (l_cp->ppm_buffer == 00) {
3548 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3551 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3553 l_cp->ppm_data = l_cp->ppm_buffer;
3557 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3558 if (p_header_size >= 4) {
3560 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3563 l_cp->ppm_len += l_N_ppm ;
3565 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3566 if (l_cp->ppm_buffer == 00) {
3567 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3570 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3572 l_cp->ppm_data = l_cp->ppm_buffer;
3579 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3581 if (l_remaining_data <= p_header_size) {
3582 /* we must store less information than available in the packet */
3583 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3584 l_cp->ppm_data_size = l_cp->ppm_len;
3585 p_header_size -= l_remaining_data;
3586 p_header_data += l_remaining_data;
3589 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3590 l_cp->ppm_data_size += p_header_size;
3591 p_header_data += p_header_size;
3604 * Reads a PPM marker (Packed packet headers, main header)
3606 * @param p_header_data the data contained in the POC box.
3607 * @param p_j2k the jpeg2000 codec.
3608 * @param p_header_size the size of the data contained in the POC marker.
3609 * @param p_manager the user event manager.
3611 opj_bool j2k_read_ppm_v3 (
3612 opj_j2k_v2_t *p_j2k,
3613 OPJ_BYTE * p_header_data,
3614 OPJ_UINT32 p_header_size,
3615 struct opj_event_mgr * p_manager
3618 opj_cp_v2_t *l_cp = 00;
3619 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3622 assert(p_header_data != 00);
3623 assert(p_j2k != 00);
3624 assert(p_manager != 00);
3626 /* Minimum size of PPM marker is equal to the size of Zppm element */
3627 if (p_header_size < 1) {
3628 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3632 l_cp = &(p_j2k->m_cp);
3635 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3639 /* First PPM marker */
3641 /* We need now at least the Nppm^0 element */
3642 if (p_header_size < 4) {
3643 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3647 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3651 /* First PPM marker: Initialization */
3652 l_cp->ppm_len = l_N_ppm;
3653 l_cp->ppm_data_read = 0;
3655 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3656 if (l_cp->ppm_data == 00) {
3657 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3660 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3662 l_cp->ppm_data_current = l_cp->ppm_data;
3664 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3667 if (p_header_size < 4) {
3668 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3672 /* Uncompleted Ippm series in the previous PPM marker?*/
3673 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3674 /* Get the place where add the remaining Ippm series*/
3675 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3676 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3679 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3683 /* Increase the size of ppm_data to add the new Ippm series*/
3684 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3686 /* Keep the position of the place where concatenate the new series*/
3687 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3688 l_cp->ppm_len += l_N_ppm;
3693 l_remaining_data = p_header_size;
3695 while (l_remaining_data >= l_N_ppm) {
3696 /* read a complete Ippm series*/
3697 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3698 p_header_size -= l_N_ppm;
3699 p_header_data += l_N_ppm;
3701 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3705 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3710 l_remaining_data = p_header_size;
3714 l_remaining_data = p_header_size;
3716 /* Next Ippm series is a complete series ?*/
3717 if (l_remaining_data > l_N_ppm) {
3718 /* Increase the size of ppm_data to add the new Ippm series*/
3719 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3721 /* Keep the position of the place where concatenate the new series */
3722 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3723 l_cp->ppm_len += l_N_ppm;
3728 /* Need to read an incomplete Ippm series*/
3729 if (l_remaining_data) {
3730 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3732 /* Keep the position of the place where concatenate the new series*/
3733 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3734 l_cp->ppm_len += l_N_ppm;
3736 /* Read incomplete Ippm series*/
3737 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3738 p_header_size -= l_remaining_data;
3739 p_header_data += l_remaining_data;
3741 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3746 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3747 if (p_header_size >= 4) {
3749 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3752 l_cp->ppm_len += l_N_ppm ;
3754 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3755 if (l_cp->ppm_buffer == 00) {
3756 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3759 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3761 l_cp->ppm_data = l_cp->ppm_buffer;
3768 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3770 if (l_remaining_data <= p_header_size) {
3771 /* we must store less information than available in the packet */
3772 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3773 l_cp->ppm_data_size = l_cp->ppm_len;
3774 p_header_size -= l_remaining_data;
3775 p_header_data += l_remaining_data;
3778 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3779 l_cp->ppm_data_size += p_header_size;
3780 p_header_data += p_header_size;
3790 * Reads a PPT marker (Packed packet headers, tile-part header)
3792 * @param p_header_data the data contained in the PPT box.
3793 * @param p_j2k the jpeg2000 codec.
3794 * @param p_header_size the size of the data contained in the PPT marker.
3795 * @param p_manager the user event manager.
3797 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3798 OPJ_BYTE * p_header_data,
3799 OPJ_UINT32 p_header_size,
3800 opj_event_mgr_t * p_manager
3803 opj_cp_v2_t *l_cp = 00;
3804 opj_tcp_v2_t *l_tcp = 00;
3808 assert(p_header_data != 00);
3809 assert(p_j2k != 00);
3810 assert(p_manager != 00);
3812 /* We need to have the Z_ppt element at minimum */
3813 if (p_header_size < 1) {
3814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3818 l_cp = &(p_j2k->m_cp);
3820 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");
3824 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3827 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3831 /* Allocate buffer to read the packet header */
3833 /* First PPT marker */
3834 l_tcp->ppt_data_size = 0;
3835 l_tcp->ppt_len = p_header_size;
3837 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3838 if (l_tcp->ppt_buffer == 00) {
3839 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3842 l_tcp->ppt_data = l_tcp->ppt_buffer;
3844 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3847 l_tcp->ppt_len += p_header_size;
3849 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3850 if (l_tcp->ppt_buffer == 00) {
3851 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3854 l_tcp->ppt_data = l_tcp->ppt_buffer;
3856 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3859 /* Read packet header from buffer */
3860 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3862 l_tcp->ppt_data_size += p_header_size;
3868 * Writes the TLM marker (Tile Length Marker)
3870 * @param p_stream the stream to write data to.
3871 * @param p_j2k J2K codec.
3872 * @param p_manager the user event manager.
3874 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3875 opj_stream_private_t *p_stream,
3876 opj_event_mgr_t * p_manager
3879 OPJ_BYTE * l_current_data = 00;
3880 OPJ_UINT32 l_tlm_size;
3883 assert(p_j2k != 00);
3884 assert(p_manager != 00);
3885 assert(p_stream != 00);
3887 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3889 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3890 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3891 = (OPJ_BYTE*)opj_realloc(
3892 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3895 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3899 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3902 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3904 /* change the way data is written to avoid seeking if possible */
3906 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
3908 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
3909 l_current_data += 2;
3911 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
3912 l_current_data += 2;
3914 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
3917 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3920 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
3921 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) {
3929 * Writes the SOT marker (Start of tile-part)
3931 * @param p_stream the stream to write data to.
3932 * @param p_j2k J2K codec.
3933 * @param p_manager the user event manager.
3935 opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
3937 OPJ_UINT32 * p_data_written,
3938 const opj_stream_private_t *p_stream,
3939 opj_event_mgr_t * p_manager
3943 assert(p_j2k != 00);
3944 assert(p_manager != 00);
3945 assert(p_stream != 00);
3947 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
3950 opj_write_bytes(p_data,10,2); /* Lsot */
3953 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
3959 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
3962 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
3967 /* update markers struct */
3969 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
3971 assert( 0 && "TODO" );
3972 #endif /* USE_JPWL */
3974 * p_data_written = 12;
3983 * Reads a PPT marker (Packed packet headers, tile-part header)
3985 * @param p_header_data the data contained in the PPT box.
3986 * @param p_j2k the jpeg2000 codec.
3987 * @param p_header_size the size of the data contained in the PPT marker.
3988 * @param p_manager the user event manager.
3990 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
3991 OPJ_BYTE * p_header_data,
3992 OPJ_UINT32 p_header_size,
3993 opj_event_mgr_t * p_manager )
3995 opj_cp_v2_t *l_cp = 00;
3996 opj_tcp_v2_t *l_tcp = 00;
3997 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3998 OPJ_UINT32 l_current_part;
3999 OPJ_UINT32 l_tile_x,l_tile_y;
4002 assert(p_header_data != 00);
4003 assert(p_j2k != 00);
4004 assert(p_manager != 00);
4006 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4007 if (p_header_size != 8) {
4008 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4012 l_cp = &(p_j2k->m_cp);
4013 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4016 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4017 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4018 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4021 if (l_cp->correct) {
4023 int tileno = p_j2k->m_current_tile_number;
4024 static int backup_tileno = 0;
4026 /* tileno is negative or larger than the number of tiles!!! */
4027 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4028 opj_event_msg_v2(p_manager, EVT_ERROR,
4029 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4030 tileno, (l_cp->tw * l_cp->th));
4032 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4035 /* we try to correct */
4036 tileno = backup_tileno;
4037 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4038 "- setting tile number to %d\n",
4042 /* keep your private count of tiles */
4045 #endif /* USE_JPWL */
4047 /* look for the tile in the list of already processed tile (in parts). */
4048 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4049 /* since the time taken by this function can only grow at the time */
4051 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4054 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4055 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4057 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4063 if (l_cp->correct) {
4065 /* totlen is negative or larger than the bytes left!!! */
4066 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))) { */
4067 opj_event_msg_v2(p_manager, EVT_ERROR,
4068 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4069 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4071 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4074 /* we try to correct */
4076 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4077 "- setting Psot to %d => assuming it is the last tile\n",
4081 #endif /* USE_JPWL */
4083 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4085 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4086 "we assuming it is the last tile-part of the codestream.\n");
4087 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4090 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4093 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4096 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4097 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4098 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4099 if (l_tcp->m_nb_tile_parts) {
4100 if (l_current_part >= l_tcp->m_nb_tile_parts){
4101 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4102 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4103 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4107 l_tcp->m_nb_tile_parts = l_num_parts;
4110 /* If know the number of tile part header we will check if we didn't read the last*/
4111 if (l_tcp->m_nb_tile_parts) {
4112 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4113 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4117 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4118 /* Keep the size of data to skip after this marker */
4119 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4122 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4123 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4126 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4128 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4129 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4130 p_j2k->m_specific_param.m_decoder.m_skip_data =
4131 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4132 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4133 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4134 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4137 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4138 p_j2k->m_specific_param.m_decoder.m_skip_data =
4139 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4143 if (p_j2k->cstr_index)
4145 assert(p_j2k->cstr_index->tile_index != 00);
4146 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4147 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4149 if (l_num_parts != 0){
4150 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4151 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4154 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4155 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4156 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4158 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4159 (opj_tp_index_t*)opj_realloc(
4160 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4161 l_num_parts* sizeof(opj_tp_index_t));
4164 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4166 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4167 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4168 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4169 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4170 sizeof(opj_tp_index_t));
4173 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4174 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4175 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4176 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4177 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4178 * sizeof(opj_tp_index_t));
4187 /* 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 */
4188 /* if (p_j2k->cstr_info) {
4191 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4194 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4195 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4196 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4197 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4200 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4203 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4207 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4210 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4211 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4212 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4218 * Writes the SOD marker (Start of data)
4220 * @param p_stream the stream to write data to.
4221 * @param p_j2k J2K codec.
4222 * @param p_manager the user event manager.
4224 opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
4225 opj_tcd_v2_t * p_tile_coder,
4227 OPJ_UINT32 * p_data_written,
4228 OPJ_UINT32 p_total_data_size,
4229 const opj_stream_private_t *p_stream,
4230 opj_event_mgr_t * p_manager
4233 opj_tcp_v2_t *l_tcp = 00;
4234 opj_codestream_info_t *l_cstr_info = 00;
4235 opj_cp_v2_t *l_cp = 00;
4237 OPJ_UINT32 l_size_tile;
4238 OPJ_UINT32 l_remaining_data;
4241 assert(p_j2k != 00);
4242 assert(p_manager != 00);
4243 assert(p_stream != 00);
4245 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4248 /* make room for the EOF marker */
4249 l_remaining_data = p_total_data_size - 4;
4251 l_cp = &(p_j2k->m_cp);
4252 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4255 /* update tile coder */
4256 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4257 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4259 l_size_tile = l_cp->th * l_cp->tw;
4262 /* TODO mergeV2: check this part which use cstr_info */
4263 /*l_cstr_info = p_j2k->cstr_info;
4265 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4266 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4267 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4273 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4275 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4280 /* update markers struct */
4281 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4283 assert( 0 && "TODO" );
4284 #endif /* USE_JPWL */
4289 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4290 p_tile_coder->tcd_image->tiles->packno = 0;
4292 l_cstr_info->packno = 0;
4296 *p_data_written = 0;
4298 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)) {
4299 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4303 *p_data_written += 2;
4312 * Reads a SOD marker (Start Of Data)
4314 * @param p_header_data the data contained in the SOD box.
4315 * @param p_j2k the jpeg2000 codec.
4316 * @param p_header_size the size of the data contained in the SOD marker.
4317 * @param p_manager the user event manager.
4319 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4320 opj_stream_private_t *p_stream,
4321 opj_event_mgr_t * p_manager
4324 OPJ_UINT32 l_current_read_size;
4325 opj_codestream_index_t * l_cstr_index = 00;
4326 OPJ_BYTE ** l_current_data = 00;
4327 opj_tcp_v2_t * l_tcp = 00;
4328 OPJ_UINT32 * l_tile_len = 00;
4331 assert(p_j2k != 00);
4332 assert(p_manager != 00);
4333 assert(p_stream != 00);
4335 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4337 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4338 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4339 // but we are in the last tile part,
4340 // so its result will fit on OPJ_UINT32 unless we find
4341 // a file with a single tile part of more than 4 GB...*/
4342 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4345 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4347 l_current_data = &(l_tcp->m_data);
4348 l_tile_len = &l_tcp->m_data_size;
4350 if (! *l_current_data) {
4351 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4354 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4357 if (*l_current_data == 00) {
4358 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4364 l_cstr_index = p_j2k->cstr_index;
4366 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4368 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4369 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4371 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4372 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4374 opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
4378 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4380 /*l_cstr_index->packno = 0;*/
4383 l_current_read_size = opj_stream_read_data( p_stream,
4384 *l_current_data + *l_tile_len,
4385 p_j2k->m_specific_param.m_decoder.m_sot_length,
4388 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4389 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4392 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4395 *l_tile_len += l_current_read_size;
4401 * Writes the RGN marker (Region Of Interest)
4403 * @param p_tile_no the tile to output
4404 * @param p_comp_no the component to output
4405 * @param p_stream the stream to write data to.
4406 * @param p_j2k J2K codec.
4407 * @param p_manager the user event manager.
4409 opj_bool opj_j2k_write_rgn(opj_j2k_v2_t *p_j2k,
4410 OPJ_UINT32 p_tile_no,
4411 OPJ_UINT32 p_comp_no,
4412 opj_stream_private_t *p_stream,
4413 opj_event_mgr_t * p_manager
4416 OPJ_BYTE * l_current_data = 00;
4417 OPJ_UINT32 l_nb_comp;
4418 OPJ_UINT32 l_rgn_size;
4419 opj_image_t *l_image = 00;
4420 opj_cp_v2_t *l_cp = 00;
4421 opj_tcp_v2_t *l_tcp = 00;
4422 opj_tccp_t *l_tccp = 00;
4423 OPJ_UINT32 l_comp_room;
4426 assert(p_j2k != 00);
4427 assert(p_manager != 00);
4428 assert(p_stream != 00);
4430 l_cp = &(p_j2k->m_cp);
4431 l_tcp = &l_cp->tcps[p_tile_no];
4432 l_tccp = &l_tcp->tccps[p_comp_no];
4434 l_nb_comp = l_image->numcomps;
4436 if (l_nb_comp <= 256) {
4443 l_rgn_size = 6 + l_comp_room;
4445 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4447 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4448 l_current_data += 2;
4450 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4451 l_current_data += 2;
4453 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4454 l_current_data+=l_comp_room;
4456 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4459 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4462 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) {
4470 static void j2k_write_eoc(opj_j2k_t *j2k) {
4471 opj_cio_t *cio = j2k->cio;
4472 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4473 cio_write(cio, J2K_MS_EOC, 2);
4477 /* update markers struct */
4478 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4479 #endif /* USE_JPWL */
4484 * Writes the EOC marker (End of Codestream)
4486 * @param p_stream the stream to write data to.
4487 * @param p_j2k J2K codec.
4488 * @param p_manager the user event manager.
4490 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4491 struct opj_stream_private *p_stream,
4492 struct opj_event_mgr * p_manager )
4495 assert(p_j2k != 00);
4496 assert(p_manager != 00);
4497 assert(p_stream != 00);
4499 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4504 /* update markers struct */
4506 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4508 #endif /* USE_JPWL */
4510 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4514 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4523 * Reads a RGN marker (Region Of Interest)
4525 * @param p_header_data the data contained in the POC box.
4526 * @param p_j2k the jpeg2000 codec.
4527 * @param p_header_size the size of the data contained in the POC marker.
4528 * @param p_manager the user event manager.
4530 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4531 OPJ_BYTE * p_header_data,
4532 OPJ_UINT32 p_header_size,
4533 opj_event_mgr_t * p_manager
4536 OPJ_UINT32 l_nb_comp;
4537 opj_image_t * l_image = 00;
4539 opj_cp_v2_t *l_cp = 00;
4540 opj_tcp_v2_t *l_tcp = 00;
4541 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4544 assert(p_header_data != 00);
4545 assert(p_j2k != 00);
4546 assert(p_manager != 00);
4548 l_image = p_j2k->m_private_image;
4549 l_nb_comp = l_image->numcomps;
4551 if (l_nb_comp <= 256) {
4556 if (p_header_size != 2 + l_comp_room) {
4557 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4561 l_cp = &(p_j2k->m_cp);
4562 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4563 &l_cp->tcps[p_j2k->m_current_tile_number] :
4564 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4566 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4567 p_header_data+=l_comp_room;
4568 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4572 if (l_cp->correct) {
4573 /* totlen is negative or larger than the bytes left!!! */
4574 if (l_comp_room >= l_nb_comp) {
4575 opj_event_msg_v2(p_manager, EVT_ERROR,
4576 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4577 l_comp_room, l_nb_comp);
4578 if (!JPWL_ASSUME || JPWL_ASSUME) {
4579 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4584 #endif /* USE_JPWL */
4586 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4593 OPJ_FLOAT32 opj_get_tp_stride (opj_tcp_v2_t * p_tcp)
4595 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4598 OPJ_FLOAT32 opj_get_default_stride (opj_tcp_v2_t * p_tcp)
4605 * Updates the rates of the tcp.
4607 * @param p_stream the stream to write data to.
4608 * @param p_j2k J2K codec.
4609 * @param p_manager the user event manager.
4611 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4612 opj_stream_private_t *p_stream,
4613 opj_event_mgr_t * p_manager )
4615 opj_cp_v2_t * l_cp = 00;
4616 opj_image_t * l_image = 00;
4617 opj_tcp_v2_t * l_tcp = 00;
4618 opj_image_comp_t * l_img_comp = 00;
4621 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4622 OPJ_FLOAT32 * l_rates = 0;
4623 OPJ_FLOAT32 l_sot_remove;
4624 OPJ_UINT32 l_bits_empty, l_size_pixel;
4625 OPJ_UINT32 l_tile_size = 0;
4626 OPJ_UINT32 l_last_res;
4627 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4630 assert(p_j2k != 00);
4631 assert(p_manager != 00);
4632 assert(p_stream != 00);
4635 l_cp = &(p_j2k->m_cp);
4636 l_image = p_j2k->m_private_image;
4639 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4640 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4641 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4643 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4644 l_tp_stride_func = opj_get_tp_stride;
4647 l_tp_stride_func = opj_get_default_stride;
4650 for (i=0;i<l_cp->th;++i) {
4651 for (j=0;j<l_cp->tw;++j) {
4652 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4654 /* 4 borders of the tile rescale on the image if necessary */
4655 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4656 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4657 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4658 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4660 l_rates = l_tcp->rates;
4662 /* Modification of the RATE >> */
4664 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4666 ((*l_rates) * l_bits_empty)
4674 for (k = 1; k < l_tcp->numlayers; ++k) {
4676 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4678 ((*l_rates) * l_bits_empty)
4694 for (i=0;i<l_cp->th;++i) {
4695 for (j=0;j<l_cp->tw;++j) {
4696 l_rates = l_tcp->rates;
4699 *l_rates -= l_sot_remove;
4701 if (*l_rates < 30) {
4708 l_last_res = l_tcp->numlayers - 1;
4710 for (k = 1; k < l_last_res; ++k) {
4713 *l_rates -= l_sot_remove;
4715 if (*l_rates < *(l_rates - 1) + 10) {
4716 *l_rates = (*(l_rates - 1)) + 20;
4724 *l_rates -= (l_sot_remove + 2.f);
4726 if (*l_rates < *(l_rates - 1) + 10) {
4727 *l_rates = (*(l_rates - 1)) + 20;
4735 l_img_comp = l_image->comps;
4738 for (i=0;i<l_image->numcomps;++i) {
4739 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4741 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4749 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4751 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4753 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4754 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4755 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4756 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4760 if (l_cp->m_specific_param.m_enc.m_cinema) {
4761 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4762 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4763 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4767 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4768 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4774 static void j2k_read_eoc(opj_j2k_t *j2k) {
4776 opj_bool success = OPJ_FALSE;
4778 /* if packets should be decoded */
4779 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4780 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4781 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4782 for (i = 0; i < j2k->cp->tileno_size; i++) {
4783 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4784 if (j2k->cp->tileno[i] != -1)
4786 tileno = j2k->cp->tileno[i];
4787 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4788 opj_free(j2k->tile_data[tileno]);
4789 j2k->tile_data[tileno] = NULL;
4790 tcd_free_decode_tile(tcd, i);
4793 success = OPJ_FALSE;
4794 if (success == OPJ_FALSE) {
4795 j2k->state |= J2K_STATE_ERR;
4799 tcd_free_decode(tcd);
4802 /* if packets should not be decoded */
4804 for (i = 0; i < j2k->cp->tileno_size; i++) {
4805 tileno = j2k->cp->tileno[i];
4806 opj_free(j2k->tile_data[tileno]);
4807 j2k->tile_data[tileno] = NULL;
4810 if (j2k->state & J2K_STATE_ERR)
4811 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4813 j2k->state = J2K_STATE_MT;
4817 * Reads a EOC marker (End Of Codestream)
4819 * @param p_header_data the data contained in the SOD box.
4820 * @param p_j2k the jpeg2000 codec.
4821 * @param p_header_size the size of the data contained in the SOD marker.
4822 * @param p_manager the user event manager.
4825 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4826 struct opj_stream_private *p_stream,
4827 struct opj_event_mgr * p_manager )
4830 opj_tcd_v2_t * l_tcd = 00;
4831 OPJ_UINT32 l_nb_tiles;
4832 opj_tcp_v2_t * l_tcp = 00;
4836 assert(p_j2k != 00);
4837 assert(p_manager != 00);
4838 assert(p_stream != 00);
4840 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4841 l_tcp = p_j2k->m_cp.tcps;
4843 l_tcd = tcd_create_v2(OPJ_TRUE);
4845 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4849 for (i = 0; i < l_nb_tiles; ++i) {
4850 if (l_tcp->m_data) {
4851 if (! tcd_init_decode_tile(l_tcd, i)) {
4852 tcd_destroy_v2(l_tcd);
4853 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4857 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4861 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4866 opj_j2k_tcp_destroy(l_tcp);
4870 tcd_destroy_v2(l_tcd);
4876 * Gets the offset of the header.
4878 * @param p_stream the stream to write data to.
4879 * @param p_j2k J2K codec.
4880 * @param p_manager the user event manager.
4882 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
4883 struct opj_stream_private *p_stream,
4884 struct opj_event_mgr * p_manager )
4887 assert(p_j2k != 00);
4888 assert(p_manager != 00);
4889 assert(p_stream != 00);
4891 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4897 * Writes the MCT marker (Multiple Component Transform)
4899 * @param p_stream the stream to write data to.
4900 * @param p_j2k J2K codec.
4901 * @param p_manager the user event manager.
4903 opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
4904 struct opj_stream_private *p_stream,
4905 struct opj_event_mgr * p_manager )
4908 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4909 opj_mct_data_t * l_mct_record;
4910 opj_tcp_v2_t * l_tcp;
4913 assert(p_j2k != 00);
4914 assert(p_stream != 00);
4915 assert(p_manager != 00);
4917 if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
4921 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4922 l_mct_record = l_tcp->m_mct_records;
4924 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4926 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
4933 l_mcc_record = l_tcp->m_mcc_records;
4935 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
4937 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
4944 if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
4952 * Writes the image components.
4954 * @param p_stream the stream to write data to.
4955 * @param p_j2k J2K codec.
4956 * @param p_manager the user event manager.
4958 opj_bool opj_j2k_write_image_components(opj_j2k_v2_t *p_j2k,
4959 struct opj_stream_private *p_stream,
4960 struct opj_event_mgr * p_manager )
4965 assert(p_j2k != 00);
4966 assert(p_manager != 00);
4967 assert(p_stream != 00);
4969 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
4971 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
4975 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
4984 * Writes regions of interests.
4986 * @param p_stream the stream to write data to.
4987 * @param p_j2k J2K codec.
4988 * @param p_manager the user event manager.
4990 opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
4991 struct opj_stream_private *p_stream,
4992 struct opj_event_mgr * p_manager )
4995 const opj_tccp_t *l_tccp = 00;
4998 assert(p_j2k != 00);
4999 assert(p_manager != 00);
5000 assert(p_stream != 00);
5002 l_tccp = p_j2k->m_cp.tcps->tccps;
5004 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5005 if (l_tccp->roishift) {
5007 if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) {
5021 * @param p_stream the stream to write data to.
5022 * @param p_j2k J2K codec.
5023 * @param p_manager the user event manager.
5025 opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k,
5026 struct opj_stream_private *p_stream,
5027 struct opj_event_mgr * p_manager )
5029 opj_codestream_index_t * l_cstr_index = 00;
5032 assert(p_j2k != 00);
5033 assert(p_manager != 00);
5034 assert(p_stream != 00);
5036 l_cstr_index = p_j2k->cstr_index;
5038 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5040 /* The following adjustment is done to adjust the codestream size */
5041 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5042 /* the first bunch of bytes is not in the codestream */
5043 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5048 /* preparation of JPWL marker segments */
5052 /* encode according to JPWL */
5053 jpwl_encode(p_j2k, p_stream, image);
5057 assert( 0 && "TODO" );
5058 #endif /* USE_JPWL */
5068 * Reads an unknown marker
5070 * @param p_stream the stream object to read from.
5071 * @param p_j2k the jpeg2000 codec.
5072 * @param p_manager the user event manager.
5074 * @return true if the marker could be deduced.
5076 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5077 opj_stream_private_t *p_stream,
5078 OPJ_UINT32 *output_marker,
5079 opj_event_mgr_t * p_manager
5082 OPJ_UINT32 l_unknown_marker;
5083 const opj_dec_memory_marker_handler_t * l_marker_handler;
5084 OPJ_UINT32 l_size_unk = 2;
5087 assert(p_j2k != 00);
5088 assert(p_manager != 00);
5089 assert(p_stream != 00);
5091 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5094 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5095 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5096 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5100 /* read 2 bytes as the new marker ID*/
5101 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5103 if (!(l_unknown_marker < 0xff00)) {
5105 /* Get the marker handler from the marker ID*/
5106 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5108 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5109 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5113 if (l_marker_handler->id != J2K_MS_UNK) {
5114 /* Add the marker to the codestream index*/
5115 if (l_marker_handler->id != J2K_MS_SOT)
5116 opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
5117 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5119 break; /* next marker is known and well located */
5127 *output_marker = l_marker_handler->id ;
5133 * Writes the MCT marker (Multiple Component Transform)
5135 * @param p_stream the stream to write data to.
5136 * @param p_j2k J2K codec.
5137 * @param p_manager the user event manager.
5139 opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5140 opj_mct_data_t * p_mct_record,
5141 struct opj_stream_private *p_stream,
5142 struct opj_event_mgr * p_manager )
5144 OPJ_UINT32 l_mct_size;
5145 OPJ_BYTE * l_current_data = 00;
5149 assert(p_j2k != 00);
5150 assert(p_manager != 00);
5151 assert(p_stream != 00);
5153 l_mct_size = 10 + p_mct_record->m_data_size;
5155 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5156 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5157 = (OPJ_BYTE*)opj_realloc(
5158 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5161 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5165 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5168 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5170 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5171 l_current_data += 2;
5173 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5174 l_current_data += 2;
5176 opj_write_bytes(l_current_data,0,2); /* Zmct */
5177 l_current_data += 2;
5179 /* only one marker atm */
5180 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5182 opj_write_bytes(l_current_data,l_tmp,2);
5183 l_current_data += 2;
5185 opj_write_bytes(l_current_data,0,2); /* Ymct */
5188 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5190 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) {
5198 * Reads a MCT marker (Multiple Component Transform)
5200 * @param p_header_data the data contained in the MCT box.
5201 * @param p_j2k the jpeg2000 codec.
5202 * @param p_header_size the size of the data contained in the MCT marker.
5203 * @param p_manager the user event manager.
5205 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5206 OPJ_BYTE * p_header_data,
5207 OPJ_UINT32 p_header_size,
5208 opj_event_mgr_t * p_manager
5212 opj_tcp_v2_t *l_tcp = 00;
5215 opj_mct_data_t * l_mct_data;
5218 assert(p_header_data != 00);
5219 assert(p_j2k != 00);
5221 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5222 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5223 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5225 if (p_header_size < 2) {
5226 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5231 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5234 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5238 if(p_header_size <= 6) {
5239 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5243 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5244 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5247 l_indix = l_tmp & 0xff;
5248 l_mct_data = l_tcp->m_mct_records;
5250 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5251 if (l_mct_data->m_index == l_indix) {
5258 if (i == l_tcp->m_nb_mct_records) {
5259 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5260 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5262 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));
5263 if(! l_tcp->m_mct_records) {
5264 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5268 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5269 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5272 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5275 if (l_mct_data->m_data) {
5276 opj_free(l_mct_data->m_data);
5277 l_mct_data->m_data = 00;
5280 l_mct_data->m_index = l_indix;
5281 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5282 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5284 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5287 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5293 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5294 if (! l_mct_data->m_data) {
5295 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5298 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5300 l_mct_data->m_data_size = p_header_size;
5301 ++l_tcp->m_nb_mct_records;
5307 * Writes the MCC marker (Multiple Component Collection)
5309 * @param p_stream the stream to write data to.
5310 * @param p_j2k J2K codec.
5311 * @param p_manager the user event manager.
5313 opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5314 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5315 struct opj_stream_private *p_stream,
5316 struct opj_event_mgr * p_manager )
5319 OPJ_UINT32 l_mcc_size;
5320 OPJ_BYTE * l_current_data = 00;
5321 OPJ_UINT32 l_nb_bytes_for_comp;
5326 assert(p_j2k != 00);
5327 assert(p_manager != 00);
5328 assert(p_stream != 00);
5330 if (p_mcc_record->m_nb_comps > 255 ) {
5331 l_nb_bytes_for_comp = 2;
5335 l_nb_bytes_for_comp = 1;
5339 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5340 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5342 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5343 = (OPJ_BYTE*)opj_realloc(
5344 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5346 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5350 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5353 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5355 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5356 l_current_data += 2;
5358 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5359 l_current_data += 2;
5362 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5363 l_current_data += 2;
5365 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5368 /* only one marker atm */
5369 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5372 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5375 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5378 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 */
5381 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5382 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5383 l_current_data+=l_nb_bytes_for_comp;
5386 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 */
5389 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5391 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5392 l_current_data+=l_nb_bytes_for_comp;
5395 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5397 if (p_mcc_record->m_decorrelation_array) {
5398 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5401 if (p_mcc_record->m_offset_array) {
5402 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5405 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5408 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) {
5416 * Reads a MCC marker (Multiple Component Collection)
5418 * @param p_header_data the data contained in the MCC box.
5419 * @param p_j2k the jpeg2000 codec.
5420 * @param p_header_size the size of the data contained in the MCC marker.
5421 * @param p_manager the user event manager.
5423 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5424 OPJ_BYTE * p_header_data,
5425 OPJ_UINT32 p_header_size,
5426 opj_event_mgr_t * p_manager )
5431 opj_tcp_v2_t * l_tcp;
5432 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5433 opj_mct_data_t * l_mct_data;
5434 OPJ_UINT32 l_nb_collections;
5435 OPJ_UINT32 l_nb_comps;
5436 OPJ_UINT32 l_nb_bytes_by_comp;
5440 assert(p_header_data != 00);
5441 assert(p_j2k != 00);
5442 assert(p_manager != 00);
5444 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5445 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5446 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5448 if (p_header_size < 2) {
5449 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5454 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5457 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5461 if (p_header_size < 7) {
5462 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5466 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5469 l_mcc_record = l_tcp->m_mcc_records;
5471 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5472 if (l_mcc_record->m_index == l_indix) {
5479 if (i == l_tcp->m_nb_mcc_records) {
5480 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5481 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5483 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5484 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5485 if (! l_tcp->m_mcc_records) {
5486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5489 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5490 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));
5492 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5494 l_mcc_record->m_index = l_indix;
5496 /* only one marker atm */
5497 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5500 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5504 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5507 if (l_nb_collections > 1) {
5508 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5514 for (i=0;i<l_nb_collections;++i) {
5515 if (p_header_size < 3) {
5516 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5520 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5524 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5528 opj_read_bytes(p_header_data,&l_nb_comps,2);
5533 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5534 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5536 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5537 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5541 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5543 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5544 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5545 p_header_data+=l_nb_bytes_by_comp;
5548 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5553 opj_read_bytes(p_header_data,&l_nb_comps,2);
5556 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5557 l_nb_comps &= 0x7fff;
5559 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5560 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5564 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5565 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5569 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5571 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5572 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5573 p_header_data+=l_nb_bytes_by_comp;
5576 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5581 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5584 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5585 l_mcc_record->m_decorrelation_array = 00;
5586 l_mcc_record->m_offset_array = 00;
5588 l_indix = l_tmp & 0xff;
5590 l_mct_data = l_tcp->m_mct_records;
5591 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5592 if (l_mct_data->m_index == l_indix) {
5593 l_mcc_record->m_decorrelation_array = l_mct_data;
5599 if (l_mcc_record->m_decorrelation_array == 00) {
5600 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5605 l_indix = (l_tmp >> 8) & 0xff;
5607 l_mct_data = l_tcp->m_mct_records;
5608 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5609 if (l_mct_data->m_index == l_indix) {
5610 l_mcc_record->m_offset_array = l_mct_data;
5616 if (l_mcc_record->m_offset_array == 00) {
5617 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5623 if (p_header_size != 0) {
5624 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5628 ++l_tcp->m_nb_mcc_records;
5635 * Writes the MCO marker (Multiple component transformation ordering)
5637 * @param p_stream the stream to write data to.
5638 * @param p_j2k J2K codec.
5639 * @param p_manager the user event manager.
5641 opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
5642 struct opj_stream_private *p_stream,
5643 struct opj_event_mgr * p_manager
5646 OPJ_BYTE * l_current_data = 00;
5647 OPJ_UINT32 l_mco_size;
5648 opj_tcp_v2_t * l_tcp = 00;
5649 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5653 assert(p_j2k != 00);
5654 assert(p_manager != 00);
5655 assert(p_stream != 00);
5657 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5658 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5660 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5661 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5663 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5664 = (OPJ_BYTE*)opj_realloc(
5665 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5667 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5672 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5675 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5676 l_current_data += 2;
5678 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5679 l_current_data += 2;
5681 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5684 l_mcc_record = l_tcp->m_mcc_records;
5685 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5686 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
5692 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) {
5700 * Reads a MCO marker (Multiple Component Transform Ordering)
5702 * @param p_header_data the data contained in the MCO box.
5703 * @param p_j2k the jpeg2000 codec.
5704 * @param p_header_size the size of the data contained in the MCO marker.
5705 * @param p_manager the user event manager.
5707 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5708 OPJ_BYTE * p_header_data,
5709 OPJ_UINT32 p_header_size,
5710 opj_event_mgr_t * p_manager
5713 OPJ_UINT32 l_tmp, i;
5714 OPJ_UINT32 l_nb_stages;
5715 opj_tcp_v2_t * l_tcp;
5716 opj_tccp_t * l_tccp;
5717 opj_image_t * l_image;
5718 opj_image_comp_t * l_img_comp;
5721 assert(p_header_data != 00);
5722 assert(p_j2k != 00);
5723 assert(p_manager != 00);
5725 l_image = p_j2k->m_private_image;
5726 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5727 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5728 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5730 if (p_header_size < 1) {
5731 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5735 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
5738 if (l_nb_stages > 1) {
5739 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5743 if (p_header_size != l_nb_stages + 1) {
5744 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5748 l_tccp = l_tcp->tccps;
5749 l_img_comp = l_image->comps;
5751 for (i=0;i<l_image->numcomps;++i) {
5752 l_tccp->m_dc_level_shift = 0;
5756 if (l_tcp->m_mct_decoding_matrix) {
5757 opj_free(l_tcp->m_mct_decoding_matrix);
5758 l_tcp->m_mct_decoding_matrix = 00;
5761 for (i=0;i<l_nb_stages;++i) {
5762 opj_read_bytes(p_header_data,&l_tmp,1);
5765 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5773 opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5776 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5777 opj_mct_data_t * l_deco_array, * l_offset_array;
5778 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5779 OPJ_UINT32 l_nb_elem;
5780 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5781 opj_tccp_t * l_tccp;
5784 assert(p_tcp != 00);
5786 l_mcc_record = p_tcp->m_mcc_records;
5788 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5789 if (l_mcc_record->m_index == p_index) {
5794 if (i==p_tcp->m_nb_mcc_records) {
5795 /** element discarded **/
5799 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5800 /** do not support number of comps != image */
5804 l_deco_array = l_mcc_record->m_decorrelation_array;
5807 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5808 if (l_deco_array->m_data_size != l_data_size) {
5812 l_nb_elem = p_image->numcomps * p_image->numcomps;
5813 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5814 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5816 if (! p_tcp->m_mct_decoding_matrix ) {
5820 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);
5823 l_offset_array = l_mcc_record->m_offset_array;
5825 if (l_offset_array) {
5826 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5827 if (l_offset_array->m_data_size != l_data_size) {
5831 l_nb_elem = p_image->numcomps;
5832 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
5833 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5835 if (! l_offset_data ) {
5839 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5841 l_tccp = p_tcp->tccps;
5842 l_current_offset_data = l_offset_data;
5844 for (i=0;i<p_image->numcomps;++i) {
5845 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
5849 opj_free(l_offset_data);
5856 * Writes the CBD marker (Component bit depth definition)
5858 * @param p_stream the stream to write data to.
5859 * @param p_j2k J2K codec.
5860 * @param p_manager the user event manager.
5862 opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
5863 struct opj_stream_private *p_stream,
5864 struct opj_event_mgr * p_manager )
5867 OPJ_UINT32 l_cbd_size;
5868 OPJ_BYTE * l_current_data = 00;
5869 opj_image_t *l_image = 00;
5870 opj_image_comp_t * l_comp = 00;
5873 assert(p_j2k != 00);
5874 assert(p_manager != 00);
5875 assert(p_stream != 00);
5877 l_image = p_j2k->m_private_image;
5878 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
5880 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5881 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5882 = (OPJ_BYTE*)opj_realloc(
5883 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5886 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5890 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
5893 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5895 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
5896 l_current_data += 2;
5898 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
5899 l_current_data += 2;
5901 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
5904 l_comp = l_image->comps;
5906 for (i=0;i<l_image->numcomps;++i) {
5907 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
5913 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) {
5921 * Reads a CBD marker (Component bit depth definition)
5922 * @param p_header_data the data contained in the CBD box.
5923 * @param p_j2k the jpeg2000 codec.
5924 * @param p_header_size the size of the data contained in the CBD marker.
5925 * @param p_manager the user event manager.
5927 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
5928 OPJ_BYTE * p_header_data,
5929 OPJ_UINT32 p_header_size,
5930 opj_event_mgr_t * p_manager
5933 OPJ_UINT32 l_nb_comp,l_num_comp;
5934 OPJ_UINT32 l_comp_def;
5936 opj_image_comp_t * l_comp = 00;
5939 assert(p_header_data != 00);
5940 assert(p_j2k != 00);
5941 assert(p_manager != 00);
5943 l_num_comp = p_j2k->m_private_image->numcomps;
5945 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
5946 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5950 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
5953 if (l_nb_comp != l_num_comp) {
5954 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5958 l_comp = p_j2k->m_private_image->comps;
5959 for (i=0;i<l_num_comp;++i) {
5960 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
5962 l_comp->sgnd = (l_comp_def>>7) & 1;
5963 l_comp->prec = (l_comp_def&0x7f) + 1;
5971 /* ----------------------------------------------------------------------- */
5972 /* J2K / JPT decoder interface */
5973 /* ----------------------------------------------------------------------- */
5975 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
5977 if(j2k && parameters) {
5978 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5979 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5982 j2k->m_cp.correct = parameters->jpwl_correct;
5983 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5984 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5985 #endif /* USE_JPWL */
5990 /* ----------------------------------------------------------------------- */
5991 /* J2K encoder interface */
5992 /* ----------------------------------------------------------------------- */
5994 opj_j2k_v2_t* opj_j2k_create_compress(void)
5996 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6001 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6003 l_j2k->m_is_decoder = 0;
6004 l_j2k->m_cp.m_is_decoder = 0;
6006 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6007 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6008 opj_j2k_destroy(l_j2k);
6012 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6014 /* validation list creation*/
6015 l_j2k->m_validation_list = opj_procedure_list_create();
6016 if (! l_j2k->m_validation_list) {
6017 opj_j2k_destroy(l_j2k);
6021 /* execution list creation*/
6022 l_j2k->m_procedure_list = opj_procedure_list_create();
6023 if (! l_j2k->m_procedure_list) {
6024 opj_j2k_destroy(l_j2k);
6032 void opj_j2k_setup_encoder( opj_j2k_v2_t *p_j2k,
6033 opj_cparameters_t *parameters,
6035 opj_event_mgr_t * p_manager)
6037 OPJ_UINT32 i, j, tileno, numpocs_tile;
6038 opj_cp_v2_t *cp = 00;
6041 if(!p_j2k || !parameters || ! image) {
6045 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6046 cp = &(p_j2k->m_cp);
6048 /* set default values for cp */
6053 copy user encoding parameters
6055 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6056 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6057 cp->rsiz = parameters->cp_rsiz;
6058 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6059 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6060 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6062 /* mod fixed_quality */
6063 if (parameters->cp_matrice) {
6064 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6065 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6066 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6070 cp->tdx = parameters->cp_tdx;
6071 cp->tdy = parameters->cp_tdy;
6074 cp->tx0 = parameters->cp_tx0;
6075 cp->ty0 = parameters->cp_ty0;
6077 /* comment string */
6078 if(parameters->cp_comment) {
6079 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6081 strcpy(cp->comment, parameters->cp_comment);
6086 calculate other encoding parameters
6089 if (parameters->tile_size_on) {
6090 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6091 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6093 cp->tdx = image->x1 - cp->tx0;
6094 cp->tdy = image->y1 - cp->ty0;
6097 if (parameters->tp_on) {
6098 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6099 cp->m_specific_param.m_enc.m_tp_on = 1;
6104 calculate JPWL encoding parameters
6107 if (parameters->jpwl_epc_on) {
6111 cp->epc_on = OPJ_TRUE;
6112 cp->info_on = OPJ_FALSE; /* no informative technique */
6115 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6116 cp->epb_on = OPJ_TRUE;
6118 cp->hprot_MH = parameters->jpwl_hprot_MH;
6119 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6120 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6121 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6123 /* if tile specs are not specified, copy MH specs */
6124 if (cp->hprot_TPH[0] == -1) {
6125 cp->hprot_TPH_tileno[0] = 0;
6126 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6128 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6129 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6130 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6131 cp->pprot[i] = parameters->jpwl_pprot[i];
6135 /* set ESD writing */
6136 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6137 cp->esd_on = OPJ_TRUE;
6139 cp->sens_size = parameters->jpwl_sens_size;
6140 cp->sens_addr = parameters->jpwl_sens_addr;
6141 cp->sens_range = parameters->jpwl_sens_range;
6143 cp->sens_MH = parameters->jpwl_sens_MH;
6144 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6145 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6146 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6150 /* always set RED writing to false: we are at the encoder */
6151 cp->red_on = OPJ_FALSE;
6154 cp->epc_on = OPJ_FALSE;
6156 #endif /* USE_JPWL */
6159 /* initialize the mutiple tiles */
6160 /* ---------------------------- */
6161 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6162 if (parameters->numpocs) {
6163 /* initialisation of POC */
6164 l_res = opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6168 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6169 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6170 tcp->numlayers = parameters->tcp_numlayers;
6172 for (j = 0; j < tcp->numlayers; j++) {
6173 if(cp->m_specific_param.m_enc.m_cinema){
6174 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6175 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6177 tcp->rates[j] = parameters->tcp_rates[j];
6179 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6180 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6182 tcp->rates[j] = parameters->tcp_rates[j];
6187 tcp->csty = parameters->csty;
6188 tcp->prg = parameters->prog_order;
6189 tcp->mct = parameters->tcp_mct;
6194 if (parameters->numpocs) {
6195 /* initialisation of POC */
6198 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6199 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6200 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6202 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6203 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6204 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6205 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6206 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6207 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6208 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6214 tcp->numpocs = numpocs_tile -1 ;
6219 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6221 if (parameters->mct_data) {
6223 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6226 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6227 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6228 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6229 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6232 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6233 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6234 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6236 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6237 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6239 tcp->mct_norms = (OPJ_FLOAT64*)
6240 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6242 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6245 for (i = 0; i < image->numcomps; i++) {
6246 opj_tccp_t *tccp = &tcp->tccps[i];
6247 tccp->m_dc_level_shift = l_dc_shift[i];
6250 opj_j2k_setup_mct_encoding(tcp,image);
6254 for (i = 0; i < image->numcomps; i++) {
6255 opj_tccp_t *tccp = &tcp->tccps[i];
6256 opj_image_comp_t * l_comp = &(image->comps[i]);
6258 if (! l_comp->sgnd) {
6259 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6264 for (i = 0; i < image->numcomps; i++) {
6265 opj_tccp_t *tccp = &tcp->tccps[i];
6267 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6268 tccp->numresolutions = parameters->numresolution;
6269 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6270 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6271 tccp->cblksty = parameters->mode;
6272 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6273 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6276 if (i == parameters->roi_compno) {
6277 tccp->roishift = parameters->roi_shift;
6282 if(parameters->cp_cinema) {
6283 //Precinct size for lowest frequency subband=128
6286 //Precinct size at all other resolutions = 256
6287 for (j = 1; j < tccp->numresolutions; j++) {
6292 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6293 OPJ_INT32 p = 0, it_res;
6294 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6295 if (p < parameters->res_spec) {
6297 if (parameters->prcw_init[p] < 1) {
6298 tccp->prcw[it_res] = 1;
6300 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6303 if (parameters->prch_init[p] < 1) {
6304 tccp->prch[it_res] = 1;
6306 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6310 int res_spec = parameters->res_spec;
6311 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6312 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6314 if (size_prcw < 1) {
6315 tccp->prcw[it_res] = 1;
6317 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6320 if (size_prch < 1) {
6321 tccp->prch[it_res] = 1;
6323 tccp->prch[it_res] = int_floorlog2(size_prch);
6327 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6330 for (j = 0; j < tccp->numresolutions; j++) {
6337 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6341 if (parameters->mct_data) {
6342 opj_free(parameters->mct_data);
6343 parameters->mct_data = 00;
6349 static void opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6351 assert(cstr_index != 00);
6353 /* expand the list? */
6354 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6355 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6356 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6359 /* add the marker */
6360 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6361 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6362 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6363 cstr_index->marknum++;
6367 static void opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6369 assert(cstr_index != 00);
6370 assert(cstr_index->tile_index != 00);
6372 /* expand the list? */
6373 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6374 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6375 cstr_index->tile_index[tileno].marker =
6376 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
6377 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6380 /* add the marker */
6381 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6382 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6383 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6384 cstr_index->tile_index[tileno].marknum++;
6386 if (type == J2K_MS_SOT) {
6387 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6389 if (cstr_index->tile_index[tileno].tp_index)
6390 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6397 * -----------------------------------------------------------------------
6398 * -----------------------------------------------------------------------
6399 * -----------------------------------------------------------------------
6403 * Ends the decompression procedures and possibiliy add data to be read after the
6406 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
6407 opj_stream_private_t *p_stream,
6408 opj_event_mgr_t * p_manager
6418 * Reads a jpeg2000 codestream header structure.
6421 * @param p_stream the stream to read data from.
6422 * @param p_j2k the jpeg2000 codec.
6423 * @param p_manager the user event manager.
6425 * @return true if the box is valid.
6427 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
6428 opj_j2k_v2_t* p_j2k,
6429 opj_image_t** p_image,
6430 opj_event_mgr_t* p_manager )
6433 assert(p_j2k != 00);
6434 assert(p_stream != 00);
6435 assert(p_manager != 00);
6437 /* create an empty image header */
6438 p_j2k->m_private_image = opj_image_create0();
6439 if (! p_j2k->m_private_image) {
6443 /* customization of the validation */
6444 opj_j2k_setup_decoding_validation(p_j2k);
6446 /* validation of the parameters codec */
6447 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6448 opj_image_destroy(p_j2k->m_private_image);
6449 p_j2k->m_private_image = NULL;
6453 /* customization of the encoding */
6454 opj_j2k_setup_header_reading(p_j2k);
6457 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6458 opj_image_destroy(p_j2k->m_private_image);
6459 p_j2k->m_private_image = NULL;
6463 *p_image = opj_image_create0();
6468 /* Copy codestream image information to the output image */
6469 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6471 /*Allocate and initialize some elements of codestrem index*/
6472 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6480 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
6482 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
6485 assert(p_j2k != 00);
6487 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6489 /* DEVELOPER CORNER, add your custom procedures */
6490 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6495 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
6496 * are valid. Developpers wanting to extend the library can add their own validation procedures.
6498 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
6501 assert(p_j2k != 00);
6503 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6504 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6505 /* DEVELOPER CORNER, add your custom validation procedure */
6511 * The mct encoding validation procedure.
6513 * @param p_j2k the jpeg2000 codec to validate.
6514 * @param p_stream the input stream to validate.
6515 * @param p_manager the user event manager.
6517 * @return true if the parameters are correct.
6519 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
6520 opj_stream_private_t *p_stream,
6521 opj_event_mgr_t * p_manager )
6523 opj_bool l_is_valid = OPJ_TRUE;
6527 assert(p_j2k != 00);
6528 assert(p_stream != 00);
6529 assert(p_manager != 00);
6531 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6532 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6533 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
6535 for (i=0;i<l_nb_tiles;++i) {
6536 if (l_tcp->mct == 2) {
6537 opj_tccp_t * l_tccp = l_tcp->tccps;
6538 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6540 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
6541 l_is_valid &= ! (l_tccp->qmfbid & 1);
6552 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
6555 OPJ_UINT32 l_indix = 1;
6556 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
6557 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
6558 OPJ_UINT32 l_mct_size,l_nb_elem;
6559 OPJ_FLOAT32 * l_data, * l_current_data;
6560 opj_tccp_t * l_tccp;
6563 assert(p_tcp != 00);
6565 if (p_tcp->mct != 2) {
6569 if (p_tcp->m_mct_decoding_matrix) {
6570 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6571 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6573 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));
6574 if (! p_tcp->m_mct_records) {
6577 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6579 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6581 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6583 if (l_mct_deco_data->m_data) {
6584 opj_free(l_mct_deco_data->m_data);
6585 l_mct_deco_data->m_data = 00;
6588 l_mct_deco_data->m_index = l_indix++;
6589 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
6590 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
6591 l_nb_elem = p_image->numcomps * p_image->numcomps;
6592 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
6593 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6595 if (! l_mct_deco_data->m_data) {
6599 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);
6601 l_mct_deco_data->m_data_size = l_mct_size;
6602 ++p_tcp->m_nb_mct_records;
6605 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6606 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6607 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));
6609 if (! p_tcp->m_mct_records) {
6613 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6614 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6616 if (l_mct_deco_data) {
6617 l_mct_deco_data = l_mct_offset_data - 1;
6621 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6623 if (l_mct_offset_data->m_data) {
6624 opj_free(l_mct_offset_data->m_data);
6625 l_mct_offset_data->m_data = 00;
6628 l_mct_offset_data->m_index = l_indix++;
6629 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
6630 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
6631 l_nb_elem = p_image->numcomps;
6632 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
6633 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6635 if (! l_mct_offset_data->m_data) {
6639 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
6641 opj_free(l_mct_offset_data->m_data);
6642 l_mct_offset_data->m_data = 00;
6646 l_tccp = p_tcp->tccps;
6647 l_current_data = l_data;
6649 for (i=0;i<l_nb_elem;++i) {
6650 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
6654 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
6658 l_mct_offset_data->m_data_size = l_mct_size;
6660 ++p_tcp->m_nb_mct_records;
6662 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
6663 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
6664 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6665 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6667 if (! p_tcp->m_mcc_records) {
6670 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6671 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));
6675 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6676 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
6677 l_mcc_data->m_is_irreversible = 1;
6678 l_mcc_data->m_nb_comps = p_image->numcomps;
6679 l_mcc_data->m_index = l_indix++;
6680 l_mcc_data->m_offset_array = l_mct_offset_data;
6681 ++p_tcp->m_nb_mcc_records;
6687 * Builds the cp decoder parameters to use to decode tile.
6689 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
6690 opj_stream_private_t *p_stream,
6691 opj_event_mgr_t * p_manager )
6693 /* add here initialization of cp
6694 copy paste of setup_decoder */
6702 * Builds the cp encoder parameters to use to encode tile.
6704 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
6705 opj_stream_private_t *p_stream,
6706 opj_event_mgr_t * p_manager )
6708 /* add here initialization of cp
6709 copy paste of setup_encoder */
6717 * The default encoding validation procedure without any extension.
6719 * @param p_j2k the jpeg2000 codec to validate.
6720 * @param p_stream the input stream to validate.
6721 * @param p_manager the user event manager.
6723 * @return true if the parameters are correct.
6725 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
6726 opj_stream_private_t *p_stream,
6727 opj_event_mgr_t * p_manager )
6729 opj_bool l_is_valid = OPJ_TRUE;
6732 assert(p_j2k != 00);
6733 assert(p_stream != 00);
6734 assert(p_manager != 00);
6736 /* STATE checking */
6737 /* make sure the state is at 0 */
6738 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
6740 /* POINTER validation */
6741 /* make sure a p_j2k codec is present */
6742 l_is_valid &= (p_j2k->m_procedure_list != 00);
6743 /* make sure a validation list is present */
6744 l_is_valid &= (p_j2k->m_validation_list != 00);
6746 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6747 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6751 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6752 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6756 /* PARAMETER VALIDATION */
6761 * The default decoding validation procedure without any extension.
6763 * @param p_j2k the jpeg2000 codec to validate.
6764 * @param p_stream the input stream to validate.
6765 * @param p_manager the user event manager.
6767 * @return true if the parameters are correct.
6769 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
6770 opj_stream_private_t *p_stream,
6771 opj_event_mgr_t * p_manager
6774 opj_bool l_is_valid = OPJ_TRUE;
6777 assert(p_j2k != 00);
6778 assert(p_stream != 00);
6779 assert(p_manager != 00);
6782 /* STATE checking */
6783 /* make sure the state is at 0 */
6785 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
6787 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
6789 /* POINTER validation */
6790 /* make sure a p_j2k codec is present */
6791 /* make sure a procedure list is present */
6792 l_is_valid &= (p_j2k->m_procedure_list != 00);
6793 /* make sure a validation list is present */
6794 l_is_valid &= (p_j2k->m_validation_list != 00);
6796 /* PARAMETER VALIDATION */
6800 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
6801 opj_stream_private_t *p_stream,
6802 opj_event_mgr_t * p_manager)
6804 OPJ_UINT32 l_current_marker;
6805 OPJ_UINT32 l_marker_size;
6806 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6809 assert(p_stream != 00);
6810 assert(p_j2k != 00);
6811 assert(p_manager != 00);
6813 /* We enter in the main header */
6814 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6816 /* Try to read the SOC marker, the codestream must begin with SOC marker */
6817 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
6818 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6822 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6823 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6824 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6828 /* Read 2 bytes as the new marker ID */
6829 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6831 /* Try to read until the SOT is detected */
6832 while (l_current_marker != J2K_MS_SOT) {
6834 /* Check if the current marker ID is valid */
6835 if (l_current_marker < 0xff00) {
6836 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6840 /* Get the marker handler from the marker ID */
6841 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6843 /* Manage case where marker is unknown */
6844 if (l_marker_handler->id == J2K_MS_UNK) {
6845 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
6846 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6850 if (l_current_marker == J2K_MS_SOT)
6851 break; /* SOT marker is detected main header is completely read */
6852 else /* Get the marker handler from the marker ID */
6853 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6856 /* Check if the marker is known and if it is the right place to find it */
6857 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6858 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6862 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6863 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6864 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6868 /* read 2 bytes as the marker size */
6869 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6870 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6872 /* Check if the marker size is compatible with the header data size */
6873 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6874 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6875 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6876 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6879 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6882 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6883 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) {
6884 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6888 /* Read the marker segment with the correct marker handler */
6889 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6890 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6894 /* Add the marker to the codestream index*/
6895 opj_j2k_add_mhmarker(p_j2k->cstr_index,
6896 l_marker_handler->id,
6897 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6898 l_marker_size + 4 );
6900 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6901 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6902 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6906 /* read 2 bytes as the new marker ID */
6907 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6910 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
6912 /* Position of the last element if the main header */
6913 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6915 /* Next step: read a tile-part header */
6916 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6922 * Excutes the given procedures on the given codec.
6924 * @param p_procedure_list the list of procedures to execute
6925 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6926 * @param p_stream the stream to execute the procedures on.
6927 * @param p_manager the user manager.
6929 * @return true if all the procedures were successfully executed.
6931 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
6932 opj_procedure_list_t * p_procedure_list,
6933 opj_stream_private_t *p_stream,
6934 opj_event_mgr_t * p_manager )
6936 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6937 opj_bool l_result = OPJ_TRUE;
6938 OPJ_UINT32 l_nb_proc, i;
6941 assert(p_procedure_list != 00);
6942 assert(p_j2k != 00);
6943 assert(p_stream != 00);
6944 assert(p_manager != 00);
6947 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6948 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);
6950 for (i=0;i<l_nb_proc;++i) {
6951 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6955 /* and clear the procedure list at the end.*/
6956 opj_procedure_list_clear(p_procedure_list);
6961 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
6962 opj_stream_private_t *p_stream,
6963 opj_event_mgr_t * p_manager
6966 opj_tcp_v2_t * l_tcp = 00;
6967 opj_tcp_v2_t * l_default_tcp = 00;
6968 OPJ_UINT32 l_nb_tiles;
6970 opj_tccp_t *l_current_tccp = 00;
6971 OPJ_UINT32 l_tccp_size;
6972 OPJ_UINT32 l_mct_size;
6973 opj_image_t * l_image;
6974 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6975 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6976 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6977 OPJ_UINT32 l_offset;
6980 assert(p_j2k != 00);
6981 assert(p_stream != 00);
6982 assert(p_manager != 00);
6984 l_image = p_j2k->m_private_image;
6985 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6986 l_tcp = p_j2k->m_cp.tcps;
6987 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6988 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6989 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6992 for (i=0; i<l_nb_tiles; ++i) {
6993 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6994 l_current_tccp = l_tcp->tccps;
6995 /*Copy default coding parameters into the current tile coding parameters*/
6996 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6997 /* Initialize some values of the current tile coding parameters*/
6999 l_tcp->ppt_data = 00;
7000 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7001 l_tcp->tccps = l_current_tccp;
7003 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7004 if (l_default_tcp->m_mct_decoding_matrix) {
7005 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7006 if (! l_tcp->m_mct_decoding_matrix ) {
7009 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7012 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7013 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7014 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7015 if (! l_tcp->m_mct_records) {
7018 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7020 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7021 l_src_mct_rec = l_default_tcp->m_mct_records;
7022 l_dest_mct_rec = l_tcp->m_mct_records;
7024 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7026 if (l_src_mct_rec->m_data) {
7028 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7029 if(! l_dest_mct_rec->m_data) {
7032 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7039 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7040 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7041 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7042 if (! l_tcp->m_mcc_records) {
7045 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7047 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7048 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7049 l_dest_mcc_rec = l_tcp->m_mcc_records;
7051 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7053 if (l_src_mcc_rec->m_decorrelation_array) {
7054 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7055 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7058 if (l_src_mcc_rec->m_offset_array) {
7059 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7060 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7067 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7068 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7070 /* Move to next tile cp*/
7074 /* Create the current tile decoder*/
7075 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7076 if (! p_j2k->m_tcd ) {
7080 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7081 tcd_destroy_v2(p_j2k->m_tcd);
7083 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7091 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7092 * with the marker value.
7093 * @param p_id Marker value to look up
7095 * @return the handler associated with the id.
7097 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7099 const opj_dec_memory_marker_handler_t *e;
7100 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7101 if (e->id == p_id) {
7102 break; /* we find a handler corresponding to the marker ID*/
7110 * Destroys a jpeg2000 codec.
7112 * @param p_j2k the jpeg20000 structure to destroy.
7114 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7120 if (p_j2k->m_is_decoder) {
7122 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7123 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7124 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7125 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7128 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7129 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7130 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7131 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7136 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7137 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7138 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7141 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7142 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7143 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7144 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7147 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7148 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7149 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7150 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7154 tcd_destroy_v2(p_j2k->m_tcd);
7156 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7157 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7159 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7160 p_j2k->m_procedure_list = 00;
7162 opj_procedure_list_destroy(p_j2k->m_validation_list);
7163 p_j2k->m_procedure_list = 00;
7165 j2k_destroy_cstr_index(p_j2k->cstr_index);
7166 p_j2k->cstr_index = NULL;
7168 opj_image_destroy(p_j2k->m_private_image);
7169 p_j2k->m_private_image = NULL;
7171 opj_image_destroy(p_j2k->m_output_image);
7172 p_j2k->m_output_image = NULL;
7177 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7181 if (p_cstr_ind->marker) {
7182 opj_free(p_cstr_ind->marker);
7183 p_cstr_ind->marker = NULL;
7186 if (p_cstr_ind->tile_index) {
7187 OPJ_UINT32 it_tile = 0;
7189 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7191 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7192 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7193 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7196 if(p_cstr_ind->tile_index[it_tile].tp_index){
7197 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7198 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7201 if(p_cstr_ind->tile_index[it_tile].marker){
7202 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7203 p_cstr_ind->tile_index[it_tile].marker = NULL;
7208 opj_free( p_cstr_ind->tile_index);
7209 p_cstr_ind->tile_index = NULL;
7212 opj_free(p_cstr_ind);
7219 * Destroys a tile coding parameter structure.
7221 * @param p_tcp the tile coding parameter to destroy.
7223 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7229 if (p_tcp->ppt_buffer != 00) {
7230 opj_free(p_tcp->ppt_buffer);
7231 p_tcp->ppt_buffer = 00;
7234 if (p_tcp->tccps != 00) {
7235 opj_free(p_tcp->tccps);
7239 if (p_tcp->m_mct_coding_matrix != 00) {
7240 opj_free(p_tcp->m_mct_coding_matrix);
7241 p_tcp->m_mct_coding_matrix = 00;
7244 if (p_tcp->m_mct_decoding_matrix != 00) {
7245 opj_free(p_tcp->m_mct_decoding_matrix);
7246 p_tcp->m_mct_decoding_matrix = 00;
7249 if (p_tcp->m_mcc_records) {
7250 opj_free(p_tcp->m_mcc_records);
7251 p_tcp->m_mcc_records = 00;
7252 p_tcp->m_nb_max_mcc_records = 0;
7253 p_tcp->m_nb_mcc_records = 0;
7256 if (p_tcp->m_mct_records) {
7257 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7260 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7261 if (l_mct_data->m_data) {
7262 opj_free(l_mct_data->m_data);
7263 l_mct_data->m_data = 00;
7269 opj_free(p_tcp->m_mct_records);
7270 p_tcp->m_mct_records = 00;
7273 if (p_tcp->mct_norms != 00) {
7274 opj_free(p_tcp->mct_norms);
7275 p_tcp->mct_norms = 00;
7278 opj_j2k_tcp_data_destroy(p_tcp);
7283 * Destroys the data inside a tile coding parameter structure.
7285 * @param p_tcp the tile coding parameter which contain data to destroy.
7287 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7289 if (p_tcp->m_data) {
7290 opj_free(p_tcp->m_data);
7291 p_tcp->m_data = NULL;
7292 p_tcp->m_data_size = 0;
7297 * Destroys a coding parameter structure.
7299 * @param p_cp the coding parameter to destroy.
7301 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7303 OPJ_UINT32 l_nb_tiles;
7304 opj_tcp_v2_t * l_current_tile = 00;
7315 l_current_tile = p_cp->tcps;
7316 l_nb_tiles = p_cp->th * p_cp->tw;
7319 (i = 0; i < l_nb_tiles; ++i)
7321 opj_j2k_tcp_destroy(l_current_tile);
7324 opj_free(p_cp->tcps);
7328 (p_cp->ppm_buffer != 00)
7330 opj_free(p_cp->ppm_buffer);
7331 p_cp->ppm_buffer = 00;
7334 (p_cp->comment != 00)
7336 opj_free(p_cp->comment);
7340 (! p_cp->m_is_decoder)
7343 (p_cp->m_specific_param.m_enc.m_matrice)
7345 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7346 p_cp->m_specific_param.m_enc.m_matrice = 00;
7354 * Reads a tile header.
7355 * @param p_j2k the jpeg2000 codec.
7356 * @param p_stream the stream to write data to.
7357 * @param p_manager the user event manager.
7359 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7360 OPJ_UINT32 * p_tile_index,
7361 OPJ_UINT32 * p_data_size,
7362 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7363 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7364 OPJ_UINT32 * p_nb_comps,
7366 opj_stream_private_t *p_stream,
7367 opj_event_mgr_t * p_manager )
7369 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7370 OPJ_UINT32 l_marker_size;
7371 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7372 opj_tcp_v2_t * l_tcp = NULL;
7373 OPJ_UINT32 l_nb_tiles;
7376 assert(p_stream != 00);
7377 assert(p_j2k != 00);
7378 assert(p_manager != 00);
7380 /* Reach the End Of Codestream ?*/
7381 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7382 l_current_marker = J2K_MS_EOC;
7384 /* We need to encounter a SOT marker (a new tile-part header) */
7385 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7389 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7390 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7392 /* Try to read until the Start Of Data is detected */
7393 while (l_current_marker != J2K_MS_SOD) {
7395 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7396 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7397 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7401 /* Read 2 bytes from the buffer as the marker size */
7402 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7404 /* Why this condition? FIXME */
7405 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7406 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7408 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7410 /* Get the marker handler from the marker ID */
7411 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7413 /* Check if the marker is known and if it is the right place to find it */
7414 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7415 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7418 /* FIXME manage case of unknown marker as in the main header ? */
7420 /* Check if the marker size is compatible with the header data size */
7421 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7422 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7423 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7424 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7427 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7430 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7431 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) {
7432 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7436 /* Read the marker segment with the correct marker handler */
7437 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7438 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7442 /* Add the marker to the codestream index*/
7443 opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
7445 l_marker_handler->id,
7446 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7447 l_marker_size + 4 );
7449 /* Keep the position of the last SOT marker read */
7450 if ( l_marker_handler->id == J2K_MS_SOT ) {
7451 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7452 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7454 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7459 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7460 /* Skip the rest of the tile part header*/
7461 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) {
7462 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7465 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7468 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7469 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7470 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7473 /* Read 2 bytes from the buffer as the new marker ID */
7474 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7478 /* If we didn't skip data before, we need to read the SOD marker*/
7479 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7480 /* Try to read the SOD marker and skip data ? FIXME */
7481 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7487 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7488 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7489 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7490 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7494 /* Read 2 bytes from buffer as the new marker ID */
7495 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7499 /* Indicate we will try to read a new tile-part header*/
7500 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7501 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7502 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7504 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7505 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7506 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7510 /* Read 2 bytes from buffer as the new marker ID */
7511 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7515 /* Current marker is the EOC marker ?*/
7516 if (l_current_marker == J2K_MS_EOC) {
7517 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7518 p_j2k->m_current_tile_number = 0;
7519 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
7524 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
7525 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
7526 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7528 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
7529 ++p_j2k->m_current_tile_number;
7533 if (p_j2k->m_current_tile_number == l_nb_tiles) {
7534 *p_go_on = OPJ_FALSE;
7540 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
7541 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7545 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
7546 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
7548 *p_tile_index = p_j2k->m_current_tile_number;
7549 *p_go_on = OPJ_TRUE;
7550 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
7551 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
7552 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
7553 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
7554 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
7555 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
7557 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
7563 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
7564 OPJ_UINT32 p_tile_index,
7566 OPJ_UINT32 p_data_size,
7567 opj_stream_private_t *p_stream,
7568 opj_event_mgr_t * p_manager )
7570 OPJ_UINT32 l_current_marker;
7571 OPJ_BYTE l_data [2];
7572 opj_tcp_v2_t * l_tcp;
7575 assert(p_stream != 00);
7576 assert(p_j2k != 00);
7577 assert(p_manager != 00);
7579 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
7580 || (p_tile_index != p_j2k->m_current_tile_number) ) {
7584 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
7585 if (! l_tcp->m_data) {
7586 opj_j2k_tcp_destroy(l_tcp);
7590 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
7594 p_j2k->cstr_index) ) {
7595 opj_j2k_tcp_destroy(l_tcp);
7596 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
7600 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
7604 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
7605 * we destroy just the data which will be re-read in read_tile_header*/
7606 /*opj_j2k_tcp_destroy(l_tcp);
7607 p_j2k->m_tcd->tcp = 0;*/
7608 opj_j2k_tcp_data_destroy(l_tcp);
7610 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7611 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
7613 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
7614 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
7615 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7619 opj_read_bytes(l_data,&l_current_marker,2);
7621 if (l_current_marker == J2K_MS_EOC) {
7622 p_j2k->m_current_tile_number = 0;
7623 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
7625 else if (l_current_marker != J2K_MS_SOT)
7627 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
7636 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
7638 OPJ_UINT32 i,j,k = 0;
7639 OPJ_UINT32 l_width_src,l_height_src;
7640 OPJ_UINT32 l_width_dest,l_height_dest;
7641 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
7642 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
7643 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
7644 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
7645 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
7647 opj_image_comp_t * l_img_comp_src = 00;
7648 opj_image_comp_t * l_img_comp_dest = 00;
7650 opj_tcd_tilecomp_v2_t * l_tilec = 00;
7651 opj_image_t * l_image_src = 00;
7652 OPJ_UINT32 l_size_comp, l_remaining;
7653 OPJ_INT32 * l_dest_ptr;
7654 opj_tcd_resolution_v2_t* l_res= 00;
7656 l_tilec = p_tcd->tcd_image->tiles->comps;
7657 l_image_src = p_tcd->image;
7658 l_img_comp_src = l_image_src->comps;
7660 l_img_comp_dest = p_output_image->comps;
7662 for (i=0; i<l_image_src->numcomps; i++) {
7664 /* Allocate output component buffer if necessary */
7665 if (!l_img_comp_dest->data) {
7667 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
7668 if (! l_img_comp_dest->data) {
7673 /* Copy info from decoded comp image to output image */
7674 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
7677 /* Compute the precision of the output buffer */
7678 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
7679 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
7680 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
7686 if (l_size_comp == 3) {
7691 /* Current tile component size*/
7693 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
7694 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
7697 l_width_src = (l_res->x1 - l_res->x0);
7698 l_height_src = (l_res->y1 - l_res->y0);
7700 /* Border of the current output component*/
7701 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
7702 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
7703 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
7704 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
7707 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
7708 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
7712 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
7713 * of the input buffer (decoded tile component) which will be move
7714 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
7715 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
7716 * by this input area.
7718 assert( l_res->x0 >= 0);
7719 assert( l_res->x1 >= 0);
7720 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
7721 l_start_x_dest = l_res->x0 - l_x0_dest;
7722 l_offset_x0_src = 0;
7724 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7725 l_width_dest = l_width_src;
7726 l_offset_x1_src = 0;
7729 l_width_dest = l_x1_dest - l_res->x0 ;
7730 l_offset_x1_src = l_width_src - l_width_dest;
7734 l_start_x_dest = 0 ;
7735 l_offset_x0_src = l_x0_dest - l_res->x0;
7737 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7738 l_width_dest = l_width_src - l_offset_x0_src;
7739 l_offset_x1_src = 0;
7742 l_width_dest = l_img_comp_dest->w ;
7743 l_offset_x1_src = l_res->x1 - l_x1_dest;
7747 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
7748 l_start_y_dest = l_res->y0 - l_y0_dest;
7749 l_offset_y0_src = 0;
7751 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7752 l_height_dest = l_height_src;
7753 l_offset_y1_src = 0;
7756 l_height_dest = l_y1_dest - l_res->y0 ;
7757 l_offset_y1_src = l_height_src - l_height_dest;
7761 l_start_y_dest = 0 ;
7762 l_offset_y0_src = l_y0_dest - l_res->y0;
7764 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7765 l_height_dest = l_height_src - l_offset_y0_src;
7766 l_offset_y1_src = 0;
7769 l_height_dest = l_img_comp_dest->h ;
7770 l_offset_y1_src = l_res->y1 - l_y1_dest;
7774 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
7779 /* Compute the input buffer offset */
7780 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
7781 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
7782 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
7784 /* Compute the output buffer offset */
7785 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
7786 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
7788 /* Move the output buffer to the first place where we will write*/
7789 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
7792 fprintf(stdout, "COMPO[%d]:\n",i);
7793 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
7794 "\t tile offset:%d, %d, %d, %d\n"
7795 "\t buffer offset: %d; %d, %d\n",
7796 l_res->x0, l_res->y0, l_width_src, l_height_src,
7797 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
7798 l_start_offset_src, l_line_offset_src, l_end_offset_src);
7800 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
7801 "\t start offset: %d, line offset= %d\n",
7802 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
7806 switch (l_size_comp) {
7809 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
7810 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
7812 if (l_img_comp_src->sgnd) {
7813 for (j = 0 ; j < l_height_dest ; ++j) {
7814 for ( k = 0 ; k < l_width_dest ; ++k) {
7815 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
7818 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
7819 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
7823 for ( j = 0 ; j < l_height_dest ; ++j ) {
7824 for ( k = 0 ; k < l_width_dest ; ++k) {
7825 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7828 l_dest_ptr+= l_line_offset_dest;
7829 l_src_ptr += l_line_offset_src;
7833 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7834 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7839 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7840 l_src_ptr += l_start_offset_src;
7842 if (l_img_comp_src->sgnd) {
7843 for (j=0;j<l_height_dest;++j) {
7844 for (k=0;k<l_width_dest;++k) {
7845 *(l_dest_ptr++) = *(l_src_ptr++);
7848 l_dest_ptr+= l_line_offset_dest;
7849 l_src_ptr += l_line_offset_src ;
7853 for (j=0;j<l_height_dest;++j) {
7854 for (k=0;k<l_width_dest;++k) {
7855 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7858 l_dest_ptr+= l_line_offset_dest;
7859 l_src_ptr += l_line_offset_src ;
7863 l_src_ptr += l_end_offset_src;
7864 p_data = (OPJ_BYTE*) l_src_ptr;
7869 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7870 l_src_ptr += l_start_offset_src;
7872 for (j=0;j<l_height_dest;++j) {
7873 for (k=0;k<l_width_dest;++k) {
7874 *(l_dest_ptr++) = (*(l_src_ptr++));
7877 l_dest_ptr+= l_line_offset_dest;
7878 l_src_ptr += l_line_offset_src ;
7881 l_src_ptr += l_end_offset_src;
7882 p_data = (OPJ_BYTE*) l_src_ptr;
7896 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7898 * @param p_j2k the jpeg2000 codec.
7899 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
7900 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
7901 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
7902 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
7903 * @param p_manager the user event manager
7905 * @return true if the area could be set.
7907 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
7908 opj_image_t* p_image,
7909 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7910 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7911 opj_event_mgr_t * p_manager )
7913 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7914 opj_image_t * l_image = p_j2k->m_private_image;
7917 OPJ_INT32 l_comp_x1, l_comp_y1;
7918 opj_image_comp_t* l_img_comp = NULL;
7920 /* Check if we are read the main header */
7921 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
7922 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7926 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7927 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
7929 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7930 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7931 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7932 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7938 /* Check if the positions provided by the user are correct */
7941 assert(p_start_x >= 0 );
7942 assert(p_start_y >= 0 );
7944 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
7945 opj_event_msg_v2(p_manager, EVT_ERROR,
7946 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7947 p_start_x, l_image->x1);
7950 else if ((OPJ_UINT32)p_start_x < l_image->x0){
7951 opj_event_msg_v2(p_manager, EVT_WARNING,
7952 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7953 p_start_x, l_image->x0);
7954 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7955 p_image->x0 = l_image->x0;
7958 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7959 p_image->x0 = p_start_x;
7963 if ((OPJ_UINT32)p_start_y > l_image->y1){
7964 opj_event_msg_v2(p_manager, EVT_ERROR,
7965 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7966 p_start_y, l_image->y1);
7969 else if ((OPJ_UINT32)p_start_y < l_image->y0){
7970 opj_event_msg_v2(p_manager, EVT_WARNING,
7971 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7972 p_start_y, l_image->y0);
7973 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7974 p_image->y0 = l_image->y0;
7977 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7978 p_image->y0 = p_start_y;
7982 assert((OPJ_UINT32)p_end_x > 0);
7983 assert((OPJ_UINT32)p_end_y > 0);
7984 if ((OPJ_UINT32)p_end_x < l_image->x0) {
7985 opj_event_msg_v2(p_manager, EVT_ERROR,
7986 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7987 p_end_x, l_image->x0);
7990 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
7991 opj_event_msg_v2(p_manager, EVT_WARNING,
7992 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7993 p_end_x, l_image->x1);
7994 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7995 p_image->x1 = l_image->x1;
7998 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7999 p_image->x1 = p_end_x;
8003 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8004 opj_event_msg_v2(p_manager, EVT_ERROR,
8005 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8006 p_end_y, l_image->y0);
8009 if ((OPJ_UINT32)p_end_y > l_image->y1){
8010 opj_event_msg_v2(p_manager, EVT_WARNING,
8011 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8012 p_end_y, l_image->y1);
8013 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8014 p_image->y1 = l_image->y1;
8017 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8018 p_image->y1 = p_end_y;
8022 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8024 l_img_comp = p_image->comps;
8025 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8029 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8030 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8031 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8032 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8034 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8035 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8037 opj_event_msg_v2(p_manager, EVT_ERROR,
8038 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8042 l_img_comp->w = l_w;
8044 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8045 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8047 opj_event_msg_v2(p_manager, EVT_ERROR,
8048 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8052 l_img_comp->h = l_h;
8057 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8058 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8065 /* ----------------------------------------------------------------------- */
8066 /* J2K / JPT decoder interface */
8067 /* ----------------------------------------------------------------------- */
8069 * Creates a J2K decompression structure.
8071 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8073 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8075 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8079 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8081 l_j2k->m_is_decoder = 1;
8082 l_j2k->m_cp.m_is_decoder = 1;
8084 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8085 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8086 opj_j2k_destroy(l_j2k);
8089 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8091 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8092 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8093 opj_j2k_destroy(l_j2k);
8097 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8099 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8101 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8103 /* codestream index creation */
8104 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8106 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8107 if (!l_j2k->cstr_index){
8108 opj_j2k_destroy(l_j2k);
8112 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8115 /* validation list creation */
8116 l_j2k->m_validation_list = opj_procedure_list_create();
8117 if (! l_j2k->m_validation_list) {
8118 opj_j2k_destroy(l_j2k);
8122 /* execution list creation */
8123 l_j2k->m_procedure_list = opj_procedure_list_create();
8124 if (! l_j2k->m_procedure_list) {
8125 opj_j2k_destroy(l_j2k);
8133 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8135 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8136 opj_calloc(1,sizeof(opj_codestream_index_t));
8140 cstr_index->maxmarknum = 100;
8141 cstr_index->marknum = 0;
8142 cstr_index->marker = (opj_marker_info_t*)
8143 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8144 if (!cstr_index-> marker)
8147 cstr_index->tile_index = NULL;
8154 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8156 * @param p_tile_no the tile index.
8157 * @param p_comp_no the component being outputted.
8158 * @param p_j2k the J2K codec.
8160 * @return the number of bytes taken by the SPCod element.
8162 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8163 OPJ_UINT32 p_tile_no,
8164 OPJ_UINT32 p_comp_no )
8166 opj_cp_v2_t *l_cp = 00;
8167 opj_tcp_v2_t *l_tcp = 00;
8168 opj_tccp_t *l_tccp = 00;
8171 assert(p_j2k != 00);
8173 l_cp = &(p_j2k->m_cp);
8174 l_tcp = &l_cp->tcps[p_tile_no];
8175 l_tccp = &l_tcp->tccps[p_comp_no];
8177 /* preconditions again */
8178 assert(p_tile_no < (l_cp->tw * l_cp->th));
8179 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8181 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8182 return 5 + l_tccp->numresolutions;
8190 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8192 * @param p_comp_no the component number to output.
8193 * @param p_stream the stream to write data to.
8194 * @param p_j2k J2K codec.
8195 * @param p_manager the user event manager.
8198 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8199 OPJ_UINT32 p_tile_no,
8200 OPJ_UINT32 p_comp_no,
8202 OPJ_UINT32 * p_header_size,
8203 struct opj_event_mgr * p_manager )
8206 opj_cp_v2_t *l_cp = 00;
8207 opj_tcp_v2_t *l_tcp = 00;
8208 opj_tccp_t *l_tccp = 00;
8211 assert(p_j2k != 00);
8212 assert(p_header_size != 00);
8213 assert(p_manager != 00);
8214 assert(p_data != 00);
8216 l_cp = &(p_j2k->m_cp);
8217 l_tcp = &l_cp->tcps[p_tile_no];
8218 l_tccp = &l_tcp->tccps[p_comp_no];
8220 /* preconditions again */
8221 assert(p_tile_no < (l_cp->tw * l_cp->th));
8222 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8224 if (*p_header_size < 5) {
8225 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8229 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8232 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8235 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8238 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8241 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8244 *p_header_size = *p_header_size - 5;
8246 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8248 if (*p_header_size < l_tccp->numresolutions) {
8249 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8253 for (i = 0; i < l_tccp->numresolutions; ++i) {
8254 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8258 *p_header_size = *p_header_size - l_tccp->numresolutions;
8265 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8266 * @param p_header_data the data contained in the COM box.
8267 * @param p_j2k the jpeg2000 codec.
8268 * @param p_header_size the size of the data contained in the COM marker.
8269 * @param p_manager the user event manager.
8271 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8273 OPJ_BYTE * p_header_data,
8274 OPJ_UINT32 * p_header_size,
8275 opj_event_mgr_t * p_manager)
8277 OPJ_UINT32 i, l_tmp;
8278 opj_cp_v2_t *l_cp = NULL;
8279 opj_tcp_v2_t *l_tcp = NULL;
8280 opj_tccp_t *l_tccp = NULL;
8281 OPJ_BYTE * l_current_ptr = NULL;
8284 assert(p_j2k != 00);
8285 assert(p_manager != 00);
8286 assert(p_header_data != 00);
8288 l_cp = &(p_j2k->m_cp);
8289 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8290 &l_cp->tcps[p_j2k->m_current_tile_number] :
8291 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8293 /* precondition again */
8294 assert(compno < p_j2k->m_private_image->numcomps);
8296 l_tccp = &l_tcp->tccps[compno];
8297 l_current_ptr = p_header_data;
8299 /* make sure room is sufficient */
8300 if (*p_header_size < 5) {
8301 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8305 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8306 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8309 /* If user wants to remove more resolutions than the codestream contains, return error */
8310 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8311 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8312 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8313 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8317 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8321 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8325 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8328 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8331 *p_header_size = *p_header_size - 5;
8333 /* use custom precinct size ? */
8334 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8335 if (*p_header_size < l_tccp->numresolutions) {
8336 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8340 for (i = 0; i < l_tccp->numresolutions; ++i) {
8341 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8343 l_tccp->prcw[i] = l_tmp & 0xf;
8344 l_tccp->prch[i] = l_tmp >> 4;
8347 *p_header_size = *p_header_size - l_tccp->numresolutions;
8350 /* set default size for the precinct width and height */
8351 for (i = 0; i < l_tccp->numresolutions; ++i) {
8352 l_tccp->prcw[i] = 15;
8353 l_tccp->prch[i] = 15;
8357 #ifdef WIP_REMOVE_MSD
8359 if (p_j2k->cstr_info && compno == 0) {
8360 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8362 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8363 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8364 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8365 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8366 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8369 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8370 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8379 * Copies the tile component parameters of all the component from the first tile component.
8381 * @param p_j2k the J2k codec.
8383 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8387 opj_cp_v2_t *l_cp = NULL;
8388 opj_tcp_v2_t *l_tcp = NULL;
8389 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8390 OPJ_UINT32 l_prc_size;
8393 assert(p_j2k != 00);
8395 l_cp = &(p_j2k->m_cp);
8396 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8397 &l_cp->tcps[p_j2k->m_current_tile_number] :
8398 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8400 l_ref_tccp = &l_tcp->tccps[0];
8401 l_copied_tccp = l_ref_tccp + 1;
8402 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8404 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8405 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8406 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8407 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8408 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8409 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8410 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8411 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8417 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
8419 * @param p_tile_no the tile index.
8420 * @param p_comp_no the component being outputted.
8421 * @param p_j2k the J2K codec.
8423 * @return the number of bytes taken by the SPCod element.
8425 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
8426 OPJ_UINT32 p_tile_no,
8427 OPJ_UINT32 p_comp_no )
8429 OPJ_UINT32 l_num_bands;
8431 opj_cp_v2_t *l_cp = 00;
8432 opj_tcp_v2_t *l_tcp = 00;
8433 opj_tccp_t *l_tccp = 00;
8436 assert(p_j2k != 00);
8438 l_cp = &(p_j2k->m_cp);
8439 l_tcp = &l_cp->tcps[p_tile_no];
8440 l_tccp = &l_tcp->tccps[p_comp_no];
8442 /* preconditions again */
8443 assert(p_tile_no < l_cp->tw * l_cp->th);
8444 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8446 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8448 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8449 return 1 + l_num_bands;
8452 return 1 + 2*l_num_bands;
8457 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
8459 * @param p_tile_no the tile to output.
8460 * @param p_comp_no the component number to output.
8461 * @param p_data the data buffer.
8462 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
8463 * @param p_j2k J2K codec.
8464 * @param p_manager the user event manager.
8467 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
8468 OPJ_UINT32 p_tile_no,
8469 OPJ_UINT32 p_comp_no,
8471 OPJ_UINT32 * p_header_size,
8472 struct opj_event_mgr * p_manager )
8474 OPJ_UINT32 l_header_size;
8475 OPJ_UINT32 l_band_no, l_num_bands;
8476 OPJ_UINT32 l_expn,l_mant;
8478 opj_cp_v2_t *l_cp = 00;
8479 opj_tcp_v2_t *l_tcp = 00;
8480 opj_tccp_t *l_tccp = 00;
8483 assert(p_j2k != 00);
8484 assert(p_header_size != 00);
8485 assert(p_manager != 00);
8486 assert(p_data != 00);
8488 l_cp = &(p_j2k->m_cp);
8489 l_tcp = &l_cp->tcps[p_tile_no];
8490 l_tccp = &l_tcp->tccps[p_comp_no];
8492 /* preconditions again */
8493 assert(p_tile_no < l_cp->tw * l_cp->th);
8494 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8496 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8498 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8499 l_header_size = 1 + l_num_bands;
8501 if (*p_header_size < l_header_size) {
8502 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8506 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8509 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8510 l_expn = l_tccp->stepsizes[l_band_no].expn;
8511 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8516 l_header_size = 1 + 2*l_num_bands;
8518 if (*p_header_size < l_header_size) {
8519 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8523 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8526 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8527 l_expn = l_tccp->stepsizes[l_band_no].expn;
8528 l_mant = l_tccp->stepsizes[l_band_no].mant;
8530 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
8535 *p_header_size = *p_header_size - l_header_size;
8541 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
8543 * @param p_comp_no the component being targeted.
8544 * @param p_header_data the data contained in the COM box.
8545 * @param p_j2k the jpeg2000 codec.
8546 * @param p_header_size the size of the data contained in the COM marker.
8547 * @param p_manager the user event manager.
8549 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
8550 OPJ_UINT32 p_comp_no,
8551 OPJ_BYTE* p_header_data,
8552 OPJ_UINT32 * p_header_size,
8553 opj_event_mgr_t * p_manager
8557 OPJ_UINT32 l_band_no;
8558 opj_cp_v2_t *l_cp = 00;
8559 opj_tcp_v2_t *l_tcp = 00;
8560 opj_tccp_t *l_tccp = 00;
8561 OPJ_BYTE * l_current_ptr = 00;
8562 OPJ_UINT32 l_tmp, l_num_band;
8565 assert(p_j2k != 00);
8566 assert(p_manager != 00);
8567 assert(p_header_data != 00);
8569 l_cp = &(p_j2k->m_cp);
8570 /* come from tile part header or main header ?*/
8571 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
8572 &l_cp->tcps[p_j2k->m_current_tile_number] :
8573 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8575 /* precondition again*/
8576 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8578 l_tccp = &l_tcp->tccps[p_comp_no];
8579 l_current_ptr = p_header_data;
8581 if (*p_header_size < 1) {
8582 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
8585 *p_header_size -= 1;
8587 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
8590 l_tccp->qntsty = l_tmp & 0x1f;
8591 l_tccp->numgbits = l_tmp >> 5;
8592 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8596 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
8598 (*p_header_size) / 2;
8600 if( l_num_band > J2K_MAXBANDS ) {
8601 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
8602 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
8603 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
8604 /*return OPJ_FALSE;*/
8609 if (l_cp->correct) {
8611 /* if JPWL is on, we check whether there are too many subbands */
8612 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
8613 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
8614 "JPWL: bad number of subbands in Sqcx (%d)\n",
8617 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
8620 /* we try to correct */
8622 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
8623 "- setting number of bands to %d => HYPOTHESIS!!!\n",
8628 #endif /* USE_JPWL */
8630 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8631 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8632 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
8634 if (l_band_no < J2K_MAXBANDS){
8635 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
8636 l_tccp->stepsizes[l_band_no].mant = 0;
8639 *p_header_size = *p_header_size - l_num_band;
8642 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8643 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
8645 if (l_band_no < J2K_MAXBANDS){
8646 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
8647 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
8650 *p_header_size = *p_header_size - 2*l_num_band;
8653 /* Add Antonin : if scalar_derived -> compute other stepsizes */
8654 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8655 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
8656 l_tccp->stepsizes[l_band_no].expn =
8657 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
8658 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
8659 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
8667 * Copies the tile component parameters of all the component from the first tile component.
8669 * @param p_j2k the J2k codec.
8671 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
8674 opj_cp_v2_t *l_cp = NULL;
8675 opj_tcp_v2_t *l_tcp = NULL;
8676 opj_tccp_t *l_ref_tccp = NULL;
8677 opj_tccp_t *l_copied_tccp = NULL;
8681 assert(p_j2k != 00);
8683 l_cp = &(p_j2k->m_cp);
8684 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
8685 &l_cp->tcps[p_j2k->m_current_tile_number] :
8686 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8688 l_ref_tccp = &l_tcp->tccps[0];
8689 l_copied_tccp = l_ref_tccp + 1;
8690 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
8692 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
8693 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
8694 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
8695 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
8701 * Dump some elements from the J2K decompression structure .
8703 *@param p_j2k the jpeg2000 codec.
8704 *@param flag flag to describe what elments are dump.
8705 *@param out_stream output stream where dump the elements.
8708 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
8710 /* Check if the flag is compatible with j2k file*/
8711 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
8712 fprintf(out_stream, "Wrong flag\n");
8716 /* Dump the image_header */
8717 if (flag & OPJ_IMG_INFO){
8718 if (p_j2k->m_private_image)
8719 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
8722 /* Dump the codestream info from main header */
8723 if (flag & OPJ_J2K_MH_INFO){
8724 opj_j2k_dump_MH_info(p_j2k, out_stream);
8728 /* Dump the codestream info of the current tile */
8729 if (flag & OPJ_J2K_TH_INFO){
8733 /* Dump the codestream index from main header */
8734 if (flag & OPJ_J2K_MH_IND){
8735 opj_j2k_dump_MH_index(p_j2k, out_stream);
8738 /* Dump the codestream index of the current tile */
8739 if (flag & OPJ_J2K_TH_IND){
8746 * Dump index elements of the codestream extract from the main header.
8748 *@param p_j2k the jpeg2000 codec.
8749 *@param out_stream output stream where dump the elements.
8752 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
8754 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
8755 OPJ_UINT32 it_marker, it_tile, it_tile_part;
8757 fprintf(out_stream, "Codestream index from main header: {\n");
8759 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
8760 "\t Main header end position=%" PRIi64 "\n",
8761 cstr_index->main_head_start, cstr_index->main_head_end);
8763 fprintf(out_stream, "\t Marker list: {\n");
8765 if (cstr_index->marker){
8766 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
8767 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8768 cstr_index->marker[it_marker].type,
8769 cstr_index->marker[it_marker].pos,
8770 cstr_index->marker[it_marker].len );
8774 fprintf(out_stream, "\t }\n");
8777 if (cstr_index->tile_index){
8779 /* Simple test to avoid to write empty information*/
8780 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
8781 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8782 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
8785 if (l_acc_nb_of_tile_part)
8787 fprintf(out_stream, "\t Tile index: {\n");
8789 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8790 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
8792 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
8794 if (cstr_index->tile_index[it_tile].tp_index){
8795 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
8796 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
8798 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
8799 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
8800 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
8804 if (cstr_index->tile_index[it_tile].marker){
8805 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
8806 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8807 cstr_index->tile_index[it_tile].marker[it_marker].type,
8808 cstr_index->tile_index[it_tile].marker[it_marker].pos,
8809 cstr_index->tile_index[it_tile].marker[it_marker].len );
8813 fprintf(out_stream,"\t }\n");
8817 fprintf(out_stream,"}\n");
8822 * Dump info elements of the codestream extract from the main header.
8824 *@param p_j2k the jpeg2000 codec.
8825 *@param out_stream output stream where dump the elements.
8828 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
8830 opj_tcp_v2_t * l_default_tile=NULL;
8832 fprintf(out_stream, "Codestream info from main header: {\n");
8834 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
8835 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
8836 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
8838 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8842 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
8844 fprintf(out_stream, "\t default tile {\n");
8845 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
8846 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
8847 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
8848 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
8850 for (compno = 0; compno < numcomps; compno++) {
8851 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
8853 OPJ_INT32 bandno, numbands;
8856 fprintf(out_stream, "\t\t comp %d {\n", compno);
8857 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
8858 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
8859 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
8860 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
8861 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
8862 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
8864 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
8865 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
8866 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
8868 fprintf(out_stream, "\n");
8870 /* quantization style*/
8871 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
8872 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
8873 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
8874 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
8875 for (bandno = 0; bandno < numbands; bandno++) {
8876 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
8877 l_tccp->stepsizes[bandno].expn);
8879 fprintf(out_stream, "\n");
8882 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
8884 fprintf(out_stream, "\t\t }\n");
8885 } /*end of component of default tile*/
8886 fprintf(out_stream, "\t }\n"); /*end of default tile*/
8890 fprintf(out_stream, "}\n");
8895 * Dump an image header structure.
8897 *@param img_header the image header to dump.
8898 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
8899 *@param out_stream output stream where dump the elements.
8901 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
8906 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
8910 fprintf(out_stream, "Image info {\n");
8911 tab[0] = '\t';tab[1] = '\0';
8914 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
8915 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
8916 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
8918 if (img_header->comps){
8920 for (compno = 0; compno < img_header->numcomps; compno++) {
8921 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
8922 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
8923 fprintf(out_stream,"%s}\n",tab);
8927 fprintf(out_stream, "}\n");
8931 * Dump a component image header structure.
8933 *@param comp_header the component image header to dump.
8934 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
8935 *@param out_stream output stream where dump the elements.
8937 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
8942 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
8945 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
8948 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
8949 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
8950 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
8953 fprintf(out_stream, "}\n");
8958 * Get the codestream info from a JPEG2000 codec.
8960 *@param p_j2k the component image header to dump.
8962 *@return the codestream information extract from the jpg2000 codec
8964 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
8967 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
8968 opj_tcp_v2_t *l_default_tile;
8969 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
8971 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
8973 cstr_info->tx0 = p_j2k->m_cp.tx0;
8974 cstr_info->ty0 = p_j2k->m_cp.ty0;
8975 cstr_info->tdx = p_j2k->m_cp.tdx;
8976 cstr_info->tdy = p_j2k->m_cp.tdy;
8977 cstr_info->tw = p_j2k->m_cp.tw;
8978 cstr_info->th = p_j2k->m_cp.th;
8980 cstr_info->tile_info = NULL; /* Not fill from the main header*/
8982 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8984 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
8985 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
8986 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
8987 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
8989 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
8991 for (compno = 0; compno < numcomps; compno++) {
8992 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
8993 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
8994 OPJ_INT32 bandno, numbands;
8997 l_tccp_info->csty = l_tccp->csty;
8998 l_tccp_info->numresolutions = l_tccp->numresolutions;
8999 l_tccp_info->cblkw = l_tccp->cblkw;
9000 l_tccp_info->cblkh = l_tccp->cblkh;
9001 l_tccp_info->cblksty = l_tccp->cblksty;
9002 l_tccp_info->qmfbid = l_tccp->qmfbid;
9003 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9005 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9006 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9009 /* quantization style*/
9010 l_tccp_info->qntsty = l_tccp->qntsty;
9011 l_tccp_info->numgbits = l_tccp->numgbits;
9013 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9014 if (numbands < J2K_MAXBANDS) {
9015 for (bandno = 0; bandno < numbands; bandno++) {
9016 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9017 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9022 l_tccp_info->roishift = l_tccp->roishift;
9030 * Get the codestream index from a JPEG2000 codec.
9032 *@param p_j2k the component image header to dump.
9034 *@return the codestream index extract from the jpg2000 codec
9036 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9038 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9039 opj_calloc(1,sizeof(opj_codestream_index_t));
9043 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9044 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9045 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9047 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9048 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9049 if (!l_cstr_index->marker){
9050 opj_free( l_cstr_index);
9054 if (p_j2k->cstr_index->marker)
9055 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9057 opj_free(l_cstr_index->marker);
9058 l_cstr_index->marker = NULL;
9061 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9062 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9063 if (!l_cstr_index->tile_index){
9064 opj_free( l_cstr_index->marker);
9065 opj_free( l_cstr_index);
9069 if (!p_j2k->cstr_index->tile_index){
9070 opj_free(l_cstr_index->tile_index);
9071 l_cstr_index->tile_index = NULL;
9074 OPJ_UINT32 it_tile = 0;
9075 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9078 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9080 l_cstr_index->tile_index[it_tile].marker =
9081 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9083 if (!l_cstr_index->tile_index[it_tile].marker) {
9084 OPJ_UINT32 it_tile_free;
9086 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9087 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9090 opj_free( l_cstr_index->tile_index);
9091 opj_free( l_cstr_index->marker);
9092 opj_free( l_cstr_index);
9096 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9097 memcpy( l_cstr_index->tile_index[it_tile].marker,
9098 p_j2k->cstr_index->tile_index[it_tile].marker,
9099 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9101 opj_free(l_cstr_index->tile_index[it_tile].marker);
9102 l_cstr_index->tile_index[it_tile].marker = NULL;
9105 /* Tile part index*/
9106 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9108 l_cstr_index->tile_index[it_tile].tp_index =
9109 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9111 if(!l_cstr_index->tile_index[it_tile].tp_index){
9112 OPJ_UINT32 it_tile_free;
9114 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9115 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9116 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9119 opj_free( l_cstr_index->tile_index);
9120 opj_free( l_cstr_index->marker);
9121 opj_free( l_cstr_index);
9125 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9126 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9127 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9128 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9131 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9132 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9135 /* Packet index (NOT USED)*/
9136 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9137 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9142 return l_cstr_index;
9145 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9147 OPJ_UINT32 it_tile=0;
9149 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9150 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9151 if (!p_j2k->cstr_index->tile_index)
9154 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9155 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9156 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9157 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9158 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9159 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9169 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9170 opj_stream_private_t *p_stream,
9171 opj_event_mgr_t * p_manager)
9173 opj_bool l_go_on = OPJ_TRUE;
9174 OPJ_UINT32 l_current_tile_no;
9175 OPJ_UINT32 l_data_size,l_max_data_size;
9176 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9177 OPJ_UINT32 l_nb_comps;
9178 OPJ_BYTE * l_current_data;
9180 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9181 if (! l_current_data) {
9184 l_max_data_size = 1000;
9189 if (! opj_j2k_read_tile_header( p_j2k,
9192 &l_tile_x0, &l_tile_y0,
9193 &l_tile_x1, &l_tile_y1,
9198 opj_free(l_current_data);
9206 if (l_data_size > l_max_data_size) {
9207 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9208 if (! l_current_data) {
9209 opj_free(l_current_data);
9213 l_max_data_size = l_data_size;
9216 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9217 opj_free(l_current_data);
9220 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);
9222 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9223 opj_free(l_current_data);
9226 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9230 opj_free(l_current_data);
9236 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9238 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9241 assert(p_j2k != 00);
9243 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9244 /* DEVELOPER CORNER, add your custom procedures */
9249 * Read and decode one tile.
9251 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9252 opj_stream_private_t *p_stream,
9253 opj_event_mgr_t * p_manager)
9255 opj_bool l_go_on = OPJ_TRUE;
9256 OPJ_UINT32 l_current_tile_no;
9257 OPJ_UINT32 l_tile_no_to_dec;
9258 OPJ_UINT32 l_data_size,l_max_data_size;
9259 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9260 OPJ_UINT32 l_nb_comps;
9261 OPJ_BYTE * l_current_data;
9263 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9264 if (! l_current_data) {
9267 l_max_data_size = 1000;
9269 /*Allocate and initialize some elements of codestrem index if not already done*/
9270 if( !p_j2k->cstr_index->tile_index)
9272 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9273 opj_free(l_current_data);
9277 /* Move into the codestream to the first SOT used to decode the desired tile */
9278 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9279 if (p_j2k->cstr_index->tile_index)
9280 if(p_j2k->cstr_index->tile_index->tp_index)
9282 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9283 /* the index for this tile has not been built,
9284 * so move to the last SOT read */
9285 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9286 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9291 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)) ) {
9292 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9296 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9297 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9298 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9302 if (! opj_j2k_read_tile_header( p_j2k,
9305 &l_tile_x0, &l_tile_y0,
9306 &l_tile_x1, &l_tile_y1,
9311 opj_free(l_current_data);
9320 if (l_data_size > l_max_data_size) {
9321 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9322 if (! l_current_data) {
9323 opj_free(l_current_data);
9327 l_max_data_size = l_data_size;
9332 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9333 opj_free(l_current_data);
9336 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);
9338 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9339 opj_free(l_current_data);
9342 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9344 if(l_current_tile_no == l_tile_no_to_dec)
9346 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9347 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9348 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9354 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);
9359 opj_free(l_current_data);
9366 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9368 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9371 assert(p_j2k != 00);
9373 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9374 /* DEVELOPER CORNER, add your custom procedures */
9380 * Decodes the tiles of the stream.
9382 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9383 opj_stream_private_t * p_stream,
9384 opj_image_t * p_image,
9385 opj_event_mgr_t * p_manager)
9392 p_j2k->m_output_image = opj_image_create0();
9393 if (! (p_j2k->m_output_image)) {
9396 opj_copy_image_header(p_image, p_j2k->m_output_image);
9398 /* customization of the decoding */
9399 opj_j2k_setup_decoding(p_j2k);
9401 /* Decode the codestream */
9402 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9403 opj_image_destroy(p_j2k->m_private_image);
9404 p_j2k->m_private_image = NULL;
9408 /* Move data and copy one information from codec to output image*/
9409 for (compno = 0; compno < p_image->numcomps; compno++) {
9410 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9411 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9412 p_j2k->m_output_image->comps[compno].data = NULL;
9420 * Get the decoded tile.
9422 * @param p_j2k the jpeg2000 codestream codec.
9423 * @param p_stream input_stream
9424 * @param p_image output image. .
9425 * @param p_manager the user event manager
9426 * @param tile_index index of the tile we want decode
9428 * @return true if succeed.
9430 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
9431 opj_stream_private_t *p_stream,
9432 opj_image_t* p_image,
9433 opj_event_mgr_t * p_manager,
9434 OPJ_UINT32 tile_index )
9437 OPJ_UINT32 l_tile_x, l_tile_y;
9438 opj_image_comp_t* l_img_comp;
9441 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
9445 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9446 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);
9450 /* Compute the dimension of the desired tile*/
9451 l_tile_x = tile_index % p_j2k->m_cp.tw;
9452 l_tile_y = tile_index / p_j2k->m_cp.tw;
9454 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9455 if (p_image->x0 < p_j2k->m_private_image->x0)
9456 p_image->x0 = p_j2k->m_private_image->x0;
9457 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9458 if (p_image->x1 > p_j2k->m_private_image->x1)
9459 p_image->x1 = p_j2k->m_private_image->x1;
9461 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9462 if (p_image->y0 < p_j2k->m_private_image->y0)
9463 p_image->y0 = p_j2k->m_private_image->y0;
9464 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9465 if (p_image->y1 > p_j2k->m_private_image->y1)
9466 p_image->y1 = p_j2k->m_private_image->y1;
9468 l_img_comp = p_image->comps;
9469 for (compno=0; compno < p_image->numcomps; ++compno)
9471 OPJ_INT32 l_comp_x1, l_comp_y1;
9473 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9475 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9476 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9477 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9478 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9480 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9481 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9486 /* Destroy the previous output image*/
9487 if (p_j2k->m_output_image)
9488 opj_image_destroy(p_j2k->m_output_image);
9490 /* Create the ouput image from the information previously computed*/
9491 p_j2k->m_output_image = opj_image_create0();
9492 if (! (p_j2k->m_output_image)) {
9495 opj_copy_image_header(p_image, p_j2k->m_output_image);
9497 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9499 /* customization of the decoding */
9500 opj_j2k_setup_decoding_tile(p_j2k);
9502 /* Decode the codestream */
9503 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9504 opj_image_destroy(p_j2k->m_private_image);
9505 p_j2k->m_private_image = NULL;
9509 /* Move data and copy one information from codec to output image*/
9510 for (compno = 0; compno < p_image->numcomps; compno++) {
9511 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9513 if (p_image->comps[compno].data)
9514 opj_free(p_image->comps[compno].data);
9516 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9518 p_j2k->m_output_image->comps[compno].data = NULL;
9524 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
9525 OPJ_UINT32 res_factor,
9526 opj_event_mgr_t * p_manager)
9530 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9532 if (p_j2k->m_private_image) {
9533 if (p_j2k->m_private_image->comps) {
9534 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9535 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9536 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9537 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9538 if ( res_factor >= max_res){
9539 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
9542 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
9555 * Encodes all the tiles in a row.
9557 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
9558 opj_stream_private_t *p_stream,
9559 opj_event_mgr_t * p_manager )
9562 OPJ_UINT32 l_nb_tiles;
9563 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
9564 OPJ_BYTE * l_current_data;
9567 assert(p_j2k != 00);
9568 assert(p_stream != 00);
9569 assert(p_manager != 00);
9571 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9572 if (! l_current_data) {
9575 l_max_tile_size = 1000;
9577 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9578 for (i=0;i<l_nb_tiles;++i) {
9579 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
9580 opj_free(l_current_data);
9584 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
9585 if (l_current_tile_size > l_max_tile_size) {
9586 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
9587 if (! l_current_data) {
9590 l_max_tile_size = l_current_tile_size;
9593 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
9595 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
9600 opj_free(l_current_data);
9605 * Ends the compression procedures and possibility add data to be read after the
9608 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
9609 opj_stream_private_t *p_stream,
9610 opj_event_mgr_t * p_manager)
9612 /* customization of the encoding */
9613 opj_j2k_setup_end_compress(p_j2k);
9615 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
9625 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
9627 * @param p_j2k the jpeg2000 codec.
9628 * @param p_stream the stream object.
9629 * @param p_manager the user event manager.
9631 * @return true if the codec is valid.
9633 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
9634 opj_stream_private_t *p_stream,
9635 opj_image_t * p_image,
9636 opj_event_mgr_t * p_manager)
9639 assert(p_j2k != 00);
9640 assert(p_stream != 00);
9641 assert(p_manager != 00);
9643 p_j2k->m_private_image = opj_image_create0();
9644 opj_copy_image_header(p_image, p_j2k->m_private_image);
9646 // TODO_MSD: Find a better way
9647 if (p_image->comps) {
9649 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
9650 if (p_image->comps[it_comp].data) {
9651 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
9652 p_image->comps[it_comp].data = NULL;
9658 /* customization of the validation */
9659 opj_j2k_setup_encoding_validation (p_j2k);
9661 /* validation of the parameters codec */
9662 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
9666 /* customization of the encoding */
9667 opj_j2k_setup_header_writting(p_j2k);
9670 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9680 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
9681 OPJ_UINT32 p_tile_index,
9682 opj_stream_private_t *p_stream,
9683 opj_event_mgr_t * p_manager )
9686 if (p_tile_index != p_j2k->m_current_tile_number) {
9687 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
9691 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);
9693 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
9694 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
9695 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
9697 /* initialisation before tile encoding */
9698 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9708 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
9710 OPJ_UINT32 i,j,k = 0;
9711 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
9712 opj_image_comp_t * l_img_comp = 00;
9713 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9714 opj_image_t * l_image = 00;
9715 OPJ_UINT32 l_size_comp, l_remaining;
9716 OPJ_INT32 * l_src_ptr;
9717 l_tilec = p_tcd->tcd_image->tiles->comps;
9718 l_image = p_tcd->image;
9719 l_img_comp = l_image->comps;
9721 for (i=0;i<p_tcd->image->numcomps;++i) {
9722 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
9723 l_remaining = l_img_comp->prec & 7; /* (%8) */
9728 if (l_size_comp == 3) {
9732 l_width = (l_tilec->x1 - l_tilec->x0);
9733 l_height = (l_tilec->y1 - l_tilec->y0);
9734 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
9735 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
9736 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
9737 l_stride = l_image_width - l_width;
9738 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
9740 switch (l_size_comp) {
9743 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
9744 if (l_img_comp->sgnd) {
9745 for (j=0;j<l_height;++j) {
9746 for (k=0;k<l_width;++k) {
9747 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
9751 l_src_ptr += l_stride;
9755 for (j=0;j<l_height;++j) {
9756 for (k=0;k<l_width;++k) {
9757 *(l_dest_ptr) = (*l_src_ptr)&0xff;
9761 l_src_ptr += l_stride;
9765 p_data = (OPJ_BYTE*) l_dest_ptr;
9770 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
9771 if (l_img_comp->sgnd) {
9772 for (j=0;j<l_height;++j) {
9773 for (k=0;k<l_width;++k) {
9774 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
9776 l_src_ptr += l_stride;
9780 for (j=0;j<l_height;++j) {
9781 for (k=0;k<l_width;++k) {
9782 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9784 l_src_ptr += l_stride;
9788 p_data = (OPJ_BYTE*) l_dest_ptr;
9793 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
9794 for (j=0;j<l_height;++j) {
9795 for (k=0;k<l_width;++k) {
9796 *(l_dest_ptr++) = *(l_src_ptr++);
9798 l_src_ptr += l_stride;
9801 p_data = (OPJ_BYTE*) l_dest_ptr;
9814 * @param p_j2k the jpeg2000 codec.
9815 * @param p_stream the stream to write data to.
9816 * @param p_manager the user event manager.
9818 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
9820 OPJ_UINT32 p_data_size,
9821 opj_stream_private_t *p_stream,
9822 opj_event_mgr_t * p_manager )
9824 opj_tcd_v2_t * l_tcd = 00;
9825 opj_cp_v2_t * l_cp = 00;
9826 opj_tcp_v2_t * l_tcp = 00;
9827 OPJ_UINT32 l_nb_bytes_written;
9828 OPJ_BYTE * l_current_data = 00;
9829 OPJ_UINT32 l_tile_size = 0;
9830 OPJ_UINT32 l_available_data;
9833 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9835 l_tcd = p_j2k->m_tcd;
9836 l_cp = &(p_j2k->m_cp);
9837 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
9839 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
9840 l_available_data = l_tile_size;
9841 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
9843 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
9844 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
9848 l_nb_bytes_written = 0;
9849 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9852 l_current_data += l_nb_bytes_written;
9853 l_available_data -= l_nb_bytes_written;
9855 l_nb_bytes_written = 0;
9856 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9860 l_available_data -= l_nb_bytes_written;
9861 l_nb_bytes_written = l_tile_size - l_available_data;
9863 if ( opj_stream_write_data( p_stream,
9864 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
9865 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
9869 ++p_j2k->m_current_tile_number;
9876 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
9877 * are valid. Developers wanting to extend the library can add their own validation procedures.
9879 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
9882 assert(p_j2k != 00);
9884 /* DEVELOPER CORNER, insert your custom procedures */
9885 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
9887 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
9888 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
9891 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );
9892 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );
9893 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
9897 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
9898 * are valid. Developers wanting to extend the library can add their own validation procedures.
9900 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
9903 assert(p_j2k != 00);
9905 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
9906 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
9908 /* DEVELOPER CORNER, add your custom validation procedure */
9909 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
9914 * Sets up the procedures to do on writing header.
9915 * Developers wanting to extend the library can add their own writing procedures.
9917 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
9920 assert(p_j2k != 00);
9922 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );
9923 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
9924 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
9925 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
9926 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
9929 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
9930 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components );
9931 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
9933 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
9934 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
9938 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);
9940 if (p_j2k->m_cp.comment != 00) {
9941 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
9944 /* DEVELOPER CORNER, insert your custom procedures */
9945 if (p_j2k->m_cp.rsiz & MCT) {
9946 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
9948 /* End of Developer Corner */
9950 if (p_j2k->cstr_index) {
9951 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
9954 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
9955 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
9959 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
9961 OPJ_UINT32 * p_data_written,
9962 OPJ_UINT32 p_total_data_size,
9963 opj_stream_private_t *p_stream,
9964 struct opj_event_mgr * p_manager )
9967 OPJ_UINT32 l_nb_bytes_written = 0;
9968 OPJ_UINT32 l_current_nb_bytes_written;
9969 OPJ_BYTE * l_begin_data = 00;
9971 opj_tcp_v2_t *l_tcp = 00;
9972 opj_tcd_v2_t * l_tcd = 00;
9973 opj_cp_v2_t * l_cp = 00;
9975 l_tcd = p_j2k->m_tcd;
9976 l_cp = &(p_j2k->m_cp);
9977 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
9979 l_tcd->cur_pino = 0;
9981 /*Get number of tile parts*/
9982 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
9987 l_current_nb_bytes_written = 0;
9988 l_begin_data = p_data;
9989 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
9994 l_nb_bytes_written += l_current_nb_bytes_written;
9995 p_data += l_current_nb_bytes_written;
9996 p_total_data_size -= l_current_nb_bytes_written;
9998 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
9999 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10000 l_current_nb_bytes_written = 0;
10001 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10002 l_nb_bytes_written += l_current_nb_bytes_written;
10003 p_data += l_current_nb_bytes_written;
10004 p_total_data_size -= l_current_nb_bytes_written;
10006 l_current_nb_bytes_written = 0;
10007 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10008 l_nb_bytes_written += l_current_nb_bytes_written;
10009 p_data += l_current_nb_bytes_written;
10010 p_total_data_size -= l_current_nb_bytes_written;
10013 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10014 l_current_nb_bytes_written = 0;
10015 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10016 l_nb_bytes_written += l_current_nb_bytes_written;
10017 p_data += l_current_nb_bytes_written;
10018 p_total_data_size -= l_current_nb_bytes_written;
10022 l_current_nb_bytes_written = 0;
10023 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10027 l_nb_bytes_written += l_current_nb_bytes_written;
10028 * p_data_written = l_nb_bytes_written;
10030 /* Writing Psot in SOT marker */
10031 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10033 if (l_cp->m_specific_param.m_enc.m_cinema){
10034 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10040 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10042 OPJ_UINT32 * p_data_written,
10043 OPJ_UINT32 p_total_data_size,
10044 opj_stream_private_t *p_stream,
10045 struct opj_event_mgr * p_manager
10048 OPJ_UINT32 tilepartno=0;
10049 OPJ_UINT32 l_nb_bytes_written = 0;
10050 OPJ_UINT32 l_current_nb_bytes_written;
10051 OPJ_UINT32 l_part_tile_size;
10052 OPJ_UINT32 tot_num_tp;
10055 OPJ_BYTE * l_begin_data;
10056 opj_tcp_v2_t *l_tcp = 00;
10057 opj_tcd_v2_t * l_tcd = 00;
10058 opj_cp_v2_t * l_cp = 00;
10061 l_tcd = p_j2k->m_tcd;
10062 l_cp = &(p_j2k->m_cp);
10063 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10065 /*Get number of tile parts*/
10066 tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
10068 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10069 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10070 l_current_nb_bytes_written = 0;
10071 l_part_tile_size = 0;
10072 l_begin_data = p_data;
10074 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10078 l_nb_bytes_written += l_current_nb_bytes_written;
10079 p_data += l_current_nb_bytes_written;
10080 p_total_data_size -= l_current_nb_bytes_written;
10081 l_part_tile_size += l_nb_bytes_written;
10083 l_current_nb_bytes_written = 0;
10084 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10088 p_data += l_current_nb_bytes_written;
10089 l_nb_bytes_written += l_current_nb_bytes_written;
10090 p_total_data_size -= l_current_nb_bytes_written;
10091 l_part_tile_size += l_nb_bytes_written;
10093 /* Writing Psot in SOT marker */
10094 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10096 if (l_cp->m_specific_param.m_enc.m_cinema) {
10097 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10100 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10103 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10104 l_tcd->cur_pino = pino;
10106 /*Get number of tile parts*/
10107 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
10108 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10109 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10110 l_current_nb_bytes_written = 0;
10111 l_part_tile_size = 0;
10112 l_begin_data = p_data;
10114 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10118 l_nb_bytes_written += l_current_nb_bytes_written;
10119 p_data += l_current_nb_bytes_written;
10120 p_total_data_size -= l_current_nb_bytes_written;
10121 l_part_tile_size += l_current_nb_bytes_written;
10123 l_current_nb_bytes_written = 0;
10125 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10129 l_nb_bytes_written += l_current_nb_bytes_written;
10130 p_data += l_current_nb_bytes_written;
10131 p_total_data_size -= l_current_nb_bytes_written;
10132 l_part_tile_size += l_current_nb_bytes_written;
10134 /* Writing Psot in SOT marker */
10135 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10137 if (l_cp->m_specific_param.m_enc.m_cinema) {
10138 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10141 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10145 *p_data_written = l_nb_bytes_written;
10151 * Writes the updated tlm.
10153 * @param p_stream the stream to write data to.
10154 * @param p_j2k J2K codec.
10155 * @param p_manager the user event manager.
10157 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10158 struct opj_stream_private *p_stream,
10159 struct opj_event_mgr * p_manager )
10161 OPJ_UINT32 l_tlm_size;
10162 OPJ_SIZE_T l_tlm_position, l_current_position;
10164 /* preconditions */
10165 assert(p_j2k != 00);
10166 assert(p_manager != 00);
10167 assert(p_stream != 00);
10169 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10170 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10171 l_current_position = opj_stream_tell(p_stream);
10173 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10177 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) {
10181 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10190 * Ends the encoding, i.e. frees memory.
10192 * @param p_stream the stream to write data to.
10193 * @param p_j2k J2K codec.
10194 * @param p_manager the user event manager.
10196 opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10197 struct opj_stream_private *p_stream,
10198 struct opj_event_mgr * p_manager )
10200 /* preconditions */
10201 assert(p_j2k != 00);
10202 assert(p_manager != 00);
10203 assert(p_stream != 00);
10205 tcd_destroy_v2(p_j2k->m_tcd);
10208 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10209 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10210 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10211 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10214 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10215 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10216 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10219 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10225 * Destroys the memory associated with the decoding of headers.
10227 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10228 opj_stream_private_t *p_stream,
10229 opj_event_mgr_t * p_manager
10232 /* preconditions */
10233 assert(p_j2k != 00);
10234 assert(p_stream != 00);
10235 assert(p_manager != 00);
10237 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10238 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10239 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10242 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10251 * @param p_stream the stream to write data to.
10252 * @param p_j2k J2K codec.
10253 * @param p_manager the user event manager.
10255 opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
10256 struct opj_stream_private *p_stream,
10257 struct opj_event_mgr * p_manager )
10259 opj_codestream_info_t * l_cstr_info = 00;
10261 /* preconditions */
10262 assert(p_j2k != 00);
10263 assert(p_manager != 00);
10264 assert(p_stream != 00);
10267 /* TODO mergeV2: check this part which use cstr_info */
10268 /*l_cstr_info = p_j2k->cstr_info;
10272 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));
10274 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10275 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10277 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10279 l_cstr_info->tw = p_j2k->m_cp.tw;
10280 l_cstr_info->th = p_j2k->m_cp.th;
10282 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10283 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10284 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10285 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10287 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10289 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10291 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10293 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10294 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10297 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10299 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10301 /*l_cstr_info->maxmarknum = 100;
10302 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10303 l_cstr_info->marknum = 0;
10306 return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
10310 * Creates a tile-coder decoder.
10312 * @param p_stream the stream to write data to.
10313 * @param p_j2k J2K codec.
10314 * @param p_manager the user event manager.
10316 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10317 opj_stream_private_t *p_stream,
10318 opj_event_mgr_t * p_manager
10321 /* preconditions */
10322 assert(p_j2k != 00);
10323 assert(p_manager != 00);
10324 assert(p_stream != 00);
10326 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10328 if (! p_j2k->m_tcd) {
10329 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10333 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10334 tcd_destroy_v2(p_j2k->m_tcd);
10345 * @param p_j2k the jpeg2000 codec.
10346 * @param p_stream the stream to write data to.
10347 * @param p_manager the user event manager.
10349 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10350 OPJ_UINT32 p_tile_index,
10352 OPJ_UINT32 p_data_size,
10353 opj_stream_private_t *p_stream,
10354 opj_event_mgr_t * p_manager )
10356 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10357 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10361 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10362 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);