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 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 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 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 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 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 j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * 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 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 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 j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 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 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 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 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 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 j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool 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 j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool 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 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 j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
372 static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 Write the SOC marker (Start Of Codestream)
382 @param j2k J2K handle
384 static void j2k_write_soc(opj_j2k_t *j2k);
387 * Writes the SOC marker (Start Of Codestream)
389 * @param p_stream the stream to write data to.
390 * @param p_j2k J2K codec.
391 * @param p_manager the user event manager.
393 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
394 struct opj_stream_private *p_stream,
395 struct opj_event_mgr * p_manager );
398 * Reads a SOC marker (Start of Codestream)
399 * @param p_header_data the data contained in the SOC box.
400 * @param jp2 the jpeg2000 file codec.
401 * @param p_header_size the size of the data contained in the SOC marker.
402 * @param p_manager the user event manager.
404 static opj_bool opj_j2k_read_soc(
406 struct opj_stream_private *p_stream,
407 struct opj_event_mgr * p_manager
411 Write the SIZ marker (image and tile size)
412 @param j2k J2K handle
414 static void j2k_write_siz(opj_j2k_t *j2k);
417 * Writes the SIZ marker (image and tile size)
419 * @param p_stream the stream to write data to.
420 * @param p_j2k J2K codec.
421 * @param p_manager the user event manager.
423 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
424 struct opj_stream_private *p_stream,
425 struct opj_event_mgr * p_manager );
428 * Reads a SIZ marker (image and tile size)
429 * @param p_header_data the data contained in the SIZ box.
430 * @param jp2 the jpeg2000 file codec.
431 * @param p_header_size the size of the data contained in the SIZ marker.
432 * @param p_manager the user event manager.
434 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
435 OPJ_BYTE * p_header_data,
436 OPJ_UINT32 p_header_size,
437 opj_event_mgr_t * p_manager);
440 Write the COM marker (comment)
441 @param j2k J2K handle
443 static void j2k_write_com(opj_j2k_t *j2k);
446 * Writes the COM marker (comment)
448 * @param p_stream the stream to write data to.
449 * @param p_j2k J2K codec.
450 * @param p_manager the user event manager.
452 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
453 struct opj_stream_private *p_stream,
454 struct opj_event_mgr * p_manager );
457 * Reads a COM marker (comments)
458 * @param p_header_data the data contained in the COM box.
459 * @param jp2 the jpeg2000 file codec.
460 * @param p_header_size the size of the data contained in the COM marker.
461 * @param p_manager the user event manager.
463 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
464 OPJ_BYTE * p_header_data,
465 OPJ_UINT32 p_header_size,
466 opj_event_mgr_t * p_manager );
468 Write the value concerning the specified component in the marker COD and COC
469 @param j2k J2K handle
470 @param compno Number of the component concerned by the information written
472 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
475 Write the COD marker (coding style default)
476 @param j2k J2K handle
478 static void j2k_write_cod(opj_j2k_t *j2k);
481 * Writes the COD marker (Coding style default)
483 * @param p_stream the stream to write data to.
484 * @param p_j2k J2K codec.
485 * @param p_manager the user event manager.
487 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
488 struct opj_stream_private *p_stream,
489 struct opj_event_mgr * p_manager );
492 * Reads a COD marker (Coding Styke defaults)
493 * @param p_header_data the data contained in the COD box.
494 * @param p_j2k the jpeg2000 codec.
495 * @param p_header_size the size of the data contained in the COD marker.
496 * @param p_manager the user event manager.
498 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
499 OPJ_BYTE * p_header_data,
500 OPJ_UINT32 p_header_size,
501 opj_event_mgr_t * p_manager);
504 Write the COC marker (coding style component)
505 @param j2k J2K handle
506 @param compno Number of the component concerned by the information written
508 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
511 * Writes the COC marker (Coding style component)
513 * @param p_comp_number the index of the component to output.
514 * @param p_stream the stream to write data to.
515 * @param p_j2k J2K codec.
516 * @param p_manager the user event manager.
518 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
519 OPJ_UINT32 p_comp_number,
520 struct opj_stream_private *p_stream,
521 struct opj_event_mgr * p_manager
524 * Writes the COC marker (Coding style component)
526 * @param p_comp_no the index of the component to output.
527 * @param p_stream the stream to write data to.
528 * @param p_j2k J2K codec.
529 * @param p_manager the user event manager.
531 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
532 OPJ_UINT32 p_comp_no,
534 OPJ_UINT32 * p_data_written,
535 struct opj_event_mgr * p_manager );
538 * Gets the maximum size taken by a coc.
540 * @param p_j2k the jpeg2000 codec to use.
542 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
546 * Reads a COC marker (Coding Style Component)
547 * @param p_header_data the data contained in the COC box.
548 * @param p_j2k the jpeg2000 codec.
549 * @param p_header_size the size of the data contained in the COC marker.
550 * @param p_manager the user event manager.
552 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
553 OPJ_BYTE * p_header_data,
554 OPJ_UINT32 p_header_size,
555 opj_event_mgr_t * p_manager );
558 Write the value concerning the specified component in the marker QCD and QCC
559 @param j2k J2K handle
560 @param compno Number of the component concerned by the information written
562 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
565 Write the QCD marker (quantization default)
566 @param j2k J2K handle
568 static void j2k_write_qcd(opj_j2k_t *j2k);
571 * Writes the QCD marker (quantization default)
573 * @param p_comp_number the index of the component to output.
574 * @param p_stream the stream to write data to.
575 * @param p_j2k J2K codec.
576 * @param p_manager the user event manager.
578 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
579 struct opj_stream_private *p_stream,
580 struct opj_event_mgr * p_manager );
583 * Reads a QCD marker (Quantization defaults)
584 * @param p_header_data the data contained in the QCD box.
585 * @param p_j2k the jpeg2000 codec.
586 * @param p_header_size the size of the data contained in the QCD marker.
587 * @param p_manager the user event manager.
589 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
590 OPJ_BYTE * p_header_data,
591 OPJ_UINT32 p_header_size,
592 opj_event_mgr_t * p_manager );
595 Write the QCC marker (quantization component)
596 @param j2k J2K handle
597 @param compno Number of the component concerned by the information written
599 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
602 * Writes the QCC marker (quantization component)
604 * @param p_comp_no the index of the component to output.
605 * @param p_stream the stream to write data to.
606 * @param p_j2k J2K codec.
607 * @param p_manager the user event manager.
609 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
610 OPJ_UINT32 p_comp_no,
611 struct opj_stream_private *p_stream,
612 struct opj_event_mgr * p_manager );
615 * Writes the QCC marker (quantization component)
617 * @param p_comp_no the index of the component to output.
618 * @param p_stream the stream to write data to.
619 * @param p_j2k J2K codec.
620 * @param p_manager the user event manager.
622 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
623 OPJ_UINT32 p_comp_no,
625 OPJ_UINT32 * p_data_written,
626 struct opj_event_mgr * p_manager );
629 * Gets the maximum size taken by a qcc.
631 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
634 * Reads a QCC marker (Quantization component)
635 * @param p_header_data the data contained in the QCC box.
636 * @param p_j2k the jpeg2000 codec.
637 * @param p_header_size the size of the data contained in the QCC marker.
638 * @param p_manager the user event manager.
640 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
641 OPJ_BYTE * p_header_data,
642 OPJ_UINT32 p_header_size,
643 opj_event_mgr_t * p_manager);
646 Write the POC marker (progression order change)
647 @param j2k J2K handle
649 static void j2k_write_poc(opj_j2k_t *j2k);
652 * Writes the POC marker (Progression Order Change)
654 * @param p_stream the stream to write data to.
655 * @param p_j2k J2K codec.
656 * @param p_manager the user event manager.
658 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
659 struct opj_stream_private *p_stream,
660 struct opj_event_mgr * p_manager );
663 * Writes the POC marker (Progression Order Change)
665 * @param p_stream the stream to write data to.
666 * @param p_j2k J2K codec.
667 * @param p_manager the user event manager.
669 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
671 OPJ_UINT32 * p_data_written,
672 struct opj_event_mgr * p_manager );
675 * Gets the maximum size taken by the writting of a POC.
677 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
680 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
682 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
685 * Gets the maximum size taken by the headers of the SOT.
687 * @param p_j2k the jpeg2000 codec to use.
689 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
692 * Reads a POC marker (Progression Order Change)
694 * @param p_header_data the data contained in the POC box.
695 * @param p_j2k the jpeg2000 codec.
696 * @param p_header_size the size of the data contained in the POC marker.
697 * @param p_manager the user event manager.
699 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
700 OPJ_BYTE * p_header_data,
701 OPJ_UINT32 p_header_size,
702 opj_event_mgr_t * p_manager );
705 * Reads a CRG marker (Component registration)
707 * @param p_header_data the data contained in the TLM box.
708 * @param p_j2k the jpeg2000 codec.
709 * @param p_header_size the size of the data contained in the TLM marker.
710 * @param p_manager the user event manager.
712 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
713 OPJ_BYTE * p_header_data,
714 OPJ_UINT32 p_header_size,
715 opj_event_mgr_t * p_manager );
717 * Reads a TLM marker (Tile Length Marker)
719 * @param p_header_data the data contained in the TLM box.
720 * @param p_j2k the jpeg2000 codec.
721 * @param p_header_size the size of the data contained in the TLM marker.
722 * @param p_manager the user event manager.
724 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
725 OPJ_BYTE * p_header_data,
726 OPJ_UINT32 p_header_size,
727 opj_event_mgr_t * p_manager);
730 * Writes the updated tlm.
732 * @param p_stream the stream to write data to.
733 * @param p_j2k J2K codec.
734 * @param p_manager the user event manager.
736 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
737 struct opj_stream_private *p_stream,
738 struct opj_event_mgr * p_manager );
741 * Reads a PLM marker (Packet length, main header marker)
743 * @param p_header_data the data contained in the TLM box.
744 * @param p_j2k the jpeg2000 codec.
745 * @param p_header_size the size of the data contained in the TLM marker.
746 * @param p_manager the user event manager.
748 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
749 OPJ_BYTE * p_header_data,
750 OPJ_UINT32 p_header_size,
751 opj_event_mgr_t * p_manager);
753 * Reads a PLT marker (Packet length, tile-part header)
755 * @param p_header_data the data contained in the PLT box.
756 * @param p_j2k the jpeg2000 codec.
757 * @param p_header_size the size of the data contained in the PLT marker.
758 * @param p_manager the user event manager.
760 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
761 OPJ_BYTE * p_header_data,
762 OPJ_UINT32 p_header_size,
763 opj_event_mgr_t * p_manager );
767 * Reads a PPM marker (Packed packet headers, main header)
769 * @param p_header_data the data contained in the POC box.
770 * @param p_j2k the jpeg2000 codec.
771 * @param p_header_size the size of the data contained in the POC marker.
772 * @param p_manager the user event manager.
775 static opj_bool j2k_read_ppm_v2 (
777 OPJ_BYTE * p_header_data,
778 OPJ_UINT32 p_header_size,
779 struct opj_event_mgr * p_manager
783 static opj_bool j2k_read_ppm_v3 (
785 OPJ_BYTE * p_header_data,
786 OPJ_UINT32 p_header_size,
787 struct opj_event_mgr * p_manager
792 * Reads a PPT marker (Packed packet headers, tile-part header)
794 * @param p_header_data the data contained in the PPT box.
795 * @param p_j2k the jpeg2000 codec.
796 * @param p_header_size the size of the data contained in the PPT marker.
797 * @param p_manager the user event manager.
799 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
800 OPJ_BYTE * p_header_data,
801 OPJ_UINT32 p_header_size,
802 opj_event_mgr_t * p_manager );
804 Write the TLM marker (Mainheader)
805 @param j2k J2K handle
807 static void j2k_write_tlm(opj_j2k_t *j2k);
810 * Writes the TLM marker (Tile Length Marker)
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 j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
817 struct opj_stream_private *p_stream,
818 struct opj_event_mgr * p_manager );
821 Write the SOT marker (start of tile-part)
822 @param j2k J2K handle
824 static void j2k_write_sot(opj_j2k_t *j2k);
827 * Writes the SOT marker (Start of tile-part)
829 * @param p_stream the stream to write data to.
830 * @param p_j2k J2K codec.
831 * @param p_manager the user event manager.
833 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
835 OPJ_UINT32 * p_data_written,
836 const struct opj_stream_private *p_stream,
837 struct opj_event_mgr * p_manager );
840 * Reads a PPT marker (Packed packet headers, tile-part header)
842 * @param p_header_data the data contained in the PPT box.
843 * @param p_j2k the jpeg2000 codec.
844 * @param p_header_size the size of the data contained in the PPT marker.
845 * @param p_manager the user event manager.
847 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
848 OPJ_BYTE * p_header_data,
849 OPJ_UINT32 p_header_size,
850 opj_event_mgr_t * p_manager );
853 Write the SOD marker (start of data)
854 @param j2k J2K handle
855 @param tile_coder Pointer to a TCD handle
857 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
860 * Writes the SOD marker (Start of data)
862 * @param p_stream the stream to write data to.
863 * @param p_j2k J2K codec.
864 * @param p_manager the user event manager.
866 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
867 struct opj_tcd_v2 * p_tile_coder,
869 OPJ_UINT32 * p_data_written,
870 OPJ_UINT32 p_total_data_size,
871 const struct opj_stream_private *p_stream,
872 struct opj_event_mgr * p_manager );
875 * Reads a SOD marker (Start Of Data)
877 * @param p_header_data the data contained in the SOD box.
878 * @param p_j2k the jpeg2000 codec.
879 * @param p_header_size the size of the data contained in the SOD marker.
880 * @param p_manager the user event manager.
882 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
883 opj_stream_private_t *p_stream,
884 opj_event_mgr_t * p_manager );
887 * Updates the Tile Length Marker.
889 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
891 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
892 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
894 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
895 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
899 Write the RGN marker (region-of-interest)
900 @param j2k J2K handle
901 @param compno Number of the component concerned by the information written
902 @param tileno Number of the tile concerned by the information written
904 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
907 * Writes the RGN marker (Region Of Interest)
909 * @param p_tile_no the tile to output
910 * @param p_comp_no the component to output
911 * @param p_stream the stream to write data to.
912 * @param p_j2k J2K codec.
913 * @param p_manager the user event manager.
915 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
916 OPJ_UINT32 p_tile_no,
917 OPJ_UINT32 p_comp_no,
918 struct opj_stream_private *p_stream,
919 struct opj_event_mgr * p_manager );
922 * Reads a RGN marker (Region Of Interest)
924 * @param p_header_data the data contained in the POC box.
925 * @param p_j2k the jpeg2000 codec.
926 * @param p_header_size the size of the data contained in the POC marker.
927 * @param p_manager the user event manager.
929 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
930 OPJ_BYTE * p_header_data,
931 OPJ_UINT32 p_header_size,
932 opj_event_mgr_t * p_manager );
935 * Writes the EOC marker (End of Codestream)
937 * @param p_stream the stream to write data to.
938 * @param p_j2k J2K codec.
939 * @param p_manager the user event manager.
941 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
942 struct opj_stream_private *p_stream,
943 struct opj_event_mgr * p_manager );
946 Write the EOC marker (end of codestream)
947 @param j2k J2K handle
949 static void j2k_write_eoc(opj_j2k_t *j2k);
951 Read the EOC marker (end of codestream)
952 @param j2k J2K handle
954 static void j2k_read_eoc(opj_j2k_t *j2k);
957 * Reads a EOC marker (End Of Codestream)
959 * @param p_header_data the data contained in the SOD box.
960 * @param p_j2k the jpeg2000 codec.
961 * @param p_header_size the size of the data contained in the SOD marker.
962 * @param p_manager the user event manager.
965 static opj_bool j2k_read_eoc_v2 (
967 struct opj_stream_private *p_stream,
968 struct opj_event_mgr * p_manager
975 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
977 * @param p_stream the stream to write data to.
978 * @param p_j2k J2K codec.
979 * @param p_manager the user event manager.
981 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
982 struct opj_stream_private *p_stream,
983 struct opj_event_mgr * p_manager );
988 * @param p_stream the stream to write data to.
989 * @param p_j2k J2K codec.
990 * @param p_manager the user event manager.
992 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
993 struct opj_stream_private *p_stream,
994 struct opj_event_mgr * p_manager );
997 Add main header marker information
998 @param cstr_info Codestream information structure
999 @param type marker type
1000 @param pos byte offset of marker segment
1001 @param len length of marker segment
1003 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1005 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1007 Add tile header marker information
1008 @param tileno tile index number
1009 @param cstr_info Codestream information structure
1010 @param type marker type
1011 @param pos byte offset of marker segment
1012 @param len length of marker segment
1014 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1016 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1019 * Reads an unknown marker
1021 * @param p_stream the stream object to read from.
1022 * @param p_j2k the jpeg2000 codec.
1023 * @param p_manager the user event manager.
1025 * @return true if the marker could be deduced.
1027 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
1028 opj_stream_private_t *p_stream,
1029 OPJ_UINT32 *output_marker,
1030 opj_event_mgr_t * p_manager );
1033 * Writes the MCT marker (Multiple Component Transform)
1035 * @param p_stream the stream to write data to.
1036 * @param p_j2k J2K codec.
1037 * @param p_manager the user event manager.
1039 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1040 opj_mct_data_t * p_mct_record,
1041 struct opj_stream_private *p_stream,
1042 struct opj_event_mgr * p_manager );
1045 * Reads a MCT marker (Multiple Component Transform)
1047 * @param p_header_data the data contained in the MCT box.
1048 * @param p_j2k the jpeg2000 codec.
1049 * @param p_header_size the size of the data contained in the MCT marker.
1050 * @param p_manager the user event manager.
1052 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1053 OPJ_BYTE * p_header_data,
1054 OPJ_UINT32 p_header_size,
1055 opj_event_mgr_t * p_manager );
1058 * Writes the MCC marker (Multiple Component Collection)
1060 * @param p_stream the stream to write data to.
1061 * @param p_j2k J2K codec.
1062 * @param p_manager the user event manager.
1064 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1065 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1066 struct opj_stream_private *p_stream,
1067 struct opj_event_mgr * p_manager );
1071 * Reads a MCC marker (Multiple Component Collection)
1073 * @param p_header_data the data contained in the MCC box.
1074 * @param p_j2k the jpeg2000 codec.
1075 * @param p_header_size the size of the data contained in the MCC marker.
1076 * @param p_manager the user event manager.
1078 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1079 OPJ_BYTE * p_header_data,
1080 OPJ_UINT32 p_header_size,
1081 opj_event_mgr_t * p_manager );
1084 * Writes the MCO marker (Multiple component transformation ordering)
1086 * @param p_stream the stream to write data to.
1087 * @param p_j2k J2K codec.
1088 * @param p_manager the user event manager.
1090 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1091 struct opj_stream_private *p_stream,
1092 struct opj_event_mgr * p_manager );
1095 * Reads a MCO marker (Multiple Component Transform Ordering)
1097 * @param p_header_data the data contained in the MCO box.
1098 * @param p_j2k the jpeg2000 codec.
1099 * @param p_header_size the size of the data contained in the MCO marker.
1100 * @param p_manager the user event manager.
1102 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1103 OPJ_BYTE * p_header_data,
1104 OPJ_UINT32 p_header_size,
1105 opj_event_mgr_t * p_manager );
1107 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1109 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1110 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1111 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1112 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1114 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1115 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1116 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1117 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1119 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1120 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1121 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 * Ends the encoding, i.e. frees memory.
1128 * @param p_stream the stream to write data to.
1129 * @param p_j2k J2K codec.
1130 * @param p_manager the user event manager.
1132 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1133 struct opj_stream_private *p_stream,
1134 struct opj_event_mgr * p_manager );
1137 * Writes the CBD marker (Component bit depth definition)
1139 * @param p_stream the stream to write data to.
1140 * @param p_j2k J2K codec.
1141 * @param p_manager the user event manager.
1143 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1144 struct opj_stream_private *p_stream,
1145 struct opj_event_mgr * p_manager );
1148 * Reads a CBD marker (Component bit depth definition)
1149 * @param p_header_data the data contained in the CBD box.
1150 * @param p_j2k the jpeg2000 codec.
1151 * @param p_header_size the size of the data contained in the CBD marker.
1152 * @param p_manager the user event manager.
1154 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1155 OPJ_BYTE * p_header_data,
1156 OPJ_UINT32 p_header_size,
1157 opj_event_mgr_t * p_manager);
1160 * Writes the image components.
1162 * @param p_stream the stream to write data to.
1163 * @param p_j2k J2K codec.
1164 * @param p_manager the user event manager.
1166 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1167 struct opj_stream_private *p_stream,
1168 struct opj_event_mgr * p_manager );
1171 * Writes regions of interests.
1173 * @param p_stream the stream to write data to.
1174 * @param p_j2k J2K codec.
1175 * @param p_manager the user event manager.
1177 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1178 struct opj_stream_private *p_stream,
1179 struct opj_event_mgr * p_manager );
1184 * @param p_stream the stream to write data to.
1185 * @param p_j2k J2K codec.
1186 * @param p_manager the user event manager.
1188 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1189 struct opj_stream_private *p_stream,
1190 struct opj_event_mgr * p_manager );
1193 * Checks the progression order changes values. Tells of the poc given as input are valid.
1194 * A nice message is outputted at errors.
1196 * @param p_pocs the progression order changes.
1197 * @param p_nb_pocs the number of progression order changes.
1198 * @param p_nb_resolutions the number of resolutions.
1199 * @param numcomps the number of components
1200 * @param numlayers the number of layers.
1202 * @return true if the pocs are valid.
1204 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1205 OPJ_UINT32 p_nb_pocs,
1206 OPJ_UINT32 p_nb_resolutions,
1207 OPJ_UINT32 numcomps,
1208 OPJ_UINT32 numlayers,
1209 opj_event_mgr_t * p_manager);
1212 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1214 * @param cp the coding parameters.
1215 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1216 * @param tileno the given tile.
1218 * @return the number of tile parts.
1220 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1222 /** mem allocation for TLM marker*/
1223 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1226 * Calculates the total number of tile parts needed by the encoder to
1227 * encode such an image. If not enough memory is available, then the function return false.
1229 * @param p_nb_tiles pointer that will hold the number of tile parts.
1230 * @param cp the coding parameters for the image.
1231 * @param image the image to encode.
1232 * @param p_j2k the p_j2k encoder.
1233 * @param p_manager the user event manager.
1235 * @return true if the function was successful, false else.
1237 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1239 OPJ_UINT32 * p_nb_tiles,
1241 opj_event_mgr_t * p_manager);
1243 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1245 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1247 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1253 /* ----------------------------------------------------------------------- */
1254 typedef struct j2k_prog_order{
1255 OPJ_PROG_ORDER enum_prog;
1259 j2k_prog_order_t j2k_prog_order_list[] = {
1265 {(OPJ_PROG_ORDER)-1, ""}
1273 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1281 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1283 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1285 opj_j2k_read_int16_to_float,
1286 opj_j2k_read_int32_to_float,
1287 opj_j2k_read_float32_to_float,
1288 opj_j2k_read_float64_to_float
1291 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1293 opj_j2k_read_int16_to_int32,
1294 opj_j2k_read_int32_to_int32,
1295 opj_j2k_read_float32_to_int32,
1296 opj_j2k_read_float64_to_int32
1299 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1301 opj_j2k_write_float_to_int16,
1302 opj_j2k_write_float_to_int32,
1303 opj_j2k_write_float_to_float,
1304 opj_j2k_write_float_to_float64
1307 typedef struct opj_dec_memory_marker_handler
1311 /** value of the state when the marker can appear */
1313 /** action linked to the marker */
1314 opj_bool (*handler) (
1315 opj_j2k_v2_t *p_j2k,
1316 OPJ_BYTE * p_header_data,
1317 OPJ_UINT32 p_header_size,
1318 struct opj_event_mgr * p_manager
1321 opj_dec_memory_marker_handler_t;
1323 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1326 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1327 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1328 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1329 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1330 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1331 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1332 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1333 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1334 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1335 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1336 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1337 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1338 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1340 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1341 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1342 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1343 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1344 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1345 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1347 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1348 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1349 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1350 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1351 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1352 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1353 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1354 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1355 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1356 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1357 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1358 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1359 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1361 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1362 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1363 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1364 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1365 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1366 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1368 #ifdef TODO_MS /* FIXME */
1369 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1370 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1371 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1372 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1374 #endif /* USE_JPWL */
1376 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1377 {J2K_MS_INSEC, 0, j2k_read_insec}
1378 #endif /* USE_JPSEC */
1379 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1384 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1386 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1387 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1391 for (i=0;i<p_nb_elem;++i) {
1392 opj_read_bytes(l_src_data,&l_temp,2);
1394 l_src_data+=sizeof(OPJ_INT16);
1396 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1400 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1402 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1403 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1407 for (i=0;i<p_nb_elem;++i) {
1408 opj_read_bytes(l_src_data,&l_temp,4);
1410 l_src_data+=sizeof(OPJ_INT32);
1412 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1416 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1418 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1419 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1423 for (i=0;i<p_nb_elem;++i) {
1424 opj_read_float(l_src_data,&l_temp);
1426 l_src_data+=sizeof(OPJ_FLOAT32);
1428 *(l_dest_data++) = l_temp;
1432 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1434 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1435 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1439 for (i=0;i<p_nb_elem;++i) {
1440 opj_read_double(l_src_data,&l_temp);
1442 l_src_data+=sizeof(OPJ_FLOAT64);
1444 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1448 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1450 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1451 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1455 for (i=0;i<p_nb_elem;++i) {
1456 opj_read_bytes(l_src_data,&l_temp,2);
1458 l_src_data+=sizeof(OPJ_INT16);
1460 *(l_dest_data++) = (OPJ_INT32) l_temp;
1464 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1466 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1467 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1471 for (i=0;i<p_nb_elem;++i) {
1472 opj_read_bytes(l_src_data,&l_temp,4);
1474 l_src_data+=sizeof(OPJ_INT32);
1476 *(l_dest_data++) = (OPJ_INT32) l_temp;
1480 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1482 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1483 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1487 for (i=0;i<p_nb_elem;++i) {
1488 opj_read_float(l_src_data,&l_temp);
1490 l_src_data+=sizeof(OPJ_FLOAT32);
1492 *(l_dest_data++) = (OPJ_INT32) l_temp;
1496 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1498 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1499 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1503 for (i=0;i<p_nb_elem;++i) {
1504 opj_read_double(l_src_data,&l_temp);
1506 l_src_data+=sizeof(OPJ_FLOAT64);
1508 *(l_dest_data++) = (OPJ_INT32) l_temp;
1512 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1514 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1515 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1519 for (i=0;i<p_nb_elem;++i) {
1520 l_temp = (OPJ_UINT32) *(l_src_data++);
1522 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1524 l_dest_data+=sizeof(OPJ_INT16);
1528 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1530 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1531 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1535 for (i=0;i<p_nb_elem;++i) {
1536 l_temp = (OPJ_UINT32) *(l_src_data++);
1538 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1540 l_dest_data+=sizeof(OPJ_INT32);
1544 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1546 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1547 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1551 for (i=0;i<p_nb_elem;++i) {
1552 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1554 opj_write_float(l_dest_data,l_temp);
1556 l_dest_data+=sizeof(OPJ_FLOAT32);
1560 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1562 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1563 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1567 for (i=0;i<p_nb_elem;++i) {
1568 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1570 opj_write_double(l_dest_data,l_temp);
1572 l_dest_data+=sizeof(OPJ_FLOAT64);
1578 * Converts an enum type progression order to string type.
1580 * @param prg_order the progression order to get.
1582 * @return the string representation of the given progression order.
1584 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1585 j2k_prog_order_t *po;
1586 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1587 if(po->enum_prog == prg_order){
1588 return po->str_prog;
1591 return po->str_prog;
1595 * Checks the progression order changes values. Tells if the poc given as input are valid.
1597 * @param p_pocs the progression order changes.
1598 * @param p_nb_pocs the number of progression order changes.
1599 * @param p_nb_resolutions the number of resolutions.
1600 * @param numcomps the number of components
1601 * @param numlayers the number of layers.
1602 * @param p_manager the user event manager.
1604 * @return true if the pocs are valid.
1606 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1607 OPJ_UINT32 p_nb_pocs,
1608 OPJ_UINT32 p_nb_resolutions,
1609 OPJ_UINT32 p_num_comps,
1610 OPJ_UINT32 p_num_layers,
1611 opj_event_mgr_t * p_manager)
1613 OPJ_UINT32* packet_array;
1614 OPJ_UINT32 index , resno, compno, layno;
1616 OPJ_UINT32 step_c = 1;
1617 OPJ_UINT32 step_r = p_num_comps * step_c;
1618 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1619 opj_bool loss = OPJ_FALSE;
1620 OPJ_UINT32 layno0 = 0;
1622 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1623 if (packet_array == 00) {
1624 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1627 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1629 if (p_nb_pocs == 0) {
1633 index = step_r * p_pocs->resno0;
1634 // take each resolution for each poc
1635 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1637 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1639 // take each comp of each resolution for each poc
1640 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1641 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1643 // and finally take each layer of each res of ...
1644 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1645 //index = step_r * resno + step_c * compno + step_l * layno;
1646 packet_array[comp_index] = 1;
1647 comp_index += step_l;
1650 res_index += step_c;
1657 // iterate through all the pocs
1658 for (i = 1; i < p_nb_pocs ; ++i) {
1659 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1661 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1662 index = step_r * p_pocs->resno0;
1664 // take each resolution for each poc
1665 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1666 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1668 // take each comp of each resolution for each poc
1669 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1670 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1672 // and finally take each layer of each res of ...
1673 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1674 //index = step_r * resno + step_c * compno + step_l * layno;
1675 packet_array[comp_index] = 1;
1676 comp_index += step_l;
1679 res_index += step_c;
1689 for (layno = 0; layno < p_num_layers ; ++layno) {
1690 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1691 for (compno = 0; compno < p_num_comps; ++compno) {
1692 loss |= (packet_array[index]!=1);
1693 //index = step_r * resno + step_c * compno + step_l * layno;
1700 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1703 opj_free(packet_array);
1708 /* ----------------------------------------------------------------------- */
1709 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1712 int tpnum=1,tpend=0;
1713 opj_tcp_t *tcp = &cp->tcps[tileno];
1714 prog = j2k_convert_progression_order(tcp->prg);
1719 if( cp->tp_flag == prog[i] ){
1720 tpend=1;cp->tp_pos=i;
1724 tpnum= tpnum * tcp->pocs[pino].compE;
1727 tpnum= tpnum * tcp->pocs[pino].resE;
1730 tpnum= tpnum * tcp->pocs[pino].prcE;
1733 tpnum= tpnum * tcp->pocs[pino].layE;
1745 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1747 * @param cp the coding parameters.
1748 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1749 * @param tileno the given tile.
1751 * @return the number of tile parts.
1753 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1755 const OPJ_CHAR *prog = 00;
1757 OPJ_UINT32 tpnum = 1;
1758 opj_tcp_v2_t *tcp = 00;
1759 opj_poc_t * l_current_poc = 00;
1762 assert(tileno < (cp->tw * cp->th));
1763 assert(pino < (cp->tcps[tileno].numpocs + 1));
1765 /* get the given tile coding parameter */
1766 tcp = &cp->tcps[tileno];
1769 l_current_poc = &(tcp->pocs[pino]);
1770 assert(l_current_poc != 0);
1772 /* get the progression order as a character string */
1773 prog = j2k_convert_progression_order(tcp->prg);
1774 assert(strlen(prog) > 0);
1776 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1780 /* component wise */
1782 tpnum *= l_current_poc->compE;
1784 /* resolution wise */
1786 tpnum *= l_current_poc->resE;
1790 tpnum *= l_current_poc->prcE;
1794 tpnum *= l_current_poc->layE;
1797 /* whould we split here ? */
1798 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1799 cp->m_specific_param.m_enc.m_tp_pos=i;
1811 /** mem allocation for TLM marker*/
1812 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1813 int pino,tileno,totnum_tp=0;
1815 OPJ_ARG_NOT_USED(img_numcomp);
1817 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1818 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1819 int cur_totnum_tp = 0;
1820 opj_tcp_t *tcp = &cp->tcps[tileno];
1821 for(pino = 0; pino <= tcp->numpocs; pino++) {
1823 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1824 if(!pi) { return -1;}
1825 tp_num = j2k_get_num_tp(cp,pino,tileno);
1826 totnum_tp = totnum_tp + tp_num;
1827 cur_totnum_tp = cur_totnum_tp + tp_num;
1828 pi_destroy(pi, cp, tileno);
1830 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1832 if (j2k->cstr_info) {
1833 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1834 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1842 * Calculates the total number of tile parts needed by the encoder to
1843 * encode such an image. If not enough memory is available, then the function return false.
1845 * @param p_nb_tiles pointer that will hold the number of tile parts.
1846 * @param cp the coding parameters for the image.
1847 * @param image the image to encode.
1848 * @param p_j2k the p_j2k encoder.
1849 * @param p_manager the user event manager.
1851 * @return true if the function was successful, false else.
1853 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1855 OPJ_UINT32 * p_nb_tiles,
1857 opj_event_mgr_t * p_manager)
1859 OPJ_UINT32 pino,tileno;
1860 OPJ_UINT32 l_nb_tiles;
1864 assert(p_nb_tiles != 00);
1866 assert(image != 00);
1867 assert(p_j2k != 00);
1868 assert(p_manager != 00);
1870 l_nb_tiles = cp->tw * cp->th;
1875 /* TODO mergeV2: check this part which use cstr_info */
1876 /*if (p_j2k->cstr_info) {
1877 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1879 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1880 OPJ_UINT32 cur_totnum_tp = 0;
1882 pi_update_encoding_parameters(image,cp,tileno);
1884 for (pino = 0; pino <= tcp->numpocs; ++pino)
1886 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1888 *p_nb_tiles = *p_nb_tiles + tp_num;
1890 cur_totnum_tp += tp_num;
1893 tcp->m_nb_tile_parts = cur_totnum_tp;
1895 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1896 if (l_info_tile_ptr->tp == 00) {
1900 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1902 l_info_tile_ptr->num_tps = cur_totnum_tp;
1909 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1910 OPJ_UINT32 cur_totnum_tp = 0;
1912 pi_update_encoding_parameters(image,cp,tileno);
1914 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1915 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1917 *p_nb_tiles = *p_nb_tiles + tp_num;
1919 cur_totnum_tp += tp_num;
1921 tcp->m_nb_tile_parts = cur_totnum_tp;
1930 static void j2k_write_soc(opj_j2k_t *j2k) {
1931 opj_cio_t *cio = j2k->cio;
1932 cio_write(cio, J2K_MS_SOC, 2);
1935 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1940 /* update markers struct */
1941 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1942 #endif /* USE_JPWL */
1947 * Writes the SOC marker (Start Of Codestream)
1949 * @param p_stream the stream to write data to.
1950 * @param p_j2k J2K codec.
1951 * @param p_manager the user event manager.
1953 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1954 struct opj_stream_private *p_stream,
1955 struct opj_event_mgr * p_manager )
1957 /* 2 bytes will be written */
1958 OPJ_BYTE * l_start_stream = 00;
1961 assert(p_stream != 00);
1962 assert(p_j2k != 00);
1963 assert(p_manager != 00);
1965 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1967 /* write SOC identifier */
1968 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1970 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1976 /* update markers struct */
1978 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1980 assert( 0 && "TODO" );
1981 #endif /* USE_JPWL */
1990 * Reads a SOC marker (Start of Codestream)
1991 * @param p_header_data the data contained in the SOC box.
1992 * @param jp2 the jpeg2000 file codec.
1993 * @param p_header_size the size of the data contained in the SOC marker.
1994 * @param p_manager the user event manager.
1996 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1997 struct opj_stream_private *p_stream,
1998 struct opj_event_mgr * p_manager )
2000 OPJ_BYTE l_data [2];
2001 OPJ_UINT32 l_marker;
2004 assert(p_j2k != 00);
2005 assert(p_manager != 00);
2006 assert(p_stream != 00);
2008 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2012 opj_read_bytes(l_data,&l_marker,2);
2013 if (l_marker != J2K_MS_SOC) {
2017 /* Next marker should be a SIZ marker in the main header */
2018 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2020 /* FIXME move it in a index structure included in p_j2k*/
2021 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2023 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2025 /* Add the marker to the codestream index*/
2026 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2031 static void j2k_write_siz(opj_j2k_t *j2k) {
2035 opj_cio_t *cio = j2k->cio;
2036 opj_image_t *image = j2k->image;
2037 opj_cp_t *cp = j2k->cp;
2039 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2040 lenp = cio_tell(cio);
2042 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2043 cio_write(cio, image->x1, 4); /* Xsiz */
2044 cio_write(cio, image->y1, 4); /* Ysiz */
2045 cio_write(cio, image->x0, 4); /* X0siz */
2046 cio_write(cio, image->y0, 4); /* Y0siz */
2047 cio_write(cio, cp->tdx, 4); /* XTsiz */
2048 cio_write(cio, cp->tdy, 4); /* YTsiz */
2049 cio_write(cio, cp->tx0, 4); /* XT0siz */
2050 cio_write(cio, cp->ty0, 4); /* YT0siz */
2051 cio_write(cio, image->numcomps, 2); /* Csiz */
2052 for (i = 0; i < image->numcomps; i++) {
2053 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2054 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2055 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2057 len = cio_tell(cio) - lenp;
2058 cio_seek(cio, lenp);
2059 cio_write(cio, len, 2); /* Lsiz */
2060 cio_seek(cio, lenp + len);
2063 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2067 * Writes the SIZ marker (image and tile size)
2069 * @param p_stream the stream to write data to.
2070 * @param p_j2k J2K codec.
2071 * @param p_manager the user event manager.
2073 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2074 struct opj_stream_private *p_stream,
2075 struct opj_event_mgr * p_manager )
2078 OPJ_UINT32 l_size_len;
2079 OPJ_BYTE * l_current_ptr;
2080 opj_image_t * l_image = 00;
2081 opj_cp_v2_t *cp = 00;
2082 opj_image_comp_t * l_img_comp = 00;
2085 assert(p_stream != 00);
2086 assert(p_j2k != 00);
2087 assert(p_manager != 00);
2089 l_image = p_j2k->m_private_image;
2090 cp = &(p_j2k->m_cp);
2091 l_size_len = 40 + 3 * l_image->numcomps;
2092 l_img_comp = l_image->comps;
2094 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2096 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2097 = (OPJ_BYTE*)opj_realloc(
2098 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2100 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2104 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2107 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2109 /* write SOC identifier */
2110 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2113 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2116 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2119 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2122 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2125 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2128 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2131 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2134 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2137 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2140 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2143 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2146 for (i = 0; i < l_image->numcomps; ++i) {
2147 /* TODO here with MCT ? */
2148 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2151 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2154 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2160 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) {
2170 * Reads a SIZ marker (image and tile size)
2171 * @param p_header_data the data contained in the SIZ box.
2172 * @param jp2 the jpeg2000 file codec.
2173 * @param p_header_size the size of the data contained in the SIZ marker.
2174 * @param p_manager the user event manager.
2176 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2177 OPJ_BYTE * p_header_data,
2178 OPJ_UINT32 p_header_size,
2179 opj_event_mgr_t * p_manager
2182 OPJ_UINT32 l_size, i;
2183 OPJ_UINT32 l_nb_comp;
2184 OPJ_UINT32 l_nb_comp_remain;
2185 OPJ_UINT32 l_remaining_size;
2186 OPJ_UINT32 l_nb_tiles;
2188 opj_image_t *l_image = 00;
2189 opj_cp_v2_t *l_cp = 00;
2190 opj_image_comp_t * l_img_comp = 00;
2191 opj_tcp_v2_t * l_current_tile_param = 00;
2194 assert(p_j2k != 00);
2195 assert(p_manager != 00);
2196 assert(p_header_data != 00);
2198 l_image = p_j2k->m_private_image;
2199 l_cp = &(p_j2k->m_cp);
2201 /* minimum size == 39 - 3 (= minimum component parameter) */
2202 if (p_header_size < 36) {
2203 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2207 l_remaining_size = p_header_size - 36;
2208 l_nb_comp = l_remaining_size / 3;
2209 l_nb_comp_remain = l_remaining_size % 3;
2210 if (l_nb_comp_remain != 0){
2211 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2215 l_size = p_header_size + 2; /* Lsiz */
2217 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2219 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2220 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2222 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2224 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2226 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2228 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2230 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2232 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2234 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2236 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2239 l_image->numcomps = (OPJ_UINT16) l_tmp;
2241 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2245 if (l_image->numcomps != l_nb_comp) {
2246 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);
2251 if (l_cp->correct) {
2252 /* if JPWL is on, we check whether TX errors have damaged
2253 too much the SIZ parameters */
2254 if (!(l_image->x1 * l_image->y1)) {
2255 opj_event_msg_v2(p_manager, EVT_ERROR,
2256 "JPWL: bad image size (%d x %d)\n",
2257 l_image->x1, l_image->y1);
2258 if (!JPWL_ASSUME || JPWL_ASSUME) {
2259 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2264 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2265 if (l_image->numcomps != ((len - 38) / 3)) {
2266 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2267 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2268 l_image->numcomps, ((len - 38) / 3));
2270 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2273 */ /* we try to correct */
2274 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2275 if (l_image->numcomps < ((len - 38) / 3)) {
2276 len = 38 + 3 * l_image->numcomps;
2277 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2280 l_image->numcomps = ((len - 38) / 3);
2281 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2287 /* update components number in the jpwl_exp_comps filed */
2288 l_cp->exp_comps = l_image->numcomps;
2290 #endif /* USE_JPWL */
2292 /* Allocate the resulting image components */
2293 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2294 if (l_image->comps == 00){
2295 l_image->numcomps = 0;
2296 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2300 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2301 l_img_comp = l_image->comps;
2303 /* Read the component information */
2304 for (i = 0; i < l_image->numcomps; ++i){
2306 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2308 l_img_comp->prec = (tmp & 0x7f) + 1;
2309 l_img_comp->sgnd = tmp >> 7;
2310 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2312 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2313 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2315 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2318 if (l_cp->correct) {
2319 /* if JPWL is on, we check whether TX errors have damaged
2320 too much the SIZ parameters, again */
2321 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2322 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2323 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2324 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2326 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2329 /* we try to correct */
2330 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2331 if (!l_image->comps[i].dx) {
2332 l_image->comps[i].dx = 1;
2333 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2334 i, l_image->comps[i].dx);
2336 if (!l_image->comps[i].dy) {
2337 l_image->comps[i].dy = 1;
2338 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2339 i, l_image->comps[i].dy);
2343 #endif /* USE_JPWL */
2344 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2345 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2349 /* Compute the number of tiles */
2350 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2351 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2352 l_nb_tiles = l_cp->tw * l_cp->th;
2354 /* Define the tiles which will be decoded */
2355 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2356 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;
2357 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;
2358 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);
2359 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);
2362 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2363 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2364 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2365 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2369 if (l_cp->correct) {
2370 /* if JPWL is on, we check whether TX errors have damaged
2371 too much the SIZ parameters */
2372 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2373 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2374 "JPWL: bad number of tiles (%d x %d)\n",
2375 l_cp->tw, l_cp->th);
2377 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2380 /* we try to correct */
2381 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2384 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2387 if (l_cp->tw > l_cp->max_tiles) {
2389 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2390 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2391 l_cp->max_tiles, l_cp->tw);
2395 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2398 if (l_cp->th > l_cp->max_tiles) {
2400 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2401 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2402 l_cp->max_tiles, l_cp->th);
2406 #endif /* USE_JPWL */
2408 /* memory allocations */
2409 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2410 if (l_cp->tcps == 00) {
2411 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2414 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2417 if (l_cp->correct) {
2419 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2420 "JPWL: could not alloc tcps field of cp\n");
2421 if (!JPWL_ASSUME || JPWL_ASSUME) {
2422 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2427 #endif /* USE_JPWL */
2429 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2430 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2431 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2435 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2437 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2438 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2440 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2441 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2444 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));
2445 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2447 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2448 (opj_simple_mcc_decorrelation_data_t*)
2449 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2451 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2452 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2455 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));
2456 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2458 /* set up default dc level shift */
2459 for (i=0;i<l_image->numcomps;++i) {
2460 if (! l_image->comps[i].sgnd) {
2461 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2465 l_current_tile_param = l_cp->tcps;
2466 for (i = 0; i < l_nb_tiles; ++i) {
2467 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2468 if (l_current_tile_param->tccps == 00) {
2469 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2472 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2474 ++l_current_tile_param;
2477 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2478 opj_image_comp_header_update(l_image,l_cp);
2485 static void j2k_write_com(opj_j2k_t *j2k) {
2489 if(j2k->cp->comment) {
2490 opj_cio_t *cio = j2k->cio;
2491 char *comment = j2k->cp->comment;
2493 cio_write(cio, J2K_MS_COM, 2);
2494 lenp = cio_tell(cio);
2496 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2497 for (i = 0; i < strlen(comment); i++) {
2498 cio_write(cio, comment[i], 1);
2500 len = cio_tell(cio) - lenp;
2501 cio_seek(cio, lenp);
2502 cio_write(cio, len, 2);
2503 cio_seek(cio, lenp + len);
2507 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2513 * Writes the COM marker (comment)
2515 * @param p_stream the stream to write data to.
2516 * @param p_j2k J2K codec.
2517 * @param p_manager the user event manager.
2519 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2520 struct opj_stream_private *p_stream,
2521 struct opj_event_mgr * p_manager )
2523 OPJ_UINT32 l_comment_size;
2524 OPJ_UINT32 l_total_com_size;
2525 const OPJ_CHAR *l_comment;
2526 OPJ_BYTE * l_current_ptr = 00;
2529 assert(p_j2k != 00);
2530 assert(p_stream != 00);
2531 assert(p_manager != 00);
2533 l_comment = p_j2k->m_cp.comment;
2534 l_comment_size = strlen(l_comment);
2535 l_total_com_size = l_comment_size + 6;
2537 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2538 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2539 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2542 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2546 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2549 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2551 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2554 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2557 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2560 memcpy( l_current_ptr,l_comment,l_comment_size);
2562 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) {
2570 * Reads a COM marker (comments)
2571 * @param p_header_data the data contained in the COM box.
2572 * @param jp2 the jpeg2000 file codec.
2573 * @param p_header_size the size of the data contained in the COM marker.
2574 * @param p_manager the user event manager.
2576 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2577 OPJ_BYTE * p_header_data,
2578 OPJ_UINT32 p_header_size,
2579 opj_event_mgr_t * p_manager
2583 assert(p_j2k != 00);
2584 assert(p_manager != 00);
2585 assert(p_header_data != 00);
2586 (void)p_header_size;
2591 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2594 opj_cp_t *cp = j2k->cp;
2595 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2596 opj_tccp_t *tccp = &tcp->tccps[compno];
2597 opj_cio_t *cio = j2k->cio;
2599 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2600 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2601 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2602 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2603 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2605 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2606 for (i = 0; i < tccp->numresolutions; i++) {
2607 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2613 static void j2k_write_cod(opj_j2k_t *j2k) {
2614 opj_cp_t *cp = NULL;
2615 opj_tcp_t *tcp = NULL;
2618 opj_cio_t *cio = j2k->cio;
2620 cio_write(cio, J2K_MS_COD, 2); /* COD */
2622 lenp = cio_tell(cio);
2626 tcp = &cp->tcps[j2k->curtileno];
2628 cio_write(cio, tcp->csty, 1); /* Scod */
2629 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2630 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2631 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2633 j2k_write_cox(j2k, 0);
2634 len = cio_tell(cio) - lenp;
2635 cio_seek(cio, lenp);
2636 cio_write(cio, len, 2); /* Lcod */
2637 cio_seek(cio, lenp + len);
2640 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2645 * Writes the COD marker (Coding style default)
2647 * @param p_stream the stream to write data to.
2648 * @param p_j2k J2K codec.
2649 * @param p_manager the user event manager.
2651 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2652 struct opj_stream_private *p_stream,
2653 struct opj_event_mgr * p_manager )
2655 opj_cp_v2_t *l_cp = 00;
2656 opj_tcp_v2_t *l_tcp = 00;
2657 OPJ_UINT32 l_code_size,l_remaining_size;
2658 OPJ_BYTE * l_current_data = 00;
2661 assert(p_j2k != 00);
2662 assert(p_manager != 00);
2663 assert(p_stream != 00);
2665 l_cp = &(p_j2k->m_cp);
2666 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2667 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2668 l_remaining_size = l_code_size;
2670 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2671 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2672 = (OPJ_BYTE*)opj_realloc(
2673 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2676 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2680 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2683 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2685 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2686 l_current_data += 2;
2688 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2689 l_current_data += 2;
2691 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2694 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2697 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2700 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2703 l_remaining_size -= 9;
2705 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2710 if (l_remaining_size != 0) {
2711 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2715 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) {
2724 * Reads a COD marker (Coding Styke defaults)
2725 * @param p_header_data the data contained in the COD box.
2726 * @param p_j2k the jpeg2000 codec.
2727 * @param p_header_size the size of the data contained in the COD marker.
2728 * @param p_manager the user event manager.
2730 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2731 OPJ_BYTE * p_header_data,
2732 OPJ_UINT32 p_header_size,
2733 opj_event_mgr_t * p_manager
2739 opj_cp_v2_t *l_cp = 00;
2740 opj_tcp_v2_t *l_tcp = 00;
2741 opj_image_t *l_image = 00;
2744 assert(p_header_data != 00);
2745 assert(p_j2k != 00);
2746 assert(p_manager != 00);
2748 l_image = p_j2k->m_private_image;
2749 l_cp = &(p_j2k->m_cp);
2751 /* If we are in the first tile-part header of the current tile */
2752 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2753 &l_cp->tcps[p_j2k->m_current_tile_number] :
2754 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2756 /* Make sure room is sufficient */
2757 if (p_header_size < 5) {
2758 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2762 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2764 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2766 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2767 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2770 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2771 if (l_cp->m_specific_param.m_dec.m_layer) {
2772 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2775 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2778 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2782 for (i = 0; i < l_image->numcomps; ++i) {
2783 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2786 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2787 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2791 if (p_header_size != 0) {
2792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2796 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2797 opj_j2k_copy_tile_component_parameters(p_j2k);
2800 #ifdef WIP_REMOVE_MSD
2801 if (p_j2k->cstr_info) {
2802 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2803 p_j2k->cstr_info->prog = l_tcp->prg;
2804 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2805 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2806 for (i = 0; i < l_image->numcomps; ++i) {
2807 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2815 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2818 opj_cp_t *cp = j2k->cp;
2819 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2820 opj_image_t *image = j2k->image;
2821 opj_cio_t *cio = j2k->cio;
2823 cio_write(cio, J2K_MS_COC, 2); /* COC */
2824 lenp = cio_tell(cio);
2826 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2827 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2828 j2k_write_cox(j2k, compno);
2829 len = cio_tell(cio) - lenp;
2830 cio_seek(cio, lenp);
2831 cio_write(cio, len, 2); /* Lcoc */
2832 cio_seek(cio, lenp + len);
2836 * Writes the COC marker (Coding style component)
2838 * @param p_comp_no the index of the component to output.
2839 * @param p_stream the stream to write data to.
2840 * @param p_j2k J2K codec.
2841 * @param p_manager the user event manager.
2843 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2844 OPJ_UINT32 p_comp_no,
2845 struct opj_stream_private *p_stream,
2846 struct opj_event_mgr * p_manager )
2848 OPJ_UINT32 l_coc_size,l_remaining_size;
2849 OPJ_UINT32 l_comp_room;
2852 assert(p_j2k != 00);
2853 assert(p_manager != 00);
2854 assert(p_stream != 00);
2856 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2858 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2860 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2861 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2862 = (OPJ_BYTE*)opj_realloc(
2863 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2865 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2869 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2872 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);
2874 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) {
2882 * Writes the COC marker (Coding style component)
2884 * @param p_comp_no the index of the component to output.
2885 * @param p_stream the stream to write data to.
2886 * @param p_j2k J2K codec.
2887 * @param p_manager the user event manager.
2889 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2890 OPJ_UINT32 p_comp_no,
2892 OPJ_UINT32 * p_data_written,
2893 struct opj_event_mgr * p_manager )
2895 opj_cp_v2_t *l_cp = 00;
2896 opj_tcp_v2_t *l_tcp = 00;
2897 OPJ_UINT32 l_coc_size,l_remaining_size;
2898 OPJ_BYTE * l_current_data = 00;
2899 opj_image_t *l_image = 00;
2900 OPJ_UINT32 l_comp_room;
2903 assert(p_j2k != 00);
2904 assert(p_manager != 00);
2906 l_cp = &(p_j2k->m_cp);
2907 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2908 l_image = p_j2k->m_private_image;
2909 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2911 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2912 l_remaining_size = l_coc_size;
2914 l_current_data = p_data;
2916 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2917 l_current_data += 2;
2919 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2920 l_current_data += 2;
2922 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2923 l_current_data+=l_comp_room;
2925 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2928 l_remaining_size -= (5 + l_comp_room);
2929 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2930 * p_data_written = l_coc_size;
2934 * Gets the maximum size taken by a coc.
2936 * @param p_j2k the jpeg2000 codec to use.
2938 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2941 OPJ_UINT32 l_nb_comp;
2942 OPJ_UINT32 l_nb_tiles;
2943 OPJ_UINT32 l_max = 0;
2947 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2948 l_nb_comp = p_j2k->m_private_image->numcomps;
2950 for (i=0;i<l_nb_tiles;++i) {
2951 for (j=0;j<l_nb_comp;++j) {
2952 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2961 * Reads a COC marker (Coding Style Component)
2962 * @param p_header_data the data contained in the COC box.
2963 * @param p_j2k the jpeg2000 codec.
2964 * @param p_header_size the size of the data contained in the COC marker.
2965 * @param p_manager the user event manager.
2967 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2968 OPJ_BYTE * p_header_data,
2969 OPJ_UINT32 p_header_size,
2970 opj_event_mgr_t * p_manager
2973 opj_cp_v2_t *l_cp = NULL;
2974 opj_tcp_v2_t *l_tcp = NULL;
2975 opj_image_t *l_image = NULL;
2976 OPJ_UINT32 l_comp_room;
2977 OPJ_UINT32 l_comp_no;
2980 assert(p_header_data != 00);
2981 assert(p_j2k != 00);
2982 assert(p_manager != 00);
2984 l_cp = &(p_j2k->m_cp);
2985 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2986 &l_cp->tcps[p_j2k->m_current_tile_number] :
2987 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2988 l_image = p_j2k->m_private_image;
2990 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2992 /* make sure room is sufficient*/
2993 if (p_header_size < l_comp_room + 1) {
2994 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2997 p_header_size -= l_comp_room + 1;
2999 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3000 p_header_data += l_comp_room;
3001 if (l_comp_no >= l_image->numcomps) {
3002 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3006 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3009 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3010 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3014 if (p_header_size != 0) {
3015 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3021 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3022 int bandno, numbands;
3025 opj_cp_t *cp = j2k->cp;
3026 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3027 opj_tccp_t *tccp = &tcp->tccps[compno];
3028 opj_cio_t *cio = j2k->cio;
3030 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3031 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3033 for (bandno = 0; bandno < numbands; bandno++) {
3034 expn = tccp->stepsizes[bandno].expn;
3035 mant = tccp->stepsizes[bandno].mant;
3037 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3038 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3040 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3046 static void j2k_write_qcd(opj_j2k_t *j2k) {
3049 opj_cio_t *cio = j2k->cio;
3051 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3052 lenp = cio_tell(cio);
3054 j2k_write_qcx(j2k, 0);
3055 len = cio_tell(cio) - lenp;
3056 cio_seek(cio, lenp);
3057 cio_write(cio, len, 2); /* Lqcd */
3058 cio_seek(cio, lenp + len);
3061 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3065 * Writes the QCD marker (quantization default)
3067 * @param p_comp_number the index of the component to output.
3068 * @param p_stream the stream to write data to.
3069 * @param p_j2k J2K codec.
3070 * @param p_manager the user event manager.
3072 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3073 struct opj_stream_private *p_stream,
3074 struct opj_event_mgr * p_manager )
3076 opj_cp_v2_t *l_cp = 00;
3077 opj_tcp_v2_t *l_tcp = 00;
3078 OPJ_UINT32 l_qcd_size,l_remaining_size;
3079 OPJ_BYTE * l_current_data = 00;
3082 assert(p_j2k != 00);
3083 assert(p_manager != 00);
3084 assert(p_stream != 00);
3086 l_cp = &(p_j2k->m_cp);
3087 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3088 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3089 l_remaining_size = l_qcd_size;
3091 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3092 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3093 = (OPJ_BYTE*)opj_realloc(
3094 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3097 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3101 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3104 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3106 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3107 l_current_data += 2;
3109 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3110 l_current_data += 2;
3112 l_remaining_size -= 4;
3114 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3115 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3119 if (l_remaining_size != 0) {
3120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3124 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) {
3132 * Reads a QCD marker (Quantization defaults)
3133 * @param p_header_data the data contained in the QCD box.
3134 * @param p_j2k the jpeg2000 codec.
3135 * @param p_header_size the size of the data contained in the QCD marker.
3136 * @param p_manager the user event manager.
3138 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3139 OPJ_BYTE * p_header_data,
3140 OPJ_UINT32 p_header_size,
3141 opj_event_mgr_t * p_manager
3145 assert(p_header_data != 00);
3146 assert(p_j2k != 00);
3147 assert(p_manager != 00);
3149 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3150 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3154 if (p_header_size != 0) {
3155 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3159 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3160 opj_j2k_copy_tile_quantization_parameters(p_j2k);
3165 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3168 opj_cio_t *cio = j2k->cio;
3170 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3171 lenp = cio_tell(cio);
3173 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3174 j2k_write_qcx(j2k, compno);
3175 len = cio_tell(cio) - lenp;
3176 cio_seek(cio, lenp);
3177 cio_write(cio, len, 2); /* Lqcc */
3178 cio_seek(cio, lenp + len);
3182 * Writes the QCC marker (quantization component)
3184 * @param p_comp_no the index of the component to output.
3185 * @param p_stream the stream to write data to.
3186 * @param p_j2k J2K codec.
3187 * @param p_manager the user event manager.
3189 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3190 OPJ_UINT32 p_comp_no,
3191 struct opj_stream_private *p_stream,
3192 struct opj_event_mgr * p_manager )
3194 OPJ_UINT32 l_qcc_size,l_remaining_size;
3197 assert(p_j2k != 00);
3198 assert(p_manager != 00);
3199 assert(p_stream != 00);
3201 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3202 l_remaining_size = l_qcc_size;
3204 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3205 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3206 = (OPJ_BYTE*)opj_realloc(
3207 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3209 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3213 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3216 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);
3218 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) {
3226 * Writes the QCC marker (quantization component)
3228 * @param p_comp_no the index of the component to output.
3229 * @param p_stream the stream to write data to.
3230 * @param p_j2k J2K codec.
3231 * @param p_manager the user event manager.
3233 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3234 OPJ_UINT32 p_comp_no,
3236 OPJ_UINT32 * p_data_written,
3237 struct opj_event_mgr * p_manager )
3239 OPJ_UINT32 l_qcc_size,l_remaining_size;
3240 OPJ_BYTE * l_current_data = 00;
3243 assert(p_j2k != 00);
3244 assert(p_manager != 00);
3246 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3247 l_remaining_size = l_qcc_size;
3249 l_current_data = p_data;
3251 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3252 l_current_data += 2;
3254 if (p_j2k->m_private_image->numcomps <= 256) {
3257 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3258 l_current_data += 2;
3260 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3263 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3264 l_remaining_size -= 6;
3267 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3268 l_current_data += 2;
3270 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3273 l_remaining_size -= 6;
3276 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3278 *p_data_written = l_qcc_size;
3282 * Gets the maximum size taken by a qcc.
3284 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3286 return j2k_get_max_coc_size(p_j2k);
3290 * Reads a QCC marker (Quantization component)
3291 * @param p_header_data the data contained in the QCC box.
3292 * @param p_j2k the jpeg2000 codec.
3293 * @param p_header_size the size of the data contained in the QCC marker.
3294 * @param p_manager the user event manager.
3296 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3297 OPJ_BYTE * p_header_data,
3298 OPJ_UINT32 p_header_size,
3299 opj_event_mgr_t * p_manager
3302 OPJ_UINT32 l_num_comp,l_comp_no;
3305 assert(p_header_data != 00);
3306 assert(p_j2k != 00);
3307 assert(p_manager != 00);
3309 l_num_comp = p_j2k->m_private_image->numcomps;
3311 if (l_num_comp <= 256) {
3312 if (p_header_size < 1) {
3313 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3316 opj_read_bytes(p_header_data,&l_comp_no,1);
3321 if (p_header_size < 2) {
3322 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3325 opj_read_bytes(p_header_data,&l_comp_no,2);
3331 if (p_j2k->m_cp.correct) {
3333 static OPJ_UINT32 backup_compno = 0;
3335 /* compno is negative or larger than the number of components!!! */
3336 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3337 opj_event_msg_v2(p_manager, EVT_ERROR,
3338 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3339 l_comp_no, l_num_comp);
3341 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3344 /* we try to correct */
3345 l_comp_no = backup_compno % l_num_comp;
3346 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3347 "- setting component number to %d\n",
3351 /* keep your private count of tiles */
3354 #endif /* USE_JPWL */
3356 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3357 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3361 if (p_header_size != 0) {
3362 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3370 static void j2k_write_poc(opj_j2k_t *j2k) {
3371 int len, numpchgs, i;
3373 int numcomps = j2k->image->numcomps;
3375 opj_cp_t *cp = j2k->cp;
3376 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3377 opj_tccp_t *tccp = &tcp->tccps[0];
3378 opj_cio_t *cio = j2k->cio;
3380 numpchgs = 1 + tcp->numpocs;
3381 cio_write(cio, J2K_MS_POC, 2); /* POC */
3382 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3383 cio_write(cio, len, 2); /* Lpoc */
3384 for (i = 0; i < numpchgs; i++) {
3385 opj_poc_t *poc = &tcp->pocs[i];
3386 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3387 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3388 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3389 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3390 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3391 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3392 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3393 poc->compno1 = int_min(poc->compno1, numcomps);
3394 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3399 * Writes the POC marker (Progression Order Change)
3401 * @param p_stream the stream to write data to.
3402 * @param p_j2k J2K codec.
3403 * @param p_manager the user event manager.
3405 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3406 struct opj_stream_private *p_stream,
3407 struct opj_event_mgr * p_manager )
3409 OPJ_UINT32 l_nb_comp;
3410 OPJ_UINT32 l_nb_poc;
3411 OPJ_UINT32 l_poc_size;
3412 OPJ_UINT32 l_written_size = 0;
3413 opj_tcp_v2_t *l_tcp = 00;
3414 opj_tccp_t *l_tccp = 00;
3415 OPJ_UINT32 l_poc_room;
3418 assert(p_j2k != 00);
3419 assert(p_manager != 00);
3420 assert(p_stream != 00);
3422 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3423 l_tccp = &l_tcp->tccps[0];
3424 l_nb_comp = p_j2k->m_private_image->numcomps;
3425 l_nb_poc = 1 + l_tcp->numpocs;
3427 if (l_nb_comp <= 256) {
3433 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3435 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3436 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3437 = (OPJ_BYTE*)opj_realloc(
3438 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3441 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3445 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3448 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3450 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) {
3459 * Writes the POC marker (Progression Order Change)
3461 * @param p_stream the stream to write data to.
3462 * @param p_j2k J2K codec.
3463 * @param p_manager the user event manager.
3465 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3467 OPJ_UINT32 * p_data_written,
3468 struct opj_event_mgr * p_manager )
3471 OPJ_BYTE * l_current_data = 00;
3472 OPJ_UINT32 l_nb_comp;
3473 OPJ_UINT32 l_nb_poc;
3474 OPJ_UINT32 l_poc_size;
3475 opj_image_t *l_image = 00;
3476 opj_tcp_v2_t *l_tcp = 00;
3477 opj_tccp_t *l_tccp = 00;
3478 opj_poc_t *l_current_poc = 00;
3479 OPJ_UINT32 l_poc_room;
3482 assert(p_j2k != 00);
3483 assert(p_manager != 00);
3485 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3486 l_tccp = &l_tcp->tccps[0];
3487 l_image = p_j2k->m_private_image;
3488 l_nb_comp = l_image->numcomps;
3489 l_nb_poc = 1 + l_tcp->numpocs;
3491 if (l_nb_comp <= 256) {
3498 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3500 l_current_data = p_data;
3502 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3503 l_current_data += 2;
3505 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3506 l_current_data += 2;
3508 l_current_poc = l_tcp->pocs;
3509 for (i = 0; i < l_nb_poc; ++i) {
3510 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3513 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3514 l_current_data+=l_poc_room;
3516 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3519 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3522 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3523 l_current_data+=l_poc_room;
3525 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3528 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3529 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3530 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3531 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3536 *p_data_written = l_poc_size;
3540 * Gets the maximum size taken by the writing of a POC.
3542 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3544 opj_tcp_v2_t * l_tcp = 00;
3545 OPJ_UINT32 l_nb_tiles = 0;
3546 OPJ_UINT32 l_max_poc = 0;
3549 l_tcp = p_j2k->m_cp.tcps;
3550 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3552 for (i=0;i<l_nb_tiles;++i) {
3553 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3559 return 4 + 9 * l_max_poc;
3563 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3565 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3568 OPJ_UINT32 l_nb_tiles;
3569 OPJ_UINT32 l_max = 0;
3570 opj_tcp_v2_t * l_tcp = 00;
3572 l_tcp = p_j2k->m_cp.tcps;
3573 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3575 for (i=0;i<l_nb_tiles;++i) {
3576 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3586 * Gets the maximum size taken by the headers of the SOT.
3588 * @param p_j2k the jpeg2000 codec to use.
3590 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3592 OPJ_UINT32 l_nb_bytes = 0;
3593 OPJ_UINT32 l_nb_comps;
3594 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3596 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3597 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3599 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3600 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3601 l_nb_bytes += l_nb_comps * l_coc_bytes;
3603 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3604 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3607 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3609 /*** DEVELOPER CORNER, Add room for your headers ***/
3616 * Reads a POC marker (Progression Order Change)
3618 * @param p_header_data the data contained in the POC box.
3619 * @param p_j2k the jpeg2000 codec.
3620 * @param p_header_size the size of the data contained in the POC marker.
3621 * @param p_manager the user event manager.
3623 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3624 OPJ_BYTE * p_header_data,
3625 OPJ_UINT32 p_header_size,
3626 opj_event_mgr_t * p_manager
3629 OPJ_UINT32 i, l_nb_comp, l_tmp;
3630 opj_image_t * l_image = 00;
3631 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3632 OPJ_UINT32 l_chunk_size, l_comp_room;
3634 opj_cp_v2_t *l_cp = 00;
3635 opj_tcp_v2_t *l_tcp = 00;
3636 opj_poc_t *l_current_poc = 00;
3639 assert(p_header_data != 00);
3640 assert(p_j2k != 00);
3641 assert(p_manager != 00);
3643 l_image = p_j2k->m_private_image;
3644 l_nb_comp = l_image->numcomps;
3645 if (l_nb_comp <= 256) {
3651 l_chunk_size = 5 + 2 * l_comp_room;
3652 l_current_poc_nb = p_header_size / l_chunk_size;
3653 l_current_poc_remaining = p_header_size % l_chunk_size;
3655 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3656 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3660 l_cp = &(p_j2k->m_cp);
3661 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3662 &l_cp->tcps[p_j2k->m_current_tile_number] :
3663 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3664 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3665 l_current_poc_nb += l_old_poc_nb;
3667 assert(l_current_poc_nb < 32);
3669 /* now poc is in use.*/
3672 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3673 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3674 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3676 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3677 p_header_data+=l_comp_room;
3678 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3680 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3682 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3683 p_header_data+=l_comp_room;
3684 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3686 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3687 /* make sure comp is in acceptable bounds */
3688 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3692 l_tcp->numpocs = l_current_poc_nb - 1;
3697 * Reads a CRG marker (Component registration)
3699 * @param p_header_data the data contained in the TLM box.
3700 * @param p_j2k the jpeg2000 codec.
3701 * @param p_header_size the size of the data contained in the TLM marker.
3702 * @param p_manager the user event manager.
3704 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3705 OPJ_BYTE * p_header_data,
3706 OPJ_UINT32 p_header_size,
3707 opj_event_mgr_t * p_manager
3710 OPJ_UINT32 l_nb_comp;
3712 assert(p_header_data != 00);
3713 assert(p_j2k != 00);
3714 assert(p_manager != 00);
3716 l_nb_comp = p_j2k->m_private_image->numcomps;
3718 if (p_header_size != l_nb_comp *4) {
3719 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3722 /* Do not care of this at the moment since only local variables are set here */
3725 (i = 0; i < l_nb_comp; ++i)
3727 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3729 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3737 * Reads a TLM marker (Tile Length Marker)
3739 * @param p_header_data the data contained in the TLM box.
3740 * @param p_j2k the jpeg2000 codec.
3741 * @param p_header_size the size of the data contained in the TLM marker.
3742 * @param p_manager the user event manager.
3744 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3745 OPJ_BYTE * p_header_data,
3746 OPJ_UINT32 p_header_size,
3747 opj_event_mgr_t * p_manager
3750 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3752 assert(p_header_data != 00);
3753 assert(p_j2k != 00);
3754 assert(p_manager != 00);
3756 if (p_header_size < 2) {
3757 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3762 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3764 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3767 l_ST = ((l_Stlm >> 4) & 0x3);
3768 l_SP = (l_Stlm >> 6) & 0x1;
3770 l_Ptlm_size = (l_SP + 1) * 2;
3771 l_quotient = l_Ptlm_size + l_ST;
3773 l_tot_num_tp = p_header_size / l_quotient;
3774 l_tot_num_tp_remaining = p_header_size % l_quotient;
3776 if (l_tot_num_tp_remaining != 0) {
3777 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3780 /* FIXME Do not care of this at the moment since only local variables are set here */
3783 (i = 0; i < l_tot_num_tp; ++i)
3785 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3786 p_header_data += l_ST;
3787 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3788 p_header_data += l_Ptlm_size;
3795 * Reads a PLM marker (Packet length, main header marker)
3797 * @param p_header_data the data contained in the TLM box.
3798 * @param p_j2k the jpeg2000 codec.
3799 * @param p_header_size the size of the data contained in the TLM marker.
3800 * @param p_manager the user event manager.
3802 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3803 OPJ_BYTE * p_header_data,
3804 OPJ_UINT32 p_header_size,
3805 opj_event_mgr_t * p_manager
3809 assert(p_header_data != 00);
3810 assert(p_j2k != 00);
3811 assert(p_manager != 00);
3813 if (p_header_size < 1) {
3814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3817 /* Do not care of this at the moment since only local variables are set here */
3819 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3826 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3828 p_header_size -= (1+l_Nplm);
3832 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3836 (i = 0; i < l_Nplm; ++i)
3838 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3840 // take only the last seven bytes
3841 l_packet_len |= (l_tmp & 0x7f);
3849 // store packet length and proceed to next packet
3856 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3865 * Reads a PLT marker (Packet length, tile-part header)
3867 * @param p_header_data the data contained in the PLT box.
3868 * @param p_j2k the jpeg2000 codec.
3869 * @param p_header_size the size of the data contained in the PLT marker.
3870 * @param p_manager the user event manager.
3872 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3873 OPJ_BYTE * p_header_data,
3874 OPJ_UINT32 p_header_size,
3875 opj_event_mgr_t * p_manager
3878 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3881 assert(p_header_data != 00);
3882 assert(p_j2k != 00);
3883 assert(p_manager != 00);
3885 if (p_header_size < 1) {
3886 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3890 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3894 for (i = 0; i < p_header_size; ++i) {
3895 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3897 /* take only the last seven bytes */
3898 l_packet_len |= (l_tmp & 0x7f);
3903 /* store packet length and proceed to next packet */
3908 if (l_packet_len != 0) {
3909 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3918 * Reads a PPM marker (Packed packet headers, main header)
3920 * @param p_header_data the data contained in the POC box.
3921 * @param p_j2k the jpeg2000 codec.
3922 * @param p_header_size the size of the data contained in the POC marker.
3923 * @param p_manager the user event manager.
3926 opj_bool j2k_read_ppm_v2 (
3927 opj_j2k_v2_t *p_j2k,
3928 OPJ_BYTE * p_header_data,
3929 OPJ_UINT32 p_header_size,
3930 struct opj_event_mgr * p_manager
3934 opj_cp_v2_t *l_cp = 00;
3935 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3938 assert(p_header_data != 00);
3939 assert(p_j2k != 00);
3940 assert(p_manager != 00);
3942 if (p_header_size < 1) {
3943 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3947 l_cp = &(p_j2k->m_cp);
3950 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3954 /* First PPM marker */
3956 if (p_header_size < 4) {
3957 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3961 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3965 /* First PPM marker: Initialization */
3966 l_cp->ppm_len = l_N_ppm;
3967 l_cp->ppm_data_size = 0;
3969 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3970 if (l_cp->ppm_buffer == 00) {
3971 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3974 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3976 l_cp->ppm_data = l_cp->ppm_buffer;
3980 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3981 if (p_header_size >= 4) {
3983 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3986 l_cp->ppm_len += l_N_ppm ;
3988 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3989 if (l_cp->ppm_buffer == 00) {
3990 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3993 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3995 l_cp->ppm_data = l_cp->ppm_buffer;
4002 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4004 if (l_remaining_data <= p_header_size) {
4005 /* we must store less information than available in the packet */
4006 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4007 l_cp->ppm_data_size = l_cp->ppm_len;
4008 p_header_size -= l_remaining_data;
4009 p_header_data += l_remaining_data;
4012 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4013 l_cp->ppm_data_size += p_header_size;
4014 p_header_data += p_header_size;
4027 * Reads a PPM marker (Packed packet headers, main header)
4029 * @param p_header_data the data contained in the POC box.
4030 * @param p_j2k the jpeg2000 codec.
4031 * @param p_header_size the size of the data contained in the POC marker.
4032 * @param p_manager the user event manager.
4034 opj_bool j2k_read_ppm_v3 (
4035 opj_j2k_v2_t *p_j2k,
4036 OPJ_BYTE * p_header_data,
4037 OPJ_UINT32 p_header_size,
4038 struct opj_event_mgr * p_manager
4041 opj_cp_v2_t *l_cp = 00;
4042 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4045 assert(p_header_data != 00);
4046 assert(p_j2k != 00);
4047 assert(p_manager != 00);
4049 /* Minimum size of PPM marker is equal to the size of Zppm element */
4050 if (p_header_size < 1) {
4051 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4055 l_cp = &(p_j2k->m_cp);
4058 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4062 /* First PPM marker */
4064 /* We need now at least the Nppm^0 element */
4065 if (p_header_size < 4) {
4066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4070 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4074 /* First PPM marker: Initialization */
4075 l_cp->ppm_len = l_N_ppm;
4076 l_cp->ppm_data_read = 0;
4078 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4079 if (l_cp->ppm_data == 00) {
4080 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4083 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4085 l_cp->ppm_data_current = l_cp->ppm_data;
4087 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4090 if (p_header_size < 4) {
4091 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4095 /* Uncompleted Ippm series in the previous PPM marker?*/
4096 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4097 /* Get the place where add the remaining Ippm series*/
4098 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4099 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4102 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4106 /* Increase the size of ppm_data to add the new Ippm series*/
4107 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4109 /* Keep the position of the place where concatenate the new series*/
4110 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4111 l_cp->ppm_len += l_N_ppm;
4116 l_remaining_data = p_header_size;
4118 while (l_remaining_data >= l_N_ppm) {
4119 /* read a complete Ippm series*/
4120 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4121 p_header_size -= l_N_ppm;
4122 p_header_data += l_N_ppm;
4124 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4128 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4133 l_remaining_data = p_header_size;
4137 l_remaining_data = p_header_size;
4139 /* Next Ippm series is a complete series ?*/
4140 if (l_remaining_data > l_N_ppm) {
4141 /* Increase the size of ppm_data to add the new Ippm series*/
4142 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4144 /* Keep the position of the place where concatenate the new series */
4145 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4146 l_cp->ppm_len += l_N_ppm;
4151 /* Need to read an incomplete Ippm series*/
4152 if (l_remaining_data) {
4153 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4155 /* Keep the position of the place where concatenate the new series*/
4156 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4157 l_cp->ppm_len += l_N_ppm;
4159 /* Read incomplete Ippm series*/
4160 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4161 p_header_size -= l_remaining_data;
4162 p_header_data += l_remaining_data;
4164 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4169 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4170 if (p_header_size >= 4) {
4172 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4175 l_cp->ppm_len += l_N_ppm ;
4177 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4178 if (l_cp->ppm_buffer == 00) {
4179 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4182 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4184 l_cp->ppm_data = l_cp->ppm_buffer;
4191 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4193 if (l_remaining_data <= p_header_size) {
4194 /* we must store less information than available in the packet */
4195 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4196 l_cp->ppm_data_size = l_cp->ppm_len;
4197 p_header_size -= l_remaining_data;
4198 p_header_data += l_remaining_data;
4201 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4202 l_cp->ppm_data_size += p_header_size;
4203 p_header_data += p_header_size;
4213 * Reads a PPT marker (Packed packet headers, tile-part header)
4215 * @param p_header_data the data contained in the PPT box.
4216 * @param p_j2k the jpeg2000 codec.
4217 * @param p_header_size the size of the data contained in the PPT marker.
4218 * @param p_manager the user event manager.
4220 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4221 OPJ_BYTE * p_header_data,
4222 OPJ_UINT32 p_header_size,
4223 opj_event_mgr_t * p_manager
4226 opj_cp_v2_t *l_cp = 00;
4227 opj_tcp_v2_t *l_tcp = 00;
4231 assert(p_header_data != 00);
4232 assert(p_j2k != 00);
4233 assert(p_manager != 00);
4235 /* We need to have the Z_ppt element at minimum */
4236 if (p_header_size < 1) {
4237 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4241 l_cp = &(p_j2k->m_cp);
4243 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");
4247 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4250 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4254 /* Allocate buffer to read the packet header */
4256 /* First PPT marker */
4257 l_tcp->ppt_data_size = 0;
4258 l_tcp->ppt_len = p_header_size;
4260 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4261 if (l_tcp->ppt_buffer == 00) {
4262 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4265 l_tcp->ppt_data = l_tcp->ppt_buffer;
4267 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4270 l_tcp->ppt_len += p_header_size;
4272 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4273 if (l_tcp->ppt_buffer == 00) {
4274 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4277 l_tcp->ppt_data = l_tcp->ppt_buffer;
4279 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4282 /* Read packet header from buffer */
4283 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4285 l_tcp->ppt_data_size += p_header_size;
4291 * Writes the TLM marker (Tile Length Marker)
4293 * @param p_stream the stream to write data to.
4294 * @param p_j2k J2K codec.
4295 * @param p_manager the user event manager.
4297 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4298 struct opj_stream_private *p_stream,
4299 struct opj_event_mgr * p_manager )
4301 OPJ_BYTE * l_current_data = 00;
4302 OPJ_UINT32 l_tlm_size;
4305 assert(p_j2k != 00);
4306 assert(p_manager != 00);
4307 assert(p_stream != 00);
4309 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4311 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4312 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4313 = (OPJ_BYTE*)opj_realloc(
4314 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4317 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4321 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4324 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4326 /* change the way data is written to avoid seeking if possible */
4328 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4330 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4331 l_current_data += 2;
4333 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4334 l_current_data += 2;
4336 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4339 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4342 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4343 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) {
4350 static void j2k_write_tlm(opj_j2k_t *j2k){
4352 opj_cio_t *cio = j2k->cio;
4353 j2k->tlm_start = cio_tell(cio);
4354 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4355 lenp = 4 + (5*j2k->totnum_tp);
4356 cio_write(cio,lenp,2); /* Ltlm */
4357 cio_write(cio, 0,1); /* Ztlm=0*/
4358 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4359 cio_skip(cio,5*j2k->totnum_tp);
4362 static void j2k_write_sot(opj_j2k_t *j2k) {
4365 opj_cio_t *cio = j2k->cio;
4367 j2k->sot_start = cio_tell(cio);
4368 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4369 lenp = cio_tell(cio);
4370 cio_skip(cio, 2); /* Lsot (further) */
4371 cio_write(cio, j2k->curtileno, 2); /* Isot */
4372 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4373 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4374 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4375 len = cio_tell(cio) - lenp;
4376 cio_seek(cio, lenp);
4377 cio_write(cio, len, 2); /* Lsot */
4378 cio_seek(cio, lenp + len);
4382 /* update markers struct */
4383 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4384 #endif /* USE_JPWL */
4387 if( j2k->cstr_info && j2k->cur_tp_num==0){
4388 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4393 * Writes the SOT marker (Start of tile-part)
4395 * @param p_stream the stream to write data to.
4396 * @param p_j2k J2K codec.
4397 * @param p_manager the user event manager.
4399 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4401 OPJ_UINT32 * p_data_written,
4402 const struct opj_stream_private *p_stream,
4403 struct opj_event_mgr * p_manager )
4406 assert(p_j2k != 00);
4407 assert(p_manager != 00);
4408 assert(p_stream != 00);
4410 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4413 opj_write_bytes(p_data,10,2); /* Lsot */
4416 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4422 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4425 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4430 /* update markers struct */
4432 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4434 assert( 0 && "TODO" );
4435 #endif /* USE_JPWL */
4437 * p_data_written = 12;
4446 * Reads a PPT marker (Packed packet headers, tile-part header)
4448 * @param p_header_data the data contained in the PPT box.
4449 * @param p_j2k the jpeg2000 codec.
4450 * @param p_header_size the size of the data contained in the PPT marker.
4451 * @param p_manager the user event manager.
4453 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4454 OPJ_BYTE * p_header_data,
4455 OPJ_UINT32 p_header_size,
4456 opj_event_mgr_t * p_manager )
4458 opj_cp_v2_t *l_cp = 00;
4459 opj_tcp_v2_t *l_tcp = 00;
4460 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4461 OPJ_UINT32 l_current_part;
4462 OPJ_UINT32 l_tile_x,l_tile_y;
4465 assert(p_header_data != 00);
4466 assert(p_j2k != 00);
4467 assert(p_manager != 00);
4469 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4470 if (p_header_size != 8) {
4471 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4475 l_cp = &(p_j2k->m_cp);
4476 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4479 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4480 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4481 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4484 if (l_cp->correct) {
4486 int tileno = p_j2k->m_current_tile_number;
4487 static int backup_tileno = 0;
4489 /* tileno is negative or larger than the number of tiles!!! */
4490 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4491 opj_event_msg_v2(p_manager, EVT_ERROR,
4492 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4493 tileno, (l_cp->tw * l_cp->th));
4495 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4498 /* we try to correct */
4499 tileno = backup_tileno;
4500 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4501 "- setting tile number to %d\n",
4505 /* keep your private count of tiles */
4508 #endif /* USE_JPWL */
4510 /* look for the tile in the list of already processed tile (in parts). */
4511 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4512 /* since the time taken by this function can only grow at the time */
4514 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4517 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4518 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4520 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4526 if (l_cp->correct) {
4528 /* totlen is negative or larger than the bytes left!!! */
4529 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))) { */
4530 opj_event_msg_v2(p_manager, EVT_ERROR,
4531 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4532 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4534 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4537 /* we try to correct */
4539 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4540 "- setting Psot to %d => assuming it is the last tile\n",
4544 #endif /* USE_JPWL */
4546 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4548 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4549 "we assuming it is the last tile-part of the codestream.\n");
4550 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4553 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4556 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4559 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4560 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4561 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4562 if (l_tcp->m_nb_tile_parts) {
4563 if (l_current_part >= l_tcp->m_nb_tile_parts){
4564 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4565 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4566 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4570 l_tcp->m_nb_tile_parts = l_num_parts;
4573 /* If know the number of tile part header we will check if we didn't read the last*/
4574 if (l_tcp->m_nb_tile_parts) {
4575 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4576 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4580 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4581 /* Keep the size of data to skip after this marker */
4582 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4585 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4586 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4589 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4591 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4592 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4593 p_j2k->m_specific_param.m_decoder.m_skip_data =
4594 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4595 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4596 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4597 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4600 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4601 p_j2k->m_specific_param.m_decoder.m_skip_data =
4602 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4606 if (p_j2k->cstr_index)
4608 assert(p_j2k->cstr_index->tile_index != 00);
4609 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4610 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4612 if (l_num_parts != 0){
4613 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4614 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4617 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4618 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4619 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4621 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4622 (opj_tp_index_t*)opj_realloc(
4623 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4624 l_num_parts* sizeof(opj_tp_index_t));
4627 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4629 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4630 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4631 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4632 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4633 sizeof(opj_tp_index_t));
4636 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4637 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4638 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4639 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4640 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4641 * sizeof(opj_tp_index_t));
4650 /* 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 */
4651 /* if (p_j2k->cstr_info) {
4654 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4657 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4658 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4659 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4660 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4663 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4666 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4670 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4673 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4674 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4675 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4680 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4683 opj_tcp_t *tcp = NULL;
4684 opj_codestream_info_t *cstr_info = NULL;
4686 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4687 opj_cp_t *cp = j2k->cp;
4688 opj_cio_t *cio = j2k->cio;
4690 tcd->tp_num = j2k->tp_num ;
4691 tcd->cur_tp_num = j2k->cur_tp_num;
4693 cio_write(cio, J2K_MS_SOD, 2);
4695 if( j2k->cstr_info && j2k->cur_tp_num==0){
4696 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4699 if (j2k->curtileno == 0) {
4700 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4704 cstr_info = j2k->cstr_info;
4706 if (!j2k->cur_tp_num ) {
4707 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4708 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4711 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4712 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4716 /* update markers struct */
4717 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4718 #endif /* USE_JPWL */
4723 tcp = &cp->tcps[j2k->curtileno];
4724 for (layno = 0; layno < tcp->numlayers; layno++) {
4725 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4726 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4727 } else if (tcp->rates[layno]) {
4728 tcp->rates[layno]=1;
4731 if(j2k->cur_tp_num == 0){
4732 tcd->tcd_image->tiles->packno = 0;
4734 cstr_info->packno = 0;
4737 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4739 /* Writing Psot in SOT marker */
4740 totlen = cio_tell(cio) + l - j2k->sot_start;
4741 cio_seek(cio, j2k->sot_start + 6);
4742 cio_write(cio, totlen, 4);
4743 cio_seek(cio, j2k->sot_start + totlen);
4744 /* Writing Ttlm and Ptlm in TLM marker */
4746 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4747 cio_write(cio, j2k->curtileno, 1);
4748 cio_write(cio, totlen, 4);
4750 cio_seek(cio, j2k->sot_start + totlen);
4754 * Writes the SOD marker (Start of data)
4756 * @param p_stream the stream to write data to.
4757 * @param p_j2k J2K codec.
4758 * @param p_manager the user event manager.
4760 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4761 struct opj_tcd_v2 * p_tile_coder,
4763 OPJ_UINT32 * p_data_written,
4764 OPJ_UINT32 p_total_data_size,
4765 const struct opj_stream_private *p_stream,
4766 struct opj_event_mgr * p_manager )
4768 opj_tcp_v2_t *l_tcp = 00;
4769 opj_codestream_info_t *l_cstr_info = 00;
4770 opj_cp_v2_t *l_cp = 00;
4772 OPJ_UINT32 l_size_tile;
4773 OPJ_UINT32 l_remaining_data;
4776 assert(p_j2k != 00);
4777 assert(p_manager != 00);
4778 assert(p_stream != 00);
4780 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4783 /* make room for the EOF marker */
4784 l_remaining_data = p_total_data_size - 4;
4786 l_cp = &(p_j2k->m_cp);
4787 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4790 /* update tile coder */
4791 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4792 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4794 l_size_tile = l_cp->th * l_cp->tw;
4797 /* TODO mergeV2: check this part which use cstr_info */
4798 /*l_cstr_info = p_j2k->cstr_info;
4800 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4801 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4802 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4808 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4810 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4815 /* update markers struct */
4816 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4818 assert( 0 && "TODO" );
4819 #endif /* USE_JPWL */
4824 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4825 p_tile_coder->tcd_image->tiles->packno = 0;
4827 l_cstr_info->packno = 0;
4831 *p_data_written = 0;
4833 if (! tcd_encode_tile_v2(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
4834 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4838 *p_data_written += 2;
4847 * Reads a SOD marker (Start Of Data)
4849 * @param p_header_data the data contained in the SOD box.
4850 * @param p_j2k the jpeg2000 codec.
4851 * @param p_header_size the size of the data contained in the SOD marker.
4852 * @param p_manager the user event manager.
4854 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4855 opj_stream_private_t *p_stream,
4856 opj_event_mgr_t * p_manager
4859 OPJ_UINT32 l_current_read_size;
4860 opj_codestream_index_t * l_cstr_index = 00;
4861 OPJ_BYTE ** l_current_data = 00;
4862 opj_tcp_v2_t * l_tcp = 00;
4863 OPJ_UINT32 * l_tile_len = 00;
4866 assert(p_j2k != 00);
4867 assert(p_manager != 00);
4868 assert(p_stream != 00);
4870 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4872 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4873 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4874 // but we are in the last tile part,
4875 // so its result will fit on OPJ_UINT32 unless we find
4876 // a file with a single tile part of more than 4 GB...*/
4877 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4880 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4882 l_current_data = &(l_tcp->m_data);
4883 l_tile_len = &l_tcp->m_data_size;
4885 if (! *l_current_data) {
4886 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4889 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4892 if (*l_current_data == 00) {
4893 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4899 l_cstr_index = p_j2k->cstr_index;
4901 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4903 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4904 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4906 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4907 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4909 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4913 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4915 /*l_cstr_index->packno = 0;*/
4918 l_current_read_size = opj_stream_read_data( p_stream,
4919 *l_current_data + *l_tile_len,
4920 p_j2k->m_specific_param.m_decoder.m_sot_length,
4923 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4924 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4927 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4930 *l_tile_len += l_current_read_size;
4936 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4937 opj_cp_t *cp = j2k->cp;
4938 opj_tcp_t *tcp = &cp->tcps[tileno];
4939 opj_cio_t *cio = j2k->cio;
4940 int numcomps = j2k->image->numcomps;
4942 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4943 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4944 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4945 cio_write(cio, 0, 1); /* Srgn */
4946 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4950 * Writes the RGN marker (Region Of Interest)
4952 * @param p_tile_no the tile to output
4953 * @param p_comp_no the component to output
4954 * @param p_stream the stream to write data to.
4955 * @param p_j2k J2K codec.
4956 * @param p_manager the user event manager.
4958 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4959 OPJ_UINT32 p_tile_no,
4960 OPJ_UINT32 p_comp_no,
4961 struct opj_stream_private *p_stream,
4962 struct opj_event_mgr * p_manager )
4964 OPJ_BYTE * l_current_data = 00;
4965 OPJ_UINT32 l_nb_comp;
4966 OPJ_UINT32 l_rgn_size;
4967 opj_image_t *l_image = 00;
4968 opj_cp_v2_t *l_cp = 00;
4969 opj_tcp_v2_t *l_tcp = 00;
4970 opj_tccp_t *l_tccp = 00;
4971 OPJ_UINT32 l_comp_room;
4974 assert(p_j2k != 00);
4975 assert(p_manager != 00);
4976 assert(p_stream != 00);
4978 l_cp = &(p_j2k->m_cp);
4979 l_tcp = &l_cp->tcps[p_tile_no];
4980 l_tccp = &l_tcp->tccps[p_comp_no];
4982 l_nb_comp = l_image->numcomps;
4984 if (l_nb_comp <= 256) {
4991 l_rgn_size = 6 + l_comp_room;
4993 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4995 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4996 l_current_data += 2;
4998 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4999 l_current_data += 2;
5001 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5002 l_current_data+=l_comp_room;
5004 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5007 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5010 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) {
5018 static void j2k_write_eoc(opj_j2k_t *j2k) {
5019 opj_cio_t *cio = j2k->cio;
5020 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5021 cio_write(cio, J2K_MS_EOC, 2);
5025 /* update markers struct */
5026 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5027 #endif /* USE_JPWL */
5032 * Writes the EOC marker (End of Codestream)
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 j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5039 struct opj_stream_private *p_stream,
5040 struct opj_event_mgr * p_manager )
5043 assert(p_j2k != 00);
5044 assert(p_manager != 00);
5045 assert(p_stream != 00);
5047 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5052 /* update markers struct */
5054 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5056 #endif /* USE_JPWL */
5058 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5062 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5071 * Reads a RGN marker (Region Of Interest)
5073 * @param p_header_data the data contained in the POC box.
5074 * @param p_j2k the jpeg2000 codec.
5075 * @param p_header_size the size of the data contained in the POC marker.
5076 * @param p_manager the user event manager.
5078 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5079 OPJ_BYTE * p_header_data,
5080 OPJ_UINT32 p_header_size,
5081 opj_event_mgr_t * p_manager
5084 OPJ_UINT32 l_nb_comp;
5085 opj_image_t * l_image = 00;
5087 opj_cp_v2_t *l_cp = 00;
5088 opj_tcp_v2_t *l_tcp = 00;
5089 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5092 assert(p_header_data != 00);
5093 assert(p_j2k != 00);
5094 assert(p_manager != 00);
5096 l_image = p_j2k->m_private_image;
5097 l_nb_comp = l_image->numcomps;
5099 if (l_nb_comp <= 256) {
5104 if (p_header_size != 2 + l_comp_room) {
5105 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5109 l_cp = &(p_j2k->m_cp);
5110 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5111 &l_cp->tcps[p_j2k->m_current_tile_number] :
5112 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5114 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5115 p_header_data+=l_comp_room;
5116 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5120 if (l_cp->correct) {
5121 /* totlen is negative or larger than the bytes left!!! */
5122 if (l_comp_room >= l_nb_comp) {
5123 opj_event_msg_v2(p_manager, EVT_ERROR,
5124 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5125 l_comp_room, l_nb_comp);
5126 if (!JPWL_ASSUME || JPWL_ASSUME) {
5127 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5132 #endif /* USE_JPWL */
5134 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5141 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5143 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5146 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5153 * Updates the rates of the tcp.
5155 * @param p_stream the stream to write data to.
5156 * @param p_j2k J2K codec.
5157 * @param p_manager the user event manager.
5159 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5160 struct opj_stream_private *p_stream,
5161 struct opj_event_mgr * p_manager )
5163 opj_cp_v2_t * l_cp = 00;
5164 opj_image_t * l_image = 00;
5165 opj_tcp_v2_t * l_tcp = 00;
5166 opj_image_comp_t * l_img_comp = 00;
5169 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5170 OPJ_FLOAT32 * l_rates = 0;
5171 OPJ_FLOAT32 l_sot_remove;
5172 OPJ_UINT32 l_bits_empty, l_size_pixel;
5173 OPJ_UINT32 l_tile_size = 0;
5174 OPJ_UINT32 l_last_res;
5175 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5178 assert(p_j2k != 00);
5179 assert(p_manager != 00);
5180 assert(p_stream != 00);
5183 l_cp = &(p_j2k->m_cp);
5184 l_image = p_j2k->m_private_image;
5187 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5188 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5189 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5191 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5192 l_tp_stride_func = get_tp_stride;
5195 l_tp_stride_func = get_default_stride;
5198 for (i=0;i<l_cp->th;++i) {
5199 for (j=0;j<l_cp->tw;++j) {
5200 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5202 /* 4 borders of the tile rescale on the image if necessary */
5203 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5204 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5205 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5206 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5208 l_rates = l_tcp->rates;
5210 /* Modification of the RATE >> */
5212 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5214 ((*l_rates) * l_bits_empty)
5222 for (k = 1; k < l_tcp->numlayers; ++k) {
5224 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5226 ((*l_rates) * l_bits_empty)
5242 for (i=0;i<l_cp->th;++i) {
5243 for (j=0;j<l_cp->tw;++j) {
5244 l_rates = l_tcp->rates;
5247 *l_rates -= l_sot_remove;
5249 if (*l_rates < 30) {
5256 l_last_res = l_tcp->numlayers - 1;
5258 for (k = 1; k < l_last_res; ++k) {
5261 *l_rates -= l_sot_remove;
5263 if (*l_rates < *(l_rates - 1) + 10) {
5264 *l_rates = (*(l_rates - 1)) + 20;
5272 *l_rates -= (l_sot_remove + 2.f);
5274 if (*l_rates < *(l_rates - 1) + 10) {
5275 *l_rates = (*(l_rates - 1)) + 20;
5283 l_img_comp = l_image->comps;
5286 for (i=0;i<l_image->numcomps;++i) {
5287 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5289 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5297 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5299 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5301 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5302 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5303 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5304 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5308 if (l_cp->m_specific_param.m_enc.m_cinema) {
5309 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5310 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5311 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5315 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5316 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5322 static void j2k_read_eoc(opj_j2k_t *j2k) {
5324 opj_bool success = OPJ_FALSE;
5326 /* if packets should be decoded */
5327 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5328 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5329 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5330 for (i = 0; i < j2k->cp->tileno_size; i++) {
5331 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5332 if (j2k->cp->tileno[i] != -1)
5334 tileno = j2k->cp->tileno[i];
5335 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5336 opj_free(j2k->tile_data[tileno]);
5337 j2k->tile_data[tileno] = NULL;
5338 tcd_free_decode_tile(tcd, i);
5341 success = OPJ_FALSE;
5342 if (success == OPJ_FALSE) {
5343 j2k->state |= J2K_STATE_ERR;
5347 tcd_free_decode(tcd);
5350 /* if packets should not be decoded */
5352 for (i = 0; i < j2k->cp->tileno_size; i++) {
5353 tileno = j2k->cp->tileno[i];
5354 opj_free(j2k->tile_data[tileno]);
5355 j2k->tile_data[tileno] = NULL;
5358 if (j2k->state & J2K_STATE_ERR)
5359 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5361 j2k->state = J2K_STATE_MT;
5365 * Reads a EOC marker (End Of Codestream)
5367 * @param p_header_data the data contained in the SOD box.
5368 * @param p_j2k the jpeg2000 codec.
5369 * @param p_header_size the size of the data contained in the SOD marker.
5370 * @param p_manager the user event manager.
5373 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5374 struct opj_stream_private *p_stream,
5375 struct opj_event_mgr * p_manager )
5378 opj_tcd_v2_t * l_tcd = 00;
5379 OPJ_UINT32 l_nb_tiles;
5380 opj_tcp_v2_t * l_tcp = 00;
5384 assert(p_j2k != 00);
5385 assert(p_manager != 00);
5386 assert(p_stream != 00);
5388 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5389 l_tcp = p_j2k->m_cp.tcps;
5391 l_tcd = tcd_create_v2(OPJ_TRUE);
5393 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5397 for (i = 0; i < l_nb_tiles; ++i) {
5398 if (l_tcp->m_data) {
5399 if (! tcd_init_decode_tile(l_tcd, i)) {
5400 tcd_destroy_v2(l_tcd);
5401 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5405 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5409 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5414 opj_j2k_tcp_destroy(l_tcp);
5418 tcd_destroy_v2(l_tcd);
5424 * Gets the offset of the header.
5426 * @param p_stream the stream to write data to.
5427 * @param p_j2k J2K codec.
5428 * @param p_manager the user event manager.
5430 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5431 struct opj_stream_private *p_stream,
5432 struct opj_event_mgr * p_manager )
5435 assert(p_j2k != 00);
5436 assert(p_manager != 00);
5437 assert(p_stream != 00);
5439 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5445 * Writes the MCT marker (Multiple Component Transform)
5447 * @param p_stream the stream to write data to.
5448 * @param p_j2k J2K codec.
5449 * @param p_manager the user event manager.
5451 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5452 struct opj_stream_private *p_stream,
5453 struct opj_event_mgr * p_manager )
5456 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5457 opj_mct_data_t * l_mct_record;
5458 opj_tcp_v2_t * l_tcp;
5461 assert(p_j2k != 00);
5462 assert(p_stream != 00);
5463 assert(p_manager != 00);
5465 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5469 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5470 l_mct_record = l_tcp->m_mct_records;
5472 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5474 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5481 l_mcc_record = l_tcp->m_mcc_records;
5483 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5485 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5492 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5500 * Writes the image components.
5502 * @param p_stream the stream to write data to.
5503 * @param p_j2k J2K codec.
5504 * @param p_manager the user event manager.
5506 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5507 struct opj_stream_private *p_stream,
5508 struct opj_event_mgr * p_manager )
5513 assert(p_j2k != 00);
5514 assert(p_manager != 00);
5515 assert(p_stream != 00);
5517 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5519 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5523 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5532 * Writes regions of interests.
5534 * @param p_stream the stream to write data to.
5535 * @param p_j2k J2K codec.
5536 * @param p_manager the user event manager.
5538 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5539 struct opj_stream_private *p_stream,
5540 struct opj_event_mgr * p_manager )
5543 const opj_tccp_t *l_tccp = 00;
5546 assert(p_j2k != 00);
5547 assert(p_manager != 00);
5548 assert(p_stream != 00);
5550 l_tccp = p_j2k->m_cp.tcps->tccps;
5552 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5553 if (l_tccp->roishift) {
5555 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5569 * @param p_stream the stream to write data to.
5570 * @param p_j2k J2K codec.
5571 * @param p_manager the user event manager.
5573 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5574 struct opj_stream_private *p_stream,
5575 struct opj_event_mgr * p_manager )
5577 opj_codestream_index_t * l_cstr_index = 00;
5580 assert(p_j2k != 00);
5581 assert(p_manager != 00);
5582 assert(p_stream != 00);
5584 l_cstr_index = p_j2k->cstr_index;
5586 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5588 /* The following adjustment is done to adjust the codestream size */
5589 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5590 /* the first bunch of bytes is not in the codestream */
5591 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5596 /* preparation of JPWL marker segments */
5600 /* encode according to JPWL */
5601 jpwl_encode(p_j2k, p_stream, image);
5605 assert( 0 && "TODO" );
5606 #endif /* USE_JPWL */
5611 typedef struct opj_dec_mstabent {
5614 /** value of the state when the marker can appear */
5616 /** action linked to the marker */
5617 void (*handler) (opj_j2k_t *j2k);
5618 } opj_dec_mstabent_t;
5620 opj_dec_mstabent_t j2k_dec_mstab[] = {
5621 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5622 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5623 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5624 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5625 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5626 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5627 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5628 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5629 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5630 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5631 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5632 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5633 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5634 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5635 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5636 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5638 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5639 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5642 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5643 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5644 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5645 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5646 #endif /* USE_JPWL */
5648 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5649 {J2K_MS_INSEC, 0, j2k_read_insec},
5650 #endif /* USE_JPSEC */
5652 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5658 * Reads an unknown marker
5660 * @param p_stream the stream object to read from.
5661 * @param p_j2k the jpeg2000 codec.
5662 * @param p_manager the user event manager.
5664 * @return true if the marker could be deduced.
5666 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5667 opj_stream_private_t *p_stream,
5668 OPJ_UINT32 *output_marker,
5669 opj_event_mgr_t * p_manager
5672 OPJ_UINT32 l_unknown_marker;
5673 const opj_dec_memory_marker_handler_t * l_marker_handler;
5674 OPJ_UINT32 l_size_unk = 2;
5677 assert(p_j2k != 00);
5678 assert(p_manager != 00);
5679 assert(p_stream != 00);
5681 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5684 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5685 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5686 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5690 /* read 2 bytes as the new marker ID*/
5691 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5693 if (!(l_unknown_marker < 0xff00)) {
5695 /* Get the marker handler from the marker ID*/
5696 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5698 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5699 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5703 if (l_marker_handler->id != J2K_MS_UNK) {
5704 /* Add the marker to the codestream index*/
5705 if (l_marker_handler->id != J2K_MS_SOT)
5706 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5707 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5709 break; /* next marker is known and well located */
5717 *output_marker = l_marker_handler->id ;
5723 Read the lookup table containing all the marker, status and action
5724 @param id Marker value
5726 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5727 opj_dec_mstabent_t *e;
5728 for (e = j2k_dec_mstab; e->id != 0; e++) {
5737 * Writes the MCT marker (Multiple Component Transform)
5739 * @param p_stream the stream to write data to.
5740 * @param p_j2k J2K codec.
5741 * @param p_manager the user event manager.
5743 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5744 opj_mct_data_t * p_mct_record,
5745 struct opj_stream_private *p_stream,
5746 struct opj_event_mgr * p_manager )
5748 OPJ_UINT32 l_mct_size;
5749 OPJ_BYTE * l_current_data = 00;
5753 assert(p_j2k != 00);
5754 assert(p_manager != 00);
5755 assert(p_stream != 00);
5757 l_mct_size = 10 + p_mct_record->m_data_size;
5759 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5760 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5761 = (OPJ_BYTE*)opj_realloc(
5762 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5765 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5769 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5772 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5774 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5775 l_current_data += 2;
5777 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5778 l_current_data += 2;
5780 opj_write_bytes(l_current_data,0,2); /* Zmct */
5781 l_current_data += 2;
5783 /* only one marker atm */
5784 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5786 opj_write_bytes(l_current_data,l_tmp,2);
5787 l_current_data += 2;
5789 opj_write_bytes(l_current_data,0,2); /* Ymct */
5792 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5794 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) {
5802 * Reads a MCT marker (Multiple Component Transform)
5804 * @param p_header_data the data contained in the MCT box.
5805 * @param p_j2k the jpeg2000 codec.
5806 * @param p_header_size the size of the data contained in the MCT marker.
5807 * @param p_manager the user event manager.
5809 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5810 OPJ_BYTE * p_header_data,
5811 OPJ_UINT32 p_header_size,
5812 opj_event_mgr_t * p_manager
5816 opj_tcp_v2_t *l_tcp = 00;
5819 opj_mct_data_t * l_mct_data;
5822 assert(p_header_data != 00);
5823 assert(p_j2k != 00);
5825 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5826 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5827 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5829 if (p_header_size < 2) {
5830 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5835 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5838 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5842 if(p_header_size <= 6) {
5843 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5847 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5848 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5851 l_indix = l_tmp & 0xff;
5852 l_mct_data = l_tcp->m_mct_records;
5854 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5855 if (l_mct_data->m_index == l_indix) {
5862 if (i == l_tcp->m_nb_mct_records) {
5863 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5864 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5866 l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
5867 if(! l_tcp->m_mct_records) {
5868 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5872 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5873 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5876 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5879 if (l_mct_data->m_data) {
5880 opj_free(l_mct_data->m_data);
5881 l_mct_data->m_data = 00;
5884 l_mct_data->m_index = l_indix;
5885 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5886 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5888 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5891 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5897 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5898 if (! l_mct_data->m_data) {
5899 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5902 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5904 l_mct_data->m_data_size = p_header_size;
5905 ++l_tcp->m_nb_mct_records;
5911 * Writes the MCC marker (Multiple Component Collection)
5913 * @param p_stream the stream to write data to.
5914 * @param p_j2k J2K codec.
5915 * @param p_manager the user event manager.
5917 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5918 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5919 struct opj_stream_private *p_stream,
5920 struct opj_event_mgr * p_manager )
5923 OPJ_UINT32 l_mcc_size;
5924 OPJ_BYTE * l_current_data = 00;
5925 OPJ_UINT32 l_nb_bytes_for_comp;
5930 assert(p_j2k != 00);
5931 assert(p_manager != 00);
5932 assert(p_stream != 00);
5934 if (p_mcc_record->m_nb_comps > 255 ) {
5935 l_nb_bytes_for_comp = 2;
5939 l_nb_bytes_for_comp = 1;
5943 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5944 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5946 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5947 = (OPJ_BYTE*)opj_realloc(
5948 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5950 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5954 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5957 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5959 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5960 l_current_data += 2;
5962 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5963 l_current_data += 2;
5966 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5967 l_current_data += 2;
5969 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5972 /* only one marker atm */
5973 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5976 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5979 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5982 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 */
5985 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5986 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5987 l_current_data+=l_nb_bytes_for_comp;
5990 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 */
5993 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5995 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5996 l_current_data+=l_nb_bytes_for_comp;
5999 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6001 if (p_mcc_record->m_decorrelation_array) {
6002 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6005 if (p_mcc_record->m_offset_array) {
6006 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6009 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6012 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) {
6020 * Reads a MCC marker (Multiple Component Collection)
6022 * @param p_header_data the data contained in the MCC box.
6023 * @param p_j2k the jpeg2000 codec.
6024 * @param p_header_size the size of the data contained in the MCC marker.
6025 * @param p_manager the user event manager.
6027 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6028 OPJ_BYTE * p_header_data,
6029 OPJ_UINT32 p_header_size,
6030 opj_event_mgr_t * p_manager )
6035 opj_tcp_v2_t * l_tcp;
6036 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6037 opj_mct_data_t * l_mct_data;
6038 OPJ_UINT32 l_nb_collections;
6039 OPJ_UINT32 l_nb_comps;
6040 OPJ_UINT32 l_nb_bytes_by_comp;
6044 assert(p_header_data != 00);
6045 assert(p_j2k != 00);
6046 assert(p_manager != 00);
6048 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6049 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6050 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6052 if (p_header_size < 2) {
6053 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6058 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6061 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6065 if (p_header_size < 7) {
6066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6070 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6073 l_mcc_record = l_tcp->m_mcc_records;
6075 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6076 if (l_mcc_record->m_index == l_indix) {
6083 if (i == l_tcp->m_nb_mcc_records) {
6084 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6085 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6087 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6088 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6089 if (! l_tcp->m_mcc_records) {
6090 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6093 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6094 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));
6096 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6098 l_mcc_record->m_index = l_indix;
6100 /* only one marker atm */
6101 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6104 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6108 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6111 if (l_nb_collections > 1) {
6112 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6118 for (i=0;i<l_nb_collections;++i) {
6119 if (p_header_size < 3) {
6120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6124 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6128 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6132 opj_read_bytes(p_header_data,&l_nb_comps,2);
6137 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6138 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6140 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6141 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6145 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6147 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6148 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6149 p_header_data+=l_nb_bytes_by_comp;
6152 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6157 opj_read_bytes(p_header_data,&l_nb_comps,2);
6160 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6161 l_nb_comps &= 0x7fff;
6163 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6164 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6168 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6169 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6173 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6175 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6176 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6177 p_header_data+=l_nb_bytes_by_comp;
6180 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6185 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6188 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6189 l_mcc_record->m_decorrelation_array = 00;
6190 l_mcc_record->m_offset_array = 00;
6192 l_indix = l_tmp & 0xff;
6194 l_mct_data = l_tcp->m_mct_records;
6195 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6196 if (l_mct_data->m_index == l_indix) {
6197 l_mcc_record->m_decorrelation_array = l_mct_data;
6203 if (l_mcc_record->m_decorrelation_array == 00) {
6204 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6209 l_indix = (l_tmp >> 8) & 0xff;
6211 l_mct_data = l_tcp->m_mct_records;
6212 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6213 if (l_mct_data->m_index == l_indix) {
6214 l_mcc_record->m_offset_array = l_mct_data;
6220 if (l_mcc_record->m_offset_array == 00) {
6221 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6227 if (p_header_size != 0) {
6228 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6232 ++l_tcp->m_nb_mcc_records;
6239 * Writes the MCO marker (Multiple component transformation ordering)
6241 * @param p_stream the stream to write data to.
6242 * @param p_j2k J2K codec.
6243 * @param p_manager the user event manager.
6245 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6246 struct opj_stream_private *p_stream,
6247 struct opj_event_mgr * p_manager
6250 OPJ_BYTE * l_current_data = 00;
6251 OPJ_UINT32 l_mco_size;
6252 opj_tcp_v2_t * l_tcp = 00;
6253 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6257 assert(p_j2k != 00);
6258 assert(p_manager != 00);
6259 assert(p_stream != 00);
6261 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6262 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6264 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6265 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6267 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6268 = (OPJ_BYTE*)opj_realloc(
6269 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6271 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6276 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6279 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6280 l_current_data += 2;
6282 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6283 l_current_data += 2;
6285 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6288 l_mcc_record = l_tcp->m_mcc_records;
6289 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6290 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6296 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) {
6304 * Reads a MCO marker (Multiple Component Transform Ordering)
6306 * @param p_header_data the data contained in the MCO box.
6307 * @param p_j2k the jpeg2000 codec.
6308 * @param p_header_size the size of the data contained in the MCO marker.
6309 * @param p_manager the user event manager.
6311 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6312 OPJ_BYTE * p_header_data,
6313 OPJ_UINT32 p_header_size,
6314 opj_event_mgr_t * p_manager
6317 OPJ_UINT32 l_tmp, i;
6318 OPJ_UINT32 l_nb_stages;
6319 opj_tcp_v2_t * l_tcp;
6320 opj_tccp_t * l_tccp;
6321 opj_image_t * l_image;
6322 opj_image_comp_t * l_img_comp;
6325 assert(p_header_data != 00);
6326 assert(p_j2k != 00);
6327 assert(p_manager != 00);
6329 l_image = p_j2k->m_private_image;
6330 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6331 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6332 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6334 if (p_header_size < 1) {
6335 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6339 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6342 if (l_nb_stages > 1) {
6343 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6347 if (p_header_size != l_nb_stages + 1) {
6348 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6352 l_tccp = l_tcp->tccps;
6353 l_img_comp = l_image->comps;
6355 for (i=0;i<l_image->numcomps;++i) {
6356 l_tccp->m_dc_level_shift = 0;
6360 if (l_tcp->m_mct_decoding_matrix) {
6361 opj_free(l_tcp->m_mct_decoding_matrix);
6362 l_tcp->m_mct_decoding_matrix = 00;
6365 for (i=0;i<l_nb_stages;++i) {
6366 opj_read_bytes(p_header_data,&l_tmp,1);
6369 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6377 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6380 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6381 opj_mct_data_t * l_deco_array, * l_offset_array;
6382 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6383 OPJ_UINT32 l_nb_elem;
6384 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6385 opj_tccp_t * l_tccp;
6388 assert(p_tcp != 00);
6390 l_mcc_record = p_tcp->m_mcc_records;
6392 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6393 if (l_mcc_record->m_index == p_index) {
6398 if (i==p_tcp->m_nb_mcc_records) {
6399 /** element discarded **/
6403 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6404 /** do not support number of comps != image */
6408 l_deco_array = l_mcc_record->m_decorrelation_array;
6411 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6412 if (l_deco_array->m_data_size != l_data_size) {
6416 l_nb_elem = p_image->numcomps * p_image->numcomps;
6417 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6418 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6420 if (! p_tcp->m_mct_decoding_matrix ) {
6424 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);
6427 l_offset_array = l_mcc_record->m_offset_array;
6429 if (l_offset_array) {
6430 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6431 if (l_offset_array->m_data_size != l_data_size) {
6435 l_nb_elem = p_image->numcomps;
6436 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6437 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6439 if (! l_offset_data ) {
6443 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6445 l_tccp = p_tcp->tccps;
6446 l_current_offset_data = l_offset_data;
6448 for (i=0;i<p_image->numcomps;++i) {
6449 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6453 opj_free(l_offset_data);
6460 * Writes the CBD marker (Component bit depth definition)
6462 * @param p_stream the stream to write data to.
6463 * @param p_j2k J2K codec.
6464 * @param p_manager the user event manager.
6466 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6467 struct opj_stream_private *p_stream,
6468 struct opj_event_mgr * p_manager )
6471 OPJ_UINT32 l_cbd_size;
6472 OPJ_BYTE * l_current_data = 00;
6473 opj_image_t *l_image = 00;
6474 opj_image_comp_t * l_comp = 00;
6477 assert(p_j2k != 00);
6478 assert(p_manager != 00);
6479 assert(p_stream != 00);
6481 l_image = p_j2k->m_private_image;
6482 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6484 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6485 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6486 = (OPJ_BYTE*)opj_realloc(
6487 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6490 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6494 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6497 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6499 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6500 l_current_data += 2;
6502 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6503 l_current_data += 2;
6505 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6508 l_comp = l_image->comps;
6510 for (i=0;i<l_image->numcomps;++i) {
6511 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6517 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) {
6525 * Reads a CBD marker (Component bit depth definition)
6526 * @param p_header_data the data contained in the CBD box.
6527 * @param p_j2k the jpeg2000 codec.
6528 * @param p_header_size the size of the data contained in the CBD marker.
6529 * @param p_manager the user event manager.
6531 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6532 OPJ_BYTE * p_header_data,
6533 OPJ_UINT32 p_header_size,
6534 opj_event_mgr_t * p_manager
6537 OPJ_UINT32 l_nb_comp,l_num_comp;
6538 OPJ_UINT32 l_comp_def;
6540 opj_image_comp_t * l_comp = 00;
6543 assert(p_header_data != 00);
6544 assert(p_j2k != 00);
6545 assert(p_manager != 00);
6547 l_num_comp = p_j2k->m_private_image->numcomps;
6549 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6550 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6554 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6557 if (l_nb_comp != l_num_comp) {
6558 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6562 l_comp = p_j2k->m_private_image->comps;
6563 for (i=0;i<l_num_comp;++i) {
6564 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6566 l_comp->sgnd = (l_comp_def>>7) & 1;
6567 l_comp->prec = (l_comp_def&0x7f) + 1;
6575 /* ----------------------------------------------------------------------- */
6576 /* J2K / JPT decoder interface */
6577 /* ----------------------------------------------------------------------- */
6581 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6584 if(j2k->tile_len != NULL) {
6585 opj_free(j2k->tile_len);
6587 if(j2k->tile_data != NULL) {
6588 opj_free(j2k->tile_data);
6590 if(j2k->default_tcp != NULL) {
6591 opj_tcp_t *default_tcp = j2k->default_tcp;
6592 if(default_tcp->ppt_data_first != NULL) {
6593 opj_free(default_tcp->ppt_data_first);
6595 if(j2k->default_tcp->tccps != NULL) {
6596 opj_free(j2k->default_tcp->tccps);
6598 opj_free(j2k->default_tcp);
6600 if(j2k->cp != NULL) {
6601 opj_cp_t *cp = j2k->cp;
6602 if(cp->tcps != NULL) {
6603 for(i = 0; i < cp->tw * cp->th; i++) {
6604 if(cp->tcps[i].ppt_data_first != NULL) {
6605 opj_free(cp->tcps[i].ppt_data_first);
6607 if(cp->tcps[i].tccps != NULL) {
6608 opj_free(cp->tcps[i].tccps);
6613 if(cp->ppm_data_first != NULL) {
6614 opj_free(cp->ppm_data_first);
6616 if(cp->tileno != NULL) {
6617 opj_free(cp->tileno);
6619 if(cp->comment != NULL) {
6620 opj_free(cp->comment);
6629 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6631 if(j2k && parameters) {
6632 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6633 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6636 j2k->m_cp.correct = parameters->jpwl_correct;
6637 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6638 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6639 #endif /* USE_JPWL */
6646 * Read a JPT-stream and decode file
6649 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6650 opj_image_t *image = NULL;
6651 opj_jpt_msg_header_t header;
6653 opj_common_ptr cinfo = j2k->cinfo;
6655 OPJ_ARG_NOT_USED(cstr_info);
6659 /* create an empty image */
6660 image = opj_image_create0();
6663 j2k->state = J2K_STATE_MHSOC;
6665 /* Initialize the header */
6666 jpt_init_msg_header(&header);
6667 /* Read the first header of the message */
6668 jpt_read_msg_header(cinfo, cio, &header);
6670 position = cio_tell(cio);
6671 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6672 opj_image_destroy(image);
6673 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6678 opj_dec_mstabent_t *e = NULL;
6681 if (!cio_numbytesleft(cio)) {
6685 /* data-bin read -> need to read a new header */
6686 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6687 jpt_read_msg_header(cinfo, cio, &header);
6688 position = cio_tell(cio);
6689 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6690 opj_image_destroy(image);
6691 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6696 id = cio_read(cio, 2);
6697 if (id >> 8 != 0xff) {
6698 opj_image_destroy(image);
6699 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6702 e = j2k_dec_mstab_lookup(id);
6703 if (!(j2k->state & e->states)) {
6704 opj_image_destroy(image);
6705 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6711 if (j2k->state == J2K_STATE_MT) {
6714 if (j2k->state == J2K_STATE_NEOC) {
6718 if (j2k->state == J2K_STATE_NEOC) {
6722 if (j2k->state != J2K_STATE_MT) {
6723 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6729 /* ----------------------------------------------------------------------- */
6730 /* J2K encoder interface */
6731 /* ----------------------------------------------------------------------- */
6733 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6734 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6741 opj_j2k_v2_t* j2k_create_compress_v2(void)
6743 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6748 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6750 l_j2k->m_is_decoder = 0;
6751 l_j2k->m_cp.m_is_decoder = 0;
6753 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6754 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6759 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6761 /* validation list creation*/
6762 l_j2k->m_validation_list = opj_procedure_list_create();
6763 if (! l_j2k->m_validation_list) {
6768 /* execution list creation*/
6769 l_j2k->m_procedure_list = opj_procedure_list_create();
6770 if (! l_j2k->m_procedure_list) {
6778 void j2k_destroy_compress(opj_j2k_t *j2k) {
6782 if(j2k->cp != NULL) {
6783 opj_cp_t *cp = j2k->cp;
6786 opj_free(cp->comment);
6789 opj_free(cp->matrice);
6791 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6792 opj_free(cp->tcps[tileno].tccps);
6801 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6803 int tileno, numpocs_tile;
6804 opj_cp_t *cp = NULL;
6806 if(!j2k || !parameters || ! image) {
6810 /* create and initialize the coding parameters structure */
6811 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6813 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6816 /* set default values for cp */
6821 copy user encoding parameters
6823 cp->cinema = parameters->cp_cinema;
6824 cp->max_comp_size = parameters->max_comp_size;
6825 cp->rsiz = parameters->cp_rsiz;
6826 cp->disto_alloc = parameters->cp_disto_alloc;
6827 cp->fixed_alloc = parameters->cp_fixed_alloc;
6828 cp->fixed_quality = parameters->cp_fixed_quality;
6830 /* mod fixed_quality */
6831 if(parameters->cp_matrice) {
6832 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6833 cp->matrice = (int *) opj_malloc(array_size);
6834 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6838 cp->tdx = parameters->cp_tdx;
6839 cp->tdy = parameters->cp_tdy;
6842 cp->tx0 = parameters->cp_tx0;
6843 cp->ty0 = parameters->cp_ty0;
6845 /* comment string */
6846 if(parameters->cp_comment) {
6847 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6849 strcpy(cp->comment, parameters->cp_comment);
6854 calculate other encoding parameters
6857 if (parameters->tile_size_on) {
6858 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6859 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6861 cp->tdx = image->x1 - cp->tx0;
6862 cp->tdy = image->y1 - cp->ty0;
6865 if(parameters->tp_on){
6866 cp->tp_flag = parameters->tp_flag;
6871 for(i=0;i<image->numcomps ;i++){
6872 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6878 calculate JPWL encoding parameters
6881 if (parameters->jpwl_epc_on) {
6885 cp->epc_on = OPJ_TRUE;
6886 cp->info_on = OPJ_FALSE; /* no informative technique */
6889 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6890 cp->epb_on = OPJ_TRUE;
6892 cp->hprot_MH = parameters->jpwl_hprot_MH;
6893 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6894 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6895 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6897 /* if tile specs are not specified, copy MH specs */
6898 if (cp->hprot_TPH[0] == -1) {
6899 cp->hprot_TPH_tileno[0] = 0;
6900 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6902 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6903 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6904 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6905 cp->pprot[i] = parameters->jpwl_pprot[i];
6909 /* set ESD writing */
6910 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6911 cp->esd_on = OPJ_TRUE;
6913 cp->sens_size = parameters->jpwl_sens_size;
6914 cp->sens_addr = parameters->jpwl_sens_addr;
6915 cp->sens_range = parameters->jpwl_sens_range;
6917 cp->sens_MH = parameters->jpwl_sens_MH;
6918 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6919 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6920 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6924 /* always set RED writing to false: we are at the encoder */
6925 cp->red_on = OPJ_FALSE;
6928 cp->epc_on = OPJ_FALSE;
6930 #endif /* USE_JPWL */
6933 /* initialize the mutiple tiles */
6934 /* ---------------------------- */
6935 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6937 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6938 opj_tcp_t *tcp = &cp->tcps[tileno];
6939 tcp->numlayers = parameters->tcp_numlayers;
6940 assert ( tcp->numlayers >= 0 );
6941 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6943 if (cp->fixed_quality) {
6944 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6946 tcp->rates[j] = parameters->tcp_rates[j];
6948 if (cp->fixed_quality) { /* add fixed_quality */
6949 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6951 tcp->rates[j] = parameters->tcp_rates[j];
6955 tcp->csty = parameters->csty;
6956 tcp->prg = parameters->prog_order;
6957 tcp->mct = parameters->tcp_mct;
6961 if (parameters->numpocs) {
6962 /* initialisation of POC */
6964 assert( parameters->numpocs >= 0 );
6965 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6966 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6967 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6968 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6969 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6970 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6971 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6972 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6973 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6974 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6978 tcp->numpocs = numpocs_tile -1 ;
6983 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6985 for (i = 0; i < image->numcomps; i++) {
6986 opj_tccp_t *tccp = &tcp->tccps[i];
6987 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6988 tccp->numresolutions = parameters->numresolution;
6989 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6990 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6991 tccp->cblksty = parameters->mode;
6992 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6993 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6995 assert(parameters->roi_compno >= 0);
6996 if (i == (OPJ_UINT32)parameters->roi_compno) {
6997 tccp->roishift = parameters->roi_shift;
7002 if(parameters->cp_cinema)
7004 /*Precinct size for lowest frequency subband=128*/
7007 /*Precinct size at all other resolutions = 256*/
7008 for (j = 1; j < tccp->numresolutions; j++) {
7013 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7015 assert(tccp->numresolutions > 0);
7016 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7017 if (p < parameters->res_spec) {
7019 if (parameters->prcw_init[p] < 1) {
7022 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7025 if (parameters->prch_init[p] < 1) {
7028 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7032 int res_spec = parameters->res_spec;
7033 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7034 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7036 if (size_prcw < 1) {
7039 tccp->prcw[j] = int_floorlog2(size_prcw);
7042 if (size_prch < 1) {
7045 tccp->prch[j] = int_floorlog2(size_prch);
7049 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7052 for (j = 0; j < tccp->numresolutions; j++) {
7059 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7064 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7065 opj_cparameters_t *parameters,
7067 struct opj_event_mgr * p_manager)
7069 OPJ_UINT32 i, j, tileno, numpocs_tile;
7070 opj_cp_v2_t *cp = 00;
7073 if(!p_j2k || !parameters || ! image) {
7077 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7078 cp = &(p_j2k->m_cp);
7080 /* set default values for cp */
7085 copy user encoding parameters
7087 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7088 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7089 cp->rsiz = parameters->cp_rsiz;
7090 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7091 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7092 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7094 /* mod fixed_quality */
7095 if (parameters->cp_matrice) {
7096 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7097 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7098 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7102 cp->tdx = parameters->cp_tdx;
7103 cp->tdy = parameters->cp_tdy;
7106 cp->tx0 = parameters->cp_tx0;
7107 cp->ty0 = parameters->cp_ty0;
7109 /* comment string */
7110 if(parameters->cp_comment) {
7111 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7113 strcpy(cp->comment, parameters->cp_comment);
7118 calculate other encoding parameters
7121 if (parameters->tile_size_on) {
7122 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7123 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7125 cp->tdx = image->x1 - cp->tx0;
7126 cp->tdy = image->y1 - cp->ty0;
7129 if (parameters->tp_on) {
7130 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7131 cp->m_specific_param.m_enc.m_tp_on = 1;
7136 calculate JPWL encoding parameters
7139 if (parameters->jpwl_epc_on) {
7143 cp->epc_on = OPJ_TRUE;
7144 cp->info_on = OPJ_FALSE; /* no informative technique */
7147 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7148 cp->epb_on = OPJ_TRUE;
7150 cp->hprot_MH = parameters->jpwl_hprot_MH;
7151 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7152 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7153 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7155 /* if tile specs are not specified, copy MH specs */
7156 if (cp->hprot_TPH[0] == -1) {
7157 cp->hprot_TPH_tileno[0] = 0;
7158 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7160 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7161 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7162 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7163 cp->pprot[i] = parameters->jpwl_pprot[i];
7167 /* set ESD writing */
7168 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7169 cp->esd_on = OPJ_TRUE;
7171 cp->sens_size = parameters->jpwl_sens_size;
7172 cp->sens_addr = parameters->jpwl_sens_addr;
7173 cp->sens_range = parameters->jpwl_sens_range;
7175 cp->sens_MH = parameters->jpwl_sens_MH;
7176 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7177 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7178 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7182 /* always set RED writing to false: we are at the encoder */
7183 cp->red_on = OPJ_FALSE;
7186 cp->epc_on = OPJ_FALSE;
7188 #endif /* USE_JPWL */
7191 /* initialize the mutiple tiles */
7192 /* ---------------------------- */
7193 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7194 if (parameters->numpocs) {
7195 /* initialisation of POC */
7196 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7200 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7201 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7202 tcp->numlayers = parameters->tcp_numlayers;
7204 for (j = 0; j < tcp->numlayers; j++) {
7205 if(cp->m_specific_param.m_enc.m_cinema){
7206 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7207 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7209 tcp->rates[j] = parameters->tcp_rates[j];
7211 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7212 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7214 tcp->rates[j] = parameters->tcp_rates[j];
7219 tcp->csty = parameters->csty;
7220 tcp->prg = parameters->prog_order;
7221 tcp->mct = parameters->tcp_mct;
7226 if (parameters->numpocs) {
7227 /* initialisation of POC */
7230 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7231 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7232 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7234 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7235 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7236 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7237 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7238 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7239 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7240 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7246 tcp->numpocs = numpocs_tile -1 ;
7251 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7253 if (parameters->mct_data) {
7255 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7258 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7259 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7260 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7261 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7264 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7265 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7266 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7268 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7269 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7271 tcp->mct_norms = (OPJ_FLOAT64*)
7272 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7274 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7277 for (i = 0; i < image->numcomps; i++) {
7278 opj_tccp_t *tccp = &tcp->tccps[i];
7279 tccp->m_dc_level_shift = l_dc_shift[i];
7282 j2k_setup_mct_encoding(tcp,image);
7286 for (i = 0; i < image->numcomps; i++) {
7287 opj_tccp_t *tccp = &tcp->tccps[i];
7288 opj_image_comp_t * l_comp = &(image->comps[i]);
7290 if (! l_comp->sgnd) {
7291 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7296 for (i = 0; i < image->numcomps; i++) {
7297 opj_tccp_t *tccp = &tcp->tccps[i];
7299 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7300 tccp->numresolutions = parameters->numresolution;
7301 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7302 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7303 tccp->cblksty = parameters->mode;
7304 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7305 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7308 if (i == parameters->roi_compno) {
7309 tccp->roishift = parameters->roi_shift;
7314 if(parameters->cp_cinema) {
7315 //Precinct size for lowest frequency subband=128
7318 //Precinct size at all other resolutions = 256
7319 for (j = 1; j < tccp->numresolutions; j++) {
7324 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7325 OPJ_INT32 p = 0, it_res;
7326 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7327 if (p < parameters->res_spec) {
7329 if (parameters->prcw_init[p] < 1) {
7330 tccp->prcw[it_res] = 1;
7332 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7335 if (parameters->prch_init[p] < 1) {
7336 tccp->prch[it_res] = 1;
7338 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7342 int res_spec = parameters->res_spec;
7343 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7344 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7346 if (size_prcw < 1) {
7347 tccp->prcw[it_res] = 1;
7349 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7352 if (size_prch < 1) {
7353 tccp->prch[it_res] = 1;
7355 tccp->prch[it_res] = int_floorlog2(size_prch);
7359 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7362 for (j = 0; j < tccp->numresolutions; j++) {
7369 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7373 if (parameters->mct_data) {
7374 opj_free(parameters->mct_data);
7375 parameters->mct_data = 00;
7380 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7383 opj_cp_t *cp = NULL;
7385 opj_tcd_t *tcd = NULL; /* TCD component */
7393 j2k->cstr_info = cstr_info;
7396 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7397 cstr_info->image_w = image->x1 - image->x0;
7398 cstr_info->image_h = image->y1 - image->y0;
7399 cstr_info->prog = (&cp->tcps[0])->prg;
7400 cstr_info->tw = cp->tw;
7401 cstr_info->th = cp->th;
7402 cstr_info->tile_x = cp->tdx; /* new version parser */
7403 cstr_info->tile_y = cp->tdy; /* new version parser */
7404 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7405 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7406 cstr_info->numcomps = image->numcomps;
7407 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7408 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7409 for (compno=0; compno < image->numcomps; compno++) {
7410 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7412 cstr_info->D_max = 0.0; /* ADD Marcela */
7413 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7414 cstr_info->maxmarknum = 100;
7415 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7416 cstr_info->marknum = 0;
7426 for (compno = 1; compno < image->numcomps; compno++) {
7427 j2k_write_coc(j2k, compno);
7428 j2k_write_qcc(j2k, compno);
7432 for (compno = 0; compno < image->numcomps; compno++) {
7433 opj_tcp_t *tcp = &cp->tcps[0];
7434 if (tcp->tccps[compno].roishift)
7435 j2k_write_rgn(j2k, compno, 0);
7437 if (cp->comment != NULL) {
7441 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7445 if (cp->cinema == CINEMA4K_24) {
7450 /* uncomment only for testing JPSEC marker writing */
7451 /* j2k_write_sec(j2k); */
7455 cstr_info->main_head_end = cio_tell(cio) - 1;
7458 /**** Main Header ENDS here ***/
7460 /* create the tile encoder */
7461 tcd = tcd_create(j2k->cinfo);
7463 /* encode each tile */
7464 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7468 int acc_pack_num = 0;
7472 opj_tcp_t *tcp = &cp->tcps[tileno];
7473 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7475 j2k->curtileno = tileno;
7476 j2k->cur_tp_num = 0;
7477 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7478 /* initialisation before tile encoding */
7480 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7482 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7487 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7488 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7489 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7490 cstr_info->tile[j2k->curtileno].marknum = 0;
7494 for(pino = 0; pino <= tcp->numpocs; pino++) {
7498 /*Get number of tile parts*/
7499 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7500 tcd->tp_pos = cp->tp_pos;
7502 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7503 j2k->tp_num = tilepartno;
7506 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7507 cio_tell(cio) + j2k->pos_correction;
7511 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7512 for (compno = 1; compno < image->numcomps; compno++) {
7513 j2k_write_coc(j2k, compno);
7514 j2k_write_qcc(j2k, compno);
7516 if (cp->tcps[tileno].numpocs) {
7523 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7524 cio_tell(cio) + j2k->pos_correction + 1;
7527 j2k_write_sod(j2k, tcd);
7531 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7532 cio_tell(cio) + j2k->pos_correction - 1;
7533 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7535 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7536 cstr_info->packno - acc_pack_num;
7537 acc_pack_num = cstr_info->packno;
7545 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7550 if (tile->PPT) { // BAD PPT !!!
7553 PPT_file=fopen("PPT","rb");
7554 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7555 for (i=0;i<tile->len_ppt;i++) {
7557 fread(&elmt, 1, 1, PPT_file);
7558 fwrite(&elmt,1,1,f);
7567 /* destroy the tile encoder */
7568 tcd_free_encode(tcd);
7571 opj_free(j2k->cur_totnum_tp);
7576 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7578 /* The following adjustment is done to adjust the codestream size */
7579 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7580 /* the first bunch of bytes is not in the codestream */
7581 cstr_info->codestream_size -= cstr_info->main_head_start;
7587 preparation of JPWL marker segments
7591 /* encode according to JPWL */
7592 jpwl_encode(j2k, cio, image);
7595 #endif /* USE_JPWL */
7600 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7602 assert(cstr_info != 00);
7604 /* expand the list? */
7605 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7606 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7607 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7610 /* add the marker */
7611 cstr_info->marker[cstr_info->marknum].type = type;
7612 cstr_info->marker[cstr_info->marknum].pos = pos;
7613 cstr_info->marker[cstr_info->marknum].len = len;
7614 cstr_info->marknum++;
7618 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7620 assert(cstr_index != 00);
7622 /* expand the list? */
7623 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7624 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7625 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7628 /* add the marker */
7629 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7630 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7631 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7632 cstr_index->marknum++;
7636 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7638 opj_marker_info_t *marker;
7640 assert(cstr_info != 00);
7642 /* expand the list? */
7643 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7644 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7645 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7648 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7650 /* add the marker */
7651 marker->type = type;
7654 cstr_info->tile[tileno].marknum++;
7657 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7659 assert(cstr_index != 00);
7660 assert(cstr_index->tile_index != 00);
7662 /* expand the list? */
7663 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7664 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7665 cstr_index->tile_index[tileno].marker =
7666 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7667 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7670 /* add the marker */
7671 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7672 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7673 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7674 cstr_index->tile_index[tileno].marknum++;
7676 if (type == J2K_MS_SOT) {
7677 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7679 if (cstr_index->tile_index[tileno].tp_index)
7680 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7687 * -----------------------------------------------------------------------
7688 * -----------------------------------------------------------------------
7689 * -----------------------------------------------------------------------
7693 * Ends the decompression procedures and possibiliy add data to be read after the
7696 opj_bool j2k_end_decompress(
7697 opj_j2k_v2_t *p_j2k,
7698 opj_stream_private_t *p_stream,
7699 opj_event_mgr_t * p_manager)
7708 * Reads a jpeg2000 codestream header structure.
7711 * @param p_stream the stream to read data from.
7712 * @param p_j2k the jpeg2000 codec.
7713 * @param p_manager the user event manager.
7715 * @return true if the box is valid.
7717 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7718 opj_j2k_v2_t* p_j2k,
7719 opj_image_t** p_image,
7720 struct opj_event_mgr* p_manager )
7723 assert(p_j2k != 00);
7724 assert(p_stream != 00);
7725 assert(p_manager != 00);
7727 /* create an empty image header */
7728 p_j2k->m_private_image = opj_image_create0();
7729 if (! p_j2k->m_private_image) {
7733 /* customization of the validation */
7734 opj_j2k_setup_decoding_validation(p_j2k);
7736 /* validation of the parameters codec */
7737 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7738 opj_image_destroy(p_j2k->m_private_image);
7739 p_j2k->m_private_image = NULL;
7743 /* customization of the encoding */
7744 opj_j2k_setup_header_reading(p_j2k);
7747 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7748 opj_image_destroy(p_j2k->m_private_image);
7749 p_j2k->m_private_image = NULL;
7753 *p_image = opj_image_create0();
7758 /* Copy codestream image information to the output image */
7759 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7761 /*Allocate and initialize some elements of codestrem index*/
7762 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
7770 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7772 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7775 assert(p_j2k != 00);
7777 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
7779 /* DEVELOPER CORNER, add your custom procedures */
7780 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
7785 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7786 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7788 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7791 assert(p_j2k != 00);
7793 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
7794 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
7795 /* DEVELOPER CORNER, add your custom validation procedure */
7801 * The mct encoding validation procedure.
7803 * @param p_j2k the jpeg2000 codec to validate.
7804 * @param p_stream the input stream to validate.
7805 * @param p_manager the user event manager.
7807 * @return true if the parameters are correct.
7809 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7810 opj_stream_private_t *p_stream,
7811 opj_event_mgr_t * p_manager )
7813 opj_bool l_is_valid = OPJ_TRUE;
7817 assert(p_j2k != 00);
7818 assert(p_stream != 00);
7819 assert(p_manager != 00);
7821 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7822 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7823 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7825 for (i=0;i<l_nb_tiles;++i) {
7826 if (l_tcp->mct == 2) {
7827 opj_tccp_t * l_tccp = l_tcp->tccps;
7828 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7830 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7831 l_is_valid &= ! (l_tccp->qmfbid & 1);
7842 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7845 OPJ_UINT32 l_indix = 1;
7846 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7847 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7848 OPJ_UINT32 l_mct_size,l_nb_elem;
7849 OPJ_FLOAT32 * l_data, * l_current_data;
7850 opj_tccp_t * l_tccp;
7853 assert(p_tcp != 00);
7855 if (p_tcp->mct != 2) {
7859 if (p_tcp->m_mct_decoding_matrix) {
7860 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7861 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7863 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7864 if (! p_tcp->m_mct_records) {
7867 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7869 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7871 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7873 if (l_mct_deco_data->m_data) {
7874 opj_free(l_mct_deco_data->m_data);
7875 l_mct_deco_data->m_data = 00;
7878 l_mct_deco_data->m_index = l_indix++;
7879 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7880 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7881 l_nb_elem = p_image->numcomps * p_image->numcomps;
7882 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7883 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7885 if (! l_mct_deco_data->m_data) {
7889 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);
7891 l_mct_deco_data->m_data_size = l_mct_size;
7892 ++p_tcp->m_nb_mct_records;
7895 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7896 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7897 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7899 if (! p_tcp->m_mct_records) {
7903 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7904 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7906 if (l_mct_deco_data) {
7907 l_mct_deco_data = l_mct_offset_data - 1;
7911 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7913 if (l_mct_offset_data->m_data) {
7914 opj_free(l_mct_offset_data->m_data);
7915 l_mct_offset_data->m_data = 00;
7918 l_mct_offset_data->m_index = l_indix++;
7919 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7920 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7921 l_nb_elem = p_image->numcomps;
7922 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7923 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7925 if (! l_mct_offset_data->m_data) {
7929 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7931 opj_free(l_mct_offset_data->m_data);
7932 l_mct_offset_data->m_data = 00;
7936 l_tccp = p_tcp->tccps;
7937 l_current_data = l_data;
7939 for (i=0;i<l_nb_elem;++i) {
7940 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7944 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7948 l_mct_offset_data->m_data_size = l_mct_size;
7950 ++p_tcp->m_nb_mct_records;
7952 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7953 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7954 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7955 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7957 if (! p_tcp->m_mcc_records) {
7960 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7961 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));
7965 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7966 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7967 l_mcc_data->m_is_irreversible = 1;
7968 l_mcc_data->m_nb_comps = p_image->numcomps;
7969 l_mcc_data->m_index = l_indix++;
7970 l_mcc_data->m_offset_array = l_mct_offset_data;
7971 ++p_tcp->m_nb_mcc_records;
7977 * Builds the cp decoder parameters to use to decode tile.
7979 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7980 opj_stream_private_t *p_stream,
7981 opj_event_mgr_t * p_manager )
7983 /* add here initialization of cp
7984 copy paste of setup_decoder */
7992 * Builds the cp encoder parameters to use to encode tile.
7994 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7995 opj_stream_private_t *p_stream,
7996 opj_event_mgr_t * p_manager )
7998 /* add here initialization of cp
7999 copy paste of setup_encoder */
8007 * The default encoding validation procedure without any extension.
8009 * @param p_j2k the jpeg2000 codec to validate.
8010 * @param p_stream the input stream to validate.
8011 * @param p_manager the user event manager.
8013 * @return true if the parameters are correct.
8015 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8016 opj_stream_private_t *p_stream,
8017 opj_event_mgr_t * p_manager )
8019 opj_bool l_is_valid = OPJ_TRUE;
8022 assert(p_j2k != 00);
8023 assert(p_stream != 00);
8024 assert(p_manager != 00);
8026 /* STATE checking */
8027 /* make sure the state is at 0 */
8028 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8030 /* POINTER validation */
8031 /* make sure a p_j2k codec is present */
8032 l_is_valid &= (p_j2k->m_procedure_list != 00);
8033 /* make sure a validation list is present */
8034 l_is_valid &= (p_j2k->m_validation_list != 00);
8036 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8037 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8041 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8042 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8046 /* PARAMETER VALIDATION */
8051 * The default decoding validation procedure without any extension.
8053 * @param p_j2k the jpeg2000 codec to validate.
8054 * @param p_stream the input stream to validate.
8055 * @param p_manager the user event manager.
8057 * @return true if the parameters are correct.
8059 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
8060 opj_stream_private_t *p_stream,
8061 opj_event_mgr_t * p_manager
8064 opj_bool l_is_valid = OPJ_TRUE;
8067 assert(p_j2k != 00);
8068 assert(p_stream != 00);
8069 assert(p_manager != 00);
8072 /* STATE checking */
8073 /* make sure the state is at 0 */
8075 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8077 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8079 /* POINTER validation */
8080 /* make sure a p_j2k codec is present */
8081 /* make sure a procedure list is present */
8082 l_is_valid &= (p_j2k->m_procedure_list != 00);
8083 /* make sure a validation list is present */
8084 l_is_valid &= (p_j2k->m_validation_list != 00);
8086 /* PARAMETER VALIDATION */
8090 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8091 opj_stream_private_t *p_stream,
8092 opj_event_mgr_t * p_manager)
8094 OPJ_UINT32 l_current_marker;
8095 OPJ_UINT32 l_marker_size;
8096 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8099 assert(p_stream != 00);
8100 assert(p_j2k != 00);
8101 assert(p_manager != 00);
8103 /* We enter in the main header */
8104 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8106 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8107 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8108 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8112 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8113 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8114 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8118 /* Read 2 bytes as the new marker ID */
8119 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8121 /* Try to read until the SOT is detected */
8122 while (l_current_marker != J2K_MS_SOT) {
8124 /* Check if the current marker ID is valid */
8125 if (l_current_marker < 0xff00) {
8126 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8130 /* Get the marker handler from the marker ID */
8131 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8133 /* Manage case where marker is unknown */
8134 if (l_marker_handler->id == J2K_MS_UNK) {
8135 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
8136 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8140 if (l_current_marker == J2K_MS_SOT)
8141 break; /* SOT marker is detected main header is completely read */
8142 else /* Get the marker handler from the marker ID */
8143 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8146 /* Check if the marker is known and if it is the right place to find it */
8147 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8148 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8152 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8153 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8154 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8158 /* read 2 bytes as the marker size */
8159 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8160 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8162 /* Check if the marker size is compatible with the header data size */
8163 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8164 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8165 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8166 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8169 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8172 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8173 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) {
8174 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8178 /* Read the marker segment with the correct marker handler */
8179 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8180 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8184 /* Add the marker to the codestream index*/
8185 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8186 l_marker_handler->id,
8187 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8188 l_marker_size + 4 );
8190 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8191 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8192 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8196 /* read 2 bytes as the new marker ID */
8197 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8200 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8202 /* Position of the last element if the main header */
8203 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8205 /* Next step: read a tile-part header */
8206 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8212 * Excutes the given procedures on the given codec.
8214 * @param p_procedure_list the list of procedures to execute
8215 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8216 * @param p_stream the stream to execute the procedures on.
8217 * @param p_manager the user manager.
8219 * @return true if all the procedures were successfully executed.
8221 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
8222 opj_procedure_list_t * p_procedure_list,
8223 opj_stream_private_t *p_stream,
8224 opj_event_mgr_t * p_manager )
8226 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8227 opj_bool l_result = OPJ_TRUE;
8228 OPJ_UINT32 l_nb_proc, i;
8231 assert(p_procedure_list != 00);
8232 assert(p_j2k != 00);
8233 assert(p_stream != 00);
8234 assert(p_manager != 00);
8237 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8238 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);
8240 for (i=0;i<l_nb_proc;++i) {
8241 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8245 /* and clear the procedure list at the end.*/
8246 opj_procedure_list_clear(p_procedure_list);
8251 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
8252 opj_stream_private_t *p_stream,
8253 opj_event_mgr_t * p_manager
8256 opj_tcp_v2_t * l_tcp = 00;
8257 opj_tcp_v2_t * l_default_tcp = 00;
8258 OPJ_UINT32 l_nb_tiles;
8260 opj_tccp_t *l_current_tccp = 00;
8261 OPJ_UINT32 l_tccp_size;
8262 OPJ_UINT32 l_mct_size;
8263 opj_image_t * l_image;
8264 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8265 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8266 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8267 OPJ_UINT32 l_offset;
8270 assert(p_j2k != 00);
8271 assert(p_stream != 00);
8272 assert(p_manager != 00);
8274 l_image = p_j2k->m_private_image;
8275 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8276 l_tcp = p_j2k->m_cp.tcps;
8277 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8278 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8279 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8282 for (i=0; i<l_nb_tiles; ++i) {
8283 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8284 l_current_tccp = l_tcp->tccps;
8285 /*Copy default coding parameters into the current tile coding parameters*/
8286 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8287 /* Initialize some values of the current tile coding parameters*/
8289 l_tcp->ppt_data = 00;
8290 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8291 l_tcp->tccps = l_current_tccp;
8293 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8294 if (l_default_tcp->m_mct_decoding_matrix) {
8295 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8296 if (! l_tcp->m_mct_decoding_matrix ) {
8299 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8302 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8303 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8304 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8305 if (! l_tcp->m_mct_records) {
8308 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8310 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8311 l_src_mct_rec = l_default_tcp->m_mct_records;
8312 l_dest_mct_rec = l_tcp->m_mct_records;
8314 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8316 if (l_src_mct_rec->m_data) {
8318 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8319 if(! l_dest_mct_rec->m_data) {
8322 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8329 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8330 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8331 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8332 if (! l_tcp->m_mcc_records) {
8335 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8337 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8338 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8339 l_dest_mcc_rec = l_tcp->m_mcc_records;
8341 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8343 if (l_src_mcc_rec->m_decorrelation_array) {
8344 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8345 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8348 if (l_src_mcc_rec->m_offset_array) {
8349 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8350 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8357 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8358 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8360 /* Move to next tile cp*/
8364 /* Create the current tile decoder*/
8365 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8366 if (! p_j2k->m_tcd ) {
8370 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8371 tcd_destroy_v2(p_j2k->m_tcd);
8373 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8381 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8382 * with the marker value.
8383 * @param p_id Marker value to look up
8385 * @return the handler associated with the id.
8387 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
8389 const opj_dec_memory_marker_handler_t *e;
8390 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8391 if (e->id == p_id) {
8392 break; /* we find a handler corresponding to the marker ID*/
8400 * Destroys a jpeg2000 codec.
8402 * @param p_j2k the jpeg20000 structure to destroy.
8404 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8410 if (p_j2k->m_is_decoder) {
8412 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8413 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8414 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8415 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8418 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8419 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8420 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8421 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8426 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8427 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8428 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8431 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8432 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8433 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8434 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8437 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8438 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8439 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8440 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8444 tcd_destroy_v2(p_j2k->m_tcd);
8446 opj_j2k_cp_destroy(&(p_j2k->m_cp));
8447 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8449 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8450 p_j2k->m_procedure_list = 00;
8452 opj_procedure_list_destroy(p_j2k->m_validation_list);
8453 p_j2k->m_procedure_list = 00;
8455 j2k_destroy_cstr_index(p_j2k->cstr_index);
8456 p_j2k->cstr_index = NULL;
8458 opj_image_destroy(p_j2k->m_private_image);
8459 p_j2k->m_private_image = NULL;
8461 opj_image_destroy(p_j2k->m_output_image);
8462 p_j2k->m_output_image = NULL;
8467 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8471 if (p_cstr_ind->marker) {
8472 opj_free(p_cstr_ind->marker);
8473 p_cstr_ind->marker = NULL;
8476 if (p_cstr_ind->tile_index) {
8477 OPJ_UINT32 it_tile = 0;
8479 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8481 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8482 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8483 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8486 if(p_cstr_ind->tile_index[it_tile].tp_index){
8487 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8488 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8491 if(p_cstr_ind->tile_index[it_tile].marker){
8492 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8493 p_cstr_ind->tile_index[it_tile].marker = NULL;
8498 opj_free( p_cstr_ind->tile_index);
8499 p_cstr_ind->tile_index = NULL;
8502 opj_free(p_cstr_ind);
8509 * Destroys a tile coding parameter structure.
8511 * @param p_tcp the tile coding parameter to destroy.
8513 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8519 if (p_tcp->ppt_buffer != 00) {
8520 opj_free(p_tcp->ppt_buffer);
8521 p_tcp->ppt_buffer = 00;
8524 if (p_tcp->tccps != 00) {
8525 opj_free(p_tcp->tccps);
8529 if (p_tcp->m_mct_coding_matrix != 00) {
8530 opj_free(p_tcp->m_mct_coding_matrix);
8531 p_tcp->m_mct_coding_matrix = 00;
8534 if (p_tcp->m_mct_decoding_matrix != 00) {
8535 opj_free(p_tcp->m_mct_decoding_matrix);
8536 p_tcp->m_mct_decoding_matrix = 00;
8539 if (p_tcp->m_mcc_records) {
8540 opj_free(p_tcp->m_mcc_records);
8541 p_tcp->m_mcc_records = 00;
8542 p_tcp->m_nb_max_mcc_records = 0;
8543 p_tcp->m_nb_mcc_records = 0;
8546 if (p_tcp->m_mct_records) {
8547 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8550 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8551 if (l_mct_data->m_data) {
8552 opj_free(l_mct_data->m_data);
8553 l_mct_data->m_data = 00;
8559 opj_free(p_tcp->m_mct_records);
8560 p_tcp->m_mct_records = 00;
8563 if (p_tcp->mct_norms != 00) {
8564 opj_free(p_tcp->mct_norms);
8565 p_tcp->mct_norms = 00;
8568 opj_j2k_tcp_data_destroy(p_tcp);
8573 * Destroys the data inside a tile coding parameter structure.
8575 * @param p_tcp the tile coding parameter which contain data to destroy.
8577 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8579 if (p_tcp->m_data) {
8580 opj_free(p_tcp->m_data);
8581 p_tcp->m_data = NULL;
8582 p_tcp->m_data_size = 0;
8587 * Destroys a coding parameter structure.
8589 * @param p_cp the coding parameter to destroy.
8591 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
8593 OPJ_UINT32 l_nb_tiles;
8594 opj_tcp_v2_t * l_current_tile = 00;
8605 l_current_tile = p_cp->tcps;
8606 l_nb_tiles = p_cp->th * p_cp->tw;
8609 (i = 0; i < l_nb_tiles; ++i)
8611 opj_j2k_tcp_destroy(l_current_tile);
8614 opj_free(p_cp->tcps);
8618 (p_cp->ppm_buffer != 00)
8620 opj_free(p_cp->ppm_buffer);
8621 p_cp->ppm_buffer = 00;
8624 (p_cp->comment != 00)
8626 opj_free(p_cp->comment);
8630 (! p_cp->m_is_decoder)
8633 (p_cp->m_specific_param.m_enc.m_matrice)
8635 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8636 p_cp->m_specific_param.m_enc.m_matrice = 00;
8644 * Reads a tile header.
8645 * @param p_j2k the jpeg2000 codec.
8646 * @param p_stream the stream to write data to.
8647 * @param p_manager the user event manager.
8649 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8650 OPJ_UINT32 * p_tile_index,
8651 OPJ_UINT32 * p_data_size,
8652 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8653 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8654 OPJ_UINT32 * p_nb_comps,
8656 opj_stream_private_t *p_stream,
8657 opj_event_mgr_t * p_manager )
8659 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8660 OPJ_UINT32 l_marker_size;
8661 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8662 opj_tcp_v2_t * l_tcp = NULL;
8663 OPJ_UINT32 l_nb_tiles;
8666 assert(p_stream != 00);
8667 assert(p_j2k != 00);
8668 assert(p_manager != 00);
8670 /* Reach the End Of Codestream ?*/
8671 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8672 l_current_marker = J2K_MS_EOC;
8674 /* We need to encounter a SOT marker (a new tile-part header) */
8675 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8679 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8680 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8682 /* Try to read until the Start Of Data is detected */
8683 while (l_current_marker != J2K_MS_SOD) {
8685 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8686 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8687 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8691 /* Read 2 bytes from the buffer as the marker size */
8692 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8694 /* Why this condition? FIXME */
8695 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8696 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8698 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8700 /* Get the marker handler from the marker ID */
8701 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8703 /* Check if the marker is known and if it is the right place to find it */
8704 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8705 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8708 /* FIXME manage case of unknown marker as in the main header ? */
8710 /* Check if the marker size is compatible with the header data size */
8711 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8712 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8713 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8714 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8717 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8720 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8721 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) {
8722 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8726 /* Read the marker segment with the correct marker handler */
8727 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8728 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8732 /* Add the marker to the codestream index*/
8733 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8735 l_marker_handler->id,
8736 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8737 l_marker_size + 4 );
8739 /* Keep the position of the last SOT marker read */
8740 if ( l_marker_handler->id == J2K_MS_SOT ) {
8741 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8742 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8744 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8749 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8750 /* Skip the rest of the tile part header*/
8751 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) {
8752 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8755 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8758 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8759 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8760 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8763 /* Read 2 bytes from the buffer as the new marker ID */
8764 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8768 /* If we didn't skip data before, we need to read the SOD marker*/
8769 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8770 /* Try to read the SOD marker and skip data ? FIXME */
8771 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8777 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8778 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8779 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8780 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8784 /* Read 2 bytes from buffer as the new marker ID */
8785 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8789 /* Indicate we will try to read a new tile-part header*/
8790 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8791 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8792 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8794 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8795 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8796 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8800 /* Read 2 bytes from buffer as the new marker ID */
8801 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8805 /* Current marker is the EOC marker ?*/
8806 if (l_current_marker == J2K_MS_EOC) {
8807 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8808 p_j2k->m_current_tile_number = 0;
8809 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8814 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8815 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8816 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8818 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8819 ++p_j2k->m_current_tile_number;
8823 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8824 *p_go_on = OPJ_FALSE;
8830 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8831 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8835 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8836 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8838 *p_tile_index = p_j2k->m_current_tile_number;
8839 *p_go_on = OPJ_TRUE;
8840 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8841 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8842 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8843 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8844 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8845 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8847 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8853 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8854 OPJ_UINT32 p_tile_index,
8856 OPJ_UINT32 p_data_size,
8857 opj_stream_private_t *p_stream,
8858 opj_event_mgr_t * p_manager )
8860 OPJ_UINT32 l_current_marker;
8861 OPJ_BYTE l_data [2];
8862 opj_tcp_v2_t * l_tcp;
8865 assert(p_stream != 00);
8866 assert(p_j2k != 00);
8867 assert(p_manager != 00);
8869 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8870 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8874 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8875 if (! l_tcp->m_data) {
8876 opj_j2k_tcp_destroy(l_tcp);
8880 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8884 p_j2k->cstr_index) ) {
8885 opj_j2k_tcp_destroy(l_tcp);
8886 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8890 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8894 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8895 * we destroy just the data which will be re-read in read_tile_header*/
8896 /*opj_j2k_tcp_destroy(l_tcp);
8897 p_j2k->m_tcd->tcp = 0;*/
8898 opj_j2k_tcp_data_destroy(l_tcp);
8900 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8901 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8903 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8904 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8905 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8909 opj_read_bytes(l_data,&l_current_marker,2);
8911 if (l_current_marker == J2K_MS_EOC) {
8912 p_j2k->m_current_tile_number = 0;
8913 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8915 else if (l_current_marker != J2K_MS_SOT)
8917 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8926 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8928 OPJ_UINT32 i,j,k = 0;
8929 OPJ_UINT32 l_width_src,l_height_src;
8930 OPJ_UINT32 l_width_dest,l_height_dest;
8931 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8932 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8933 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8934 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8935 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8937 opj_image_comp_t * l_img_comp_src = 00;
8938 opj_image_comp_t * l_img_comp_dest = 00;
8940 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8941 opj_image_t * l_image_src = 00;
8942 OPJ_UINT32 l_size_comp, l_remaining;
8943 OPJ_INT32 * l_dest_ptr;
8944 opj_tcd_resolution_v2_t* l_res= 00;
8946 l_tilec = p_tcd->tcd_image->tiles->comps;
8947 l_image_src = p_tcd->image;
8948 l_img_comp_src = l_image_src->comps;
8950 l_img_comp_dest = p_output_image->comps;
8952 for (i=0; i<l_image_src->numcomps; i++) {
8954 /* Allocate output component buffer if necessary */
8955 if (!l_img_comp_dest->data) {
8957 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8958 if (! l_img_comp_dest->data) {
8963 /* Copy info from decoded comp image to output image */
8964 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8967 /* Compute the precision of the output buffer */
8968 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8969 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8970 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8976 if (l_size_comp == 3) {
8981 /* Current tile component size*/
8983 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8984 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8987 l_width_src = (l_res->x1 - l_res->x0);
8988 l_height_src = (l_res->y1 - l_res->y0);
8990 /* Border of the current output component*/
8991 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8992 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8993 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8994 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8997 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8998 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9002 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9003 * of the input buffer (decoded tile component) which will be move
9004 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9005 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9006 * by this input area.
9008 assert( l_res->x0 >= 0);
9009 assert( l_res->x1 >= 0);
9010 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9011 l_start_x_dest = l_res->x0 - l_x0_dest;
9012 l_offset_x0_src = 0;
9014 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9015 l_width_dest = l_width_src;
9016 l_offset_x1_src = 0;
9019 l_width_dest = l_x1_dest - l_res->x0 ;
9020 l_offset_x1_src = l_width_src - l_width_dest;
9024 l_start_x_dest = 0 ;
9025 l_offset_x0_src = l_x0_dest - l_res->x0;
9027 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9028 l_width_dest = l_width_src - l_offset_x0_src;
9029 l_offset_x1_src = 0;
9032 l_width_dest = l_img_comp_dest->w ;
9033 l_offset_x1_src = l_res->x1 - l_x1_dest;
9037 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9038 l_start_y_dest = l_res->y0 - l_y0_dest;
9039 l_offset_y0_src = 0;
9041 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9042 l_height_dest = l_height_src;
9043 l_offset_y1_src = 0;
9046 l_height_dest = l_y1_dest - l_res->y0 ;
9047 l_offset_y1_src = l_height_src - l_height_dest;
9051 l_start_y_dest = 0 ;
9052 l_offset_y0_src = l_y0_dest - l_res->y0;
9054 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9055 l_height_dest = l_height_src - l_offset_y0_src;
9056 l_offset_y1_src = 0;
9059 l_height_dest = l_img_comp_dest->h ;
9060 l_offset_y1_src = l_res->y1 - l_y1_dest;
9064 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9069 /* Compute the input buffer offset */
9070 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9071 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9072 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9074 /* Compute the output buffer offset */
9075 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9076 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9078 /* Move the output buffer to the first place where we will write*/
9079 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9082 fprintf(stdout, "COMPO[%d]:\n",i);
9083 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9084 "\t tile offset:%d, %d, %d, %d\n"
9085 "\t buffer offset: %d; %d, %d\n",
9086 l_res->x0, l_res->y0, l_width_src, l_height_src,
9087 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9088 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9090 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9091 "\t start offset: %d, line offset= %d\n",
9092 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9096 switch (l_size_comp) {
9099 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9100 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9102 if (l_img_comp_src->sgnd) {
9103 for (j = 0 ; j < l_height_dest ; ++j) {
9104 for ( k = 0 ; k < l_width_dest ; ++k) {
9105 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9108 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9109 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9113 for ( j = 0 ; j < l_height_dest ; ++j ) {
9114 for ( k = 0 ; k < l_width_dest ; ++k) {
9115 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9118 l_dest_ptr+= l_line_offset_dest;
9119 l_src_ptr += l_line_offset_src;
9123 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9124 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9129 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9130 l_src_ptr += l_start_offset_src;
9132 if (l_img_comp_src->sgnd) {
9133 for (j=0;j<l_height_dest;++j) {
9134 for (k=0;k<l_width_dest;++k) {
9135 *(l_dest_ptr++) = *(l_src_ptr++);
9138 l_dest_ptr+= l_line_offset_dest;
9139 l_src_ptr += l_line_offset_src ;
9143 for (j=0;j<l_height_dest;++j) {
9144 for (k=0;k<l_width_dest;++k) {
9145 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9148 l_dest_ptr+= l_line_offset_dest;
9149 l_src_ptr += l_line_offset_src ;
9153 l_src_ptr += l_end_offset_src;
9154 p_data = (OPJ_BYTE*) l_src_ptr;
9159 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9160 l_src_ptr += l_start_offset_src;
9162 for (j=0;j<l_height_dest;++j) {
9163 for (k=0;k<l_width_dest;++k) {
9164 *(l_dest_ptr++) = (*(l_src_ptr++));
9167 l_dest_ptr+= l_line_offset_dest;
9168 l_src_ptr += l_line_offset_src ;
9171 l_src_ptr += l_end_offset_src;
9172 p_data = (OPJ_BYTE*) l_src_ptr;
9186 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9188 * @param p_j2k the jpeg2000 codec.
9189 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9190 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9191 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9192 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9193 * @param p_manager the user event manager
9195 * @return true if the area could be set.
9197 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9198 opj_image_t* p_image,
9199 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9200 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9201 struct opj_event_mgr * p_manager )
9203 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9204 opj_image_t * l_image = p_j2k->m_private_image;
9207 OPJ_INT32 l_comp_x1, l_comp_y1;
9208 opj_image_comp_t* l_img_comp = NULL;
9210 /* Check if we are read the main header */
9211 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9212 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9216 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9217 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9219 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9220 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9221 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9222 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9228 /* Check if the positions provided by the user are correct */
9231 assert(p_start_x >= 0 );
9232 assert(p_start_y >= 0 );
9234 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9235 opj_event_msg_v2(p_manager, EVT_ERROR,
9236 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9237 p_start_x, l_image->x1);
9240 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9241 opj_event_msg_v2(p_manager, EVT_WARNING,
9242 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9243 p_start_x, l_image->x0);
9244 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9245 p_image->x0 = l_image->x0;
9248 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9249 p_image->x0 = p_start_x;
9253 if ((OPJ_UINT32)p_start_y > l_image->y1){
9254 opj_event_msg_v2(p_manager, EVT_ERROR,
9255 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9256 p_start_y, l_image->y1);
9259 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9260 opj_event_msg_v2(p_manager, EVT_WARNING,
9261 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9262 p_start_y, l_image->y0);
9263 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9264 p_image->y0 = l_image->y0;
9267 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9268 p_image->y0 = p_start_y;
9272 assert((OPJ_UINT32)p_end_x > 0);
9273 assert((OPJ_UINT32)p_end_y > 0);
9274 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9275 opj_event_msg_v2(p_manager, EVT_ERROR,
9276 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9277 p_end_x, l_image->x0);
9280 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9281 opj_event_msg_v2(p_manager, EVT_WARNING,
9282 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9283 p_end_x, l_image->x1);
9284 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9285 p_image->x1 = l_image->x1;
9288 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9289 p_image->x1 = p_end_x;
9293 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9294 opj_event_msg_v2(p_manager, EVT_ERROR,
9295 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9296 p_end_y, l_image->y0);
9299 if ((OPJ_UINT32)p_end_y > l_image->y1){
9300 opj_event_msg_v2(p_manager, EVT_WARNING,
9301 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9302 p_end_y, l_image->y1);
9303 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9304 p_image->y1 = l_image->y1;
9307 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9308 p_image->y1 = p_end_y;
9312 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9314 l_img_comp = p_image->comps;
9315 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9319 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9320 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9321 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9322 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9324 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9325 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9327 opj_event_msg_v2(p_manager, EVT_ERROR,
9328 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9332 l_img_comp->w = l_w;
9334 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9335 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9337 opj_event_msg_v2(p_manager, EVT_ERROR,
9338 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9342 l_img_comp->h = l_h;
9347 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9348 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9355 /* ----------------------------------------------------------------------- */
9356 /* J2K / JPT decoder interface */
9357 /* ----------------------------------------------------------------------- */
9359 * Creates a J2K decompression structure.
9361 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9363 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9365 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9369 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9371 l_j2k->m_is_decoder = 1;
9372 l_j2k->m_cp.m_is_decoder = 1;
9374 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9375 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9379 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9381 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9382 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9387 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9389 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9391 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9393 /* codestream index creation */
9394 l_j2k->cstr_index = opj_j2k_create_cstr_index();
9396 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9397 if (!l_j2k->cstr_index){
9402 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9405 /* validation list creation */
9406 l_j2k->m_validation_list = opj_procedure_list_create();
9407 if (! l_j2k->m_validation_list) {
9412 /* execution list creation */
9413 l_j2k->m_procedure_list = opj_procedure_list_create();
9414 if (! l_j2k->m_procedure_list) {
9423 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
9425 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9426 opj_calloc(1,sizeof(opj_codestream_index_t));
9430 cstr_index->maxmarknum = 100;
9431 cstr_index->marknum = 0;
9432 cstr_index->marker = (opj_marker_info_t*)
9433 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9434 if (!cstr_index-> marker)
9437 cstr_index->tile_index = NULL;
9444 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9446 * @param p_tile_no the tile index.
9447 * @param p_comp_no the component being outputted.
9448 * @param p_j2k the J2K codec.
9450 * @return the number of bytes taken by the SPCod element.
9452 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9453 OPJ_UINT32 p_tile_no,
9454 OPJ_UINT32 p_comp_no )
9456 opj_cp_v2_t *l_cp = 00;
9457 opj_tcp_v2_t *l_tcp = 00;
9458 opj_tccp_t *l_tccp = 00;
9461 assert(p_j2k != 00);
9463 l_cp = &(p_j2k->m_cp);
9464 l_tcp = &l_cp->tcps[p_tile_no];
9465 l_tccp = &l_tcp->tccps[p_comp_no];
9467 /* preconditions again */
9468 assert(p_tile_no < (l_cp->tw * l_cp->th));
9469 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9471 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9472 return 5 + l_tccp->numresolutions;
9480 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9482 * @param p_comp_no the component number to output.
9483 * @param p_stream the stream to write data to.
9484 * @param p_j2k J2K codec.
9485 * @param p_manager the user event manager.
9488 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9489 OPJ_UINT32 p_tile_no,
9490 OPJ_UINT32 p_comp_no,
9492 OPJ_UINT32 * p_header_size,
9493 struct opj_event_mgr * p_manager )
9496 opj_cp_v2_t *l_cp = 00;
9497 opj_tcp_v2_t *l_tcp = 00;
9498 opj_tccp_t *l_tccp = 00;
9501 assert(p_j2k != 00);
9502 assert(p_header_size != 00);
9503 assert(p_manager != 00);
9504 assert(p_data != 00);
9506 l_cp = &(p_j2k->m_cp);
9507 l_tcp = &l_cp->tcps[p_tile_no];
9508 l_tccp = &l_tcp->tccps[p_comp_no];
9510 /* preconditions again */
9511 assert(p_tile_no < (l_cp->tw * l_cp->th));
9512 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9514 if (*p_header_size < 5) {
9515 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9519 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9522 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9525 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9528 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9531 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9534 *p_header_size = *p_header_size - 5;
9536 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9538 if (*p_header_size < l_tccp->numresolutions) {
9539 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9543 for (i = 0; i < l_tccp->numresolutions; ++i) {
9544 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9548 *p_header_size = *p_header_size - l_tccp->numresolutions;
9555 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9556 * @param p_header_data the data contained in the COM box.
9557 * @param p_j2k the jpeg2000 codec.
9558 * @param p_header_size the size of the data contained in the COM marker.
9559 * @param p_manager the user event manager.
9561 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9563 OPJ_BYTE * p_header_data,
9564 OPJ_UINT32 * p_header_size,
9565 opj_event_mgr_t * p_manager)
9567 OPJ_UINT32 i, l_tmp;
9568 opj_cp_v2_t *l_cp = NULL;
9569 opj_tcp_v2_t *l_tcp = NULL;
9570 opj_tccp_t *l_tccp = NULL;
9571 OPJ_BYTE * l_current_ptr = NULL;
9574 assert(p_j2k != 00);
9575 assert(p_manager != 00);
9576 assert(p_header_data != 00);
9578 l_cp = &(p_j2k->m_cp);
9579 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9580 &l_cp->tcps[p_j2k->m_current_tile_number] :
9581 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9583 /* precondition again */
9584 assert(compno < p_j2k->m_private_image->numcomps);
9586 l_tccp = &l_tcp->tccps[compno];
9587 l_current_ptr = p_header_data;
9589 /* make sure room is sufficient */
9590 if (*p_header_size < 5) {
9591 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9595 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9596 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9599 /* If user wants to remove more resolutions than the codestream contains, return error */
9600 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9601 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9602 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9603 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9607 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9611 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9615 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9618 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9621 *p_header_size = *p_header_size - 5;
9623 /* use custom precinct size ? */
9624 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9625 if (*p_header_size < l_tccp->numresolutions) {
9626 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9630 for (i = 0; i < l_tccp->numresolutions; ++i) {
9631 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9633 l_tccp->prcw[i] = l_tmp & 0xf;
9634 l_tccp->prch[i] = l_tmp >> 4;
9637 *p_header_size = *p_header_size - l_tccp->numresolutions;
9640 /* set default size for the precinct width and height */
9641 for (i = 0; i < l_tccp->numresolutions; ++i) {
9642 l_tccp->prcw[i] = 15;
9643 l_tccp->prch[i] = 15;
9647 #ifdef WIP_REMOVE_MSD
9649 if (p_j2k->cstr_info && compno == 0) {
9650 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9652 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9653 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9654 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9655 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9656 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9659 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9660 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9669 * Copies the tile component parameters of all the component from the first tile component.
9671 * @param p_j2k the J2k codec.
9673 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9677 opj_cp_v2_t *l_cp = NULL;
9678 opj_tcp_v2_t *l_tcp = NULL;
9679 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9680 OPJ_UINT32 l_prc_size;
9683 assert(p_j2k != 00);
9685 l_cp = &(p_j2k->m_cp);
9686 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9687 &l_cp->tcps[p_j2k->m_current_tile_number] :
9688 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9690 l_ref_tccp = &l_tcp->tccps[0];
9691 l_copied_tccp = l_ref_tccp + 1;
9692 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9694 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9695 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9696 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9697 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9698 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9699 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9700 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9701 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9707 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9709 * @param p_tile_no the tile index.
9710 * @param p_comp_no the component being outputted.
9711 * @param p_j2k the J2K codec.
9713 * @return the number of bytes taken by the SPCod element.
9715 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9716 OPJ_UINT32 p_tile_no,
9717 OPJ_UINT32 p_comp_no )
9719 OPJ_UINT32 l_num_bands;
9721 opj_cp_v2_t *l_cp = 00;
9722 opj_tcp_v2_t *l_tcp = 00;
9723 opj_tccp_t *l_tccp = 00;
9726 assert(p_j2k != 00);
9728 l_cp = &(p_j2k->m_cp);
9729 l_tcp = &l_cp->tcps[p_tile_no];
9730 l_tccp = &l_tcp->tccps[p_comp_no];
9732 /* preconditions again */
9733 assert(p_tile_no < l_cp->tw * l_cp->th);
9734 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9736 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9738 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9739 return 1 + l_num_bands;
9742 return 1 + 2*l_num_bands;
9747 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9749 * @param p_tile_no the tile to output.
9750 * @param p_comp_no the component number to output.
9751 * @param p_data the data buffer.
9752 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9753 * @param p_j2k J2K codec.
9754 * @param p_manager the user event manager.
9757 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9758 OPJ_UINT32 p_tile_no,
9759 OPJ_UINT32 p_comp_no,
9761 OPJ_UINT32 * p_header_size,
9762 struct opj_event_mgr * p_manager )
9764 OPJ_UINT32 l_header_size;
9765 OPJ_UINT32 l_band_no, l_num_bands;
9766 OPJ_UINT32 l_expn,l_mant;
9768 opj_cp_v2_t *l_cp = 00;
9769 opj_tcp_v2_t *l_tcp = 00;
9770 opj_tccp_t *l_tccp = 00;
9773 assert(p_j2k != 00);
9774 assert(p_header_size != 00);
9775 assert(p_manager != 00);
9776 assert(p_data != 00);
9778 l_cp = &(p_j2k->m_cp);
9779 l_tcp = &l_cp->tcps[p_tile_no];
9780 l_tccp = &l_tcp->tccps[p_comp_no];
9782 /* preconditions again */
9783 assert(p_tile_no < l_cp->tw * l_cp->th);
9784 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9786 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9788 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9789 l_header_size = 1 + l_num_bands;
9791 if (*p_header_size < l_header_size) {
9792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9796 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9799 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9800 l_expn = l_tccp->stepsizes[l_band_no].expn;
9801 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9806 l_header_size = 1 + 2*l_num_bands;
9808 if (*p_header_size < l_header_size) {
9809 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9813 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9816 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9817 l_expn = l_tccp->stepsizes[l_band_no].expn;
9818 l_mant = l_tccp->stepsizes[l_band_no].mant;
9820 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9825 *p_header_size = *p_header_size - l_header_size;
9831 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9833 * @param p_comp_no the component being targeted.
9834 * @param p_header_data the data contained in the COM box.
9835 * @param p_j2k the jpeg2000 codec.
9836 * @param p_header_size the size of the data contained in the COM marker.
9837 * @param p_manager the user event manager.
9839 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9840 OPJ_UINT32 p_comp_no,
9841 OPJ_BYTE* p_header_data,
9842 OPJ_UINT32 * p_header_size,
9843 opj_event_mgr_t * p_manager
9847 OPJ_UINT32 l_band_no;
9848 opj_cp_v2_t *l_cp = 00;
9849 opj_tcp_v2_t *l_tcp = 00;
9850 opj_tccp_t *l_tccp = 00;
9851 OPJ_BYTE * l_current_ptr = 00;
9852 OPJ_UINT32 l_tmp, l_num_band;
9855 assert(p_j2k != 00);
9856 assert(p_manager != 00);
9857 assert(p_header_data != 00);
9859 l_cp = &(p_j2k->m_cp);
9860 /* come from tile part header or main header ?*/
9861 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9862 &l_cp->tcps[p_j2k->m_current_tile_number] :
9863 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9865 /* precondition again*/
9866 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9868 l_tccp = &l_tcp->tccps[p_comp_no];
9869 l_current_ptr = p_header_data;
9871 if (*p_header_size < 1) {
9872 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9875 *p_header_size -= 1;
9877 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9880 l_tccp->qntsty = l_tmp & 0x1f;
9881 l_tccp->numgbits = l_tmp >> 5;
9882 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9886 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9888 (*p_header_size) / 2;
9890 if( l_num_band > J2K_MAXBANDS ) {
9891 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9892 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9893 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9894 /*return OPJ_FALSE;*/
9899 if (l_cp->correct) {
9901 /* if JPWL is on, we check whether there are too many subbands */
9902 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9903 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9904 "JPWL: bad number of subbands in Sqcx (%d)\n",
9907 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9910 /* we try to correct */
9912 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9913 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9918 #endif /* USE_JPWL */
9920 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9921 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9922 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9924 if (l_band_no < J2K_MAXBANDS){
9925 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9926 l_tccp->stepsizes[l_band_no].mant = 0;
9929 *p_header_size = *p_header_size - l_num_band;
9932 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9933 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9935 if (l_band_no < J2K_MAXBANDS){
9936 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9937 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9940 *p_header_size = *p_header_size - 2*l_num_band;
9943 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9944 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9945 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9946 l_tccp->stepsizes[l_band_no].expn =
9947 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9948 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9949 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9957 * Copies the tile component parameters of all the component from the first tile component.
9959 * @param p_j2k the J2k codec.
9961 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9964 opj_cp_v2_t *l_cp = NULL;
9965 opj_tcp_v2_t *l_tcp = NULL;
9966 opj_tccp_t *l_ref_tccp = NULL;
9967 opj_tccp_t *l_copied_tccp = NULL;
9971 assert(p_j2k != 00);
9973 l_cp = &(p_j2k->m_cp);
9974 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9975 &l_cp->tcps[p_j2k->m_current_tile_number] :
9976 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9978 l_ref_tccp = &l_tcp->tccps[0];
9979 l_copied_tccp = l_ref_tccp + 1;
9980 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9982 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9983 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9984 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9985 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9991 * Dump some elements from the J2K decompression structure .
9993 *@param p_j2k the jpeg2000 codec.
9994 *@param flag flag to describe what elments are dump.
9995 *@param out_stream output stream where dump the elements.
9998 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10000 /* Check if the flag is compatible with j2k file*/
10001 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10002 fprintf(out_stream, "Wrong flag\n");
10006 /* Dump the image_header */
10007 if (flag & OPJ_IMG_INFO){
10008 if (p_j2k->m_private_image)
10009 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10012 /* Dump the codestream info from main header */
10013 if (flag & OPJ_J2K_MH_INFO){
10014 opj_j2k_dump_MH_info(p_j2k, out_stream);
10018 /* Dump the codestream info of the current tile */
10019 if (flag & OPJ_J2K_TH_INFO){
10023 /* Dump the codestream index from main header */
10024 if (flag & OPJ_J2K_MH_IND){
10025 opj_j2k_dump_MH_index(p_j2k, out_stream);
10028 /* Dump the codestream index of the current tile */
10029 if (flag & OPJ_J2K_TH_IND){
10036 * Dump index elements of the codestream extract from the main header.
10038 *@param p_j2k the jpeg2000 codec.
10039 *@param out_stream output stream where dump the elements.
10042 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10044 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10045 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10047 fprintf(out_stream, "Codestream index from main header: {\n");
10049 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10050 "\t Main header end position=%" PRIi64 "\n",
10051 cstr_index->main_head_start, cstr_index->main_head_end);
10053 fprintf(out_stream, "\t Marker list: {\n");
10055 if (cstr_index->marker){
10056 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10057 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10058 cstr_index->marker[it_marker].type,
10059 cstr_index->marker[it_marker].pos,
10060 cstr_index->marker[it_marker].len );
10064 fprintf(out_stream, "\t }\n");
10067 if (cstr_index->tile_index){
10069 /* Simple test to avoid to write empty information*/
10070 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10071 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10072 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10075 if (l_acc_nb_of_tile_part)
10077 fprintf(out_stream, "\t Tile index: {\n");
10079 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10080 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10082 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10084 if (cstr_index->tile_index[it_tile].tp_index){
10085 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10086 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10088 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10089 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10090 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10094 if (cstr_index->tile_index[it_tile].marker){
10095 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10096 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10097 cstr_index->tile_index[it_tile].marker[it_marker].type,
10098 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10099 cstr_index->tile_index[it_tile].marker[it_marker].len );
10103 fprintf(out_stream,"\t }\n");
10107 fprintf(out_stream,"}\n");
10112 * Dump info elements of the codestream extract from the main header.
10114 *@param p_j2k the jpeg2000 codec.
10115 *@param out_stream output stream where dump the elements.
10118 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10120 opj_tcp_v2_t * l_default_tile=NULL;
10122 fprintf(out_stream, "Codestream info from main header: {\n");
10124 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10125 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10126 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10128 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10129 if (l_default_tile)
10132 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10134 fprintf(out_stream, "\t default tile {\n");
10135 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10136 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10137 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10138 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10140 for (compno = 0; compno < numcomps; compno++) {
10141 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10143 OPJ_INT32 bandno, numbands;
10146 fprintf(out_stream, "\t\t comp %d {\n", compno);
10147 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10148 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10149 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10150 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10151 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10152 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10154 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10155 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10156 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10158 fprintf(out_stream, "\n");
10160 /* quantization style*/
10161 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10162 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10163 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10164 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10165 for (bandno = 0; bandno < numbands; bandno++) {
10166 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10167 l_tccp->stepsizes[bandno].expn);
10169 fprintf(out_stream, "\n");
10172 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10174 fprintf(out_stream, "\t\t }\n");
10175 } /*end of component of default tile*/
10176 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10180 fprintf(out_stream, "}\n");
10185 * Dump an image header structure.
10187 *@param img_header the image header to dump.
10188 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10189 *@param out_stream output stream where dump the elements.
10191 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10195 if (dev_dump_flag){
10196 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10200 fprintf(out_stream, "Image info {\n");
10201 tab[0] = '\t';tab[1] = '\0';
10204 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10205 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10206 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10208 if (img_header->comps){
10210 for (compno = 0; compno < img_header->numcomps; compno++) {
10211 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10212 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10213 fprintf(out_stream,"%s}\n",tab);
10217 fprintf(out_stream, "}\n");
10221 * Dump a component image header structure.
10223 *@param comp_header the component image header to dump.
10224 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10225 *@param out_stream output stream where dump the elements.
10227 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10231 if (dev_dump_flag){
10232 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10235 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10238 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10239 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10240 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10243 fprintf(out_stream, "}\n");
10248 * Get the codestream info from a JPEG2000 codec.
10250 *@param p_j2k the component image header to dump.
10252 *@return the codestream information extract from the jpg2000 codec
10254 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10257 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10258 opj_tcp_v2_t *l_default_tile;
10259 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10261 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10263 cstr_info->tx0 = p_j2k->m_cp.tx0;
10264 cstr_info->ty0 = p_j2k->m_cp.ty0;
10265 cstr_info->tdx = p_j2k->m_cp.tdx;
10266 cstr_info->tdy = p_j2k->m_cp.tdy;
10267 cstr_info->tw = p_j2k->m_cp.tw;
10268 cstr_info->th = p_j2k->m_cp.th;
10270 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10272 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10274 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10275 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10276 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10277 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10279 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10281 for (compno = 0; compno < numcomps; compno++) {
10282 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10283 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10284 OPJ_INT32 bandno, numbands;
10287 l_tccp_info->csty = l_tccp->csty;
10288 l_tccp_info->numresolutions = l_tccp->numresolutions;
10289 l_tccp_info->cblkw = l_tccp->cblkw;
10290 l_tccp_info->cblkh = l_tccp->cblkh;
10291 l_tccp_info->cblksty = l_tccp->cblksty;
10292 l_tccp_info->qmfbid = l_tccp->qmfbid;
10293 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10295 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10296 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10299 /* quantization style*/
10300 l_tccp_info->qntsty = l_tccp->qntsty;
10301 l_tccp_info->numgbits = l_tccp->numgbits;
10303 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10304 if (numbands < J2K_MAXBANDS) {
10305 for (bandno = 0; bandno < numbands; bandno++) {
10306 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10307 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10312 l_tccp_info->roishift = l_tccp->roishift;
10320 * Get the codestream index from a JPEG2000 codec.
10322 *@param p_j2k the component image header to dump.
10324 *@return the codestream index extract from the jpg2000 codec
10326 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10328 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10329 opj_calloc(1,sizeof(opj_codestream_index_t));
10333 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10334 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10335 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10337 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10338 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10339 if (!l_cstr_index->marker){
10340 opj_free( l_cstr_index);
10344 if (p_j2k->cstr_index->marker)
10345 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10347 opj_free(l_cstr_index->marker);
10348 l_cstr_index->marker = NULL;
10351 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10352 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10353 if (!l_cstr_index->tile_index){
10354 opj_free( l_cstr_index->marker);
10355 opj_free( l_cstr_index);
10359 if (!p_j2k->cstr_index->tile_index){
10360 opj_free(l_cstr_index->tile_index);
10361 l_cstr_index->tile_index = NULL;
10364 OPJ_UINT32 it_tile = 0;
10365 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10368 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10370 l_cstr_index->tile_index[it_tile].marker =
10371 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10373 if (!l_cstr_index->tile_index[it_tile].marker) {
10374 OPJ_UINT32 it_tile_free;
10376 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10377 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10380 opj_free( l_cstr_index->tile_index);
10381 opj_free( l_cstr_index->marker);
10382 opj_free( l_cstr_index);
10386 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10387 memcpy( l_cstr_index->tile_index[it_tile].marker,
10388 p_j2k->cstr_index->tile_index[it_tile].marker,
10389 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10391 opj_free(l_cstr_index->tile_index[it_tile].marker);
10392 l_cstr_index->tile_index[it_tile].marker = NULL;
10395 /* Tile part index*/
10396 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10398 l_cstr_index->tile_index[it_tile].tp_index =
10399 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10401 if(!l_cstr_index->tile_index[it_tile].tp_index){
10402 OPJ_UINT32 it_tile_free;
10404 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10405 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10406 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10409 opj_free( l_cstr_index->tile_index);
10410 opj_free( l_cstr_index->marker);
10411 opj_free( l_cstr_index);
10415 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10416 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10417 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10418 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10421 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10422 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10425 /* Packet index (NOT USED)*/
10426 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10427 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10432 return l_cstr_index;
10435 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10437 OPJ_UINT32 it_tile=0;
10439 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10440 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10441 if (!p_j2k->cstr_index->tile_index)
10444 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10445 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10446 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10447 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10448 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10449 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10459 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10460 opj_stream_private_t *p_stream,
10461 opj_event_mgr_t * p_manager)
10463 opj_bool l_go_on = OPJ_TRUE;
10464 OPJ_UINT32 l_current_tile_no;
10465 OPJ_UINT32 l_data_size,l_max_data_size;
10466 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10467 OPJ_UINT32 l_nb_comps;
10468 OPJ_BYTE * l_current_data;
10470 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10471 if (! l_current_data) {
10474 l_max_data_size = 1000;
10479 if (! j2k_read_tile_header( p_j2k,
10480 &l_current_tile_no,
10482 &l_tile_x0, &l_tile_y0,
10483 &l_tile_x1, &l_tile_y1,
10488 opj_free(l_current_data);
10496 if (l_data_size > l_max_data_size) {
10497 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10498 if (! l_current_data) {
10499 opj_free(l_current_data);
10503 l_max_data_size = l_data_size;
10506 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10507 opj_free(l_current_data);
10510 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);
10512 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10513 opj_free(l_current_data);
10516 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10520 opj_free(l_current_data);
10526 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10528 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10531 assert(p_j2k != 00);
10533 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
10534 /* DEVELOPER CORNER, add your custom procedures */
10539 * Read and decode one tile.
10541 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10542 opj_stream_private_t *p_stream,
10543 opj_event_mgr_t * p_manager)
10545 opj_bool l_go_on = OPJ_TRUE;
10546 OPJ_UINT32 l_current_tile_no;
10547 OPJ_UINT32 l_tile_no_to_dec;
10548 OPJ_UINT32 l_data_size,l_max_data_size;
10549 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10550 OPJ_UINT32 l_nb_comps;
10551 OPJ_BYTE * l_current_data;
10553 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10554 if (! l_current_data) {
10557 l_max_data_size = 1000;
10559 /*Allocate and initialize some elements of codestrem index if not already done*/
10560 if( !p_j2k->cstr_index->tile_index)
10562 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
10563 opj_free(l_current_data);
10567 /* Move into the codestream to the first SOT used to decode the desired tile */
10568 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10569 if (p_j2k->cstr_index->tile_index)
10570 if(p_j2k->cstr_index->tile_index->tp_index)
10572 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10573 /* the index for this tile has not been built,
10574 * so move to the last SOT read */
10575 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10576 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10581 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)) {
10582 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10586 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10587 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10588 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10592 if (! j2k_read_tile_header( p_j2k,
10593 &l_current_tile_no,
10595 &l_tile_x0, &l_tile_y0,
10596 &l_tile_x1, &l_tile_y1,
10601 opj_free(l_current_data);
10610 if (l_data_size > l_max_data_size) {
10611 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10612 if (! l_current_data) {
10613 opj_free(l_current_data);
10617 l_max_data_size = l_data_size;
10622 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10623 opj_free(l_current_data);
10626 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);
10628 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10629 opj_free(l_current_data);
10632 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10634 if(l_current_tile_no == l_tile_no_to_dec)
10636 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10637 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10638 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10644 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);
10649 opj_free(l_current_data);
10656 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10658 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10661 assert(p_j2k != 00);
10663 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
10664 /* DEVELOPER CORNER, add your custom procedures */
10670 * Decodes the tiles of the stream.
10672 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10673 opj_stream_private_t * p_stream,
10674 opj_image_t * p_image,
10675 opj_event_mgr_t * p_manager)
10682 p_j2k->m_output_image = opj_image_create0();
10683 if (! (p_j2k->m_output_image)) {
10686 opj_copy_image_header(p_image, p_j2k->m_output_image);
10688 /* customization of the decoding */
10689 opj_j2k_setup_decoding(p_j2k);
10691 /* Decode the codestream */
10692 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10693 opj_image_destroy(p_j2k->m_private_image);
10694 p_j2k->m_private_image = NULL;
10698 /* Move data and copy one information from codec to output image*/
10699 for (compno = 0; compno < p_image->numcomps; compno++) {
10700 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10701 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10702 p_j2k->m_output_image->comps[compno].data = NULL;
10710 * Get the decoded tile.
10712 * @param p_j2k the jpeg2000 codestream codec.
10713 * @param p_stream input_stream
10714 * @param p_image output image. .
10715 * @param p_manager the user event manager
10716 * @param tile_index index of the tile we want decode
10718 * @return true if succeed.
10720 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10721 opj_stream_private_t *p_stream,
10722 opj_image_t* p_image,
10723 opj_event_mgr_t * p_manager,
10724 OPJ_UINT32 tile_index )
10727 OPJ_UINT32 l_tile_x, l_tile_y;
10728 opj_image_comp_t* l_img_comp;
10731 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10735 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10736 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);
10740 /* Compute the dimension of the desired tile*/
10741 l_tile_x = tile_index % p_j2k->m_cp.tw;
10742 l_tile_y = tile_index / p_j2k->m_cp.tw;
10744 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10745 if (p_image->x0 < p_j2k->m_private_image->x0)
10746 p_image->x0 = p_j2k->m_private_image->x0;
10747 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10748 if (p_image->x1 > p_j2k->m_private_image->x1)
10749 p_image->x1 = p_j2k->m_private_image->x1;
10751 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10752 if (p_image->y0 < p_j2k->m_private_image->y0)
10753 p_image->y0 = p_j2k->m_private_image->y0;
10754 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10755 if (p_image->y1 > p_j2k->m_private_image->y1)
10756 p_image->y1 = p_j2k->m_private_image->y1;
10758 l_img_comp = p_image->comps;
10759 for (compno=0; compno < p_image->numcomps; ++compno)
10761 OPJ_INT32 l_comp_x1, l_comp_y1;
10763 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10765 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10766 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10767 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10768 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10770 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10771 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10776 /* Destroy the previous output image*/
10777 if (p_j2k->m_output_image)
10778 opj_image_destroy(p_j2k->m_output_image);
10780 /* Create the ouput image from the information previously computed*/
10781 p_j2k->m_output_image = opj_image_create0();
10782 if (! (p_j2k->m_output_image)) {
10785 opj_copy_image_header(p_image, p_j2k->m_output_image);
10787 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10789 /* customization of the decoding */
10790 opj_j2k_setup_decoding_tile(p_j2k);
10792 /* Decode the codestream */
10793 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10794 opj_image_destroy(p_j2k->m_private_image);
10795 p_j2k->m_private_image = NULL;
10799 /* Move data and copy one information from codec to output image*/
10800 for (compno = 0; compno < p_image->numcomps; compno++) {
10801 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10803 if (p_image->comps[compno].data)
10804 opj_free(p_image->comps[compno].data);
10806 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10808 p_j2k->m_output_image->comps[compno].data = NULL;
10814 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10816 OPJ_UINT32 it_comp;
10818 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10820 if (p_j2k->m_private_image) {
10821 if (p_j2k->m_private_image->comps) {
10822 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10823 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10824 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10825 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10826 if ( res_factor >= max_res){
10827 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10830 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10843 * Encodes all the tiles in a row.
10845 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10846 opj_stream_private_t *p_stream,
10847 opj_event_mgr_t * p_manager )
10850 OPJ_UINT32 l_nb_tiles;
10851 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10852 OPJ_BYTE * l_current_data;
10854 /* preconditions */
10855 assert(p_j2k != 00);
10856 assert(p_stream != 00);
10857 assert(p_manager != 00);
10859 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10860 if (! l_current_data) {
10863 l_max_tile_size = 1000;
10865 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10866 for (i=0;i<l_nb_tiles;++i) {
10867 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10868 opj_free(l_current_data);
10872 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10873 if (l_current_tile_size > l_max_tile_size) {
10874 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10875 if (! l_current_data) {
10878 l_max_tile_size = l_current_tile_size;
10881 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10883 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10888 opj_free(l_current_data);
10893 * Ends the compression procedures and possibility add data to be read after the
10896 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
10897 opj_stream_private_t *p_stream,
10898 struct opj_event_mgr * p_manager)
10900 /* customization of the encoding */
10901 j2k_setup_end_compress(p_j2k);
10903 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10913 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10915 * @param p_j2k the jpeg2000 codec.
10916 * @param p_stream the stream object.
10917 * @param p_manager the user event manager.
10919 * @return true if the codec is valid.
10921 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
10922 opj_stream_private_t *p_stream,
10923 opj_image_t * p_image,
10924 opj_event_mgr_t * p_manager)
10926 /* preconditions */
10927 assert(p_j2k != 00);
10928 assert(p_stream != 00);
10929 assert(p_manager != 00);
10931 p_j2k->m_private_image = opj_image_create0();
10932 opj_copy_image_header(p_image, p_j2k->m_private_image);
10934 // TODO_MSD: Find a better way
10935 if (p_image->comps) {
10936 OPJ_UINT32 it_comp;
10937 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10938 if (p_image->comps[it_comp].data) {
10939 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10940 p_image->comps[it_comp].data = NULL;
10946 /* customization of the validation */
10947 j2k_setup_encoding_validation (p_j2k);
10949 /* validation of the parameters codec */
10950 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10954 /* customization of the encoding */
10955 j2k_setup_header_writting(p_j2k);
10958 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10968 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10969 OPJ_UINT32 p_tile_index,
10970 opj_stream_private_t *p_stream,
10971 opj_event_mgr_t * p_manager )
10974 if (p_tile_index != p_j2k->m_current_tile_number) {
10975 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10979 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);
10981 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10982 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10983 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10985 /* initialisation before tile encoding */
10986 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10996 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10998 OPJ_UINT32 i,j,k = 0;
10999 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11000 opj_image_comp_t * l_img_comp = 00;
11001 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11002 opj_image_t * l_image = 00;
11003 OPJ_UINT32 l_size_comp, l_remaining;
11004 OPJ_INT32 * l_src_ptr;
11005 l_tilec = p_tcd->tcd_image->tiles->comps;
11006 l_image = p_tcd->image;
11007 l_img_comp = l_image->comps;
11009 for (i=0;i<p_tcd->image->numcomps;++i) {
11010 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11011 l_remaining = l_img_comp->prec & 7; /* (%8) */
11016 if (l_size_comp == 3) {
11020 l_width = (l_tilec->x1 - l_tilec->x0);
11021 l_height = (l_tilec->y1 - l_tilec->y0);
11022 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11023 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11024 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11025 l_stride = l_image_width - l_width;
11026 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11028 switch (l_size_comp) {
11031 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11032 if (l_img_comp->sgnd) {
11033 for (j=0;j<l_height;++j) {
11034 for (k=0;k<l_width;++k) {
11035 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11039 l_src_ptr += l_stride;
11043 for (j=0;j<l_height;++j) {
11044 for (k=0;k<l_width;++k) {
11045 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11049 l_src_ptr += l_stride;
11053 p_data = (OPJ_BYTE*) l_dest_ptr;
11058 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11059 if (l_img_comp->sgnd) {
11060 for (j=0;j<l_height;++j) {
11061 for (k=0;k<l_width;++k) {
11062 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11064 l_src_ptr += l_stride;
11068 for (j=0;j<l_height;++j) {
11069 for (k=0;k<l_width;++k) {
11070 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11072 l_src_ptr += l_stride;
11076 p_data = (OPJ_BYTE*) l_dest_ptr;
11081 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11082 for (j=0;j<l_height;++j) {
11083 for (k=0;k<l_width;++k) {
11084 *(l_dest_ptr++) = *(l_src_ptr++);
11086 l_src_ptr += l_stride;
11089 p_data = (OPJ_BYTE*) l_dest_ptr;
11102 * @param p_j2k the jpeg2000 codec.
11103 * @param p_stream the stream to write data to.
11104 * @param p_manager the user event manager.
11106 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11108 OPJ_UINT32 p_data_size,
11109 opj_stream_private_t *p_stream,
11110 opj_event_mgr_t * p_manager )
11112 opj_tcd_v2_t * l_tcd = 00;
11113 opj_cp_v2_t * l_cp = 00;
11114 opj_tcp_v2_t * l_tcp = 00;
11115 OPJ_UINT32 l_nb_bytes_written;
11116 OPJ_BYTE * l_current_data = 00;
11117 OPJ_UINT32 l_tile_size = 0;
11118 OPJ_UINT32 l_available_data;
11120 /* preconditions */
11121 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11123 l_tcd = p_j2k->m_tcd;
11124 l_cp = &(p_j2k->m_cp);
11125 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11127 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11128 l_available_data = l_tile_size;
11129 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11131 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11132 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11136 l_nb_bytes_written = 0;
11137 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11140 l_current_data += l_nb_bytes_written;
11141 l_available_data -= l_nb_bytes_written;
11143 l_nb_bytes_written = 0;
11144 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11148 l_available_data -= l_nb_bytes_written;
11149 l_nb_bytes_written = l_tile_size - l_available_data;
11151 if ( opj_stream_write_data( p_stream,
11152 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11153 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11157 ++p_j2k->m_current_tile_number;
11164 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11165 * are valid. Developers wanting to extend the library can add their own validation procedures.
11167 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11169 /* preconditions */
11170 assert(p_j2k != 00);
11172 /* DEVELOPER CORNER, insert your custom procedures */
11173 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11175 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11176 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11179 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11180 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11181 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11185 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11186 * are valid. Developers wanting to extend the library can add their own validation procedures.
11188 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11190 /* preconditions */
11191 assert(p_j2k != 00);
11193 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11194 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11196 /* DEVELOPER CORNER, add your custom validation procedure */
11197 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11202 * Sets up the procedures to do on writing header.
11203 * Developers wanting to extend the library can add their own writing procedures.
11205 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11207 /* preconditions */
11208 assert(p_j2k != 00);
11210 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11211 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11212 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11213 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11214 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11217 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11218 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11219 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11221 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11222 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11226 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11228 if (p_j2k->m_cp.comment != 00) {
11229 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11232 /* DEVELOPER CORNER, insert your custom procedures */
11233 if (p_j2k->m_cp.rsiz & MCT) {
11234 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11236 /* End of Developer Corner */
11238 if (p_j2k->cstr_index) {
11239 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11242 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11243 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11247 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11249 OPJ_UINT32 * p_data_written,
11250 OPJ_UINT32 p_total_data_size,
11251 opj_stream_private_t *p_stream,
11252 struct opj_event_mgr * p_manager )
11255 OPJ_UINT32 l_nb_bytes_written = 0;
11256 OPJ_UINT32 l_current_nb_bytes_written;
11257 OPJ_BYTE * l_begin_data = 00;
11259 opj_tcp_v2_t *l_tcp = 00;
11260 opj_tcd_v2_t * l_tcd = 00;
11261 opj_cp_v2_t * l_cp = 00;
11263 l_tcd = p_j2k->m_tcd;
11264 l_cp = &(p_j2k->m_cp);
11265 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11267 l_tcd->cur_pino = 0;
11269 /*Get number of tile parts*/
11270 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11275 l_current_nb_bytes_written = 0;
11276 l_begin_data = p_data;
11277 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11282 l_nb_bytes_written += l_current_nb_bytes_written;
11283 p_data += l_current_nb_bytes_written;
11284 p_total_data_size -= l_current_nb_bytes_written;
11286 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11287 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11288 l_current_nb_bytes_written = 0;
11289 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11290 l_nb_bytes_written += l_current_nb_bytes_written;
11291 p_data += l_current_nb_bytes_written;
11292 p_total_data_size -= l_current_nb_bytes_written;
11294 l_current_nb_bytes_written = 0;
11295 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11296 l_nb_bytes_written += l_current_nb_bytes_written;
11297 p_data += l_current_nb_bytes_written;
11298 p_total_data_size -= l_current_nb_bytes_written;
11301 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11302 l_current_nb_bytes_written = 0;
11303 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11304 l_nb_bytes_written += l_current_nb_bytes_written;
11305 p_data += l_current_nb_bytes_written;
11306 p_total_data_size -= l_current_nb_bytes_written;
11310 l_current_nb_bytes_written = 0;
11311 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11315 l_nb_bytes_written += l_current_nb_bytes_written;
11316 * p_data_written = l_nb_bytes_written;
11318 /* Writing Psot in SOT marker */
11319 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11321 if (l_cp->m_specific_param.m_enc.m_cinema){
11322 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11328 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11330 OPJ_UINT32 * p_data_written,
11331 OPJ_UINT32 p_total_data_size,
11332 opj_stream_private_t *p_stream,
11333 struct opj_event_mgr * p_manager
11336 OPJ_UINT32 tilepartno=0;
11337 OPJ_UINT32 l_nb_bytes_written = 0;
11338 OPJ_UINT32 l_current_nb_bytes_written;
11339 OPJ_UINT32 l_part_tile_size;
11340 OPJ_UINT32 tot_num_tp;
11343 OPJ_BYTE * l_begin_data;
11344 opj_tcp_v2_t *l_tcp = 00;
11345 opj_tcd_v2_t * l_tcd = 00;
11346 opj_cp_v2_t * l_cp = 00;
11349 l_tcd = p_j2k->m_tcd;
11350 l_cp = &(p_j2k->m_cp);
11351 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11353 /*Get number of tile parts*/
11354 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11356 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11357 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11358 l_current_nb_bytes_written = 0;
11359 l_part_tile_size = 0;
11360 l_begin_data = p_data;
11362 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11366 l_nb_bytes_written += l_current_nb_bytes_written;
11367 p_data += l_current_nb_bytes_written;
11368 p_total_data_size -= l_current_nb_bytes_written;
11369 l_part_tile_size += l_nb_bytes_written;
11371 l_current_nb_bytes_written = 0;
11372 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11376 p_data += l_current_nb_bytes_written;
11377 l_nb_bytes_written += l_current_nb_bytes_written;
11378 p_total_data_size -= l_current_nb_bytes_written;
11379 l_part_tile_size += l_nb_bytes_written;
11381 /* Writing Psot in SOT marker */
11382 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11384 if (l_cp->m_specific_param.m_enc.m_cinema) {
11385 j2k_update_tlm(p_j2k,l_part_tile_size);
11388 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11391 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11392 l_tcd->cur_pino = pino;
11394 /*Get number of tile parts*/
11395 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11396 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11397 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11398 l_current_nb_bytes_written = 0;
11399 l_part_tile_size = 0;
11400 l_begin_data = p_data;
11402 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11406 l_nb_bytes_written += l_current_nb_bytes_written;
11407 p_data += l_current_nb_bytes_written;
11408 p_total_data_size -= l_current_nb_bytes_written;
11409 l_part_tile_size += l_current_nb_bytes_written;
11411 l_current_nb_bytes_written = 0;
11413 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11417 l_nb_bytes_written += l_current_nb_bytes_written;
11418 p_data += l_current_nb_bytes_written;
11419 p_total_data_size -= l_current_nb_bytes_written;
11420 l_part_tile_size += l_current_nb_bytes_written;
11422 /* Writing Psot in SOT marker */
11423 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11425 if (l_cp->m_specific_param.m_enc.m_cinema) {
11426 j2k_update_tlm(p_j2k,l_part_tile_size);
11429 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11433 *p_data_written = l_nb_bytes_written;
11439 * Writes the updated tlm.
11441 * @param p_stream the stream to write data to.
11442 * @param p_j2k J2K codec.
11443 * @param p_manager the user event manager.
11445 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11446 struct opj_stream_private *p_stream,
11447 struct opj_event_mgr * p_manager )
11449 OPJ_UINT32 l_tlm_size;
11450 OPJ_SIZE_T l_tlm_position, l_current_position;
11452 /* preconditions */
11453 assert(p_j2k != 00);
11454 assert(p_manager != 00);
11455 assert(p_stream != 00);
11457 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11458 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11459 l_current_position = opj_stream_tell(p_stream);
11461 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11465 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) {
11469 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11478 * Ends the encoding, i.e. frees memory.
11480 * @param p_stream the stream to write data to.
11481 * @param p_j2k J2K codec.
11482 * @param p_manager the user event manager.
11484 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11485 struct opj_stream_private *p_stream,
11486 struct opj_event_mgr * p_manager )
11488 /* preconditions */
11489 assert(p_j2k != 00);
11490 assert(p_manager != 00);
11491 assert(p_stream != 00);
11493 tcd_destroy_v2(p_j2k->m_tcd);
11496 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11497 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11498 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11499 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11502 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11503 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11504 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11507 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11513 * Destroys the memory associated with the decoding of headers.
11515 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11516 opj_stream_private_t *p_stream,
11517 opj_event_mgr_t * p_manager )
11519 /* preconditions */
11520 assert(p_j2k != 00);
11521 assert(p_stream != 00);
11522 assert(p_manager != 00);
11524 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11525 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11526 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11529 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11538 * @param p_stream the stream to write data to.
11539 * @param p_j2k J2K codec.
11540 * @param p_manager the user event manager.
11542 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11543 struct opj_stream_private *p_stream,
11544 struct opj_event_mgr * p_manager )
11546 opj_codestream_info_t * l_cstr_info = 00;
11548 /* preconditions */
11549 assert(p_j2k != 00);
11550 assert(p_manager != 00);
11551 assert(p_stream != 00);
11554 /* TODO mergeV2: check this part which use cstr_info */
11555 /*l_cstr_info = p_j2k->cstr_info;
11559 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));
11561 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11562 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11564 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11566 l_cstr_info->tw = p_j2k->m_cp.tw;
11567 l_cstr_info->th = p_j2k->m_cp.th;
11569 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11570 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11571 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11572 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11574 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11576 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11578 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11580 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11581 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11584 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11586 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11588 /*l_cstr_info->maxmarknum = 100;
11589 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11590 l_cstr_info->marknum = 0;
11593 return j2k_calculate_tp_v2(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
11597 * Creates a tile-coder decoder.
11599 * @param p_stream the stream to write data to.
11600 * @param p_j2k J2K codec.
11601 * @param p_manager the user event manager.
11603 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11604 struct opj_stream_private *p_stream,
11605 struct opj_event_mgr * p_manager )
11607 /* preconditions */
11608 assert(p_j2k != 00);
11609 assert(p_manager != 00);
11610 assert(p_stream != 00);
11612 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11614 if (! p_j2k->m_tcd) {
11615 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11619 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11620 tcd_destroy_v2(p_j2k->m_tcd);
11631 * @param p_j2k the jpeg2000 codec.
11632 * @param p_stream the stream to write data to.
11633 * @param p_manager the user event manager.
11635 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11636 OPJ_UINT32 p_tile_index,
11638 OPJ_UINT32 p_data_size,
11639 opj_stream_private_t *p_stream,
11640 opj_event_mgr_t * p_manager )
11642 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11643 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11647 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11648 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);