2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 static void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool opj_j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool opj_j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool opj_j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 opj_stream_private_t *p_stream,
135 opj_event_mgr_t * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 opj_event_mgr_t * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool opj_j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void opj_j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool opj_j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool opj_j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 opj_stream_private_t *p_stream,
370 opj_event_mgr_t * p_manager );
372 static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 * Writes the SOC marker (Start Of Codestream)
383 * @param p_stream the stream to write data to.
384 * @param p_j2k J2K codec.
385 * @param p_manager the user event manager.
387 static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
388 opj_stream_private_t *p_stream,
389 opj_event_mgr_t * p_manager );
392 * Reads a SOC marker (Start of Codestream)
393 * @param p_header_data the data contained in the SOC box.
394 * @param jp2 the jpeg2000 file codec.
395 * @param p_header_size the size of the data contained in the SOC marker.
396 * @param p_manager the user event manager.
398 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
399 opj_stream_private_t *p_stream,
400 opj_event_mgr_t * p_manager );
403 * Writes the SIZ marker (image and tile size)
405 * @param p_stream the stream to write data to.
406 * @param p_j2k J2K codec.
407 * @param p_manager the user event manager.
409 static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
410 opj_stream_private_t *p_stream,
411 opj_event_mgr_t * p_manager );
414 * Reads a SIZ marker (image and tile size)
415 * @param p_header_data the data contained in the SIZ box.
416 * @param jp2 the jpeg2000 file codec.
417 * @param p_header_size the size of the data contained in the SIZ marker.
418 * @param p_manager the user event manager.
420 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
421 OPJ_BYTE * p_header_data,
422 OPJ_UINT32 p_header_size,
423 opj_event_mgr_t * p_manager);
426 * Writes the COM marker (comment)
428 * @param p_stream the stream to write data to.
429 * @param p_j2k J2K codec.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
433 opj_stream_private_t *p_stream,
434 opj_event_mgr_t * p_manager );
437 * Reads a COM marker (comments)
438 * @param p_header_data the data contained in the COM box.
439 * @param jp2 the jpeg2000 file codec.
440 * @param p_header_size the size of the data contained in the COM marker.
441 * @param p_manager the user event manager.
443 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
444 OPJ_BYTE * p_header_data,
445 OPJ_UINT32 p_header_size,
446 opj_event_mgr_t * p_manager );
448 * Writes the COD marker (Coding style default)
450 * @param p_stream the stream to write data to.
451 * @param p_j2k J2K codec.
452 * @param p_manager the user event manager.
454 static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
455 opj_stream_private_t *p_stream,
456 opj_event_mgr_t * p_manager );
459 * Reads a COD marker (Coding Styke defaults)
460 * @param p_header_data the data contained in the COD box.
461 * @param p_j2k the jpeg2000 codec.
462 * @param p_header_size the size of the data contained in the COD marker.
463 * @param p_manager the user event manager.
465 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
466 OPJ_BYTE * p_header_data,
467 OPJ_UINT32 p_header_size,
468 opj_event_mgr_t * p_manager);
471 * Writes the COC marker (Coding style component)
473 * @param p_comp_number the index of the component to output.
474 * @param p_stream the stream to write data to.
475 * @param p_j2k J2K codec.
476 * @param p_manager the user event manager.
478 static opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
479 OPJ_UINT32 p_comp_no,
480 opj_stream_private_t *p_stream,
481 opj_event_mgr_t * p_manager );
483 * Writes the COC marker (Coding style component)
485 * @param p_comp_no the index of the component to output.
486 * @param p_stream the stream to write data to.
487 * @param p_j2k J2K codec.
488 * @param p_manager the user event manager.
490 static void opj_j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
491 OPJ_UINT32 p_comp_no,
493 OPJ_UINT32 * p_data_written,
494 opj_event_mgr_t * p_manager );
497 * Gets the maximum size taken by a coc.
499 * @param p_j2k the jpeg2000 codec to use.
501 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
505 * Reads a COC marker (Coding Style Component)
506 * @param p_header_data the data contained in the COC box.
507 * @param p_j2k the jpeg2000 codec.
508 * @param p_header_size the size of the data contained in the COC marker.
509 * @param p_manager the user event manager.
511 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
512 OPJ_BYTE * p_header_data,
513 OPJ_UINT32 p_header_size,
514 opj_event_mgr_t * p_manager );
517 * Writes the QCD marker (quantization default)
519 * @param p_comp_number the index of the component to output.
520 * @param p_stream the stream to write data to.
521 * @param p_j2k J2K codec.
522 * @param p_manager the user event manager.
524 static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
525 opj_stream_private_t *p_stream,
526 opj_event_mgr_t * p_manager );
529 * Reads a QCD marker (Quantization defaults)
530 * @param p_header_data the data contained in the QCD box.
531 * @param p_j2k the jpeg2000 codec.
532 * @param p_header_size the size of the data contained in the QCD marker.
533 * @param p_manager the user event manager.
535 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
536 OPJ_BYTE * p_header_data,
537 OPJ_UINT32 p_header_size,
538 opj_event_mgr_t * p_manager );
540 * Writes the QCC marker (quantization component)
542 * @param p_comp_no the index of the component to output.
543 * @param p_stream the stream to write data to.
544 * @param p_j2k J2K codec.
545 * @param p_manager the user event manager.
547 static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
548 OPJ_UINT32 p_comp_no,
549 opj_stream_private_t *p_stream,
550 opj_event_mgr_t * p_manager );
553 * Writes the QCC marker (quantization component)
555 * @param p_comp_no the index of the component to output.
556 * @param p_stream the stream to write data to.
557 * @param p_j2k J2K codec.
558 * @param p_manager the user event manager.
560 static void opj_j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
561 OPJ_UINT32 p_comp_no,
563 OPJ_UINT32 * p_data_written,
564 opj_event_mgr_t * p_manager );
567 * Gets the maximum size taken by a qcc.
569 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
572 * Reads a QCC marker (Quantization component)
573 * @param p_header_data the data contained in the QCC box.
574 * @param p_j2k the jpeg2000 codec.
575 * @param p_header_size the size of the data contained in the QCC marker.
576 * @param p_manager the user event manager.
578 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 opj_event_mgr_t * p_manager);
583 * Writes the POC marker (Progression Order Change)
585 * @param p_stream the stream to write data to.
586 * @param p_j2k J2K codec.
587 * @param p_manager the user event manager.
589 static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
590 opj_stream_private_t *p_stream,
591 opj_event_mgr_t * p_manager );
593 * Writes the POC marker (Progression Order Change)
595 * @param p_stream the stream to write data to.
596 * @param p_j2k J2K codec.
597 * @param p_manager the user event manager.
599 static void opj_j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
601 OPJ_UINT32 * p_data_written,
602 opj_event_mgr_t * p_manager );
604 * Gets the maximum size taken by the writting of a POC.
606 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
609 * Reads a POC marker (Progression Order Change)
611 * @param p_header_data the data contained in the POC box.
612 * @param p_j2k the jpeg2000 codec.
613 * @param p_header_size the size of the data contained in the POC marker.
614 * @param p_manager the user event manager.
616 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
617 OPJ_BYTE * p_header_data,
618 OPJ_UINT32 p_header_size,
619 opj_event_mgr_t * p_manager );
622 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
624 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
627 * Gets the maximum size taken by the headers of the SOT.
629 * @param p_j2k the jpeg2000 codec to use.
631 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
635 * Reads a CRG marker (Component registration)
637 * @param p_header_data the data contained in the TLM box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the TLM marker.
640 * @param p_manager the user event manager.
642 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
643 OPJ_BYTE * p_header_data,
644 OPJ_UINT32 p_header_size,
645 opj_event_mgr_t * p_manager );
647 * Reads a TLM marker (Tile Length Marker)
649 * @param p_header_data the data contained in the TLM box.
650 * @param p_j2k the jpeg2000 codec.
651 * @param p_header_size the size of the data contained in the TLM marker.
652 * @param p_manager the user event manager.
654 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
655 OPJ_BYTE * p_header_data,
656 OPJ_UINT32 p_header_size,
657 opj_event_mgr_t * p_manager);
660 * Writes the updated tlm.
662 * @param p_stream the stream to write data to.
663 * @param p_j2k J2K codec.
664 * @param p_manager the user event manager.
666 static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
667 opj_stream_private_t *p_stream,
668 opj_event_mgr_t * p_manager );
671 * Reads a PLM marker (Packet length, main header marker)
673 * @param p_header_data the data contained in the TLM box.
674 * @param p_j2k the jpeg2000 codec.
675 * @param p_header_size the size of the data contained in the TLM marker.
676 * @param p_manager the user event manager.
678 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
679 OPJ_BYTE * p_header_data,
680 OPJ_UINT32 p_header_size,
681 opj_event_mgr_t * p_manager);
683 * Reads a PLT marker (Packet length, tile-part header)
685 * @param p_header_data the data contained in the PLT box.
686 * @param p_j2k the jpeg2000 codec.
687 * @param p_header_size the size of the data contained in the PLT marker.
688 * @param p_manager the user event manager.
690 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
691 OPJ_BYTE * p_header_data,
692 OPJ_UINT32 p_header_size,
693 opj_event_mgr_t * p_manager );
697 * Reads a PPM marker (Packed packet headers, main header)
699 * @param p_header_data the data contained in the POC box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the POC marker.
702 * @param p_manager the user event manager.
705 static opj_bool j2k_read_ppm_v2 (
707 OPJ_BYTE * p_header_data,
708 OPJ_UINT32 p_header_size,
709 struct opj_event_mgr * p_manager
713 static opj_bool j2k_read_ppm_v3 (
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 opj_event_mgr_t * p_manager );
721 * Reads a PPT marker (Packed packet headers, tile-part header)
723 * @param p_header_data the data contained in the PPT box.
724 * @param p_j2k the jpeg2000 codec.
725 * @param p_header_size the size of the data contained in the PPT marker.
726 * @param p_manager the user event manager.
728 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
729 OPJ_BYTE * p_header_data,
730 OPJ_UINT32 p_header_size,
731 opj_event_mgr_t * p_manager );
733 * Writes the TLM marker (Tile Length Marker)
735 * @param p_stream the stream to write data to.
736 * @param p_j2k J2K codec.
737 * @param p_manager the user event manager.
739 static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
740 opj_stream_private_t *p_stream,
741 opj_event_mgr_t * p_manager );
744 * Writes the SOT marker (Start of tile-part)
746 * @param p_stream the stream to write data to.
747 * @param p_j2k J2K codec.
748 * @param p_manager the user event manager.
750 static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
752 OPJ_UINT32 * p_data_written,
753 const opj_stream_private_t *p_stream,
754 opj_event_mgr_t * p_manager );
757 * Reads a PPT marker (Packed packet headers, tile-part header)
759 * @param p_header_data the data contained in the PPT box.
760 * @param p_j2k the jpeg2000 codec.
761 * @param p_header_size the size of the data contained in the PPT marker.
762 * @param p_manager the user event manager.
764 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
765 OPJ_BYTE * p_header_data,
766 OPJ_UINT32 p_header_size,
767 opj_event_mgr_t * p_manager );
769 * Writes the SOD marker (Start of data)
771 * @param p_stream the stream to write data to.
772 * @param p_j2k J2K codec.
773 * @param p_manager the user event manager.
775 static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
776 opj_tcd_v2_t * p_tile_coder,
778 OPJ_UINT32 * p_data_written,
779 OPJ_UINT32 p_total_data_size,
780 const opj_stream_private_t *p_stream,
781 opj_event_mgr_t * p_manager );
784 * Reads a SOD marker (Start Of Data)
786 * @param p_header_data the data contained in the SOD box.
787 * @param p_j2k the jpeg2000 codec.
788 * @param p_header_size the size of the data contained in the SOD marker.
789 * @param p_manager the user event manager.
791 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
792 opj_stream_private_t *p_stream,
793 opj_event_mgr_t * p_manager );
796 * Updates the Tile Length Marker.
798 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
800 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
801 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
803 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
804 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
808 * Writes the RGN marker (Region Of Interest)
810 * @param p_tile_no the tile to output
811 * @param p_comp_no the component to output
812 * @param p_stream the stream to write data to.
813 * @param p_j2k J2K codec.
814 * @param p_manager the user event manager.
816 static opj_bool opj_j2k_write_rgn( opj_j2k_v2_t *p_j2k,
817 OPJ_UINT32 p_tile_no,
818 OPJ_UINT32 p_comp_no,
819 opj_stream_private_t *p_stream,
820 opj_event_mgr_t * p_manager );
823 * Reads a RGN marker (Region Of Interest)
825 * @param p_header_data the data contained in the POC box.
826 * @param p_j2k the jpeg2000 codec.
827 * @param p_header_size the size of the data contained in the POC marker.
828 * @param p_manager the user event manager.
830 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
831 OPJ_BYTE * p_header_data,
832 OPJ_UINT32 p_header_size,
833 opj_event_mgr_t * p_manager );
836 * Writes the EOC marker (End of Codestream)
838 * @param p_stream the stream to write data to.
839 * @param p_j2k J2K codec.
840 * @param p_manager the user event manager.
842 static opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k,
843 opj_stream_private_t *p_stream,
844 opj_event_mgr_t * p_manager );
847 * Reads a EOC marker (End Of Codestream)
849 * @param p_header_data the data contained in the SOD box.
850 * @param p_j2k the jpeg2000 codec.
851 * @param p_header_size the size of the data contained in the SOD marker.
852 * @param p_manager the user event manager.
854 static opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k,
855 opj_stream_private_t *p_stream,
856 opj_event_mgr_t * p_manager );
859 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
861 * @param p_stream the stream to write data to.
862 * @param p_j2k J2K codec.
863 * @param p_manager the user event manager.
865 static opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
866 opj_stream_private_t *p_stream,
867 opj_event_mgr_t * p_manager );
872 * @param p_stream the stream to write data to.
873 * @param p_j2k J2K codec.
874 * @param p_manager the user event manager.
876 static opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
877 opj_stream_private_t *p_stream,
878 opj_event_mgr_t * p_manager );
881 Add main header marker information
882 @param cstr_info Codestream information structure
883 @param type marker type
884 @param pos byte offset of marker segment
885 @param len length of marker segment
887 static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
889 Add tile header marker information
890 @param tileno tile index number
891 @param cstr_info Codestream information structure
892 @param type marker type
893 @param pos byte offset of marker segment
894 @param len length of marker segment
896 static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
899 * Reads an unknown marker
901 * @param p_stream the stream object to read from.
902 * @param p_j2k the jpeg2000 codec.
903 * @param p_manager the user event manager.
905 * @return true if the marker could be deduced.
907 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
908 opj_stream_private_t *p_stream,
909 OPJ_UINT32 *output_marker,
910 opj_event_mgr_t * p_manager );
913 * Writes the MCT marker (Multiple Component Transform)
915 * @param p_stream the stream to write data to.
916 * @param p_j2k J2K codec.
917 * @param p_manager the user event manager.
919 static opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
920 opj_mct_data_t * p_mct_record,
921 opj_stream_private_t *p_stream,
922 opj_event_mgr_t * p_manager );
925 * Reads a MCT marker (Multiple Component Transform)
927 * @param p_header_data the data contained in the MCT box.
928 * @param p_j2k the jpeg2000 codec.
929 * @param p_header_size the size of the data contained in the MCT marker.
930 * @param p_manager the user event manager.
932 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
933 OPJ_BYTE * p_header_data,
934 OPJ_UINT32 p_header_size,
935 opj_event_mgr_t * p_manager );
938 * Writes the MCC marker (Multiple Component Collection)
940 * @param p_stream the stream to write data to.
941 * @param p_j2k J2K codec.
942 * @param p_manager the user event manager.
944 static opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
945 opj_simple_mcc_decorrelation_data_t * p_mcc_record,
946 opj_stream_private_t *p_stream,
947 opj_event_mgr_t * p_manager );
951 * Reads a MCC marker (Multiple Component Collection)
953 * @param p_header_data the data contained in the MCC box.
954 * @param p_j2k the jpeg2000 codec.
955 * @param p_header_size the size of the data contained in the MCC marker.
956 * @param p_manager the user event manager.
958 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
959 OPJ_BYTE * p_header_data,
960 OPJ_UINT32 p_header_size,
961 opj_event_mgr_t * p_manager );
964 * Writes the MCO marker (Multiple component transformation ordering)
966 * @param p_stream the stream to write data to.
967 * @param p_j2k J2K codec.
968 * @param p_manager the user event manager.
970 static opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
971 opj_stream_private_t *p_stream,
972 opj_event_mgr_t * p_manager );
975 * Reads a MCO marker (Multiple Component Transform Ordering)
977 * @param p_header_data the data contained in the MCO box.
978 * @param p_j2k the jpeg2000 codec.
979 * @param p_header_size the size of the data contained in the MCO marker.
980 * @param p_manager the user event manager.
982 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
983 OPJ_BYTE * p_header_data,
984 OPJ_UINT32 p_header_size,
985 opj_event_mgr_t * p_manager );
987 static opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
989 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
990 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
991 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
992 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
994 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
995 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
996 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
997 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
999 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1000 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1001 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1002 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1006 * Ends the encoding, i.e. frees memory.
1008 * @param p_stream the stream to write data to.
1009 * @param p_j2k J2K codec.
1010 * @param p_manager the user event manager.
1012 static opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1013 opj_stream_private_t *p_stream,
1014 opj_event_mgr_t * p_manager );
1017 * Writes the CBD marker (Component bit depth definition)
1019 * @param p_stream the stream to write data to.
1020 * @param p_j2k J2K codec.
1021 * @param p_manager the user event manager.
1023 static opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1024 opj_stream_private_t *p_stream,
1025 opj_event_mgr_t * p_manager );
1028 * Reads a CBD marker (Component bit depth definition)
1029 * @param p_header_data the data contained in the CBD box.
1030 * @param p_j2k the jpeg2000 codec.
1031 * @param p_header_size the size of the data contained in the CBD marker.
1032 * @param p_manager the user event manager.
1034 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1035 OPJ_BYTE * p_header_data,
1036 OPJ_UINT32 p_header_size,
1037 opj_event_mgr_t * p_manager);
1040 * Writes the image components.
1042 * @param p_stream the stream to write data to.
1043 * @param p_j2k J2K codec.
1044 * @param p_manager the user event manager.
1046 static opj_bool opj_j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1047 opj_stream_private_t *p_stream,
1048 opj_event_mgr_t * p_manager );
1051 * Writes regions of interests.
1053 * @param p_stream the stream to write data to.
1054 * @param p_j2k J2K codec.
1055 * @param p_manager the user event manager.
1057 static opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
1058 opj_stream_private_t *p_stream,
1059 opj_event_mgr_t * p_manager );
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_epc( opj_j2k_v2_t *p_j2k,
1069 opj_stream_private_t *p_stream,
1070 opj_event_mgr_t * p_manager );
1073 * Checks the progression order changes values. Tells of the poc given as input are valid.
1074 * A nice message is outputted at errors.
1076 * @param p_pocs the progression order changes.
1077 * @param p_nb_pocs the number of progression order changes.
1078 * @param p_nb_resolutions the number of resolutions.
1079 * @param numcomps the number of components
1080 * @param numlayers the number of layers.
1082 * @return true if the pocs are valid.
1084 static opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1085 OPJ_UINT32 p_nb_pocs,
1086 OPJ_UINT32 p_nb_resolutions,
1087 OPJ_UINT32 numcomps,
1088 OPJ_UINT32 numlayers,
1089 opj_event_mgr_t * p_manager);
1092 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1094 * @param cp the coding parameters.
1095 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1096 * @param tileno the given tile.
1098 * @return the number of tile parts.
1100 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1103 * Calculates the total number of tile parts needed by the encoder to
1104 * encode such an image. If not enough memory is available, then the function return false.
1106 * @param p_nb_tiles pointer that will hold the number of tile parts.
1107 * @param cp the coding parameters for the image.
1108 * @param image the image to encode.
1109 * @param p_j2k the p_j2k encoder.
1110 * @param p_manager the user event manager.
1112 * @return true if the function was successful, false else.
1114 static opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1116 OPJ_UINT32 * p_nb_tiles,
1118 opj_event_mgr_t * p_manager);
1120 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1122 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1124 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1126 static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_v2_t * p_tcp);
1128 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_v2_t * p_tcp);
1134 /* ----------------------------------------------------------------------- */
1135 typedef struct j2k_prog_order{
1136 OPJ_PROG_ORDER enum_prog;
1140 j2k_prog_order_t j2k_prog_order_list[] = {
1146 {(OPJ_PROG_ORDER)-1, ""}
1154 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1162 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1164 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1166 opj_j2k_read_int16_to_float,
1167 opj_j2k_read_int32_to_float,
1168 opj_j2k_read_float32_to_float,
1169 opj_j2k_read_float64_to_float
1172 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1174 opj_j2k_read_int16_to_int32,
1175 opj_j2k_read_int32_to_int32,
1176 opj_j2k_read_float32_to_int32,
1177 opj_j2k_read_float64_to_int32
1180 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1182 opj_j2k_write_float_to_int16,
1183 opj_j2k_write_float_to_int32,
1184 opj_j2k_write_float_to_float,
1185 opj_j2k_write_float_to_float64
1188 typedef struct opj_dec_memory_marker_handler
1192 /** value of the state when the marker can appear */
1194 /** action linked to the marker */
1195 opj_bool (*handler) ( opj_j2k_v2_t *p_j2k,
1196 OPJ_BYTE * p_header_data,
1197 OPJ_UINT32 p_header_size,
1198 opj_event_mgr_t * p_manager );
1200 opj_dec_memory_marker_handler_t;
1202 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1204 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1205 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1206 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1207 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1208 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1209 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1210 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1211 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1212 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1213 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1214 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1215 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1216 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1218 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1219 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1220 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1221 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1222 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1223 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1225 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1226 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1227 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1228 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1229 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1231 #endif /* USE_JPWL */
1233 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1234 {J2K_MS_INSEC, 0, j2k_read_insec}
1235 #endif /* USE_JPSEC */
1236 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1239 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1241 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1242 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1246 for (i=0;i<p_nb_elem;++i) {
1247 opj_read_bytes(l_src_data,&l_temp,2);
1249 l_src_data+=sizeof(OPJ_INT16);
1251 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1255 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1257 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1258 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1262 for (i=0;i<p_nb_elem;++i) {
1263 opj_read_bytes(l_src_data,&l_temp,4);
1265 l_src_data+=sizeof(OPJ_INT32);
1267 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1271 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1273 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1274 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1278 for (i=0;i<p_nb_elem;++i) {
1279 opj_read_float(l_src_data,&l_temp);
1281 l_src_data+=sizeof(OPJ_FLOAT32);
1283 *(l_dest_data++) = l_temp;
1287 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1289 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1290 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1294 for (i=0;i<p_nb_elem;++i) {
1295 opj_read_double(l_src_data,&l_temp);
1297 l_src_data+=sizeof(OPJ_FLOAT64);
1299 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1303 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1305 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1306 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1310 for (i=0;i<p_nb_elem;++i) {
1311 opj_read_bytes(l_src_data,&l_temp,2);
1313 l_src_data+=sizeof(OPJ_INT16);
1315 *(l_dest_data++) = (OPJ_INT32) l_temp;
1319 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1321 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1322 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1326 for (i=0;i<p_nb_elem;++i) {
1327 opj_read_bytes(l_src_data,&l_temp,4);
1329 l_src_data+=sizeof(OPJ_INT32);
1331 *(l_dest_data++) = (OPJ_INT32) l_temp;
1335 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1337 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1338 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1342 for (i=0;i<p_nb_elem;++i) {
1343 opj_read_float(l_src_data,&l_temp);
1345 l_src_data+=sizeof(OPJ_FLOAT32);
1347 *(l_dest_data++) = (OPJ_INT32) l_temp;
1351 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1353 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1354 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1358 for (i=0;i<p_nb_elem;++i) {
1359 opj_read_double(l_src_data,&l_temp);
1361 l_src_data+=sizeof(OPJ_FLOAT64);
1363 *(l_dest_data++) = (OPJ_INT32) l_temp;
1367 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1369 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1370 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1374 for (i=0;i<p_nb_elem;++i) {
1375 l_temp = (OPJ_UINT32) *(l_src_data++);
1377 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1379 l_dest_data+=sizeof(OPJ_INT16);
1383 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1385 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1386 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1390 for (i=0;i<p_nb_elem;++i) {
1391 l_temp = (OPJ_UINT32) *(l_src_data++);
1393 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1395 l_dest_data+=sizeof(OPJ_INT32);
1399 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1401 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1402 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1406 for (i=0;i<p_nb_elem;++i) {
1407 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1409 opj_write_float(l_dest_data,l_temp);
1411 l_dest_data+=sizeof(OPJ_FLOAT32);
1415 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1417 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1418 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1422 for (i=0;i<p_nb_elem;++i) {
1423 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1425 opj_write_double(l_dest_data,l_temp);
1427 l_dest_data+=sizeof(OPJ_FLOAT64);
1433 * Converts an enum type progression order to string type.
1435 * @param prg_order the progression order to get.
1437 * @return the string representation of the given progression order.
1439 char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1440 j2k_prog_order_t *po;
1441 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1442 if(po->enum_prog == prg_order){
1443 return po->str_prog;
1446 return po->str_prog;
1450 * Checks the progression order changes values. Tells if the poc given as input are valid.
1452 * @param p_pocs the progression order changes.
1453 * @param p_nb_pocs the number of progression order changes.
1454 * @param p_nb_resolutions the number of resolutions.
1455 * @param numcomps the number of components
1456 * @param numlayers the number of layers.
1457 * @param p_manager the user event manager.
1459 * @return true if the pocs are valid.
1461 opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1462 OPJ_UINT32 p_nb_pocs,
1463 OPJ_UINT32 p_nb_resolutions,
1464 OPJ_UINT32 p_num_comps,
1465 OPJ_UINT32 p_num_layers,
1466 opj_event_mgr_t * p_manager)
1468 OPJ_UINT32* packet_array;
1469 OPJ_UINT32 index , resno, compno, layno;
1471 OPJ_UINT32 step_c = 1;
1472 OPJ_UINT32 step_r = p_num_comps * step_c;
1473 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1474 opj_bool loss = OPJ_FALSE;
1475 OPJ_UINT32 layno0 = 0;
1477 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1478 if (packet_array == 00) {
1479 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1482 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1484 if (p_nb_pocs == 0) {
1488 index = step_r * p_pocs->resno0;
1489 // take each resolution for each poc
1490 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1492 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1494 // take each comp of each resolution for each poc
1495 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1496 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1498 // and finally take each layer of each res of ...
1499 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1500 //index = step_r * resno + step_c * compno + step_l * layno;
1501 packet_array[comp_index] = 1;
1502 comp_index += step_l;
1505 res_index += step_c;
1512 // iterate through all the pocs
1513 for (i = 1; i < p_nb_pocs ; ++i) {
1514 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1516 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1517 index = step_r * p_pocs->resno0;
1519 // take each resolution for each poc
1520 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1521 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1523 // take each comp of each resolution for each poc
1524 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1525 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1527 // and finally take each layer of each res of ...
1528 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1529 //index = step_r * resno + step_c * compno + step_l * layno;
1530 packet_array[comp_index] = 1;
1531 comp_index += step_l;
1534 res_index += step_c;
1544 for (layno = 0; layno < p_num_layers ; ++layno) {
1545 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1546 for (compno = 0; compno < p_num_comps; ++compno) {
1547 loss |= (packet_array[index]!=1);
1548 //index = step_r * resno + step_c * compno + step_l * layno;
1555 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1558 opj_free(packet_array);
1563 /* ----------------------------------------------------------------------- */
1566 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1568 * @param cp the coding parameters.
1569 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1570 * @param tileno the given tile.
1572 * @return the number of tile parts.
1574 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1576 const OPJ_CHAR *prog = 00;
1578 OPJ_UINT32 tpnum = 1;
1579 opj_tcp_v2_t *tcp = 00;
1580 opj_poc_t * l_current_poc = 00;
1583 assert(tileno < (cp->tw * cp->th));
1584 assert(pino < (cp->tcps[tileno].numpocs + 1));
1586 /* get the given tile coding parameter */
1587 tcp = &cp->tcps[tileno];
1590 l_current_poc = &(tcp->pocs[pino]);
1591 assert(l_current_poc != 0);
1593 /* get the progression order as a character string */
1594 prog = opj_j2k_convert_progression_order(tcp->prg);
1595 assert(strlen(prog) > 0);
1597 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1601 /* component wise */
1603 tpnum *= l_current_poc->compE;
1605 /* resolution wise */
1607 tpnum *= l_current_poc->resE;
1611 tpnum *= l_current_poc->prcE;
1615 tpnum *= l_current_poc->layE;
1618 /* whould we split here ? */
1619 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1620 cp->m_specific_param.m_enc.m_tp_pos=i;
1633 * Calculates the total number of tile parts needed by the encoder to
1634 * encode such an image. If not enough memory is available, then the function return false.
1636 * @param p_nb_tiles pointer that will hold the number of tile parts.
1637 * @param cp the coding parameters for the image.
1638 * @param image the image to encode.
1639 * @param p_j2k the p_j2k encoder.
1640 * @param p_manager the user event manager.
1642 * @return true if the function was successful, false else.
1644 opj_bool opj_j2k_calculate_tp( opj_j2k_v2_t *p_j2k,
1646 OPJ_UINT32 * p_nb_tiles,
1648 opj_event_mgr_t * p_manager
1651 OPJ_UINT32 pino,tileno;
1652 OPJ_UINT32 l_nb_tiles;
1656 assert(p_nb_tiles != 00);
1658 assert(image != 00);
1659 assert(p_j2k != 00);
1660 assert(p_manager != 00);
1662 l_nb_tiles = cp->tw * cp->th;
1667 /* TODO mergeV2: check this part which use cstr_info */
1668 /*if (p_j2k->cstr_info) {
1669 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1671 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1672 OPJ_UINT32 cur_totnum_tp = 0;
1674 pi_update_encoding_parameters(image,cp,tileno);
1676 for (pino = 0; pino <= tcp->numpocs; ++pino)
1678 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1680 *p_nb_tiles = *p_nb_tiles + tp_num;
1682 cur_totnum_tp += tp_num;
1685 tcp->m_nb_tile_parts = cur_totnum_tp;
1687 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1688 if (l_info_tile_ptr->tp == 00) {
1692 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1694 l_info_tile_ptr->num_tps = cur_totnum_tp;
1701 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1702 OPJ_UINT32 cur_totnum_tp = 0;
1704 pi_update_encoding_parameters(image,cp,tileno);
1706 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1707 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1709 *p_nb_tiles = *p_nb_tiles + tp_num;
1711 cur_totnum_tp += tp_num;
1713 tcp->m_nb_tile_parts = cur_totnum_tp;
1723 * Writes the SOC marker (Start Of Codestream)
1725 * @param p_stream the stream to write data to.
1726 * @param p_j2k J2K codec.
1727 * @param p_manager the user event manager.
1729 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1730 opj_stream_private_t *p_stream,
1731 opj_event_mgr_t * p_manager )
1733 /* 2 bytes will be written */
1734 OPJ_BYTE * l_start_stream = 00;
1737 assert(p_stream != 00);
1738 assert(p_j2k != 00);
1739 assert(p_manager != 00);
1741 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1743 /* write SOC identifier */
1744 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1746 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1752 /* update markers struct */
1754 opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1756 assert( 0 && "TODO" );
1757 #endif /* USE_JPWL */
1766 * Reads a SOC marker (Start of Codestream)
1767 * @param p_header_data the data contained in the SOC box.
1768 * @param jp2 the jpeg2000 file codec.
1769 * @param p_header_size the size of the data contained in the SOC marker.
1770 * @param p_manager the user event manager.
1772 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1773 opj_stream_private_t *p_stream,
1774 opj_event_mgr_t * p_manager
1777 OPJ_BYTE l_data [2];
1778 OPJ_UINT32 l_marker;
1781 assert(p_j2k != 00);
1782 assert(p_manager != 00);
1783 assert(p_stream != 00);
1785 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1789 opj_read_bytes(l_data,&l_marker,2);
1790 if (l_marker != J2K_MS_SOC) {
1794 /* Next marker should be a SIZ marker in the main header */
1795 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1797 /* FIXME move it in a index structure included in p_j2k*/
1798 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1800 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1802 /* Add the marker to the codestream index*/
1803 if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) {
1804 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1812 * Writes the SIZ marker (image and tile size)
1814 * @param p_stream the stream to write data to.
1815 * @param p_j2k J2K codec.
1816 * @param p_manager the user event manager.
1818 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1819 opj_stream_private_t *p_stream,
1820 opj_event_mgr_t * p_manager )
1823 OPJ_UINT32 l_size_len;
1824 OPJ_BYTE * l_current_ptr;
1825 opj_image_t * l_image = 00;
1826 opj_cp_v2_t *cp = 00;
1827 opj_image_comp_t * l_img_comp = 00;
1830 assert(p_stream != 00);
1831 assert(p_j2k != 00);
1832 assert(p_manager != 00);
1834 l_image = p_j2k->m_private_image;
1835 cp = &(p_j2k->m_cp);
1836 l_size_len = 40 + 3 * l_image->numcomps;
1837 l_img_comp = l_image->comps;
1839 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1841 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1842 if (! new_header_tile_data) {
1843 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1844 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1845 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1846 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1849 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
1850 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1853 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1855 /* write SOC identifier */
1856 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1859 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1862 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1865 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1868 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1871 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1874 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1877 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1880 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1883 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
1886 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
1889 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
1892 for (i = 0; i < l_image->numcomps; ++i) {
1893 /* TODO here with MCT ? */
1894 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
1897 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
1900 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
1906 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) {
1916 * Reads a SIZ marker (image and tile size)
1917 * @param p_header_data the data contained in the SIZ box.
1918 * @param jp2 the jpeg2000 file codec.
1919 * @param p_header_size the size of the data contained in the SIZ marker.
1920 * @param p_manager the user event manager.
1922 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
1923 OPJ_BYTE * p_header_data,
1924 OPJ_UINT32 p_header_size,
1925 opj_event_mgr_t * p_manager
1928 OPJ_UINT32 l_size, i;
1929 OPJ_UINT32 l_nb_comp;
1930 OPJ_UINT32 l_nb_comp_remain;
1931 OPJ_UINT32 l_remaining_size;
1932 OPJ_UINT32 l_nb_tiles;
1934 opj_image_t *l_image = 00;
1935 opj_cp_v2_t *l_cp = 00;
1936 opj_image_comp_t * l_img_comp = 00;
1937 opj_tcp_v2_t * l_current_tile_param = 00;
1940 assert(p_j2k != 00);
1941 assert(p_manager != 00);
1942 assert(p_header_data != 00);
1944 l_image = p_j2k->m_private_image;
1945 l_cp = &(p_j2k->m_cp);
1947 /* minimum size == 39 - 3 (= minimum component parameter) */
1948 if (p_header_size < 36) {
1949 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1953 l_remaining_size = p_header_size - 36;
1954 l_nb_comp = l_remaining_size / 3;
1955 l_nb_comp_remain = l_remaining_size % 3;
1956 if (l_nb_comp_remain != 0){
1957 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1961 l_size = p_header_size + 2; /* Lsiz */
1963 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1965 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1966 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1968 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1970 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1972 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1974 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1976 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1978 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1980 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1982 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1985 l_image->numcomps = (OPJ_UINT16) l_tmp;
1987 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1991 if (l_image->numcomps != l_nb_comp) {
1992 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);
1997 if (l_cp->correct) {
1998 /* if JPWL is on, we check whether TX errors have damaged
1999 too much the SIZ parameters */
2000 if (!(l_image->x1 * l_image->y1)) {
2001 opj_event_msg_v2(p_manager, EVT_ERROR,
2002 "JPWL: bad image size (%d x %d)\n",
2003 l_image->x1, l_image->y1);
2004 if (!JPWL_ASSUME || JPWL_ASSUME) {
2005 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2010 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2011 if (l_image->numcomps != ((len - 38) / 3)) {
2012 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2013 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2014 l_image->numcomps, ((len - 38) / 3));
2016 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2019 */ /* we try to correct */
2020 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2021 if (l_image->numcomps < ((len - 38) / 3)) {
2022 len = 38 + 3 * l_image->numcomps;
2023 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2026 l_image->numcomps = ((len - 38) / 3);
2027 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2033 /* update components number in the jpwl_exp_comps filed */
2034 l_cp->exp_comps = l_image->numcomps;
2036 #endif /* USE_JPWL */
2038 /* Allocate the resulting image components */
2039 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2040 if (l_image->comps == 00){
2041 l_image->numcomps = 0;
2042 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2046 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2047 l_img_comp = l_image->comps;
2049 /* Read the component information */
2050 for (i = 0; i < l_image->numcomps; ++i){
2052 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2054 l_img_comp->prec = (tmp & 0x7f) + 1;
2055 l_img_comp->sgnd = tmp >> 7;
2056 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2058 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2059 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2061 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2064 if (l_cp->correct) {
2065 /* if JPWL is on, we check whether TX errors have damaged
2066 too much the SIZ parameters, again */
2067 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2068 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2069 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2070 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2072 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2075 /* we try to correct */
2076 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2077 if (!l_image->comps[i].dx) {
2078 l_image->comps[i].dx = 1;
2079 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2080 i, l_image->comps[i].dx);
2082 if (!l_image->comps[i].dy) {
2083 l_image->comps[i].dy = 1;
2084 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2085 i, l_image->comps[i].dy);
2089 #endif /* USE_JPWL */
2090 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2091 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2095 /* Compute the number of tiles */
2096 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2097 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2098 l_nb_tiles = l_cp->tw * l_cp->th;
2100 /* Define the tiles which will be decoded */
2101 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2102 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;
2103 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;
2104 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);
2105 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);
2108 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2109 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2110 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2111 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2115 if (l_cp->correct) {
2116 /* if JPWL is on, we check whether TX errors have damaged
2117 too much the SIZ parameters */
2118 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2119 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2120 "JPWL: bad number of tiles (%d x %d)\n",
2121 l_cp->tw, l_cp->th);
2123 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2126 /* we try to correct */
2127 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2130 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2133 if (l_cp->tw > l_cp->max_tiles) {
2135 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2136 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2137 l_cp->max_tiles, l_cp->tw);
2141 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2144 if (l_cp->th > l_cp->max_tiles) {
2146 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2147 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2148 l_cp->max_tiles, l_cp->th);
2152 #endif /* USE_JPWL */
2154 /* memory allocations */
2155 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2156 if (l_cp->tcps == 00) {
2157 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2160 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2163 if (l_cp->correct) {
2165 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2166 "JPWL: could not alloc tcps field of cp\n");
2167 if (!JPWL_ASSUME || JPWL_ASSUME) {
2168 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2173 #endif /* USE_JPWL */
2175 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2176 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2177 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2178 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2181 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2183 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2184 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2186 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2187 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2190 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));
2191 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2193 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2194 (opj_simple_mcc_decorrelation_data_t*)
2195 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2197 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2198 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2201 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));
2202 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2204 /* set up default dc level shift */
2205 for (i=0;i<l_image->numcomps;++i) {
2206 if (! l_image->comps[i].sgnd) {
2207 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2211 l_current_tile_param = l_cp->tcps;
2212 for (i = 0; i < l_nb_tiles; ++i) {
2213 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2214 if (l_current_tile_param->tccps == 00) {
2215 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2218 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2220 ++l_current_tile_param;
2223 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2224 opj_image_comp_header_update(l_image,l_cp);
2230 * Writes the COM marker (comment)
2232 * @param p_stream the stream to write data to.
2233 * @param p_j2k J2K codec.
2234 * @param p_manager the user event manager.
2236 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2237 opj_stream_private_t *p_stream,
2238 opj_event_mgr_t * p_manager
2241 OPJ_UINT32 l_comment_size;
2242 OPJ_UINT32 l_total_com_size;
2243 const OPJ_CHAR *l_comment;
2244 OPJ_BYTE * l_current_ptr = 00;
2247 assert(p_j2k != 00);
2248 assert(p_stream != 00);
2249 assert(p_manager != 00);
2251 l_comment = p_j2k->m_cp.comment;
2252 l_comment_size = strlen(l_comment);
2253 l_total_com_size = l_comment_size + 6;
2255 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2256 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
2257 if (! new_header_tile_data) {
2258 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2259 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2260 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2261 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
2264 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2265 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2268 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2270 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2273 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2276 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2279 memcpy( l_current_ptr,l_comment,l_comment_size);
2281 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) {
2289 * Reads a COM marker (comments)
2290 * @param p_header_data the data contained in the COM box.
2291 * @param jp2 the jpeg2000 file codec.
2292 * @param p_header_size the size of the data contained in the COM marker.
2293 * @param p_manager the user event manager.
2295 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2296 OPJ_BYTE * p_header_data,
2297 OPJ_UINT32 p_header_size,
2298 opj_event_mgr_t * p_manager
2302 assert(p_j2k != 00);
2303 assert(p_manager != 00);
2304 assert(p_header_data != 00);
2305 (void)p_header_size;
2311 * Writes the COD marker (Coding style default)
2313 * @param p_stream the stream to write data to.
2314 * @param p_j2k J2K codec.
2315 * @param p_manager the user event manager.
2317 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2318 opj_stream_private_t *p_stream,
2319 opj_event_mgr_t * p_manager )
2321 opj_cp_v2_t *l_cp = 00;
2322 opj_tcp_v2_t *l_tcp = 00;
2323 OPJ_UINT32 l_code_size,l_remaining_size;
2324 OPJ_BYTE * l_current_data = 00;
2327 assert(p_j2k != 00);
2328 assert(p_manager != 00);
2329 assert(p_stream != 00);
2331 l_cp = &(p_j2k->m_cp);
2332 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2333 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2334 l_remaining_size = l_code_size;
2336 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2337 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2338 if (! new_header_tile_data) {
2339 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2340 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2341 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2342 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2345 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2346 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2349 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2351 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2352 l_current_data += 2;
2354 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2355 l_current_data += 2;
2357 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2360 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2363 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2366 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2369 l_remaining_size -= 9;
2371 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2372 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2376 if (l_remaining_size != 0) {
2377 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2381 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) {
2390 * Reads a COD marker (Coding Styke defaults)
2391 * @param p_header_data the data contained in the COD box.
2392 * @param p_j2k the jpeg2000 codec.
2393 * @param p_header_size the size of the data contained in the COD marker.
2394 * @param p_manager the user event manager.
2396 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2397 OPJ_BYTE * p_header_data,
2398 OPJ_UINT32 p_header_size,
2399 opj_event_mgr_t * p_manager
2405 opj_cp_v2_t *l_cp = 00;
2406 opj_tcp_v2_t *l_tcp = 00;
2407 opj_image_t *l_image = 00;
2410 assert(p_header_data != 00);
2411 assert(p_j2k != 00);
2412 assert(p_manager != 00);
2414 l_image = p_j2k->m_private_image;
2415 l_cp = &(p_j2k->m_cp);
2417 /* If we are in the first tile-part header of the current tile */
2418 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2419 &l_cp->tcps[p_j2k->m_current_tile_number] :
2420 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2422 /* Make sure room is sufficient */
2423 if (p_header_size < 5) {
2424 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2428 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2430 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2432 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2433 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2436 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2437 if (l_cp->m_specific_param.m_dec.m_layer) {
2438 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2441 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2444 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2448 for (i = 0; i < l_image->numcomps; ++i) {
2449 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2452 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2453 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2457 if (p_header_size != 0) {
2458 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2462 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2463 opj_j2k_copy_tile_component_parameters(p_j2k);
2466 #ifdef WIP_REMOVE_MSD
2467 if (p_j2k->cstr_info) {
2468 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2469 p_j2k->cstr_info->prog = l_tcp->prg;
2470 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2471 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2472 for (i = 0; i < l_image->numcomps; ++i) {
2473 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2482 * Writes the COC marker (Coding style component)
2484 * @param p_comp_no the index of the component to output.
2485 * @param p_stream the stream to write data to.
2486 * @param p_j2k J2K codec.
2487 * @param p_manager the user event manager.
2489 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2490 OPJ_UINT32 p_comp_no,
2491 opj_stream_private_t *p_stream,
2492 opj_event_mgr_t * p_manager )
2494 OPJ_UINT32 l_coc_size,l_remaining_size;
2495 OPJ_UINT32 l_comp_room;
2498 assert(p_j2k != 00);
2499 assert(p_manager != 00);
2500 assert(p_stream != 00);
2502 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2504 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2506 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2507 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2508 = (OPJ_BYTE*)opj_realloc(
2509 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2512 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2513 if (! new_header_tile_data) {
2514 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2515 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2516 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2517 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2520 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2521 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2524 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);
2526 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) {
2534 * Writes the COC marker (Coding style component)
2536 * @param p_comp_no the index of the component to output.
2537 * @param p_stream the stream to write data to.
2538 * @param p_j2k J2K codec.
2539 * @param p_manager the user event manager.
2541 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2542 OPJ_UINT32 p_comp_no,
2544 OPJ_UINT32 * p_data_written,
2545 opj_event_mgr_t * p_manager
2548 opj_cp_v2_t *l_cp = 00;
2549 opj_tcp_v2_t *l_tcp = 00;
2550 OPJ_UINT32 l_coc_size,l_remaining_size;
2551 OPJ_BYTE * l_current_data = 00;
2552 opj_image_t *l_image = 00;
2553 OPJ_UINT32 l_comp_room;
2556 assert(p_j2k != 00);
2557 assert(p_manager != 00);
2559 l_cp = &(p_j2k->m_cp);
2560 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2561 l_image = p_j2k->m_private_image;
2562 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2564 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2565 l_remaining_size = l_coc_size;
2567 l_current_data = p_data;
2569 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2570 l_current_data += 2;
2572 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2573 l_current_data += 2;
2575 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2576 l_current_data+=l_comp_room;
2578 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2581 l_remaining_size -= (5 + l_comp_room);
2582 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2583 * p_data_written = l_coc_size;
2587 * Gets the maximum size taken by a coc.
2589 * @param p_j2k the jpeg2000 codec to use.
2591 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2594 OPJ_UINT32 l_nb_comp;
2595 OPJ_UINT32 l_nb_tiles;
2596 OPJ_UINT32 l_max = 0;
2600 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2601 l_nb_comp = p_j2k->m_private_image->numcomps;
2603 for (i=0;i<l_nb_tiles;++i) {
2604 for (j=0;j<l_nb_comp;++j) {
2605 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2614 * Reads a COC marker (Coding Style Component)
2615 * @param p_header_data the data contained in the COC box.
2616 * @param p_j2k the jpeg2000 codec.
2617 * @param p_header_size the size of the data contained in the COC marker.
2618 * @param p_manager the user event manager.
2620 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2621 OPJ_BYTE * p_header_data,
2622 OPJ_UINT32 p_header_size,
2623 opj_event_mgr_t * p_manager
2626 opj_cp_v2_t *l_cp = NULL;
2627 opj_tcp_v2_t *l_tcp = NULL;
2628 opj_image_t *l_image = NULL;
2629 OPJ_UINT32 l_comp_room;
2630 OPJ_UINT32 l_comp_no;
2633 assert(p_header_data != 00);
2634 assert(p_j2k != 00);
2635 assert(p_manager != 00);
2637 l_cp = &(p_j2k->m_cp);
2638 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2639 &l_cp->tcps[p_j2k->m_current_tile_number] :
2640 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2641 l_image = p_j2k->m_private_image;
2643 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2645 /* make sure room is sufficient*/
2646 if (p_header_size < l_comp_room + 1) {
2647 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2650 p_header_size -= l_comp_room + 1;
2652 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2653 p_header_data += l_comp_room;
2654 if (l_comp_no >= l_image->numcomps) {
2655 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2659 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2662 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2663 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2667 if (p_header_size != 0) {
2668 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2675 * Writes the QCD marker (quantization default)
2677 * @param p_comp_number the index of the component to output.
2678 * @param p_stream the stream to write data to.
2679 * @param p_j2k J2K codec.
2680 * @param p_manager the user event manager.
2682 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2683 opj_stream_private_t *p_stream,
2684 opj_event_mgr_t * p_manager
2687 opj_cp_v2_t *l_cp = 00;
2688 opj_tcp_v2_t *l_tcp = 00;
2689 OPJ_UINT32 l_qcd_size,l_remaining_size;
2690 OPJ_BYTE * l_current_data = 00;
2693 assert(p_j2k != 00);
2694 assert(p_manager != 00);
2695 assert(p_stream != 00);
2697 l_cp = &(p_j2k->m_cp);
2698 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2699 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2700 l_remaining_size = l_qcd_size;
2702 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2703 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2704 if (! new_header_tile_data) {
2705 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2706 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2707 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2708 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2711 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2712 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2715 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2717 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2718 l_current_data += 2;
2720 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2721 l_current_data += 2;
2723 l_remaining_size -= 4;
2725 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2726 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2730 if (l_remaining_size != 0) {
2731 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2735 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) {
2743 * Reads a QCD marker (Quantization defaults)
2744 * @param p_header_data the data contained in the QCD box.
2745 * @param p_j2k the jpeg2000 codec.
2746 * @param p_header_size the size of the data contained in the QCD marker.
2747 * @param p_manager the user event manager.
2749 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2750 OPJ_BYTE * p_header_data,
2751 OPJ_UINT32 p_header_size,
2752 opj_event_mgr_t * p_manager
2756 assert(p_header_data != 00);
2757 assert(p_j2k != 00);
2758 assert(p_manager != 00);
2760 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2761 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2765 if (p_header_size != 0) {
2766 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2770 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2771 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2777 * Writes the QCC marker (quantization component)
2779 * @param p_comp_no the index of the component to output.
2780 * @param p_stream the stream to write data to.
2781 * @param p_j2k J2K codec.
2782 * @param p_manager the user event manager.
2784 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2785 OPJ_UINT32 p_comp_no,
2786 opj_stream_private_t *p_stream,
2787 opj_event_mgr_t * p_manager
2790 OPJ_UINT32 l_qcc_size,l_remaining_size;
2793 assert(p_j2k != 00);
2794 assert(p_manager != 00);
2795 assert(p_stream != 00);
2797 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2798 l_remaining_size = l_qcc_size;
2800 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2801 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
2802 if (! new_header_tile_data) {
2803 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2804 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2805 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2806 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
2809 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2810 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2813 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);
2815 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) {
2823 * Writes the QCC marker (quantization component)
2825 * @param p_comp_no the index of the component to output.
2826 * @param p_stream the stream to write data to.
2827 * @param p_j2k J2K codec.
2828 * @param p_manager the user event manager.
2830 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2831 OPJ_UINT32 p_comp_no,
2833 OPJ_UINT32 * p_data_written,
2834 opj_event_mgr_t * p_manager
2837 OPJ_UINT32 l_qcc_size,l_remaining_size;
2838 OPJ_BYTE * l_current_data = 00;
2841 assert(p_j2k != 00);
2842 assert(p_manager != 00);
2844 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2845 l_remaining_size = l_qcc_size;
2847 l_current_data = p_data;
2849 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2850 l_current_data += 2;
2852 if (p_j2k->m_private_image->numcomps <= 256) {
2855 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2856 l_current_data += 2;
2858 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2861 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2862 l_remaining_size -= 6;
2865 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2866 l_current_data += 2;
2868 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2871 l_remaining_size -= 6;
2874 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2876 *p_data_written = l_qcc_size;
2880 * Gets the maximum size taken by a qcc.
2882 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
2884 return opj_j2k_get_max_coc_size(p_j2k);
2888 * Reads a QCC marker (Quantization component)
2889 * @param p_header_data the data contained in the QCC box.
2890 * @param p_j2k the jpeg2000 codec.
2891 * @param p_header_size the size of the data contained in the QCC marker.
2892 * @param p_manager the user event manager.
2894 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
2895 OPJ_BYTE * p_header_data,
2896 OPJ_UINT32 p_header_size,
2897 opj_event_mgr_t * p_manager
2900 OPJ_UINT32 l_num_comp,l_comp_no;
2903 assert(p_header_data != 00);
2904 assert(p_j2k != 00);
2905 assert(p_manager != 00);
2907 l_num_comp = p_j2k->m_private_image->numcomps;
2909 if (l_num_comp <= 256) {
2910 if (p_header_size < 1) {
2911 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2914 opj_read_bytes(p_header_data,&l_comp_no,1);
2919 if (p_header_size < 2) {
2920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2923 opj_read_bytes(p_header_data,&l_comp_no,2);
2929 if (p_j2k->m_cp.correct) {
2931 static OPJ_UINT32 backup_compno = 0;
2933 /* compno is negative or larger than the number of components!!! */
2934 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2935 opj_event_msg_v2(p_manager, EVT_ERROR,
2936 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2937 l_comp_no, l_num_comp);
2939 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2942 /* we try to correct */
2943 l_comp_no = backup_compno % l_num_comp;
2944 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2945 "- setting component number to %d\n",
2949 /* keep your private count of tiles */
2952 #endif /* USE_JPWL */
2954 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2955 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2959 if (p_header_size != 0) {
2960 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2968 * Writes the POC marker (Progression Order Change)
2970 * @param p_stream the stream to write data to.
2971 * @param p_j2k J2K codec.
2972 * @param p_manager the user event manager.
2974 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
2975 opj_stream_private_t *p_stream,
2976 opj_event_mgr_t * p_manager
2979 OPJ_UINT32 l_nb_comp;
2980 OPJ_UINT32 l_nb_poc;
2981 OPJ_UINT32 l_poc_size;
2982 OPJ_UINT32 l_written_size = 0;
2983 opj_tcp_v2_t *l_tcp = 00;
2984 opj_tccp_t *l_tccp = 00;
2985 OPJ_UINT32 l_poc_room;
2988 assert(p_j2k != 00);
2989 assert(p_manager != 00);
2990 assert(p_stream != 00);
2992 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2993 l_tccp = &l_tcp->tccps[0];
2994 l_nb_comp = p_j2k->m_private_image->numcomps;
2995 l_nb_poc = 1 + l_tcp->numpocs;
2997 if (l_nb_comp <= 256) {
3003 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3005 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3006 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
3007 if (! new_header_tile_data) {
3008 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3009 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3010 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3011 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
3014 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3015 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3018 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3020 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) {
3029 * Writes the POC marker (Progression Order Change)
3031 * @param p_stream the stream to write data to.
3032 * @param p_j2k J2K codec.
3033 * @param p_manager the user event manager.
3035 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3037 OPJ_UINT32 * p_data_written,
3038 opj_event_mgr_t * p_manager
3042 OPJ_BYTE * l_current_data = 00;
3043 OPJ_UINT32 l_nb_comp;
3044 OPJ_UINT32 l_nb_poc;
3045 OPJ_UINT32 l_poc_size;
3046 opj_image_t *l_image = 00;
3047 opj_tcp_v2_t *l_tcp = 00;
3048 opj_tccp_t *l_tccp = 00;
3049 opj_poc_t *l_current_poc = 00;
3050 OPJ_UINT32 l_poc_room;
3053 assert(p_j2k != 00);
3054 assert(p_manager != 00);
3056 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3057 l_tccp = &l_tcp->tccps[0];
3058 l_image = p_j2k->m_private_image;
3059 l_nb_comp = l_image->numcomps;
3060 l_nb_poc = 1 + l_tcp->numpocs;
3062 if (l_nb_comp <= 256) {
3069 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3071 l_current_data = p_data;
3073 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3074 l_current_data += 2;
3076 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3077 l_current_data += 2;
3079 l_current_poc = l_tcp->pocs;
3080 for (i = 0; i < l_nb_poc; ++i) {
3081 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3084 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3085 l_current_data+=l_poc_room;
3087 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3090 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3093 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3094 l_current_data+=l_poc_room;
3096 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3099 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3100 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3101 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3102 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3107 *p_data_written = l_poc_size;
3111 * Gets the maximum size taken by the writing of a POC.
3113 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3115 opj_tcp_v2_t * l_tcp = 00;
3116 OPJ_UINT32 l_nb_tiles = 0;
3117 OPJ_UINT32 l_max_poc = 0;
3120 l_tcp = p_j2k->m_cp.tcps;
3121 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3123 for (i=0;i<l_nb_tiles;++i) {
3124 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3130 return 4 + 9 * l_max_poc;
3134 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3136 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3139 OPJ_UINT32 l_nb_tiles;
3140 OPJ_UINT32 l_max = 0;
3141 opj_tcp_v2_t * l_tcp = 00;
3143 l_tcp = p_j2k->m_cp.tcps;
3144 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3146 for (i=0;i<l_nb_tiles;++i) {
3147 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3157 * Gets the maximum size taken by the headers of the SOT.
3159 * @param p_j2k the jpeg2000 codec to use.
3161 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3163 OPJ_UINT32 l_nb_bytes = 0;
3164 OPJ_UINT32 l_nb_comps;
3165 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3167 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3168 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3170 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3171 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3172 l_nb_bytes += l_nb_comps * l_coc_bytes;
3174 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3175 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3178 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3180 /*** DEVELOPER CORNER, Add room for your headers ***/
3187 * Reads a POC marker (Progression Order Change)
3189 * @param p_header_data the data contained in the POC box.
3190 * @param p_j2k the jpeg2000 codec.
3191 * @param p_header_size the size of the data contained in the POC marker.
3192 * @param p_manager the user event manager.
3194 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3195 OPJ_BYTE * p_header_data,
3196 OPJ_UINT32 p_header_size,
3197 opj_event_mgr_t * p_manager
3200 OPJ_UINT32 i, l_nb_comp, l_tmp;
3201 opj_image_t * l_image = 00;
3202 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3203 OPJ_UINT32 l_chunk_size, l_comp_room;
3205 opj_cp_v2_t *l_cp = 00;
3206 opj_tcp_v2_t *l_tcp = 00;
3207 opj_poc_t *l_current_poc = 00;
3210 assert(p_header_data != 00);
3211 assert(p_j2k != 00);
3212 assert(p_manager != 00);
3214 l_image = p_j2k->m_private_image;
3215 l_nb_comp = l_image->numcomps;
3216 if (l_nb_comp <= 256) {
3222 l_chunk_size = 5 + 2 * l_comp_room;
3223 l_current_poc_nb = p_header_size / l_chunk_size;
3224 l_current_poc_remaining = p_header_size % l_chunk_size;
3226 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3227 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3231 l_cp = &(p_j2k->m_cp);
3232 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3233 &l_cp->tcps[p_j2k->m_current_tile_number] :
3234 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3235 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3236 l_current_poc_nb += l_old_poc_nb;
3238 assert(l_current_poc_nb < 32);
3240 /* now poc is in use.*/
3243 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3244 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3245 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3247 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3248 p_header_data+=l_comp_room;
3249 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3251 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3253 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3254 p_header_data+=l_comp_room;
3255 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3257 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3258 /* make sure comp is in acceptable bounds */
3259 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3263 l_tcp->numpocs = l_current_poc_nb - 1;
3268 * Reads a CRG marker (Component registration)
3270 * @param p_header_data the data contained in the TLM box.
3271 * @param p_j2k the jpeg2000 codec.
3272 * @param p_header_size the size of the data contained in the TLM marker.
3273 * @param p_manager the user event manager.
3275 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3276 OPJ_BYTE * p_header_data,
3277 OPJ_UINT32 p_header_size,
3278 opj_event_mgr_t * p_manager
3281 OPJ_UINT32 l_nb_comp;
3283 assert(p_header_data != 00);
3284 assert(p_j2k != 00);
3285 assert(p_manager != 00);
3287 l_nb_comp = p_j2k->m_private_image->numcomps;
3289 if (p_header_size != l_nb_comp *4) {
3290 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3293 /* Do not care of this at the moment since only local variables are set here */
3296 (i = 0; i < l_nb_comp; ++i)
3298 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3300 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3308 * Reads a TLM marker (Tile Length Marker)
3310 * @param p_header_data the data contained in the TLM box.
3311 * @param p_j2k the jpeg2000 codec.
3312 * @param p_header_size the size of the data contained in the TLM marker.
3313 * @param p_manager the user event manager.
3315 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3316 OPJ_BYTE * p_header_data,
3317 OPJ_UINT32 p_header_size,
3318 opj_event_mgr_t * p_manager
3321 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3323 assert(p_header_data != 00);
3324 assert(p_j2k != 00);
3325 assert(p_manager != 00);
3327 if (p_header_size < 2) {
3328 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3333 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3335 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3338 l_ST = ((l_Stlm >> 4) & 0x3);
3339 l_SP = (l_Stlm >> 6) & 0x1;
3341 l_Ptlm_size = (l_SP + 1) * 2;
3342 l_quotient = l_Ptlm_size + l_ST;
3344 l_tot_num_tp = p_header_size / l_quotient;
3345 l_tot_num_tp_remaining = p_header_size % l_quotient;
3347 if (l_tot_num_tp_remaining != 0) {
3348 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3351 /* FIXME Do not care of this at the moment since only local variables are set here */
3354 (i = 0; i < l_tot_num_tp; ++i)
3356 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3357 p_header_data += l_ST;
3358 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3359 p_header_data += l_Ptlm_size;
3366 * Reads a PLM marker (Packet length, main header marker)
3368 * @param p_header_data the data contained in the TLM box.
3369 * @param p_j2k the jpeg2000 codec.
3370 * @param p_header_size the size of the data contained in the TLM marker.
3371 * @param p_manager the user event manager.
3373 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3374 OPJ_BYTE * p_header_data,
3375 OPJ_UINT32 p_header_size,
3376 opj_event_mgr_t * p_manager
3380 assert(p_header_data != 00);
3381 assert(p_j2k != 00);
3382 assert(p_manager != 00);
3384 if (p_header_size < 1) {
3385 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3388 /* Do not care of this at the moment since only local variables are set here */
3390 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3397 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3399 p_header_size -= (1+l_Nplm);
3403 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3407 (i = 0; i < l_Nplm; ++i)
3409 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3411 // take only the last seven bytes
3412 l_packet_len |= (l_tmp & 0x7f);
3420 // store packet length and proceed to next packet
3427 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3436 * Reads a PLT marker (Packet length, tile-part header)
3438 * @param p_header_data the data contained in the PLT box.
3439 * @param p_j2k the jpeg2000 codec.
3440 * @param p_header_size the size of the data contained in the PLT marker.
3441 * @param p_manager the user event manager.
3443 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3444 OPJ_BYTE * p_header_data,
3445 OPJ_UINT32 p_header_size,
3446 opj_event_mgr_t * p_manager
3449 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3452 assert(p_header_data != 00);
3453 assert(p_j2k != 00);
3454 assert(p_manager != 00);
3456 if (p_header_size < 1) {
3457 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3461 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3465 for (i = 0; i < p_header_size; ++i) {
3466 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3468 /* take only the last seven bytes */
3469 l_packet_len |= (l_tmp & 0x7f);
3474 /* store packet length and proceed to next packet */
3479 if (l_packet_len != 0) {
3480 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3489 * Reads a PPM marker (Packed packet headers, main header)
3491 * @param p_header_data the data contained in the POC box.
3492 * @param p_j2k the jpeg2000 codec.
3493 * @param p_header_size the size of the data contained in the POC marker.
3494 * @param p_manager the user event manager.
3497 opj_bool j2k_read_ppm_v2 (
3498 opj_j2k_v2_t *p_j2k,
3499 OPJ_BYTE * p_header_data,
3500 OPJ_UINT32 p_header_size,
3501 struct opj_event_mgr * p_manager
3505 opj_cp_v2_t *l_cp = 00;
3506 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3509 assert(p_header_data != 00);
3510 assert(p_j2k != 00);
3511 assert(p_manager != 00);
3513 if (p_header_size < 1) {
3514 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3518 l_cp = &(p_j2k->m_cp);
3521 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3525 /* First PPM marker */
3527 if (p_header_size < 4) {
3528 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3532 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3536 /* First PPM marker: Initialization */
3537 l_cp->ppm_len = l_N_ppm;
3538 l_cp->ppm_data_size = 0;
3540 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3541 if (l_cp->ppm_buffer == 00) {
3542 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3545 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3547 l_cp->ppm_data = l_cp->ppm_buffer;
3551 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3552 if (p_header_size >= 4) {
3554 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3557 l_cp->ppm_len += l_N_ppm ;
3559 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3560 if (! new_ppm_buffer) {
3561 opj_free(l_cp->ppm_buffer);
3562 l_cp->ppm_buffer = NULL;
3564 l_cp->ppm_data = NULL;
3565 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3568 l_cp->ppm_buffer = new_ppm_buffer;
3569 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3570 l_cp->ppm_data = l_cp->ppm_buffer;
3577 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3579 if (l_remaining_data <= p_header_size) {
3580 /* we must store less information than available in the packet */
3581 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3582 l_cp->ppm_data_size = l_cp->ppm_len;
3583 p_header_size -= l_remaining_data;
3584 p_header_data += l_remaining_data;
3587 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3588 l_cp->ppm_data_size += p_header_size;
3589 p_header_data += p_header_size;
3602 * Reads a PPM marker (Packed packet headers, main header)
3604 * @param p_header_data the data contained in the POC box.
3605 * @param p_j2k the jpeg2000 codec.
3606 * @param p_header_size the size of the data contained in the POC marker.
3607 * @param p_manager the user event manager.
3609 opj_bool j2k_read_ppm_v3 (
3610 opj_j2k_v2_t *p_j2k,
3611 OPJ_BYTE * p_header_data,
3612 OPJ_UINT32 p_header_size,
3613 struct opj_event_mgr * p_manager
3616 opj_cp_v2_t *l_cp = 00;
3617 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3620 assert(p_header_data != 00);
3621 assert(p_j2k != 00);
3622 assert(p_manager != 00);
3624 /* Minimum size of PPM marker is equal to the size of Zppm element */
3625 if (p_header_size < 1) {
3626 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3630 l_cp = &(p_j2k->m_cp);
3633 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3637 /* First PPM marker */
3639 /* We need now at least the Nppm^0 element */
3640 if (p_header_size < 4) {
3641 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3645 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3649 /* First PPM marker: Initialization */
3650 l_cp->ppm_len = l_N_ppm;
3651 l_cp->ppm_data_read = 0;
3653 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3654 l_cp->ppm_buffer = l_cp->ppm_data;
3655 if (l_cp->ppm_data == 00) {
3656 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
3659 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3661 l_cp->ppm_data_current = l_cp->ppm_data;
3663 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3666 if (p_header_size < 4) {
3667 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3671 /* Uncompleted Ippm series in the previous PPM marker?*/
3672 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3673 /* Get the place where add the remaining Ippm series*/
3674 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3675 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3678 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3682 /* Increase the size of ppm_data to add the new Ippm series*/
3683 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3684 OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3685 if (! new_ppm_data) {
3686 opj_free(l_cp->ppm_data);
3687 l_cp->ppm_data = NULL;
3688 l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3690 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n");
3693 l_cp->ppm_data = new_ppm_data;
3694 l_cp->ppm_buffer = l_cp->ppm_data;
3696 /* Keep the position of the place where concatenate the new series*/
3697 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3698 l_cp->ppm_len += l_N_ppm;
3703 l_remaining_data = p_header_size;
3705 while (l_remaining_data >= l_N_ppm) {
3706 /* read a complete Ippm series*/
3707 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3708 p_header_size -= l_N_ppm;
3709 p_header_data += l_N_ppm;
3711 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3715 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3720 l_remaining_data = p_header_size;
3724 l_remaining_data = p_header_size;
3726 /* Next Ippm series is a complete series ?*/
3727 if (l_remaining_data > l_N_ppm) {
3728 /* Increase the size of ppm_data to add the new Ippm series*/
3729 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3730 OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3731 if (! new_ppm_data) {
3732 opj_free(l_cp->ppm_data);
3733 l_cp->ppm_data = NULL;
3734 l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3736 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");
3739 l_cp->ppm_data = new_ppm_data;
3740 l_cp->ppm_buffer = l_cp->ppm_data;
3742 /* Keep the position of the place where concatenate the new series */
3743 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3744 l_cp->ppm_len += l_N_ppm;
3749 /* Need to read an incomplete Ippm series*/
3750 if (l_remaining_data) {
3751 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3752 OPJ_BYTE *new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3753 if (! new_ppm_data) {
3754 opj_free(l_cp->ppm_data);
3755 l_cp->ppm_data = NULL;
3756 l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3758 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n");
3761 l_cp->ppm_data = new_ppm_data;
3762 l_cp->ppm_buffer = l_cp->ppm_data;
3764 /* Keep the position of the place where concatenate the new series*/
3765 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3766 l_cp->ppm_len += l_N_ppm;
3768 /* Read incomplete Ippm series*/
3769 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3770 p_header_size -= l_remaining_data;
3771 p_header_data += l_remaining_data;
3773 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3778 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3779 if (p_header_size >= 4) {
3781 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3784 l_cp->ppm_len += l_N_ppm ;
3786 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3787 if (! new_ppm_buffer) {
3788 opj_free(l_cp->ppm_buffer);
3789 l_cp->ppm_buffer = NULL;
3791 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
3794 l_cp->ppm_buffer = new_ppm_buffer;
3795 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3797 l_cp->ppm_data = l_cp->ppm_buffer;
3804 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3806 if (l_remaining_data <= p_header_size) {
3807 /* we must store less information than available in the packet */
3808 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3809 l_cp->ppm_data_size = l_cp->ppm_len;
3810 p_header_size -= l_remaining_data;
3811 p_header_data += l_remaining_data;
3814 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3815 l_cp->ppm_data_size += p_header_size;
3816 p_header_data += p_header_size;
3826 * Reads a PPT marker (Packed packet headers, tile-part header)
3828 * @param p_header_data the data contained in the PPT box.
3829 * @param p_j2k the jpeg2000 codec.
3830 * @param p_header_size the size of the data contained in the PPT marker.
3831 * @param p_manager the user event manager.
3833 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3834 OPJ_BYTE * p_header_data,
3835 OPJ_UINT32 p_header_size,
3836 opj_event_mgr_t * p_manager
3839 opj_cp_v2_t *l_cp = 00;
3840 opj_tcp_v2_t *l_tcp = 00;
3844 assert(p_header_data != 00);
3845 assert(p_j2k != 00);
3846 assert(p_manager != 00);
3848 /* We need to have the Z_ppt element at minimum */
3849 if (p_header_size < 1) {
3850 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3854 l_cp = &(p_j2k->m_cp);
3856 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");
3860 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3863 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3867 /* Allocate buffer to read the packet header */
3869 /* First PPT marker */
3870 l_tcp->ppt_data_size = 0;
3871 l_tcp->ppt_len = p_header_size;
3873 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3874 if (l_tcp->ppt_buffer == 00) {
3875 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3878 l_tcp->ppt_data = l_tcp->ppt_buffer;
3880 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3883 l_tcp->ppt_len += p_header_size;
3885 OPJ_BYTE *new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len);
3886 if (! new_ppt_buffer) {
3887 opj_free(l_tcp->ppt_buffer);
3888 l_tcp->ppt_buffer = NULL;
3890 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3893 l_tcp->ppt_buffer = new_ppt_buffer;
3894 l_tcp->ppt_data = l_tcp->ppt_buffer;
3896 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3899 /* Read packet header from buffer */
3900 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3902 l_tcp->ppt_data_size += p_header_size;
3908 * Writes the TLM marker (Tile Length Marker)
3910 * @param p_stream the stream to write data to.
3911 * @param p_j2k J2K codec.
3912 * @param p_manager the user event manager.
3914 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3915 opj_stream_private_t *p_stream,
3916 opj_event_mgr_t * p_manager
3919 OPJ_BYTE * l_current_data = 00;
3920 OPJ_UINT32 l_tlm_size;
3923 assert(p_j2k != 00);
3924 assert(p_manager != 00);
3925 assert(p_stream != 00);
3927 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3929 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3930 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
3931 if (! new_header_tile_data) {
3932 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3933 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3934 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3935 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
3938 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3939 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3942 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3944 /* change the way data is written to avoid seeking if possible */
3946 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
3948 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
3949 l_current_data += 2;
3951 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
3952 l_current_data += 2;
3954 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
3957 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3960 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
3961 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) {
3969 * Writes the SOT marker (Start of tile-part)
3971 * @param p_stream the stream to write data to.
3972 * @param p_j2k J2K codec.
3973 * @param p_manager the user event manager.
3975 opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
3977 OPJ_UINT32 * p_data_written,
3978 const opj_stream_private_t *p_stream,
3979 opj_event_mgr_t * p_manager
3983 assert(p_j2k != 00);
3984 assert(p_manager != 00);
3985 assert(p_stream != 00);
3987 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
3990 opj_write_bytes(p_data,10,2); /* Lsot */
3993 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
3999 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4002 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4007 /* update markers struct */
4009 opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4011 assert( 0 && "TODO" );
4012 #endif /* USE_JPWL */
4014 * p_data_written = 12;
4023 * Reads a PPT marker (Packed packet headers, tile-part header)
4025 * @param p_header_data the data contained in the PPT box.
4026 * @param p_j2k the jpeg2000 codec.
4027 * @param p_header_size the size of the data contained in the PPT marker.
4028 * @param p_manager the user event manager.
4030 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4031 OPJ_BYTE * p_header_data,
4032 OPJ_UINT32 p_header_size,
4033 opj_event_mgr_t * p_manager )
4035 opj_cp_v2_t *l_cp = 00;
4036 opj_tcp_v2_t *l_tcp = 00;
4037 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4038 OPJ_UINT32 l_current_part;
4039 OPJ_UINT32 l_tile_x,l_tile_y;
4042 assert(p_header_data != 00);
4043 assert(p_j2k != 00);
4044 assert(p_manager != 00);
4046 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4047 if (p_header_size != 8) {
4048 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4052 l_cp = &(p_j2k->m_cp);
4053 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4056 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4057 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4058 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4061 if (l_cp->correct) {
4063 OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
4064 static OPJ_UINT32 backup_tileno = 0;
4066 /* tileno is negative or larger than the number of tiles!!! */
4067 if (tileno > (l_cp->tw * l_cp->th)) {
4068 opj_event_msg_v2(p_manager, EVT_ERROR,
4069 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4070 tileno, (l_cp->tw * l_cp->th));
4072 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4075 /* we try to correct */
4076 tileno = backup_tileno;
4077 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4078 "- setting tile number to %d\n",
4082 /* keep your private count of tiles */
4085 #endif /* USE_JPWL */
4087 /* look for the tile in the list of already processed tile (in parts). */
4088 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4089 /* since the time taken by this function can only grow at the time */
4091 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4094 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4095 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4097 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4103 if (l_cp->correct) {
4105 /* totlen is negative or larger than the bytes left!!! */
4106 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))) { */
4107 opj_event_msg_v2(p_manager, EVT_ERROR,
4108 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4109 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4111 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4114 /* we try to correct */
4116 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4117 "- setting Psot to %d => assuming it is the last tile\n",
4121 #endif /* USE_JPWL */
4123 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4125 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4126 "we assuming it is the last tile-part of the codestream.\n");
4127 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4130 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4133 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4136 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4137 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4138 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4139 if (l_tcp->m_nb_tile_parts) {
4140 if (l_current_part >= l_tcp->m_nb_tile_parts){
4141 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4142 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4143 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4147 l_tcp->m_nb_tile_parts = l_num_parts;
4150 /* If know the number of tile part header we will check if we didn't read the last*/
4151 if (l_tcp->m_nb_tile_parts) {
4152 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4153 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4157 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4158 /* Keep the size of data to skip after this marker */
4159 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4162 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4163 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4166 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4168 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4169 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4170 p_j2k->m_specific_param.m_decoder.m_skip_data =
4171 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4172 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4173 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4174 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4177 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4178 p_j2k->m_specific_param.m_decoder.m_skip_data =
4179 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4183 if (p_j2k->cstr_index)
4185 assert(p_j2k->cstr_index->tile_index != 00);
4186 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4187 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4189 if (l_num_parts != 0){
4190 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4191 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4194 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4195 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4196 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4199 opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4200 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t));
4201 if (! new_tp_index) {
4202 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4203 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4204 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4207 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4211 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4213 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4214 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4215 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4216 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4217 sizeof(opj_tp_index_t));
4220 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4221 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4222 opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4223 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4224 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));
4225 if (! new_tp_index) {
4226 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4227 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4228 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4229 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4232 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4241 /* 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 */
4242 /* if (p_j2k->cstr_info) {
4245 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4248 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4249 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4250 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4251 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4254 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4257 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4261 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4264 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4265 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4266 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4272 * Writes the SOD marker (Start of data)
4274 * @param p_stream the stream to write data to.
4275 * @param p_j2k J2K codec.
4276 * @param p_manager the user event manager.
4278 opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
4279 opj_tcd_v2_t * p_tile_coder,
4281 OPJ_UINT32 * p_data_written,
4282 OPJ_UINT32 p_total_data_size,
4283 const opj_stream_private_t *p_stream,
4284 opj_event_mgr_t * p_manager
4287 opj_tcp_v2_t *l_tcp = 00;
4288 opj_codestream_info_t *l_cstr_info = 00;
4289 opj_cp_v2_t *l_cp = 00;
4291 OPJ_UINT32 l_size_tile;
4292 OPJ_UINT32 l_remaining_data;
4295 assert(p_j2k != 00);
4296 assert(p_manager != 00);
4297 assert(p_stream != 00);
4299 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4302 /* make room for the EOF marker */
4303 l_remaining_data = p_total_data_size - 4;
4305 l_cp = &(p_j2k->m_cp);
4306 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4309 /* update tile coder */
4310 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4311 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4313 l_size_tile = l_cp->th * l_cp->tw;
4316 /* TODO mergeV2: check this part which use cstr_info */
4317 /*l_cstr_info = p_j2k->cstr_info;
4319 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4320 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4321 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4327 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4329 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4334 /* update markers struct */
4335 /*opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4337 assert( 0 && "TODO" );
4338 #endif /* USE_JPWL */
4343 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4344 p_tile_coder->tcd_image->tiles->packno = 0;
4346 l_cstr_info->packno = 0;
4350 *p_data_written = 0;
4352 if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
4353 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4357 *p_data_written += 2;
4366 * Reads a SOD marker (Start Of Data)
4368 * @param p_header_data the data contained in the SOD box.
4369 * @param p_j2k the jpeg2000 codec.
4370 * @param p_header_size the size of the data contained in the SOD marker.
4371 * @param p_manager the user event manager.
4373 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4374 opj_stream_private_t *p_stream,
4375 opj_event_mgr_t * p_manager
4378 OPJ_UINT32 l_current_read_size;
4379 opj_codestream_index_t * l_cstr_index = 00;
4380 OPJ_BYTE ** l_current_data = 00;
4381 opj_tcp_v2_t * l_tcp = 00;
4382 OPJ_UINT32 * l_tile_len = 00;
4385 assert(p_j2k != 00);
4386 assert(p_manager != 00);
4387 assert(p_stream != 00);
4389 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4391 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4392 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4393 // but we are in the last tile part,
4394 // so its result will fit on OPJ_UINT32 unless we find
4395 // a file with a single tile part of more than 4 GB...*/
4396 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4399 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4401 l_current_data = &(l_tcp->m_data);
4402 l_tile_len = &l_tcp->m_data_size;
4404 if (! *l_current_data) {
4405 /* LH: oddly enough, in this path, l_tile_len!=0.
4406 * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
4408 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4411 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4412 if (! l_new_current_data) {
4413 opj_free(*l_current_data);
4414 // nothing more is done as l_current_data will be set to null, and just
4415 // afterward we enter in the error path
4416 // and the actual tile_len is updated (committed) at the end of the
4419 *l_current_data = l_new_current_data;
4422 if (*l_current_data == 00) {
4423 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
4429 l_cstr_index = p_j2k->cstr_index;
4431 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4433 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4434 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4436 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4437 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4439 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
4443 p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
4444 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
4448 /*l_cstr_index->packno = 0;*/
4451 l_current_read_size = opj_stream_read_data(
4453 *l_current_data + *l_tile_len,
4454 p_j2k->m_specific_param.m_decoder.m_sot_length,
4457 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4458 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4461 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4464 *l_tile_len += l_current_read_size;
4470 * Writes the RGN marker (Region Of Interest)
4472 * @param p_tile_no the tile to output
4473 * @param p_comp_no the component to output
4474 * @param p_stream the stream to write data to.
4475 * @param p_j2k J2K codec.
4476 * @param p_manager the user event manager.
4478 opj_bool opj_j2k_write_rgn(opj_j2k_v2_t *p_j2k,
4479 OPJ_UINT32 p_tile_no,
4480 OPJ_UINT32 p_comp_no,
4481 opj_stream_private_t *p_stream,
4482 opj_event_mgr_t * p_manager
4485 OPJ_BYTE * l_current_data = 00;
4486 OPJ_UINT32 l_nb_comp;
4487 OPJ_UINT32 l_rgn_size;
4488 opj_image_t *l_image = 00;
4489 opj_cp_v2_t *l_cp = 00;
4490 opj_tcp_v2_t *l_tcp = 00;
4491 opj_tccp_t *l_tccp = 00;
4492 OPJ_UINT32 l_comp_room;
4495 assert(p_j2k != 00);
4496 assert(p_manager != 00);
4497 assert(p_stream != 00);
4499 l_cp = &(p_j2k->m_cp);
4500 l_tcp = &l_cp->tcps[p_tile_no];
4501 l_tccp = &l_tcp->tccps[p_comp_no];
4503 l_nb_comp = l_image->numcomps;
4505 if (l_nb_comp <= 256) {
4512 l_rgn_size = 6 + l_comp_room;
4514 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4516 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4517 l_current_data += 2;
4519 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4520 l_current_data += 2;
4522 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4523 l_current_data+=l_comp_room;
4525 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4528 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4531 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) {
4539 * Writes the EOC marker (End of Codestream)
4541 * @param p_stream the stream to write data to.
4542 * @param p_j2k J2K codec.
4543 * @param p_manager the user event manager.
4545 opj_bool opj_j2k_write_eoc( opj_j2k_v2_t *p_j2k,
4546 opj_stream_private_t *p_stream,
4547 opj_event_mgr_t * p_manager
4551 assert(p_j2k != 00);
4552 assert(p_manager != 00);
4553 assert(p_stream != 00);
4555 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4560 /* update markers struct */
4562 opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4564 #endif /* USE_JPWL */
4566 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4570 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4579 * Reads a RGN marker (Region Of Interest)
4581 * @param p_header_data the data contained in the POC box.
4582 * @param p_j2k the jpeg2000 codec.
4583 * @param p_header_size the size of the data contained in the POC marker.
4584 * @param p_manager the user event manager.
4586 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4587 OPJ_BYTE * p_header_data,
4588 OPJ_UINT32 p_header_size,
4589 opj_event_mgr_t * p_manager
4592 OPJ_UINT32 l_nb_comp;
4593 opj_image_t * l_image = 00;
4595 opj_cp_v2_t *l_cp = 00;
4596 opj_tcp_v2_t *l_tcp = 00;
4597 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4600 assert(p_header_data != 00);
4601 assert(p_j2k != 00);
4602 assert(p_manager != 00);
4604 l_image = p_j2k->m_private_image;
4605 l_nb_comp = l_image->numcomps;
4607 if (l_nb_comp <= 256) {
4612 if (p_header_size != 2 + l_comp_room) {
4613 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4617 l_cp = &(p_j2k->m_cp);
4618 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4619 &l_cp->tcps[p_j2k->m_current_tile_number] :
4620 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4622 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4623 p_header_data+=l_comp_room;
4624 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4628 if (l_cp->correct) {
4629 /* totlen is negative or larger than the bytes left!!! */
4630 if (l_comp_room >= l_nb_comp) {
4631 opj_event_msg_v2(p_manager, EVT_ERROR,
4632 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4633 l_comp_room, l_nb_comp);
4634 if (!JPWL_ASSUME || JPWL_ASSUME) {
4635 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4640 #endif /* USE_JPWL */
4642 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4649 OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_v2_t * p_tcp)
4651 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4654 OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_v2_t * p_tcp)
4661 * Updates the rates of the tcp.
4663 * @param p_stream the stream to write data to.
4664 * @param p_j2k J2K codec.
4665 * @param p_manager the user event manager.
4667 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4668 opj_stream_private_t *p_stream,
4669 opj_event_mgr_t * p_manager )
4671 opj_cp_v2_t * l_cp = 00;
4672 opj_image_t * l_image = 00;
4673 opj_tcp_v2_t * l_tcp = 00;
4674 opj_image_comp_t * l_img_comp = 00;
4677 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4678 OPJ_FLOAT32 * l_rates = 0;
4679 OPJ_FLOAT32 l_sot_remove;
4680 OPJ_UINT32 l_bits_empty, l_size_pixel;
4681 OPJ_UINT32 l_tile_size = 0;
4682 OPJ_UINT32 l_last_res;
4683 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4686 assert(p_j2k != 00);
4687 assert(p_manager != 00);
4688 assert(p_stream != 00);
4691 l_cp = &(p_j2k->m_cp);
4692 l_image = p_j2k->m_private_image;
4695 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4696 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4697 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4699 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4700 l_tp_stride_func = opj_j2k_get_tp_stride;
4703 l_tp_stride_func = opj_j2k_get_default_stride;
4706 for (i=0;i<l_cp->th;++i) {
4707 for (j=0;j<l_cp->tw;++j) {
4708 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4710 /* 4 borders of the tile rescale on the image if necessary */
4711 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4712 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4713 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4714 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4716 l_rates = l_tcp->rates;
4718 /* Modification of the RATE >> */
4720 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4722 ((*l_rates) * l_bits_empty)
4730 for (k = 1; k < l_tcp->numlayers; ++k) {
4732 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4734 ((*l_rates) * l_bits_empty)
4750 for (i=0;i<l_cp->th;++i) {
4751 for (j=0;j<l_cp->tw;++j) {
4752 l_rates = l_tcp->rates;
4755 *l_rates -= l_sot_remove;
4757 if (*l_rates < 30) {
4764 l_last_res = l_tcp->numlayers - 1;
4766 for (k = 1; k < l_last_res; ++k) {
4769 *l_rates -= l_sot_remove;
4771 if (*l_rates < *(l_rates - 1) + 10) {
4772 *l_rates = (*(l_rates - 1)) + 20;
4780 *l_rates -= (l_sot_remove + 2.f);
4782 if (*l_rates < *(l_rates - 1) + 10) {
4783 *l_rates = (*(l_rates - 1)) + 20;
4791 l_img_comp = l_image->comps;
4794 for (i=0;i<l_image->numcomps;++i) {
4795 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4797 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4805 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4807 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4809 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4810 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4811 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4812 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4816 if (l_cp->m_specific_param.m_enc.m_cinema) {
4817 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4818 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4819 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4823 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4824 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4832 * Reads a EOC marker (End Of Codestream)
4834 * @param p_header_data the data contained in the SOD box.
4835 * @param p_j2k the jpeg2000 codec.
4836 * @param p_header_size the size of the data contained in the SOD marker.
4837 * @param p_manager the user event manager.
4839 opj_bool opj_j2k_read_eoc ( opj_j2k_v2_t *p_j2k,
4840 opj_stream_private_t *p_stream,
4841 opj_event_mgr_t * p_manager )
4844 opj_tcd_v2_t * l_tcd = 00;
4845 OPJ_UINT32 l_nb_tiles;
4846 opj_tcp_v2_t * l_tcp = 00;
4850 assert(p_j2k != 00);
4851 assert(p_manager != 00);
4852 assert(p_stream != 00);
4854 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4855 l_tcp = p_j2k->m_cp.tcps;
4857 l_tcd = opj_tcd_create(OPJ_TRUE);
4859 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4863 for (i = 0; i < l_nb_tiles; ++i) {
4864 if (l_tcp->m_data) {
4865 if (! opj_tcd_init_decode_tile(l_tcd, i)) {
4866 opj_tcd_destroy(l_tcd);
4867 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4871 l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4875 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4880 opj_j2k_tcp_destroy(l_tcp);
4884 opj_tcd_destroy(l_tcd);
4889 * Gets the offset of the header.
4891 * @param p_stream the stream to write data to.
4892 * @param p_j2k J2K codec.
4893 * @param p_manager the user event manager.
4895 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
4896 struct opj_stream_private *p_stream,
4897 struct opj_event_mgr * p_manager )
4900 assert(p_j2k != 00);
4901 assert(p_manager != 00);
4902 assert(p_stream != 00);
4904 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4910 * Writes the MCT marker (Multiple Component Transform)
4912 * @param p_stream the stream to write data to.
4913 * @param p_j2k J2K codec.
4914 * @param p_manager the user event manager.
4916 opj_bool opj_j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
4917 struct opj_stream_private *p_stream,
4918 struct opj_event_mgr * p_manager )
4921 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4922 opj_mct_data_t * l_mct_record;
4923 opj_tcp_v2_t * l_tcp;
4926 assert(p_j2k != 00);
4927 assert(p_stream != 00);
4928 assert(p_manager != 00);
4930 if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
4934 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4935 l_mct_record = l_tcp->m_mct_records;
4937 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4939 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
4946 l_mcc_record = l_tcp->m_mcc_records;
4948 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
4950 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
4957 if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
4965 * Writes the image components.
4967 * @param p_stream the stream to write data to.
4968 * @param p_j2k J2K codec.
4969 * @param p_manager the user event manager.
4971 opj_bool opj_j2k_write_image_components(opj_j2k_v2_t *p_j2k,
4972 struct opj_stream_private *p_stream,
4973 struct opj_event_mgr * p_manager )
4978 assert(p_j2k != 00);
4979 assert(p_manager != 00);
4980 assert(p_stream != 00);
4982 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
4984 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
4988 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
4997 * Writes regions of interests.
4999 * @param p_stream the stream to write data to.
5000 * @param p_j2k J2K codec.
5001 * @param p_manager the user event manager.
5003 opj_bool opj_j2k_write_regions( opj_j2k_v2_t *p_j2k,
5004 struct opj_stream_private *p_stream,
5005 struct opj_event_mgr * p_manager )
5008 const opj_tccp_t *l_tccp = 00;
5011 assert(p_j2k != 00);
5012 assert(p_manager != 00);
5013 assert(p_stream != 00);
5015 l_tccp = p_j2k->m_cp.tcps->tccps;
5017 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5018 if (l_tccp->roishift) {
5020 if (! opj_j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) {
5034 * @param p_stream the stream to write data to.
5035 * @param p_j2k J2K codec.
5036 * @param p_manager the user event manager.
5038 opj_bool opj_j2k_write_epc( opj_j2k_v2_t *p_j2k,
5039 struct opj_stream_private *p_stream,
5040 struct opj_event_mgr * p_manager )
5042 opj_codestream_index_t * l_cstr_index = 00;
5045 assert(p_j2k != 00);
5046 assert(p_manager != 00);
5047 assert(p_stream != 00);
5049 l_cstr_index = p_j2k->cstr_index;
5051 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5053 /* The following adjustment is done to adjust the codestream size */
5054 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5055 /* the first bunch of bytes is not in the codestream */
5056 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5061 /* preparation of JPWL marker segments */
5065 /* encode according to JPWL */
5066 jpwl_encode(p_j2k, p_stream, image);
5070 assert( 0 && "TODO" );
5071 #endif /* USE_JPWL */
5081 * Reads an unknown marker
5083 * @param p_stream the stream object to read from.
5084 * @param p_j2k the jpeg2000 codec.
5085 * @param p_manager the user event manager.
5087 * @return true if the marker could be deduced.
5089 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5090 opj_stream_private_t *p_stream,
5091 OPJ_UINT32 *output_marker,
5092 opj_event_mgr_t * p_manager
5095 OPJ_UINT32 l_unknown_marker;
5096 const opj_dec_memory_marker_handler_t * l_marker_handler;
5097 OPJ_UINT32 l_size_unk = 2;
5100 assert(p_j2k != 00);
5101 assert(p_manager != 00);
5102 assert(p_stream != 00);
5104 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5107 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5108 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5109 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5113 /* read 2 bytes as the new marker ID*/
5114 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5116 if (!(l_unknown_marker < 0xff00)) {
5118 /* Get the marker handler from the marker ID*/
5119 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5121 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5122 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5126 if (l_marker_handler->id != J2K_MS_UNK) {
5127 /* Add the marker to the codestream index*/
5128 if (l_marker_handler->id != J2K_MS_SOT)
5130 opj_bool res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
5131 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5133 if (res == OPJ_FALSE) {
5134 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
5138 break; /* next marker is known and well located */
5146 *output_marker = l_marker_handler->id ;
5152 * Writes the MCT marker (Multiple Component Transform)
5154 * @param p_stream the stream to write data to.
5155 * @param p_j2k J2K codec.
5156 * @param p_manager the user event manager.
5158 opj_bool opj_j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5159 opj_mct_data_t * p_mct_record,
5160 struct opj_stream_private *p_stream,
5161 struct opj_event_mgr * p_manager )
5163 OPJ_UINT32 l_mct_size;
5164 OPJ_BYTE * l_current_data = 00;
5168 assert(p_j2k != 00);
5169 assert(p_manager != 00);
5170 assert(p_stream != 00);
5172 l_mct_size = 10 + p_mct_record->m_data_size;
5174 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5175 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
5176 if (! new_header_tile_data) {
5177 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5178 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5179 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5180 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
5183 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5184 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5187 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5189 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5190 l_current_data += 2;
5192 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5193 l_current_data += 2;
5195 opj_write_bytes(l_current_data,0,2); /* Zmct */
5196 l_current_data += 2;
5198 /* only one marker atm */
5199 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5201 opj_write_bytes(l_current_data,l_tmp,2);
5202 l_current_data += 2;
5204 opj_write_bytes(l_current_data,0,2); /* Ymct */
5207 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5209 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) {
5217 * Reads a MCT marker (Multiple Component Transform)
5219 * @param p_header_data the data contained in the MCT box.
5220 * @param p_j2k the jpeg2000 codec.
5221 * @param p_header_size the size of the data contained in the MCT marker.
5222 * @param p_manager the user event manager.
5224 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5225 OPJ_BYTE * p_header_data,
5226 OPJ_UINT32 p_header_size,
5227 opj_event_mgr_t * p_manager
5231 opj_tcp_v2_t *l_tcp = 00;
5234 opj_mct_data_t * l_mct_data;
5237 assert(p_header_data != 00);
5238 assert(p_j2k != 00);
5240 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5241 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5242 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5244 if (p_header_size < 2) {
5245 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5250 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5253 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5257 if(p_header_size <= 6) {
5258 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5262 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5263 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5266 l_indix = l_tmp & 0xff;
5267 l_mct_data = l_tcp->m_mct_records;
5269 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5270 if (l_mct_data->m_index == l_indix) {
5277 if (i == l_tcp->m_nb_mct_records) {
5278 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5279 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5281 opj_mct_data_t *new_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));
5282 if (! new_mct_records) {
5283 opj_free(l_tcp->m_mct_records);
5284 l_tcp->m_mct_records = NULL;
5285 l_tcp->m_nb_max_mct_records = 0;
5286 l_tcp->m_nb_mct_records = 0;
5287 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
5290 l_tcp->m_mct_records = new_mct_records;
5291 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5292 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5295 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5298 if (l_mct_data->m_data) {
5299 opj_free(l_mct_data->m_data);
5300 l_mct_data->m_data = 00;
5303 l_mct_data->m_index = l_indix;
5304 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5305 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5307 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5310 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5316 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5317 if (! l_mct_data->m_data) {
5318 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5321 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5323 l_mct_data->m_data_size = p_header_size;
5324 ++l_tcp->m_nb_mct_records;
5330 * Writes the MCC marker (Multiple Component Collection)
5332 * @param p_stream the stream to write data to.
5333 * @param p_j2k J2K codec.
5334 * @param p_manager the user event manager.
5336 opj_bool opj_j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5337 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5338 struct opj_stream_private *p_stream,
5339 struct opj_event_mgr * p_manager )
5342 OPJ_UINT32 l_mcc_size;
5343 OPJ_BYTE * l_current_data = 00;
5344 OPJ_UINT32 l_nb_bytes_for_comp;
5349 assert(p_j2k != 00);
5350 assert(p_manager != 00);
5351 assert(p_stream != 00);
5353 if (p_mcc_record->m_nb_comps > 255 ) {
5354 l_nb_bytes_for_comp = 2;
5358 l_nb_bytes_for_comp = 1;
5362 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5363 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5365 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
5366 if (! new_header_tile_data) {
5367 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5368 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5369 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5370 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
5373 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5374 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5377 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5379 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5380 l_current_data += 2;
5382 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5383 l_current_data += 2;
5386 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5387 l_current_data += 2;
5389 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5392 /* only one marker atm */
5393 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5396 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5399 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5402 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 */
5405 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5406 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5407 l_current_data+=l_nb_bytes_for_comp;
5410 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 */
5413 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5415 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5416 l_current_data+=l_nb_bytes_for_comp;
5419 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5421 if (p_mcc_record->m_decorrelation_array) {
5422 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5425 if (p_mcc_record->m_offset_array) {
5426 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5429 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5432 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) {
5440 * Reads a MCC marker (Multiple Component Collection)
5442 * @param p_header_data the data contained in the MCC box.
5443 * @param p_j2k the jpeg2000 codec.
5444 * @param p_header_size the size of the data contained in the MCC marker.
5445 * @param p_manager the user event manager.
5447 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5448 OPJ_BYTE * p_header_data,
5449 OPJ_UINT32 p_header_size,
5450 opj_event_mgr_t * p_manager )
5455 opj_tcp_v2_t * l_tcp;
5456 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5457 opj_mct_data_t * l_mct_data;
5458 OPJ_UINT32 l_nb_collections;
5459 OPJ_UINT32 l_nb_comps;
5460 OPJ_UINT32 l_nb_bytes_by_comp;
5464 assert(p_header_data != 00);
5465 assert(p_j2k != 00);
5466 assert(p_manager != 00);
5468 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5469 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5470 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5472 if (p_header_size < 2) {
5473 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5478 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5481 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5485 if (p_header_size < 7) {
5486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5490 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5493 l_mcc_record = l_tcp->m_mcc_records;
5495 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5496 if (l_mcc_record->m_index == l_indix) {
5503 if (i == l_tcp->m_nb_mcc_records) {
5504 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5505 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5507 opj_simple_mcc_decorrelation_data_t *new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
5508 l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5509 if (! new_mcc_records) {
5510 opj_free(l_tcp->m_mcc_records);
5511 l_tcp->m_mcc_records = NULL;
5512 l_tcp->m_nb_max_mcc_records = 0;
5513 l_tcp->m_nb_mcc_records = 0;
5514 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
5517 l_tcp->m_mcc_records = new_mcc_records;
5518 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5519 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));
5521 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5523 l_mcc_record->m_index = l_indix;
5525 /* only one marker atm */
5526 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5529 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5533 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5536 if (l_nb_collections > 1) {
5537 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5543 for (i=0;i<l_nb_collections;++i) {
5544 if (p_header_size < 3) {
5545 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5549 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5553 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5557 opj_read_bytes(p_header_data,&l_nb_comps,2);
5562 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5563 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5565 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5566 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5570 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5572 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5573 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5574 p_header_data+=l_nb_bytes_by_comp;
5577 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5582 opj_read_bytes(p_header_data,&l_nb_comps,2);
5585 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5586 l_nb_comps &= 0x7fff;
5588 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5589 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5593 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5594 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5598 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5600 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5601 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5602 p_header_data+=l_nb_bytes_by_comp;
5605 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5610 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5613 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5614 l_mcc_record->m_decorrelation_array = 00;
5615 l_mcc_record->m_offset_array = 00;
5617 l_indix = l_tmp & 0xff;
5619 l_mct_data = l_tcp->m_mct_records;
5620 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5621 if (l_mct_data->m_index == l_indix) {
5622 l_mcc_record->m_decorrelation_array = l_mct_data;
5628 if (l_mcc_record->m_decorrelation_array == 00) {
5629 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5634 l_indix = (l_tmp >> 8) & 0xff;
5636 l_mct_data = l_tcp->m_mct_records;
5637 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5638 if (l_mct_data->m_index == l_indix) {
5639 l_mcc_record->m_offset_array = l_mct_data;
5645 if (l_mcc_record->m_offset_array == 00) {
5646 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5652 if (p_header_size != 0) {
5653 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5657 ++l_tcp->m_nb_mcc_records;
5664 * Writes the MCO marker (Multiple component transformation ordering)
5666 * @param p_stream the stream to write data to.
5667 * @param p_j2k J2K codec.
5668 * @param p_manager the user event manager.
5670 opj_bool opj_j2k_write_mco( opj_j2k_v2_t *p_j2k,
5671 struct opj_stream_private *p_stream,
5672 struct opj_event_mgr * p_manager
5675 OPJ_BYTE * l_current_data = 00;
5676 OPJ_UINT32 l_mco_size;
5677 opj_tcp_v2_t * l_tcp = 00;
5678 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5682 assert(p_j2k != 00);
5683 assert(p_manager != 00);
5684 assert(p_stream != 00);
5686 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5687 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5689 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5690 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5692 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
5693 if (! new_header_tile_data) {
5694 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5695 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5696 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5697 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
5700 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5701 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5704 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5705 l_current_data += 2;
5707 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5708 l_current_data += 2;
5710 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5713 l_mcc_record = l_tcp->m_mcc_records;
5714 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5715 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/
5721 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) {
5729 * Reads a MCO marker (Multiple Component Transform Ordering)
5731 * @param p_header_data the data contained in the MCO box.
5732 * @param p_j2k the jpeg2000 codec.
5733 * @param p_header_size the size of the data contained in the MCO marker.
5734 * @param p_manager the user event manager.
5736 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5737 OPJ_BYTE * p_header_data,
5738 OPJ_UINT32 p_header_size,
5739 opj_event_mgr_t * p_manager
5742 OPJ_UINT32 l_tmp, i;
5743 OPJ_UINT32 l_nb_stages;
5744 opj_tcp_v2_t * l_tcp;
5745 opj_tccp_t * l_tccp;
5746 opj_image_t * l_image;
5747 opj_image_comp_t * l_img_comp;
5750 assert(p_header_data != 00);
5751 assert(p_j2k != 00);
5752 assert(p_manager != 00);
5754 l_image = p_j2k->m_private_image;
5755 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5756 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5757 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5759 if (p_header_size < 1) {
5760 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5764 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
5767 if (l_nb_stages > 1) {
5768 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5772 if (p_header_size != l_nb_stages + 1) {
5773 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5777 l_tccp = l_tcp->tccps;
5778 l_img_comp = l_image->comps;
5780 for (i=0;i<l_image->numcomps;++i) {
5781 l_tccp->m_dc_level_shift = 0;
5785 if (l_tcp->m_mct_decoding_matrix) {
5786 opj_free(l_tcp->m_mct_decoding_matrix);
5787 l_tcp->m_mct_decoding_matrix = 00;
5790 for (i=0;i<l_nb_stages;++i) {
5791 opj_read_bytes(p_header_data,&l_tmp,1);
5794 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5802 opj_bool opj_j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5805 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5806 opj_mct_data_t * l_deco_array, * l_offset_array;
5807 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5808 OPJ_UINT32 l_nb_elem;
5809 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5810 opj_tccp_t * l_tccp;
5813 assert(p_tcp != 00);
5815 l_mcc_record = p_tcp->m_mcc_records;
5817 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5818 if (l_mcc_record->m_index == p_index) {
5823 if (i==p_tcp->m_nb_mcc_records) {
5824 /** element discarded **/
5828 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5829 /** do not support number of comps != image */
5833 l_deco_array = l_mcc_record->m_decorrelation_array;
5836 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5837 if (l_deco_array->m_data_size != l_data_size) {
5841 l_nb_elem = p_image->numcomps * p_image->numcomps;
5842 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5843 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5845 if (! p_tcp->m_mct_decoding_matrix ) {
5849 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);
5852 l_offset_array = l_mcc_record->m_offset_array;
5854 if (l_offset_array) {
5855 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5856 if (l_offset_array->m_data_size != l_data_size) {
5860 l_nb_elem = p_image->numcomps;
5861 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
5862 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5864 if (! l_offset_data ) {
5868 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5870 l_tccp = p_tcp->tccps;
5871 l_current_offset_data = l_offset_data;
5873 for (i=0;i<p_image->numcomps;++i) {
5874 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
5878 opj_free(l_offset_data);
5885 * Writes the CBD marker (Component bit depth definition)
5887 * @param p_stream the stream to write data to.
5888 * @param p_j2k J2K codec.
5889 * @param p_manager the user event manager.
5891 opj_bool opj_j2k_write_cbd( opj_j2k_v2_t *p_j2k,
5892 struct opj_stream_private *p_stream,
5893 struct opj_event_mgr * p_manager )
5896 OPJ_UINT32 l_cbd_size;
5897 OPJ_BYTE * l_current_data = 00;
5898 opj_image_t *l_image = 00;
5899 opj_image_comp_t * l_comp = 00;
5902 assert(p_j2k != 00);
5903 assert(p_manager != 00);
5904 assert(p_stream != 00);
5906 l_image = p_j2k->m_private_image;
5907 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
5909 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5910 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
5911 if (! new_header_tile_data) {
5912 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5913 p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5914 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5915 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
5918 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5919 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
5922 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5924 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
5925 l_current_data += 2;
5927 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
5928 l_current_data += 2;
5930 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
5933 l_comp = l_image->comps;
5935 for (i=0;i<l_image->numcomps;++i) {
5936 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
5942 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) {
5950 * Reads a CBD marker (Component bit depth definition)
5951 * @param p_header_data the data contained in the CBD box.
5952 * @param p_j2k the jpeg2000 codec.
5953 * @param p_header_size the size of the data contained in the CBD marker.
5954 * @param p_manager the user event manager.
5956 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
5957 OPJ_BYTE * p_header_data,
5958 OPJ_UINT32 p_header_size,
5959 opj_event_mgr_t * p_manager
5962 OPJ_UINT32 l_nb_comp,l_num_comp;
5963 OPJ_UINT32 l_comp_def;
5965 opj_image_comp_t * l_comp = 00;
5968 assert(p_header_data != 00);
5969 assert(p_j2k != 00);
5970 assert(p_manager != 00);
5972 l_num_comp = p_j2k->m_private_image->numcomps;
5974 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
5975 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5979 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
5982 if (l_nb_comp != l_num_comp) {
5983 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5987 l_comp = p_j2k->m_private_image->comps;
5988 for (i=0;i<l_num_comp;++i) {
5989 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
5991 l_comp->sgnd = (l_comp_def>>7) & 1;
5992 l_comp->prec = (l_comp_def&0x7f) + 1;
6000 /* ----------------------------------------------------------------------- */
6001 /* J2K / JPT decoder interface */
6002 /* ----------------------------------------------------------------------- */
6004 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6006 if(j2k && parameters) {
6007 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6008 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6011 j2k->m_cp.correct = parameters->jpwl_correct;
6012 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6013 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6014 #endif /* USE_JPWL */
6019 /* ----------------------------------------------------------------------- */
6020 /* J2K encoder interface */
6021 /* ----------------------------------------------------------------------- */
6023 opj_j2k_v2_t* opj_j2k_create_compress(void)
6025 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6030 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6032 l_j2k->m_is_decoder = 0;
6033 l_j2k->m_cp.m_is_decoder = 0;
6035 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6036 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6037 opj_j2k_destroy(l_j2k);
6041 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6043 /* validation list creation*/
6044 l_j2k->m_validation_list = opj_procedure_list_create();
6045 if (! l_j2k->m_validation_list) {
6046 opj_j2k_destroy(l_j2k);
6050 /* execution list creation*/
6051 l_j2k->m_procedure_list = opj_procedure_list_create();
6052 if (! l_j2k->m_procedure_list) {
6053 opj_j2k_destroy(l_j2k);
6061 void opj_j2k_setup_encoder( opj_j2k_v2_t *p_j2k,
6062 opj_cparameters_t *parameters,
6064 opj_event_mgr_t * p_manager)
6066 OPJ_UINT32 i, j, tileno, numpocs_tile;
6067 opj_cp_v2_t *cp = 00;
6070 if(!p_j2k || !parameters || ! image) {
6074 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6075 cp = &(p_j2k->m_cp);
6077 /* set default values for cp */
6082 copy user encoding parameters
6084 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6085 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6086 cp->rsiz = parameters->cp_rsiz;
6087 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6088 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6089 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6091 /* mod fixed_quality */
6092 if (parameters->cp_matrice) {
6093 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6094 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6095 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6099 cp->tdx = parameters->cp_tdx;
6100 cp->tdy = parameters->cp_tdy;
6103 cp->tx0 = parameters->cp_tx0;
6104 cp->ty0 = parameters->cp_ty0;
6106 /* comment string */
6107 if(parameters->cp_comment) {
6108 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6110 strcpy(cp->comment, parameters->cp_comment);
6115 calculate other encoding parameters
6118 if (parameters->tile_size_on) {
6119 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6120 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6122 cp->tdx = image->x1 - cp->tx0;
6123 cp->tdy = image->y1 - cp->ty0;
6126 if (parameters->tp_on) {
6127 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6128 cp->m_specific_param.m_enc.m_tp_on = 1;
6133 calculate JPWL encoding parameters
6136 if (parameters->jpwl_epc_on) {
6140 cp->epc_on = OPJ_TRUE;
6141 cp->info_on = OPJ_FALSE; /* no informative technique */
6144 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6145 cp->epb_on = OPJ_TRUE;
6147 cp->hprot_MH = parameters->jpwl_hprot_MH;
6148 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6149 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6150 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6152 /* if tile specs are not specified, copy MH specs */
6153 if (cp->hprot_TPH[0] == -1) {
6154 cp->hprot_TPH_tileno[0] = 0;
6155 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6157 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6158 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6159 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6160 cp->pprot[i] = parameters->jpwl_pprot[i];
6164 /* set ESD writing */
6165 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6166 cp->esd_on = OPJ_TRUE;
6168 cp->sens_size = parameters->jpwl_sens_size;
6169 cp->sens_addr = parameters->jpwl_sens_addr;
6170 cp->sens_range = parameters->jpwl_sens_range;
6172 cp->sens_MH = parameters->jpwl_sens_MH;
6173 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6174 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6175 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6179 /* always set RED writing to false: we are at the encoder */
6180 cp->red_on = OPJ_FALSE;
6183 cp->epc_on = OPJ_FALSE;
6185 #endif /* USE_JPWL */
6188 /* initialize the mutiple tiles */
6189 /* ---------------------------- */
6190 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6191 if (parameters->numpocs) {
6192 /* initialisation of POC */
6193 l_res = opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6197 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6198 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6199 tcp->numlayers = parameters->tcp_numlayers;
6201 for (j = 0; j < tcp->numlayers; j++) {
6202 if(cp->m_specific_param.m_enc.m_cinema){
6203 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6204 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6206 tcp->rates[j] = parameters->tcp_rates[j];
6208 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6209 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6211 tcp->rates[j] = parameters->tcp_rates[j];
6216 tcp->csty = parameters->csty;
6217 tcp->prg = parameters->prog_order;
6218 tcp->mct = parameters->tcp_mct;
6223 if (parameters->numpocs) {
6224 /* initialisation of POC */
6227 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6228 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6229 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6231 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6232 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6233 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6234 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6235 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6236 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6237 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6243 tcp->numpocs = numpocs_tile -1 ;
6248 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6250 if (parameters->mct_data) {
6252 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6255 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6256 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6257 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6258 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6261 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6262 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6263 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6265 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6266 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6268 tcp->mct_norms = (OPJ_FLOAT64*)
6269 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6271 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6274 for (i = 0; i < image->numcomps; i++) {
6275 opj_tccp_t *tccp = &tcp->tccps[i];
6276 tccp->m_dc_level_shift = l_dc_shift[i];
6279 opj_j2k_setup_mct_encoding(tcp,image);
6283 for (i = 0; i < image->numcomps; i++) {
6284 opj_tccp_t *tccp = &tcp->tccps[i];
6285 opj_image_comp_t * l_comp = &(image->comps[i]);
6287 if (! l_comp->sgnd) {
6288 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6293 for (i = 0; i < image->numcomps; i++) {
6294 opj_tccp_t *tccp = &tcp->tccps[i];
6296 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6297 tccp->numresolutions = parameters->numresolution;
6298 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6299 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6300 tccp->cblksty = parameters->mode;
6301 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6302 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6305 if (i == parameters->roi_compno) {
6306 tccp->roishift = parameters->roi_shift;
6311 if(parameters->cp_cinema) {
6312 //Precinct size for lowest frequency subband=128
6315 //Precinct size at all other resolutions = 256
6316 for (j = 1; j < tccp->numresolutions; j++) {
6321 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6322 OPJ_INT32 p = 0, it_res;
6323 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6324 if (p < parameters->res_spec) {
6326 if (parameters->prcw_init[p] < 1) {
6327 tccp->prcw[it_res] = 1;
6329 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6332 if (parameters->prch_init[p] < 1) {
6333 tccp->prch[it_res] = 1;
6335 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6339 int res_spec = parameters->res_spec;
6340 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6341 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6343 if (size_prcw < 1) {
6344 tccp->prcw[it_res] = 1;
6346 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6349 if (size_prch < 1) {
6350 tccp->prch[it_res] = 1;
6352 tccp->prch[it_res] = int_floorlog2(size_prch);
6356 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6359 for (j = 0; j < tccp->numresolutions; j++) {
6366 opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6370 if (parameters->mct_data) {
6371 opj_free(parameters->mct_data);
6372 parameters->mct_data = 00;
6378 static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6380 assert(cstr_index != 00);
6382 /* expand the list? */
6383 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6384 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6385 opj_marker_info_t *new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6387 opj_free(cstr_index->marker);
6388 cstr_index->marker = NULL;
6389 cstr_index->maxmarknum = 0;
6390 cstr_index->marknum = 0;
6391 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
6394 cstr_index->marker = new_marker;
6397 /* add the marker */
6398 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6399 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6400 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6401 cstr_index->marknum++;
6405 static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6407 assert(cstr_index != 00);
6408 assert(cstr_index->tile_index != 00);
6410 /* expand the list? */
6411 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6412 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6413 opj_marker_info_t *new_marker = (opj_marker_info_t *) opj_realloc(
6414 cstr_index->tile_index[tileno].marker,
6415 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6417 opj_free(cstr_index->tile_index[tileno].marker);
6418 cstr_index->tile_index[tileno].marker = NULL;
6419 cstr_index->tile_index[tileno].maxmarknum = 0;
6420 cstr_index->tile_index[tileno].marknum = 0;
6421 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
6424 cstr_index->tile_index[tileno].marker = new_marker;
6427 /* add the marker */
6428 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6429 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6430 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6431 cstr_index->tile_index[tileno].marknum++;
6433 if (type == J2K_MS_SOT) {
6434 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6436 if (cstr_index->tile_index[tileno].tp_index)
6437 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6445 * -----------------------------------------------------------------------
6446 * -----------------------------------------------------------------------
6447 * -----------------------------------------------------------------------
6451 * Ends the decompression procedures and possibiliy add data to be read after the
6454 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
6455 opj_stream_private_t *p_stream,
6456 opj_event_mgr_t * p_manager
6466 * Reads a jpeg2000 codestream header structure.
6469 * @param p_stream the stream to read data from.
6470 * @param p_j2k the jpeg2000 codec.
6471 * @param p_manager the user event manager.
6473 * @return true if the box is valid.
6475 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
6476 opj_j2k_v2_t* p_j2k,
6477 opj_image_t** p_image,
6478 opj_event_mgr_t* p_manager )
6481 assert(p_j2k != 00);
6482 assert(p_stream != 00);
6483 assert(p_manager != 00);
6485 /* create an empty image header */
6486 p_j2k->m_private_image = opj_image_create0();
6487 if (! p_j2k->m_private_image) {
6491 /* customization of the validation */
6492 opj_j2k_setup_decoding_validation(p_j2k);
6494 /* validation of the parameters codec */
6495 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6496 opj_image_destroy(p_j2k->m_private_image);
6497 p_j2k->m_private_image = NULL;
6501 /* customization of the encoding */
6502 opj_j2k_setup_header_reading(p_j2k);
6505 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6506 opj_image_destroy(p_j2k->m_private_image);
6507 p_j2k->m_private_image = NULL;
6511 *p_image = opj_image_create0();
6516 /* Copy codestream image information to the output image */
6517 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6519 /*Allocate and initialize some elements of codestrem index*/
6520 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6528 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
6530 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
6533 assert(p_j2k != 00);
6535 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6537 /* DEVELOPER CORNER, add your custom procedures */
6538 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6543 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
6544 * are valid. Developpers wanting to extend the library can add their own validation procedures.
6546 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
6549 assert(p_j2k != 00);
6551 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6552 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6553 /* DEVELOPER CORNER, add your custom validation procedure */
6559 * The mct encoding validation procedure.
6561 * @param p_j2k the jpeg2000 codec to validate.
6562 * @param p_stream the input stream to validate.
6563 * @param p_manager the user event manager.
6565 * @return true if the parameters are correct.
6567 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
6568 opj_stream_private_t *p_stream,
6569 opj_event_mgr_t * p_manager )
6571 opj_bool l_is_valid = OPJ_TRUE;
6575 assert(p_j2k != 00);
6576 assert(p_stream != 00);
6577 assert(p_manager != 00);
6579 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6580 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6581 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
6583 for (i=0;i<l_nb_tiles;++i) {
6584 if (l_tcp->mct == 2) {
6585 opj_tccp_t * l_tccp = l_tcp->tccps;
6586 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6588 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
6589 l_is_valid &= ! (l_tccp->qmfbid & 1);
6600 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
6603 OPJ_UINT32 l_indix = 1;
6604 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
6605 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
6606 OPJ_UINT32 l_mct_size,l_nb_elem;
6607 OPJ_FLOAT32 * l_data, * l_current_data;
6608 opj_tccp_t * l_tccp;
6611 assert(p_tcp != 00);
6613 if (p_tcp->mct != 2) {
6617 if (p_tcp->m_mct_decoding_matrix) {
6618 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6619 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6621 opj_mct_data_t *new_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));
6622 if (! new_mct_records) {
6623 opj_free(p_tcp->m_mct_records);
6624 p_tcp->m_mct_records = NULL;
6625 p_tcp->m_nb_max_mct_records = 0;
6626 p_tcp->m_nb_mct_records = 0;
6627 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6630 p_tcp->m_mct_records = new_mct_records;
6631 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6633 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6635 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6637 if (l_mct_deco_data->m_data) {
6638 opj_free(l_mct_deco_data->m_data);
6639 l_mct_deco_data->m_data = 00;
6642 l_mct_deco_data->m_index = l_indix++;
6643 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
6644 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
6645 l_nb_elem = p_image->numcomps * p_image->numcomps;
6646 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
6647 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6649 if (! l_mct_deco_data->m_data) {
6653 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);
6655 l_mct_deco_data->m_data_size = l_mct_size;
6656 ++p_tcp->m_nb_mct_records;
6659 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6660 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6661 opj_mct_data_t *new_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));
6662 if (! new_mct_records) {
6663 opj_free(p_tcp->m_mct_records);
6664 p_tcp->m_mct_records = NULL;
6665 p_tcp->m_nb_max_mct_records = 0;
6666 p_tcp->m_nb_mct_records = 0;
6667 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6670 p_tcp->m_mct_records = new_mct_records;
6671 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6673 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6675 if (l_mct_deco_data) {
6676 l_mct_deco_data = l_mct_offset_data - 1;
6680 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6682 if (l_mct_offset_data->m_data) {
6683 opj_free(l_mct_offset_data->m_data);
6684 l_mct_offset_data->m_data = 00;
6687 l_mct_offset_data->m_index = l_indix++;
6688 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
6689 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
6690 l_nb_elem = p_image->numcomps;
6691 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
6692 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6694 if (! l_mct_offset_data->m_data) {
6698 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
6700 opj_free(l_mct_offset_data->m_data);
6701 l_mct_offset_data->m_data = 00;
6705 l_tccp = p_tcp->tccps;
6706 l_current_data = l_data;
6708 for (i=0;i<l_nb_elem;++i) {
6709 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
6713 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
6717 l_mct_offset_data->m_data_size = l_mct_size;
6719 ++p_tcp->m_nb_mct_records;
6721 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
6722 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
6723 opj_simple_mcc_decorrelation_data_t *new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
6724 p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6725 if (! new_mcc_records) {
6726 opj_free(p_tcp->m_mcc_records);
6727 p_tcp->m_mcc_records = NULL;
6728 p_tcp->m_nb_max_mcc_records = 0;
6729 p_tcp->m_nb_mcc_records = 0;
6730 /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6733 p_tcp->m_mcc_records = new_mcc_records;
6734 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6735 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));
6739 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6740 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
6741 l_mcc_data->m_is_irreversible = 1;
6742 l_mcc_data->m_nb_comps = p_image->numcomps;
6743 l_mcc_data->m_index = l_indix++;
6744 l_mcc_data->m_offset_array = l_mct_offset_data;
6745 ++p_tcp->m_nb_mcc_records;
6751 * Builds the cp decoder parameters to use to decode tile.
6753 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
6754 opj_stream_private_t *p_stream,
6755 opj_event_mgr_t * p_manager )
6757 /* add here initialization of cp
6758 copy paste of setup_decoder */
6766 * Builds the cp encoder parameters to use to encode tile.
6768 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
6769 opj_stream_private_t *p_stream,
6770 opj_event_mgr_t * p_manager )
6772 /* add here initialization of cp
6773 copy paste of setup_encoder */
6781 * The default encoding validation procedure without any extension.
6783 * @param p_j2k the jpeg2000 codec to validate.
6784 * @param p_stream the input stream to validate.
6785 * @param p_manager the user event manager.
6787 * @return true if the parameters are correct.
6789 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
6790 opj_stream_private_t *p_stream,
6791 opj_event_mgr_t * p_manager )
6793 opj_bool l_is_valid = OPJ_TRUE;
6796 assert(p_j2k != 00);
6797 assert(p_stream != 00);
6798 assert(p_manager != 00);
6800 /* STATE checking */
6801 /* make sure the state is at 0 */
6802 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
6804 /* POINTER validation */
6805 /* make sure a p_j2k codec is present */
6806 l_is_valid &= (p_j2k->m_procedure_list != 00);
6807 /* make sure a validation list is present */
6808 l_is_valid &= (p_j2k->m_validation_list != 00);
6810 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6811 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6815 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6816 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6820 /* PARAMETER VALIDATION */
6825 * The default decoding validation procedure without any extension.
6827 * @param p_j2k the jpeg2000 codec to validate.
6828 * @param p_stream the input stream to validate.
6829 * @param p_manager the user event manager.
6831 * @return true if the parameters are correct.
6833 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
6834 opj_stream_private_t *p_stream,
6835 opj_event_mgr_t * p_manager
6838 opj_bool l_is_valid = OPJ_TRUE;
6841 assert(p_j2k != 00);
6842 assert(p_stream != 00);
6843 assert(p_manager != 00);
6846 /* STATE checking */
6847 /* make sure the state is at 0 */
6849 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
6851 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
6853 /* POINTER validation */
6854 /* make sure a p_j2k codec is present */
6855 /* make sure a procedure list is present */
6856 l_is_valid &= (p_j2k->m_procedure_list != 00);
6857 /* make sure a validation list is present */
6858 l_is_valid &= (p_j2k->m_validation_list != 00);
6860 /* PARAMETER VALIDATION */
6864 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
6865 opj_stream_private_t *p_stream,
6866 opj_event_mgr_t * p_manager)
6868 OPJ_UINT32 l_current_marker;
6869 OPJ_UINT32 l_marker_size;
6870 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6873 assert(p_stream != 00);
6874 assert(p_j2k != 00);
6875 assert(p_manager != 00);
6877 /* We enter in the main header */
6878 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6880 /* Try to read the SOC marker, the codestream must begin with SOC marker */
6881 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
6882 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6886 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6887 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6888 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6892 /* Read 2 bytes as the new marker ID */
6893 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6895 /* Try to read until the SOT is detected */
6896 while (l_current_marker != J2K_MS_SOT) {
6898 /* Check if the current marker ID is valid */
6899 if (l_current_marker < 0xff00) {
6900 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6904 /* Get the marker handler from the marker ID */
6905 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6907 /* Manage case where marker is unknown */
6908 if (l_marker_handler->id == J2K_MS_UNK) {
6909 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
6910 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6914 if (l_current_marker == J2K_MS_SOT)
6915 break; /* SOT marker is detected main header is completely read */
6916 else /* Get the marker handler from the marker ID */
6917 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6920 /* Check if the marker is known and if it is the right place to find it */
6921 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6922 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6926 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6927 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6928 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6932 /* read 2 bytes as the marker size */
6933 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6934 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6936 /* Check if the marker size is compatible with the header data size */
6937 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6938 OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
6939 if (! new_header_data) {
6940 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6941 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
6942 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6943 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read header\n");
6946 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
6947 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6950 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6951 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) {
6952 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6956 /* Read the marker segment with the correct marker handler */
6957 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6958 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6962 /* Add the marker to the codestream index*/
6963 if (OPJ_FALSE == opj_j2k_add_mhmarker(
6965 l_marker_handler->id,
6966 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6967 l_marker_size + 4 )) {
6968 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
6972 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6973 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6974 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6978 /* read 2 bytes as the new marker ID */
6979 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6982 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
6984 /* Position of the last element if the main header */
6985 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6987 /* Next step: read a tile-part header */
6988 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6994 * Excutes the given procedures on the given codec.
6996 * @param p_procedure_list the list of procedures to execute
6997 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6998 * @param p_stream the stream to execute the procedures on.
6999 * @param p_manager the user manager.
7001 * @return true if all the procedures were successfully executed.
7003 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7004 opj_procedure_list_t * p_procedure_list,
7005 opj_stream_private_t *p_stream,
7006 opj_event_mgr_t * p_manager )
7008 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7009 opj_bool l_result = OPJ_TRUE;
7010 OPJ_UINT32 l_nb_proc, i;
7013 assert(p_procedure_list != 00);
7014 assert(p_j2k != 00);
7015 assert(p_stream != 00);
7016 assert(p_manager != 00);
7019 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7020 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);
7022 for (i=0;i<l_nb_proc;++i) {
7023 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7027 /* and clear the procedure list at the end.*/
7028 opj_procedure_list_clear(p_procedure_list);
7033 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7034 opj_stream_private_t *p_stream,
7035 opj_event_mgr_t * p_manager
7038 opj_tcp_v2_t * l_tcp = 00;
7039 opj_tcp_v2_t * l_default_tcp = 00;
7040 OPJ_UINT32 l_nb_tiles;
7042 opj_tccp_t *l_current_tccp = 00;
7043 OPJ_UINT32 l_tccp_size;
7044 OPJ_UINT32 l_mct_size;
7045 opj_image_t * l_image;
7046 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7047 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7048 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7049 OPJ_UINT32 l_offset;
7052 assert(p_j2k != 00);
7053 assert(p_stream != 00);
7054 assert(p_manager != 00);
7056 l_image = p_j2k->m_private_image;
7057 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7058 l_tcp = p_j2k->m_cp.tcps;
7059 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7060 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7061 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7064 for (i=0; i<l_nb_tiles; ++i) {
7065 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7066 l_current_tccp = l_tcp->tccps;
7067 /*Copy default coding parameters into the current tile coding parameters*/
7068 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7069 /* Initialize some values of the current tile coding parameters*/
7071 l_tcp->ppt_data = 00;
7072 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7073 l_tcp->tccps = l_current_tccp;
7075 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7076 if (l_default_tcp->m_mct_decoding_matrix) {
7077 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7078 if (! l_tcp->m_mct_decoding_matrix ) {
7081 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7084 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7085 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7086 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7087 if (! l_tcp->m_mct_records) {
7090 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7092 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7093 l_src_mct_rec = l_default_tcp->m_mct_records;
7094 l_dest_mct_rec = l_tcp->m_mct_records;
7096 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7098 if (l_src_mct_rec->m_data) {
7100 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7101 if(! l_dest_mct_rec->m_data) {
7104 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7111 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7112 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7113 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7114 if (! l_tcp->m_mcc_records) {
7117 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7119 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7120 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7121 l_dest_mcc_rec = l_tcp->m_mcc_records;
7123 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7125 if (l_src_mcc_rec->m_decorrelation_array) {
7126 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7127 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7130 if (l_src_mcc_rec->m_offset_array) {
7131 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7132 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7139 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7140 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7142 /* Move to next tile cp*/
7146 /* Create the current tile decoder*/
7147 p_j2k->m_tcd = (opj_tcd_v2_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */
7148 if (! p_j2k->m_tcd ) {
7152 if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7153 opj_tcd_destroy(p_j2k->m_tcd);
7155 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7163 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7164 * with the marker value.
7165 * @param p_id Marker value to look up
7167 * @return the handler associated with the id.
7169 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7171 const opj_dec_memory_marker_handler_t *e;
7172 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7173 if (e->id == p_id) {
7174 break; /* we find a handler corresponding to the marker ID*/
7182 * Destroys a jpeg2000 codec.
7184 * @param p_j2k the jpeg20000 structure to destroy.
7186 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7192 if (p_j2k->m_is_decoder) {
7194 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7195 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7196 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7197 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7200 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7201 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7202 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7203 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7208 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7209 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7210 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7213 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7214 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7215 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7216 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7219 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7220 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7221 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7222 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7226 opj_tcd_destroy(p_j2k->m_tcd);
7228 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7229 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7231 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7232 p_j2k->m_procedure_list = 00;
7234 opj_procedure_list_destroy(p_j2k->m_validation_list);
7235 p_j2k->m_procedure_list = 00;
7237 j2k_destroy_cstr_index(p_j2k->cstr_index);
7238 p_j2k->cstr_index = NULL;
7240 opj_image_destroy(p_j2k->m_private_image);
7241 p_j2k->m_private_image = NULL;
7243 opj_image_destroy(p_j2k->m_output_image);
7244 p_j2k->m_output_image = NULL;
7249 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7253 if (p_cstr_ind->marker) {
7254 opj_free(p_cstr_ind->marker);
7255 p_cstr_ind->marker = NULL;
7258 if (p_cstr_ind->tile_index) {
7259 OPJ_UINT32 it_tile = 0;
7261 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7263 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7264 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7265 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7268 if(p_cstr_ind->tile_index[it_tile].tp_index){
7269 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7270 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7273 if(p_cstr_ind->tile_index[it_tile].marker){
7274 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7275 p_cstr_ind->tile_index[it_tile].marker = NULL;
7280 opj_free( p_cstr_ind->tile_index);
7281 p_cstr_ind->tile_index = NULL;
7284 opj_free(p_cstr_ind);
7291 * Destroys a tile coding parameter structure.
7293 * @param p_tcp the tile coding parameter to destroy.
7295 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7301 if (p_tcp->ppt_buffer != 00) {
7302 opj_free(p_tcp->ppt_buffer);
7303 p_tcp->ppt_buffer = 00;
7306 if (p_tcp->tccps != 00) {
7307 opj_free(p_tcp->tccps);
7311 if (p_tcp->m_mct_coding_matrix != 00) {
7312 opj_free(p_tcp->m_mct_coding_matrix);
7313 p_tcp->m_mct_coding_matrix = 00;
7316 if (p_tcp->m_mct_decoding_matrix != 00) {
7317 opj_free(p_tcp->m_mct_decoding_matrix);
7318 p_tcp->m_mct_decoding_matrix = 00;
7321 if (p_tcp->m_mcc_records) {
7322 opj_free(p_tcp->m_mcc_records);
7323 p_tcp->m_mcc_records = 00;
7324 p_tcp->m_nb_max_mcc_records = 0;
7325 p_tcp->m_nb_mcc_records = 0;
7328 if (p_tcp->m_mct_records) {
7329 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7332 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7333 if (l_mct_data->m_data) {
7334 opj_free(l_mct_data->m_data);
7335 l_mct_data->m_data = 00;
7341 opj_free(p_tcp->m_mct_records);
7342 p_tcp->m_mct_records = 00;
7345 if (p_tcp->mct_norms != 00) {
7346 opj_free(p_tcp->mct_norms);
7347 p_tcp->mct_norms = 00;
7350 opj_j2k_tcp_data_destroy(p_tcp);
7355 * Destroys the data inside a tile coding parameter structure.
7357 * @param p_tcp the tile coding parameter which contain data to destroy.
7359 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7361 if (p_tcp->m_data) {
7362 opj_free(p_tcp->m_data);
7363 p_tcp->m_data = NULL;
7364 p_tcp->m_data_size = 0;
7369 * Destroys a coding parameter structure.
7371 * @param p_cp the coding parameter to destroy.
7373 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7375 OPJ_UINT32 l_nb_tiles;
7376 opj_tcp_v2_t * l_current_tile = 00;
7383 if (p_cp->tcps != 00)
7385 l_current_tile = p_cp->tcps;
7386 l_nb_tiles = p_cp->th * p_cp->tw;
7388 for (i = 0; i < l_nb_tiles; ++i)
7390 opj_j2k_tcp_destroy(l_current_tile);
7393 opj_free(p_cp->tcps);
7396 opj_free(p_cp->ppm_buffer);
7397 p_cp->ppm_buffer = 00;
7398 p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
7399 opj_free(p_cp->comment);
7401 if (! p_cp->m_is_decoder)
7403 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7404 p_cp->m_specific_param.m_enc.m_matrice = 00;
7411 * Reads a tile header.
7412 * @param p_j2k the jpeg2000 codec.
7413 * @param p_stream the stream to write data to.
7414 * @param p_manager the user event manager.
7416 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7417 OPJ_UINT32 * p_tile_index,
7418 OPJ_UINT32 * p_data_size,
7419 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7420 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7421 OPJ_UINT32 * p_nb_comps,
7423 opj_stream_private_t *p_stream,
7424 opj_event_mgr_t * p_manager )
7426 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7427 OPJ_UINT32 l_marker_size;
7428 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7429 opj_tcp_v2_t * l_tcp = NULL;
7430 OPJ_UINT32 l_nb_tiles;
7433 assert(p_stream != 00);
7434 assert(p_j2k != 00);
7435 assert(p_manager != 00);
7437 /* Reach the End Of Codestream ?*/
7438 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7439 l_current_marker = J2K_MS_EOC;
7441 /* We need to encounter a SOT marker (a new tile-part header) */
7442 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7446 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7447 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7449 /* Try to read until the Start Of Data is detected */
7450 while (l_current_marker != J2K_MS_SOD) {
7452 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7453 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7454 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7458 /* Read 2 bytes from the buffer as the marker size */
7459 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7461 /* Why this condition? FIXME */
7462 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7463 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7465 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7467 /* Get the marker handler from the marker ID */
7468 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7470 /* Check if the marker is known and if it is the right place to find it */
7471 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7472 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7475 /* FIXME manage case of unknown marker as in the main header ? */
7477 /* Check if the marker size is compatible with the header data size */
7478 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7479 OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
7480 if (! new_header_data) {
7481 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7482 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
7483 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7484 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read header\n");
7487 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
7488 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7491 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7492 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) {
7493 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7497 /* Read the marker segment with the correct marker handler */
7498 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7499 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7503 /* Add the marker to the codestream index*/
7504 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
7506 l_marker_handler->id,
7507 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7508 l_marker_size + 4 )) {
7509 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
7513 /* Keep the position of the last SOT marker read */
7514 if ( l_marker_handler->id == J2K_MS_SOT ) {
7515 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7516 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7518 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7523 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7524 /* Skip the rest of the tile part header*/
7525 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) {
7526 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7529 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7532 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7533 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7534 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7537 /* Read 2 bytes from the buffer as the new marker ID */
7538 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7542 /* If we didn't skip data before, we need to read the SOD marker*/
7543 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7544 /* Try to read the SOD marker and skip data ? FIXME */
7545 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7551 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7552 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7553 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7554 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7558 /* Read 2 bytes from buffer as the new marker ID */
7559 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7563 /* Indicate we will try to read a new tile-part header*/
7564 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7565 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7566 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7568 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7569 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7570 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7574 /* Read 2 bytes from buffer as the new marker ID */
7575 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7579 /* Current marker is the EOC marker ?*/
7580 if (l_current_marker == J2K_MS_EOC) {
7581 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7582 p_j2k->m_current_tile_number = 0;
7583 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
7588 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
7589 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
7590 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7592 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
7593 ++p_j2k->m_current_tile_number;
7597 if (p_j2k->m_current_tile_number == l_nb_tiles) {
7598 *p_go_on = OPJ_FALSE;
7604 if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
7605 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7609 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
7610 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
7612 *p_tile_index = p_j2k->m_current_tile_number;
7613 *p_go_on = OPJ_TRUE;
7614 *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd);
7615 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
7616 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
7617 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
7618 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
7619 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
7621 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
7627 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
7628 OPJ_UINT32 p_tile_index,
7630 OPJ_UINT32 p_data_size,
7631 opj_stream_private_t *p_stream,
7632 opj_event_mgr_t * p_manager )
7634 OPJ_UINT32 l_current_marker;
7635 OPJ_BYTE l_data [2];
7636 opj_tcp_v2_t * l_tcp;
7639 assert(p_stream != 00);
7640 assert(p_j2k != 00);
7641 assert(p_manager != 00);
7643 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
7644 || (p_tile_index != p_j2k->m_current_tile_number) ) {
7648 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
7649 if (! l_tcp->m_data) {
7650 opj_j2k_tcp_destroy(l_tcp);
7654 if (! opj_tcd_decode_tile( p_j2k->m_tcd,
7658 p_j2k->cstr_index) ) {
7659 opj_j2k_tcp_destroy(l_tcp);
7660 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
7664 if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
7668 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
7669 * we destroy just the data which will be re-read in read_tile_header*/
7670 /*opj_j2k_tcp_destroy(l_tcp);
7671 p_j2k->m_tcd->tcp = 0;*/
7672 opj_j2k_tcp_data_destroy(l_tcp);
7674 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7675 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
7677 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
7678 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
7679 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7683 opj_read_bytes(l_data,&l_current_marker,2);
7685 if (l_current_marker == J2K_MS_EOC) {
7686 p_j2k->m_current_tile_number = 0;
7687 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
7689 else if (l_current_marker != J2K_MS_SOT)
7691 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
7700 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
7702 OPJ_UINT32 i,j,k = 0;
7703 OPJ_UINT32 l_width_src,l_height_src;
7704 OPJ_UINT32 l_width_dest,l_height_dest;
7705 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
7706 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
7707 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
7708 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
7709 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
7711 opj_image_comp_t * l_img_comp_src = 00;
7712 opj_image_comp_t * l_img_comp_dest = 00;
7714 opj_tcd_tilecomp_v2_t * l_tilec = 00;
7715 opj_image_t * l_image_src = 00;
7716 OPJ_UINT32 l_size_comp, l_remaining;
7717 OPJ_INT32 * l_dest_ptr;
7718 opj_tcd_resolution_v2_t* l_res= 00;
7720 l_tilec = p_tcd->tcd_image->tiles->comps;
7721 l_image_src = p_tcd->image;
7722 l_img_comp_src = l_image_src->comps;
7724 l_img_comp_dest = p_output_image->comps;
7726 for (i=0; i<l_image_src->numcomps; i++) {
7728 /* Allocate output component buffer if necessary */
7729 if (!l_img_comp_dest->data) {
7731 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
7732 if (! l_img_comp_dest->data) {
7737 /* Copy info from decoded comp image to output image */
7738 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
7741 /* Compute the precision of the output buffer */
7742 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
7743 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
7744 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
7750 if (l_size_comp == 3) {
7755 /* Current tile component size*/
7757 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
7758 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
7761 l_width_src = (l_res->x1 - l_res->x0);
7762 l_height_src = (l_res->y1 - l_res->y0);
7764 /* Border of the current output component*/
7765 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
7766 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
7767 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
7768 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
7771 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
7772 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
7776 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
7777 * of the input buffer (decoded tile component) which will be move
7778 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
7779 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
7780 * by this input area.
7782 assert( l_res->x0 >= 0);
7783 assert( l_res->x1 >= 0);
7784 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
7785 l_start_x_dest = l_res->x0 - l_x0_dest;
7786 l_offset_x0_src = 0;
7788 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7789 l_width_dest = l_width_src;
7790 l_offset_x1_src = 0;
7793 l_width_dest = l_x1_dest - l_res->x0 ;
7794 l_offset_x1_src = l_width_src - l_width_dest;
7798 l_start_x_dest = 0 ;
7799 l_offset_x0_src = l_x0_dest - l_res->x0;
7801 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7802 l_width_dest = l_width_src - l_offset_x0_src;
7803 l_offset_x1_src = 0;
7806 l_width_dest = l_img_comp_dest->w ;
7807 l_offset_x1_src = l_res->x1 - l_x1_dest;
7811 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
7812 l_start_y_dest = l_res->y0 - l_y0_dest;
7813 l_offset_y0_src = 0;
7815 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7816 l_height_dest = l_height_src;
7817 l_offset_y1_src = 0;
7820 l_height_dest = l_y1_dest - l_res->y0 ;
7821 l_offset_y1_src = l_height_src - l_height_dest;
7825 l_start_y_dest = 0 ;
7826 l_offset_y0_src = l_y0_dest - l_res->y0;
7828 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7829 l_height_dest = l_height_src - l_offset_y0_src;
7830 l_offset_y1_src = 0;
7833 l_height_dest = l_img_comp_dest->h ;
7834 l_offset_y1_src = l_res->y1 - l_y1_dest;
7838 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
7843 /* Compute the input buffer offset */
7844 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
7845 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
7846 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
7848 /* Compute the output buffer offset */
7849 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
7850 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
7852 /* Move the output buffer to the first place where we will write*/
7853 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
7856 fprintf(stdout, "COMPO[%d]:\n",i);
7857 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
7858 "\t tile offset:%d, %d, %d, %d\n"
7859 "\t buffer offset: %d; %d, %d\n",
7860 l_res->x0, l_res->y0, l_width_src, l_height_src,
7861 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
7862 l_start_offset_src, l_line_offset_src, l_end_offset_src);
7864 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
7865 "\t start offset: %d, line offset= %d\n",
7866 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
7870 switch (l_size_comp) {
7873 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
7874 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
7876 if (l_img_comp_src->sgnd) {
7877 for (j = 0 ; j < l_height_dest ; ++j) {
7878 for ( k = 0 ; k < l_width_dest ; ++k) {
7879 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
7882 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
7883 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
7887 for ( j = 0 ; j < l_height_dest ; ++j ) {
7888 for ( k = 0 ; k < l_width_dest ; ++k) {
7889 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7892 l_dest_ptr+= l_line_offset_dest;
7893 l_src_ptr += l_line_offset_src;
7897 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7898 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7903 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7904 l_src_ptr += l_start_offset_src;
7906 if (l_img_comp_src->sgnd) {
7907 for (j=0;j<l_height_dest;++j) {
7908 for (k=0;k<l_width_dest;++k) {
7909 *(l_dest_ptr++) = *(l_src_ptr++);
7912 l_dest_ptr+= l_line_offset_dest;
7913 l_src_ptr += l_line_offset_src ;
7917 for (j=0;j<l_height_dest;++j) {
7918 for (k=0;k<l_width_dest;++k) {
7919 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7922 l_dest_ptr+= l_line_offset_dest;
7923 l_src_ptr += l_line_offset_src ;
7927 l_src_ptr += l_end_offset_src;
7928 p_data = (OPJ_BYTE*) l_src_ptr;
7933 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7934 l_src_ptr += l_start_offset_src;
7936 for (j=0;j<l_height_dest;++j) {
7937 for (k=0;k<l_width_dest;++k) {
7938 *(l_dest_ptr++) = (*(l_src_ptr++));
7941 l_dest_ptr+= l_line_offset_dest;
7942 l_src_ptr += l_line_offset_src ;
7945 l_src_ptr += l_end_offset_src;
7946 p_data = (OPJ_BYTE*) l_src_ptr;
7960 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7962 * @param p_j2k the jpeg2000 codec.
7963 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
7964 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
7965 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
7966 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
7967 * @param p_manager the user event manager
7969 * @return true if the area could be set.
7971 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
7972 opj_image_t* p_image,
7973 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7974 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7975 opj_event_mgr_t * p_manager )
7977 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7978 opj_image_t * l_image = p_j2k->m_private_image;
7981 OPJ_INT32 l_comp_x1, l_comp_y1;
7982 opj_image_comp_t* l_img_comp = NULL;
7984 /* Check if we are read the main header */
7985 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
7986 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7990 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7991 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
7993 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7994 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7995 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7996 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8002 /* Check if the positions provided by the user are correct */
8005 assert(p_start_x >= 0 );
8006 assert(p_start_y >= 0 );
8008 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8009 opj_event_msg_v2(p_manager, EVT_ERROR,
8010 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8011 p_start_x, l_image->x1);
8014 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8015 opj_event_msg_v2(p_manager, EVT_WARNING,
8016 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8017 p_start_x, l_image->x0);
8018 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8019 p_image->x0 = l_image->x0;
8022 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8023 p_image->x0 = p_start_x;
8027 if ((OPJ_UINT32)p_start_y > l_image->y1){
8028 opj_event_msg_v2(p_manager, EVT_ERROR,
8029 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8030 p_start_y, l_image->y1);
8033 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8034 opj_event_msg_v2(p_manager, EVT_WARNING,
8035 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8036 p_start_y, l_image->y0);
8037 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8038 p_image->y0 = l_image->y0;
8041 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8042 p_image->y0 = p_start_y;
8046 assert((OPJ_UINT32)p_end_x > 0);
8047 assert((OPJ_UINT32)p_end_y > 0);
8048 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8049 opj_event_msg_v2(p_manager, EVT_ERROR,
8050 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8051 p_end_x, l_image->x0);
8054 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8055 opj_event_msg_v2(p_manager, EVT_WARNING,
8056 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8057 p_end_x, l_image->x1);
8058 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8059 p_image->x1 = l_image->x1;
8062 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8063 p_image->x1 = p_end_x;
8067 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8068 opj_event_msg_v2(p_manager, EVT_ERROR,
8069 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8070 p_end_y, l_image->y0);
8073 if ((OPJ_UINT32)p_end_y > l_image->y1){
8074 opj_event_msg_v2(p_manager, EVT_WARNING,
8075 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8076 p_end_y, l_image->y1);
8077 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8078 p_image->y1 = l_image->y1;
8081 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8082 p_image->y1 = p_end_y;
8086 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8088 l_img_comp = p_image->comps;
8089 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8093 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8094 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8095 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8096 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8098 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8099 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8101 opj_event_msg_v2(p_manager, EVT_ERROR,
8102 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8106 l_img_comp->w = l_w;
8108 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8109 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8111 opj_event_msg_v2(p_manager, EVT_ERROR,
8112 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8116 l_img_comp->h = l_h;
8121 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8122 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8129 /* ----------------------------------------------------------------------- */
8130 /* J2K / JPT decoder interface */
8131 /* ----------------------------------------------------------------------- */
8133 * Creates a J2K decompression structure.
8135 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8137 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8139 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8143 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8145 l_j2k->m_is_decoder = 1;
8146 l_j2k->m_cp.m_is_decoder = 1;
8148 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8149 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8150 opj_j2k_destroy(l_j2k);
8153 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8155 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8156 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8157 opj_j2k_destroy(l_j2k);
8161 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8163 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8165 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8167 /* codestream index creation */
8168 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8170 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8171 if (!l_j2k->cstr_index){
8172 opj_j2k_destroy(l_j2k);
8176 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8179 /* validation list creation */
8180 l_j2k->m_validation_list = opj_procedure_list_create();
8181 if (! l_j2k->m_validation_list) {
8182 opj_j2k_destroy(l_j2k);
8186 /* execution list creation */
8187 l_j2k->m_procedure_list = opj_procedure_list_create();
8188 if (! l_j2k->m_procedure_list) {
8189 opj_j2k_destroy(l_j2k);
8197 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8199 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8200 opj_calloc(1,sizeof(opj_codestream_index_t));
8204 cstr_index->maxmarknum = 100;
8205 cstr_index->marknum = 0;
8206 cstr_index->marker = (opj_marker_info_t*)
8207 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8208 if (!cstr_index-> marker)
8211 cstr_index->tile_index = NULL;
8218 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8220 * @param p_tile_no the tile index.
8221 * @param p_comp_no the component being outputted.
8222 * @param p_j2k the J2K codec.
8224 * @return the number of bytes taken by the SPCod element.
8226 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8227 OPJ_UINT32 p_tile_no,
8228 OPJ_UINT32 p_comp_no )
8230 opj_cp_v2_t *l_cp = 00;
8231 opj_tcp_v2_t *l_tcp = 00;
8232 opj_tccp_t *l_tccp = 00;
8235 assert(p_j2k != 00);
8237 l_cp = &(p_j2k->m_cp);
8238 l_tcp = &l_cp->tcps[p_tile_no];
8239 l_tccp = &l_tcp->tccps[p_comp_no];
8241 /* preconditions again */
8242 assert(p_tile_no < (l_cp->tw * l_cp->th));
8243 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8245 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8246 return 5 + l_tccp->numresolutions;
8254 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8256 * @param p_comp_no the component number to output.
8257 * @param p_stream the stream to write data to.
8258 * @param p_j2k J2K codec.
8259 * @param p_manager the user event manager.
8262 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8263 OPJ_UINT32 p_tile_no,
8264 OPJ_UINT32 p_comp_no,
8266 OPJ_UINT32 * p_header_size,
8267 struct opj_event_mgr * p_manager )
8270 opj_cp_v2_t *l_cp = 00;
8271 opj_tcp_v2_t *l_tcp = 00;
8272 opj_tccp_t *l_tccp = 00;
8275 assert(p_j2k != 00);
8276 assert(p_header_size != 00);
8277 assert(p_manager != 00);
8278 assert(p_data != 00);
8280 l_cp = &(p_j2k->m_cp);
8281 l_tcp = &l_cp->tcps[p_tile_no];
8282 l_tccp = &l_tcp->tccps[p_comp_no];
8284 /* preconditions again */
8285 assert(p_tile_no < (l_cp->tw * l_cp->th));
8286 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8288 if (*p_header_size < 5) {
8289 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8293 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8296 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8299 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8302 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8305 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8308 *p_header_size = *p_header_size - 5;
8310 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8312 if (*p_header_size < l_tccp->numresolutions) {
8313 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8317 for (i = 0; i < l_tccp->numresolutions; ++i) {
8318 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8322 *p_header_size = *p_header_size - l_tccp->numresolutions;
8329 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8330 * @param p_header_data the data contained in the COM box.
8331 * @param p_j2k the jpeg2000 codec.
8332 * @param p_header_size the size of the data contained in the COM marker.
8333 * @param p_manager the user event manager.
8335 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8337 OPJ_BYTE * p_header_data,
8338 OPJ_UINT32 * p_header_size,
8339 opj_event_mgr_t * p_manager)
8341 OPJ_UINT32 i, l_tmp;
8342 opj_cp_v2_t *l_cp = NULL;
8343 opj_tcp_v2_t *l_tcp = NULL;
8344 opj_tccp_t *l_tccp = NULL;
8345 OPJ_BYTE * l_current_ptr = NULL;
8348 assert(p_j2k != 00);
8349 assert(p_manager != 00);
8350 assert(p_header_data != 00);
8352 l_cp = &(p_j2k->m_cp);
8353 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8354 &l_cp->tcps[p_j2k->m_current_tile_number] :
8355 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8357 /* precondition again */
8358 assert(compno < p_j2k->m_private_image->numcomps);
8360 l_tccp = &l_tcp->tccps[compno];
8361 l_current_ptr = p_header_data;
8363 /* make sure room is sufficient */
8364 if (*p_header_size < 5) {
8365 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8369 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8370 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8373 /* If user wants to remove more resolutions than the codestream contains, return error */
8374 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8375 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8376 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8377 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8381 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8385 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8389 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8392 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8395 *p_header_size = *p_header_size - 5;
8397 /* use custom precinct size ? */
8398 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8399 if (*p_header_size < l_tccp->numresolutions) {
8400 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8404 for (i = 0; i < l_tccp->numresolutions; ++i) {
8405 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8407 l_tccp->prcw[i] = l_tmp & 0xf;
8408 l_tccp->prch[i] = l_tmp >> 4;
8411 *p_header_size = *p_header_size - l_tccp->numresolutions;
8414 /* set default size for the precinct width and height */
8415 for (i = 0; i < l_tccp->numresolutions; ++i) {
8416 l_tccp->prcw[i] = 15;
8417 l_tccp->prch[i] = 15;
8421 #ifdef WIP_REMOVE_MSD
8423 if (p_j2k->cstr_info && compno == 0) {
8424 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8426 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8427 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8428 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8429 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8430 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8433 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8434 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8443 * Copies the tile component parameters of all the component from the first tile component.
8445 * @param p_j2k the J2k codec.
8447 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8451 opj_cp_v2_t *l_cp = NULL;
8452 opj_tcp_v2_t *l_tcp = NULL;
8453 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8454 OPJ_UINT32 l_prc_size;
8457 assert(p_j2k != 00);
8459 l_cp = &(p_j2k->m_cp);
8460 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8461 &l_cp->tcps[p_j2k->m_current_tile_number] :
8462 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8464 l_ref_tccp = &l_tcp->tccps[0];
8465 l_copied_tccp = l_ref_tccp + 1;
8466 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8468 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8469 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8470 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8471 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8472 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8473 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8474 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8475 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8481 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
8483 * @param p_tile_no the tile index.
8484 * @param p_comp_no the component being outputted.
8485 * @param p_j2k the J2K codec.
8487 * @return the number of bytes taken by the SPCod element.
8489 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
8490 OPJ_UINT32 p_tile_no,
8491 OPJ_UINT32 p_comp_no )
8493 OPJ_UINT32 l_num_bands;
8495 opj_cp_v2_t *l_cp = 00;
8496 opj_tcp_v2_t *l_tcp = 00;
8497 opj_tccp_t *l_tccp = 00;
8500 assert(p_j2k != 00);
8502 l_cp = &(p_j2k->m_cp);
8503 l_tcp = &l_cp->tcps[p_tile_no];
8504 l_tccp = &l_tcp->tccps[p_comp_no];
8506 /* preconditions again */
8507 assert(p_tile_no < l_cp->tw * l_cp->th);
8508 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8510 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8512 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8513 return 1 + l_num_bands;
8516 return 1 + 2*l_num_bands;
8521 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
8523 * @param p_tile_no the tile to output.
8524 * @param p_comp_no the component number to output.
8525 * @param p_data the data buffer.
8526 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
8527 * @param p_j2k J2K codec.
8528 * @param p_manager the user event manager.
8531 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
8532 OPJ_UINT32 p_tile_no,
8533 OPJ_UINT32 p_comp_no,
8535 OPJ_UINT32 * p_header_size,
8536 struct opj_event_mgr * p_manager )
8538 OPJ_UINT32 l_header_size;
8539 OPJ_UINT32 l_band_no, l_num_bands;
8540 OPJ_UINT32 l_expn,l_mant;
8542 opj_cp_v2_t *l_cp = 00;
8543 opj_tcp_v2_t *l_tcp = 00;
8544 opj_tccp_t *l_tccp = 00;
8547 assert(p_j2k != 00);
8548 assert(p_header_size != 00);
8549 assert(p_manager != 00);
8550 assert(p_data != 00);
8552 l_cp = &(p_j2k->m_cp);
8553 l_tcp = &l_cp->tcps[p_tile_no];
8554 l_tccp = &l_tcp->tccps[p_comp_no];
8556 /* preconditions again */
8557 assert(p_tile_no < l_cp->tw * l_cp->th);
8558 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8560 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8562 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8563 l_header_size = 1 + l_num_bands;
8565 if (*p_header_size < l_header_size) {
8566 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8570 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8573 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8574 l_expn = l_tccp->stepsizes[l_band_no].expn;
8575 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8580 l_header_size = 1 + 2*l_num_bands;
8582 if (*p_header_size < l_header_size) {
8583 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8587 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8590 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8591 l_expn = l_tccp->stepsizes[l_band_no].expn;
8592 l_mant = l_tccp->stepsizes[l_band_no].mant;
8594 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
8599 *p_header_size = *p_header_size - l_header_size;
8605 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
8607 * @param p_comp_no the component being targeted.
8608 * @param p_header_data the data contained in the COM box.
8609 * @param p_j2k the jpeg2000 codec.
8610 * @param p_header_size the size of the data contained in the COM marker.
8611 * @param p_manager the user event manager.
8613 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
8614 OPJ_UINT32 p_comp_no,
8615 OPJ_BYTE* p_header_data,
8616 OPJ_UINT32 * p_header_size,
8617 opj_event_mgr_t * p_manager
8621 OPJ_UINT32 l_band_no;
8622 opj_cp_v2_t *l_cp = 00;
8623 opj_tcp_v2_t *l_tcp = 00;
8624 opj_tccp_t *l_tccp = 00;
8625 OPJ_BYTE * l_current_ptr = 00;
8626 OPJ_UINT32 l_tmp, l_num_band;
8629 assert(p_j2k != 00);
8630 assert(p_manager != 00);
8631 assert(p_header_data != 00);
8633 l_cp = &(p_j2k->m_cp);
8634 /* come from tile part header or main header ?*/
8635 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
8636 &l_cp->tcps[p_j2k->m_current_tile_number] :
8637 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8639 /* precondition again*/
8640 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8642 l_tccp = &l_tcp->tccps[p_comp_no];
8643 l_current_ptr = p_header_data;
8645 if (*p_header_size < 1) {
8646 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
8649 *p_header_size -= 1;
8651 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
8654 l_tccp->qntsty = l_tmp & 0x1f;
8655 l_tccp->numgbits = l_tmp >> 5;
8656 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8660 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
8662 (*p_header_size) / 2;
8664 if( l_num_band > J2K_MAXBANDS ) {
8665 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
8666 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
8667 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
8668 /*return OPJ_FALSE;*/
8673 if (l_cp->correct) {
8675 /* if JPWL is on, we check whether there are too many subbands */
8676 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
8677 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
8678 "JPWL: bad number of subbands in Sqcx (%d)\n",
8681 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
8684 /* we try to correct */
8686 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
8687 "- setting number of bands to %d => HYPOTHESIS!!!\n",
8692 #endif /* USE_JPWL */
8694 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8695 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8696 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
8698 if (l_band_no < J2K_MAXBANDS){
8699 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
8700 l_tccp->stepsizes[l_band_no].mant = 0;
8703 *p_header_size = *p_header_size - l_num_band;
8706 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8707 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
8709 if (l_band_no < J2K_MAXBANDS){
8710 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
8711 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
8714 *p_header_size = *p_header_size - 2*l_num_band;
8717 /* Add Antonin : if scalar_derived -> compute other stepsizes */
8718 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8719 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
8720 l_tccp->stepsizes[l_band_no].expn =
8721 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
8722 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
8723 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
8731 * Copies the tile component parameters of all the component from the first tile component.
8733 * @param p_j2k the J2k codec.
8735 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
8738 opj_cp_v2_t *l_cp = NULL;
8739 opj_tcp_v2_t *l_tcp = NULL;
8740 opj_tccp_t *l_ref_tccp = NULL;
8741 opj_tccp_t *l_copied_tccp = NULL;
8745 assert(p_j2k != 00);
8747 l_cp = &(p_j2k->m_cp);
8748 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
8749 &l_cp->tcps[p_j2k->m_current_tile_number] :
8750 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8752 l_ref_tccp = &l_tcp->tccps[0];
8753 l_copied_tccp = l_ref_tccp + 1;
8754 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
8756 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
8757 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
8758 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
8759 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
8765 * Dump some elements from the J2K decompression structure .
8767 *@param p_j2k the jpeg2000 codec.
8768 *@param flag flag to describe what elments are dump.
8769 *@param out_stream output stream where dump the elements.
8772 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
8774 /* Check if the flag is compatible with j2k file*/
8775 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
8776 fprintf(out_stream, "Wrong flag\n");
8780 /* Dump the image_header */
8781 if (flag & OPJ_IMG_INFO){
8782 if (p_j2k->m_private_image)
8783 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
8786 /* Dump the codestream info from main header */
8787 if (flag & OPJ_J2K_MH_INFO){
8788 opj_j2k_dump_MH_info(p_j2k, out_stream);
8792 /* Dump the codestream info of the current tile */
8793 if (flag & OPJ_J2K_TH_INFO){
8797 /* Dump the codestream index from main header */
8798 if (flag & OPJ_J2K_MH_IND){
8799 opj_j2k_dump_MH_index(p_j2k, out_stream);
8802 /* Dump the codestream index of the current tile */
8803 if (flag & OPJ_J2K_TH_IND){
8810 * Dump index elements of the codestream extract from the main header.
8812 *@param p_j2k the jpeg2000 codec.
8813 *@param out_stream output stream where dump the elements.
8816 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
8818 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
8819 OPJ_UINT32 it_marker, it_tile, it_tile_part;
8821 fprintf(out_stream, "Codestream index from main header: {\n");
8823 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
8824 "\t Main header end position=%" PRIi64 "\n",
8825 cstr_index->main_head_start, cstr_index->main_head_end);
8827 fprintf(out_stream, "\t Marker list: {\n");
8829 if (cstr_index->marker){
8830 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
8831 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8832 cstr_index->marker[it_marker].type,
8833 cstr_index->marker[it_marker].pos,
8834 cstr_index->marker[it_marker].len );
8838 fprintf(out_stream, "\t }\n");
8841 if (cstr_index->tile_index){
8843 /* Simple test to avoid to write empty information*/
8844 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
8845 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8846 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
8849 if (l_acc_nb_of_tile_part)
8851 fprintf(out_stream, "\t Tile index: {\n");
8853 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8854 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
8856 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
8858 if (cstr_index->tile_index[it_tile].tp_index){
8859 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
8860 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
8862 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
8863 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
8864 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
8868 if (cstr_index->tile_index[it_tile].marker){
8869 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
8870 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8871 cstr_index->tile_index[it_tile].marker[it_marker].type,
8872 cstr_index->tile_index[it_tile].marker[it_marker].pos,
8873 cstr_index->tile_index[it_tile].marker[it_marker].len );
8877 fprintf(out_stream,"\t }\n");
8881 fprintf(out_stream,"}\n");
8886 * Dump info elements of the codestream extract from the main header.
8888 *@param p_j2k the jpeg2000 codec.
8889 *@param out_stream output stream where dump the elements.
8892 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
8894 opj_tcp_v2_t * l_default_tile=NULL;
8896 fprintf(out_stream, "Codestream info from main header: {\n");
8898 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
8899 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
8900 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
8902 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8906 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
8908 fprintf(out_stream, "\t default tile {\n");
8909 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
8910 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
8911 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
8912 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
8914 for (compno = 0; compno < numcomps; compno++) {
8915 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
8917 OPJ_INT32 bandno, numbands;
8920 fprintf(out_stream, "\t\t comp %d {\n", compno);
8921 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
8922 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
8923 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
8924 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
8925 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
8926 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
8928 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
8929 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
8930 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
8932 fprintf(out_stream, "\n");
8934 /* quantization style*/
8935 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
8936 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
8937 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
8938 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
8939 for (bandno = 0; bandno < numbands; bandno++) {
8940 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
8941 l_tccp->stepsizes[bandno].expn);
8943 fprintf(out_stream, "\n");
8946 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
8948 fprintf(out_stream, "\t\t }\n");
8949 } /*end of component of default tile*/
8950 fprintf(out_stream, "\t }\n"); /*end of default tile*/
8954 fprintf(out_stream, "}\n");
8959 * Dump an image header structure.
8961 *@param img_header the image header to dump.
8962 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
8963 *@param out_stream output stream where dump the elements.
8965 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
8970 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
8974 fprintf(out_stream, "Image info {\n");
8975 tab[0] = '\t';tab[1] = '\0';
8978 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
8979 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
8980 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
8982 if (img_header->comps){
8984 for (compno = 0; compno < img_header->numcomps; compno++) {
8985 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
8986 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
8987 fprintf(out_stream,"%s}\n",tab);
8991 fprintf(out_stream, "}\n");
8995 * Dump a component image header structure.
8997 *@param comp_header the component image header to dump.
8998 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
8999 *@param out_stream output stream where dump the elements.
9001 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9006 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9009 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9012 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9013 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9014 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9017 fprintf(out_stream, "}\n");
9022 * Get the codestream info from a JPEG2000 codec.
9024 *@param p_j2k the component image header to dump.
9026 *@return the codestream information extract from the jpg2000 codec
9028 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9031 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9032 opj_tcp_v2_t *l_default_tile;
9033 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9035 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9037 cstr_info->tx0 = p_j2k->m_cp.tx0;
9038 cstr_info->ty0 = p_j2k->m_cp.ty0;
9039 cstr_info->tdx = p_j2k->m_cp.tdx;
9040 cstr_info->tdy = p_j2k->m_cp.tdy;
9041 cstr_info->tw = p_j2k->m_cp.tw;
9042 cstr_info->th = p_j2k->m_cp.th;
9044 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9046 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9048 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9049 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9050 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9051 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9053 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9055 for (compno = 0; compno < numcomps; compno++) {
9056 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9057 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9058 OPJ_INT32 bandno, numbands;
9061 l_tccp_info->csty = l_tccp->csty;
9062 l_tccp_info->numresolutions = l_tccp->numresolutions;
9063 l_tccp_info->cblkw = l_tccp->cblkw;
9064 l_tccp_info->cblkh = l_tccp->cblkh;
9065 l_tccp_info->cblksty = l_tccp->cblksty;
9066 l_tccp_info->qmfbid = l_tccp->qmfbid;
9067 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9069 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9070 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9073 /* quantization style*/
9074 l_tccp_info->qntsty = l_tccp->qntsty;
9075 l_tccp_info->numgbits = l_tccp->numgbits;
9077 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9078 if (numbands < J2K_MAXBANDS) {
9079 for (bandno = 0; bandno < numbands; bandno++) {
9080 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9081 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9086 l_tccp_info->roishift = l_tccp->roishift;
9094 * Get the codestream index from a JPEG2000 codec.
9096 *@param p_j2k the component image header to dump.
9098 *@return the codestream index extract from the jpg2000 codec
9100 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9102 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9103 opj_calloc(1,sizeof(opj_codestream_index_t));
9107 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9108 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9109 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9111 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9112 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9113 if (!l_cstr_index->marker){
9114 opj_free( l_cstr_index);
9118 if (p_j2k->cstr_index->marker)
9119 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9121 opj_free(l_cstr_index->marker);
9122 l_cstr_index->marker = NULL;
9125 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9126 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9127 if (!l_cstr_index->tile_index){
9128 opj_free( l_cstr_index->marker);
9129 opj_free( l_cstr_index);
9133 if (!p_j2k->cstr_index->tile_index){
9134 opj_free(l_cstr_index->tile_index);
9135 l_cstr_index->tile_index = NULL;
9138 OPJ_UINT32 it_tile = 0;
9139 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9142 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9144 l_cstr_index->tile_index[it_tile].marker =
9145 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9147 if (!l_cstr_index->tile_index[it_tile].marker) {
9148 OPJ_UINT32 it_tile_free;
9150 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9151 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9154 opj_free( l_cstr_index->tile_index);
9155 opj_free( l_cstr_index->marker);
9156 opj_free( l_cstr_index);
9160 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9161 memcpy( l_cstr_index->tile_index[it_tile].marker,
9162 p_j2k->cstr_index->tile_index[it_tile].marker,
9163 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9165 opj_free(l_cstr_index->tile_index[it_tile].marker);
9166 l_cstr_index->tile_index[it_tile].marker = NULL;
9169 /* Tile part index*/
9170 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9172 l_cstr_index->tile_index[it_tile].tp_index =
9173 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9175 if(!l_cstr_index->tile_index[it_tile].tp_index){
9176 OPJ_UINT32 it_tile_free;
9178 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9179 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9180 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9183 opj_free( l_cstr_index->tile_index);
9184 opj_free( l_cstr_index->marker);
9185 opj_free( l_cstr_index);
9189 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9190 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9191 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9192 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9195 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9196 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9199 /* Packet index (NOT USED)*/
9200 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9201 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9206 return l_cstr_index;
9209 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9211 OPJ_UINT32 it_tile=0;
9213 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9214 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9215 if (!p_j2k->cstr_index->tile_index)
9218 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9219 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9220 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9221 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9222 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9223 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9233 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9234 opj_stream_private_t *p_stream,
9235 opj_event_mgr_t * p_manager)
9237 opj_bool l_go_on = OPJ_TRUE;
9238 OPJ_UINT32 l_current_tile_no;
9239 OPJ_UINT32 l_data_size,l_max_data_size;
9240 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9241 OPJ_UINT32 l_nb_comps;
9242 OPJ_BYTE * l_current_data;
9244 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9245 if (! l_current_data) {
9246 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n");
9249 l_max_data_size = 1000;
9254 if (! opj_j2k_read_tile_header( p_j2k,
9257 &l_tile_x0, &l_tile_y0,
9258 &l_tile_x1, &l_tile_y1,
9263 opj_free(l_current_data);
9271 if (l_data_size > l_max_data_size) {
9272 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9273 if (! l_new_current_data) {
9274 opj_free(l_current_data);
9275 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
9278 l_current_data = l_new_current_data;
9279 l_max_data_size = l_data_size;
9282 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9283 opj_free(l_current_data);
9286 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);
9288 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9289 opj_free(l_current_data);
9292 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9296 opj_free(l_current_data);
9302 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9304 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9307 assert(p_j2k != 00);
9309 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9310 /* DEVELOPER CORNER, add your custom procedures */
9315 * Read and decode one tile.
9317 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9318 opj_stream_private_t *p_stream,
9319 opj_event_mgr_t * p_manager)
9321 opj_bool l_go_on = OPJ_TRUE;
9322 OPJ_UINT32 l_current_tile_no;
9323 OPJ_UINT32 l_tile_no_to_dec;
9324 OPJ_UINT32 l_data_size,l_max_data_size;
9325 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9326 OPJ_UINT32 l_nb_comps;
9327 OPJ_BYTE * l_current_data;
9329 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9330 if (! l_current_data) {
9331 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n");
9334 l_max_data_size = 1000;
9336 /*Allocate and initialize some elements of codestrem index if not already done*/
9337 if( !p_j2k->cstr_index->tile_index)
9339 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9340 opj_free(l_current_data);
9344 /* Move into the codestream to the first SOT used to decode the desired tile */
9345 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9346 if (p_j2k->cstr_index->tile_index)
9347 if(p_j2k->cstr_index->tile_index->tp_index)
9349 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9350 /* the index for this tile has not been built,
9351 * so move to the last SOT read */
9352 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9353 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9358 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)) ) {
9359 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9363 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9364 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9365 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9369 if (! opj_j2k_read_tile_header( p_j2k,
9372 &l_tile_x0, &l_tile_y0,
9373 &l_tile_x1, &l_tile_y1,
9378 opj_free(l_current_data);
9387 if (l_data_size > l_max_data_size) {
9388 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9389 if (! l_new_current_data) {
9390 opj_free(l_current_data);
9391 l_current_data = NULL;
9392 // TODO: LH: why tile numbering policy used in messages differs from
9393 // the one used in opj_j2k_decode_tiles() ?
9394 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9397 l_current_data = l_new_current_data;
9398 l_max_data_size = l_data_size;
9403 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9404 opj_free(l_current_data);
9407 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);
9409 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9410 opj_free(l_current_data);
9413 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9415 if(l_current_tile_no == l_tile_no_to_dec)
9417 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9418 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9419 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9425 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);
9430 opj_free(l_current_data);
9437 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9439 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9442 assert(p_j2k != 00);
9444 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9445 /* DEVELOPER CORNER, add your custom procedures */
9451 * Decodes the tiles of the stream.
9453 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9454 opj_stream_private_t * p_stream,
9455 opj_image_t * p_image,
9456 opj_event_mgr_t * p_manager)
9463 p_j2k->m_output_image = opj_image_create0();
9464 if (! (p_j2k->m_output_image)) {
9467 opj_copy_image_header(p_image, p_j2k->m_output_image);
9469 /* customization of the decoding */
9470 opj_j2k_setup_decoding(p_j2k);
9472 /* Decode the codestream */
9473 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9474 opj_image_destroy(p_j2k->m_private_image);
9475 p_j2k->m_private_image = NULL;
9479 /* Move data and copy one information from codec to output image*/
9480 for (compno = 0; compno < p_image->numcomps; compno++) {
9481 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9482 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9483 p_j2k->m_output_image->comps[compno].data = NULL;
9491 * Get the decoded tile.
9493 * @param p_j2k the jpeg2000 codestream codec.
9494 * @param p_stream input_stream
9495 * @param p_image output image. .
9496 * @param p_manager the user event manager
9497 * @param tile_index index of the tile we want decode
9499 * @return true if succeed.
9501 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
9502 opj_stream_private_t *p_stream,
9503 opj_image_t* p_image,
9504 opj_event_mgr_t * p_manager,
9505 OPJ_UINT32 tile_index )
9508 OPJ_UINT32 l_tile_x, l_tile_y;
9509 opj_image_comp_t* l_img_comp;
9512 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
9516 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9517 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);
9521 /* Compute the dimension of the desired tile*/
9522 l_tile_x = tile_index % p_j2k->m_cp.tw;
9523 l_tile_y = tile_index / p_j2k->m_cp.tw;
9525 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9526 if (p_image->x0 < p_j2k->m_private_image->x0)
9527 p_image->x0 = p_j2k->m_private_image->x0;
9528 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9529 if (p_image->x1 > p_j2k->m_private_image->x1)
9530 p_image->x1 = p_j2k->m_private_image->x1;
9532 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9533 if (p_image->y0 < p_j2k->m_private_image->y0)
9534 p_image->y0 = p_j2k->m_private_image->y0;
9535 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9536 if (p_image->y1 > p_j2k->m_private_image->y1)
9537 p_image->y1 = p_j2k->m_private_image->y1;
9539 l_img_comp = p_image->comps;
9540 for (compno=0; compno < p_image->numcomps; ++compno)
9542 OPJ_INT32 l_comp_x1, l_comp_y1;
9544 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9546 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9547 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9548 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9549 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9551 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9552 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9557 /* Destroy the previous output image*/
9558 if (p_j2k->m_output_image)
9559 opj_image_destroy(p_j2k->m_output_image);
9561 /* Create the ouput image from the information previously computed*/
9562 p_j2k->m_output_image = opj_image_create0();
9563 if (! (p_j2k->m_output_image)) {
9566 opj_copy_image_header(p_image, p_j2k->m_output_image);
9568 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9570 /* customization of the decoding */
9571 opj_j2k_setup_decoding_tile(p_j2k);
9573 /* Decode the codestream */
9574 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9575 opj_image_destroy(p_j2k->m_private_image);
9576 p_j2k->m_private_image = NULL;
9580 /* Move data and copy one information from codec to output image*/
9581 for (compno = 0; compno < p_image->numcomps; compno++) {
9582 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9584 if (p_image->comps[compno].data)
9585 opj_free(p_image->comps[compno].data);
9587 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9589 p_j2k->m_output_image->comps[compno].data = NULL;
9595 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
9596 OPJ_UINT32 res_factor,
9597 opj_event_mgr_t * p_manager)
9601 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9603 if (p_j2k->m_private_image) {
9604 if (p_j2k->m_private_image->comps) {
9605 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9606 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9607 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9608 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9609 if ( res_factor >= max_res){
9610 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
9613 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
9626 * Encodes all the tiles in a row.
9628 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
9629 opj_stream_private_t *p_stream,
9630 opj_event_mgr_t * p_manager )
9633 OPJ_UINT32 l_nb_tiles;
9634 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
9635 OPJ_BYTE * l_current_data;
9638 assert(p_j2k != 00);
9639 assert(p_stream != 00);
9640 assert(p_manager != 00);
9642 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9643 if (! l_current_data) {
9644 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
9647 l_max_tile_size = 1000;
9649 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9650 for (i=0;i<l_nb_tiles;++i) {
9651 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
9652 opj_free(l_current_data);
9656 l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
9657 if (l_current_tile_size > l_max_tile_size) {
9658 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
9659 if (! l_new_current_data) {
9660 opj_free(l_current_data);
9661 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
9664 l_current_data = l_new_current_data;
9665 l_max_tile_size = l_current_tile_size;
9668 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
9670 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
9675 opj_free(l_current_data);
9680 * Ends the compression procedures and possibility add data to be read after the
9683 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
9684 opj_stream_private_t *p_stream,
9685 opj_event_mgr_t * p_manager)
9687 /* customization of the encoding */
9688 opj_j2k_setup_end_compress(p_j2k);
9690 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
9700 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
9702 * @param p_j2k the jpeg2000 codec.
9703 * @param p_stream the stream object.
9704 * @param p_manager the user event manager.
9706 * @return true if the codec is valid.
9708 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
9709 opj_stream_private_t *p_stream,
9710 opj_image_t * p_image,
9711 opj_event_mgr_t * p_manager)
9714 assert(p_j2k != 00);
9715 assert(p_stream != 00);
9716 assert(p_manager != 00);
9718 p_j2k->m_private_image = opj_image_create0();
9719 opj_copy_image_header(p_image, p_j2k->m_private_image);
9721 // TODO_MSD: Find a better way
9722 if (p_image->comps) {
9724 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
9725 if (p_image->comps[it_comp].data) {
9726 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
9727 p_image->comps[it_comp].data = NULL;
9733 /* customization of the validation */
9734 opj_j2k_setup_encoding_validation (p_j2k);
9736 /* validation of the parameters codec */
9737 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
9741 /* customization of the encoding */
9742 opj_j2k_setup_header_writting(p_j2k);
9745 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9755 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
9756 OPJ_UINT32 p_tile_index,
9757 opj_stream_private_t *p_stream,
9758 opj_event_mgr_t * p_manager )
9761 if (p_tile_index != p_j2k->m_current_tile_number) {
9762 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
9766 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);
9768 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
9769 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
9770 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
9772 /* initialisation before tile encoding */
9773 if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9783 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
9785 OPJ_UINT32 i,j,k = 0;
9786 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
9787 opj_image_comp_t * l_img_comp = 00;
9788 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9789 opj_image_t * l_image = 00;
9790 OPJ_UINT32 l_size_comp, l_remaining;
9791 OPJ_INT32 * l_src_ptr;
9792 l_tilec = p_tcd->tcd_image->tiles->comps;
9793 l_image = p_tcd->image;
9794 l_img_comp = l_image->comps;
9796 for (i=0;i<p_tcd->image->numcomps;++i) {
9797 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
9798 l_remaining = l_img_comp->prec & 7; /* (%8) */
9803 if (l_size_comp == 3) {
9807 l_width = (l_tilec->x1 - l_tilec->x0);
9808 l_height = (l_tilec->y1 - l_tilec->y0);
9809 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
9810 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
9811 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
9812 l_stride = l_image_width - l_width;
9813 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
9815 switch (l_size_comp) {
9818 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
9819 if (l_img_comp->sgnd) {
9820 for (j=0;j<l_height;++j) {
9821 for (k=0;k<l_width;++k) {
9822 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
9826 l_src_ptr += l_stride;
9830 for (j=0;j<l_height;++j) {
9831 for (k=0;k<l_width;++k) {
9832 *(l_dest_ptr) = (*l_src_ptr)&0xff;
9836 l_src_ptr += l_stride;
9840 p_data = (OPJ_BYTE*) l_dest_ptr;
9845 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
9846 if (l_img_comp->sgnd) {
9847 for (j=0;j<l_height;++j) {
9848 for (k=0;k<l_width;++k) {
9849 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
9851 l_src_ptr += l_stride;
9855 for (j=0;j<l_height;++j) {
9856 for (k=0;k<l_width;++k) {
9857 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9859 l_src_ptr += l_stride;
9863 p_data = (OPJ_BYTE*) l_dest_ptr;
9868 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
9869 for (j=0;j<l_height;++j) {
9870 for (k=0;k<l_width;++k) {
9871 *(l_dest_ptr++) = *(l_src_ptr++);
9873 l_src_ptr += l_stride;
9876 p_data = (OPJ_BYTE*) l_dest_ptr;
9889 * @param p_j2k the jpeg2000 codec.
9890 * @param p_stream the stream to write data to.
9891 * @param p_manager the user event manager.
9893 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
9895 OPJ_UINT32 p_data_size,
9896 opj_stream_private_t *p_stream,
9897 opj_event_mgr_t * p_manager )
9899 opj_tcd_v2_t * l_tcd = 00;
9900 opj_cp_v2_t * l_cp = 00;
9901 opj_tcp_v2_t * l_tcp = 00;
9902 OPJ_UINT32 l_nb_bytes_written;
9903 OPJ_BYTE * l_current_data = 00;
9904 OPJ_UINT32 l_tile_size = 0;
9905 OPJ_UINT32 l_available_data;
9908 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9910 l_tcd = p_j2k->m_tcd;
9911 l_cp = &(p_j2k->m_cp);
9912 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
9914 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
9915 l_available_data = l_tile_size;
9916 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
9918 if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
9919 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
9923 l_nb_bytes_written = 0;
9924 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9927 l_current_data += l_nb_bytes_written;
9928 l_available_data -= l_nb_bytes_written;
9930 l_nb_bytes_written = 0;
9931 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9935 l_available_data -= l_nb_bytes_written;
9936 l_nb_bytes_written = l_tile_size - l_available_data;
9938 if ( opj_stream_write_data( p_stream,
9939 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
9940 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
9944 ++p_j2k->m_current_tile_number;
9951 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
9952 * are valid. Developers wanting to extend the library can add their own validation procedures.
9954 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
9957 assert(p_j2k != 00);
9959 /* DEVELOPER CORNER, insert your custom procedures */
9960 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );
9962 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
9963 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
9966 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );
9967 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );
9968 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
9972 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
9973 * are valid. Developers wanting to extend the library can add their own validation procedures.
9975 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
9978 assert(p_j2k != 00);
9980 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
9981 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
9983 /* DEVELOPER CORNER, add your custom validation procedure */
9984 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
9989 * Sets up the procedures to do on writing header.
9990 * Developers wanting to extend the library can add their own writing procedures.
9992 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
9995 assert(p_j2k != 00);
9997 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );
9998 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
9999 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10000 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10001 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10004 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10005 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components );
10006 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
10008 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10009 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
10013 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);
10015 if (p_j2k->m_cp.comment != 00) {
10016 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10019 /* DEVELOPER CORNER, insert your custom procedures */
10020 if (p_j2k->m_cp.rsiz & MCT) {
10021 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
10023 /* End of Developer Corner */
10025 if (p_j2k->cstr_index) {
10026 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10029 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10030 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10034 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10036 OPJ_UINT32 * p_data_written,
10037 OPJ_UINT32 p_total_data_size,
10038 opj_stream_private_t *p_stream,
10039 struct opj_event_mgr * p_manager )
10042 OPJ_UINT32 l_nb_bytes_written = 0;
10043 OPJ_UINT32 l_current_nb_bytes_written;
10044 OPJ_BYTE * l_begin_data = 00;
10046 opj_tcp_v2_t *l_tcp = 00;
10047 opj_tcd_v2_t * l_tcd = 00;
10048 opj_cp_v2_t * l_cp = 00;
10050 l_tcd = p_j2k->m_tcd;
10051 l_cp = &(p_j2k->m_cp);
10052 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10054 l_tcd->cur_pino = 0;
10056 /*Get number of tile parts*/
10057 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10062 l_current_nb_bytes_written = 0;
10063 l_begin_data = p_data;
10064 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10069 l_nb_bytes_written += l_current_nb_bytes_written;
10070 p_data += l_current_nb_bytes_written;
10071 p_total_data_size -= l_current_nb_bytes_written;
10073 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10074 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10075 l_current_nb_bytes_written = 0;
10076 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10077 l_nb_bytes_written += l_current_nb_bytes_written;
10078 p_data += l_current_nb_bytes_written;
10079 p_total_data_size -= l_current_nb_bytes_written;
10081 l_current_nb_bytes_written = 0;
10082 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10083 l_nb_bytes_written += l_current_nb_bytes_written;
10084 p_data += l_current_nb_bytes_written;
10085 p_total_data_size -= l_current_nb_bytes_written;
10088 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10089 l_current_nb_bytes_written = 0;
10090 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10091 l_nb_bytes_written += l_current_nb_bytes_written;
10092 p_data += l_current_nb_bytes_written;
10093 p_total_data_size -= l_current_nb_bytes_written;
10097 l_current_nb_bytes_written = 0;
10098 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10102 l_nb_bytes_written += l_current_nb_bytes_written;
10103 * p_data_written = l_nb_bytes_written;
10105 /* Writing Psot in SOT marker */
10106 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10108 if (l_cp->m_specific_param.m_enc.m_cinema){
10109 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10115 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10117 OPJ_UINT32 * p_data_written,
10118 OPJ_UINT32 p_total_data_size,
10119 opj_stream_private_t *p_stream,
10120 struct opj_event_mgr * p_manager
10123 OPJ_UINT32 tilepartno=0;
10124 OPJ_UINT32 l_nb_bytes_written = 0;
10125 OPJ_UINT32 l_current_nb_bytes_written;
10126 OPJ_UINT32 l_part_tile_size;
10127 OPJ_UINT32 tot_num_tp;
10130 OPJ_BYTE * l_begin_data;
10131 opj_tcp_v2_t *l_tcp = 00;
10132 opj_tcd_v2_t * l_tcd = 00;
10133 opj_cp_v2_t * l_cp = 00;
10136 l_tcd = p_j2k->m_tcd;
10137 l_cp = &(p_j2k->m_cp);
10138 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10140 /*Get number of tile parts*/
10141 tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
10143 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10144 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10145 l_current_nb_bytes_written = 0;
10146 l_part_tile_size = 0;
10147 l_begin_data = p_data;
10149 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10153 l_nb_bytes_written += l_current_nb_bytes_written;
10154 p_data += l_current_nb_bytes_written;
10155 p_total_data_size -= l_current_nb_bytes_written;
10156 l_part_tile_size += l_nb_bytes_written;
10158 l_current_nb_bytes_written = 0;
10159 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10163 p_data += l_current_nb_bytes_written;
10164 l_nb_bytes_written += l_current_nb_bytes_written;
10165 p_total_data_size -= l_current_nb_bytes_written;
10166 l_part_tile_size += l_nb_bytes_written;
10168 /* Writing Psot in SOT marker */
10169 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10171 if (l_cp->m_specific_param.m_enc.m_cinema) {
10172 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10175 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10178 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10179 l_tcd->cur_pino = pino;
10181 /*Get number of tile parts*/
10182 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
10183 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10184 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10185 l_current_nb_bytes_written = 0;
10186 l_part_tile_size = 0;
10187 l_begin_data = p_data;
10189 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10193 l_nb_bytes_written += l_current_nb_bytes_written;
10194 p_data += l_current_nb_bytes_written;
10195 p_total_data_size -= l_current_nb_bytes_written;
10196 l_part_tile_size += l_current_nb_bytes_written;
10198 l_current_nb_bytes_written = 0;
10200 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10204 l_nb_bytes_written += l_current_nb_bytes_written;
10205 p_data += l_current_nb_bytes_written;
10206 p_total_data_size -= l_current_nb_bytes_written;
10207 l_part_tile_size += l_current_nb_bytes_written;
10209 /* Writing Psot in SOT marker */
10210 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10212 if (l_cp->m_specific_param.m_enc.m_cinema) {
10213 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10216 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10220 *p_data_written = l_nb_bytes_written;
10226 * Writes the updated tlm.
10228 * @param p_stream the stream to write data to.
10229 * @param p_j2k J2K codec.
10230 * @param p_manager the user event manager.
10232 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10233 struct opj_stream_private *p_stream,
10234 struct opj_event_mgr * p_manager )
10236 OPJ_UINT32 l_tlm_size;
10237 OPJ_OFF_T l_tlm_position, l_current_position;
10239 /* preconditions */
10240 assert(p_j2k != 00);
10241 assert(p_manager != 00);
10242 assert(p_stream != 00);
10244 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10245 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10246 l_current_position = opj_stream_tell(p_stream);
10248 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10252 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) {
10256 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10265 * Ends the encoding, i.e. frees memory.
10267 * @param p_stream the stream to write data to.
10268 * @param p_j2k J2K codec.
10269 * @param p_manager the user event manager.
10271 opj_bool opj_j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10272 struct opj_stream_private *p_stream,
10273 struct opj_event_mgr * p_manager )
10275 /* preconditions */
10276 assert(p_j2k != 00);
10277 assert(p_manager != 00);
10278 assert(p_stream != 00);
10280 opj_tcd_destroy(p_j2k->m_tcd);
10283 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10284 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10285 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10286 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10289 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10290 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10291 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10294 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10300 * Destroys the memory associated with the decoding of headers.
10302 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10303 opj_stream_private_t *p_stream,
10304 opj_event_mgr_t * p_manager
10307 /* preconditions */
10308 assert(p_j2k != 00);
10309 assert(p_stream != 00);
10310 assert(p_manager != 00);
10312 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10313 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10314 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10317 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10326 * @param p_stream the stream to write data to.
10327 * @param p_j2k J2K codec.
10328 * @param p_manager the user event manager.
10330 opj_bool opj_j2k_init_info( opj_j2k_v2_t *p_j2k,
10331 struct opj_stream_private *p_stream,
10332 struct opj_event_mgr * p_manager )
10334 opj_codestream_info_t * l_cstr_info = 00;
10336 /* preconditions */
10337 assert(p_j2k != 00);
10338 assert(p_manager != 00);
10339 assert(p_stream != 00);
10342 /* TODO mergeV2: check this part which use cstr_info */
10343 /*l_cstr_info = p_j2k->cstr_info;
10347 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));
10349 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10350 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10352 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10354 l_cstr_info->tw = p_j2k->m_cp.tw;
10355 l_cstr_info->th = p_j2k->m_cp.th;
10357 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10358 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10359 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10360 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10362 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10364 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10366 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10368 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10369 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10372 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10374 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10376 /*l_cstr_info->maxmarknum = 100;
10377 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10378 l_cstr_info->marknum = 0;
10381 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);
10385 * Creates a tile-coder decoder.
10387 * @param p_stream the stream to write data to.
10388 * @param p_j2k J2K codec.
10389 * @param p_manager the user event manager.
10391 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10392 opj_stream_private_t *p_stream,
10393 opj_event_mgr_t * p_manager
10396 /* preconditions */
10397 assert(p_j2k != 00);
10398 assert(p_manager != 00);
10399 assert(p_stream != 00);
10401 p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
10403 if (! p_j2k->m_tcd) {
10404 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10408 if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10409 opj_tcd_destroy(p_j2k->m_tcd);
10420 * @param p_j2k the jpeg2000 codec.
10421 * @param p_stream the stream to write data to.
10422 * @param p_manager the user event manager.
10424 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10425 OPJ_UINT32 p_tile_index,
10427 OPJ_UINT32 p_data_size,
10428 opj_stream_private_t *p_stream,
10429 opj_event_mgr_t * p_manager )
10431 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10432 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10436 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10437 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);