2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 static void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool opj_j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool opj_j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool opj_j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 opj_stream_private_t *p_stream,
135 opj_event_mgr_t * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool opj_j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void opj_j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool opj_j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool opj_j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
372 static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 * Writes the SOC marker (Start Of Codestream)
383 * @param p_stream the stream to write data to.
384 * @param p_j2k J2K codec.
385 * @param p_manager the user event manager.
387 static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
388 opj_stream_private_t *p_stream,
389 opj_event_mgr_t * p_manager );
392 * Reads a SOC marker (Start of Codestream)
393 * @param p_header_data the data contained in the SOC box.
394 * @param jp2 the jpeg2000 file codec.
395 * @param p_header_size the size of the data contained in the SOC marker.
396 * @param p_manager the user event manager.
398 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
399 opj_stream_private_t *p_stream,
400 opj_event_mgr_t * p_manager );
403 * Writes the SIZ marker (image and tile size)
405 * @param p_stream the stream to write data to.
406 * @param p_j2k J2K codec.
407 * @param p_manager the user event manager.
409 static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
410 opj_stream_private_t *p_stream,
411 opj_event_mgr_t * p_manager );
414 * Reads a SIZ marker (image and tile size)
415 * @param p_header_data the data contained in the SIZ box.
416 * @param jp2 the jpeg2000 file codec.
417 * @param p_header_size the size of the data contained in the SIZ marker.
418 * @param p_manager the user event manager.
420 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
421 OPJ_BYTE * p_header_data,
422 OPJ_UINT32 p_header_size,
423 opj_event_mgr_t * p_manager);
426 * Writes the COM marker (comment)
428 * @param p_stream the stream to write data to.
429 * @param p_j2k J2K codec.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
433 opj_stream_private_t *p_stream,
434 opj_event_mgr_t * p_manager );
437 * Reads a COM marker (comments)
438 * @param p_header_data the data contained in the COM box.
439 * @param jp2 the jpeg2000 file codec.
440 * @param p_header_size the size of the data contained in the COM marker.
441 * @param p_manager the user event manager.
443 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
444 OPJ_BYTE * p_header_data,
445 OPJ_UINT32 p_header_size,
446 opj_event_mgr_t * p_manager );
448 * Writes the COD marker (Coding style default)
450 * @param p_stream the stream to write data to.
451 * @param p_j2k J2K codec.
452 * @param p_manager the user event manager.
454 static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
455 opj_stream_private_t *p_stream,
456 opj_event_mgr_t * p_manager );
459 * Reads a COD marker (Coding Styke defaults)
460 * @param p_header_data the data contained in the COD box.
461 * @param p_j2k the jpeg2000 codec.
462 * @param p_header_size the size of the data contained in the COD marker.
463 * @param p_manager the user event manager.
465 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
466 OPJ_BYTE * p_header_data,
467 OPJ_UINT32 p_header_size,
468 opj_event_mgr_t * p_manager);
471 * Writes the COC marker (Coding style component)
473 * @param p_comp_number the index of the component to output.
474 * @param p_stream the stream to write data to.
475 * @param p_j2k J2K codec.
476 * @param p_manager the user event manager.
478 static opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
479 OPJ_UINT32 p_comp_no,
480 opj_stream_private_t *p_stream,
481 opj_event_mgr_t * p_manager );
483 * Writes the COC marker (Coding style component)
485 * @param p_comp_no the index of the component to output.
486 * @param p_stream the stream to write data to.
487 * @param p_j2k J2K codec.
488 * @param p_manager the user event manager.
490 static void opj_j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
491 OPJ_UINT32 p_comp_no,
493 OPJ_UINT32 * p_data_written,
494 opj_event_mgr_t * p_manager );
497 * Gets the maximum size taken by a coc.
499 * @param p_j2k the jpeg2000 codec to use.
501 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
505 * Reads a COC marker (Coding Style Component)
506 * @param p_header_data the data contained in the COC box.
507 * @param p_j2k the jpeg2000 codec.
508 * @param p_header_size the size of the data contained in the COC marker.
509 * @param p_manager the user event manager.
511 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
512 OPJ_BYTE * p_header_data,
513 OPJ_UINT32 p_header_size,
514 opj_event_mgr_t * p_manager );
517 * Writes the QCD marker (quantization default)
519 * @param p_comp_number the index of the component to output.
520 * @param p_stream the stream to write data to.
521 * @param p_j2k J2K codec.
522 * @param p_manager the user event manager.
524 static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
525 opj_stream_private_t *p_stream,
526 opj_event_mgr_t * p_manager );
529 * Reads a QCD marker (Quantization defaults)
530 * @param p_header_data the data contained in the QCD box.
531 * @param p_j2k the jpeg2000 codec.
532 * @param p_header_size the size of the data contained in the QCD marker.
533 * @param p_manager the user event manager.
535 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
536 OPJ_BYTE * p_header_data,
537 OPJ_UINT32 p_header_size,
538 opj_event_mgr_t * p_manager );
540 * Writes the QCC marker (quantization component)
542 * @param p_comp_no the index of the component to output.
543 * @param p_stream the stream to write data to.
544 * @param p_j2k J2K codec.
545 * @param p_manager the user event manager.
547 static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
548 OPJ_UINT32 p_comp_no,
549 opj_stream_private_t *p_stream,
550 opj_event_mgr_t * p_manager );
553 * Writes the QCC marker (quantization component)
555 * @param p_comp_no the index of the component to output.
556 * @param p_stream the stream to write data to.
557 * @param p_j2k J2K codec.
558 * @param p_manager the user event manager.
560 static void opj_j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
561 OPJ_UINT32 p_comp_no,
563 OPJ_UINT32 * p_data_written,
564 opj_event_mgr_t * p_manager );
567 * Gets the maximum size taken by a qcc.
569 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
572 * Reads a QCC marker (Quantization component)
573 * @param p_header_data the data contained in the QCC box.
574 * @param p_j2k the jpeg2000 codec.
575 * @param p_header_size the size of the data contained in the QCC marker.
576 * @param p_manager the user event manager.
578 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 opj_event_mgr_t * p_manager);
583 * Writes the POC marker (Progression Order Change)
585 * @param p_stream the stream to write data to.
586 * @param p_j2k J2K codec.
587 * @param p_manager the user event manager.
589 static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
590 opj_stream_private_t *p_stream,
591 opj_event_mgr_t * p_manager );
593 * Writes the POC marker (Progression Order Change)
595 * @param p_stream the stream to write data to.
596 * @param p_j2k J2K codec.
597 * @param p_manager the user event manager.
599 static void opj_j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
601 OPJ_UINT32 * p_data_written,
602 opj_event_mgr_t * p_manager );
604 * Gets the maximum size taken by the writting of a POC.
606 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
609 * Reads a POC marker (Progression Order Change)
611 * @param p_header_data the data contained in the POC box.
612 * @param p_j2k the jpeg2000 codec.
613 * @param p_header_size the size of the data contained in the POC marker.
614 * @param p_manager the user event manager.
616 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
617 OPJ_BYTE * p_header_data,
618 OPJ_UINT32 p_header_size,
619 opj_event_mgr_t * p_manager );
622 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
624 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
627 * Gets the maximum size taken by the headers of the SOT.
629 * @param p_j2k the jpeg2000 codec to use.
631 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
635 * Reads a CRG marker (Component registration)
637 * @param p_header_data the data contained in the TLM box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the TLM marker.
640 * @param p_manager the user event manager.
642 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
643 OPJ_BYTE * p_header_data,
644 OPJ_UINT32 p_header_size,
645 opj_event_mgr_t * p_manager );
647 * Reads a TLM marker (Tile Length Marker)
649 * @param p_header_data the data contained in the TLM box.
650 * @param p_j2k the jpeg2000 codec.
651 * @param p_header_size the size of the data contained in the TLM marker.
652 * @param p_manager the user event manager.
654 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
655 OPJ_BYTE * p_header_data,
656 OPJ_UINT32 p_header_size,
657 opj_event_mgr_t * p_manager);
660 * Writes the updated tlm.
662 * @param p_stream the stream to write data to.
663 * @param p_j2k J2K codec.
664 * @param p_manager the user event manager.
666 static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
667 struct opj_stream_private *p_stream,
668 struct opj_event_mgr * p_manager );
671 * Reads a PLM marker (Packet length, main header marker)
673 * @param p_header_data the data contained in the TLM box.
674 * @param p_j2k the jpeg2000 codec.
675 * @param p_header_size the size of the data contained in the TLM marker.
676 * @param p_manager the user event manager.
678 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
679 OPJ_BYTE * p_header_data,
680 OPJ_UINT32 p_header_size,
681 opj_event_mgr_t * p_manager);
683 * Reads a PLT marker (Packet length, tile-part header)
685 * @param p_header_data the data contained in the PLT box.
686 * @param p_j2k the jpeg2000 codec.
687 * @param p_header_size the size of the data contained in the PLT marker.
688 * @param p_manager the user event manager.
690 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
691 OPJ_BYTE * p_header_data,
692 OPJ_UINT32 p_header_size,
693 opj_event_mgr_t * p_manager );
697 * Reads a PPM marker (Packed packet headers, main header)
699 * @param p_header_data the data contained in the POC box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the POC marker.
702 * @param p_manager the user event manager.
705 static opj_bool j2k_read_ppm_v2 (
707 OPJ_BYTE * p_header_data,
708 OPJ_UINT32 p_header_size,
709 struct opj_event_mgr * p_manager
713 static opj_bool j2k_read_ppm_v3 (
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 struct opj_event_mgr * p_manager
722 * Reads a PPT marker (Packed packet headers, tile-part header)
724 * @param p_header_data the data contained in the PPT box.
725 * @param p_j2k the jpeg2000 codec.
726 * @param p_header_size the size of the data contained in the PPT marker.
727 * @param p_manager the user event manager.
729 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
730 OPJ_BYTE * p_header_data,
731 OPJ_UINT32 p_header_size,
732 opj_event_mgr_t * p_manager );
734 * Writes the TLM marker (Tile Length Marker)
736 * @param p_stream the stream to write data to.
737 * @param p_j2k J2K codec.
738 * @param p_manager the user event manager.
740 static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
741 opj_stream_private_t *p_stream,
742 opj_event_mgr_t * p_manager );
745 * Writes the SOT marker (Start of tile-part)
747 * @param p_stream the stream to write data to.
748 * @param p_j2k J2K codec.
749 * @param p_manager the user event manager.
751 static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
753 OPJ_UINT32 * p_data_written,
754 const opj_stream_private_t *p_stream,
755 opj_event_mgr_t * p_manager );
758 * Reads a PPT marker (Packed packet headers, tile-part header)
760 * @param p_header_data the data contained in the PPT box.
761 * @param p_j2k the jpeg2000 codec.
762 * @param p_header_size the size of the data contained in the PPT marker.
763 * @param p_manager the user event manager.
765 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
766 OPJ_BYTE * p_header_data,
767 OPJ_UINT32 p_header_size,
768 opj_event_mgr_t * p_manager );
770 * Writes the SOD marker (Start of data)
772 * @param p_stream the stream to write data to.
773 * @param p_j2k J2K codec.
774 * @param p_manager the user event manager.
776 static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
777 opj_tcd_v2_t * p_tile_coder,
779 OPJ_UINT32 * p_data_written,
780 OPJ_UINT32 p_total_data_size,
781 const opj_stream_private_t *p_stream,
782 opj_event_mgr_t * p_manager );
785 * Reads a SOD marker (Start Of Data)
787 * @param p_header_data the data contained in the SOD box.
788 * @param p_j2k the jpeg2000 codec.
789 * @param p_header_size the size of the data contained in the SOD marker.
790 * @param p_manager the user event manager.
792 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
793 opj_stream_private_t *p_stream,
794 opj_event_mgr_t * p_manager );
797 * Updates the Tile Length Marker.
799 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
801 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
802 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
804 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
805 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
809 * Writes the RGN marker (Region Of Interest)
811 * @param p_tile_no the tile to output
812 * @param p_comp_no the component to output
813 * @param p_stream the stream to write data to.
814 * @param p_j2k J2K codec.
815 * @param p_manager the user event manager.
817 static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k,
818 OPJ_UINT32 p_tile_no,
819 OPJ_UINT32 p_comp_no,
820 opj_stream_private_t *p_stream,
821 opj_event_mgr_t * p_manager );
824 * Reads a RGN marker (Region Of Interest)
826 * @param p_header_data the data contained in the POC box.
827 * @param p_j2k the jpeg2000 codec.
828 * @param p_header_size the size of the data contained in the POC marker.
829 * @param p_manager the user event manager.
831 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
832 OPJ_BYTE * p_header_data,
833 OPJ_UINT32 p_header_size,
834 opj_event_mgr_t * p_manager );
837 * Writes the EOC marker (End of Codestream)
839 * @param p_stream the stream to write data to.
840 * @param p_j2k J2K codec.
841 * @param p_manager the user event manager.
843 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
844 struct opj_stream_private *p_stream,
845 struct opj_event_mgr * p_manager );
848 Write the EOC marker (end of codestream)
849 @param j2k J2K handle
851 static void j2k_write_eoc(opj_j2k_t *j2k);
853 Read the EOC marker (end of codestream)
854 @param j2k J2K handle
856 static void j2k_read_eoc(opj_j2k_t *j2k);
859 * Reads a EOC marker (End Of Codestream)
861 * @param p_header_data the data contained in the SOD box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the SOD marker.
864 * @param p_manager the user event manager.
867 static opj_bool j2k_read_eoc_v2 (
869 struct opj_stream_private *p_stream,
870 struct opj_event_mgr * p_manager
877 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
879 * @param p_stream the stream to write data to.
880 * @param p_j2k J2K codec.
881 * @param p_manager the user event manager.
883 static opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
884 struct opj_stream_private *p_stream,
885 struct opj_event_mgr * p_manager );
890 * @param p_stream the stream to write data to.
891 * @param p_j2k J2K codec.
892 * @param p_manager the user event manager.
894 static opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
895 struct opj_stream_private *p_stream,
896 struct opj_event_mgr * p_manager );
899 Add main header marker information
900 @param cstr_info Codestream information structure
901 @param type marker type
902 @param pos byte offset of marker segment
903 @param len length of marker segment
905 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
907 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
909 Add tile header marker information
910 @param tileno tile index number
911 @param cstr_info Codestream information structure
912 @param type marker type
913 @param pos byte offset of marker segment
914 @param len length of marker segment
916 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
918 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
921 * Reads an unknown marker
923 * @param p_stream the stream object to read from.
924 * @param p_j2k the jpeg2000 codec.
925 * @param p_manager the user event manager.
927 * @return true if the marker could be deduced.
929 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
930 opj_stream_private_t *p_stream,
931 OPJ_UINT32 *output_marker,
932 opj_event_mgr_t * p_manager );
935 * Writes the MCT marker (Multiple Component Transform)
937 * @param p_stream the stream to write data to.
938 * @param p_j2k J2K codec.
939 * @param p_manager the user event manager.
941 static opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
942 opj_mct_data_t * p_mct_record,
943 struct opj_stream_private *p_stream,
944 struct opj_event_mgr * p_manager );
947 * Reads a MCT marker (Multiple Component Transform)
949 * @param p_header_data the data contained in the MCT box.
950 * @param p_j2k the jpeg2000 codec.
951 * @param p_header_size the size of the data contained in the MCT marker.
952 * @param p_manager the user event manager.
954 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
955 OPJ_BYTE * p_header_data,
956 OPJ_UINT32 p_header_size,
957 opj_event_mgr_t * p_manager );
960 * Writes the MCC marker (Multiple Component Collection)
962 * @param p_stream the stream to write data to.
963 * @param p_j2k J2K codec.
964 * @param p_manager the user event manager.
966 static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
967 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
968 struct opj_stream_private *p_stream,
969 struct opj_event_mgr * p_manager );
973 * Reads a MCC marker (Multiple Component Collection)
975 * @param p_header_data the data contained in the MCC box.
976 * @param p_j2k the jpeg2000 codec.
977 * @param p_header_size the size of the data contained in the MCC marker.
978 * @param p_manager the user event manager.
980 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
981 OPJ_BYTE * p_header_data,
982 OPJ_UINT32 p_header_size,
983 opj_event_mgr_t * p_manager );
986 * Writes the MCO marker (Multiple component transformation ordering)
988 * @param p_stream the stream to write data to.
989 * @param p_j2k J2K codec.
990 * @param p_manager the user event manager.
992 static opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
993 struct opj_stream_private *p_stream,
994 struct opj_event_mgr * p_manager );
997 * Reads a MCO marker (Multiple Component Transform Ordering)
999 * @param p_header_data the data contained in the MCO box.
1000 * @param p_j2k the jpeg2000 codec.
1001 * @param p_header_size the size of the data contained in the MCO marker.
1002 * @param p_manager the user event manager.
1004 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1005 OPJ_BYTE * p_header_data,
1006 OPJ_UINT32 p_header_size,
1007 opj_event_mgr_t * p_manager );
1009 static opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1011 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1012 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1013 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1014 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1016 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1017 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1018 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1019 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1021 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1022 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1023 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1024 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1028 * Ends the encoding, i.e. frees memory.
1030 * @param p_stream the stream to write data to.
1031 * @param p_j2k J2K codec.
1032 * @param p_manager the user event manager.
1034 static opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1035 struct opj_stream_private *p_stream,
1036 struct opj_event_mgr * p_manager );
1039 * Writes the CBD marker (Component bit depth definition)
1041 * @param p_stream the stream to write data to.
1042 * @param p_j2k J2K codec.
1043 * @param p_manager the user event manager.
1045 static opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1046 struct opj_stream_private *p_stream,
1047 struct opj_event_mgr * p_manager );
1050 * Reads a CBD marker (Component bit depth definition)
1051 * @param p_header_data the data contained in the CBD box.
1052 * @param p_j2k the jpeg2000 codec.
1053 * @param p_header_size the size of the data contained in the CBD marker.
1054 * @param p_manager the user event manager.
1056 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1057 OPJ_BYTE * p_header_data,
1058 OPJ_UINT32 p_header_size,
1059 opj_event_mgr_t * p_manager);
1062 * Writes the image components.
1064 * @param p_stream the stream to write data to.
1065 * @param p_j2k J2K codec.
1066 * @param p_manager the user event manager.
1068 static opj_bool opj_j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1069 opj_stream_private_t *p_stream,
1070 opj_event_mgr_t * p_manager );
1073 * Writes regions of interests.
1075 * @param p_stream the stream to write data to.
1076 * @param p_j2k J2K codec.
1077 * @param p_manager the user event manager.
1079 static opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
1080 opj_stream_private_t *p_stream,
1081 opj_event_mgr_t * p_manager );
1086 * @param p_stream the stream to write data to.
1087 * @param p_j2k J2K codec.
1088 * @param p_manager the user event manager.
1090 static opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k,
1091 opj_stream_private_t *p_stream,
1092 opj_event_mgr_t * p_manager );
1095 * Checks the progression order changes values. Tells of the poc given as input are valid.
1096 * A nice message is outputted at errors.
1098 * @param p_pocs the progression order changes.
1099 * @param p_nb_pocs the number of progression order changes.
1100 * @param p_nb_resolutions the number of resolutions.
1101 * @param numcomps the number of components
1102 * @param numlayers the number of layers.
1104 * @return true if the pocs are valid.
1106 static opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1107 OPJ_UINT32 p_nb_pocs,
1108 OPJ_UINT32 p_nb_resolutions,
1109 OPJ_UINT32 numcomps,
1110 OPJ_UINT32 numlayers,
1111 opj_event_mgr_t * p_manager);
1114 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1116 * @param cp the coding parameters.
1117 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1118 * @param tileno the given tile.
1120 * @return the number of tile parts.
1122 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1125 * Calculates the total number of tile parts needed by the encoder to
1126 * encode such an image. If not enough memory is available, then the function return false.
1128 * @param p_nb_tiles pointer that will hold the number of tile parts.
1129 * @param cp the coding parameters for the image.
1130 * @param image the image to encode.
1131 * @param p_j2k the p_j2k encoder.
1132 * @param p_manager the user event manager.
1134 * @return true if the function was successful, false else.
1136 static opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1138 OPJ_UINT32 * p_nb_tiles,
1140 opj_event_mgr_t * p_manager);
1142 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1144 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1146 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1152 /* ----------------------------------------------------------------------- */
1153 typedef struct j2k_prog_order{
1154 OPJ_PROG_ORDER enum_prog;
1158 j2k_prog_order_t j2k_prog_order_list[] = {
1164 {(OPJ_PROG_ORDER)-1, ""}
1172 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1180 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1182 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1184 opj_j2k_read_int16_to_float,
1185 opj_j2k_read_int32_to_float,
1186 opj_j2k_read_float32_to_float,
1187 opj_j2k_read_float64_to_float
1190 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1192 opj_j2k_read_int16_to_int32,
1193 opj_j2k_read_int32_to_int32,
1194 opj_j2k_read_float32_to_int32,
1195 opj_j2k_read_float64_to_int32
1198 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1200 opj_j2k_write_float_to_int16,
1201 opj_j2k_write_float_to_int32,
1202 opj_j2k_write_float_to_float,
1203 opj_j2k_write_float_to_float64
1206 typedef struct opj_dec_memory_marker_handler
1210 /** value of the state when the marker can appear */
1212 /** action linked to the marker */
1213 opj_bool (*handler) (
1214 opj_j2k_v2_t *p_j2k,
1215 OPJ_BYTE * p_header_data,
1216 OPJ_UINT32 p_header_size,
1217 struct opj_event_mgr * p_manager
1220 opj_dec_memory_marker_handler_t;
1222 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1225 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1226 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1227 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1228 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1229 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1230 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1231 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1232 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1233 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1234 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1235 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1236 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1237 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1239 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1240 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1241 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1242 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1243 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1244 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1246 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1247 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1248 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1249 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1250 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1251 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1252 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1253 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1254 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1255 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1256 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1257 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1258 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1260 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1261 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1262 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1263 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1264 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1265 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1267 #ifdef TODO_MS /* FIXME */
1268 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1269 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1270 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1271 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1273 #endif /* USE_JPWL */
1275 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1276 {J2K_MS_INSEC, 0, j2k_read_insec}
1277 #endif /* USE_JPSEC */
1278 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1283 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1285 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1286 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1290 for (i=0;i<p_nb_elem;++i) {
1291 opj_read_bytes(l_src_data,&l_temp,2);
1293 l_src_data+=sizeof(OPJ_INT16);
1295 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1299 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1301 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1302 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1306 for (i=0;i<p_nb_elem;++i) {
1307 opj_read_bytes(l_src_data,&l_temp,4);
1309 l_src_data+=sizeof(OPJ_INT32);
1311 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1315 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1317 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1318 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1322 for (i=0;i<p_nb_elem;++i) {
1323 opj_read_float(l_src_data,&l_temp);
1325 l_src_data+=sizeof(OPJ_FLOAT32);
1327 *(l_dest_data++) = l_temp;
1331 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1333 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1334 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1338 for (i=0;i<p_nb_elem;++i) {
1339 opj_read_double(l_src_data,&l_temp);
1341 l_src_data+=sizeof(OPJ_FLOAT64);
1343 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1347 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1349 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1350 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1354 for (i=0;i<p_nb_elem;++i) {
1355 opj_read_bytes(l_src_data,&l_temp,2);
1357 l_src_data+=sizeof(OPJ_INT16);
1359 *(l_dest_data++) = (OPJ_INT32) l_temp;
1363 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1365 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1366 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1370 for (i=0;i<p_nb_elem;++i) {
1371 opj_read_bytes(l_src_data,&l_temp,4);
1373 l_src_data+=sizeof(OPJ_INT32);
1375 *(l_dest_data++) = (OPJ_INT32) l_temp;
1379 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1381 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1382 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1386 for (i=0;i<p_nb_elem;++i) {
1387 opj_read_float(l_src_data,&l_temp);
1389 l_src_data+=sizeof(OPJ_FLOAT32);
1391 *(l_dest_data++) = (OPJ_INT32) l_temp;
1395 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1397 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1398 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1402 for (i=0;i<p_nb_elem;++i) {
1403 opj_read_double(l_src_data,&l_temp);
1405 l_src_data+=sizeof(OPJ_FLOAT64);
1407 *(l_dest_data++) = (OPJ_INT32) l_temp;
1411 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1413 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1414 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1418 for (i=0;i<p_nb_elem;++i) {
1419 l_temp = (OPJ_UINT32) *(l_src_data++);
1421 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1423 l_dest_data+=sizeof(OPJ_INT16);
1427 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1429 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1430 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1434 for (i=0;i<p_nb_elem;++i) {
1435 l_temp = (OPJ_UINT32) *(l_src_data++);
1437 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1439 l_dest_data+=sizeof(OPJ_INT32);
1443 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1445 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1446 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1450 for (i=0;i<p_nb_elem;++i) {
1451 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1453 opj_write_float(l_dest_data,l_temp);
1455 l_dest_data+=sizeof(OPJ_FLOAT32);
1459 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1461 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1462 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1466 for (i=0;i<p_nb_elem;++i) {
1467 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1469 opj_write_double(l_dest_data,l_temp);
1471 l_dest_data+=sizeof(OPJ_FLOAT64);
1477 * Converts an enum type progression order to string type.
1479 * @param prg_order the progression order to get.
1481 * @return the string representation of the given progression order.
1483 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1484 j2k_prog_order_t *po;
1485 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1486 if(po->enum_prog == prg_order){
1487 return po->str_prog;
1490 return po->str_prog;
1494 * Checks the progression order changes values. Tells if the poc given as input are valid.
1496 * @param p_pocs the progression order changes.
1497 * @param p_nb_pocs the number of progression order changes.
1498 * @param p_nb_resolutions the number of resolutions.
1499 * @param numcomps the number of components
1500 * @param numlayers the number of layers.
1501 * @param p_manager the user event manager.
1503 * @return true if the pocs are valid.
1505 opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1506 OPJ_UINT32 p_nb_pocs,
1507 OPJ_UINT32 p_nb_resolutions,
1508 OPJ_UINT32 p_num_comps,
1509 OPJ_UINT32 p_num_layers,
1510 opj_event_mgr_t * p_manager)
1512 OPJ_UINT32* packet_array;
1513 OPJ_UINT32 index , resno, compno, layno;
1515 OPJ_UINT32 step_c = 1;
1516 OPJ_UINT32 step_r = p_num_comps * step_c;
1517 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1518 opj_bool loss = OPJ_FALSE;
1519 OPJ_UINT32 layno0 = 0;
1521 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1522 if (packet_array == 00) {
1523 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1526 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1528 if (p_nb_pocs == 0) {
1532 index = step_r * p_pocs->resno0;
1533 // take each resolution for each poc
1534 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1536 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1538 // take each comp of each resolution for each poc
1539 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1540 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1542 // and finally take each layer of each res of ...
1543 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1544 //index = step_r * resno + step_c * compno + step_l * layno;
1545 packet_array[comp_index] = 1;
1546 comp_index += step_l;
1549 res_index += step_c;
1556 // iterate through all the pocs
1557 for (i = 1; i < p_nb_pocs ; ++i) {
1558 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1560 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1561 index = step_r * p_pocs->resno0;
1563 // take each resolution for each poc
1564 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1565 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1567 // take each comp of each resolution for each poc
1568 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1569 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1571 // and finally take each layer of each res of ...
1572 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1573 //index = step_r * resno + step_c * compno + step_l * layno;
1574 packet_array[comp_index] = 1;
1575 comp_index += step_l;
1578 res_index += step_c;
1588 for (layno = 0; layno < p_num_layers ; ++layno) {
1589 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1590 for (compno = 0; compno < p_num_comps; ++compno) {
1591 loss |= (packet_array[index]!=1);
1592 //index = step_r * resno + step_c * compno + step_l * layno;
1599 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1602 opj_free(packet_array);
1607 /* ----------------------------------------------------------------------- */
1610 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1612 * @param cp the coding parameters.
1613 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1614 * @param tileno the given tile.
1616 * @return the number of tile parts.
1618 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1620 const OPJ_CHAR *prog = 00;
1622 OPJ_UINT32 tpnum = 1;
1623 opj_tcp_v2_t *tcp = 00;
1624 opj_poc_t * l_current_poc = 00;
1627 assert(tileno < (cp->tw * cp->th));
1628 assert(pino < (cp->tcps[tileno].numpocs + 1));
1630 /* get the given tile coding parameter */
1631 tcp = &cp->tcps[tileno];
1634 l_current_poc = &(tcp->pocs[pino]);
1635 assert(l_current_poc != 0);
1637 /* get the progression order as a character string */
1638 prog = j2k_convert_progression_order(tcp->prg);
1639 assert(strlen(prog) > 0);
1641 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1645 /* component wise */
1647 tpnum *= l_current_poc->compE;
1649 /* resolution wise */
1651 tpnum *= l_current_poc->resE;
1655 tpnum *= l_current_poc->prcE;
1659 tpnum *= l_current_poc->layE;
1662 /* whould we split here ? */
1663 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1664 cp->m_specific_param.m_enc.m_tp_pos=i;
1677 * Calculates the total number of tile parts needed by the encoder to
1678 * encode such an image. If not enough memory is available, then the function return false.
1680 * @param p_nb_tiles pointer that will hold the number of tile parts.
1681 * @param cp the coding parameters for the image.
1682 * @param image the image to encode.
1683 * @param p_j2k the p_j2k encoder.
1684 * @param p_manager the user event manager.
1686 * @return true if the function was successful, false else.
1688 opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1690 OPJ_UINT32 * p_nb_tiles,
1692 opj_event_mgr_t * p_manager
1695 OPJ_UINT32 pino,tileno;
1696 OPJ_UINT32 l_nb_tiles;
1700 assert(p_nb_tiles != 00);
1702 assert(image != 00);
1703 assert(p_j2k != 00);
1704 assert(p_manager != 00);
1706 l_nb_tiles = cp->tw * cp->th;
1711 /* TODO mergeV2: check this part which use cstr_info */
1712 /*if (p_j2k->cstr_info) {
1713 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1715 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1716 OPJ_UINT32 cur_totnum_tp = 0;
1718 pi_update_encoding_parameters(image,cp,tileno);
1720 for (pino = 0; pino <= tcp->numpocs; ++pino)
1722 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1724 *p_nb_tiles = *p_nb_tiles + tp_num;
1726 cur_totnum_tp += tp_num;
1729 tcp->m_nb_tile_parts = cur_totnum_tp;
1731 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1732 if (l_info_tile_ptr->tp == 00) {
1736 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1738 l_info_tile_ptr->num_tps = cur_totnum_tp;
1745 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1746 OPJ_UINT32 cur_totnum_tp = 0;
1748 pi_update_encoding_parameters(image,cp,tileno);
1750 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1751 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1753 *p_nb_tiles = *p_nb_tiles + tp_num;
1755 cur_totnum_tp += tp_num;
1757 tcp->m_nb_tile_parts = cur_totnum_tp;
1767 * Writes the SOC marker (Start Of Codestream)
1769 * @param p_stream the stream to write data to.
1770 * @param p_j2k J2K codec.
1771 * @param p_manager the user event manager.
1773 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1774 opj_stream_private_t *p_stream,
1775 opj_event_mgr_t * p_manager )
1777 /* 2 bytes will be written */
1778 OPJ_BYTE * l_start_stream = 00;
1781 assert(p_stream != 00);
1782 assert(p_j2k != 00);
1783 assert(p_manager != 00);
1785 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1787 /* write SOC identifier */
1788 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1790 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1796 /* update markers struct */
1798 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1800 assert( 0 && "TODO" );
1801 #endif /* USE_JPWL */
1810 * Reads a SOC marker (Start of Codestream)
1811 * @param p_header_data the data contained in the SOC box.
1812 * @param jp2 the jpeg2000 file codec.
1813 * @param p_header_size the size of the data contained in the SOC marker.
1814 * @param p_manager the user event manager.
1816 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1817 opj_stream_private_t *p_stream,
1818 opj_event_mgr_t * p_manager
1821 OPJ_BYTE l_data [2];
1822 OPJ_UINT32 l_marker;
1825 assert(p_j2k != 00);
1826 assert(p_manager != 00);
1827 assert(p_stream != 00);
1829 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1833 opj_read_bytes(l_data,&l_marker,2);
1834 if (l_marker != J2K_MS_SOC) {
1838 /* Next marker should be a SIZ marker in the main header */
1839 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1841 /* FIXME move it in a index structure included in p_j2k*/
1842 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1844 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1846 /* Add the marker to the codestream index*/
1847 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1854 * Writes the SIZ marker (image and tile size)
1856 * @param p_stream the stream to write data to.
1857 * @param p_j2k J2K codec.
1858 * @param p_manager the user event manager.
1860 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1861 opj_stream_private_t *p_stream,
1862 opj_event_mgr_t * p_manager )
1865 OPJ_UINT32 l_size_len;
1866 OPJ_BYTE * l_current_ptr;
1867 opj_image_t * l_image = 00;
1868 opj_cp_v2_t *cp = 00;
1869 opj_image_comp_t * l_img_comp = 00;
1872 assert(p_stream != 00);
1873 assert(p_j2k != 00);
1874 assert(p_manager != 00);
1876 l_image = p_j2k->m_private_image;
1877 cp = &(p_j2k->m_cp);
1878 l_size_len = 40 + 3 * l_image->numcomps;
1879 l_img_comp = l_image->comps;
1881 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1883 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1884 = (OPJ_BYTE*)opj_realloc(
1885 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1887 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1891 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1894 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1896 /* write SOC identifier */
1897 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1900 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1903 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1906 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1909 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1912 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1915 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1918 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1921 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1924 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
1927 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
1930 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
1933 for (i = 0; i < l_image->numcomps; ++i) {
1934 /* TODO here with MCT ? */
1935 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
1938 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
1941 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
1947 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) {
1957 * Reads a SIZ marker (image and tile size)
1958 * @param p_header_data the data contained in the SIZ box.
1959 * @param jp2 the jpeg2000 file codec.
1960 * @param p_header_size the size of the data contained in the SIZ marker.
1961 * @param p_manager the user event manager.
1963 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
1964 OPJ_BYTE * p_header_data,
1965 OPJ_UINT32 p_header_size,
1966 opj_event_mgr_t * p_manager
1969 OPJ_UINT32 l_size, i;
1970 OPJ_UINT32 l_nb_comp;
1971 OPJ_UINT32 l_nb_comp_remain;
1972 OPJ_UINT32 l_remaining_size;
1973 OPJ_UINT32 l_nb_tiles;
1975 opj_image_t *l_image = 00;
1976 opj_cp_v2_t *l_cp = 00;
1977 opj_image_comp_t * l_img_comp = 00;
1978 opj_tcp_v2_t * l_current_tile_param = 00;
1981 assert(p_j2k != 00);
1982 assert(p_manager != 00);
1983 assert(p_header_data != 00);
1985 l_image = p_j2k->m_private_image;
1986 l_cp = &(p_j2k->m_cp);
1988 /* minimum size == 39 - 3 (= minimum component parameter) */
1989 if (p_header_size < 36) {
1990 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1994 l_remaining_size = p_header_size - 36;
1995 l_nb_comp = l_remaining_size / 3;
1996 l_nb_comp_remain = l_remaining_size % 3;
1997 if (l_nb_comp_remain != 0){
1998 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2002 l_size = p_header_size + 2; /* Lsiz */
2004 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2006 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2007 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2009 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2011 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2013 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2015 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2017 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2019 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2021 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2023 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2026 l_image->numcomps = (OPJ_UINT16) l_tmp;
2028 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2032 if (l_image->numcomps != l_nb_comp) {
2033 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);
2038 if (l_cp->correct) {
2039 /* if JPWL is on, we check whether TX errors have damaged
2040 too much the SIZ parameters */
2041 if (!(l_image->x1 * l_image->y1)) {
2042 opj_event_msg_v2(p_manager, EVT_ERROR,
2043 "JPWL: bad image size (%d x %d)\n",
2044 l_image->x1, l_image->y1);
2045 if (!JPWL_ASSUME || JPWL_ASSUME) {
2046 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2051 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2052 if (l_image->numcomps != ((len - 38) / 3)) {
2053 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2054 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2055 l_image->numcomps, ((len - 38) / 3));
2057 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2060 */ /* we try to correct */
2061 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2062 if (l_image->numcomps < ((len - 38) / 3)) {
2063 len = 38 + 3 * l_image->numcomps;
2064 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2067 l_image->numcomps = ((len - 38) / 3);
2068 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2074 /* update components number in the jpwl_exp_comps filed */
2075 l_cp->exp_comps = l_image->numcomps;
2077 #endif /* USE_JPWL */
2079 /* Allocate the resulting image components */
2080 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2081 if (l_image->comps == 00){
2082 l_image->numcomps = 0;
2083 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2087 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2088 l_img_comp = l_image->comps;
2090 /* Read the component information */
2091 for (i = 0; i < l_image->numcomps; ++i){
2093 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2095 l_img_comp->prec = (tmp & 0x7f) + 1;
2096 l_img_comp->sgnd = tmp >> 7;
2097 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2099 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2100 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2102 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2105 if (l_cp->correct) {
2106 /* if JPWL is on, we check whether TX errors have damaged
2107 too much the SIZ parameters, again */
2108 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2109 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2110 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2111 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2113 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2116 /* we try to correct */
2117 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2118 if (!l_image->comps[i].dx) {
2119 l_image->comps[i].dx = 1;
2120 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2121 i, l_image->comps[i].dx);
2123 if (!l_image->comps[i].dy) {
2124 l_image->comps[i].dy = 1;
2125 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2126 i, l_image->comps[i].dy);
2130 #endif /* USE_JPWL */
2131 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2132 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2136 /* Compute the number of tiles */
2137 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2138 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2139 l_nb_tiles = l_cp->tw * l_cp->th;
2141 /* Define the tiles which will be decoded */
2142 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2143 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;
2144 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;
2145 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);
2146 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);
2149 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2150 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2151 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2152 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2156 if (l_cp->correct) {
2157 /* if JPWL is on, we check whether TX errors have damaged
2158 too much the SIZ parameters */
2159 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2160 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2161 "JPWL: bad number of tiles (%d x %d)\n",
2162 l_cp->tw, l_cp->th);
2164 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2167 /* we try to correct */
2168 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2171 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2174 if (l_cp->tw > l_cp->max_tiles) {
2176 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2177 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2178 l_cp->max_tiles, l_cp->tw);
2182 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2185 if (l_cp->th > l_cp->max_tiles) {
2187 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2188 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2189 l_cp->max_tiles, l_cp->th);
2193 #endif /* USE_JPWL */
2195 /* memory allocations */
2196 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2197 if (l_cp->tcps == 00) {
2198 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2201 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2204 if (l_cp->correct) {
2206 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2207 "JPWL: could not alloc tcps field of cp\n");
2208 if (!JPWL_ASSUME || JPWL_ASSUME) {
2209 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2214 #endif /* USE_JPWL */
2216 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2217 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2218 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2219 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2222 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2224 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2225 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2227 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2228 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2231 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));
2232 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2234 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2235 (opj_simple_mcc_decorrelation_data_t*)
2236 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2238 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2239 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2242 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));
2243 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2245 /* set up default dc level shift */
2246 for (i=0;i<l_image->numcomps;++i) {
2247 if (! l_image->comps[i].sgnd) {
2248 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2252 l_current_tile_param = l_cp->tcps;
2253 for (i = 0; i < l_nb_tiles; ++i) {
2254 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2255 if (l_current_tile_param->tccps == 00) {
2256 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2259 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2261 ++l_current_tile_param;
2264 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2265 opj_image_comp_header_update(l_image,l_cp);
2271 * Writes the COM marker (comment)
2273 * @param p_stream the stream to write data to.
2274 * @param p_j2k J2K codec.
2275 * @param p_manager the user event manager.
2277 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2278 opj_stream_private_t *p_stream,
2279 opj_event_mgr_t * p_manager
2282 OPJ_UINT32 l_comment_size;
2283 OPJ_UINT32 l_total_com_size;
2284 const OPJ_CHAR *l_comment;
2285 OPJ_BYTE * l_current_ptr = 00;
2288 assert(p_j2k != 00);
2289 assert(p_stream != 00);
2290 assert(p_manager != 00);
2292 l_comment = p_j2k->m_cp.comment;
2293 l_comment_size = strlen(l_comment);
2294 l_total_com_size = l_comment_size + 6;
2296 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2297 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2298 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2301 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2305 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2308 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2310 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2313 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2316 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2319 memcpy( l_current_ptr,l_comment,l_comment_size);
2321 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) {
2329 * Reads a COM marker (comments)
2330 * @param p_header_data the data contained in the COM box.
2331 * @param jp2 the jpeg2000 file codec.
2332 * @param p_header_size the size of the data contained in the COM marker.
2333 * @param p_manager the user event manager.
2335 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2336 OPJ_BYTE * p_header_data,
2337 OPJ_UINT32 p_header_size,
2338 opj_event_mgr_t * p_manager
2342 assert(p_j2k != 00);
2343 assert(p_manager != 00);
2344 assert(p_header_data != 00);
2345 (void)p_header_size;
2351 * Writes the COD marker (Coding style default)
2353 * @param p_stream the stream to write data to.
2354 * @param p_j2k J2K codec.
2355 * @param p_manager the user event manager.
2357 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2358 opj_stream_private_t *p_stream,
2359 opj_event_mgr_t * p_manager )
2361 opj_cp_v2_t *l_cp = 00;
2362 opj_tcp_v2_t *l_tcp = 00;
2363 OPJ_UINT32 l_code_size,l_remaining_size;
2364 OPJ_BYTE * l_current_data = 00;
2367 assert(p_j2k != 00);
2368 assert(p_manager != 00);
2369 assert(p_stream != 00);
2371 l_cp = &(p_j2k->m_cp);
2372 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2373 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2374 l_remaining_size = l_code_size;
2376 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2377 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2378 = (OPJ_BYTE*)opj_realloc(
2379 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2382 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2386 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2389 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2391 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2392 l_current_data += 2;
2394 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2395 l_current_data += 2;
2397 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2400 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2403 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2406 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2409 l_remaining_size -= 9;
2411 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2412 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2416 if (l_remaining_size != 0) {
2417 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2421 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) {
2430 * Reads a COD marker (Coding Styke defaults)
2431 * @param p_header_data the data contained in the COD box.
2432 * @param p_j2k the jpeg2000 codec.
2433 * @param p_header_size the size of the data contained in the COD marker.
2434 * @param p_manager the user event manager.
2436 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2437 OPJ_BYTE * p_header_data,
2438 OPJ_UINT32 p_header_size,
2439 opj_event_mgr_t * p_manager
2445 opj_cp_v2_t *l_cp = 00;
2446 opj_tcp_v2_t *l_tcp = 00;
2447 opj_image_t *l_image = 00;
2450 assert(p_header_data != 00);
2451 assert(p_j2k != 00);
2452 assert(p_manager != 00);
2454 l_image = p_j2k->m_private_image;
2455 l_cp = &(p_j2k->m_cp);
2457 /* If we are in the first tile-part header of the current tile */
2458 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2459 &l_cp->tcps[p_j2k->m_current_tile_number] :
2460 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2462 /* Make sure room is sufficient */
2463 if (p_header_size < 5) {
2464 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2468 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2470 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2472 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2473 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2476 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2477 if (l_cp->m_specific_param.m_dec.m_layer) {
2478 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2481 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2484 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2488 for (i = 0; i < l_image->numcomps; ++i) {
2489 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2492 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2493 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2497 if (p_header_size != 0) {
2498 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2502 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2503 opj_j2k_copy_tile_component_parameters(p_j2k);
2506 #ifdef WIP_REMOVE_MSD
2507 if (p_j2k->cstr_info) {
2508 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2509 p_j2k->cstr_info->prog = l_tcp->prg;
2510 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2511 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2512 for (i = 0; i < l_image->numcomps; ++i) {
2513 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2522 * Writes the COC marker (Coding style component)
2524 * @param p_comp_no the index of the component to output.
2525 * @param p_stream the stream to write data to.
2526 * @param p_j2k J2K codec.
2527 * @param p_manager the user event manager.
2529 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2530 OPJ_UINT32 p_comp_no,
2531 opj_stream_private_t *p_stream,
2532 opj_event_mgr_t * p_manager )
2534 OPJ_UINT32 l_coc_size,l_remaining_size;
2535 OPJ_UINT32 l_comp_room;
2538 assert(p_j2k != 00);
2539 assert(p_manager != 00);
2540 assert(p_stream != 00);
2542 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2544 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2546 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2547 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2548 = (OPJ_BYTE*)opj_realloc(
2549 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2551 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2555 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2558 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);
2560 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) {
2568 * Writes the COC marker (Coding style component)
2570 * @param p_comp_no the index of the component to output.
2571 * @param p_stream the stream to write data to.
2572 * @param p_j2k J2K codec.
2573 * @param p_manager the user event manager.
2575 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2576 OPJ_UINT32 p_comp_no,
2578 OPJ_UINT32 * p_data_written,
2579 opj_event_mgr_t * p_manager
2582 opj_cp_v2_t *l_cp = 00;
2583 opj_tcp_v2_t *l_tcp = 00;
2584 OPJ_UINT32 l_coc_size,l_remaining_size;
2585 OPJ_BYTE * l_current_data = 00;
2586 opj_image_t *l_image = 00;
2587 OPJ_UINT32 l_comp_room;
2590 assert(p_j2k != 00);
2591 assert(p_manager != 00);
2593 l_cp = &(p_j2k->m_cp);
2594 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2595 l_image = p_j2k->m_private_image;
2596 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2598 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2599 l_remaining_size = l_coc_size;
2601 l_current_data = p_data;
2603 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2604 l_current_data += 2;
2606 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2607 l_current_data += 2;
2609 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2610 l_current_data+=l_comp_room;
2612 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2615 l_remaining_size -= (5 + l_comp_room);
2616 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2617 * p_data_written = l_coc_size;
2621 * Gets the maximum size taken by a coc.
2623 * @param p_j2k the jpeg2000 codec to use.
2625 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2628 OPJ_UINT32 l_nb_comp;
2629 OPJ_UINT32 l_nb_tiles;
2630 OPJ_UINT32 l_max = 0;
2634 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2635 l_nb_comp = p_j2k->m_private_image->numcomps;
2637 for (i=0;i<l_nb_tiles;++i) {
2638 for (j=0;j<l_nb_comp;++j) {
2639 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2648 * Reads a COC marker (Coding Style Component)
2649 * @param p_header_data the data contained in the COC box.
2650 * @param p_j2k the jpeg2000 codec.
2651 * @param p_header_size the size of the data contained in the COC marker.
2652 * @param p_manager the user event manager.
2654 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2655 OPJ_BYTE * p_header_data,
2656 OPJ_UINT32 p_header_size,
2657 opj_event_mgr_t * p_manager
2660 opj_cp_v2_t *l_cp = NULL;
2661 opj_tcp_v2_t *l_tcp = NULL;
2662 opj_image_t *l_image = NULL;
2663 OPJ_UINT32 l_comp_room;
2664 OPJ_UINT32 l_comp_no;
2667 assert(p_header_data != 00);
2668 assert(p_j2k != 00);
2669 assert(p_manager != 00);
2671 l_cp = &(p_j2k->m_cp);
2672 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2673 &l_cp->tcps[p_j2k->m_current_tile_number] :
2674 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2675 l_image = p_j2k->m_private_image;
2677 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2679 /* make sure room is sufficient*/
2680 if (p_header_size < l_comp_room + 1) {
2681 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2684 p_header_size -= l_comp_room + 1;
2686 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2687 p_header_data += l_comp_room;
2688 if (l_comp_no >= l_image->numcomps) {
2689 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2693 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2696 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2697 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2701 if (p_header_size != 0) {
2702 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2709 * Writes the QCD marker (quantization default)
2711 * @param p_comp_number the index of the component to output.
2712 * @param p_stream the stream to write data to.
2713 * @param p_j2k J2K codec.
2714 * @param p_manager the user event manager.
2716 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2717 opj_stream_private_t *p_stream,
2718 opj_event_mgr_t * p_manager
2721 opj_cp_v2_t *l_cp = 00;
2722 opj_tcp_v2_t *l_tcp = 00;
2723 OPJ_UINT32 l_qcd_size,l_remaining_size;
2724 OPJ_BYTE * l_current_data = 00;
2727 assert(p_j2k != 00);
2728 assert(p_manager != 00);
2729 assert(p_stream != 00);
2731 l_cp = &(p_j2k->m_cp);
2732 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2733 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2734 l_remaining_size = l_qcd_size;
2736 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2737 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2738 = (OPJ_BYTE*)opj_realloc(
2739 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2742 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2746 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2749 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2751 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2752 l_current_data += 2;
2754 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2755 l_current_data += 2;
2757 l_remaining_size -= 4;
2759 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2760 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2764 if (l_remaining_size != 0) {
2765 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2769 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) {
2777 * Reads a QCD marker (Quantization defaults)
2778 * @param p_header_data the data contained in the QCD box.
2779 * @param p_j2k the jpeg2000 codec.
2780 * @param p_header_size the size of the data contained in the QCD marker.
2781 * @param p_manager the user event manager.
2783 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2784 OPJ_BYTE * p_header_data,
2785 OPJ_UINT32 p_header_size,
2786 opj_event_mgr_t * p_manager
2790 assert(p_header_data != 00);
2791 assert(p_j2k != 00);
2792 assert(p_manager != 00);
2794 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2795 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2799 if (p_header_size != 0) {
2800 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2804 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2805 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2811 * Writes the QCC marker (quantization component)
2813 * @param p_comp_no the index of the component to output.
2814 * @param p_stream the stream to write data to.
2815 * @param p_j2k J2K codec.
2816 * @param p_manager the user event manager.
2818 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2819 OPJ_UINT32 p_comp_no,
2820 opj_stream_private_t *p_stream,
2821 opj_event_mgr_t * p_manager
2824 OPJ_UINT32 l_qcc_size,l_remaining_size;
2827 assert(p_j2k != 00);
2828 assert(p_manager != 00);
2829 assert(p_stream != 00);
2831 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2832 l_remaining_size = l_qcc_size;
2834 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2835 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2836 = (OPJ_BYTE*)opj_realloc(
2837 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2839 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2843 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2846 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);
2848 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) {
2856 * Writes the QCC marker (quantization component)
2858 * @param p_comp_no the index of the component to output.
2859 * @param p_stream the stream to write data to.
2860 * @param p_j2k J2K codec.
2861 * @param p_manager the user event manager.
2863 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2864 OPJ_UINT32 p_comp_no,
2866 OPJ_UINT32 * p_data_written,
2867 opj_event_mgr_t * p_manager
2870 OPJ_UINT32 l_qcc_size,l_remaining_size;
2871 OPJ_BYTE * l_current_data = 00;
2874 assert(p_j2k != 00);
2875 assert(p_manager != 00);
2877 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2878 l_remaining_size = l_qcc_size;
2880 l_current_data = p_data;
2882 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2883 l_current_data += 2;
2885 if (p_j2k->m_private_image->numcomps <= 256) {
2888 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2889 l_current_data += 2;
2891 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2894 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2895 l_remaining_size -= 6;
2898 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2899 l_current_data += 2;
2901 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2904 l_remaining_size -= 6;
2907 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2909 *p_data_written = l_qcc_size;
2913 * Gets the maximum size taken by a qcc.
2915 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
2917 return opj_j2k_get_max_coc_size(p_j2k);
2921 * Reads a QCC marker (Quantization component)
2922 * @param p_header_data the data contained in the QCC box.
2923 * @param p_j2k the jpeg2000 codec.
2924 * @param p_header_size the size of the data contained in the QCC marker.
2925 * @param p_manager the user event manager.
2927 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
2928 OPJ_BYTE * p_header_data,
2929 OPJ_UINT32 p_header_size,
2930 opj_event_mgr_t * p_manager
2933 OPJ_UINT32 l_num_comp,l_comp_no;
2936 assert(p_header_data != 00);
2937 assert(p_j2k != 00);
2938 assert(p_manager != 00);
2940 l_num_comp = p_j2k->m_private_image->numcomps;
2942 if (l_num_comp <= 256) {
2943 if (p_header_size < 1) {
2944 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2947 opj_read_bytes(p_header_data,&l_comp_no,1);
2952 if (p_header_size < 2) {
2953 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2956 opj_read_bytes(p_header_data,&l_comp_no,2);
2962 if (p_j2k->m_cp.correct) {
2964 static OPJ_UINT32 backup_compno = 0;
2966 /* compno is negative or larger than the number of components!!! */
2967 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2968 opj_event_msg_v2(p_manager, EVT_ERROR,
2969 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2970 l_comp_no, l_num_comp);
2972 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2975 /* we try to correct */
2976 l_comp_no = backup_compno % l_num_comp;
2977 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2978 "- setting component number to %d\n",
2982 /* keep your private count of tiles */
2985 #endif /* USE_JPWL */
2987 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2988 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2992 if (p_header_size != 0) {
2993 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3001 * Writes the POC marker (Progression Order Change)
3003 * @param p_stream the stream to write data to.
3004 * @param p_j2k J2K codec.
3005 * @param p_manager the user event manager.
3007 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
3008 opj_stream_private_t *p_stream,
3009 opj_event_mgr_t * p_manager
3012 OPJ_UINT32 l_nb_comp;
3013 OPJ_UINT32 l_nb_poc;
3014 OPJ_UINT32 l_poc_size;
3015 OPJ_UINT32 l_written_size = 0;
3016 opj_tcp_v2_t *l_tcp = 00;
3017 opj_tccp_t *l_tccp = 00;
3018 OPJ_UINT32 l_poc_room;
3021 assert(p_j2k != 00);
3022 assert(p_manager != 00);
3023 assert(p_stream != 00);
3025 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3026 l_tccp = &l_tcp->tccps[0];
3027 l_nb_comp = p_j2k->m_private_image->numcomps;
3028 l_nb_poc = 1 + l_tcp->numpocs;
3030 if (l_nb_comp <= 256) {
3036 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3038 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3039 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3040 = (OPJ_BYTE*)opj_realloc(
3041 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3044 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3048 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3051 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3053 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) {
3062 * Writes the POC marker (Progression Order Change)
3064 * @param p_stream the stream to write data to.
3065 * @param p_j2k J2K codec.
3066 * @param p_manager the user event manager.
3068 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3070 OPJ_UINT32 * p_data_written,
3071 opj_event_mgr_t * p_manager
3075 OPJ_BYTE * l_current_data = 00;
3076 OPJ_UINT32 l_nb_comp;
3077 OPJ_UINT32 l_nb_poc;
3078 OPJ_UINT32 l_poc_size;
3079 opj_image_t *l_image = 00;
3080 opj_tcp_v2_t *l_tcp = 00;
3081 opj_tccp_t *l_tccp = 00;
3082 opj_poc_t *l_current_poc = 00;
3083 OPJ_UINT32 l_poc_room;
3086 assert(p_j2k != 00);
3087 assert(p_manager != 00);
3089 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3090 l_tccp = &l_tcp->tccps[0];
3091 l_image = p_j2k->m_private_image;
3092 l_nb_comp = l_image->numcomps;
3093 l_nb_poc = 1 + l_tcp->numpocs;
3095 if (l_nb_comp <= 256) {
3102 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3104 l_current_data = p_data;
3106 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3107 l_current_data += 2;
3109 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3110 l_current_data += 2;
3112 l_current_poc = l_tcp->pocs;
3113 for (i = 0; i < l_nb_poc; ++i) {
3114 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3117 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3118 l_current_data+=l_poc_room;
3120 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3123 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3126 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3127 l_current_data+=l_poc_room;
3129 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3132 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3133 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3134 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3135 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3140 *p_data_written = l_poc_size;
3144 * Gets the maximum size taken by the writing of a POC.
3146 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3148 opj_tcp_v2_t * l_tcp = 00;
3149 OPJ_UINT32 l_nb_tiles = 0;
3150 OPJ_UINT32 l_max_poc = 0;
3153 l_tcp = p_j2k->m_cp.tcps;
3154 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3156 for (i=0;i<l_nb_tiles;++i) {
3157 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3163 return 4 + 9 * l_max_poc;
3167 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3169 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3172 OPJ_UINT32 l_nb_tiles;
3173 OPJ_UINT32 l_max = 0;
3174 opj_tcp_v2_t * l_tcp = 00;
3176 l_tcp = p_j2k->m_cp.tcps;
3177 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3179 for (i=0;i<l_nb_tiles;++i) {
3180 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3190 * Gets the maximum size taken by the headers of the SOT.
3192 * @param p_j2k the jpeg2000 codec to use.
3194 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3196 OPJ_UINT32 l_nb_bytes = 0;
3197 OPJ_UINT32 l_nb_comps;
3198 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3200 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3201 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3203 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3204 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3205 l_nb_bytes += l_nb_comps * l_coc_bytes;
3207 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3208 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3211 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3213 /*** DEVELOPER CORNER, Add room for your headers ***/
3220 * Reads a POC marker (Progression Order Change)
3222 * @param p_header_data the data contained in the POC box.
3223 * @param p_j2k the jpeg2000 codec.
3224 * @param p_header_size the size of the data contained in the POC marker.
3225 * @param p_manager the user event manager.
3227 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3228 OPJ_BYTE * p_header_data,
3229 OPJ_UINT32 p_header_size,
3230 opj_event_mgr_t * p_manager
3233 OPJ_UINT32 i, l_nb_comp, l_tmp;
3234 opj_image_t * l_image = 00;
3235 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3236 OPJ_UINT32 l_chunk_size, l_comp_room;
3238 opj_cp_v2_t *l_cp = 00;
3239 opj_tcp_v2_t *l_tcp = 00;
3240 opj_poc_t *l_current_poc = 00;
3243 assert(p_header_data != 00);
3244 assert(p_j2k != 00);
3245 assert(p_manager != 00);
3247 l_image = p_j2k->m_private_image;
3248 l_nb_comp = l_image->numcomps;
3249 if (l_nb_comp <= 256) {
3255 l_chunk_size = 5 + 2 * l_comp_room;
3256 l_current_poc_nb = p_header_size / l_chunk_size;
3257 l_current_poc_remaining = p_header_size % l_chunk_size;
3259 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3260 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3264 l_cp = &(p_j2k->m_cp);
3265 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3266 &l_cp->tcps[p_j2k->m_current_tile_number] :
3267 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3268 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3269 l_current_poc_nb += l_old_poc_nb;
3271 assert(l_current_poc_nb < 32);
3273 /* now poc is in use.*/
3276 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3277 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3278 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3280 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3281 p_header_data+=l_comp_room;
3282 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3284 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3286 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3287 p_header_data+=l_comp_room;
3288 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3290 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3291 /* make sure comp is in acceptable bounds */
3292 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3296 l_tcp->numpocs = l_current_poc_nb - 1;
3301 * Reads a CRG marker (Component registration)
3303 * @param p_header_data the data contained in the TLM box.
3304 * @param p_j2k the jpeg2000 codec.
3305 * @param p_header_size the size of the data contained in the TLM marker.
3306 * @param p_manager the user event manager.
3308 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3309 OPJ_BYTE * p_header_data,
3310 OPJ_UINT32 p_header_size,
3311 opj_event_mgr_t * p_manager
3314 OPJ_UINT32 l_nb_comp;
3316 assert(p_header_data != 00);
3317 assert(p_j2k != 00);
3318 assert(p_manager != 00);
3320 l_nb_comp = p_j2k->m_private_image->numcomps;
3322 if (p_header_size != l_nb_comp *4) {
3323 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3326 /* Do not care of this at the moment since only local variables are set here */
3329 (i = 0; i < l_nb_comp; ++i)
3331 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3333 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3341 * Reads a TLM marker (Tile Length Marker)
3343 * @param p_header_data the data contained in the TLM box.
3344 * @param p_j2k the jpeg2000 codec.
3345 * @param p_header_size the size of the data contained in the TLM marker.
3346 * @param p_manager the user event manager.
3348 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3349 OPJ_BYTE * p_header_data,
3350 OPJ_UINT32 p_header_size,
3351 opj_event_mgr_t * p_manager
3354 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3356 assert(p_header_data != 00);
3357 assert(p_j2k != 00);
3358 assert(p_manager != 00);
3360 if (p_header_size < 2) {
3361 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3366 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3368 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3371 l_ST = ((l_Stlm >> 4) & 0x3);
3372 l_SP = (l_Stlm >> 6) & 0x1;
3374 l_Ptlm_size = (l_SP + 1) * 2;
3375 l_quotient = l_Ptlm_size + l_ST;
3377 l_tot_num_tp = p_header_size / l_quotient;
3378 l_tot_num_tp_remaining = p_header_size % l_quotient;
3380 if (l_tot_num_tp_remaining != 0) {
3381 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3384 /* FIXME Do not care of this at the moment since only local variables are set here */
3387 (i = 0; i < l_tot_num_tp; ++i)
3389 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3390 p_header_data += l_ST;
3391 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3392 p_header_data += l_Ptlm_size;
3399 * Reads a PLM marker (Packet length, main header marker)
3401 * @param p_header_data the data contained in the TLM box.
3402 * @param p_j2k the jpeg2000 codec.
3403 * @param p_header_size the size of the data contained in the TLM marker.
3404 * @param p_manager the user event manager.
3406 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3407 OPJ_BYTE * p_header_data,
3408 OPJ_UINT32 p_header_size,
3409 opj_event_mgr_t * p_manager
3413 assert(p_header_data != 00);
3414 assert(p_j2k != 00);
3415 assert(p_manager != 00);
3417 if (p_header_size < 1) {
3418 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3421 /* Do not care of this at the moment since only local variables are set here */
3423 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3430 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3432 p_header_size -= (1+l_Nplm);
3436 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3440 (i = 0; i < l_Nplm; ++i)
3442 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3444 // take only the last seven bytes
3445 l_packet_len |= (l_tmp & 0x7f);
3453 // store packet length and proceed to next packet
3460 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3469 * Reads a PLT marker (Packet length, tile-part header)
3471 * @param p_header_data the data contained in the PLT box.
3472 * @param p_j2k the jpeg2000 codec.
3473 * @param p_header_size the size of the data contained in the PLT marker.
3474 * @param p_manager the user event manager.
3476 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3477 OPJ_BYTE * p_header_data,
3478 OPJ_UINT32 p_header_size,
3479 opj_event_mgr_t * p_manager
3482 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3485 assert(p_header_data != 00);
3486 assert(p_j2k != 00);
3487 assert(p_manager != 00);
3489 if (p_header_size < 1) {
3490 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3494 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3498 for (i = 0; i < p_header_size; ++i) {
3499 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3501 /* take only the last seven bytes */
3502 l_packet_len |= (l_tmp & 0x7f);
3507 /* store packet length and proceed to next packet */
3512 if (l_packet_len != 0) {
3513 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3522 * Reads a PPM marker (Packed packet headers, main header)
3524 * @param p_header_data the data contained in the POC box.
3525 * @param p_j2k the jpeg2000 codec.
3526 * @param p_header_size the size of the data contained in the POC marker.
3527 * @param p_manager the user event manager.
3530 opj_bool j2k_read_ppm_v2 (
3531 opj_j2k_v2_t *p_j2k,
3532 OPJ_BYTE * p_header_data,
3533 OPJ_UINT32 p_header_size,
3534 struct opj_event_mgr * p_manager
3538 opj_cp_v2_t *l_cp = 00;
3539 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3542 assert(p_header_data != 00);
3543 assert(p_j2k != 00);
3544 assert(p_manager != 00);
3546 if (p_header_size < 1) {
3547 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3551 l_cp = &(p_j2k->m_cp);
3554 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3558 /* First PPM marker */
3560 if (p_header_size < 4) {
3561 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3565 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3569 /* First PPM marker: Initialization */
3570 l_cp->ppm_len = l_N_ppm;
3571 l_cp->ppm_data_size = 0;
3573 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3574 if (l_cp->ppm_buffer == 00) {
3575 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3578 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3580 l_cp->ppm_data = l_cp->ppm_buffer;
3584 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3585 if (p_header_size >= 4) {
3587 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3590 l_cp->ppm_len += l_N_ppm ;
3592 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3593 if (l_cp->ppm_buffer == 00) {
3594 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3597 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3599 l_cp->ppm_data = l_cp->ppm_buffer;
3606 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3608 if (l_remaining_data <= p_header_size) {
3609 /* we must store less information than available in the packet */
3610 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3611 l_cp->ppm_data_size = l_cp->ppm_len;
3612 p_header_size -= l_remaining_data;
3613 p_header_data += l_remaining_data;
3616 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3617 l_cp->ppm_data_size += p_header_size;
3618 p_header_data += p_header_size;
3631 * Reads a PPM marker (Packed packet headers, main header)
3633 * @param p_header_data the data contained in the POC box.
3634 * @param p_j2k the jpeg2000 codec.
3635 * @param p_header_size the size of the data contained in the POC marker.
3636 * @param p_manager the user event manager.
3638 opj_bool j2k_read_ppm_v3 (
3639 opj_j2k_v2_t *p_j2k,
3640 OPJ_BYTE * p_header_data,
3641 OPJ_UINT32 p_header_size,
3642 struct opj_event_mgr * p_manager
3645 opj_cp_v2_t *l_cp = 00;
3646 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3649 assert(p_header_data != 00);
3650 assert(p_j2k != 00);
3651 assert(p_manager != 00);
3653 /* Minimum size of PPM marker is equal to the size of Zppm element */
3654 if (p_header_size < 1) {
3655 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3659 l_cp = &(p_j2k->m_cp);
3662 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3666 /* First PPM marker */
3668 /* We need now at least the Nppm^0 element */
3669 if (p_header_size < 4) {
3670 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3674 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3678 /* First PPM marker: Initialization */
3679 l_cp->ppm_len = l_N_ppm;
3680 l_cp->ppm_data_read = 0;
3682 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3683 if (l_cp->ppm_data == 00) {
3684 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3687 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3689 l_cp->ppm_data_current = l_cp->ppm_data;
3691 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3694 if (p_header_size < 4) {
3695 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3699 /* Uncompleted Ippm series in the previous PPM marker?*/
3700 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3701 /* Get the place where add the remaining Ippm series*/
3702 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3703 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3706 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3710 /* Increase the size of ppm_data to add the new Ippm series*/
3711 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3713 /* Keep the position of the place where concatenate the new series*/
3714 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3715 l_cp->ppm_len += l_N_ppm;
3720 l_remaining_data = p_header_size;
3722 while (l_remaining_data >= l_N_ppm) {
3723 /* read a complete Ippm series*/
3724 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3725 p_header_size -= l_N_ppm;
3726 p_header_data += l_N_ppm;
3728 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3732 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3737 l_remaining_data = p_header_size;
3741 l_remaining_data = p_header_size;
3743 /* Next Ippm series is a complete series ?*/
3744 if (l_remaining_data > l_N_ppm) {
3745 /* Increase the size of ppm_data to add the new Ippm series*/
3746 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3748 /* Keep the position of the place where concatenate the new series */
3749 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3750 l_cp->ppm_len += l_N_ppm;
3755 /* Need to read an incomplete Ippm series*/
3756 if (l_remaining_data) {
3757 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3759 /* Keep the position of the place where concatenate the new series*/
3760 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3761 l_cp->ppm_len += l_N_ppm;
3763 /* Read incomplete Ippm series*/
3764 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3765 p_header_size -= l_remaining_data;
3766 p_header_data += l_remaining_data;
3768 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3773 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3774 if (p_header_size >= 4) {
3776 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3779 l_cp->ppm_len += l_N_ppm ;
3781 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3782 if (l_cp->ppm_buffer == 00) {
3783 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3786 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3788 l_cp->ppm_data = l_cp->ppm_buffer;
3795 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3797 if (l_remaining_data <= p_header_size) {
3798 /* we must store less information than available in the packet */
3799 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3800 l_cp->ppm_data_size = l_cp->ppm_len;
3801 p_header_size -= l_remaining_data;
3802 p_header_data += l_remaining_data;
3805 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3806 l_cp->ppm_data_size += p_header_size;
3807 p_header_data += p_header_size;
3817 * Reads a PPT marker (Packed packet headers, tile-part header)
3819 * @param p_header_data the data contained in the PPT box.
3820 * @param p_j2k the jpeg2000 codec.
3821 * @param p_header_size the size of the data contained in the PPT marker.
3822 * @param p_manager the user event manager.
3824 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3825 OPJ_BYTE * p_header_data,
3826 OPJ_UINT32 p_header_size,
3827 opj_event_mgr_t * p_manager
3830 opj_cp_v2_t *l_cp = 00;
3831 opj_tcp_v2_t *l_tcp = 00;
3835 assert(p_header_data != 00);
3836 assert(p_j2k != 00);
3837 assert(p_manager != 00);
3839 /* We need to have the Z_ppt element at minimum */
3840 if (p_header_size < 1) {
3841 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3845 l_cp = &(p_j2k->m_cp);
3847 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");
3851 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3854 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3858 /* Allocate buffer to read the packet header */
3860 /* First PPT marker */
3861 l_tcp->ppt_data_size = 0;
3862 l_tcp->ppt_len = p_header_size;
3864 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3865 if (l_tcp->ppt_buffer == 00) {
3866 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3869 l_tcp->ppt_data = l_tcp->ppt_buffer;
3871 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3874 l_tcp->ppt_len += p_header_size;
3876 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3877 if (l_tcp->ppt_buffer == 00) {
3878 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3881 l_tcp->ppt_data = l_tcp->ppt_buffer;
3883 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3886 /* Read packet header from buffer */
3887 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3889 l_tcp->ppt_data_size += p_header_size;
3895 * Writes the TLM marker (Tile Length Marker)
3897 * @param p_stream the stream to write data to.
3898 * @param p_j2k J2K codec.
3899 * @param p_manager the user event manager.
3901 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3902 opj_stream_private_t *p_stream,
3903 opj_event_mgr_t * p_manager
3906 OPJ_BYTE * l_current_data = 00;
3907 OPJ_UINT32 l_tlm_size;
3910 assert(p_j2k != 00);
3911 assert(p_manager != 00);
3912 assert(p_stream != 00);
3914 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3916 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3917 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3918 = (OPJ_BYTE*)opj_realloc(
3919 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3922 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3926 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3929 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3931 /* change the way data is written to avoid seeking if possible */
3933 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
3935 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
3936 l_current_data += 2;
3938 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
3939 l_current_data += 2;
3941 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
3944 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3947 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
3948 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) {
3956 * Writes the SOT marker (Start of tile-part)
3958 * @param p_stream the stream to write data to.
3959 * @param p_j2k J2K codec.
3960 * @param p_manager the user event manager.
3962 opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
3964 OPJ_UINT32 * p_data_written,
3965 const opj_stream_private_t *p_stream,
3966 opj_event_mgr_t * p_manager
3970 assert(p_j2k != 00);
3971 assert(p_manager != 00);
3972 assert(p_stream != 00);
3974 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
3977 opj_write_bytes(p_data,10,2); /* Lsot */
3980 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
3986 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
3989 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
3994 /* update markers struct */
3996 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
3998 assert( 0 && "TODO" );
3999 #endif /* USE_JPWL */
4001 * p_data_written = 12;
4010 * Reads a PPT marker (Packed packet headers, tile-part header)
4012 * @param p_header_data the data contained in the PPT box.
4013 * @param p_j2k the jpeg2000 codec.
4014 * @param p_header_size the size of the data contained in the PPT marker.
4015 * @param p_manager the user event manager.
4017 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4018 OPJ_BYTE * p_header_data,
4019 OPJ_UINT32 p_header_size,
4020 opj_event_mgr_t * p_manager )
4022 opj_cp_v2_t *l_cp = 00;
4023 opj_tcp_v2_t *l_tcp = 00;
4024 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4025 OPJ_UINT32 l_current_part;
4026 OPJ_UINT32 l_tile_x,l_tile_y;
4029 assert(p_header_data != 00);
4030 assert(p_j2k != 00);
4031 assert(p_manager != 00);
4033 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4034 if (p_header_size != 8) {
4035 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4039 l_cp = &(p_j2k->m_cp);
4040 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4043 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4044 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4045 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4048 if (l_cp->correct) {
4050 int tileno = p_j2k->m_current_tile_number;
4051 static int backup_tileno = 0;
4053 /* tileno is negative or larger than the number of tiles!!! */
4054 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4055 opj_event_msg_v2(p_manager, EVT_ERROR,
4056 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4057 tileno, (l_cp->tw * l_cp->th));
4059 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4062 /* we try to correct */
4063 tileno = backup_tileno;
4064 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4065 "- setting tile number to %d\n",
4069 /* keep your private count of tiles */
4072 #endif /* USE_JPWL */
4074 /* look for the tile in the list of already processed tile (in parts). */
4075 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4076 /* since the time taken by this function can only grow at the time */
4078 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4081 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4082 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4084 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4090 if (l_cp->correct) {
4092 /* totlen is negative or larger than the bytes left!!! */
4093 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))) { */
4094 opj_event_msg_v2(p_manager, EVT_ERROR,
4095 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4096 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4098 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4101 /* we try to correct */
4103 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4104 "- setting Psot to %d => assuming it is the last tile\n",
4108 #endif /* USE_JPWL */
4110 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4112 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4113 "we assuming it is the last tile-part of the codestream.\n");
4114 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4117 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4120 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4123 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4124 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4125 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4126 if (l_tcp->m_nb_tile_parts) {
4127 if (l_current_part >= l_tcp->m_nb_tile_parts){
4128 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4129 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4130 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4134 l_tcp->m_nb_tile_parts = l_num_parts;
4137 /* If know the number of tile part header we will check if we didn't read the last*/
4138 if (l_tcp->m_nb_tile_parts) {
4139 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4140 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4144 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4145 /* Keep the size of data to skip after this marker */
4146 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4149 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4150 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4153 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4155 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4156 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4157 p_j2k->m_specific_param.m_decoder.m_skip_data =
4158 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4159 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4160 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4161 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4164 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4165 p_j2k->m_specific_param.m_decoder.m_skip_data =
4166 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4170 if (p_j2k->cstr_index)
4172 assert(p_j2k->cstr_index->tile_index != 00);
4173 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4174 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4176 if (l_num_parts != 0){
4177 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4178 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4181 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4182 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4183 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4185 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4186 (opj_tp_index_t*)opj_realloc(
4187 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4188 l_num_parts* sizeof(opj_tp_index_t));
4191 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4193 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4194 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4195 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4196 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4197 sizeof(opj_tp_index_t));
4200 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4201 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4202 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4203 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4204 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4205 * sizeof(opj_tp_index_t));
4214 /* 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 */
4215 /* if (p_j2k->cstr_info) {
4218 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4221 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4222 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4223 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4224 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4227 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4230 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4234 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4237 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4238 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4239 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4245 * Writes the SOD marker (Start of data)
4247 * @param p_stream the stream to write data to.
4248 * @param p_j2k J2K codec.
4249 * @param p_manager the user event manager.
4251 opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
4252 opj_tcd_v2_t * p_tile_coder,
4254 OPJ_UINT32 * p_data_written,
4255 OPJ_UINT32 p_total_data_size,
4256 const opj_stream_private_t *p_stream,
4257 opj_event_mgr_t * p_manager
4260 opj_tcp_v2_t *l_tcp = 00;
4261 opj_codestream_info_t *l_cstr_info = 00;
4262 opj_cp_v2_t *l_cp = 00;
4264 OPJ_UINT32 l_size_tile;
4265 OPJ_UINT32 l_remaining_data;
4268 assert(p_j2k != 00);
4269 assert(p_manager != 00);
4270 assert(p_stream != 00);
4272 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4275 /* make room for the EOF marker */
4276 l_remaining_data = p_total_data_size - 4;
4278 l_cp = &(p_j2k->m_cp);
4279 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4282 /* update tile coder */
4283 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4284 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4286 l_size_tile = l_cp->th * l_cp->tw;
4289 /* TODO mergeV2: check this part which use cstr_info */
4290 /*l_cstr_info = p_j2k->cstr_info;
4292 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4293 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4294 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4300 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4302 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4307 /* update markers struct */
4308 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4310 assert( 0 && "TODO" );
4311 #endif /* USE_JPWL */
4316 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4317 p_tile_coder->tcd_image->tiles->packno = 0;
4319 l_cstr_info->packno = 0;
4323 *p_data_written = 0;
4325 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)) {
4326 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4330 *p_data_written += 2;
4339 * Reads a SOD marker (Start Of Data)
4341 * @param p_header_data the data contained in the SOD box.
4342 * @param p_j2k the jpeg2000 codec.
4343 * @param p_header_size the size of the data contained in the SOD marker.
4344 * @param p_manager the user event manager.
4346 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4347 opj_stream_private_t *p_stream,
4348 opj_event_mgr_t * p_manager
4351 OPJ_UINT32 l_current_read_size;
4352 opj_codestream_index_t * l_cstr_index = 00;
4353 OPJ_BYTE ** l_current_data = 00;
4354 opj_tcp_v2_t * l_tcp = 00;
4355 OPJ_UINT32 * l_tile_len = 00;
4358 assert(p_j2k != 00);
4359 assert(p_manager != 00);
4360 assert(p_stream != 00);
4362 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4364 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4365 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4366 // but we are in the last tile part,
4367 // so its result will fit on OPJ_UINT32 unless we find
4368 // a file with a single tile part of more than 4 GB...*/
4369 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4372 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4374 l_current_data = &(l_tcp->m_data);
4375 l_tile_len = &l_tcp->m_data_size;
4377 if (! *l_current_data) {
4378 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4381 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4384 if (*l_current_data == 00) {
4385 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4391 l_cstr_index = p_j2k->cstr_index;
4393 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4395 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4396 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4398 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4399 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4401 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4405 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4407 /*l_cstr_index->packno = 0;*/
4410 l_current_read_size = opj_stream_read_data( p_stream,
4411 *l_current_data + *l_tile_len,
4412 p_j2k->m_specific_param.m_decoder.m_sot_length,
4415 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4416 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4419 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4422 *l_tile_len += l_current_read_size;
4428 * Writes the RGN marker (Region Of Interest)
4430 * @param p_tile_no the tile to output
4431 * @param p_comp_no the component to output
4432 * @param p_stream the stream to write data to.
4433 * @param p_j2k J2K codec.
4434 * @param p_manager the user event manager.
4436 opj_bool opj_j2k_write_rgn(opj_j2k_v2_t *p_j2k,
4437 OPJ_UINT32 p_tile_no,
4438 OPJ_UINT32 p_comp_no,
4439 opj_stream_private_t *p_stream,
4440 opj_event_mgr_t * p_manager
4443 OPJ_BYTE * l_current_data = 00;
4444 OPJ_UINT32 l_nb_comp;
4445 OPJ_UINT32 l_rgn_size;
4446 opj_image_t *l_image = 00;
4447 opj_cp_v2_t *l_cp = 00;
4448 opj_tcp_v2_t *l_tcp = 00;
4449 opj_tccp_t *l_tccp = 00;
4450 OPJ_UINT32 l_comp_room;
4453 assert(p_j2k != 00);
4454 assert(p_manager != 00);
4455 assert(p_stream != 00);
4457 l_cp = &(p_j2k->m_cp);
4458 l_tcp = &l_cp->tcps[p_tile_no];
4459 l_tccp = &l_tcp->tccps[p_comp_no];
4461 l_nb_comp = l_image->numcomps;
4463 if (l_nb_comp <= 256) {
4470 l_rgn_size = 6 + l_comp_room;
4472 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4474 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4475 l_current_data += 2;
4477 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4478 l_current_data += 2;
4480 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4481 l_current_data+=l_comp_room;
4483 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4486 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4489 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) {
4497 static void j2k_write_eoc(opj_j2k_t *j2k) {
4498 opj_cio_t *cio = j2k->cio;
4499 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4500 cio_write(cio, J2K_MS_EOC, 2);
4504 /* update markers struct */
4505 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4506 #endif /* USE_JPWL */
4511 * Writes the EOC marker (End of Codestream)
4513 * @param p_stream the stream to write data to.
4514 * @param p_j2k J2K codec.
4515 * @param p_manager the user event manager.
4517 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4518 struct opj_stream_private *p_stream,
4519 struct opj_event_mgr * p_manager )
4522 assert(p_j2k != 00);
4523 assert(p_manager != 00);
4524 assert(p_stream != 00);
4526 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4531 /* update markers struct */
4533 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4535 #endif /* USE_JPWL */
4537 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4541 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4550 * Reads a RGN marker (Region Of Interest)
4552 * @param p_header_data the data contained in the POC box.
4553 * @param p_j2k the jpeg2000 codec.
4554 * @param p_header_size the size of the data contained in the POC marker.
4555 * @param p_manager the user event manager.
4557 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4558 OPJ_BYTE * p_header_data,
4559 OPJ_UINT32 p_header_size,
4560 opj_event_mgr_t * p_manager
4563 OPJ_UINT32 l_nb_comp;
4564 opj_image_t * l_image = 00;
4566 opj_cp_v2_t *l_cp = 00;
4567 opj_tcp_v2_t *l_tcp = 00;
4568 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4571 assert(p_header_data != 00);
4572 assert(p_j2k != 00);
4573 assert(p_manager != 00);
4575 l_image = p_j2k->m_private_image;
4576 l_nb_comp = l_image->numcomps;
4578 if (l_nb_comp <= 256) {
4583 if (p_header_size != 2 + l_comp_room) {
4584 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4588 l_cp = &(p_j2k->m_cp);
4589 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4590 &l_cp->tcps[p_j2k->m_current_tile_number] :
4591 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4593 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4594 p_header_data+=l_comp_room;
4595 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4599 if (l_cp->correct) {
4600 /* totlen is negative or larger than the bytes left!!! */
4601 if (l_comp_room >= l_nb_comp) {
4602 opj_event_msg_v2(p_manager, EVT_ERROR,
4603 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4604 l_comp_room, l_nb_comp);
4605 if (!JPWL_ASSUME || JPWL_ASSUME) {
4606 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4611 #endif /* USE_JPWL */
4613 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4620 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
4622 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4625 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
4632 * Updates the rates of the tcp.
4634 * @param p_stream the stream to write data to.
4635 * @param p_j2k J2K codec.
4636 * @param p_manager the user event manager.
4638 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4639 struct opj_stream_private *p_stream,
4640 struct opj_event_mgr * p_manager )
4642 opj_cp_v2_t * l_cp = 00;
4643 opj_image_t * l_image = 00;
4644 opj_tcp_v2_t * l_tcp = 00;
4645 opj_image_comp_t * l_img_comp = 00;
4648 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4649 OPJ_FLOAT32 * l_rates = 0;
4650 OPJ_FLOAT32 l_sot_remove;
4651 OPJ_UINT32 l_bits_empty, l_size_pixel;
4652 OPJ_UINT32 l_tile_size = 0;
4653 OPJ_UINT32 l_last_res;
4654 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4657 assert(p_j2k != 00);
4658 assert(p_manager != 00);
4659 assert(p_stream != 00);
4662 l_cp = &(p_j2k->m_cp);
4663 l_image = p_j2k->m_private_image;
4666 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4667 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4668 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4670 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4671 l_tp_stride_func = get_tp_stride;
4674 l_tp_stride_func = get_default_stride;
4677 for (i=0;i<l_cp->th;++i) {
4678 for (j=0;j<l_cp->tw;++j) {
4679 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4681 /* 4 borders of the tile rescale on the image if necessary */
4682 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4683 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4684 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4685 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4687 l_rates = l_tcp->rates;
4689 /* Modification of the RATE >> */
4691 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4693 ((*l_rates) * l_bits_empty)
4701 for (k = 1; k < l_tcp->numlayers; ++k) {
4703 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4705 ((*l_rates) * l_bits_empty)
4721 for (i=0;i<l_cp->th;++i) {
4722 for (j=0;j<l_cp->tw;++j) {
4723 l_rates = l_tcp->rates;
4726 *l_rates -= l_sot_remove;
4728 if (*l_rates < 30) {
4735 l_last_res = l_tcp->numlayers - 1;
4737 for (k = 1; k < l_last_res; ++k) {
4740 *l_rates -= l_sot_remove;
4742 if (*l_rates < *(l_rates - 1) + 10) {
4743 *l_rates = (*(l_rates - 1)) + 20;
4751 *l_rates -= (l_sot_remove + 2.f);
4753 if (*l_rates < *(l_rates - 1) + 10) {
4754 *l_rates = (*(l_rates - 1)) + 20;
4762 l_img_comp = l_image->comps;
4765 for (i=0;i<l_image->numcomps;++i) {
4766 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4768 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4776 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4778 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4780 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4781 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4782 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4783 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4787 if (l_cp->m_specific_param.m_enc.m_cinema) {
4788 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4789 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4790 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4794 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4795 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4801 static void j2k_read_eoc(opj_j2k_t *j2k) {
4803 opj_bool success = OPJ_FALSE;
4805 /* if packets should be decoded */
4806 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4807 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4808 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4809 for (i = 0; i < j2k->cp->tileno_size; i++) {
4810 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4811 if (j2k->cp->tileno[i] != -1)
4813 tileno = j2k->cp->tileno[i];
4814 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4815 opj_free(j2k->tile_data[tileno]);
4816 j2k->tile_data[tileno] = NULL;
4817 tcd_free_decode_tile(tcd, i);
4820 success = OPJ_FALSE;
4821 if (success == OPJ_FALSE) {
4822 j2k->state |= J2K_STATE_ERR;
4826 tcd_free_decode(tcd);
4829 /* if packets should not be decoded */
4831 for (i = 0; i < j2k->cp->tileno_size; i++) {
4832 tileno = j2k->cp->tileno[i];
4833 opj_free(j2k->tile_data[tileno]);
4834 j2k->tile_data[tileno] = NULL;
4837 if (j2k->state & J2K_STATE_ERR)
4838 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4840 j2k->state = J2K_STATE_MT;
4844 * Reads a EOC marker (End Of Codestream)
4846 * @param p_header_data the data contained in the SOD box.
4847 * @param p_j2k the jpeg2000 codec.
4848 * @param p_header_size the size of the data contained in the SOD marker.
4849 * @param p_manager the user event manager.
4852 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4853 struct opj_stream_private *p_stream,
4854 struct opj_event_mgr * p_manager )
4857 opj_tcd_v2_t * l_tcd = 00;
4858 OPJ_UINT32 l_nb_tiles;
4859 opj_tcp_v2_t * l_tcp = 00;
4863 assert(p_j2k != 00);
4864 assert(p_manager != 00);
4865 assert(p_stream != 00);
4867 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4868 l_tcp = p_j2k->m_cp.tcps;
4870 l_tcd = tcd_create_v2(OPJ_TRUE);
4872 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4876 for (i = 0; i < l_nb_tiles; ++i) {
4877 if (l_tcp->m_data) {
4878 if (! tcd_init_decode_tile(l_tcd, i)) {
4879 tcd_destroy_v2(l_tcd);
4880 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4884 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4888 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4893 opj_j2k_tcp_destroy(l_tcp);
4897 tcd_destroy_v2(l_tcd);
4903 * Gets the offset of the header.
4905 * @param p_stream the stream to write data to.
4906 * @param p_j2k J2K codec.
4907 * @param p_manager the user event manager.
4909 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
4910 struct opj_stream_private *p_stream,
4911 struct opj_event_mgr * p_manager )
4914 assert(p_j2k != 00);
4915 assert(p_manager != 00);
4916 assert(p_stream != 00);
4918 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4924 * Writes the MCT marker (Multiple Component Transform)
4926 * @param p_stream the stream to write data to.
4927 * @param p_j2k J2K codec.
4928 * @param p_manager the user event manager.
4930 opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
4931 struct opj_stream_private *p_stream,
4932 struct opj_event_mgr * p_manager )
4935 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4936 opj_mct_data_t * l_mct_record;
4937 opj_tcp_v2_t * l_tcp;
4940 assert(p_j2k != 00);
4941 assert(p_stream != 00);
4942 assert(p_manager != 00);
4944 if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
4948 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4949 l_mct_record = l_tcp->m_mct_records;
4951 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4953 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
4960 l_mcc_record = l_tcp->m_mcc_records;
4962 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
4964 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
4971 if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
4979 * Writes the image components.
4981 * @param p_stream the stream to write data to.
4982 * @param p_j2k J2K codec.
4983 * @param p_manager the user event manager.
4985 opj_bool opj_j2k_write_image_components(opj_j2k_v2_t *p_j2k,
4986 struct opj_stream_private *p_stream,
4987 struct opj_event_mgr * p_manager )
4992 assert(p_j2k != 00);
4993 assert(p_manager != 00);
4994 assert(p_stream != 00);
4996 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
4998 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
5002 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
5011 * Writes regions of interests.
5013 * @param p_stream the stream to write data to.
5014 * @param p_j2k J2K codec.
5015 * @param p_manager the user event manager.
5017 opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
5018 struct opj_stream_private *p_stream,
5019 struct opj_event_mgr * p_manager )
5022 const opj_tccp_t *l_tccp = 00;
5025 assert(p_j2k != 00);
5026 assert(p_manager != 00);
5027 assert(p_stream != 00);
5029 l_tccp = p_j2k->m_cp.tcps->tccps;
5031 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5032 if (l_tccp->roishift) {
5034 if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) {
5048 * @param p_stream the stream to write data to.
5049 * @param p_j2k J2K codec.
5050 * @param p_manager the user event manager.
5052 opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k,
5053 struct opj_stream_private *p_stream,
5054 struct opj_event_mgr * p_manager )
5056 opj_codestream_index_t * l_cstr_index = 00;
5059 assert(p_j2k != 00);
5060 assert(p_manager != 00);
5061 assert(p_stream != 00);
5063 l_cstr_index = p_j2k->cstr_index;
5065 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5067 /* The following adjustment is done to adjust the codestream size */
5068 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5069 /* the first bunch of bytes is not in the codestream */
5070 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5075 /* preparation of JPWL marker segments */
5079 /* encode according to JPWL */
5080 jpwl_encode(p_j2k, p_stream, image);
5084 assert( 0 && "TODO" );
5085 #endif /* USE_JPWL */
5090 typedef struct opj_dec_mstabent {
5093 /** value of the state when the marker can appear */
5095 /** action linked to the marker */
5096 void (*handler) (opj_j2k_t *j2k);
5097 } opj_dec_mstabent_t;
5099 opj_dec_mstabent_t j2k_dec_mstab[] = {
5100 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5101 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5102 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5103 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5104 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5105 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5106 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5107 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5108 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5109 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5110 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5111 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5112 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5113 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5114 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5115 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5117 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5118 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5121 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5122 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5123 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5124 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5125 #endif /* USE_JPWL */
5127 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5128 {J2K_MS_INSEC, 0, j2k_read_insec},
5129 #endif /* USE_JPSEC */
5131 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5137 * Reads an unknown marker
5139 * @param p_stream the stream object to read from.
5140 * @param p_j2k the jpeg2000 codec.
5141 * @param p_manager the user event manager.
5143 * @return true if the marker could be deduced.
5145 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5146 opj_stream_private_t *p_stream,
5147 OPJ_UINT32 *output_marker,
5148 opj_event_mgr_t * p_manager
5151 OPJ_UINT32 l_unknown_marker;
5152 const opj_dec_memory_marker_handler_t * l_marker_handler;
5153 OPJ_UINT32 l_size_unk = 2;
5156 assert(p_j2k != 00);
5157 assert(p_manager != 00);
5158 assert(p_stream != 00);
5160 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5163 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5164 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5165 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5169 /* read 2 bytes as the new marker ID*/
5170 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5172 if (!(l_unknown_marker < 0xff00)) {
5174 /* Get the marker handler from the marker ID*/
5175 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5177 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5178 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5182 if (l_marker_handler->id != J2K_MS_UNK) {
5183 /* Add the marker to the codestream index*/
5184 if (l_marker_handler->id != J2K_MS_SOT)
5185 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5186 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5188 break; /* next marker is known and well located */
5196 *output_marker = l_marker_handler->id ;
5202 Read the lookup table containing all the marker, status and action
5203 @param id Marker value
5205 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5206 opj_dec_mstabent_t *e;
5207 for (e = j2k_dec_mstab; e->id != 0; e++) {
5216 * Writes the MCT marker (Multiple Component Transform)
5218 * @param p_stream the stream to write data to.
5219 * @param p_j2k J2K codec.
5220 * @param p_manager the user event manager.
5222 opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5223 opj_mct_data_t * p_mct_record,
5224 struct opj_stream_private *p_stream,
5225 struct opj_event_mgr * p_manager )
5227 OPJ_UINT32 l_mct_size;
5228 OPJ_BYTE * l_current_data = 00;
5232 assert(p_j2k != 00);
5233 assert(p_manager != 00);
5234 assert(p_stream != 00);
5236 l_mct_size = 10 + p_mct_record->m_data_size;
5238 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5239 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5240 = (OPJ_BYTE*)opj_realloc(
5241 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5244 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5248 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5251 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5253 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5254 l_current_data += 2;
5256 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5257 l_current_data += 2;
5259 opj_write_bytes(l_current_data,0,2); /* Zmct */
5260 l_current_data += 2;
5262 /* only one marker atm */
5263 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5265 opj_write_bytes(l_current_data,l_tmp,2);
5266 l_current_data += 2;
5268 opj_write_bytes(l_current_data,0,2); /* Ymct */
5271 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5273 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) {
5281 * Reads a MCT marker (Multiple Component Transform)
5283 * @param p_header_data the data contained in the MCT box.
5284 * @param p_j2k the jpeg2000 codec.
5285 * @param p_header_size the size of the data contained in the MCT marker.
5286 * @param p_manager the user event manager.
5288 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5289 OPJ_BYTE * p_header_data,
5290 OPJ_UINT32 p_header_size,
5291 opj_event_mgr_t * p_manager
5295 opj_tcp_v2_t *l_tcp = 00;
5298 opj_mct_data_t * l_mct_data;
5301 assert(p_header_data != 00);
5302 assert(p_j2k != 00);
5304 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5305 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5306 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5308 if (p_header_size < 2) {
5309 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5314 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5317 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5321 if(p_header_size <= 6) {
5322 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5326 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5327 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5330 l_indix = l_tmp & 0xff;
5331 l_mct_data = l_tcp->m_mct_records;
5333 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5334 if (l_mct_data->m_index == l_indix) {
5341 if (i == l_tcp->m_nb_mct_records) {
5342 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5343 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5345 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));
5346 if(! l_tcp->m_mct_records) {
5347 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5351 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5352 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5355 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5358 if (l_mct_data->m_data) {
5359 opj_free(l_mct_data->m_data);
5360 l_mct_data->m_data = 00;
5363 l_mct_data->m_index = l_indix;
5364 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5365 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5367 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5370 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5376 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5377 if (! l_mct_data->m_data) {
5378 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5381 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5383 l_mct_data->m_data_size = p_header_size;
5384 ++l_tcp->m_nb_mct_records;
5390 * Writes the MCC marker (Multiple Component Collection)
5392 * @param p_stream the stream to write data to.
5393 * @param p_j2k J2K codec.
5394 * @param p_manager the user event manager.
5396 opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5397 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5398 struct opj_stream_private *p_stream,
5399 struct opj_event_mgr * p_manager )
5402 OPJ_UINT32 l_mcc_size;
5403 OPJ_BYTE * l_current_data = 00;
5404 OPJ_UINT32 l_nb_bytes_for_comp;
5409 assert(p_j2k != 00);
5410 assert(p_manager != 00);
5411 assert(p_stream != 00);
5413 if (p_mcc_record->m_nb_comps > 255 ) {
5414 l_nb_bytes_for_comp = 2;
5418 l_nb_bytes_for_comp = 1;
5422 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5423 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5425 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5426 = (OPJ_BYTE*)opj_realloc(
5427 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5429 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5433 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5436 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5438 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5439 l_current_data += 2;
5441 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5442 l_current_data += 2;
5445 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5446 l_current_data += 2;
5448 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5451 /* only one marker atm */
5452 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5455 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5458 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5461 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 */
5464 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5465 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5466 l_current_data+=l_nb_bytes_for_comp;
5469 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 */
5472 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5474 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5475 l_current_data+=l_nb_bytes_for_comp;
5478 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5480 if (p_mcc_record->m_decorrelation_array) {
5481 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5484 if (p_mcc_record->m_offset_array) {
5485 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5488 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5491 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) {
5499 * Reads a MCC marker (Multiple Component Collection)
5501 * @param p_header_data the data contained in the MCC box.
5502 * @param p_j2k the jpeg2000 codec.
5503 * @param p_header_size the size of the data contained in the MCC marker.
5504 * @param p_manager the user event manager.
5506 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5507 OPJ_BYTE * p_header_data,
5508 OPJ_UINT32 p_header_size,
5509 opj_event_mgr_t * p_manager )
5514 opj_tcp_v2_t * l_tcp;
5515 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5516 opj_mct_data_t * l_mct_data;
5517 OPJ_UINT32 l_nb_collections;
5518 OPJ_UINT32 l_nb_comps;
5519 OPJ_UINT32 l_nb_bytes_by_comp;
5523 assert(p_header_data != 00);
5524 assert(p_j2k != 00);
5525 assert(p_manager != 00);
5527 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5528 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5529 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5531 if (p_header_size < 2) {
5532 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5537 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5540 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5544 if (p_header_size < 7) {
5545 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5549 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5552 l_mcc_record = l_tcp->m_mcc_records;
5554 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5555 if (l_mcc_record->m_index == l_indix) {
5562 if (i == l_tcp->m_nb_mcc_records) {
5563 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5564 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5566 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5567 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5568 if (! l_tcp->m_mcc_records) {
5569 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5572 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5573 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));
5575 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5577 l_mcc_record->m_index = l_indix;
5579 /* only one marker atm */
5580 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5583 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5587 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5590 if (l_nb_collections > 1) {
5591 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5597 for (i=0;i<l_nb_collections;++i) {
5598 if (p_header_size < 3) {
5599 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5603 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5607 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5611 opj_read_bytes(p_header_data,&l_nb_comps,2);
5616 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5617 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5619 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5620 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5624 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5626 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5627 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5628 p_header_data+=l_nb_bytes_by_comp;
5631 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5636 opj_read_bytes(p_header_data,&l_nb_comps,2);
5639 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5640 l_nb_comps &= 0x7fff;
5642 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5643 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5647 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5648 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5652 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5654 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5655 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5656 p_header_data+=l_nb_bytes_by_comp;
5659 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5664 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5667 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5668 l_mcc_record->m_decorrelation_array = 00;
5669 l_mcc_record->m_offset_array = 00;
5671 l_indix = l_tmp & 0xff;
5673 l_mct_data = l_tcp->m_mct_records;
5674 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5675 if (l_mct_data->m_index == l_indix) {
5676 l_mcc_record->m_decorrelation_array = l_mct_data;
5682 if (l_mcc_record->m_decorrelation_array == 00) {
5683 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5688 l_indix = (l_tmp >> 8) & 0xff;
5690 l_mct_data = l_tcp->m_mct_records;
5691 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5692 if (l_mct_data->m_index == l_indix) {
5693 l_mcc_record->m_offset_array = l_mct_data;
5699 if (l_mcc_record->m_offset_array == 00) {
5700 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5706 if (p_header_size != 0) {
5707 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5711 ++l_tcp->m_nb_mcc_records;
5718 * Writes the MCO marker (Multiple component transformation ordering)
5720 * @param p_stream the stream to write data to.
5721 * @param p_j2k J2K codec.
5722 * @param p_manager the user event manager.
5724 opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
5725 struct opj_stream_private *p_stream,
5726 struct opj_event_mgr * p_manager
5729 OPJ_BYTE * l_current_data = 00;
5730 OPJ_UINT32 l_mco_size;
5731 opj_tcp_v2_t * l_tcp = 00;
5732 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5736 assert(p_j2k != 00);
5737 assert(p_manager != 00);
5738 assert(p_stream != 00);
5740 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5741 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5743 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5744 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5746 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5747 = (OPJ_BYTE*)opj_realloc(
5748 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5750 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5755 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5758 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5759 l_current_data += 2;
5761 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5762 l_current_data += 2;
5764 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5767 l_mcc_record = l_tcp->m_mcc_records;
5768 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5769 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
5775 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) {
5783 * Reads a MCO marker (Multiple Component Transform Ordering)
5785 * @param p_header_data the data contained in the MCO box.
5786 * @param p_j2k the jpeg2000 codec.
5787 * @param p_header_size the size of the data contained in the MCO marker.
5788 * @param p_manager the user event manager.
5790 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5791 OPJ_BYTE * p_header_data,
5792 OPJ_UINT32 p_header_size,
5793 opj_event_mgr_t * p_manager
5796 OPJ_UINT32 l_tmp, i;
5797 OPJ_UINT32 l_nb_stages;
5798 opj_tcp_v2_t * l_tcp;
5799 opj_tccp_t * l_tccp;
5800 opj_image_t * l_image;
5801 opj_image_comp_t * l_img_comp;
5804 assert(p_header_data != 00);
5805 assert(p_j2k != 00);
5806 assert(p_manager != 00);
5808 l_image = p_j2k->m_private_image;
5809 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5810 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5811 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5813 if (p_header_size < 1) {
5814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5818 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
5821 if (l_nb_stages > 1) {
5822 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5826 if (p_header_size != l_nb_stages + 1) {
5827 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5831 l_tccp = l_tcp->tccps;
5832 l_img_comp = l_image->comps;
5834 for (i=0;i<l_image->numcomps;++i) {
5835 l_tccp->m_dc_level_shift = 0;
5839 if (l_tcp->m_mct_decoding_matrix) {
5840 opj_free(l_tcp->m_mct_decoding_matrix);
5841 l_tcp->m_mct_decoding_matrix = 00;
5844 for (i=0;i<l_nb_stages;++i) {
5845 opj_read_bytes(p_header_data,&l_tmp,1);
5848 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5856 opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5859 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5860 opj_mct_data_t * l_deco_array, * l_offset_array;
5861 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5862 OPJ_UINT32 l_nb_elem;
5863 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5864 opj_tccp_t * l_tccp;
5867 assert(p_tcp != 00);
5869 l_mcc_record = p_tcp->m_mcc_records;
5871 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5872 if (l_mcc_record->m_index == p_index) {
5877 if (i==p_tcp->m_nb_mcc_records) {
5878 /** element discarded **/
5882 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5883 /** do not support number of comps != image */
5887 l_deco_array = l_mcc_record->m_decorrelation_array;
5890 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5891 if (l_deco_array->m_data_size != l_data_size) {
5895 l_nb_elem = p_image->numcomps * p_image->numcomps;
5896 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5897 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5899 if (! p_tcp->m_mct_decoding_matrix ) {
5903 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);
5906 l_offset_array = l_mcc_record->m_offset_array;
5908 if (l_offset_array) {
5909 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5910 if (l_offset_array->m_data_size != l_data_size) {
5914 l_nb_elem = p_image->numcomps;
5915 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
5916 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5918 if (! l_offset_data ) {
5922 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5924 l_tccp = p_tcp->tccps;
5925 l_current_offset_data = l_offset_data;
5927 for (i=0;i<p_image->numcomps;++i) {
5928 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
5932 opj_free(l_offset_data);
5939 * Writes the CBD marker (Component bit depth definition)
5941 * @param p_stream the stream to write data to.
5942 * @param p_j2k J2K codec.
5943 * @param p_manager the user event manager.
5945 opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
5946 struct opj_stream_private *p_stream,
5947 struct opj_event_mgr * p_manager )
5950 OPJ_UINT32 l_cbd_size;
5951 OPJ_BYTE * l_current_data = 00;
5952 opj_image_t *l_image = 00;
5953 opj_image_comp_t * l_comp = 00;
5956 assert(p_j2k != 00);
5957 assert(p_manager != 00);
5958 assert(p_stream != 00);
5960 l_image = p_j2k->m_private_image;
5961 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
5963 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5964 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5965 = (OPJ_BYTE*)opj_realloc(
5966 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5969 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5973 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
5976 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5978 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
5979 l_current_data += 2;
5981 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
5982 l_current_data += 2;
5984 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
5987 l_comp = l_image->comps;
5989 for (i=0;i<l_image->numcomps;++i) {
5990 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
5996 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) {
6004 * Reads a CBD marker (Component bit depth definition)
6005 * @param p_header_data the data contained in the CBD box.
6006 * @param p_j2k the jpeg2000 codec.
6007 * @param p_header_size the size of the data contained in the CBD marker.
6008 * @param p_manager the user event manager.
6010 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6011 OPJ_BYTE * p_header_data,
6012 OPJ_UINT32 p_header_size,
6013 opj_event_mgr_t * p_manager
6016 OPJ_UINT32 l_nb_comp,l_num_comp;
6017 OPJ_UINT32 l_comp_def;
6019 opj_image_comp_t * l_comp = 00;
6022 assert(p_header_data != 00);
6023 assert(p_j2k != 00);
6024 assert(p_manager != 00);
6026 l_num_comp = p_j2k->m_private_image->numcomps;
6028 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6029 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6033 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6036 if (l_nb_comp != l_num_comp) {
6037 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6041 l_comp = p_j2k->m_private_image->comps;
6042 for (i=0;i<l_num_comp;++i) {
6043 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6045 l_comp->sgnd = (l_comp_def>>7) & 1;
6046 l_comp->prec = (l_comp_def&0x7f) + 1;
6054 /* ----------------------------------------------------------------------- */
6055 /* J2K / JPT decoder interface */
6056 /* ----------------------------------------------------------------------- */
6058 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6060 if(j2k && parameters) {
6061 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6062 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6065 j2k->m_cp.correct = parameters->jpwl_correct;
6066 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6067 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6068 #endif /* USE_JPWL */
6073 /* ----------------------------------------------------------------------- */
6074 /* J2K encoder interface */
6075 /* ----------------------------------------------------------------------- */
6077 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6078 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6085 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6087 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6092 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6094 l_j2k->m_is_decoder = 0;
6095 l_j2k->m_cp.m_is_decoder = 0;
6097 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6098 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6099 opj_j2k_destroy(l_j2k);
6103 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6105 /* validation list creation*/
6106 l_j2k->m_validation_list = opj_procedure_list_create();
6107 if (! l_j2k->m_validation_list) {
6108 opj_j2k_destroy(l_j2k);
6112 /* execution list creation*/
6113 l_j2k->m_procedure_list = opj_procedure_list_create();
6114 if (! l_j2k->m_procedure_list) {
6115 opj_j2k_destroy(l_j2k);
6122 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6124 int tileno, numpocs_tile;
6125 opj_cp_t *cp = NULL;
6127 if(!j2k || !parameters || ! image) {
6131 /* create and initialize the coding parameters structure */
6132 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6134 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6137 /* set default values for cp */
6142 copy user encoding parameters
6144 cp->cinema = parameters->cp_cinema;
6145 cp->max_comp_size = parameters->max_comp_size;
6146 cp->rsiz = parameters->cp_rsiz;
6147 cp->disto_alloc = parameters->cp_disto_alloc;
6148 cp->fixed_alloc = parameters->cp_fixed_alloc;
6149 cp->fixed_quality = parameters->cp_fixed_quality;
6151 /* mod fixed_quality */
6152 if(parameters->cp_matrice) {
6153 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6154 cp->matrice = (int *) opj_malloc(array_size);
6155 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6159 cp->tdx = parameters->cp_tdx;
6160 cp->tdy = parameters->cp_tdy;
6163 cp->tx0 = parameters->cp_tx0;
6164 cp->ty0 = parameters->cp_ty0;
6166 /* comment string */
6167 if(parameters->cp_comment) {
6168 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6170 strcpy(cp->comment, parameters->cp_comment);
6175 calculate other encoding parameters
6178 if (parameters->tile_size_on) {
6179 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6180 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6182 cp->tdx = image->x1 - cp->tx0;
6183 cp->tdy = image->y1 - cp->ty0;
6186 if(parameters->tp_on){
6187 cp->tp_flag = parameters->tp_flag;
6192 for(i=0;i<image->numcomps ;i++){
6193 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6199 calculate JPWL encoding parameters
6202 if (parameters->jpwl_epc_on) {
6206 cp->epc_on = OPJ_TRUE;
6207 cp->info_on = OPJ_FALSE; /* no informative technique */
6210 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6211 cp->epb_on = OPJ_TRUE;
6213 cp->hprot_MH = parameters->jpwl_hprot_MH;
6214 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6215 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6216 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6218 /* if tile specs are not specified, copy MH specs */
6219 if (cp->hprot_TPH[0] == -1) {
6220 cp->hprot_TPH_tileno[0] = 0;
6221 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6223 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6224 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6225 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6226 cp->pprot[i] = parameters->jpwl_pprot[i];
6230 /* set ESD writing */
6231 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6232 cp->esd_on = OPJ_TRUE;
6234 cp->sens_size = parameters->jpwl_sens_size;
6235 cp->sens_addr = parameters->jpwl_sens_addr;
6236 cp->sens_range = parameters->jpwl_sens_range;
6238 cp->sens_MH = parameters->jpwl_sens_MH;
6239 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6240 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6241 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6245 /* always set RED writing to false: we are at the encoder */
6246 cp->red_on = OPJ_FALSE;
6249 cp->epc_on = OPJ_FALSE;
6251 #endif /* USE_JPWL */
6254 /* initialize the mutiple tiles */
6255 /* ---------------------------- */
6256 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6258 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6259 opj_tcp_t *tcp = &cp->tcps[tileno];
6260 tcp->numlayers = parameters->tcp_numlayers;
6261 assert ( tcp->numlayers >= 0 );
6262 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6264 if (cp->fixed_quality) {
6265 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6267 tcp->rates[j] = parameters->tcp_rates[j];
6269 if (cp->fixed_quality) { /* add fixed_quality */
6270 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6272 tcp->rates[j] = parameters->tcp_rates[j];
6276 tcp->csty = parameters->csty;
6277 tcp->prg = parameters->prog_order;
6278 tcp->mct = parameters->tcp_mct;
6282 if (parameters->numpocs) {
6283 /* initialisation of POC */
6285 assert( parameters->numpocs >= 0 );
6286 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6287 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6288 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6289 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6290 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6291 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6292 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6293 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6294 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6295 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6299 tcp->numpocs = numpocs_tile -1 ;
6304 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6306 for (i = 0; i < image->numcomps; i++) {
6307 opj_tccp_t *tccp = &tcp->tccps[i];
6308 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6309 tccp->numresolutions = parameters->numresolution;
6310 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6311 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6312 tccp->cblksty = parameters->mode;
6313 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6314 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6316 assert(parameters->roi_compno >= 0);
6317 if (i == (OPJ_UINT32)parameters->roi_compno) {
6318 tccp->roishift = parameters->roi_shift;
6323 if(parameters->cp_cinema)
6325 /*Precinct size for lowest frequency subband=128*/
6328 /*Precinct size at all other resolutions = 256*/
6329 for (j = 1; j < tccp->numresolutions; j++) {
6334 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6336 assert(tccp->numresolutions > 0);
6337 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6338 if (p < parameters->res_spec) {
6340 if (parameters->prcw_init[p] < 1) {
6343 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6346 if (parameters->prch_init[p] < 1) {
6349 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6353 int res_spec = parameters->res_spec;
6354 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6355 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6357 if (size_prcw < 1) {
6360 tccp->prcw[j] = int_floorlog2(size_prcw);
6363 if (size_prch < 1) {
6366 tccp->prch[j] = int_floorlog2(size_prch);
6370 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6373 for (j = 0; j < tccp->numresolutions; j++) {
6380 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6385 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6386 opj_cparameters_t *parameters,
6388 struct opj_event_mgr * p_manager)
6390 OPJ_UINT32 i, j, tileno, numpocs_tile;
6391 opj_cp_v2_t *cp = 00;
6394 if(!p_j2k || !parameters || ! image) {
6398 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6399 cp = &(p_j2k->m_cp);
6401 /* set default values for cp */
6406 copy user encoding parameters
6408 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6409 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6410 cp->rsiz = parameters->cp_rsiz;
6411 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6412 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6413 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6415 /* mod fixed_quality */
6416 if (parameters->cp_matrice) {
6417 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6418 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6419 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6423 cp->tdx = parameters->cp_tdx;
6424 cp->tdy = parameters->cp_tdy;
6427 cp->tx0 = parameters->cp_tx0;
6428 cp->ty0 = parameters->cp_ty0;
6430 /* comment string */
6431 if(parameters->cp_comment) {
6432 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6434 strcpy(cp->comment, parameters->cp_comment);
6439 calculate other encoding parameters
6442 if (parameters->tile_size_on) {
6443 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6444 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6446 cp->tdx = image->x1 - cp->tx0;
6447 cp->tdy = image->y1 - cp->ty0;
6450 if (parameters->tp_on) {
6451 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6452 cp->m_specific_param.m_enc.m_tp_on = 1;
6457 calculate JPWL encoding parameters
6460 if (parameters->jpwl_epc_on) {
6464 cp->epc_on = OPJ_TRUE;
6465 cp->info_on = OPJ_FALSE; /* no informative technique */
6468 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6469 cp->epb_on = OPJ_TRUE;
6471 cp->hprot_MH = parameters->jpwl_hprot_MH;
6472 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6473 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6474 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6476 /* if tile specs are not specified, copy MH specs */
6477 if (cp->hprot_TPH[0] == -1) {
6478 cp->hprot_TPH_tileno[0] = 0;
6479 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6481 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6482 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6483 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6484 cp->pprot[i] = parameters->jpwl_pprot[i];
6488 /* set ESD writing */
6489 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6490 cp->esd_on = OPJ_TRUE;
6492 cp->sens_size = parameters->jpwl_sens_size;
6493 cp->sens_addr = parameters->jpwl_sens_addr;
6494 cp->sens_range = parameters->jpwl_sens_range;
6496 cp->sens_MH = parameters->jpwl_sens_MH;
6497 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6498 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6499 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6503 /* always set RED writing to false: we are at the encoder */
6504 cp->red_on = OPJ_FALSE;
6507 cp->epc_on = OPJ_FALSE;
6509 #endif /* USE_JPWL */
6512 /* initialize the mutiple tiles */
6513 /* ---------------------------- */
6514 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6515 if (parameters->numpocs) {
6516 /* initialisation of POC */
6517 l_res = opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6521 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6522 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6523 tcp->numlayers = parameters->tcp_numlayers;
6525 for (j = 0; j < tcp->numlayers; j++) {
6526 if(cp->m_specific_param.m_enc.m_cinema){
6527 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6528 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6530 tcp->rates[j] = parameters->tcp_rates[j];
6532 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6533 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6535 tcp->rates[j] = parameters->tcp_rates[j];
6540 tcp->csty = parameters->csty;
6541 tcp->prg = parameters->prog_order;
6542 tcp->mct = parameters->tcp_mct;
6547 if (parameters->numpocs) {
6548 /* initialisation of POC */
6551 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6552 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6553 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6555 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6556 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6557 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6558 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6559 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6560 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6561 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6567 tcp->numpocs = numpocs_tile -1 ;
6572 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6574 if (parameters->mct_data) {
6576 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6579 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6580 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6581 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6582 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6585 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6586 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6587 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6589 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6590 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6592 tcp->mct_norms = (OPJ_FLOAT64*)
6593 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6595 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6598 for (i = 0; i < image->numcomps; i++) {
6599 opj_tccp_t *tccp = &tcp->tccps[i];
6600 tccp->m_dc_level_shift = l_dc_shift[i];
6603 opj_j2k_setup_mct_encoding(tcp,image);
6607 for (i = 0; i < image->numcomps; i++) {
6608 opj_tccp_t *tccp = &tcp->tccps[i];
6609 opj_image_comp_t * l_comp = &(image->comps[i]);
6611 if (! l_comp->sgnd) {
6612 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6617 for (i = 0; i < image->numcomps; i++) {
6618 opj_tccp_t *tccp = &tcp->tccps[i];
6620 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6621 tccp->numresolutions = parameters->numresolution;
6622 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6623 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6624 tccp->cblksty = parameters->mode;
6625 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6626 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6629 if (i == parameters->roi_compno) {
6630 tccp->roishift = parameters->roi_shift;
6635 if(parameters->cp_cinema) {
6636 //Precinct size for lowest frequency subband=128
6639 //Precinct size at all other resolutions = 256
6640 for (j = 1; j < tccp->numresolutions; j++) {
6645 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6646 OPJ_INT32 p = 0, it_res;
6647 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6648 if (p < parameters->res_spec) {
6650 if (parameters->prcw_init[p] < 1) {
6651 tccp->prcw[it_res] = 1;
6653 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6656 if (parameters->prch_init[p] < 1) {
6657 tccp->prch[it_res] = 1;
6659 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6663 int res_spec = parameters->res_spec;
6664 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6665 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6667 if (size_prcw < 1) {
6668 tccp->prcw[it_res] = 1;
6670 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6673 if (size_prch < 1) {
6674 tccp->prch[it_res] = 1;
6676 tccp->prch[it_res] = int_floorlog2(size_prch);
6680 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6683 for (j = 0; j < tccp->numresolutions; j++) {
6690 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6694 if (parameters->mct_data) {
6695 opj_free(parameters->mct_data);
6696 parameters->mct_data = 00;
6703 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6705 assert(cstr_info != 00);
6707 /* expand the list? */
6708 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
6709 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
6710 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
6713 /* add the marker */
6714 cstr_info->marker[cstr_info->marknum].type = type;
6715 cstr_info->marker[cstr_info->marknum].pos = pos;
6716 cstr_info->marker[cstr_info->marknum].len = len;
6717 cstr_info->marknum++;
6721 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6723 assert(cstr_index != 00);
6725 /* expand the list? */
6726 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6727 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6728 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6731 /* add the marker */
6732 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6733 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6734 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6735 cstr_index->marknum++;
6739 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6741 opj_marker_info_t *marker;
6743 assert(cstr_info != 00);
6745 /* expand the list? */
6746 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
6747 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
6748 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
6751 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
6753 /* add the marker */
6754 marker->type = type;
6757 cstr_info->tile[tileno].marknum++;
6760 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6762 assert(cstr_index != 00);
6763 assert(cstr_index->tile_index != 00);
6765 /* expand the list? */
6766 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6767 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6768 cstr_index->tile_index[tileno].marker =
6769 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
6770 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6773 /* add the marker */
6774 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6775 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6776 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6777 cstr_index->tile_index[tileno].marknum++;
6779 if (type == J2K_MS_SOT) {
6780 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6782 if (cstr_index->tile_index[tileno].tp_index)
6783 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6790 * -----------------------------------------------------------------------
6791 * -----------------------------------------------------------------------
6792 * -----------------------------------------------------------------------
6796 * Ends the decompression procedures and possibiliy add data to be read after the
6799 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
6800 opj_stream_private_t *p_stream,
6801 opj_event_mgr_t * p_manager
6811 * Reads a jpeg2000 codestream header structure.
6814 * @param p_stream the stream to read data from.
6815 * @param p_j2k the jpeg2000 codec.
6816 * @param p_manager the user event manager.
6818 * @return true if the box is valid.
6820 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
6821 opj_j2k_v2_t* p_j2k,
6822 opj_image_t** p_image,
6823 opj_event_mgr_t* p_manager )
6826 assert(p_j2k != 00);
6827 assert(p_stream != 00);
6828 assert(p_manager != 00);
6830 /* create an empty image header */
6831 p_j2k->m_private_image = opj_image_create0();
6832 if (! p_j2k->m_private_image) {
6836 /* customization of the validation */
6837 opj_j2k_setup_decoding_validation(p_j2k);
6839 /* validation of the parameters codec */
6840 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6841 opj_image_destroy(p_j2k->m_private_image);
6842 p_j2k->m_private_image = NULL;
6846 /* customization of the encoding */
6847 opj_j2k_setup_header_reading(p_j2k);
6850 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6851 opj_image_destroy(p_j2k->m_private_image);
6852 p_j2k->m_private_image = NULL;
6856 *p_image = opj_image_create0();
6861 /* Copy codestream image information to the output image */
6862 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6864 /*Allocate and initialize some elements of codestrem index*/
6865 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6873 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
6875 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
6878 assert(p_j2k != 00);
6880 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6882 /* DEVELOPER CORNER, add your custom procedures */
6883 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6888 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
6889 * are valid. Developpers wanting to extend the library can add their own validation procedures.
6891 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
6894 assert(p_j2k != 00);
6896 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6897 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6898 /* DEVELOPER CORNER, add your custom validation procedure */
6904 * The mct encoding validation procedure.
6906 * @param p_j2k the jpeg2000 codec to validate.
6907 * @param p_stream the input stream to validate.
6908 * @param p_manager the user event manager.
6910 * @return true if the parameters are correct.
6912 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
6913 opj_stream_private_t *p_stream,
6914 opj_event_mgr_t * p_manager )
6916 opj_bool l_is_valid = OPJ_TRUE;
6920 assert(p_j2k != 00);
6921 assert(p_stream != 00);
6922 assert(p_manager != 00);
6924 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6925 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6926 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
6928 for (i=0;i<l_nb_tiles;++i) {
6929 if (l_tcp->mct == 2) {
6930 opj_tccp_t * l_tccp = l_tcp->tccps;
6931 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6933 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
6934 l_is_valid &= ! (l_tccp->qmfbid & 1);
6945 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
6948 OPJ_UINT32 l_indix = 1;
6949 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
6950 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
6951 OPJ_UINT32 l_mct_size,l_nb_elem;
6952 OPJ_FLOAT32 * l_data, * l_current_data;
6953 opj_tccp_t * l_tccp;
6956 assert(p_tcp != 00);
6958 if (p_tcp->mct != 2) {
6962 if (p_tcp->m_mct_decoding_matrix) {
6963 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6964 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6966 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));
6967 if (! p_tcp->m_mct_records) {
6970 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6972 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6974 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6976 if (l_mct_deco_data->m_data) {
6977 opj_free(l_mct_deco_data->m_data);
6978 l_mct_deco_data->m_data = 00;
6981 l_mct_deco_data->m_index = l_indix++;
6982 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
6983 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
6984 l_nb_elem = p_image->numcomps * p_image->numcomps;
6985 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
6986 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6988 if (! l_mct_deco_data->m_data) {
6992 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);
6994 l_mct_deco_data->m_data_size = l_mct_size;
6995 ++p_tcp->m_nb_mct_records;
6998 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6999 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7000 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));
7002 if (! p_tcp->m_mct_records) {
7006 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7007 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7009 if (l_mct_deco_data) {
7010 l_mct_deco_data = l_mct_offset_data - 1;
7014 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7016 if (l_mct_offset_data->m_data) {
7017 opj_free(l_mct_offset_data->m_data);
7018 l_mct_offset_data->m_data = 00;
7021 l_mct_offset_data->m_index = l_indix++;
7022 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7023 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7024 l_nb_elem = p_image->numcomps;
7025 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7026 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7028 if (! l_mct_offset_data->m_data) {
7032 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7034 opj_free(l_mct_offset_data->m_data);
7035 l_mct_offset_data->m_data = 00;
7039 l_tccp = p_tcp->tccps;
7040 l_current_data = l_data;
7042 for (i=0;i<l_nb_elem;++i) {
7043 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7047 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7051 l_mct_offset_data->m_data_size = l_mct_size;
7053 ++p_tcp->m_nb_mct_records;
7055 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7056 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7057 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7058 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7060 if (! p_tcp->m_mcc_records) {
7063 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7064 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));
7068 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7069 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7070 l_mcc_data->m_is_irreversible = 1;
7071 l_mcc_data->m_nb_comps = p_image->numcomps;
7072 l_mcc_data->m_index = l_indix++;
7073 l_mcc_data->m_offset_array = l_mct_offset_data;
7074 ++p_tcp->m_nb_mcc_records;
7080 * Builds the cp decoder parameters to use to decode tile.
7082 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7083 opj_stream_private_t *p_stream,
7084 opj_event_mgr_t * p_manager )
7086 /* add here initialization of cp
7087 copy paste of setup_decoder */
7095 * Builds the cp encoder parameters to use to encode tile.
7097 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7098 opj_stream_private_t *p_stream,
7099 opj_event_mgr_t * p_manager )
7101 /* add here initialization of cp
7102 copy paste of setup_encoder */
7110 * The default encoding validation procedure without any extension.
7112 * @param p_j2k the jpeg2000 codec to validate.
7113 * @param p_stream the input stream to validate.
7114 * @param p_manager the user event manager.
7116 * @return true if the parameters are correct.
7118 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7119 opj_stream_private_t *p_stream,
7120 opj_event_mgr_t * p_manager )
7122 opj_bool l_is_valid = OPJ_TRUE;
7125 assert(p_j2k != 00);
7126 assert(p_stream != 00);
7127 assert(p_manager != 00);
7129 /* STATE checking */
7130 /* make sure the state is at 0 */
7131 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7133 /* POINTER validation */
7134 /* make sure a p_j2k codec is present */
7135 l_is_valid &= (p_j2k->m_procedure_list != 00);
7136 /* make sure a validation list is present */
7137 l_is_valid &= (p_j2k->m_validation_list != 00);
7139 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7140 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7144 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7145 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7149 /* PARAMETER VALIDATION */
7154 * The default decoding validation procedure without any extension.
7156 * @param p_j2k the jpeg2000 codec to validate.
7157 * @param p_stream the input stream to validate.
7158 * @param p_manager the user event manager.
7160 * @return true if the parameters are correct.
7162 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7163 opj_stream_private_t *p_stream,
7164 opj_event_mgr_t * p_manager
7167 opj_bool l_is_valid = OPJ_TRUE;
7170 assert(p_j2k != 00);
7171 assert(p_stream != 00);
7172 assert(p_manager != 00);
7175 /* STATE checking */
7176 /* make sure the state is at 0 */
7178 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7180 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7182 /* POINTER validation */
7183 /* make sure a p_j2k codec is present */
7184 /* make sure a procedure list is present */
7185 l_is_valid &= (p_j2k->m_procedure_list != 00);
7186 /* make sure a validation list is present */
7187 l_is_valid &= (p_j2k->m_validation_list != 00);
7189 /* PARAMETER VALIDATION */
7193 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7194 opj_stream_private_t *p_stream,
7195 opj_event_mgr_t * p_manager)
7197 OPJ_UINT32 l_current_marker;
7198 OPJ_UINT32 l_marker_size;
7199 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7202 assert(p_stream != 00);
7203 assert(p_j2k != 00);
7204 assert(p_manager != 00);
7206 /* We enter in the main header */
7207 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7209 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7210 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7211 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7215 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7216 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7217 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7221 /* Read 2 bytes as the new marker ID */
7222 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7224 /* Try to read until the SOT is detected */
7225 while (l_current_marker != J2K_MS_SOT) {
7227 /* Check if the current marker ID is valid */
7228 if (l_current_marker < 0xff00) {
7229 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7233 /* Get the marker handler from the marker ID */
7234 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7236 /* Manage case where marker is unknown */
7237 if (l_marker_handler->id == J2K_MS_UNK) {
7238 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7239 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7243 if (l_current_marker == J2K_MS_SOT)
7244 break; /* SOT marker is detected main header is completely read */
7245 else /* Get the marker handler from the marker ID */
7246 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7249 /* Check if the marker is known and if it is the right place to find it */
7250 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7251 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7255 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7256 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7257 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7261 /* read 2 bytes as the marker size */
7262 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7263 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7265 /* Check if the marker size is compatible with the header data size */
7266 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7267 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7268 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7269 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7272 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7275 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7276 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) {
7277 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7281 /* Read the marker segment with the correct marker handler */
7282 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7283 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7287 /* Add the marker to the codestream index*/
7288 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7289 l_marker_handler->id,
7290 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7291 l_marker_size + 4 );
7293 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7294 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7295 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7299 /* read 2 bytes as the new marker ID */
7300 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7303 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7305 /* Position of the last element if the main header */
7306 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7308 /* Next step: read a tile-part header */
7309 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7315 * Excutes the given procedures on the given codec.
7317 * @param p_procedure_list the list of procedures to execute
7318 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7319 * @param p_stream the stream to execute the procedures on.
7320 * @param p_manager the user manager.
7322 * @return true if all the procedures were successfully executed.
7324 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7325 opj_procedure_list_t * p_procedure_list,
7326 opj_stream_private_t *p_stream,
7327 opj_event_mgr_t * p_manager )
7329 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7330 opj_bool l_result = OPJ_TRUE;
7331 OPJ_UINT32 l_nb_proc, i;
7334 assert(p_procedure_list != 00);
7335 assert(p_j2k != 00);
7336 assert(p_stream != 00);
7337 assert(p_manager != 00);
7340 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7341 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);
7343 for (i=0;i<l_nb_proc;++i) {
7344 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7348 /* and clear the procedure list at the end.*/
7349 opj_procedure_list_clear(p_procedure_list);
7354 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7355 opj_stream_private_t *p_stream,
7356 opj_event_mgr_t * p_manager
7359 opj_tcp_v2_t * l_tcp = 00;
7360 opj_tcp_v2_t * l_default_tcp = 00;
7361 OPJ_UINT32 l_nb_tiles;
7363 opj_tccp_t *l_current_tccp = 00;
7364 OPJ_UINT32 l_tccp_size;
7365 OPJ_UINT32 l_mct_size;
7366 opj_image_t * l_image;
7367 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7368 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7369 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7370 OPJ_UINT32 l_offset;
7373 assert(p_j2k != 00);
7374 assert(p_stream != 00);
7375 assert(p_manager != 00);
7377 l_image = p_j2k->m_private_image;
7378 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7379 l_tcp = p_j2k->m_cp.tcps;
7380 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7381 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7382 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7385 for (i=0; i<l_nb_tiles; ++i) {
7386 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7387 l_current_tccp = l_tcp->tccps;
7388 /*Copy default coding parameters into the current tile coding parameters*/
7389 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7390 /* Initialize some values of the current tile coding parameters*/
7392 l_tcp->ppt_data = 00;
7393 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7394 l_tcp->tccps = l_current_tccp;
7396 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7397 if (l_default_tcp->m_mct_decoding_matrix) {
7398 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7399 if (! l_tcp->m_mct_decoding_matrix ) {
7402 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7405 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7406 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7407 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7408 if (! l_tcp->m_mct_records) {
7411 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7413 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7414 l_src_mct_rec = l_default_tcp->m_mct_records;
7415 l_dest_mct_rec = l_tcp->m_mct_records;
7417 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7419 if (l_src_mct_rec->m_data) {
7421 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7422 if(! l_dest_mct_rec->m_data) {
7425 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7432 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7433 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7434 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7435 if (! l_tcp->m_mcc_records) {
7438 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7440 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7441 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7442 l_dest_mcc_rec = l_tcp->m_mcc_records;
7444 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7446 if (l_src_mcc_rec->m_decorrelation_array) {
7447 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7448 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7451 if (l_src_mcc_rec->m_offset_array) {
7452 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7453 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7460 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7461 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7463 /* Move to next tile cp*/
7467 /* Create the current tile decoder*/
7468 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7469 if (! p_j2k->m_tcd ) {
7473 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7474 tcd_destroy_v2(p_j2k->m_tcd);
7476 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7484 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7485 * with the marker value.
7486 * @param p_id Marker value to look up
7488 * @return the handler associated with the id.
7490 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7492 const opj_dec_memory_marker_handler_t *e;
7493 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7494 if (e->id == p_id) {
7495 break; /* we find a handler corresponding to the marker ID*/
7503 * Destroys a jpeg2000 codec.
7505 * @param p_j2k the jpeg20000 structure to destroy.
7507 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7513 if (p_j2k->m_is_decoder) {
7515 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7516 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7517 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7518 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7521 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7522 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7523 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7524 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7529 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7530 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7531 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7534 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7535 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7536 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7537 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7540 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7541 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7542 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7543 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7547 tcd_destroy_v2(p_j2k->m_tcd);
7549 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7550 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7552 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7553 p_j2k->m_procedure_list = 00;
7555 opj_procedure_list_destroy(p_j2k->m_validation_list);
7556 p_j2k->m_procedure_list = 00;
7558 j2k_destroy_cstr_index(p_j2k->cstr_index);
7559 p_j2k->cstr_index = NULL;
7561 opj_image_destroy(p_j2k->m_private_image);
7562 p_j2k->m_private_image = NULL;
7564 opj_image_destroy(p_j2k->m_output_image);
7565 p_j2k->m_output_image = NULL;
7570 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7574 if (p_cstr_ind->marker) {
7575 opj_free(p_cstr_ind->marker);
7576 p_cstr_ind->marker = NULL;
7579 if (p_cstr_ind->tile_index) {
7580 OPJ_UINT32 it_tile = 0;
7582 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7584 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7585 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7586 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7589 if(p_cstr_ind->tile_index[it_tile].tp_index){
7590 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7591 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7594 if(p_cstr_ind->tile_index[it_tile].marker){
7595 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7596 p_cstr_ind->tile_index[it_tile].marker = NULL;
7601 opj_free( p_cstr_ind->tile_index);
7602 p_cstr_ind->tile_index = NULL;
7605 opj_free(p_cstr_ind);
7612 * Destroys a tile coding parameter structure.
7614 * @param p_tcp the tile coding parameter to destroy.
7616 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7622 if (p_tcp->ppt_buffer != 00) {
7623 opj_free(p_tcp->ppt_buffer);
7624 p_tcp->ppt_buffer = 00;
7627 if (p_tcp->tccps != 00) {
7628 opj_free(p_tcp->tccps);
7632 if (p_tcp->m_mct_coding_matrix != 00) {
7633 opj_free(p_tcp->m_mct_coding_matrix);
7634 p_tcp->m_mct_coding_matrix = 00;
7637 if (p_tcp->m_mct_decoding_matrix != 00) {
7638 opj_free(p_tcp->m_mct_decoding_matrix);
7639 p_tcp->m_mct_decoding_matrix = 00;
7642 if (p_tcp->m_mcc_records) {
7643 opj_free(p_tcp->m_mcc_records);
7644 p_tcp->m_mcc_records = 00;
7645 p_tcp->m_nb_max_mcc_records = 0;
7646 p_tcp->m_nb_mcc_records = 0;
7649 if (p_tcp->m_mct_records) {
7650 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7653 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7654 if (l_mct_data->m_data) {
7655 opj_free(l_mct_data->m_data);
7656 l_mct_data->m_data = 00;
7662 opj_free(p_tcp->m_mct_records);
7663 p_tcp->m_mct_records = 00;
7666 if (p_tcp->mct_norms != 00) {
7667 opj_free(p_tcp->mct_norms);
7668 p_tcp->mct_norms = 00;
7671 opj_j2k_tcp_data_destroy(p_tcp);
7676 * Destroys the data inside a tile coding parameter structure.
7678 * @param p_tcp the tile coding parameter which contain data to destroy.
7680 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7682 if (p_tcp->m_data) {
7683 opj_free(p_tcp->m_data);
7684 p_tcp->m_data = NULL;
7685 p_tcp->m_data_size = 0;
7690 * Destroys a coding parameter structure.
7692 * @param p_cp the coding parameter to destroy.
7694 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7696 OPJ_UINT32 l_nb_tiles;
7697 opj_tcp_v2_t * l_current_tile = 00;
7708 l_current_tile = p_cp->tcps;
7709 l_nb_tiles = p_cp->th * p_cp->tw;
7712 (i = 0; i < l_nb_tiles; ++i)
7714 opj_j2k_tcp_destroy(l_current_tile);
7717 opj_free(p_cp->tcps);
7721 (p_cp->ppm_buffer != 00)
7723 opj_free(p_cp->ppm_buffer);
7724 p_cp->ppm_buffer = 00;
7727 (p_cp->comment != 00)
7729 opj_free(p_cp->comment);
7733 (! p_cp->m_is_decoder)
7736 (p_cp->m_specific_param.m_enc.m_matrice)
7738 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7739 p_cp->m_specific_param.m_enc.m_matrice = 00;
7747 * Reads a tile header.
7748 * @param p_j2k the jpeg2000 codec.
7749 * @param p_stream the stream to write data to.
7750 * @param p_manager the user event manager.
7752 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7753 OPJ_UINT32 * p_tile_index,
7754 OPJ_UINT32 * p_data_size,
7755 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7756 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7757 OPJ_UINT32 * p_nb_comps,
7759 opj_stream_private_t *p_stream,
7760 opj_event_mgr_t * p_manager )
7762 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7763 OPJ_UINT32 l_marker_size;
7764 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7765 opj_tcp_v2_t * l_tcp = NULL;
7766 OPJ_UINT32 l_nb_tiles;
7769 assert(p_stream != 00);
7770 assert(p_j2k != 00);
7771 assert(p_manager != 00);
7773 /* Reach the End Of Codestream ?*/
7774 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7775 l_current_marker = J2K_MS_EOC;
7777 /* We need to encounter a SOT marker (a new tile-part header) */
7778 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7782 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7783 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7785 /* Try to read until the Start Of Data is detected */
7786 while (l_current_marker != J2K_MS_SOD) {
7788 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7789 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7790 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7794 /* Read 2 bytes from the buffer as the marker size */
7795 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7797 /* Why this condition? FIXME */
7798 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7799 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7801 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7803 /* Get the marker handler from the marker ID */
7804 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7806 /* Check if the marker is known and if it is the right place to find it */
7807 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7808 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7811 /* FIXME manage case of unknown marker as in the main header ? */
7813 /* Check if the marker size is compatible with the header data size */
7814 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7815 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7816 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7817 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7820 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7823 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7824 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) {
7825 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7829 /* Read the marker segment with the correct marker handler */
7830 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7831 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7835 /* Add the marker to the codestream index*/
7836 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
7838 l_marker_handler->id,
7839 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7840 l_marker_size + 4 );
7842 /* Keep the position of the last SOT marker read */
7843 if ( l_marker_handler->id == J2K_MS_SOT ) {
7844 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7845 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7847 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7852 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7853 /* Skip the rest of the tile part header*/
7854 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) {
7855 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7858 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7861 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7862 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7863 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7866 /* Read 2 bytes from the buffer as the new marker ID */
7867 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7871 /* If we didn't skip data before, we need to read the SOD marker*/
7872 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7873 /* Try to read the SOD marker and skip data ? FIXME */
7874 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7880 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7881 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7882 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7883 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7887 /* Read 2 bytes from buffer as the new marker ID */
7888 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7892 /* Indicate we will try to read a new tile-part header*/
7893 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7894 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7895 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7897 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7898 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7899 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7903 /* Read 2 bytes from buffer as the new marker ID */
7904 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7908 /* Current marker is the EOC marker ?*/
7909 if (l_current_marker == J2K_MS_EOC) {
7910 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7911 p_j2k->m_current_tile_number = 0;
7912 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
7917 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
7918 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
7919 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7921 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
7922 ++p_j2k->m_current_tile_number;
7926 if (p_j2k->m_current_tile_number == l_nb_tiles) {
7927 *p_go_on = OPJ_FALSE;
7933 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
7934 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7938 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
7939 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
7941 *p_tile_index = p_j2k->m_current_tile_number;
7942 *p_go_on = OPJ_TRUE;
7943 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
7944 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
7945 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
7946 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
7947 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
7948 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
7950 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
7956 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
7957 OPJ_UINT32 p_tile_index,
7959 OPJ_UINT32 p_data_size,
7960 opj_stream_private_t *p_stream,
7961 opj_event_mgr_t * p_manager )
7963 OPJ_UINT32 l_current_marker;
7964 OPJ_BYTE l_data [2];
7965 opj_tcp_v2_t * l_tcp;
7968 assert(p_stream != 00);
7969 assert(p_j2k != 00);
7970 assert(p_manager != 00);
7972 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
7973 || (p_tile_index != p_j2k->m_current_tile_number) ) {
7977 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
7978 if (! l_tcp->m_data) {
7979 opj_j2k_tcp_destroy(l_tcp);
7983 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
7987 p_j2k->cstr_index) ) {
7988 opj_j2k_tcp_destroy(l_tcp);
7989 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
7993 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
7997 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
7998 * we destroy just the data which will be re-read in read_tile_header*/
7999 /*opj_j2k_tcp_destroy(l_tcp);
8000 p_j2k->m_tcd->tcp = 0;*/
8001 opj_j2k_tcp_data_destroy(l_tcp);
8003 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8004 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8006 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8007 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8008 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8012 opj_read_bytes(l_data,&l_current_marker,2);
8014 if (l_current_marker == J2K_MS_EOC) {
8015 p_j2k->m_current_tile_number = 0;
8016 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8018 else if (l_current_marker != J2K_MS_SOT)
8020 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8029 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8031 OPJ_UINT32 i,j,k = 0;
8032 OPJ_UINT32 l_width_src,l_height_src;
8033 OPJ_UINT32 l_width_dest,l_height_dest;
8034 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8035 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8036 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8037 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8038 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8040 opj_image_comp_t * l_img_comp_src = 00;
8041 opj_image_comp_t * l_img_comp_dest = 00;
8043 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8044 opj_image_t * l_image_src = 00;
8045 OPJ_UINT32 l_size_comp, l_remaining;
8046 OPJ_INT32 * l_dest_ptr;
8047 opj_tcd_resolution_v2_t* l_res= 00;
8049 l_tilec = p_tcd->tcd_image->tiles->comps;
8050 l_image_src = p_tcd->image;
8051 l_img_comp_src = l_image_src->comps;
8053 l_img_comp_dest = p_output_image->comps;
8055 for (i=0; i<l_image_src->numcomps; i++) {
8057 /* Allocate output component buffer if necessary */
8058 if (!l_img_comp_dest->data) {
8060 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8061 if (! l_img_comp_dest->data) {
8066 /* Copy info from decoded comp image to output image */
8067 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8070 /* Compute the precision of the output buffer */
8071 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8072 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8073 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8079 if (l_size_comp == 3) {
8084 /* Current tile component size*/
8086 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8087 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8090 l_width_src = (l_res->x1 - l_res->x0);
8091 l_height_src = (l_res->y1 - l_res->y0);
8093 /* Border of the current output component*/
8094 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8095 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8096 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8097 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8100 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8101 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8105 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8106 * of the input buffer (decoded tile component) which will be move
8107 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8108 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8109 * by this input area.
8111 assert( l_res->x0 >= 0);
8112 assert( l_res->x1 >= 0);
8113 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8114 l_start_x_dest = l_res->x0 - l_x0_dest;
8115 l_offset_x0_src = 0;
8117 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8118 l_width_dest = l_width_src;
8119 l_offset_x1_src = 0;
8122 l_width_dest = l_x1_dest - l_res->x0 ;
8123 l_offset_x1_src = l_width_src - l_width_dest;
8127 l_start_x_dest = 0 ;
8128 l_offset_x0_src = l_x0_dest - l_res->x0;
8130 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8131 l_width_dest = l_width_src - l_offset_x0_src;
8132 l_offset_x1_src = 0;
8135 l_width_dest = l_img_comp_dest->w ;
8136 l_offset_x1_src = l_res->x1 - l_x1_dest;
8140 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8141 l_start_y_dest = l_res->y0 - l_y0_dest;
8142 l_offset_y0_src = 0;
8144 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8145 l_height_dest = l_height_src;
8146 l_offset_y1_src = 0;
8149 l_height_dest = l_y1_dest - l_res->y0 ;
8150 l_offset_y1_src = l_height_src - l_height_dest;
8154 l_start_y_dest = 0 ;
8155 l_offset_y0_src = l_y0_dest - l_res->y0;
8157 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8158 l_height_dest = l_height_src - l_offset_y0_src;
8159 l_offset_y1_src = 0;
8162 l_height_dest = l_img_comp_dest->h ;
8163 l_offset_y1_src = l_res->y1 - l_y1_dest;
8167 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8172 /* Compute the input buffer offset */
8173 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8174 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8175 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8177 /* Compute the output buffer offset */
8178 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8179 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8181 /* Move the output buffer to the first place where we will write*/
8182 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8185 fprintf(stdout, "COMPO[%d]:\n",i);
8186 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8187 "\t tile offset:%d, %d, %d, %d\n"
8188 "\t buffer offset: %d; %d, %d\n",
8189 l_res->x0, l_res->y0, l_width_src, l_height_src,
8190 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8191 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8193 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8194 "\t start offset: %d, line offset= %d\n",
8195 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8199 switch (l_size_comp) {
8202 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8203 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8205 if (l_img_comp_src->sgnd) {
8206 for (j = 0 ; j < l_height_dest ; ++j) {
8207 for ( k = 0 ; k < l_width_dest ; ++k) {
8208 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8211 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8212 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8216 for ( j = 0 ; j < l_height_dest ; ++j ) {
8217 for ( k = 0 ; k < l_width_dest ; ++k) {
8218 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8221 l_dest_ptr+= l_line_offset_dest;
8222 l_src_ptr += l_line_offset_src;
8226 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8227 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8232 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8233 l_src_ptr += l_start_offset_src;
8235 if (l_img_comp_src->sgnd) {
8236 for (j=0;j<l_height_dest;++j) {
8237 for (k=0;k<l_width_dest;++k) {
8238 *(l_dest_ptr++) = *(l_src_ptr++);
8241 l_dest_ptr+= l_line_offset_dest;
8242 l_src_ptr += l_line_offset_src ;
8246 for (j=0;j<l_height_dest;++j) {
8247 for (k=0;k<l_width_dest;++k) {
8248 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8251 l_dest_ptr+= l_line_offset_dest;
8252 l_src_ptr += l_line_offset_src ;
8256 l_src_ptr += l_end_offset_src;
8257 p_data = (OPJ_BYTE*) l_src_ptr;
8262 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8263 l_src_ptr += l_start_offset_src;
8265 for (j=0;j<l_height_dest;++j) {
8266 for (k=0;k<l_width_dest;++k) {
8267 *(l_dest_ptr++) = (*(l_src_ptr++));
8270 l_dest_ptr+= l_line_offset_dest;
8271 l_src_ptr += l_line_offset_src ;
8274 l_src_ptr += l_end_offset_src;
8275 p_data = (OPJ_BYTE*) l_src_ptr;
8289 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8291 * @param p_j2k the jpeg2000 codec.
8292 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8293 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8294 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8295 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8296 * @param p_manager the user event manager
8298 * @return true if the area could be set.
8300 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8301 opj_image_t* p_image,
8302 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8303 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8304 opj_event_mgr_t * p_manager )
8306 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8307 opj_image_t * l_image = p_j2k->m_private_image;
8310 OPJ_INT32 l_comp_x1, l_comp_y1;
8311 opj_image_comp_t* l_img_comp = NULL;
8313 /* Check if we are read the main header */
8314 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8315 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8319 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8320 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8322 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8323 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8324 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8325 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8331 /* Check if the positions provided by the user are correct */
8334 assert(p_start_x >= 0 );
8335 assert(p_start_y >= 0 );
8337 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8338 opj_event_msg_v2(p_manager, EVT_ERROR,
8339 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8340 p_start_x, l_image->x1);
8343 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8344 opj_event_msg_v2(p_manager, EVT_WARNING,
8345 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8346 p_start_x, l_image->x0);
8347 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8348 p_image->x0 = l_image->x0;
8351 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8352 p_image->x0 = p_start_x;
8356 if ((OPJ_UINT32)p_start_y > l_image->y1){
8357 opj_event_msg_v2(p_manager, EVT_ERROR,
8358 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8359 p_start_y, l_image->y1);
8362 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8363 opj_event_msg_v2(p_manager, EVT_WARNING,
8364 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8365 p_start_y, l_image->y0);
8366 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8367 p_image->y0 = l_image->y0;
8370 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8371 p_image->y0 = p_start_y;
8375 assert((OPJ_UINT32)p_end_x > 0);
8376 assert((OPJ_UINT32)p_end_y > 0);
8377 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8378 opj_event_msg_v2(p_manager, EVT_ERROR,
8379 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8380 p_end_x, l_image->x0);
8383 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8384 opj_event_msg_v2(p_manager, EVT_WARNING,
8385 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8386 p_end_x, l_image->x1);
8387 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8388 p_image->x1 = l_image->x1;
8391 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8392 p_image->x1 = p_end_x;
8396 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8397 opj_event_msg_v2(p_manager, EVT_ERROR,
8398 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8399 p_end_y, l_image->y0);
8402 if ((OPJ_UINT32)p_end_y > l_image->y1){
8403 opj_event_msg_v2(p_manager, EVT_WARNING,
8404 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8405 p_end_y, l_image->y1);
8406 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8407 p_image->y1 = l_image->y1;
8410 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8411 p_image->y1 = p_end_y;
8415 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8417 l_img_comp = p_image->comps;
8418 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8422 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8423 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8424 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8425 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8427 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8428 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8430 opj_event_msg_v2(p_manager, EVT_ERROR,
8431 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8435 l_img_comp->w = l_w;
8437 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8438 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8440 opj_event_msg_v2(p_manager, EVT_ERROR,
8441 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8445 l_img_comp->h = l_h;
8450 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8451 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8458 /* ----------------------------------------------------------------------- */
8459 /* J2K / JPT decoder interface */
8460 /* ----------------------------------------------------------------------- */
8462 * Creates a J2K decompression structure.
8464 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8466 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8468 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8472 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8474 l_j2k->m_is_decoder = 1;
8475 l_j2k->m_cp.m_is_decoder = 1;
8477 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8478 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8479 opj_j2k_destroy(l_j2k);
8482 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8484 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8485 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8486 opj_j2k_destroy(l_j2k);
8490 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8492 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8494 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8496 /* codestream index creation */
8497 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8499 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8500 if (!l_j2k->cstr_index){
8501 opj_j2k_destroy(l_j2k);
8505 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8508 /* validation list creation */
8509 l_j2k->m_validation_list = opj_procedure_list_create();
8510 if (! l_j2k->m_validation_list) {
8511 opj_j2k_destroy(l_j2k);
8515 /* execution list creation */
8516 l_j2k->m_procedure_list = opj_procedure_list_create();
8517 if (! l_j2k->m_procedure_list) {
8518 opj_j2k_destroy(l_j2k);
8526 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8528 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8529 opj_calloc(1,sizeof(opj_codestream_index_t));
8533 cstr_index->maxmarknum = 100;
8534 cstr_index->marknum = 0;
8535 cstr_index->marker = (opj_marker_info_t*)
8536 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8537 if (!cstr_index-> marker)
8540 cstr_index->tile_index = NULL;
8547 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8549 * @param p_tile_no the tile index.
8550 * @param p_comp_no the component being outputted.
8551 * @param p_j2k the J2K codec.
8553 * @return the number of bytes taken by the SPCod element.
8555 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8556 OPJ_UINT32 p_tile_no,
8557 OPJ_UINT32 p_comp_no )
8559 opj_cp_v2_t *l_cp = 00;
8560 opj_tcp_v2_t *l_tcp = 00;
8561 opj_tccp_t *l_tccp = 00;
8564 assert(p_j2k != 00);
8566 l_cp = &(p_j2k->m_cp);
8567 l_tcp = &l_cp->tcps[p_tile_no];
8568 l_tccp = &l_tcp->tccps[p_comp_no];
8570 /* preconditions again */
8571 assert(p_tile_no < (l_cp->tw * l_cp->th));
8572 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8574 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8575 return 5 + l_tccp->numresolutions;
8583 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8585 * @param p_comp_no the component number to output.
8586 * @param p_stream the stream to write data to.
8587 * @param p_j2k J2K codec.
8588 * @param p_manager the user event manager.
8591 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8592 OPJ_UINT32 p_tile_no,
8593 OPJ_UINT32 p_comp_no,
8595 OPJ_UINT32 * p_header_size,
8596 struct opj_event_mgr * p_manager )
8599 opj_cp_v2_t *l_cp = 00;
8600 opj_tcp_v2_t *l_tcp = 00;
8601 opj_tccp_t *l_tccp = 00;
8604 assert(p_j2k != 00);
8605 assert(p_header_size != 00);
8606 assert(p_manager != 00);
8607 assert(p_data != 00);
8609 l_cp = &(p_j2k->m_cp);
8610 l_tcp = &l_cp->tcps[p_tile_no];
8611 l_tccp = &l_tcp->tccps[p_comp_no];
8613 /* preconditions again */
8614 assert(p_tile_no < (l_cp->tw * l_cp->th));
8615 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8617 if (*p_header_size < 5) {
8618 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8622 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8625 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8628 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8631 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8634 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8637 *p_header_size = *p_header_size - 5;
8639 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8641 if (*p_header_size < l_tccp->numresolutions) {
8642 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8646 for (i = 0; i < l_tccp->numresolutions; ++i) {
8647 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8651 *p_header_size = *p_header_size - l_tccp->numresolutions;
8658 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8659 * @param p_header_data the data contained in the COM box.
8660 * @param p_j2k the jpeg2000 codec.
8661 * @param p_header_size the size of the data contained in the COM marker.
8662 * @param p_manager the user event manager.
8664 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8666 OPJ_BYTE * p_header_data,
8667 OPJ_UINT32 * p_header_size,
8668 opj_event_mgr_t * p_manager)
8670 OPJ_UINT32 i, l_tmp;
8671 opj_cp_v2_t *l_cp = NULL;
8672 opj_tcp_v2_t *l_tcp = NULL;
8673 opj_tccp_t *l_tccp = NULL;
8674 OPJ_BYTE * l_current_ptr = NULL;
8677 assert(p_j2k != 00);
8678 assert(p_manager != 00);
8679 assert(p_header_data != 00);
8681 l_cp = &(p_j2k->m_cp);
8682 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8683 &l_cp->tcps[p_j2k->m_current_tile_number] :
8684 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8686 /* precondition again */
8687 assert(compno < p_j2k->m_private_image->numcomps);
8689 l_tccp = &l_tcp->tccps[compno];
8690 l_current_ptr = p_header_data;
8692 /* make sure room is sufficient */
8693 if (*p_header_size < 5) {
8694 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8698 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8699 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8702 /* If user wants to remove more resolutions than the codestream contains, return error */
8703 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8704 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8705 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8706 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8710 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8714 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8718 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8721 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8724 *p_header_size = *p_header_size - 5;
8726 /* use custom precinct size ? */
8727 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8728 if (*p_header_size < l_tccp->numresolutions) {
8729 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8733 for (i = 0; i < l_tccp->numresolutions; ++i) {
8734 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8736 l_tccp->prcw[i] = l_tmp & 0xf;
8737 l_tccp->prch[i] = l_tmp >> 4;
8740 *p_header_size = *p_header_size - l_tccp->numresolutions;
8743 /* set default size for the precinct width and height */
8744 for (i = 0; i < l_tccp->numresolutions; ++i) {
8745 l_tccp->prcw[i] = 15;
8746 l_tccp->prch[i] = 15;
8750 #ifdef WIP_REMOVE_MSD
8752 if (p_j2k->cstr_info && compno == 0) {
8753 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8755 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8756 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8757 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8758 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8759 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8762 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8763 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8772 * Copies the tile component parameters of all the component from the first tile component.
8774 * @param p_j2k the J2k codec.
8776 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8780 opj_cp_v2_t *l_cp = NULL;
8781 opj_tcp_v2_t *l_tcp = NULL;
8782 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8783 OPJ_UINT32 l_prc_size;
8786 assert(p_j2k != 00);
8788 l_cp = &(p_j2k->m_cp);
8789 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8790 &l_cp->tcps[p_j2k->m_current_tile_number] :
8791 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8793 l_ref_tccp = &l_tcp->tccps[0];
8794 l_copied_tccp = l_ref_tccp + 1;
8795 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8797 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8798 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8799 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8800 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8801 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8802 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8803 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8804 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8810 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
8812 * @param p_tile_no the tile index.
8813 * @param p_comp_no the component being outputted.
8814 * @param p_j2k the J2K codec.
8816 * @return the number of bytes taken by the SPCod element.
8818 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
8819 OPJ_UINT32 p_tile_no,
8820 OPJ_UINT32 p_comp_no )
8822 OPJ_UINT32 l_num_bands;
8824 opj_cp_v2_t *l_cp = 00;
8825 opj_tcp_v2_t *l_tcp = 00;
8826 opj_tccp_t *l_tccp = 00;
8829 assert(p_j2k != 00);
8831 l_cp = &(p_j2k->m_cp);
8832 l_tcp = &l_cp->tcps[p_tile_no];
8833 l_tccp = &l_tcp->tccps[p_comp_no];
8835 /* preconditions again */
8836 assert(p_tile_no < l_cp->tw * l_cp->th);
8837 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8839 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8841 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8842 return 1 + l_num_bands;
8845 return 1 + 2*l_num_bands;
8850 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
8852 * @param p_tile_no the tile to output.
8853 * @param p_comp_no the component number to output.
8854 * @param p_data the data buffer.
8855 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
8856 * @param p_j2k J2K codec.
8857 * @param p_manager the user event manager.
8860 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
8861 OPJ_UINT32 p_tile_no,
8862 OPJ_UINT32 p_comp_no,
8864 OPJ_UINT32 * p_header_size,
8865 struct opj_event_mgr * p_manager )
8867 OPJ_UINT32 l_header_size;
8868 OPJ_UINT32 l_band_no, l_num_bands;
8869 OPJ_UINT32 l_expn,l_mant;
8871 opj_cp_v2_t *l_cp = 00;
8872 opj_tcp_v2_t *l_tcp = 00;
8873 opj_tccp_t *l_tccp = 00;
8876 assert(p_j2k != 00);
8877 assert(p_header_size != 00);
8878 assert(p_manager != 00);
8879 assert(p_data != 00);
8881 l_cp = &(p_j2k->m_cp);
8882 l_tcp = &l_cp->tcps[p_tile_no];
8883 l_tccp = &l_tcp->tccps[p_comp_no];
8885 /* preconditions again */
8886 assert(p_tile_no < l_cp->tw * l_cp->th);
8887 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8889 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8891 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8892 l_header_size = 1 + l_num_bands;
8894 if (*p_header_size < l_header_size) {
8895 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8899 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8902 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8903 l_expn = l_tccp->stepsizes[l_band_no].expn;
8904 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8909 l_header_size = 1 + 2*l_num_bands;
8911 if (*p_header_size < l_header_size) {
8912 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8916 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8919 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8920 l_expn = l_tccp->stepsizes[l_band_no].expn;
8921 l_mant = l_tccp->stepsizes[l_band_no].mant;
8923 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
8928 *p_header_size = *p_header_size - l_header_size;
8934 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
8936 * @param p_comp_no the component being targeted.
8937 * @param p_header_data the data contained in the COM box.
8938 * @param p_j2k the jpeg2000 codec.
8939 * @param p_header_size the size of the data contained in the COM marker.
8940 * @param p_manager the user event manager.
8942 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
8943 OPJ_UINT32 p_comp_no,
8944 OPJ_BYTE* p_header_data,
8945 OPJ_UINT32 * p_header_size,
8946 opj_event_mgr_t * p_manager
8950 OPJ_UINT32 l_band_no;
8951 opj_cp_v2_t *l_cp = 00;
8952 opj_tcp_v2_t *l_tcp = 00;
8953 opj_tccp_t *l_tccp = 00;
8954 OPJ_BYTE * l_current_ptr = 00;
8955 OPJ_UINT32 l_tmp, l_num_band;
8958 assert(p_j2k != 00);
8959 assert(p_manager != 00);
8960 assert(p_header_data != 00);
8962 l_cp = &(p_j2k->m_cp);
8963 /* come from tile part header or main header ?*/
8964 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
8965 &l_cp->tcps[p_j2k->m_current_tile_number] :
8966 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8968 /* precondition again*/
8969 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8971 l_tccp = &l_tcp->tccps[p_comp_no];
8972 l_current_ptr = p_header_data;
8974 if (*p_header_size < 1) {
8975 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
8978 *p_header_size -= 1;
8980 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
8983 l_tccp->qntsty = l_tmp & 0x1f;
8984 l_tccp->numgbits = l_tmp >> 5;
8985 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8989 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
8991 (*p_header_size) / 2;
8993 if( l_num_band > J2K_MAXBANDS ) {
8994 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
8995 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
8996 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
8997 /*return OPJ_FALSE;*/
9002 if (l_cp->correct) {
9004 /* if JPWL is on, we check whether there are too many subbands */
9005 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9006 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9007 "JPWL: bad number of subbands in Sqcx (%d)\n",
9010 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9013 /* we try to correct */
9015 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9016 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9021 #endif /* USE_JPWL */
9023 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9024 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9025 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9027 if (l_band_no < J2K_MAXBANDS){
9028 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9029 l_tccp->stepsizes[l_band_no].mant = 0;
9032 *p_header_size = *p_header_size - l_num_band;
9035 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9036 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9038 if (l_band_no < J2K_MAXBANDS){
9039 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9040 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9043 *p_header_size = *p_header_size - 2*l_num_band;
9046 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9047 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9048 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9049 l_tccp->stepsizes[l_band_no].expn =
9050 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9051 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9052 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9060 * Copies the tile component parameters of all the component from the first tile component.
9062 * @param p_j2k the J2k codec.
9064 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9067 opj_cp_v2_t *l_cp = NULL;
9068 opj_tcp_v2_t *l_tcp = NULL;
9069 opj_tccp_t *l_ref_tccp = NULL;
9070 opj_tccp_t *l_copied_tccp = NULL;
9074 assert(p_j2k != 00);
9076 l_cp = &(p_j2k->m_cp);
9077 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9078 &l_cp->tcps[p_j2k->m_current_tile_number] :
9079 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9081 l_ref_tccp = &l_tcp->tccps[0];
9082 l_copied_tccp = l_ref_tccp + 1;
9083 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9085 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9086 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9087 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9088 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9094 * Dump some elements from the J2K decompression structure .
9096 *@param p_j2k the jpeg2000 codec.
9097 *@param flag flag to describe what elments are dump.
9098 *@param out_stream output stream where dump the elements.
9101 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9103 /* Check if the flag is compatible with j2k file*/
9104 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9105 fprintf(out_stream, "Wrong flag\n");
9109 /* Dump the image_header */
9110 if (flag & OPJ_IMG_INFO){
9111 if (p_j2k->m_private_image)
9112 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9115 /* Dump the codestream info from main header */
9116 if (flag & OPJ_J2K_MH_INFO){
9117 opj_j2k_dump_MH_info(p_j2k, out_stream);
9121 /* Dump the codestream info of the current tile */
9122 if (flag & OPJ_J2K_TH_INFO){
9126 /* Dump the codestream index from main header */
9127 if (flag & OPJ_J2K_MH_IND){
9128 opj_j2k_dump_MH_index(p_j2k, out_stream);
9131 /* Dump the codestream index of the current tile */
9132 if (flag & OPJ_J2K_TH_IND){
9139 * Dump index elements of the codestream extract from the main header.
9141 *@param p_j2k the jpeg2000 codec.
9142 *@param out_stream output stream where dump the elements.
9145 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9147 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9148 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9150 fprintf(out_stream, "Codestream index from main header: {\n");
9152 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9153 "\t Main header end position=%" PRIi64 "\n",
9154 cstr_index->main_head_start, cstr_index->main_head_end);
9156 fprintf(out_stream, "\t Marker list: {\n");
9158 if (cstr_index->marker){
9159 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9160 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9161 cstr_index->marker[it_marker].type,
9162 cstr_index->marker[it_marker].pos,
9163 cstr_index->marker[it_marker].len );
9167 fprintf(out_stream, "\t }\n");
9170 if (cstr_index->tile_index){
9172 /* Simple test to avoid to write empty information*/
9173 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9174 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9175 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9178 if (l_acc_nb_of_tile_part)
9180 fprintf(out_stream, "\t Tile index: {\n");
9182 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9183 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9185 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9187 if (cstr_index->tile_index[it_tile].tp_index){
9188 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9189 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9191 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9192 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9193 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9197 if (cstr_index->tile_index[it_tile].marker){
9198 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9199 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9200 cstr_index->tile_index[it_tile].marker[it_marker].type,
9201 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9202 cstr_index->tile_index[it_tile].marker[it_marker].len );
9206 fprintf(out_stream,"\t }\n");
9210 fprintf(out_stream,"}\n");
9215 * Dump info elements of the codestream extract from the main header.
9217 *@param p_j2k the jpeg2000 codec.
9218 *@param out_stream output stream where dump the elements.
9221 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9223 opj_tcp_v2_t * l_default_tile=NULL;
9225 fprintf(out_stream, "Codestream info from main header: {\n");
9227 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9228 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9229 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9231 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9235 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9237 fprintf(out_stream, "\t default tile {\n");
9238 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9239 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9240 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9241 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9243 for (compno = 0; compno < numcomps; compno++) {
9244 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9246 OPJ_INT32 bandno, numbands;
9249 fprintf(out_stream, "\t\t comp %d {\n", compno);
9250 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9251 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9252 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9253 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9254 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9255 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9257 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9258 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9259 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9261 fprintf(out_stream, "\n");
9263 /* quantization style*/
9264 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9265 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9266 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9267 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9268 for (bandno = 0; bandno < numbands; bandno++) {
9269 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9270 l_tccp->stepsizes[bandno].expn);
9272 fprintf(out_stream, "\n");
9275 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9277 fprintf(out_stream, "\t\t }\n");
9278 } /*end of component of default tile*/
9279 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9283 fprintf(out_stream, "}\n");
9288 * Dump an image header structure.
9290 *@param img_header the image header to dump.
9291 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9292 *@param out_stream output stream where dump the elements.
9294 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9299 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9303 fprintf(out_stream, "Image info {\n");
9304 tab[0] = '\t';tab[1] = '\0';
9307 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9308 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9309 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9311 if (img_header->comps){
9313 for (compno = 0; compno < img_header->numcomps; compno++) {
9314 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9315 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9316 fprintf(out_stream,"%s}\n",tab);
9320 fprintf(out_stream, "}\n");
9324 * Dump a component image header structure.
9326 *@param comp_header the component image header to dump.
9327 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9328 *@param out_stream output stream where dump the elements.
9330 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9335 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9338 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9341 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9342 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9343 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9346 fprintf(out_stream, "}\n");
9351 * Get the codestream info from a JPEG2000 codec.
9353 *@param p_j2k the component image header to dump.
9355 *@return the codestream information extract from the jpg2000 codec
9357 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9360 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9361 opj_tcp_v2_t *l_default_tile;
9362 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9364 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9366 cstr_info->tx0 = p_j2k->m_cp.tx0;
9367 cstr_info->ty0 = p_j2k->m_cp.ty0;
9368 cstr_info->tdx = p_j2k->m_cp.tdx;
9369 cstr_info->tdy = p_j2k->m_cp.tdy;
9370 cstr_info->tw = p_j2k->m_cp.tw;
9371 cstr_info->th = p_j2k->m_cp.th;
9373 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9375 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9377 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9378 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9379 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9380 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9382 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9384 for (compno = 0; compno < numcomps; compno++) {
9385 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9386 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9387 OPJ_INT32 bandno, numbands;
9390 l_tccp_info->csty = l_tccp->csty;
9391 l_tccp_info->numresolutions = l_tccp->numresolutions;
9392 l_tccp_info->cblkw = l_tccp->cblkw;
9393 l_tccp_info->cblkh = l_tccp->cblkh;
9394 l_tccp_info->cblksty = l_tccp->cblksty;
9395 l_tccp_info->qmfbid = l_tccp->qmfbid;
9396 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9398 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9399 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9402 /* quantization style*/
9403 l_tccp_info->qntsty = l_tccp->qntsty;
9404 l_tccp_info->numgbits = l_tccp->numgbits;
9406 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9407 if (numbands < J2K_MAXBANDS) {
9408 for (bandno = 0; bandno < numbands; bandno++) {
9409 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9410 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9415 l_tccp_info->roishift = l_tccp->roishift;
9423 * Get the codestream index from a JPEG2000 codec.
9425 *@param p_j2k the component image header to dump.
9427 *@return the codestream index extract from the jpg2000 codec
9429 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9431 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9432 opj_calloc(1,sizeof(opj_codestream_index_t));
9436 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9437 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9438 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9440 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9441 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9442 if (!l_cstr_index->marker){
9443 opj_free( l_cstr_index);
9447 if (p_j2k->cstr_index->marker)
9448 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9450 opj_free(l_cstr_index->marker);
9451 l_cstr_index->marker = NULL;
9454 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9455 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9456 if (!l_cstr_index->tile_index){
9457 opj_free( l_cstr_index->marker);
9458 opj_free( l_cstr_index);
9462 if (!p_j2k->cstr_index->tile_index){
9463 opj_free(l_cstr_index->tile_index);
9464 l_cstr_index->tile_index = NULL;
9467 OPJ_UINT32 it_tile = 0;
9468 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9471 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9473 l_cstr_index->tile_index[it_tile].marker =
9474 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9476 if (!l_cstr_index->tile_index[it_tile].marker) {
9477 OPJ_UINT32 it_tile_free;
9479 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9480 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9483 opj_free( l_cstr_index->tile_index);
9484 opj_free( l_cstr_index->marker);
9485 opj_free( l_cstr_index);
9489 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9490 memcpy( l_cstr_index->tile_index[it_tile].marker,
9491 p_j2k->cstr_index->tile_index[it_tile].marker,
9492 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9494 opj_free(l_cstr_index->tile_index[it_tile].marker);
9495 l_cstr_index->tile_index[it_tile].marker = NULL;
9498 /* Tile part index*/
9499 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9501 l_cstr_index->tile_index[it_tile].tp_index =
9502 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9504 if(!l_cstr_index->tile_index[it_tile].tp_index){
9505 OPJ_UINT32 it_tile_free;
9507 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9508 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9509 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9512 opj_free( l_cstr_index->tile_index);
9513 opj_free( l_cstr_index->marker);
9514 opj_free( l_cstr_index);
9518 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9519 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9520 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9521 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9524 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9525 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9528 /* Packet index (NOT USED)*/
9529 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9530 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9535 return l_cstr_index;
9538 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9540 OPJ_UINT32 it_tile=0;
9542 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9543 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9544 if (!p_j2k->cstr_index->tile_index)
9547 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9548 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9549 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9550 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9551 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9552 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9562 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9563 opj_stream_private_t *p_stream,
9564 opj_event_mgr_t * p_manager)
9566 opj_bool l_go_on = OPJ_TRUE;
9567 OPJ_UINT32 l_current_tile_no;
9568 OPJ_UINT32 l_data_size,l_max_data_size;
9569 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9570 OPJ_UINT32 l_nb_comps;
9571 OPJ_BYTE * l_current_data;
9573 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9574 if (! l_current_data) {
9577 l_max_data_size = 1000;
9582 if (! opj_j2k_read_tile_header( p_j2k,
9585 &l_tile_x0, &l_tile_y0,
9586 &l_tile_x1, &l_tile_y1,
9591 opj_free(l_current_data);
9599 if (l_data_size > l_max_data_size) {
9600 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9601 if (! l_current_data) {
9602 opj_free(l_current_data);
9606 l_max_data_size = l_data_size;
9609 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9610 opj_free(l_current_data);
9613 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);
9615 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9616 opj_free(l_current_data);
9619 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9623 opj_free(l_current_data);
9629 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9631 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9634 assert(p_j2k != 00);
9636 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9637 /* DEVELOPER CORNER, add your custom procedures */
9642 * Read and decode one tile.
9644 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9645 opj_stream_private_t *p_stream,
9646 opj_event_mgr_t * p_manager)
9648 opj_bool l_go_on = OPJ_TRUE;
9649 OPJ_UINT32 l_current_tile_no;
9650 OPJ_UINT32 l_tile_no_to_dec;
9651 OPJ_UINT32 l_data_size,l_max_data_size;
9652 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9653 OPJ_UINT32 l_nb_comps;
9654 OPJ_BYTE * l_current_data;
9656 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9657 if (! l_current_data) {
9660 l_max_data_size = 1000;
9662 /*Allocate and initialize some elements of codestrem index if not already done*/
9663 if( !p_j2k->cstr_index->tile_index)
9665 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9666 opj_free(l_current_data);
9670 /* Move into the codestream to the first SOT used to decode the desired tile */
9671 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9672 if (p_j2k->cstr_index->tile_index)
9673 if(p_j2k->cstr_index->tile_index->tp_index)
9675 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9676 /* the index for this tile has not been built,
9677 * so move to the last SOT read */
9678 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9679 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9684 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)) ) {
9685 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9689 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9690 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9691 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9695 if (! opj_j2k_read_tile_header( p_j2k,
9698 &l_tile_x0, &l_tile_y0,
9699 &l_tile_x1, &l_tile_y1,
9704 opj_free(l_current_data);
9713 if (l_data_size > l_max_data_size) {
9714 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9715 if (! l_current_data) {
9716 opj_free(l_current_data);
9720 l_max_data_size = l_data_size;
9725 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9726 opj_free(l_current_data);
9729 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);
9731 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9732 opj_free(l_current_data);
9735 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9737 if(l_current_tile_no == l_tile_no_to_dec)
9739 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9740 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9741 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9747 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);
9752 opj_free(l_current_data);
9759 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9761 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9764 assert(p_j2k != 00);
9766 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9767 /* DEVELOPER CORNER, add your custom procedures */
9773 * Decodes the tiles of the stream.
9775 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9776 opj_stream_private_t * p_stream,
9777 opj_image_t * p_image,
9778 opj_event_mgr_t * p_manager)
9785 p_j2k->m_output_image = opj_image_create0();
9786 if (! (p_j2k->m_output_image)) {
9789 opj_copy_image_header(p_image, p_j2k->m_output_image);
9791 /* customization of the decoding */
9792 opj_j2k_setup_decoding(p_j2k);
9794 /* Decode the codestream */
9795 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9796 opj_image_destroy(p_j2k->m_private_image);
9797 p_j2k->m_private_image = NULL;
9801 /* Move data and copy one information from codec to output image*/
9802 for (compno = 0; compno < p_image->numcomps; compno++) {
9803 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9804 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9805 p_j2k->m_output_image->comps[compno].data = NULL;
9813 * Get the decoded tile.
9815 * @param p_j2k the jpeg2000 codestream codec.
9816 * @param p_stream input_stream
9817 * @param p_image output image. .
9818 * @param p_manager the user event manager
9819 * @param tile_index index of the tile we want decode
9821 * @return true if succeed.
9823 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
9824 opj_stream_private_t *p_stream,
9825 opj_image_t* p_image,
9826 opj_event_mgr_t * p_manager,
9827 OPJ_UINT32 tile_index )
9830 OPJ_UINT32 l_tile_x, l_tile_y;
9831 opj_image_comp_t* l_img_comp;
9834 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
9838 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9839 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);
9843 /* Compute the dimension of the desired tile*/
9844 l_tile_x = tile_index % p_j2k->m_cp.tw;
9845 l_tile_y = tile_index / p_j2k->m_cp.tw;
9847 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9848 if (p_image->x0 < p_j2k->m_private_image->x0)
9849 p_image->x0 = p_j2k->m_private_image->x0;
9850 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9851 if (p_image->x1 > p_j2k->m_private_image->x1)
9852 p_image->x1 = p_j2k->m_private_image->x1;
9854 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9855 if (p_image->y0 < p_j2k->m_private_image->y0)
9856 p_image->y0 = p_j2k->m_private_image->y0;
9857 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9858 if (p_image->y1 > p_j2k->m_private_image->y1)
9859 p_image->y1 = p_j2k->m_private_image->y1;
9861 l_img_comp = p_image->comps;
9862 for (compno=0; compno < p_image->numcomps; ++compno)
9864 OPJ_INT32 l_comp_x1, l_comp_y1;
9866 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9868 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9869 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9870 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9871 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9873 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9874 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9879 /* Destroy the previous output image*/
9880 if (p_j2k->m_output_image)
9881 opj_image_destroy(p_j2k->m_output_image);
9883 /* Create the ouput image from the information previously computed*/
9884 p_j2k->m_output_image = opj_image_create0();
9885 if (! (p_j2k->m_output_image)) {
9888 opj_copy_image_header(p_image, p_j2k->m_output_image);
9890 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9892 /* customization of the decoding */
9893 opj_j2k_setup_decoding_tile(p_j2k);
9895 /* Decode the codestream */
9896 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9897 opj_image_destroy(p_j2k->m_private_image);
9898 p_j2k->m_private_image = NULL;
9902 /* Move data and copy one information from codec to output image*/
9903 for (compno = 0; compno < p_image->numcomps; compno++) {
9904 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9906 if (p_image->comps[compno].data)
9907 opj_free(p_image->comps[compno].data);
9909 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9911 p_j2k->m_output_image->comps[compno].data = NULL;
9917 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
9918 OPJ_UINT32 res_factor,
9919 opj_event_mgr_t * p_manager)
9923 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9925 if (p_j2k->m_private_image) {
9926 if (p_j2k->m_private_image->comps) {
9927 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9928 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9929 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9930 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9931 if ( res_factor >= max_res){
9932 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
9935 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
9948 * Encodes all the tiles in a row.
9950 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
9951 opj_stream_private_t *p_stream,
9952 opj_event_mgr_t * p_manager )
9955 OPJ_UINT32 l_nb_tiles;
9956 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
9957 OPJ_BYTE * l_current_data;
9960 assert(p_j2k != 00);
9961 assert(p_stream != 00);
9962 assert(p_manager != 00);
9964 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9965 if (! l_current_data) {
9968 l_max_tile_size = 1000;
9970 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9971 for (i=0;i<l_nb_tiles;++i) {
9972 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
9973 opj_free(l_current_data);
9977 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
9978 if (l_current_tile_size > l_max_tile_size) {
9979 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
9980 if (! l_current_data) {
9983 l_max_tile_size = l_current_tile_size;
9986 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
9988 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
9993 opj_free(l_current_data);
9998 * Ends the compression procedures and possibility add data to be read after the
10001 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10002 opj_stream_private_t *p_stream,
10003 opj_event_mgr_t * p_manager)
10005 /* customization of the encoding */
10006 opj_j2k_setup_end_compress(p_j2k);
10008 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10018 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10020 * @param p_j2k the jpeg2000 codec.
10021 * @param p_stream the stream object.
10022 * @param p_manager the user event manager.
10024 * @return true if the codec is valid.
10026 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10027 opj_stream_private_t *p_stream,
10028 opj_image_t * p_image,
10029 opj_event_mgr_t * p_manager)
10031 /* preconditions */
10032 assert(p_j2k != 00);
10033 assert(p_stream != 00);
10034 assert(p_manager != 00);
10036 p_j2k->m_private_image = opj_image_create0();
10037 opj_copy_image_header(p_image, p_j2k->m_private_image);
10039 // TODO_MSD: Find a better way
10040 if (p_image->comps) {
10041 OPJ_UINT32 it_comp;
10042 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10043 if (p_image->comps[it_comp].data) {
10044 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10045 p_image->comps[it_comp].data = NULL;
10051 /* customization of the validation */
10052 opj_j2k_setup_encoding_validation (p_j2k);
10054 /* validation of the parameters codec */
10055 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10059 /* customization of the encoding */
10060 opj_j2k_setup_header_writting(p_j2k);
10063 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10073 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10074 OPJ_UINT32 p_tile_index,
10075 opj_stream_private_t *p_stream,
10076 opj_event_mgr_t * p_manager )
10079 if (p_tile_index != p_j2k->m_current_tile_number) {
10080 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10084 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);
10086 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10087 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10088 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10090 /* initialisation before tile encoding */
10091 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10101 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10103 OPJ_UINT32 i,j,k = 0;
10104 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10105 opj_image_comp_t * l_img_comp = 00;
10106 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10107 opj_image_t * l_image = 00;
10108 OPJ_UINT32 l_size_comp, l_remaining;
10109 OPJ_INT32 * l_src_ptr;
10110 l_tilec = p_tcd->tcd_image->tiles->comps;
10111 l_image = p_tcd->image;
10112 l_img_comp = l_image->comps;
10114 for (i=0;i<p_tcd->image->numcomps;++i) {
10115 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10116 l_remaining = l_img_comp->prec & 7; /* (%8) */
10121 if (l_size_comp == 3) {
10125 l_width = (l_tilec->x1 - l_tilec->x0);
10126 l_height = (l_tilec->y1 - l_tilec->y0);
10127 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10128 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10129 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10130 l_stride = l_image_width - l_width;
10131 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10133 switch (l_size_comp) {
10136 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10137 if (l_img_comp->sgnd) {
10138 for (j=0;j<l_height;++j) {
10139 for (k=0;k<l_width;++k) {
10140 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10144 l_src_ptr += l_stride;
10148 for (j=0;j<l_height;++j) {
10149 for (k=0;k<l_width;++k) {
10150 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10154 l_src_ptr += l_stride;
10158 p_data = (OPJ_BYTE*) l_dest_ptr;
10163 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10164 if (l_img_comp->sgnd) {
10165 for (j=0;j<l_height;++j) {
10166 for (k=0;k<l_width;++k) {
10167 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10169 l_src_ptr += l_stride;
10173 for (j=0;j<l_height;++j) {
10174 for (k=0;k<l_width;++k) {
10175 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10177 l_src_ptr += l_stride;
10181 p_data = (OPJ_BYTE*) l_dest_ptr;
10186 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10187 for (j=0;j<l_height;++j) {
10188 for (k=0;k<l_width;++k) {
10189 *(l_dest_ptr++) = *(l_src_ptr++);
10191 l_src_ptr += l_stride;
10194 p_data = (OPJ_BYTE*) l_dest_ptr;
10207 * @param p_j2k the jpeg2000 codec.
10208 * @param p_stream the stream to write data to.
10209 * @param p_manager the user event manager.
10211 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10213 OPJ_UINT32 p_data_size,
10214 opj_stream_private_t *p_stream,
10215 opj_event_mgr_t * p_manager )
10217 opj_tcd_v2_t * l_tcd = 00;
10218 opj_cp_v2_t * l_cp = 00;
10219 opj_tcp_v2_t * l_tcp = 00;
10220 OPJ_UINT32 l_nb_bytes_written;
10221 OPJ_BYTE * l_current_data = 00;
10222 OPJ_UINT32 l_tile_size = 0;
10223 OPJ_UINT32 l_available_data;
10225 /* preconditions */
10226 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10228 l_tcd = p_j2k->m_tcd;
10229 l_cp = &(p_j2k->m_cp);
10230 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10232 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10233 l_available_data = l_tile_size;
10234 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10236 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10237 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10241 l_nb_bytes_written = 0;
10242 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10245 l_current_data += l_nb_bytes_written;
10246 l_available_data -= l_nb_bytes_written;
10248 l_nb_bytes_written = 0;
10249 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10253 l_available_data -= l_nb_bytes_written;
10254 l_nb_bytes_written = l_tile_size - l_available_data;
10256 if ( opj_stream_write_data( p_stream,
10257 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10258 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10262 ++p_j2k->m_current_tile_number;
10269 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10270 * are valid. Developers wanting to extend the library can add their own validation procedures.
10272 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10274 /* preconditions */
10275 assert(p_j2k != 00);
10277 /* DEVELOPER CORNER, insert your custom procedures */
10278 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10280 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10281 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
10284 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );
10285 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );
10286 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
10290 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10291 * are valid. Developers wanting to extend the library can add their own validation procedures.
10293 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
10295 /* preconditions */
10296 assert(p_j2k != 00);
10298 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10299 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10301 /* DEVELOPER CORNER, add your custom validation procedure */
10302 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10307 * Sets up the procedures to do on writing header.
10308 * Developers wanting to extend the library can add their own writing procedures.
10310 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10312 /* preconditions */
10313 assert(p_j2k != 00);
10315 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );
10316 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10317 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10318 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10319 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10322 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10323 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components );
10324 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
10326 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10327 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
10331 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);
10333 if (p_j2k->m_cp.comment != 00) {
10334 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10337 /* DEVELOPER CORNER, insert your custom procedures */
10338 if (p_j2k->m_cp.rsiz & MCT) {
10339 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
10341 /* End of Developer Corner */
10343 if (p_j2k->cstr_index) {
10344 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10347 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10348 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10352 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10354 OPJ_UINT32 * p_data_written,
10355 OPJ_UINT32 p_total_data_size,
10356 opj_stream_private_t *p_stream,
10357 struct opj_event_mgr * p_manager )
10360 OPJ_UINT32 l_nb_bytes_written = 0;
10361 OPJ_UINT32 l_current_nb_bytes_written;
10362 OPJ_BYTE * l_begin_data = 00;
10364 opj_tcp_v2_t *l_tcp = 00;
10365 opj_tcd_v2_t * l_tcd = 00;
10366 opj_cp_v2_t * l_cp = 00;
10368 l_tcd = p_j2k->m_tcd;
10369 l_cp = &(p_j2k->m_cp);
10370 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10372 l_tcd->cur_pino = 0;
10374 /*Get number of tile parts*/
10375 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10380 l_current_nb_bytes_written = 0;
10381 l_begin_data = p_data;
10382 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10387 l_nb_bytes_written += l_current_nb_bytes_written;
10388 p_data += l_current_nb_bytes_written;
10389 p_total_data_size -= l_current_nb_bytes_written;
10391 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10392 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10393 l_current_nb_bytes_written = 0;
10394 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10395 l_nb_bytes_written += l_current_nb_bytes_written;
10396 p_data += l_current_nb_bytes_written;
10397 p_total_data_size -= l_current_nb_bytes_written;
10399 l_current_nb_bytes_written = 0;
10400 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10401 l_nb_bytes_written += l_current_nb_bytes_written;
10402 p_data += l_current_nb_bytes_written;
10403 p_total_data_size -= l_current_nb_bytes_written;
10406 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10407 l_current_nb_bytes_written = 0;
10408 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10409 l_nb_bytes_written += l_current_nb_bytes_written;
10410 p_data += l_current_nb_bytes_written;
10411 p_total_data_size -= l_current_nb_bytes_written;
10415 l_current_nb_bytes_written = 0;
10416 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10420 l_nb_bytes_written += l_current_nb_bytes_written;
10421 * p_data_written = l_nb_bytes_written;
10423 /* Writing Psot in SOT marker */
10424 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10426 if (l_cp->m_specific_param.m_enc.m_cinema){
10427 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10433 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10435 OPJ_UINT32 * p_data_written,
10436 OPJ_UINT32 p_total_data_size,
10437 opj_stream_private_t *p_stream,
10438 struct opj_event_mgr * p_manager
10441 OPJ_UINT32 tilepartno=0;
10442 OPJ_UINT32 l_nb_bytes_written = 0;
10443 OPJ_UINT32 l_current_nb_bytes_written;
10444 OPJ_UINT32 l_part_tile_size;
10445 OPJ_UINT32 tot_num_tp;
10448 OPJ_BYTE * l_begin_data;
10449 opj_tcp_v2_t *l_tcp = 00;
10450 opj_tcd_v2_t * l_tcd = 00;
10451 opj_cp_v2_t * l_cp = 00;
10454 l_tcd = p_j2k->m_tcd;
10455 l_cp = &(p_j2k->m_cp);
10456 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10458 /*Get number of tile parts*/
10459 tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
10461 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10462 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10463 l_current_nb_bytes_written = 0;
10464 l_part_tile_size = 0;
10465 l_begin_data = p_data;
10467 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10471 l_nb_bytes_written += l_current_nb_bytes_written;
10472 p_data += l_current_nb_bytes_written;
10473 p_total_data_size -= l_current_nb_bytes_written;
10474 l_part_tile_size += l_nb_bytes_written;
10476 l_current_nb_bytes_written = 0;
10477 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10481 p_data += l_current_nb_bytes_written;
10482 l_nb_bytes_written += l_current_nb_bytes_written;
10483 p_total_data_size -= l_current_nb_bytes_written;
10484 l_part_tile_size += l_nb_bytes_written;
10486 /* Writing Psot in SOT marker */
10487 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10489 if (l_cp->m_specific_param.m_enc.m_cinema) {
10490 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10493 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10496 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10497 l_tcd->cur_pino = pino;
10499 /*Get number of tile parts*/
10500 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
10501 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10502 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10503 l_current_nb_bytes_written = 0;
10504 l_part_tile_size = 0;
10505 l_begin_data = p_data;
10507 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10511 l_nb_bytes_written += l_current_nb_bytes_written;
10512 p_data += l_current_nb_bytes_written;
10513 p_total_data_size -= l_current_nb_bytes_written;
10514 l_part_tile_size += l_current_nb_bytes_written;
10516 l_current_nb_bytes_written = 0;
10518 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10522 l_nb_bytes_written += l_current_nb_bytes_written;
10523 p_data += l_current_nb_bytes_written;
10524 p_total_data_size -= l_current_nb_bytes_written;
10525 l_part_tile_size += l_current_nb_bytes_written;
10527 /* Writing Psot in SOT marker */
10528 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10530 if (l_cp->m_specific_param.m_enc.m_cinema) {
10531 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10534 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10538 *p_data_written = l_nb_bytes_written;
10544 * Writes the updated tlm.
10546 * @param p_stream the stream to write data to.
10547 * @param p_j2k J2K codec.
10548 * @param p_manager the user event manager.
10550 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10551 struct opj_stream_private *p_stream,
10552 struct opj_event_mgr * p_manager )
10554 OPJ_UINT32 l_tlm_size;
10555 OPJ_SIZE_T l_tlm_position, l_current_position;
10557 /* preconditions */
10558 assert(p_j2k != 00);
10559 assert(p_manager != 00);
10560 assert(p_stream != 00);
10562 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10563 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10564 l_current_position = opj_stream_tell(p_stream);
10566 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10570 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) {
10574 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10583 * Ends the encoding, i.e. frees memory.
10585 * @param p_stream the stream to write data to.
10586 * @param p_j2k J2K codec.
10587 * @param p_manager the user event manager.
10589 opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10590 struct opj_stream_private *p_stream,
10591 struct opj_event_mgr * p_manager )
10593 /* preconditions */
10594 assert(p_j2k != 00);
10595 assert(p_manager != 00);
10596 assert(p_stream != 00);
10598 tcd_destroy_v2(p_j2k->m_tcd);
10601 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10602 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10603 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10604 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10607 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10608 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10609 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10612 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10618 * Destroys the memory associated with the decoding of headers.
10620 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10621 opj_stream_private_t *p_stream,
10622 opj_event_mgr_t * p_manager
10625 /* preconditions */
10626 assert(p_j2k != 00);
10627 assert(p_stream != 00);
10628 assert(p_manager != 00);
10630 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10631 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10632 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10635 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10644 * @param p_stream the stream to write data to.
10645 * @param p_j2k J2K codec.
10646 * @param p_manager the user event manager.
10648 opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
10649 struct opj_stream_private *p_stream,
10650 struct opj_event_mgr * p_manager )
10652 opj_codestream_info_t * l_cstr_info = 00;
10654 /* preconditions */
10655 assert(p_j2k != 00);
10656 assert(p_manager != 00);
10657 assert(p_stream != 00);
10660 /* TODO mergeV2: check this part which use cstr_info */
10661 /*l_cstr_info = p_j2k->cstr_info;
10665 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));
10667 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10668 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10670 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10672 l_cstr_info->tw = p_j2k->m_cp.tw;
10673 l_cstr_info->th = p_j2k->m_cp.th;
10675 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10676 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10677 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10678 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10680 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10682 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10684 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10686 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10687 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10690 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10692 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10694 /*l_cstr_info->maxmarknum = 100;
10695 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10696 l_cstr_info->marknum = 0;
10699 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);
10703 * Creates a tile-coder decoder.
10705 * @param p_stream the stream to write data to.
10706 * @param p_j2k J2K codec.
10707 * @param p_manager the user event manager.
10709 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10710 opj_stream_private_t *p_stream,
10711 opj_event_mgr_t * p_manager
10714 /* preconditions */
10715 assert(p_j2k != 00);
10716 assert(p_manager != 00);
10717 assert(p_stream != 00);
10719 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10721 if (! p_j2k->m_tcd) {
10722 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10726 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10727 tcd_destroy_v2(p_j2k->m_tcd);
10738 * @param p_j2k the jpeg2000 codec.
10739 * @param p_stream the stream to write data to.
10740 * @param p_manager the user event manager.
10742 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10743 OPJ_UINT32 p_tile_index,
10745 OPJ_UINT32 p_data_size,
10746 opj_stream_private_t *p_stream,
10747 opj_event_mgr_t * p_manager )
10749 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10750 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10754 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10755 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);