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 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool 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 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 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 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 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 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 * 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 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 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 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 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 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 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 j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool 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 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 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 Read an unknown marker
998 @param j2k J2K handle
1000 static void j2k_read_unk(opj_j2k_t *j2k);
1002 Add main header marker information
1003 @param cstr_info Codestream information structure
1004 @param type marker type
1005 @param pos byte offset of marker segment
1006 @param len length of marker segment
1008 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1010 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1012 Add tile header marker information
1013 @param tileno tile index number
1014 @param cstr_info Codestream information structure
1015 @param type marker type
1016 @param pos byte offset of marker segment
1017 @param len length of marker segment
1019 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1021 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);
1024 * Reads an unknown marker
1026 * @param p_stream the stream object to read from.
1027 * @param p_j2k the jpeg2000 codec.
1028 * @param p_manager the user event manager.
1030 * @return true if the marker could be deduced.
1032 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1033 struct opj_stream_private *p_stream,
1034 OPJ_UINT32 *output_marker,
1035 struct opj_event_mgr * p_manager );
1038 * Writes the MCT marker (Multiple Component Transform)
1040 * @param p_stream the stream to write data to.
1041 * @param p_j2k J2K codec.
1042 * @param p_manager the user event manager.
1044 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1045 opj_mct_data_t * p_mct_record,
1046 struct opj_stream_private *p_stream,
1047 struct opj_event_mgr * p_manager );
1050 * Reads a MCT marker (Multiple Component Transform)
1052 * @param p_header_data the data contained in the MCT box.
1053 * @param p_j2k the jpeg2000 codec.
1054 * @param p_header_size the size of the data contained in the MCT marker.
1055 * @param p_manager the user event manager.
1057 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1058 OPJ_BYTE * p_header_data,
1059 OPJ_UINT32 p_header_size,
1060 struct opj_event_mgr * p_manager );
1063 * Writes the MCC marker (Multiple Component Collection)
1065 * @param p_stream the stream to write data to.
1066 * @param p_j2k J2K codec.
1067 * @param p_manager the user event manager.
1069 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1070 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1071 struct opj_stream_private *p_stream,
1072 struct opj_event_mgr * p_manager );
1076 * Reads a MCC marker (Multiple Component Collection)
1078 * @param p_header_data the data contained in the MCC box.
1079 * @param p_j2k the jpeg2000 codec.
1080 * @param p_header_size the size of the data contained in the MCC marker.
1081 * @param p_manager the user event manager.
1083 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1084 OPJ_BYTE * p_header_data,
1085 OPJ_UINT32 p_header_size,
1086 struct opj_event_mgr * p_manager );
1089 * Writes the MCO marker (Multiple component transformation ordering)
1091 * @param p_stream the stream to write data to.
1092 * @param p_j2k J2K codec.
1093 * @param p_manager the user event manager.
1095 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1096 struct opj_stream_private *p_stream,
1097 struct opj_event_mgr * p_manager );
1100 * Reads a MCO marker (Multiple Component Transform Ordering)
1102 * @param p_header_data the data contained in the MCO box.
1103 * @param p_j2k the jpeg2000 codec.
1104 * @param p_header_size the size of the data contained in the MCO marker.
1105 * @param p_manager the user event manager.
1107 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1108 OPJ_BYTE * p_header_data,
1109 OPJ_UINT32 p_header_size,
1110 struct opj_event_mgr * p_manager );
1112 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1114 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1115 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1116 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1117 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1119 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1120 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1121 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1124 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1125 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1127 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1131 * Ends the encoding, i.e. frees memory.
1133 * @param p_stream the stream to write data to.
1134 * @param p_j2k J2K codec.
1135 * @param p_manager the user event manager.
1137 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1138 struct opj_stream_private *p_stream,
1139 struct opj_event_mgr * p_manager );
1142 * Writes the CBD marker (Component bit depth definition)
1144 * @param p_stream the stream to write data to.
1145 * @param p_j2k J2K codec.
1146 * @param p_manager the user event manager.
1148 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1149 struct opj_stream_private *p_stream,
1150 struct opj_event_mgr * p_manager );
1153 * Reads a CBD marker (Component bit depth definition)
1154 * @param p_header_data the data contained in the CBD box.
1155 * @param p_j2k the jpeg2000 codec.
1156 * @param p_header_size the size of the data contained in the CBD marker.
1157 * @param p_manager the user event manager.
1159 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1160 OPJ_BYTE * p_header_data,
1161 OPJ_UINT32 p_header_size,
1162 struct opj_event_mgr * p_manager);
1165 * Writes the image components.
1167 * @param p_stream the stream to write data to.
1168 * @param p_j2k J2K codec.
1169 * @param p_manager the user event manager.
1171 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1172 struct opj_stream_private *p_stream,
1173 struct opj_event_mgr * p_manager );
1176 * Writes regions of interests.
1178 * @param p_stream the stream to write data to.
1179 * @param p_j2k J2K codec.
1180 * @param p_manager the user event manager.
1182 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1183 struct opj_stream_private *p_stream,
1184 struct opj_event_mgr * p_manager );
1189 * @param p_stream the stream to write data to.
1190 * @param p_j2k J2K codec.
1191 * @param p_manager the user event manager.
1193 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1194 struct opj_stream_private *p_stream,
1195 struct opj_event_mgr * p_manager );
1198 * Checks the progression order changes values. Tells of the poc given as input are valid.
1199 * A nice message is outputted at errors.
1201 * @param p_pocs the progression order changes.
1202 * @param p_nb_pocs the number of progression order changes.
1203 * @param p_nb_resolutions the number of resolutions.
1204 * @param numcomps the number of components
1205 * @param numlayers the number of layers.
1207 * @return true if the pocs are valid.
1209 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1210 OPJ_UINT32 p_nb_pocs,
1211 OPJ_UINT32 p_nb_resolutions,
1212 OPJ_UINT32 numcomps,
1213 OPJ_UINT32 numlayers,
1214 opj_event_mgr_t * p_manager);
1217 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1219 * @param cp the coding parameters.
1220 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1221 * @param tileno the given tile.
1223 * @return the number of tile parts.
1225 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1227 /** mem allocation for TLM marker*/
1228 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1231 * Calculates the total number of tile parts needed by the encoder to
1232 * encode such an image. If not enough memory is available, then the function return false.
1234 * @param p_nb_tiles pointer that will hold the number of tile parts.
1235 * @param cp the coding parameters for the image.
1236 * @param image the image to encode.
1237 * @param p_j2k the p_j2k encoder.
1238 * @param p_manager the user event manager.
1240 * @return true if the function was successful, false else.
1242 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1244 OPJ_UINT32 * p_nb_tiles,
1246 opj_event_mgr_t * p_manager);
1248 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1250 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1252 static opj_codestream_index_t* j2k_create_cstr_index(void);
1258 /* ----------------------------------------------------------------------- */
1259 typedef struct j2k_prog_order{
1260 OPJ_PROG_ORDER enum_prog;
1264 j2k_prog_order_t j2k_prog_order_list[] = {
1270 {(OPJ_PROG_ORDER)-1, ""}
1278 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1286 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1288 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1290 j2k_read_int16_to_float,
1291 j2k_read_int32_to_float,
1292 j2k_read_float32_to_float,
1293 j2k_read_float64_to_float
1296 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1298 j2k_read_int16_to_int32,
1299 j2k_read_int32_to_int32,
1300 j2k_read_float32_to_int32,
1301 j2k_read_float64_to_int32
1304 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1306 j2k_write_float_to_int16,
1307 j2k_write_float_to_int32,
1308 j2k_write_float_to_float,
1309 j2k_write_float_to_float64
1312 typedef struct opj_dec_memory_marker_handler
1316 /** value of the state when the marker can appear */
1318 /** action linked to the marker */
1319 opj_bool (*handler) (
1320 opj_j2k_v2_t *p_j2k,
1321 OPJ_BYTE * p_header_data,
1322 OPJ_UINT32 p_header_size,
1323 struct opj_event_mgr * p_manager
1326 opj_dec_memory_marker_handler_t;
1328 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1331 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1332 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1333 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1334 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1335 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1336 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1337 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1338 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1339 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1340 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1341 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1342 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1343 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1345 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1346 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1347 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1348 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1349 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1350 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1352 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1353 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1354 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1355 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1356 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1357 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1358 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1359 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1360 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1361 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1362 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1363 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1364 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1366 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1367 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1368 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1369 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1370 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1371 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1373 #ifdef TODO_MS /* FIXME */
1374 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1375 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1376 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1377 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1379 #endif /* USE_JPWL */
1381 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1382 {J2K_MS_INSEC, 0, j2k_read_insec}
1383 #endif /* USE_JPSEC */
1384 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1389 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1391 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1392 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1396 for (i=0;i<p_nb_elem;++i) {
1397 opj_read_bytes(l_src_data,&l_temp,2);
1399 l_src_data+=sizeof(OPJ_INT16);
1401 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1405 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1407 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1408 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1412 for (i=0;i<p_nb_elem;++i) {
1413 opj_read_bytes(l_src_data,&l_temp,4);
1415 l_src_data+=sizeof(OPJ_INT32);
1417 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1421 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1423 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1424 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1428 for (i=0;i<p_nb_elem;++i) {
1429 opj_read_float(l_src_data,&l_temp);
1431 l_src_data+=sizeof(OPJ_FLOAT32);
1433 *(l_dest_data++) = l_temp;
1437 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1439 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1440 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1444 for (i=0;i<p_nb_elem;++i) {
1445 opj_read_double(l_src_data,&l_temp);
1447 l_src_data+=sizeof(OPJ_FLOAT64);
1449 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1453 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1455 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1456 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1460 for (i=0;i<p_nb_elem;++i) {
1461 opj_read_bytes(l_src_data,&l_temp,2);
1463 l_src_data+=sizeof(OPJ_INT16);
1465 *(l_dest_data++) = (OPJ_INT32) l_temp;
1469 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1471 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1472 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1476 for (i=0;i<p_nb_elem;++i) {
1477 opj_read_bytes(l_src_data,&l_temp,4);
1479 l_src_data+=sizeof(OPJ_INT32);
1481 *(l_dest_data++) = (OPJ_INT32) l_temp;
1485 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1487 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1488 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1492 for (i=0;i<p_nb_elem;++i) {
1493 opj_read_float(l_src_data,&l_temp);
1495 l_src_data+=sizeof(OPJ_FLOAT32);
1497 *(l_dest_data++) = (OPJ_INT32) l_temp;
1501 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1503 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1504 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1508 for (i=0;i<p_nb_elem;++i) {
1509 opj_read_double(l_src_data,&l_temp);
1511 l_src_data+=sizeof(OPJ_FLOAT64);
1513 *(l_dest_data++) = (OPJ_INT32) l_temp;
1517 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1519 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1520 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1524 for (i=0;i<p_nb_elem;++i) {
1525 l_temp = (OPJ_UINT32) *(l_src_data++);
1527 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1529 l_dest_data+=sizeof(OPJ_INT16);
1533 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1535 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1536 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1540 for (i=0;i<p_nb_elem;++i) {
1541 l_temp = (OPJ_UINT32) *(l_src_data++);
1543 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1545 l_dest_data+=sizeof(OPJ_INT32);
1549 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1551 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1552 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1556 for (i=0;i<p_nb_elem;++i) {
1557 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1559 opj_write_float(l_dest_data,l_temp);
1561 l_dest_data+=sizeof(OPJ_FLOAT32);
1565 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1567 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1568 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1572 for (i=0;i<p_nb_elem;++i) {
1573 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1575 opj_write_double(l_dest_data,l_temp);
1577 l_dest_data+=sizeof(OPJ_FLOAT64);
1583 * Converts an enum type progression order to string type.
1585 * @param prg_order the progression order to get.
1587 * @return the string representation of the given progression order.
1589 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1590 j2k_prog_order_t *po;
1591 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1592 if(po->enum_prog == prg_order){
1593 return po->str_prog;
1596 return po->str_prog;
1600 * Checks the progression order changes values. Tells if the poc given as input are valid.
1602 * @param p_pocs the progression order changes.
1603 * @param p_nb_pocs the number of progression order changes.
1604 * @param p_nb_resolutions the number of resolutions.
1605 * @param numcomps the number of components
1606 * @param numlayers the number of layers.
1607 * @param p_manager the user event manager.
1609 * @return true if the pocs are valid.
1611 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1612 OPJ_UINT32 p_nb_pocs,
1613 OPJ_UINT32 p_nb_resolutions,
1614 OPJ_UINT32 p_num_comps,
1615 OPJ_UINT32 p_num_layers,
1616 opj_event_mgr_t * p_manager)
1618 OPJ_UINT32* packet_array;
1619 OPJ_UINT32 index , resno, compno, layno;
1621 OPJ_UINT32 step_c = 1;
1622 OPJ_UINT32 step_r = p_num_comps * step_c;
1623 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1624 opj_bool loss = OPJ_FALSE;
1625 OPJ_UINT32 layno0 = 0;
1627 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1628 if (packet_array == 00) {
1629 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1632 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1634 if (p_nb_pocs == 0) {
1638 index = step_r * p_pocs->resno0;
1639 // take each resolution for each poc
1640 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1642 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1644 // take each comp of each resolution for each poc
1645 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1646 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1648 // and finally take each layer of each res of ...
1649 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1650 //index = step_r * resno + step_c * compno + step_l * layno;
1651 packet_array[comp_index] = 1;
1652 comp_index += step_l;
1655 res_index += step_c;
1662 // iterate through all the pocs
1663 for (i = 1; i < p_nb_pocs ; ++i) {
1664 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1666 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1667 index = step_r * p_pocs->resno0;
1669 // take each resolution for each poc
1670 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1671 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1673 // take each comp of each resolution for each poc
1674 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1675 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1677 // and finally take each layer of each res of ...
1678 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1679 //index = step_r * resno + step_c * compno + step_l * layno;
1680 packet_array[comp_index] = 1;
1681 comp_index += step_l;
1684 res_index += step_c;
1694 for (layno = 0; layno < p_num_layers ; ++layno) {
1695 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1696 for (compno = 0; compno < p_num_comps; ++compno) {
1697 loss |= (packet_array[index]!=1);
1698 //index = step_r * resno + step_c * compno + step_l * layno;
1705 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1708 opj_free(packet_array);
1713 /* ----------------------------------------------------------------------- */
1714 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1717 int tpnum=1,tpend=0;
1718 opj_tcp_t *tcp = &cp->tcps[tileno];
1719 prog = j2k_convert_progression_order(tcp->prg);
1724 if( cp->tp_flag == prog[i] ){
1725 tpend=1;cp->tp_pos=i;
1729 tpnum= tpnum * tcp->pocs[pino].compE;
1732 tpnum= tpnum * tcp->pocs[pino].resE;
1735 tpnum= tpnum * tcp->pocs[pino].prcE;
1738 tpnum= tpnum * tcp->pocs[pino].layE;
1750 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1752 * @param cp the coding parameters.
1753 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1754 * @param tileno the given tile.
1756 * @return the number of tile parts.
1758 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1760 const OPJ_CHAR *prog = 00;
1762 OPJ_UINT32 tpnum = 1;
1763 opj_tcp_v2_t *tcp = 00;
1764 opj_poc_t * l_current_poc = 00;
1767 assert(tileno < (cp->tw * cp->th));
1768 assert(pino < (cp->tcps[tileno].numpocs + 1));
1770 /* get the given tile coding parameter */
1771 tcp = &cp->tcps[tileno];
1774 l_current_poc = &(tcp->pocs[pino]);
1775 assert(l_current_poc != 0);
1777 /* get the progression order as a character string */
1778 prog = j2k_convert_progression_order(tcp->prg);
1779 assert(strlen(prog) > 0);
1781 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1785 /* component wise */
1787 tpnum *= l_current_poc->compE;
1789 /* resolution wise */
1791 tpnum *= l_current_poc->resE;
1795 tpnum *= l_current_poc->prcE;
1799 tpnum *= l_current_poc->layE;
1802 /* whould we split here ? */
1803 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1804 cp->m_specific_param.m_enc.m_tp_pos=i;
1816 /** mem allocation for TLM marker*/
1817 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1818 int pino,tileno,totnum_tp=0;
1820 OPJ_ARG_NOT_USED(img_numcomp);
1822 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1823 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1824 int cur_totnum_tp = 0;
1825 opj_tcp_t *tcp = &cp->tcps[tileno];
1826 for(pino = 0; pino <= tcp->numpocs; pino++) {
1828 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1829 if(!pi) { return -1;}
1830 tp_num = j2k_get_num_tp(cp,pino,tileno);
1831 totnum_tp = totnum_tp + tp_num;
1832 cur_totnum_tp = cur_totnum_tp + tp_num;
1833 pi_destroy(pi, cp, tileno);
1835 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1837 if (j2k->cstr_info) {
1838 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1839 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1847 * Calculates the total number of tile parts needed by the encoder to
1848 * encode such an image. If not enough memory is available, then the function return false.
1850 * @param p_nb_tiles pointer that will hold the number of tile parts.
1851 * @param cp the coding parameters for the image.
1852 * @param image the image to encode.
1853 * @param p_j2k the p_j2k encoder.
1854 * @param p_manager the user event manager.
1856 * @return true if the function was successful, false else.
1858 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1860 OPJ_UINT32 * p_nb_tiles,
1862 opj_event_mgr_t * p_manager)
1864 OPJ_UINT32 pino,tileno;
1865 OPJ_UINT32 l_nb_tiles;
1869 assert(p_nb_tiles != 00);
1871 assert(image != 00);
1872 assert(p_j2k != 00);
1873 assert(p_manager != 00);
1875 l_nb_tiles = cp->tw * cp->th;
1880 /* TODO mergeV2: check this part which use cstr_info */
1881 /*if (p_j2k->cstr_info) {
1882 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1884 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1885 OPJ_UINT32 cur_totnum_tp = 0;
1887 pi_update_encoding_parameters(image,cp,tileno);
1889 for (pino = 0; pino <= tcp->numpocs; ++pino)
1891 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1893 *p_nb_tiles = *p_nb_tiles + tp_num;
1895 cur_totnum_tp += tp_num;
1898 tcp->m_nb_tile_parts = cur_totnum_tp;
1900 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1901 if (l_info_tile_ptr->tp == 00) {
1905 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1907 l_info_tile_ptr->num_tps = cur_totnum_tp;
1914 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1915 OPJ_UINT32 cur_totnum_tp = 0;
1917 pi_update_encoding_parameters(image,cp,tileno);
1919 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1920 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1922 *p_nb_tiles = *p_nb_tiles + tp_num;
1924 cur_totnum_tp += tp_num;
1926 tcp->m_nb_tile_parts = cur_totnum_tp;
1935 static void j2k_write_soc(opj_j2k_t *j2k) {
1936 opj_cio_t *cio = j2k->cio;
1937 cio_write(cio, J2K_MS_SOC, 2);
1940 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1945 /* update markers struct */
1946 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1947 #endif /* USE_JPWL */
1952 * Writes the SOC marker (Start Of Codestream)
1954 * @param p_stream the stream to write data to.
1955 * @param p_j2k J2K codec.
1956 * @param p_manager the user event manager.
1958 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1959 struct opj_stream_private *p_stream,
1960 struct opj_event_mgr * p_manager )
1962 /* 2 bytes will be written */
1963 OPJ_BYTE * l_start_stream = 00;
1966 assert(p_stream != 00);
1967 assert(p_j2k != 00);
1968 assert(p_manager != 00);
1970 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1972 /* write SOC identifier */
1973 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1975 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1981 /* update markers struct */
1983 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1985 assert( 0 && "TODO" );
1986 #endif /* USE_JPWL */
1995 * Reads a SOC marker (Start of Codestream)
1996 * @param p_header_data the data contained in the SOC box.
1997 * @param jp2 the jpeg2000 file codec.
1998 * @param p_header_size the size of the data contained in the SOC marker.
1999 * @param p_manager the user event manager.
2001 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2002 struct opj_stream_private *p_stream,
2003 struct opj_event_mgr * p_manager )
2005 OPJ_BYTE l_data [2];
2006 OPJ_UINT32 l_marker;
2009 assert(p_j2k != 00);
2010 assert(p_manager != 00);
2011 assert(p_stream != 00);
2013 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2017 opj_read_bytes(l_data,&l_marker,2);
2018 if (l_marker != J2K_MS_SOC) {
2022 /* Next marker should be a SIZ marker in the main header */
2023 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2025 /* FIXME move it in a index structure included in p_j2k*/
2026 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2028 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2030 /* Add the marker to the codestream index*/
2031 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2036 static void j2k_write_siz(opj_j2k_t *j2k) {
2040 opj_cio_t *cio = j2k->cio;
2041 opj_image_t *image = j2k->image;
2042 opj_cp_t *cp = j2k->cp;
2044 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2045 lenp = cio_tell(cio);
2047 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2048 cio_write(cio, image->x1, 4); /* Xsiz */
2049 cio_write(cio, image->y1, 4); /* Ysiz */
2050 cio_write(cio, image->x0, 4); /* X0siz */
2051 cio_write(cio, image->y0, 4); /* Y0siz */
2052 cio_write(cio, cp->tdx, 4); /* XTsiz */
2053 cio_write(cio, cp->tdy, 4); /* YTsiz */
2054 cio_write(cio, cp->tx0, 4); /* XT0siz */
2055 cio_write(cio, cp->ty0, 4); /* YT0siz */
2056 cio_write(cio, image->numcomps, 2); /* Csiz */
2057 for (i = 0; i < image->numcomps; i++) {
2058 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2059 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2060 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2062 len = cio_tell(cio) - lenp;
2063 cio_seek(cio, lenp);
2064 cio_write(cio, len, 2); /* Lsiz */
2065 cio_seek(cio, lenp + len);
2068 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2072 * Writes the SIZ marker (image and tile size)
2074 * @param p_stream the stream to write data to.
2075 * @param p_j2k J2K codec.
2076 * @param p_manager the user event manager.
2078 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2079 struct opj_stream_private *p_stream,
2080 struct opj_event_mgr * p_manager )
2083 OPJ_UINT32 l_size_len;
2084 OPJ_BYTE * l_current_ptr;
2085 opj_image_t * l_image = 00;
2086 opj_cp_v2_t *cp = 00;
2087 opj_image_comp_t * l_img_comp = 00;
2090 assert(p_stream != 00);
2091 assert(p_j2k != 00);
2092 assert(p_manager != 00);
2094 l_image = p_j2k->m_private_image;
2095 cp = &(p_j2k->m_cp);
2096 l_size_len = 40 + 3 * l_image->numcomps;
2097 l_img_comp = l_image->comps;
2099 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2101 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2102 = (OPJ_BYTE*)opj_realloc(
2103 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2105 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2109 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2112 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2114 /* write SOC identifier */
2115 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2118 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2121 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2124 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2127 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2130 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2133 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2136 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2139 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2142 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2145 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2148 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2151 for (i = 0; i < l_image->numcomps; ++i) {
2152 /* TODO here with MCT ? */
2153 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2156 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2159 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2165 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) {
2175 * Reads a SIZ marker (image and tile size)
2176 * @param p_header_data the data contained in the SIZ box.
2177 * @param jp2 the jpeg2000 file codec.
2178 * @param p_header_size the size of the data contained in the SIZ marker.
2179 * @param p_manager the user event manager.
2181 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2182 OPJ_BYTE * p_header_data,
2183 OPJ_UINT32 p_header_size,
2184 opj_event_mgr_t * p_manager
2187 OPJ_UINT32 l_size, i;
2188 OPJ_UINT32 l_nb_comp;
2189 OPJ_UINT32 l_nb_comp_remain;
2190 OPJ_UINT32 l_remaining_size;
2191 OPJ_UINT32 l_nb_tiles;
2193 opj_image_t *l_image = 00;
2194 opj_cp_v2_t *l_cp = 00;
2195 opj_image_comp_t * l_img_comp = 00;
2196 opj_tcp_v2_t * l_current_tile_param = 00;
2199 assert(p_j2k != 00);
2200 assert(p_manager != 00);
2201 assert(p_header_data != 00);
2203 l_image = p_j2k->m_private_image;
2204 l_cp = &(p_j2k->m_cp);
2206 /* minimum size == 39 - 3 (= minimum component parameter) */
2207 if (p_header_size < 36) {
2208 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2212 l_remaining_size = p_header_size - 36;
2213 l_nb_comp = l_remaining_size / 3;
2214 l_nb_comp_remain = l_remaining_size % 3;
2215 if (l_nb_comp_remain != 0){
2216 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2220 l_size = p_header_size + 2; /* Lsiz */
2222 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2224 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2225 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2227 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2229 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2231 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2233 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2235 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2237 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2239 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2241 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2244 l_image->numcomps = (OPJ_UINT16) l_tmp;
2246 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2250 if (l_image->numcomps != l_nb_comp) {
2251 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);
2256 if (l_cp->correct) {
2257 /* if JPWL is on, we check whether TX errors have damaged
2258 too much the SIZ parameters */
2259 if (!(l_image->x1 * l_image->y1)) {
2260 opj_event_msg_v2(p_manager, EVT_ERROR,
2261 "JPWL: bad image size (%d x %d)\n",
2262 l_image->x1, l_image->y1);
2263 if (!JPWL_ASSUME || JPWL_ASSUME) {
2264 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2269 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2270 if (l_image->numcomps != ((len - 38) / 3)) {
2271 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2272 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2273 l_image->numcomps, ((len - 38) / 3));
2275 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2278 */ /* we try to correct */
2279 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2280 if (l_image->numcomps < ((len - 38) / 3)) {
2281 len = 38 + 3 * l_image->numcomps;
2282 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2285 l_image->numcomps = ((len - 38) / 3);
2286 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2292 /* update components number in the jpwl_exp_comps filed */
2293 l_cp->exp_comps = l_image->numcomps;
2295 #endif /* USE_JPWL */
2297 /* Allocate the resulting image components */
2298 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2299 if (l_image->comps == 00){
2300 l_image->numcomps = 0;
2301 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2305 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2306 l_img_comp = l_image->comps;
2308 /* Read the component information */
2309 for (i = 0; i < l_image->numcomps; ++i){
2311 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2313 l_img_comp->prec = (tmp & 0x7f) + 1;
2314 l_img_comp->sgnd = tmp >> 7;
2315 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2317 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2318 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2320 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2323 if (l_cp->correct) {
2324 /* if JPWL is on, we check whether TX errors have damaged
2325 too much the SIZ parameters, again */
2326 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2327 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2328 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2329 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2331 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2334 /* we try to correct */
2335 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2336 if (!l_image->comps[i].dx) {
2337 l_image->comps[i].dx = 1;
2338 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2339 i, l_image->comps[i].dx);
2341 if (!l_image->comps[i].dy) {
2342 l_image->comps[i].dy = 1;
2343 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2344 i, l_image->comps[i].dy);
2348 #endif /* USE_JPWL */
2349 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2350 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2354 /* Compute the number of tiles */
2355 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2356 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2357 l_nb_tiles = l_cp->tw * l_cp->th;
2359 /* Define the tiles which will be decoded */
2360 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2361 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;
2362 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;
2363 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);
2364 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);
2367 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2368 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2369 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2370 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2374 if (l_cp->correct) {
2375 /* if JPWL is on, we check whether TX errors have damaged
2376 too much the SIZ parameters */
2377 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2378 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2379 "JPWL: bad number of tiles (%d x %d)\n",
2380 l_cp->tw, l_cp->th);
2382 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2385 /* we try to correct */
2386 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2389 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2392 if (l_cp->tw > l_cp->max_tiles) {
2394 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2395 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2396 l_cp->max_tiles, l_cp->tw);
2400 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2403 if (l_cp->th > l_cp->max_tiles) {
2405 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2406 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2407 l_cp->max_tiles, l_cp->th);
2411 #endif /* USE_JPWL */
2413 /* memory allocations */
2414 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2415 if (l_cp->tcps == 00) {
2416 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2419 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2422 if (l_cp->correct) {
2424 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2425 "JPWL: could not alloc tcps field of cp\n");
2426 if (!JPWL_ASSUME || JPWL_ASSUME) {
2427 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2432 #endif /* USE_JPWL */
2434 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2435 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2436 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2437 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2440 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2442 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2443 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2445 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2446 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2449 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));
2450 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2452 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2453 (opj_simple_mcc_decorrelation_data_t*)
2454 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2456 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2457 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2460 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));
2461 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2463 /* set up default dc level shift */
2464 for (i=0;i<l_image->numcomps;++i) {
2465 if (! l_image->comps[i].sgnd) {
2466 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2470 l_current_tile_param = l_cp->tcps;
2471 for (i = 0; i < l_nb_tiles; ++i) {
2472 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2473 if (l_current_tile_param->tccps == 00) {
2474 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2477 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2479 ++l_current_tile_param;
2482 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2483 opj_image_comp_header_update(l_image,l_cp);
2490 static void j2k_write_com(opj_j2k_t *j2k) {
2494 if(j2k->cp->comment) {
2495 opj_cio_t *cio = j2k->cio;
2496 char *comment = j2k->cp->comment;
2498 cio_write(cio, J2K_MS_COM, 2);
2499 lenp = cio_tell(cio);
2501 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2502 for (i = 0; i < strlen(comment); i++) {
2503 cio_write(cio, comment[i], 1);
2505 len = cio_tell(cio) - lenp;
2506 cio_seek(cio, lenp);
2507 cio_write(cio, len, 2);
2508 cio_seek(cio, lenp + len);
2512 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2518 * Writes the COM marker (comment)
2520 * @param p_stream the stream to write data to.
2521 * @param p_j2k J2K codec.
2522 * @param p_manager the user event manager.
2524 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2525 struct opj_stream_private *p_stream,
2526 struct opj_event_mgr * p_manager )
2528 OPJ_UINT32 l_comment_size;
2529 OPJ_UINT32 l_total_com_size;
2530 const OPJ_CHAR *l_comment;
2531 OPJ_BYTE * l_current_ptr = 00;
2534 assert(p_j2k != 00);
2535 assert(p_stream != 00);
2536 assert(p_manager != 00);
2538 l_comment = p_j2k->m_cp.comment;
2539 l_comment_size = strlen(l_comment);
2540 l_total_com_size = l_comment_size + 6;
2542 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2543 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2544 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2547 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2551 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2554 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2556 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2559 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2562 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2565 memcpy( l_current_ptr,l_comment,l_comment_size);
2567 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) {
2575 * Reads a COM marker (comments)
2576 * @param p_header_data the data contained in the COM box.
2577 * @param jp2 the jpeg2000 file codec.
2578 * @param p_header_size the size of the data contained in the COM marker.
2579 * @param p_manager the user event manager.
2581 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2582 OPJ_BYTE * p_header_data,
2583 OPJ_UINT32 p_header_size,
2584 opj_event_mgr_t * p_manager
2588 assert(p_j2k != 00);
2589 assert(p_manager != 00);
2590 assert(p_header_data != 00);
2591 (void)p_header_size;
2596 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2599 opj_cp_t *cp = j2k->cp;
2600 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2601 opj_tccp_t *tccp = &tcp->tccps[compno];
2602 opj_cio_t *cio = j2k->cio;
2604 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2605 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2606 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2607 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2608 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2610 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2611 for (i = 0; i < tccp->numresolutions; i++) {
2612 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2618 static void j2k_write_cod(opj_j2k_t *j2k) {
2619 opj_cp_t *cp = NULL;
2620 opj_tcp_t *tcp = NULL;
2623 opj_cio_t *cio = j2k->cio;
2625 cio_write(cio, J2K_MS_COD, 2); /* COD */
2627 lenp = cio_tell(cio);
2631 tcp = &cp->tcps[j2k->curtileno];
2633 cio_write(cio, tcp->csty, 1); /* Scod */
2634 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2635 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2636 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2638 j2k_write_cox(j2k, 0);
2639 len = cio_tell(cio) - lenp;
2640 cio_seek(cio, lenp);
2641 cio_write(cio, len, 2); /* Lcod */
2642 cio_seek(cio, lenp + len);
2645 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2650 * Writes the COD marker (Coding style default)
2652 * @param p_stream the stream to write data to.
2653 * @param p_j2k J2K codec.
2654 * @param p_manager the user event manager.
2656 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2657 struct opj_stream_private *p_stream,
2658 struct opj_event_mgr * p_manager )
2660 opj_cp_v2_t *l_cp = 00;
2661 opj_tcp_v2_t *l_tcp = 00;
2662 OPJ_UINT32 l_code_size,l_remaining_size;
2663 OPJ_BYTE * l_current_data = 00;
2666 assert(p_j2k != 00);
2667 assert(p_manager != 00);
2668 assert(p_stream != 00);
2670 l_cp = &(p_j2k->m_cp);
2671 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2672 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2673 l_remaining_size = l_code_size;
2675 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2676 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2677 = (OPJ_BYTE*)opj_realloc(
2678 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2681 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2685 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2688 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2690 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2691 l_current_data += 2;
2693 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2694 l_current_data += 2;
2696 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2699 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2702 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2705 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2708 l_remaining_size -= 9;
2710 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2711 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2715 if (l_remaining_size != 0) {
2716 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2720 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) {
2729 * Reads a COD marker (Coding Styke defaults)
2730 * @param p_header_data the data contained in the COD box.
2731 * @param p_j2k the jpeg2000 codec.
2732 * @param p_header_size the size of the data contained in the COD marker.
2733 * @param p_manager the user event manager.
2735 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2736 OPJ_BYTE * p_header_data,
2737 OPJ_UINT32 p_header_size,
2738 opj_event_mgr_t * p_manager
2744 opj_cp_v2_t *l_cp = 00;
2745 opj_tcp_v2_t *l_tcp = 00;
2746 opj_image_t *l_image = 00;
2749 assert(p_header_data != 00);
2750 assert(p_j2k != 00);
2751 assert(p_manager != 00);
2753 l_image = p_j2k->m_private_image;
2754 l_cp = &(p_j2k->m_cp);
2756 /* If we are in the first tile-part header of the current tile */
2757 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2758 &l_cp->tcps[p_j2k->m_current_tile_number] :
2759 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2761 /* Make sure room is sufficient */
2762 if (p_header_size < 5) {
2763 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2767 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2769 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2771 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2772 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2775 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2776 if (l_cp->m_specific_param.m_dec.m_layer) {
2777 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2780 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2783 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2787 for (i = 0; i < l_image->numcomps; ++i) {
2788 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2791 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2796 if (p_header_size != 0) {
2797 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2801 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2802 j2k_copy_tile_component_parameters(p_j2k);
2805 #ifdef WIP_REMOVE_MSD
2806 if (p_j2k->cstr_info) {
2807 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2808 p_j2k->cstr_info->prog = l_tcp->prg;
2809 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2810 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2811 for (i = 0; i < l_image->numcomps; ++i) {
2812 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2820 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2823 opj_cp_t *cp = j2k->cp;
2824 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2825 opj_image_t *image = j2k->image;
2826 opj_cio_t *cio = j2k->cio;
2828 cio_write(cio, J2K_MS_COC, 2); /* COC */
2829 lenp = cio_tell(cio);
2831 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2832 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2833 j2k_write_cox(j2k, compno);
2834 len = cio_tell(cio) - lenp;
2835 cio_seek(cio, lenp);
2836 cio_write(cio, len, 2); /* Lcoc */
2837 cio_seek(cio, lenp + len);
2841 * Writes the COC marker (Coding style component)
2843 * @param p_comp_no the index of the component to output.
2844 * @param p_stream the stream to write data to.
2845 * @param p_j2k J2K codec.
2846 * @param p_manager the user event manager.
2848 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2849 OPJ_UINT32 p_comp_no,
2850 struct opj_stream_private *p_stream,
2851 struct opj_event_mgr * p_manager )
2853 OPJ_UINT32 l_coc_size,l_remaining_size;
2854 OPJ_UINT32 l_comp_room;
2857 assert(p_j2k != 00);
2858 assert(p_manager != 00);
2859 assert(p_stream != 00);
2861 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2863 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2865 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2866 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2867 = (OPJ_BYTE*)opj_realloc(
2868 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2870 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2874 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2877 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);
2879 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) {
2887 * Writes the COC marker (Coding style component)
2889 * @param p_comp_no the index of the component to output.
2890 * @param p_stream the stream to write data to.
2891 * @param p_j2k J2K codec.
2892 * @param p_manager the user event manager.
2894 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2895 OPJ_UINT32 p_comp_no,
2897 OPJ_UINT32 * p_data_written,
2898 struct opj_event_mgr * p_manager )
2900 opj_cp_v2_t *l_cp = 00;
2901 opj_tcp_v2_t *l_tcp = 00;
2902 OPJ_UINT32 l_coc_size,l_remaining_size;
2903 OPJ_BYTE * l_current_data = 00;
2904 opj_image_t *l_image = 00;
2905 OPJ_UINT32 l_comp_room;
2908 assert(p_j2k != 00);
2909 assert(p_manager != 00);
2911 l_cp = &(p_j2k->m_cp);
2912 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2913 l_image = p_j2k->m_private_image;
2914 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2916 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2917 l_remaining_size = l_coc_size;
2919 l_current_data = p_data;
2921 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2922 l_current_data += 2;
2924 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2925 l_current_data += 2;
2927 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2928 l_current_data+=l_comp_room;
2930 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2933 l_remaining_size -= (5 + l_comp_room);
2934 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2935 * p_data_written = l_coc_size;
2939 * Gets the maximum size taken by a coc.
2941 * @param p_j2k the jpeg2000 codec to use.
2943 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2946 OPJ_UINT32 l_nb_comp;
2947 OPJ_UINT32 l_nb_tiles;
2948 OPJ_UINT32 l_max = 0;
2952 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2953 l_nb_comp = p_j2k->m_private_image->numcomps;
2955 for (i=0;i<l_nb_tiles;++i) {
2956 for (j=0;j<l_nb_comp;++j) {
2957 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2966 * Reads a COC marker (Coding Style Component)
2967 * @param p_header_data the data contained in the COC box.
2968 * @param p_j2k the jpeg2000 codec.
2969 * @param p_header_size the size of the data contained in the COC marker.
2970 * @param p_manager the user event manager.
2972 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2973 OPJ_BYTE * p_header_data,
2974 OPJ_UINT32 p_header_size,
2975 opj_event_mgr_t * p_manager
2978 opj_cp_v2_t *l_cp = NULL;
2979 opj_tcp_v2_t *l_tcp = NULL;
2980 opj_image_t *l_image = NULL;
2981 OPJ_UINT32 l_comp_room;
2982 OPJ_UINT32 l_comp_no;
2985 assert(p_header_data != 00);
2986 assert(p_j2k != 00);
2987 assert(p_manager != 00);
2989 l_cp = &(p_j2k->m_cp);
2990 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2991 &l_cp->tcps[p_j2k->m_current_tile_number] :
2992 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2993 l_image = p_j2k->m_private_image;
2995 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2997 /* make sure room is sufficient*/
2998 if (p_header_size < l_comp_room + 1) {
2999 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3002 p_header_size -= l_comp_room + 1;
3004 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3005 p_header_data += l_comp_room;
3006 if (l_comp_no >= l_image->numcomps) {
3007 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3011 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3014 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3015 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3019 if (p_header_size != 0) {
3020 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3026 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3027 int bandno, numbands;
3030 opj_cp_t *cp = j2k->cp;
3031 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3032 opj_tccp_t *tccp = &tcp->tccps[compno];
3033 opj_cio_t *cio = j2k->cio;
3035 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3036 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3038 for (bandno = 0; bandno < numbands; bandno++) {
3039 expn = tccp->stepsizes[bandno].expn;
3040 mant = tccp->stepsizes[bandno].mant;
3042 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3043 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3045 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3051 static void j2k_write_qcd(opj_j2k_t *j2k) {
3054 opj_cio_t *cio = j2k->cio;
3056 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3057 lenp = cio_tell(cio);
3059 j2k_write_qcx(j2k, 0);
3060 len = cio_tell(cio) - lenp;
3061 cio_seek(cio, lenp);
3062 cio_write(cio, len, 2); /* Lqcd */
3063 cio_seek(cio, lenp + len);
3066 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3070 * Writes the QCD marker (quantization default)
3072 * @param p_comp_number the index of the component to output.
3073 * @param p_stream the stream to write data to.
3074 * @param p_j2k J2K codec.
3075 * @param p_manager the user event manager.
3077 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3078 struct opj_stream_private *p_stream,
3079 struct opj_event_mgr * p_manager )
3081 opj_cp_v2_t *l_cp = 00;
3082 opj_tcp_v2_t *l_tcp = 00;
3083 OPJ_UINT32 l_qcd_size,l_remaining_size;
3084 OPJ_BYTE * l_current_data = 00;
3087 assert(p_j2k != 00);
3088 assert(p_manager != 00);
3089 assert(p_stream != 00);
3091 l_cp = &(p_j2k->m_cp);
3092 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3093 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3094 l_remaining_size = l_qcd_size;
3096 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3097 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3098 = (OPJ_BYTE*)opj_realloc(
3099 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3102 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3106 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3109 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3111 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3112 l_current_data += 2;
3114 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3115 l_current_data += 2;
3117 l_remaining_size -= 4;
3119 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3124 if (l_remaining_size != 0) {
3125 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3129 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) {
3137 * Reads a QCD marker (Quantization defaults)
3138 * @param p_header_data the data contained in the QCD box.
3139 * @param p_j2k the jpeg2000 codec.
3140 * @param p_header_size the size of the data contained in the QCD marker.
3141 * @param p_manager the user event manager.
3143 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3144 OPJ_BYTE * p_header_data,
3145 OPJ_UINT32 p_header_size,
3146 opj_event_mgr_t * p_manager
3150 assert(p_header_data != 00);
3151 assert(p_j2k != 00);
3152 assert(p_manager != 00);
3154 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3155 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3159 if (p_header_size != 0) {
3160 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3164 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3165 j2k_copy_tile_quantization_parameters(p_j2k);
3170 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3173 opj_cio_t *cio = j2k->cio;
3175 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3176 lenp = cio_tell(cio);
3178 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3179 j2k_write_qcx(j2k, compno);
3180 len = cio_tell(cio) - lenp;
3181 cio_seek(cio, lenp);
3182 cio_write(cio, len, 2); /* Lqcc */
3183 cio_seek(cio, lenp + len);
3187 * Writes the QCC marker (quantization component)
3189 * @param p_comp_no the index of the component to output.
3190 * @param p_stream the stream to write data to.
3191 * @param p_j2k J2K codec.
3192 * @param p_manager the user event manager.
3194 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3195 OPJ_UINT32 p_comp_no,
3196 struct opj_stream_private *p_stream,
3197 struct opj_event_mgr * p_manager )
3199 OPJ_UINT32 l_qcc_size,l_remaining_size;
3202 assert(p_j2k != 00);
3203 assert(p_manager != 00);
3204 assert(p_stream != 00);
3206 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3207 l_remaining_size = l_qcc_size;
3209 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3210 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3211 = (OPJ_BYTE*)opj_realloc(
3212 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3214 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3218 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3221 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);
3223 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) {
3231 * Writes the QCC marker (quantization component)
3233 * @param p_comp_no the index of the component to output.
3234 * @param p_stream the stream to write data to.
3235 * @param p_j2k J2K codec.
3236 * @param p_manager the user event manager.
3238 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3239 OPJ_UINT32 p_comp_no,
3241 OPJ_UINT32 * p_data_written,
3242 struct opj_event_mgr * p_manager )
3244 OPJ_UINT32 l_qcc_size,l_remaining_size;
3245 OPJ_BYTE * l_current_data = 00;
3248 assert(p_j2k != 00);
3249 assert(p_manager != 00);
3251 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3252 l_remaining_size = l_qcc_size;
3254 l_current_data = p_data;
3256 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3257 l_current_data += 2;
3259 if (p_j2k->m_private_image->numcomps <= 256) {
3262 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3263 l_current_data += 2;
3265 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3268 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3269 l_remaining_size -= 6;
3272 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3273 l_current_data += 2;
3275 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3278 l_remaining_size -= 6;
3281 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3283 *p_data_written = l_qcc_size;
3287 * Gets the maximum size taken by a qcc.
3289 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3291 return j2k_get_max_coc_size(p_j2k);
3295 * Reads a QCC marker (Quantization component)
3296 * @param p_header_data the data contained in the QCC box.
3297 * @param p_j2k the jpeg2000 codec.
3298 * @param p_header_size the size of the data contained in the QCC marker.
3299 * @param p_manager the user event manager.
3301 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3302 OPJ_BYTE * p_header_data,
3303 OPJ_UINT32 p_header_size,
3304 opj_event_mgr_t * p_manager
3307 OPJ_UINT32 l_num_comp,l_comp_no;
3310 assert(p_header_data != 00);
3311 assert(p_j2k != 00);
3312 assert(p_manager != 00);
3314 l_num_comp = p_j2k->m_private_image->numcomps;
3316 if (l_num_comp <= 256) {
3317 if (p_header_size < 1) {
3318 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3321 opj_read_bytes(p_header_data,&l_comp_no,1);
3326 if (p_header_size < 2) {
3327 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3330 opj_read_bytes(p_header_data,&l_comp_no,2);
3336 if (p_j2k->m_cp.correct) {
3338 static OPJ_UINT32 backup_compno = 0;
3340 /* compno is negative or larger than the number of components!!! */
3341 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3342 opj_event_msg_v2(p_manager, EVT_ERROR,
3343 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3344 l_comp_no, l_num_comp);
3346 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3349 /* we try to correct */
3350 l_comp_no = backup_compno % l_num_comp;
3351 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3352 "- setting component number to %d\n",
3356 /* keep your private count of tiles */
3359 #endif /* USE_JPWL */
3361 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3362 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3366 if (p_header_size != 0) {
3367 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3375 static void j2k_write_poc(opj_j2k_t *j2k) {
3376 int len, numpchgs, i;
3378 int numcomps = j2k->image->numcomps;
3380 opj_cp_t *cp = j2k->cp;
3381 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3382 opj_tccp_t *tccp = &tcp->tccps[0];
3383 opj_cio_t *cio = j2k->cio;
3385 numpchgs = 1 + tcp->numpocs;
3386 cio_write(cio, J2K_MS_POC, 2); /* POC */
3387 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3388 cio_write(cio, len, 2); /* Lpoc */
3389 for (i = 0; i < numpchgs; i++) {
3390 opj_poc_t *poc = &tcp->pocs[i];
3391 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3392 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3393 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3394 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3395 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3396 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3397 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3398 poc->compno1 = int_min(poc->compno1, numcomps);
3399 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3404 * Writes the POC marker (Progression Order Change)
3406 * @param p_stream the stream to write data to.
3407 * @param p_j2k J2K codec.
3408 * @param p_manager the user event manager.
3410 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3411 struct opj_stream_private *p_stream,
3412 struct opj_event_mgr * p_manager )
3414 OPJ_UINT32 l_nb_comp;
3415 OPJ_UINT32 l_nb_poc;
3416 OPJ_UINT32 l_poc_size;
3417 OPJ_UINT32 l_written_size = 0;
3418 opj_tcp_v2_t *l_tcp = 00;
3419 opj_tccp_t *l_tccp = 00;
3420 OPJ_UINT32 l_poc_room;
3423 assert(p_j2k != 00);
3424 assert(p_manager != 00);
3425 assert(p_stream != 00);
3427 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3428 l_tccp = &l_tcp->tccps[0];
3429 l_nb_comp = p_j2k->m_private_image->numcomps;
3430 l_nb_poc = 1 + l_tcp->numpocs;
3432 if (l_nb_comp <= 256) {
3438 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3440 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3441 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3442 = (OPJ_BYTE*)opj_realloc(
3443 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3446 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3450 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3453 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3455 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) {
3464 * Writes the POC marker (Progression Order Change)
3466 * @param p_stream the stream to write data to.
3467 * @param p_j2k J2K codec.
3468 * @param p_manager the user event manager.
3470 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3472 OPJ_UINT32 * p_data_written,
3473 struct opj_event_mgr * p_manager )
3476 OPJ_BYTE * l_current_data = 00;
3477 OPJ_UINT32 l_nb_comp;
3478 OPJ_UINT32 l_nb_poc;
3479 OPJ_UINT32 l_poc_size;
3480 opj_image_t *l_image = 00;
3481 opj_tcp_v2_t *l_tcp = 00;
3482 opj_tccp_t *l_tccp = 00;
3483 opj_poc_t *l_current_poc = 00;
3484 OPJ_UINT32 l_poc_room;
3487 assert(p_j2k != 00);
3488 assert(p_manager != 00);
3490 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3491 l_tccp = &l_tcp->tccps[0];
3492 l_image = p_j2k->m_private_image;
3493 l_nb_comp = l_image->numcomps;
3494 l_nb_poc = 1 + l_tcp->numpocs;
3496 if (l_nb_comp <= 256) {
3503 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3505 l_current_data = p_data;
3507 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3508 l_current_data += 2;
3510 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3511 l_current_data += 2;
3513 l_current_poc = l_tcp->pocs;
3514 for (i = 0; i < l_nb_poc; ++i) {
3515 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3518 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3519 l_current_data+=l_poc_room;
3521 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3524 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3527 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3528 l_current_data+=l_poc_room;
3530 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3533 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3534 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3535 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3536 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3541 *p_data_written = l_poc_size;
3545 * Gets the maximum size taken by the writing of a POC.
3547 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3549 opj_tcp_v2_t * l_tcp = 00;
3550 OPJ_UINT32 l_nb_tiles = 0;
3551 OPJ_UINT32 l_max_poc = 0;
3554 l_tcp = p_j2k->m_cp.tcps;
3555 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3557 for (i=0;i<l_nb_tiles;++i) {
3558 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3564 return 4 + 9 * l_max_poc;
3568 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3570 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3573 OPJ_UINT32 l_nb_tiles;
3574 OPJ_UINT32 l_max = 0;
3575 opj_tcp_v2_t * l_tcp = 00;
3577 l_tcp = p_j2k->m_cp.tcps;
3578 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3580 for (i=0;i<l_nb_tiles;++i) {
3581 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3591 * Gets the maximum size taken by the headers of the SOT.
3593 * @param p_j2k the jpeg2000 codec to use.
3595 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3597 OPJ_UINT32 l_nb_bytes = 0;
3598 OPJ_UINT32 l_nb_comps;
3599 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3601 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3602 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3604 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3605 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3606 l_nb_bytes += l_nb_comps * l_coc_bytes;
3608 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3609 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3612 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3614 /*** DEVELOPER CORNER, Add room for your headers ***/
3621 * Reads a POC marker (Progression Order Change)
3623 * @param p_header_data the data contained in the POC box.
3624 * @param p_j2k the jpeg2000 codec.
3625 * @param p_header_size the size of the data contained in the POC marker.
3626 * @param p_manager the user event manager.
3628 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3629 OPJ_BYTE * p_header_data,
3630 OPJ_UINT32 p_header_size,
3631 opj_event_mgr_t * p_manager
3634 OPJ_UINT32 i, l_nb_comp, l_tmp;
3635 opj_image_t * l_image = 00;
3636 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3637 OPJ_UINT32 l_chunk_size, l_comp_room;
3639 opj_cp_v2_t *l_cp = 00;
3640 opj_tcp_v2_t *l_tcp = 00;
3641 opj_poc_t *l_current_poc = 00;
3644 assert(p_header_data != 00);
3645 assert(p_j2k != 00);
3646 assert(p_manager != 00);
3648 l_image = p_j2k->m_private_image;
3649 l_nb_comp = l_image->numcomps;
3650 if (l_nb_comp <= 256) {
3656 l_chunk_size = 5 + 2 * l_comp_room;
3657 l_current_poc_nb = p_header_size / l_chunk_size;
3658 l_current_poc_remaining = p_header_size % l_chunk_size;
3660 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3661 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3665 l_cp = &(p_j2k->m_cp);
3666 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3667 &l_cp->tcps[p_j2k->m_current_tile_number] :
3668 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3669 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3670 l_current_poc_nb += l_old_poc_nb;
3672 assert(l_current_poc_nb < 32);
3674 /* now poc is in use.*/
3677 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3678 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3679 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3681 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3682 p_header_data+=l_comp_room;
3683 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3685 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3687 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3688 p_header_data+=l_comp_room;
3689 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3691 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3692 /* make sure comp is in acceptable bounds */
3693 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3697 l_tcp->numpocs = l_current_poc_nb - 1;
3702 * Reads a CRG marker (Component registration)
3704 * @param p_header_data the data contained in the TLM box.
3705 * @param p_j2k the jpeg2000 codec.
3706 * @param p_header_size the size of the data contained in the TLM marker.
3707 * @param p_manager the user event manager.
3709 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3710 OPJ_BYTE * p_header_data,
3711 OPJ_UINT32 p_header_size,
3712 opj_event_mgr_t * p_manager
3715 OPJ_UINT32 l_nb_comp;
3717 assert(p_header_data != 00);
3718 assert(p_j2k != 00);
3719 assert(p_manager != 00);
3721 l_nb_comp = p_j2k->m_private_image->numcomps;
3723 if (p_header_size != l_nb_comp *4) {
3724 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3727 /* Do not care of this at the moment since only local variables are set here */
3730 (i = 0; i < l_nb_comp; ++i)
3732 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3734 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3742 * Reads a TLM marker (Tile Length Marker)
3744 * @param p_header_data the data contained in the TLM box.
3745 * @param p_j2k the jpeg2000 codec.
3746 * @param p_header_size the size of the data contained in the TLM marker.
3747 * @param p_manager the user event manager.
3749 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3750 OPJ_BYTE * p_header_data,
3751 OPJ_UINT32 p_header_size,
3752 opj_event_mgr_t * p_manager
3755 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3757 assert(p_header_data != 00);
3758 assert(p_j2k != 00);
3759 assert(p_manager != 00);
3761 if (p_header_size < 2) {
3762 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3767 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3769 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3772 l_ST = ((l_Stlm >> 4) & 0x3);
3773 l_SP = (l_Stlm >> 6) & 0x1;
3775 l_Ptlm_size = (l_SP + 1) * 2;
3776 l_quotient = l_Ptlm_size + l_ST;
3778 l_tot_num_tp = p_header_size / l_quotient;
3779 l_tot_num_tp_remaining = p_header_size % l_quotient;
3781 if (l_tot_num_tp_remaining != 0) {
3782 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3785 /* FIXME Do not care of this at the moment since only local variables are set here */
3788 (i = 0; i < l_tot_num_tp; ++i)
3790 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3791 p_header_data += l_ST;
3792 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3793 p_header_data += l_Ptlm_size;
3800 * Reads a PLM marker (Packet length, main header marker)
3802 * @param p_header_data the data contained in the TLM box.
3803 * @param p_j2k the jpeg2000 codec.
3804 * @param p_header_size the size of the data contained in the TLM marker.
3805 * @param p_manager the user event manager.
3807 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3808 OPJ_BYTE * p_header_data,
3809 OPJ_UINT32 p_header_size,
3810 opj_event_mgr_t * p_manager
3814 assert(p_header_data != 00);
3815 assert(p_j2k != 00);
3816 assert(p_manager != 00);
3818 if (p_header_size < 1) {
3819 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3822 /* Do not care of this at the moment since only local variables are set here */
3824 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3831 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3833 p_header_size -= (1+l_Nplm);
3837 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3841 (i = 0; i < l_Nplm; ++i)
3843 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3845 // take only the last seven bytes
3846 l_packet_len |= (l_tmp & 0x7f);
3854 // store packet length and proceed to next packet
3861 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3870 * Reads a PLT marker (Packet length, tile-part header)
3872 * @param p_header_data the data contained in the PLT box.
3873 * @param p_j2k the jpeg2000 codec.
3874 * @param p_header_size the size of the data contained in the PLT marker.
3875 * @param p_manager the user event manager.
3877 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3878 OPJ_BYTE * p_header_data,
3879 OPJ_UINT32 p_header_size,
3880 opj_event_mgr_t * p_manager
3883 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3886 assert(p_header_data != 00);
3887 assert(p_j2k != 00);
3888 assert(p_manager != 00);
3890 if (p_header_size < 1) {
3891 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3895 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3899 for (i = 0; i < p_header_size; ++i) {
3900 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3902 /* take only the last seven bytes */
3903 l_packet_len |= (l_tmp & 0x7f);
3908 /* store packet length and proceed to next packet */
3913 if (l_packet_len != 0) {
3914 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3923 * Reads a PPM marker (Packed packet headers, main header)
3925 * @param p_header_data the data contained in the POC box.
3926 * @param p_j2k the jpeg2000 codec.
3927 * @param p_header_size the size of the data contained in the POC marker.
3928 * @param p_manager the user event manager.
3931 opj_bool j2k_read_ppm_v2 (
3932 opj_j2k_v2_t *p_j2k,
3933 OPJ_BYTE * p_header_data,
3934 OPJ_UINT32 p_header_size,
3935 struct opj_event_mgr * p_manager
3939 opj_cp_v2_t *l_cp = 00;
3940 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3943 assert(p_header_data != 00);
3944 assert(p_j2k != 00);
3945 assert(p_manager != 00);
3947 if (p_header_size < 1) {
3948 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3952 l_cp = &(p_j2k->m_cp);
3955 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3959 /* First PPM marker */
3961 if (p_header_size < 4) {
3962 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3966 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3970 /* First PPM marker: Initialization */
3971 l_cp->ppm_len = l_N_ppm;
3972 l_cp->ppm_data_size = 0;
3974 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3975 if (l_cp->ppm_buffer == 00) {
3976 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3979 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3981 l_cp->ppm_data = l_cp->ppm_buffer;
3985 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3986 if (p_header_size >= 4) {
3988 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3991 l_cp->ppm_len += l_N_ppm ;
3993 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3994 if (l_cp->ppm_buffer == 00) {
3995 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3998 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4000 l_cp->ppm_data = l_cp->ppm_buffer;
4007 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4009 if (l_remaining_data <= p_header_size) {
4010 /* we must store less information than available in the packet */
4011 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4012 l_cp->ppm_data_size = l_cp->ppm_len;
4013 p_header_size -= l_remaining_data;
4014 p_header_data += l_remaining_data;
4017 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4018 l_cp->ppm_data_size += p_header_size;
4019 p_header_data += p_header_size;
4032 * Reads a PPM marker (Packed packet headers, main header)
4034 * @param p_header_data the data contained in the POC box.
4035 * @param p_j2k the jpeg2000 codec.
4036 * @param p_header_size the size of the data contained in the POC marker.
4037 * @param p_manager the user event manager.
4039 opj_bool j2k_read_ppm_v3 (
4040 opj_j2k_v2_t *p_j2k,
4041 OPJ_BYTE * p_header_data,
4042 OPJ_UINT32 p_header_size,
4043 struct opj_event_mgr * p_manager
4046 opj_cp_v2_t *l_cp = 00;
4047 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4050 assert(p_header_data != 00);
4051 assert(p_j2k != 00);
4052 assert(p_manager != 00);
4054 /* Minimum size of PPM marker is equal to the size of Zppm element */
4055 if (p_header_size < 1) {
4056 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4060 l_cp = &(p_j2k->m_cp);
4063 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4067 /* First PPM marker */
4069 /* We need now at least the Nppm^0 element */
4070 if (p_header_size < 4) {
4071 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4075 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4079 /* First PPM marker: Initialization */
4080 l_cp->ppm_len = l_N_ppm;
4081 l_cp->ppm_data_read = 0;
4083 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4084 if (l_cp->ppm_data == 00) {
4085 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4088 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4090 l_cp->ppm_data_current = l_cp->ppm_data;
4092 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4095 if (p_header_size < 4) {
4096 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4100 /* Uncompleted Ippm series in the previous PPM marker?*/
4101 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4102 /* Get the place where add the remaining Ippm series*/
4103 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4104 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4107 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4111 /* Increase the size of ppm_data to add the new Ippm series*/
4112 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4114 /* Keep the position of the place where concatenate the new series*/
4115 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4116 l_cp->ppm_len += l_N_ppm;
4121 l_remaining_data = p_header_size;
4123 while (l_remaining_data >= l_N_ppm) {
4124 /* read a complete Ippm series*/
4125 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4126 p_header_size -= l_N_ppm;
4127 p_header_data += l_N_ppm;
4129 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4133 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4138 l_remaining_data = p_header_size;
4142 l_remaining_data = p_header_size;
4144 /* Next Ippm series is a complete series ?*/
4145 if (l_remaining_data > l_N_ppm) {
4146 /* Increase the size of ppm_data to add the new Ippm series*/
4147 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4149 /* Keep the position of the place where concatenate the new series */
4150 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4151 l_cp->ppm_len += l_N_ppm;
4156 /* Need to read an incomplete Ippm series*/
4157 if (l_remaining_data) {
4158 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4160 /* Keep the position of the place where concatenate the new series*/
4161 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4162 l_cp->ppm_len += l_N_ppm;
4164 /* Read incomplete Ippm series*/
4165 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4166 p_header_size -= l_remaining_data;
4167 p_header_data += l_remaining_data;
4169 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4174 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4175 if (p_header_size >= 4) {
4177 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4180 l_cp->ppm_len += l_N_ppm ;
4182 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4183 if (l_cp->ppm_buffer == 00) {
4184 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4187 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4189 l_cp->ppm_data = l_cp->ppm_buffer;
4196 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4198 if (l_remaining_data <= p_header_size) {
4199 /* we must store less information than available in the packet */
4200 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4201 l_cp->ppm_data_size = l_cp->ppm_len;
4202 p_header_size -= l_remaining_data;
4203 p_header_data += l_remaining_data;
4206 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4207 l_cp->ppm_data_size += p_header_size;
4208 p_header_data += p_header_size;
4218 * Reads a PPT marker (Packed packet headers, tile-part header)
4220 * @param p_header_data the data contained in the PPT box.
4221 * @param p_j2k the jpeg2000 codec.
4222 * @param p_header_size the size of the data contained in the PPT marker.
4223 * @param p_manager the user event manager.
4225 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4226 OPJ_BYTE * p_header_data,
4227 OPJ_UINT32 p_header_size,
4228 opj_event_mgr_t * p_manager
4231 opj_cp_v2_t *l_cp = 00;
4232 opj_tcp_v2_t *l_tcp = 00;
4236 assert(p_header_data != 00);
4237 assert(p_j2k != 00);
4238 assert(p_manager != 00);
4240 /* We need to have the Z_ppt element at minimum */
4241 if (p_header_size < 1) {
4242 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4246 l_cp = &(p_j2k->m_cp);
4248 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");
4252 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4255 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4259 /* Allocate buffer to read the packet header */
4261 /* First PPT marker */
4262 l_tcp->ppt_data_size = 0;
4263 l_tcp->ppt_len = p_header_size;
4265 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4266 if (l_tcp->ppt_buffer == 00) {
4267 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4270 l_tcp->ppt_data = l_tcp->ppt_buffer;
4272 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4275 l_tcp->ppt_len += p_header_size;
4277 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4278 if (l_tcp->ppt_buffer == 00) {
4279 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4282 l_tcp->ppt_data = l_tcp->ppt_buffer;
4284 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4287 /* Read packet header from buffer */
4288 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4290 l_tcp->ppt_data_size += p_header_size;
4296 * Writes the TLM marker (Tile Length Marker)
4298 * @param p_stream the stream to write data to.
4299 * @param p_j2k J2K codec.
4300 * @param p_manager the user event manager.
4302 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4303 struct opj_stream_private *p_stream,
4304 struct opj_event_mgr * p_manager )
4306 OPJ_BYTE * l_current_data = 00;
4307 OPJ_UINT32 l_tlm_size;
4310 assert(p_j2k != 00);
4311 assert(p_manager != 00);
4312 assert(p_stream != 00);
4314 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4316 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4317 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4318 = (OPJ_BYTE*)opj_realloc(
4319 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4322 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4326 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4329 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4331 /* change the way data is written to avoid seeking if possible */
4333 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4335 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4336 l_current_data += 2;
4338 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4339 l_current_data += 2;
4341 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4344 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4347 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4348 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) {
4355 static void j2k_write_tlm(opj_j2k_t *j2k){
4357 opj_cio_t *cio = j2k->cio;
4358 j2k->tlm_start = cio_tell(cio);
4359 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4360 lenp = 4 + (5*j2k->totnum_tp);
4361 cio_write(cio,lenp,2); /* Ltlm */
4362 cio_write(cio, 0,1); /* Ztlm=0*/
4363 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4364 cio_skip(cio,5*j2k->totnum_tp);
4367 static void j2k_write_sot(opj_j2k_t *j2k) {
4370 opj_cio_t *cio = j2k->cio;
4372 j2k->sot_start = cio_tell(cio);
4373 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4374 lenp = cio_tell(cio);
4375 cio_skip(cio, 2); /* Lsot (further) */
4376 cio_write(cio, j2k->curtileno, 2); /* Isot */
4377 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4378 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4379 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4380 len = cio_tell(cio) - lenp;
4381 cio_seek(cio, lenp);
4382 cio_write(cio, len, 2); /* Lsot */
4383 cio_seek(cio, lenp + len);
4387 /* update markers struct */
4388 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4389 #endif /* USE_JPWL */
4392 if( j2k->cstr_info && j2k->cur_tp_num==0){
4393 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4398 * Writes the SOT marker (Start of tile-part)
4400 * @param p_stream the stream to write data to.
4401 * @param p_j2k J2K codec.
4402 * @param p_manager the user event manager.
4404 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4406 OPJ_UINT32 * p_data_written,
4407 const struct opj_stream_private *p_stream,
4408 struct opj_event_mgr * p_manager )
4411 assert(p_j2k != 00);
4412 assert(p_manager != 00);
4413 assert(p_stream != 00);
4415 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4418 opj_write_bytes(p_data,10,2); /* Lsot */
4421 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4427 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4430 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4435 /* update markers struct */
4437 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4439 assert( 0 && "TODO" );
4440 #endif /* USE_JPWL */
4442 * p_data_written = 12;
4451 * Reads a PPT marker (Packed packet headers, tile-part header)
4453 * @param p_header_data the data contained in the PPT box.
4454 * @param p_j2k the jpeg2000 codec.
4455 * @param p_header_size the size of the data contained in the PPT marker.
4456 * @param p_manager the user event manager.
4458 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4459 OPJ_BYTE * p_header_data,
4460 OPJ_UINT32 p_header_size,
4461 opj_event_mgr_t * p_manager )
4463 opj_cp_v2_t *l_cp = 00;
4464 opj_tcp_v2_t *l_tcp = 00;
4465 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4466 OPJ_UINT32 l_current_part;
4467 OPJ_UINT32 l_tile_x,l_tile_y;
4470 assert(p_header_data != 00);
4471 assert(p_j2k != 00);
4472 assert(p_manager != 00);
4474 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4475 if (p_header_size != 8) {
4476 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4480 l_cp = &(p_j2k->m_cp);
4481 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4484 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4485 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4486 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4489 if (l_cp->correct) {
4491 int tileno = p_j2k->m_current_tile_number;
4492 static int backup_tileno = 0;
4494 /* tileno is negative or larger than the number of tiles!!! */
4495 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4496 opj_event_msg_v2(p_manager, EVT_ERROR,
4497 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4498 tileno, (l_cp->tw * l_cp->th));
4500 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4503 /* we try to correct */
4504 tileno = backup_tileno;
4505 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4506 "- setting tile number to %d\n",
4510 /* keep your private count of tiles */
4513 #endif /* USE_JPWL */
4515 /* look for the tile in the list of already processed tile (in parts). */
4516 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4517 /* since the time taken by this function can only grow at the time */
4519 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4522 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4523 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4525 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4531 if (l_cp->correct) {
4533 /* totlen is negative or larger than the bytes left!!! */
4534 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))) { */
4535 opj_event_msg_v2(p_manager, EVT_ERROR,
4536 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4537 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4539 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4542 /* we try to correct */
4544 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4545 "- setting Psot to %d => assuming it is the last tile\n",
4549 #endif /* USE_JPWL */
4551 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4553 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4554 "we assuming it is the last tile-part of the codestream.\n");
4555 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4558 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4561 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4564 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4565 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4566 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4567 if (l_tcp->m_nb_tile_parts) {
4568 if (l_current_part >= l_tcp->m_nb_tile_parts){
4569 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4570 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4571 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4575 l_tcp->m_nb_tile_parts = l_num_parts;
4578 /* If know the number of tile part header we will check if we didn't read the last*/
4579 if (l_tcp->m_nb_tile_parts) {
4580 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4581 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4585 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4586 /* Keep the size of data to skip after this marker */
4587 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4590 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4591 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4594 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4596 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4597 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4598 p_j2k->m_specific_param.m_decoder.m_skip_data =
4599 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4600 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4601 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4602 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4605 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4606 p_j2k->m_specific_param.m_decoder.m_skip_data =
4607 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4611 if (p_j2k->cstr_index)
4613 assert(p_j2k->cstr_index->tile_index != 00);
4614 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4615 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4617 if (l_num_parts != 0){
4618 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4619 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4622 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4623 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4624 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4626 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4627 (opj_tp_index_t*)opj_realloc(
4628 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4629 l_num_parts* sizeof(opj_tp_index_t));
4632 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4634 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4635 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4636 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4637 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4638 sizeof(opj_tp_index_t));
4641 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4642 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4643 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4644 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4645 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4646 * sizeof(opj_tp_index_t));
4655 /* 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 */
4656 /* if (p_j2k->cstr_info) {
4659 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4662 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4663 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4664 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4665 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4668 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4671 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4675 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4678 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4679 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4680 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4685 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4688 opj_tcp_t *tcp = NULL;
4689 opj_codestream_info_t *cstr_info = NULL;
4691 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4692 opj_cp_t *cp = j2k->cp;
4693 opj_cio_t *cio = j2k->cio;
4695 tcd->tp_num = j2k->tp_num ;
4696 tcd->cur_tp_num = j2k->cur_tp_num;
4698 cio_write(cio, J2K_MS_SOD, 2);
4700 if( j2k->cstr_info && j2k->cur_tp_num==0){
4701 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4704 if (j2k->curtileno == 0) {
4705 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4709 cstr_info = j2k->cstr_info;
4711 if (!j2k->cur_tp_num ) {
4712 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4713 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4716 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4717 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4721 /* update markers struct */
4722 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4723 #endif /* USE_JPWL */
4728 tcp = &cp->tcps[j2k->curtileno];
4729 for (layno = 0; layno < tcp->numlayers; layno++) {
4730 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4731 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4732 } else if (tcp->rates[layno]) {
4733 tcp->rates[layno]=1;
4736 if(j2k->cur_tp_num == 0){
4737 tcd->tcd_image->tiles->packno = 0;
4739 cstr_info->packno = 0;
4742 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4744 /* Writing Psot in SOT marker */
4745 totlen = cio_tell(cio) + l - j2k->sot_start;
4746 cio_seek(cio, j2k->sot_start + 6);
4747 cio_write(cio, totlen, 4);
4748 cio_seek(cio, j2k->sot_start + totlen);
4749 /* Writing Ttlm and Ptlm in TLM marker */
4751 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4752 cio_write(cio, j2k->curtileno, 1);
4753 cio_write(cio, totlen, 4);
4755 cio_seek(cio, j2k->sot_start + totlen);
4759 * Writes the SOD marker (Start of data)
4761 * @param p_stream the stream to write data to.
4762 * @param p_j2k J2K codec.
4763 * @param p_manager the user event manager.
4765 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4766 struct opj_tcd_v2 * p_tile_coder,
4768 OPJ_UINT32 * p_data_written,
4769 OPJ_UINT32 p_total_data_size,
4770 const struct opj_stream_private *p_stream,
4771 struct opj_event_mgr * p_manager )
4773 opj_tcp_v2_t *l_tcp = 00;
4774 opj_codestream_info_t *l_cstr_info = 00;
4775 opj_cp_v2_t *l_cp = 00;
4777 OPJ_UINT32 l_size_tile;
4778 OPJ_UINT32 l_remaining_data;
4781 assert(p_j2k != 00);
4782 assert(p_manager != 00);
4783 assert(p_stream != 00);
4785 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4788 /* make room for the EOF marker */
4789 l_remaining_data = p_total_data_size - 4;
4791 l_cp = &(p_j2k->m_cp);
4792 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4795 /* update tile coder */
4796 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4797 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4799 l_size_tile = l_cp->th * l_cp->tw;
4802 /* TODO mergeV2: check this part which use cstr_info */
4803 /*l_cstr_info = p_j2k->cstr_info;
4805 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4806 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4807 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4813 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4815 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4820 /* update markers struct */
4821 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4823 assert( 0 && "TODO" );
4824 #endif /* USE_JPWL */
4829 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4830 p_tile_coder->tcd_image->tiles->packno = 0;
4832 l_cstr_info->packno = 0;
4836 *p_data_written = 0;
4838 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)) {
4839 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4843 *p_data_written += 2;
4852 * Reads a SOD marker (Start Of Data)
4854 * @param p_header_data the data contained in the SOD box.
4855 * @param p_j2k the jpeg2000 codec.
4856 * @param p_header_size the size of the data contained in the SOD marker.
4857 * @param p_manager the user event manager.
4859 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4860 opj_stream_private_t *p_stream,
4861 opj_event_mgr_t * p_manager
4864 OPJ_UINT32 l_current_read_size;
4865 opj_codestream_index_t * l_cstr_index = 00;
4866 OPJ_BYTE ** l_current_data = 00;
4867 opj_tcp_v2_t * l_tcp = 00;
4868 OPJ_UINT32 * l_tile_len = 00;
4871 assert(p_j2k != 00);
4872 assert(p_manager != 00);
4873 assert(p_stream != 00);
4875 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4877 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4878 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4879 // but we are in the last tile part,
4880 // so its result will fit on OPJ_UINT32 unless we find
4881 // a file with a single tile part of more than 4 GB...*/
4882 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4885 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4887 l_current_data = &(l_tcp->m_data);
4888 l_tile_len = &l_tcp->m_data_size;
4890 if (! *l_current_data) {
4891 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4894 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4897 if (*l_current_data == 00) {
4898 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4904 l_cstr_index = p_j2k->cstr_index;
4906 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4908 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4909 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4911 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4912 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4914 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4918 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4920 /*l_cstr_index->packno = 0;*/
4923 l_current_read_size = opj_stream_read_data( p_stream,
4924 *l_current_data + *l_tile_len,
4925 p_j2k->m_specific_param.m_decoder.m_sot_length,
4928 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4929 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4932 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4935 *l_tile_len += l_current_read_size;
4941 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4942 opj_cp_t *cp = j2k->cp;
4943 opj_tcp_t *tcp = &cp->tcps[tileno];
4944 opj_cio_t *cio = j2k->cio;
4945 int numcomps = j2k->image->numcomps;
4947 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4948 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4949 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4950 cio_write(cio, 0, 1); /* Srgn */
4951 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4955 * Writes the RGN marker (Region Of Interest)
4957 * @param p_tile_no the tile to output
4958 * @param p_comp_no the component to output
4959 * @param p_stream the stream to write data to.
4960 * @param p_j2k J2K codec.
4961 * @param p_manager the user event manager.
4963 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4964 OPJ_UINT32 p_tile_no,
4965 OPJ_UINT32 p_comp_no,
4966 struct opj_stream_private *p_stream,
4967 struct opj_event_mgr * p_manager )
4969 OPJ_BYTE * l_current_data = 00;
4970 OPJ_UINT32 l_nb_comp;
4971 OPJ_UINT32 l_rgn_size;
4972 opj_image_t *l_image = 00;
4973 opj_cp_v2_t *l_cp = 00;
4974 opj_tcp_v2_t *l_tcp = 00;
4975 opj_tccp_t *l_tccp = 00;
4976 OPJ_UINT32 l_comp_room;
4979 assert(p_j2k != 00);
4980 assert(p_manager != 00);
4981 assert(p_stream != 00);
4983 l_cp = &(p_j2k->m_cp);
4984 l_tcp = &l_cp->tcps[p_tile_no];
4985 l_tccp = &l_tcp->tccps[p_comp_no];
4987 l_nb_comp = l_image->numcomps;
4989 if (l_nb_comp <= 256) {
4996 l_rgn_size = 6 + l_comp_room;
4998 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5000 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5001 l_current_data += 2;
5003 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5004 l_current_data += 2;
5006 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5007 l_current_data+=l_comp_room;
5009 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5012 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5015 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) {
5023 static void j2k_write_eoc(opj_j2k_t *j2k) {
5024 opj_cio_t *cio = j2k->cio;
5025 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5026 cio_write(cio, J2K_MS_EOC, 2);
5030 /* update markers struct */
5031 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5032 #endif /* USE_JPWL */
5037 * Writes the EOC marker (End of Codestream)
5039 * @param p_stream the stream to write data to.
5040 * @param p_j2k J2K codec.
5041 * @param p_manager the user event manager.
5043 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5044 struct opj_stream_private *p_stream,
5045 struct opj_event_mgr * p_manager )
5048 assert(p_j2k != 00);
5049 assert(p_manager != 00);
5050 assert(p_stream != 00);
5052 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5057 /* update markers struct */
5059 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5061 #endif /* USE_JPWL */
5063 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5067 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5076 * Reads a RGN marker (Region Of Interest)
5078 * @param p_header_data the data contained in the POC box.
5079 * @param p_j2k the jpeg2000 codec.
5080 * @param p_header_size the size of the data contained in the POC marker.
5081 * @param p_manager the user event manager.
5083 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5084 OPJ_BYTE * p_header_data,
5085 OPJ_UINT32 p_header_size,
5086 opj_event_mgr_t * p_manager
5089 OPJ_UINT32 l_nb_comp;
5090 opj_image_t * l_image = 00;
5092 opj_cp_v2_t *l_cp = 00;
5093 opj_tcp_v2_t *l_tcp = 00;
5094 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5097 assert(p_header_data != 00);
5098 assert(p_j2k != 00);
5099 assert(p_manager != 00);
5101 l_image = p_j2k->m_private_image;
5102 l_nb_comp = l_image->numcomps;
5104 if (l_nb_comp <= 256) {
5109 if (p_header_size != 2 + l_comp_room) {
5110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5114 l_cp = &(p_j2k->m_cp);
5115 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5116 &l_cp->tcps[p_j2k->m_current_tile_number] :
5117 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5119 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5120 p_header_data+=l_comp_room;
5121 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5125 if (l_cp->correct) {
5126 /* totlen is negative or larger than the bytes left!!! */
5127 if (l_comp_room >= l_nb_comp) {
5128 opj_event_msg_v2(p_manager, EVT_ERROR,
5129 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5130 l_comp_room, l_nb_comp);
5131 if (!JPWL_ASSUME || JPWL_ASSUME) {
5132 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5137 #endif /* USE_JPWL */
5139 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5146 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5148 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5151 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5158 * Updates the rates of the tcp.
5160 * @param p_stream the stream to write data to.
5161 * @param p_j2k J2K codec.
5162 * @param p_manager the user event manager.
5164 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5165 struct opj_stream_private *p_stream,
5166 struct opj_event_mgr * p_manager )
5168 opj_cp_v2_t * l_cp = 00;
5169 opj_image_t * l_image = 00;
5170 opj_tcp_v2_t * l_tcp = 00;
5171 opj_image_comp_t * l_img_comp = 00;
5174 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5175 OPJ_FLOAT32 * l_rates = 0;
5176 OPJ_FLOAT32 l_sot_remove;
5177 OPJ_UINT32 l_bits_empty, l_size_pixel;
5178 OPJ_UINT32 l_tile_size = 0;
5179 OPJ_UINT32 l_last_res;
5180 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5183 assert(p_j2k != 00);
5184 assert(p_manager != 00);
5185 assert(p_stream != 00);
5188 l_cp = &(p_j2k->m_cp);
5189 l_image = p_j2k->m_private_image;
5192 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5193 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5194 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5196 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5197 l_tp_stride_func = get_tp_stride;
5200 l_tp_stride_func = get_default_stride;
5203 for (i=0;i<l_cp->th;++i) {
5204 for (j=0;j<l_cp->tw;++j) {
5205 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5207 /* 4 borders of the tile rescale on the image if necessary */
5208 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5209 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5210 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5211 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5213 l_rates = l_tcp->rates;
5215 /* Modification of the RATE >> */
5217 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5219 ((*l_rates) * l_bits_empty)
5227 for (k = 1; k < l_tcp->numlayers; ++k) {
5229 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5231 ((*l_rates) * l_bits_empty)
5247 for (i=0;i<l_cp->th;++i) {
5248 for (j=0;j<l_cp->tw;++j) {
5249 l_rates = l_tcp->rates;
5252 *l_rates -= l_sot_remove;
5254 if (*l_rates < 30) {
5261 l_last_res = l_tcp->numlayers - 1;
5263 for (k = 1; k < l_last_res; ++k) {
5266 *l_rates -= l_sot_remove;
5268 if (*l_rates < *(l_rates - 1) + 10) {
5269 *l_rates = (*(l_rates - 1)) + 20;
5277 *l_rates -= (l_sot_remove + 2.f);
5279 if (*l_rates < *(l_rates - 1) + 10) {
5280 *l_rates = (*(l_rates - 1)) + 20;
5288 l_img_comp = l_image->comps;
5291 for (i=0;i<l_image->numcomps;++i) {
5292 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5294 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5302 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5304 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5306 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5307 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5308 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5309 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5313 if (l_cp->m_specific_param.m_enc.m_cinema) {
5314 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5315 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5316 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5320 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5321 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5327 static void j2k_read_eoc(opj_j2k_t *j2k) {
5329 opj_bool success = OPJ_FALSE;
5331 /* if packets should be decoded */
5332 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5333 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5334 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5335 for (i = 0; i < j2k->cp->tileno_size; i++) {
5336 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5337 if (j2k->cp->tileno[i] != -1)
5339 tileno = j2k->cp->tileno[i];
5340 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5341 opj_free(j2k->tile_data[tileno]);
5342 j2k->tile_data[tileno] = NULL;
5343 tcd_free_decode_tile(tcd, i);
5346 success = OPJ_FALSE;
5347 if (success == OPJ_FALSE) {
5348 j2k->state |= J2K_STATE_ERR;
5352 tcd_free_decode(tcd);
5355 /* if packets should not be decoded */
5357 for (i = 0; i < j2k->cp->tileno_size; i++) {
5358 tileno = j2k->cp->tileno[i];
5359 opj_free(j2k->tile_data[tileno]);
5360 j2k->tile_data[tileno] = NULL;
5363 if (j2k->state & J2K_STATE_ERR)
5364 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5366 j2k->state = J2K_STATE_MT;
5370 * Reads a EOC marker (End Of Codestream)
5372 * @param p_header_data the data contained in the SOD box.
5373 * @param p_j2k the jpeg2000 codec.
5374 * @param p_header_size the size of the data contained in the SOD marker.
5375 * @param p_manager the user event manager.
5378 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5379 struct opj_stream_private *p_stream,
5380 struct opj_event_mgr * p_manager )
5383 opj_tcd_v2_t * l_tcd = 00;
5384 OPJ_UINT32 l_nb_tiles;
5385 opj_tcp_v2_t * l_tcp = 00;
5389 assert(p_j2k != 00);
5390 assert(p_manager != 00);
5391 assert(p_stream != 00);
5393 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5394 l_tcp = p_j2k->m_cp.tcps;
5396 l_tcd = tcd_create_v2(OPJ_TRUE);
5398 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5402 for (i = 0; i < l_nb_tiles; ++i) {
5403 if (l_tcp->m_data) {
5404 if (! tcd_init_decode_tile(l_tcd, i)) {
5405 tcd_destroy_v2(l_tcd);
5406 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5410 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5414 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5419 j2k_tcp_destroy(l_tcp);
5423 tcd_destroy_v2(l_tcd);
5429 * Gets the offset of the header.
5431 * @param p_stream the stream to write data to.
5432 * @param p_j2k J2K codec.
5433 * @param p_manager the user event manager.
5435 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5436 struct opj_stream_private *p_stream,
5437 struct opj_event_mgr * p_manager )
5440 assert(p_j2k != 00);
5441 assert(p_manager != 00);
5442 assert(p_stream != 00);
5444 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5450 * Writes the MCT marker (Multiple Component Transform)
5452 * @param p_stream the stream to write data to.
5453 * @param p_j2k J2K codec.
5454 * @param p_manager the user event manager.
5456 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5457 struct opj_stream_private *p_stream,
5458 struct opj_event_mgr * p_manager )
5461 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5462 opj_mct_data_t * l_mct_record;
5463 opj_tcp_v2_t * l_tcp;
5466 assert(p_j2k != 00);
5467 assert(p_stream != 00);
5468 assert(p_manager != 00);
5470 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5474 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5475 l_mct_record = l_tcp->m_mct_records;
5477 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5479 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5486 l_mcc_record = l_tcp->m_mcc_records;
5488 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5490 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5497 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5505 * Writes the image components.
5507 * @param p_stream the stream to write data to.
5508 * @param p_j2k J2K codec.
5509 * @param p_manager the user event manager.
5511 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5512 struct opj_stream_private *p_stream,
5513 struct opj_event_mgr * p_manager )
5518 assert(p_j2k != 00);
5519 assert(p_manager != 00);
5520 assert(p_stream != 00);
5522 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5524 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5528 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5537 * Writes regions of interests.
5539 * @param p_stream the stream to write data to.
5540 * @param p_j2k J2K codec.
5541 * @param p_manager the user event manager.
5543 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5544 struct opj_stream_private *p_stream,
5545 struct opj_event_mgr * p_manager )
5548 const opj_tccp_t *l_tccp = 00;
5551 assert(p_j2k != 00);
5552 assert(p_manager != 00);
5553 assert(p_stream != 00);
5555 l_tccp = p_j2k->m_cp.tcps->tccps;
5557 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5558 if (l_tccp->roishift) {
5560 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5574 * @param p_stream the stream to write data to.
5575 * @param p_j2k J2K codec.
5576 * @param p_manager the user event manager.
5578 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5579 struct opj_stream_private *p_stream,
5580 struct opj_event_mgr * p_manager )
5582 opj_codestream_index_t * l_cstr_index = 00;
5585 assert(p_j2k != 00);
5586 assert(p_manager != 00);
5587 assert(p_stream != 00);
5589 l_cstr_index = p_j2k->cstr_index;
5591 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5593 /* The following adjustment is done to adjust the codestream size */
5594 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5595 /* the first bunch of bytes is not in the codestream */
5596 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5601 /* preparation of JPWL marker segments */
5605 /* encode according to JPWL */
5606 jpwl_encode(p_j2k, p_stream, image);
5610 assert( 0 && "TODO" );
5611 #endif /* USE_JPWL */
5616 typedef struct opj_dec_mstabent {
5619 /** value of the state when the marker can appear */
5621 /** action linked to the marker */
5622 void (*handler) (opj_j2k_t *j2k);
5623 } opj_dec_mstabent_t;
5625 opj_dec_mstabent_t j2k_dec_mstab[] = {
5626 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5627 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5628 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5629 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5630 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5631 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5632 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5633 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5634 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5635 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5636 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5637 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5638 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5639 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5640 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5641 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5643 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5644 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5647 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5648 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5649 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5650 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5651 #endif /* USE_JPWL */
5653 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5654 {J2K_MS_INSEC, 0, j2k_read_insec},
5655 #endif /* USE_JPSEC */
5657 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5660 static void j2k_read_unk(opj_j2k_t *j2k) {
5661 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5664 if (j2k->cp->correct) {
5666 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5667 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5668 id = cio_read(j2k->cio, 2);
5669 opj_event_msg(j2k->cinfo, EVT_ERROR,
5670 "JPWL: really don't know this marker %x\n",
5673 opj_event_msg(j2k->cinfo, EVT_ERROR,
5674 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5677 /* OK, activate this at your own risk!!! */
5678 /* we look for the marker at the minimum hamming distance from this */
5679 while (j2k_dec_mstab[m].id) {
5681 /* 1's where they differ */
5682 tmp_id = j2k_dec_mstab[m].id ^ id;
5684 /* compute the hamming distance between our id and the current */
5686 for (i = 0; i < 16; i++) {
5687 if ((tmp_id >> i) & 0x0001) {
5692 /* if current distance is smaller, set the minimum */
5693 if (cur_dist < min_dist) {
5694 min_dist = cur_dist;
5695 min_id = j2k_dec_mstab[m].id;
5698 /* jump to the next marker */
5702 /* do we substitute the marker? */
5703 if (min_dist < JPWL_MAXIMUM_HAMMING) {
5704 opj_event_msg(j2k->cinfo, EVT_ERROR,
5705 "- marker %x is at distance %d from the read %x\n",
5706 min_id, min_dist, id);
5707 opj_event_msg(j2k->cinfo, EVT_ERROR,
5708 "- trying to substitute in place and crossing fingers!\n");
5709 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5710 cio_write(j2k->cio, min_id, 2);
5713 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5718 #endif /* USE_JPWL */
5723 * Reads an unknown marker
5725 * @param p_stream the stream object to read from.
5726 * @param p_j2k the jpeg2000 codec.
5727 * @param p_manager the user event manager.
5729 * @return true if the marker could be deduced.
5731 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
5732 struct opj_stream_private *p_stream,
5733 OPJ_UINT32 *output_marker,
5734 struct opj_event_mgr * p_manager
5737 OPJ_UINT32 l_unknown_marker;
5738 const opj_dec_memory_marker_handler_t * l_marker_handler;
5739 OPJ_UINT32 l_size_unk = 2;
5742 assert(p_j2k != 00);
5743 assert(p_manager != 00);
5744 assert(p_stream != 00);
5746 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5749 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5750 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5751 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5755 /* read 2 bytes as the new marker ID*/
5756 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5758 if (!(l_unknown_marker < 0xff00)) {
5760 /* Get the marker handler from the marker ID*/
5761 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5763 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5764 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5768 if (l_marker_handler->id != J2K_MS_UNK) {
5769 /* Add the marker to the codestream index*/
5770 if (l_marker_handler->id != J2K_MS_SOT)
5771 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5772 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5774 break; /* next marker is known and well located */
5782 *output_marker = l_marker_handler->id ;
5788 Read the lookup table containing all the marker, status and action
5789 @param id Marker value
5791 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5792 opj_dec_mstabent_t *e;
5793 for (e = j2k_dec_mstab; e->id != 0; e++) {
5802 * Writes the MCT marker (Multiple Component Transform)
5804 * @param p_stream the stream to write data to.
5805 * @param p_j2k J2K codec.
5806 * @param p_manager the user event manager.
5808 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5809 opj_mct_data_t * p_mct_record,
5810 struct opj_stream_private *p_stream,
5811 struct opj_event_mgr * p_manager )
5813 OPJ_UINT32 l_mct_size;
5814 OPJ_BYTE * l_current_data = 00;
5818 assert(p_j2k != 00);
5819 assert(p_manager != 00);
5820 assert(p_stream != 00);
5822 l_mct_size = 10 + p_mct_record->m_data_size;
5824 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5825 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5826 = (OPJ_BYTE*)opj_realloc(
5827 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5830 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5834 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5837 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5839 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5840 l_current_data += 2;
5842 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5843 l_current_data += 2;
5845 opj_write_bytes(l_current_data,0,2); /* Zmct */
5846 l_current_data += 2;
5848 /* only one marker atm */
5849 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5851 opj_write_bytes(l_current_data,l_tmp,2);
5852 l_current_data += 2;
5854 opj_write_bytes(l_current_data,0,2); /* Ymct */
5857 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5859 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) {
5867 * Reads a MCT marker (Multiple Component Transform)
5869 * @param p_header_data the data contained in the MCT box.
5870 * @param p_j2k the jpeg2000 codec.
5871 * @param p_header_size the size of the data contained in the MCT marker.
5872 * @param p_manager the user event manager.
5874 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5875 OPJ_BYTE * p_header_data,
5876 OPJ_UINT32 p_header_size,
5877 struct opj_event_mgr * p_manager )
5880 opj_tcp_v2_t *l_tcp = 00;
5883 opj_mct_data_t * l_mct_data;
5886 assert(p_header_data != 00);
5887 assert(p_j2k != 00);
5889 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5890 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5891 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5893 if (p_header_size < 2) {
5894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5899 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5902 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5906 if(p_header_size <= 6) {
5907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5911 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5912 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5915 l_indix = l_tmp & 0xff;
5916 l_mct_data = l_tcp->m_mct_records;
5918 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5919 if (l_mct_data->m_index == l_indix) {
5926 if (i == l_tcp->m_nb_mct_records) {
5927 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5928 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5930 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));
5931 if(! l_tcp->m_mct_records) {
5932 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5936 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5937 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5940 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5943 if (l_mct_data->m_data) {
5944 opj_free(l_mct_data->m_data);
5945 l_mct_data->m_data = 00;
5948 l_mct_data->m_index = l_indix;
5949 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5950 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5952 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5955 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5961 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5962 if (! l_mct_data->m_data) {
5963 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5966 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5968 l_mct_data->m_data_size = p_header_size;
5969 ++l_tcp->m_nb_mct_records;
5975 * Writes the MCC marker (Multiple Component Collection)
5977 * @param p_stream the stream to write data to.
5978 * @param p_j2k J2K codec.
5979 * @param p_manager the user event manager.
5981 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5982 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5983 struct opj_stream_private *p_stream,
5984 struct opj_event_mgr * p_manager )
5987 OPJ_UINT32 l_mcc_size;
5988 OPJ_BYTE * l_current_data = 00;
5989 OPJ_UINT32 l_nb_bytes_for_comp;
5994 assert(p_j2k != 00);
5995 assert(p_manager != 00);
5996 assert(p_stream != 00);
5998 if (p_mcc_record->m_nb_comps > 255 ) {
5999 l_nb_bytes_for_comp = 2;
6003 l_nb_bytes_for_comp = 1;
6007 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6008 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6010 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6011 = (OPJ_BYTE*)opj_realloc(
6012 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6014 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6018 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6021 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6023 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6024 l_current_data += 2;
6026 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6027 l_current_data += 2;
6030 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6031 l_current_data += 2;
6033 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6036 /* only one marker atm */
6037 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6040 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6043 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6046 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 */
6049 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6050 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6051 l_current_data+=l_nb_bytes_for_comp;
6054 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 */
6057 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6059 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6060 l_current_data+=l_nb_bytes_for_comp;
6063 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6065 if (p_mcc_record->m_decorrelation_array) {
6066 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6069 if (p_mcc_record->m_offset_array) {
6070 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6073 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6076 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) {
6084 * Reads a MCC marker (Multiple Component Collection)
6086 * @param p_header_data the data contained in the MCC box.
6087 * @param p_j2k the jpeg2000 codec.
6088 * @param p_header_size the size of the data contained in the MCC marker.
6089 * @param p_manager the user event manager.
6091 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6092 OPJ_BYTE * p_header_data,
6093 OPJ_UINT32 p_header_size,
6094 struct opj_event_mgr * p_manager )
6099 opj_tcp_v2_t * l_tcp;
6100 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6101 opj_mct_data_t * l_mct_data;
6102 OPJ_UINT32 l_nb_collections;
6103 OPJ_UINT32 l_nb_comps;
6104 OPJ_UINT32 l_nb_bytes_by_comp;
6108 assert(p_header_data != 00);
6109 assert(p_j2k != 00);
6110 assert(p_manager != 00);
6112 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6113 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6114 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6116 if (p_header_size < 2) {
6117 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6122 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6125 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6129 if (p_header_size < 7) {
6130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6134 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6137 l_mcc_record = l_tcp->m_mcc_records;
6139 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6140 if (l_mcc_record->m_index == l_indix) {
6147 if (i == l_tcp->m_nb_mcc_records) {
6148 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6149 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6151 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6152 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6153 if (! l_tcp->m_mcc_records) {
6154 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6157 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6158 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));
6160 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6162 l_mcc_record->m_index = l_indix;
6164 /* only one marker atm */
6165 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6168 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6172 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6175 if (l_nb_collections > 1) {
6176 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6182 for (i=0;i<l_nb_collections;++i) {
6183 if (p_header_size < 3) {
6184 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6188 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6192 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6196 opj_read_bytes(p_header_data,&l_nb_comps,2);
6201 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6202 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6204 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6205 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6209 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6211 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6212 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6213 p_header_data+=l_nb_bytes_by_comp;
6216 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6221 opj_read_bytes(p_header_data,&l_nb_comps,2);
6224 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6225 l_nb_comps &= 0x7fff;
6227 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6228 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6232 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6233 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6237 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6239 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6240 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6241 p_header_data+=l_nb_bytes_by_comp;
6244 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6249 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6252 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6253 l_mcc_record->m_decorrelation_array = 00;
6254 l_mcc_record->m_offset_array = 00;
6256 l_indix = l_tmp & 0xff;
6258 l_mct_data = l_tcp->m_mct_records;
6259 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6260 if (l_mct_data->m_index == l_indix) {
6261 l_mcc_record->m_decorrelation_array = l_mct_data;
6267 if (l_mcc_record->m_decorrelation_array == 00) {
6268 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6273 l_indix = (l_tmp >> 8) & 0xff;
6275 l_mct_data = l_tcp->m_mct_records;
6276 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6277 if (l_mct_data->m_index == l_indix) {
6278 l_mcc_record->m_offset_array = l_mct_data;
6284 if (l_mcc_record->m_offset_array == 00) {
6285 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6291 if (p_header_size != 0) {
6292 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6296 ++l_tcp->m_nb_mcc_records;
6303 * Writes the MCO marker (Multiple component transformation ordering)
6305 * @param p_stream the stream to write data to.
6306 * @param p_j2k J2K codec.
6307 * @param p_manager the user event manager.
6309 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6310 struct opj_stream_private *p_stream,
6311 struct opj_event_mgr * p_manager
6314 OPJ_BYTE * l_current_data = 00;
6315 OPJ_UINT32 l_mco_size;
6316 opj_tcp_v2_t * l_tcp = 00;
6317 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6321 assert(p_j2k != 00);
6322 assert(p_manager != 00);
6323 assert(p_stream != 00);
6325 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6326 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6328 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6329 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6331 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6332 = (OPJ_BYTE*)opj_realloc(
6333 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6335 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6340 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6343 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6344 l_current_data += 2;
6346 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6347 l_current_data += 2;
6349 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6352 l_mcc_record = l_tcp->m_mcc_records;
6353 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6354 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6360 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) {
6368 * Reads a MCO marker (Multiple Component Transform Ordering)
6370 * @param p_header_data the data contained in the MCO box.
6371 * @param p_j2k the jpeg2000 codec.
6372 * @param p_header_size the size of the data contained in the MCO marker.
6373 * @param p_manager the user event manager.
6375 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6376 OPJ_BYTE * p_header_data,
6377 OPJ_UINT32 p_header_size,
6378 struct opj_event_mgr * p_manager )
6380 OPJ_UINT32 l_tmp, i;
6381 OPJ_UINT32 l_nb_stages;
6382 opj_tcp_v2_t * l_tcp;
6383 opj_tccp_t * l_tccp;
6384 opj_image_t * l_image;
6385 opj_image_comp_t * l_img_comp;
6388 assert(p_header_data != 00);
6389 assert(p_j2k != 00);
6390 assert(p_manager != 00);
6392 l_image = p_j2k->m_private_image;
6393 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6394 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6395 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6397 if (p_header_size < 1) {
6398 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6402 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6405 if (l_nb_stages > 1) {
6406 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6410 if (p_header_size != l_nb_stages + 1) {
6411 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6415 l_tccp = l_tcp->tccps;
6416 l_img_comp = l_image->comps;
6418 for (i=0;i<l_image->numcomps;++i) {
6419 l_tccp->m_dc_level_shift = 0;
6423 if (l_tcp->m_mct_decoding_matrix) {
6424 opj_free(l_tcp->m_mct_decoding_matrix);
6425 l_tcp->m_mct_decoding_matrix = 00;
6428 for (i=0;i<l_nb_stages;++i) {
6429 opj_read_bytes(p_header_data,&l_tmp,1);
6432 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6440 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6443 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6444 opj_mct_data_t * l_deco_array, * l_offset_array;
6445 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6446 OPJ_UINT32 l_nb_elem;
6447 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6448 opj_tccp_t * l_tccp;
6451 assert(p_tcp != 00);
6453 l_mcc_record = p_tcp->m_mcc_records;
6455 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6456 if (l_mcc_record->m_index == p_index) {
6461 if (i==p_tcp->m_nb_mcc_records) {
6462 /** element discarded **/
6466 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6467 /** do not support number of comps != image */
6471 l_deco_array = l_mcc_record->m_decorrelation_array;
6474 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6475 if (l_deco_array->m_data_size != l_data_size) {
6479 l_nb_elem = p_image->numcomps * p_image->numcomps;
6480 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6481 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6483 if (! p_tcp->m_mct_decoding_matrix ) {
6487 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);
6490 l_offset_array = l_mcc_record->m_offset_array;
6492 if (l_offset_array) {
6493 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6494 if (l_offset_array->m_data_size != l_data_size) {
6498 l_nb_elem = p_image->numcomps;
6499 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6500 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6502 if (! l_offset_data ) {
6506 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6508 l_tccp = p_tcp->tccps;
6509 l_current_offset_data = l_offset_data;
6511 for (i=0;i<p_image->numcomps;++i) {
6512 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6516 opj_free(l_offset_data);
6523 * Writes the CBD marker (Component bit depth definition)
6525 * @param p_stream the stream to write data to.
6526 * @param p_j2k J2K codec.
6527 * @param p_manager the user event manager.
6529 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6530 struct opj_stream_private *p_stream,
6531 struct opj_event_mgr * p_manager )
6534 OPJ_UINT32 l_cbd_size;
6535 OPJ_BYTE * l_current_data = 00;
6536 opj_image_t *l_image = 00;
6537 opj_image_comp_t * l_comp = 00;
6540 assert(p_j2k != 00);
6541 assert(p_manager != 00);
6542 assert(p_stream != 00);
6544 l_image = p_j2k->m_private_image;
6545 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6547 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6548 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6549 = (OPJ_BYTE*)opj_realloc(
6550 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6553 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6557 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6560 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6562 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6563 l_current_data += 2;
6565 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6566 l_current_data += 2;
6568 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6571 l_comp = l_image->comps;
6573 for (i=0;i<l_image->numcomps;++i) {
6574 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6580 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) {
6588 * Reads a CBD marker (Component bit depth definition)
6589 * @param p_header_data the data contained in the CBD box.
6590 * @param p_j2k the jpeg2000 codec.
6591 * @param p_header_size the size of the data contained in the CBD marker.
6592 * @param p_manager the user event manager.
6594 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6595 OPJ_BYTE * p_header_data,
6596 OPJ_UINT32 p_header_size,
6597 struct opj_event_mgr * p_manager)
6599 OPJ_UINT32 l_nb_comp,l_num_comp;
6600 OPJ_UINT32 l_comp_def;
6602 opj_image_comp_t * l_comp = 00;
6605 assert(p_header_data != 00);
6606 assert(p_j2k != 00);
6607 assert(p_manager != 00);
6609 l_num_comp = p_j2k->m_private_image->numcomps;
6611 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6612 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6616 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6619 if (l_nb_comp != l_num_comp) {
6620 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6624 l_comp = p_j2k->m_private_image->comps;
6625 for (i=0;i<l_num_comp;++i) {
6626 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6628 l_comp->sgnd = (l_comp_def>>7) & 1;
6629 l_comp->prec = (l_comp_def&0x7f) + 1;
6637 /* ----------------------------------------------------------------------- */
6638 /* J2K / JPT decoder interface */
6639 /* ----------------------------------------------------------------------- */
6643 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6646 if(j2k->tile_len != NULL) {
6647 opj_free(j2k->tile_len);
6649 if(j2k->tile_data != NULL) {
6650 opj_free(j2k->tile_data);
6652 if(j2k->default_tcp != NULL) {
6653 opj_tcp_t *default_tcp = j2k->default_tcp;
6654 if(default_tcp->ppt_data_first != NULL) {
6655 opj_free(default_tcp->ppt_data_first);
6657 if(j2k->default_tcp->tccps != NULL) {
6658 opj_free(j2k->default_tcp->tccps);
6660 opj_free(j2k->default_tcp);
6662 if(j2k->cp != NULL) {
6663 opj_cp_t *cp = j2k->cp;
6664 if(cp->tcps != NULL) {
6665 for(i = 0; i < cp->tw * cp->th; i++) {
6666 if(cp->tcps[i].ppt_data_first != NULL) {
6667 opj_free(cp->tcps[i].ppt_data_first);
6669 if(cp->tcps[i].tccps != NULL) {
6670 opj_free(cp->tcps[i].tccps);
6675 if(cp->ppm_data_first != NULL) {
6676 opj_free(cp->ppm_data_first);
6678 if(cp->tileno != NULL) {
6679 opj_free(cp->tileno);
6681 if(cp->comment != NULL) {
6682 opj_free(cp->comment);
6691 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6693 if(j2k && parameters) {
6694 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6695 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6698 j2k->m_cp.correct = parameters->jpwl_correct;
6699 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6700 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6701 #endif /* USE_JPWL */
6708 * Read a JPT-stream and decode file
6711 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6712 opj_image_t *image = NULL;
6713 opj_jpt_msg_header_t header;
6715 opj_common_ptr cinfo = j2k->cinfo;
6717 OPJ_ARG_NOT_USED(cstr_info);
6721 /* create an empty image */
6722 image = opj_image_create0();
6725 j2k->state = J2K_STATE_MHSOC;
6727 /* Initialize the header */
6728 jpt_init_msg_header(&header);
6729 /* Read the first header of the message */
6730 jpt_read_msg_header(cinfo, cio, &header);
6732 position = cio_tell(cio);
6733 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6734 opj_image_destroy(image);
6735 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6740 opj_dec_mstabent_t *e = NULL;
6743 if (!cio_numbytesleft(cio)) {
6747 /* data-bin read -> need to read a new header */
6748 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6749 jpt_read_msg_header(cinfo, cio, &header);
6750 position = cio_tell(cio);
6751 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6752 opj_image_destroy(image);
6753 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6758 id = cio_read(cio, 2);
6759 if (id >> 8 != 0xff) {
6760 opj_image_destroy(image);
6761 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6764 e = j2k_dec_mstab_lookup(id);
6765 if (!(j2k->state & e->states)) {
6766 opj_image_destroy(image);
6767 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6773 if (j2k->state == J2K_STATE_MT) {
6776 if (j2k->state == J2K_STATE_NEOC) {
6780 if (j2k->state == J2K_STATE_NEOC) {
6784 if (j2k->state != J2K_STATE_MT) {
6785 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6791 /* ----------------------------------------------------------------------- */
6792 /* J2K encoder interface */
6793 /* ----------------------------------------------------------------------- */
6795 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6796 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6803 opj_j2k_v2_t* j2k_create_compress_v2(void)
6805 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6810 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6812 l_j2k->m_is_decoder = 0;
6813 l_j2k->m_cp.m_is_decoder = 0;
6815 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6816 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6821 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6823 /* validation list creation*/
6824 l_j2k->m_validation_list = opj_procedure_list_create();
6825 if (! l_j2k->m_validation_list) {
6830 /* execution list creation*/
6831 l_j2k->m_procedure_list = opj_procedure_list_create();
6832 if (! l_j2k->m_procedure_list) {
6840 void j2k_destroy_compress(opj_j2k_t *j2k) {
6844 if(j2k->cp != NULL) {
6845 opj_cp_t *cp = j2k->cp;
6848 opj_free(cp->comment);
6851 opj_free(cp->matrice);
6853 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6854 opj_free(cp->tcps[tileno].tccps);
6863 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6865 int tileno, numpocs_tile;
6866 opj_cp_t *cp = NULL;
6868 if(!j2k || !parameters || ! image) {
6872 /* create and initialize the coding parameters structure */
6873 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6875 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6878 /* set default values for cp */
6883 copy user encoding parameters
6885 cp->cinema = parameters->cp_cinema;
6886 cp->max_comp_size = parameters->max_comp_size;
6887 cp->rsiz = parameters->cp_rsiz;
6888 cp->disto_alloc = parameters->cp_disto_alloc;
6889 cp->fixed_alloc = parameters->cp_fixed_alloc;
6890 cp->fixed_quality = parameters->cp_fixed_quality;
6892 /* mod fixed_quality */
6893 if(parameters->cp_matrice) {
6894 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6895 cp->matrice = (int *) opj_malloc(array_size);
6896 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6900 cp->tdx = parameters->cp_tdx;
6901 cp->tdy = parameters->cp_tdy;
6904 cp->tx0 = parameters->cp_tx0;
6905 cp->ty0 = parameters->cp_ty0;
6907 /* comment string */
6908 if(parameters->cp_comment) {
6909 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6911 strcpy(cp->comment, parameters->cp_comment);
6916 calculate other encoding parameters
6919 if (parameters->tile_size_on) {
6920 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6921 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6923 cp->tdx = image->x1 - cp->tx0;
6924 cp->tdy = image->y1 - cp->ty0;
6927 if(parameters->tp_on){
6928 cp->tp_flag = parameters->tp_flag;
6933 for(i=0;i<image->numcomps ;i++){
6934 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6940 calculate JPWL encoding parameters
6943 if (parameters->jpwl_epc_on) {
6947 cp->epc_on = OPJ_TRUE;
6948 cp->info_on = OPJ_FALSE; /* no informative technique */
6951 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6952 cp->epb_on = OPJ_TRUE;
6954 cp->hprot_MH = parameters->jpwl_hprot_MH;
6955 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6956 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6957 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6959 /* if tile specs are not specified, copy MH specs */
6960 if (cp->hprot_TPH[0] == -1) {
6961 cp->hprot_TPH_tileno[0] = 0;
6962 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6964 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6965 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6966 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6967 cp->pprot[i] = parameters->jpwl_pprot[i];
6971 /* set ESD writing */
6972 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6973 cp->esd_on = OPJ_TRUE;
6975 cp->sens_size = parameters->jpwl_sens_size;
6976 cp->sens_addr = parameters->jpwl_sens_addr;
6977 cp->sens_range = parameters->jpwl_sens_range;
6979 cp->sens_MH = parameters->jpwl_sens_MH;
6980 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6981 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6982 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6986 /* always set RED writing to false: we are at the encoder */
6987 cp->red_on = OPJ_FALSE;
6990 cp->epc_on = OPJ_FALSE;
6992 #endif /* USE_JPWL */
6995 /* initialize the mutiple tiles */
6996 /* ---------------------------- */
6997 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6999 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7000 opj_tcp_t *tcp = &cp->tcps[tileno];
7001 tcp->numlayers = parameters->tcp_numlayers;
7002 assert ( tcp->numlayers >= 0 );
7003 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7005 if (cp->fixed_quality) {
7006 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7008 tcp->rates[j] = parameters->tcp_rates[j];
7010 if (cp->fixed_quality) { /* add fixed_quality */
7011 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7013 tcp->rates[j] = parameters->tcp_rates[j];
7017 tcp->csty = parameters->csty;
7018 tcp->prg = parameters->prog_order;
7019 tcp->mct = parameters->tcp_mct;
7023 if (parameters->numpocs) {
7024 /* initialisation of POC */
7026 assert( parameters->numpocs >= 0 );
7027 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7028 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7029 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7030 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7031 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7032 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7033 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7034 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7035 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7036 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7040 tcp->numpocs = numpocs_tile -1 ;
7045 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7047 for (i = 0; i < image->numcomps; i++) {
7048 opj_tccp_t *tccp = &tcp->tccps[i];
7049 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7050 tccp->numresolutions = parameters->numresolution;
7051 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7052 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7053 tccp->cblksty = parameters->mode;
7054 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7055 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7057 assert(parameters->roi_compno >= 0);
7058 if (i == (OPJ_UINT32)parameters->roi_compno) {
7059 tccp->roishift = parameters->roi_shift;
7064 if(parameters->cp_cinema)
7066 /*Precinct size for lowest frequency subband=128*/
7069 /*Precinct size at all other resolutions = 256*/
7070 for (j = 1; j < tccp->numresolutions; j++) {
7075 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7077 assert(tccp->numresolutions > 0);
7078 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7079 if (p < parameters->res_spec) {
7081 if (parameters->prcw_init[p] < 1) {
7084 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7087 if (parameters->prch_init[p] < 1) {
7090 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7094 int res_spec = parameters->res_spec;
7095 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7096 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7098 if (size_prcw < 1) {
7101 tccp->prcw[j] = int_floorlog2(size_prcw);
7104 if (size_prch < 1) {
7107 tccp->prch[j] = int_floorlog2(size_prch);
7111 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7114 for (j = 0; j < tccp->numresolutions; j++) {
7121 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7126 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7127 opj_cparameters_t *parameters,
7129 struct opj_event_mgr * p_manager)
7131 OPJ_UINT32 i, j, tileno, numpocs_tile;
7132 opj_cp_v2_t *cp = 00;
7135 if(!p_j2k || !parameters || ! image) {
7139 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7140 cp = &(p_j2k->m_cp);
7142 /* set default values for cp */
7147 copy user encoding parameters
7149 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7150 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7151 cp->rsiz = parameters->cp_rsiz;
7152 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7153 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7154 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7156 /* mod fixed_quality */
7157 if (parameters->cp_matrice) {
7158 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7159 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7160 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7164 cp->tdx = parameters->cp_tdx;
7165 cp->tdy = parameters->cp_tdy;
7168 cp->tx0 = parameters->cp_tx0;
7169 cp->ty0 = parameters->cp_ty0;
7171 /* comment string */
7172 if(parameters->cp_comment) {
7173 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7175 strcpy(cp->comment, parameters->cp_comment);
7180 calculate other encoding parameters
7183 if (parameters->tile_size_on) {
7184 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7185 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7187 cp->tdx = image->x1 - cp->tx0;
7188 cp->tdy = image->y1 - cp->ty0;
7191 if (parameters->tp_on) {
7192 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7193 cp->m_specific_param.m_enc.m_tp_on = 1;
7198 calculate JPWL encoding parameters
7201 if (parameters->jpwl_epc_on) {
7205 cp->epc_on = OPJ_TRUE;
7206 cp->info_on = OPJ_FALSE; /* no informative technique */
7209 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7210 cp->epb_on = OPJ_TRUE;
7212 cp->hprot_MH = parameters->jpwl_hprot_MH;
7213 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7214 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7215 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7217 /* if tile specs are not specified, copy MH specs */
7218 if (cp->hprot_TPH[0] == -1) {
7219 cp->hprot_TPH_tileno[0] = 0;
7220 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7222 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7223 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7224 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7225 cp->pprot[i] = parameters->jpwl_pprot[i];
7229 /* set ESD writing */
7230 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7231 cp->esd_on = OPJ_TRUE;
7233 cp->sens_size = parameters->jpwl_sens_size;
7234 cp->sens_addr = parameters->jpwl_sens_addr;
7235 cp->sens_range = parameters->jpwl_sens_range;
7237 cp->sens_MH = parameters->jpwl_sens_MH;
7238 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7239 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7240 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7244 /* always set RED writing to false: we are at the encoder */
7245 cp->red_on = OPJ_FALSE;
7248 cp->epc_on = OPJ_FALSE;
7250 #endif /* USE_JPWL */
7253 /* initialize the mutiple tiles */
7254 /* ---------------------------- */
7255 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7256 if (parameters->numpocs) {
7257 /* initialisation of POC */
7258 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7262 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7263 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7264 tcp->numlayers = parameters->tcp_numlayers;
7266 for (j = 0; j < tcp->numlayers; j++) {
7267 if(cp->m_specific_param.m_enc.m_cinema){
7268 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7269 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7271 tcp->rates[j] = parameters->tcp_rates[j];
7273 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7274 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7276 tcp->rates[j] = parameters->tcp_rates[j];
7281 tcp->csty = parameters->csty;
7282 tcp->prg = parameters->prog_order;
7283 tcp->mct = parameters->tcp_mct;
7288 if (parameters->numpocs) {
7289 /* initialisation of POC */
7292 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7293 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7294 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7296 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7297 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7298 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7299 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7300 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7301 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7302 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7308 tcp->numpocs = numpocs_tile -1 ;
7313 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7315 if (parameters->mct_data) {
7317 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7320 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7321 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7322 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7323 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7326 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7327 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7328 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7330 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7331 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7333 tcp->mct_norms = (OPJ_FLOAT64*)
7334 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7336 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7339 for (i = 0; i < image->numcomps; i++) {
7340 opj_tccp_t *tccp = &tcp->tccps[i];
7341 tccp->m_dc_level_shift = l_dc_shift[i];
7344 j2k_setup_mct_encoding(tcp,image);
7348 for (i = 0; i < image->numcomps; i++) {
7349 opj_tccp_t *tccp = &tcp->tccps[i];
7350 opj_image_comp_t * l_comp = &(image->comps[i]);
7352 if (! l_comp->sgnd) {
7353 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7358 for (i = 0; i < image->numcomps; i++) {
7359 opj_tccp_t *tccp = &tcp->tccps[i];
7361 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7362 tccp->numresolutions = parameters->numresolution;
7363 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7364 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7365 tccp->cblksty = parameters->mode;
7366 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7367 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7370 if (i == parameters->roi_compno) {
7371 tccp->roishift = parameters->roi_shift;
7376 if(parameters->cp_cinema) {
7377 //Precinct size for lowest frequency subband=128
7380 //Precinct size at all other resolutions = 256
7381 for (j = 1; j < tccp->numresolutions; j++) {
7386 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7387 OPJ_INT32 p = 0, it_res;
7388 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7389 if (p < parameters->res_spec) {
7391 if (parameters->prcw_init[p] < 1) {
7392 tccp->prcw[it_res] = 1;
7394 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7397 if (parameters->prch_init[p] < 1) {
7398 tccp->prch[it_res] = 1;
7400 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7404 int res_spec = parameters->res_spec;
7405 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7406 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7408 if (size_prcw < 1) {
7409 tccp->prcw[it_res] = 1;
7411 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7414 if (size_prch < 1) {
7415 tccp->prch[it_res] = 1;
7417 tccp->prch[it_res] = int_floorlog2(size_prch);
7421 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7424 for (j = 0; j < tccp->numresolutions; j++) {
7431 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7435 if (parameters->mct_data) {
7436 opj_free(parameters->mct_data);
7437 parameters->mct_data = 00;
7442 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7445 opj_cp_t *cp = NULL;
7447 opj_tcd_t *tcd = NULL; /* TCD component */
7455 j2k->cstr_info = cstr_info;
7458 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7459 cstr_info->image_w = image->x1 - image->x0;
7460 cstr_info->image_h = image->y1 - image->y0;
7461 cstr_info->prog = (&cp->tcps[0])->prg;
7462 cstr_info->tw = cp->tw;
7463 cstr_info->th = cp->th;
7464 cstr_info->tile_x = cp->tdx; /* new version parser */
7465 cstr_info->tile_y = cp->tdy; /* new version parser */
7466 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7467 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7468 cstr_info->numcomps = image->numcomps;
7469 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7470 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7471 for (compno=0; compno < image->numcomps; compno++) {
7472 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7474 cstr_info->D_max = 0.0; /* ADD Marcela */
7475 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7476 cstr_info->maxmarknum = 100;
7477 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7478 cstr_info->marknum = 0;
7488 for (compno = 1; compno < image->numcomps; compno++) {
7489 j2k_write_coc(j2k, compno);
7490 j2k_write_qcc(j2k, compno);
7494 for (compno = 0; compno < image->numcomps; compno++) {
7495 opj_tcp_t *tcp = &cp->tcps[0];
7496 if (tcp->tccps[compno].roishift)
7497 j2k_write_rgn(j2k, compno, 0);
7499 if (cp->comment != NULL) {
7503 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7507 if (cp->cinema == CINEMA4K_24) {
7512 /* uncomment only for testing JPSEC marker writing */
7513 /* j2k_write_sec(j2k); */
7517 cstr_info->main_head_end = cio_tell(cio) - 1;
7520 /**** Main Header ENDS here ***/
7522 /* create the tile encoder */
7523 tcd = tcd_create(j2k->cinfo);
7525 /* encode each tile */
7526 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7530 int acc_pack_num = 0;
7534 opj_tcp_t *tcp = &cp->tcps[tileno];
7535 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7537 j2k->curtileno = tileno;
7538 j2k->cur_tp_num = 0;
7539 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7540 /* initialisation before tile encoding */
7542 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7544 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7549 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7550 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7551 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7552 cstr_info->tile[j2k->curtileno].marknum = 0;
7556 for(pino = 0; pino <= tcp->numpocs; pino++) {
7560 /*Get number of tile parts*/
7561 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7562 tcd->tp_pos = cp->tp_pos;
7564 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7565 j2k->tp_num = tilepartno;
7568 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7569 cio_tell(cio) + j2k->pos_correction;
7573 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7574 for (compno = 1; compno < image->numcomps; compno++) {
7575 j2k_write_coc(j2k, compno);
7576 j2k_write_qcc(j2k, compno);
7578 if (cp->tcps[tileno].numpocs) {
7585 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7586 cio_tell(cio) + j2k->pos_correction + 1;
7589 j2k_write_sod(j2k, tcd);
7593 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7594 cio_tell(cio) + j2k->pos_correction - 1;
7595 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7597 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7598 cstr_info->packno - acc_pack_num;
7599 acc_pack_num = cstr_info->packno;
7607 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7612 if (tile->PPT) { // BAD PPT !!!
7615 PPT_file=fopen("PPT","rb");
7616 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7617 for (i=0;i<tile->len_ppt;i++) {
7619 fread(&elmt, 1, 1, PPT_file);
7620 fwrite(&elmt,1,1,f);
7629 /* destroy the tile encoder */
7630 tcd_free_encode(tcd);
7633 opj_free(j2k->cur_totnum_tp);
7638 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7640 /* The following adjustment is done to adjust the codestream size */
7641 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7642 /* the first bunch of bytes is not in the codestream */
7643 cstr_info->codestream_size -= cstr_info->main_head_start;
7649 preparation of JPWL marker segments
7653 /* encode according to JPWL */
7654 jpwl_encode(j2k, cio, image);
7657 #endif /* USE_JPWL */
7662 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7664 assert(cstr_info != 00);
7666 /* expand the list? */
7667 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7668 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7669 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7672 /* add the marker */
7673 cstr_info->marker[cstr_info->marknum].type = type;
7674 cstr_info->marker[cstr_info->marknum].pos = pos;
7675 cstr_info->marker[cstr_info->marknum].len = len;
7676 cstr_info->marknum++;
7680 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7682 assert(cstr_index != 00);
7684 /* expand the list? */
7685 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7686 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7687 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7690 /* add the marker */
7691 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7692 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7693 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7694 cstr_index->marknum++;
7698 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7700 opj_marker_info_t *marker;
7702 assert(cstr_info != 00);
7704 /* expand the list? */
7705 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7706 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7707 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7710 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7712 /* add the marker */
7713 marker->type = type;
7716 cstr_info->tile[tileno].marknum++;
7719 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)
7721 assert(cstr_index != 00);
7722 assert(cstr_index->tile_index != 00);
7724 /* expand the list? */
7725 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7726 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7727 cstr_index->tile_index[tileno].marker =
7728 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7729 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7732 /* add the marker */
7733 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7734 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7735 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7736 cstr_index->tile_index[tileno].marknum++;
7738 if (type == J2K_MS_SOT) {
7739 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7741 if (cstr_index->tile_index[tileno].tp_index)
7742 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7749 * -----------------------------------------------------------------------
7750 * -----------------------------------------------------------------------
7751 * -----------------------------------------------------------------------
7755 * Ends the decompression procedures and possibiliy add data to be read after the
7758 opj_bool j2k_end_decompress(
7759 opj_j2k_v2_t *p_j2k,
7760 opj_stream_private_t *p_stream,
7761 opj_event_mgr_t * p_manager)
7770 * Reads a jpeg2000 codestream header structure.
7773 * @param p_stream the stream to read data from.
7774 * @param p_j2k the jpeg2000 codec.
7775 * @param p_manager the user event manager.
7777 * @return true if the box is valid.
7779 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7780 opj_j2k_v2_t* p_j2k,
7781 opj_image_t** p_image,
7782 struct opj_event_mgr* p_manager )
7785 assert(p_j2k != 00);
7786 assert(p_stream != 00);
7787 assert(p_manager != 00);
7789 /* create an empty image header */
7790 p_j2k->m_private_image = opj_image_create0();
7791 if (! p_j2k->m_private_image) {
7795 /* customization of the validation */
7796 j2k_setup_decoding_validation(p_j2k);
7798 /* validation of the parameters codec */
7799 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7800 opj_image_destroy(p_j2k->m_private_image);
7801 p_j2k->m_private_image = NULL;
7805 /* customization of the encoding */
7806 j2k_setup_header_reading(p_j2k);
7809 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7810 opj_image_destroy(p_j2k->m_private_image);
7811 p_j2k->m_private_image = NULL;
7815 *p_image = opj_image_create0();
7820 /* Copy codestream image information to the output image */
7821 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7823 /*Allocate and initialize some elements of codestrem index*/
7824 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7832 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7834 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7837 assert(p_j2k != 00);
7839 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7841 /* DEVELOPER CORNER, add your custom procedures */
7842 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7847 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7848 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7850 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7853 assert(p_j2k != 00);
7855 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7856 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7857 /* DEVELOPER CORNER, add your custom validation procedure */
7863 * The mct encoding validation procedure.
7865 * @param p_j2k the jpeg2000 codec to validate.
7866 * @param p_stream the input stream to validate.
7867 * @param p_manager the user event manager.
7869 * @return true if the parameters are correct.
7871 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7872 opj_stream_private_t *p_stream,
7873 opj_event_mgr_t * p_manager )
7875 opj_bool l_is_valid = OPJ_TRUE;
7879 assert(p_j2k != 00);
7880 assert(p_stream != 00);
7881 assert(p_manager != 00);
7883 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7884 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7885 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7887 for (i=0;i<l_nb_tiles;++i) {
7888 if (l_tcp->mct == 2) {
7889 opj_tccp_t * l_tccp = l_tcp->tccps;
7890 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7892 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7893 l_is_valid &= ! (l_tccp->qmfbid & 1);
7904 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7907 OPJ_UINT32 l_indix = 1;
7908 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7909 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7910 OPJ_UINT32 l_mct_size,l_nb_elem;
7911 OPJ_FLOAT32 * l_data, * l_current_data;
7912 opj_tccp_t * l_tccp;
7915 assert(p_tcp != 00);
7917 if (p_tcp->mct != 2) {
7921 if (p_tcp->m_mct_decoding_matrix) {
7922 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7923 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7925 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));
7926 if (! p_tcp->m_mct_records) {
7929 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7931 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7933 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7935 if (l_mct_deco_data->m_data) {
7936 opj_free(l_mct_deco_data->m_data);
7937 l_mct_deco_data->m_data = 00;
7940 l_mct_deco_data->m_index = l_indix++;
7941 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7942 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7943 l_nb_elem = p_image->numcomps * p_image->numcomps;
7944 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7945 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7947 if (! l_mct_deco_data->m_data) {
7951 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);
7953 l_mct_deco_data->m_data_size = l_mct_size;
7954 ++p_tcp->m_nb_mct_records;
7957 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7958 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7959 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));
7961 if (! p_tcp->m_mct_records) {
7965 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7966 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7968 if (l_mct_deco_data) {
7969 l_mct_deco_data = l_mct_offset_data - 1;
7973 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7975 if (l_mct_offset_data->m_data) {
7976 opj_free(l_mct_offset_data->m_data);
7977 l_mct_offset_data->m_data = 00;
7980 l_mct_offset_data->m_index = l_indix++;
7981 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7982 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7983 l_nb_elem = p_image->numcomps;
7984 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7985 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7987 if (! l_mct_offset_data->m_data) {
7991 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7993 opj_free(l_mct_offset_data->m_data);
7994 l_mct_offset_data->m_data = 00;
7998 l_tccp = p_tcp->tccps;
7999 l_current_data = l_data;
8001 for (i=0;i<l_nb_elem;++i) {
8002 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8006 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8010 l_mct_offset_data->m_data_size = l_mct_size;
8012 ++p_tcp->m_nb_mct_records;
8014 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8015 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8016 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8017 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8019 if (! p_tcp->m_mcc_records) {
8022 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8023 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));
8027 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8028 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8029 l_mcc_data->m_is_irreversible = 1;
8030 l_mcc_data->m_nb_comps = p_image->numcomps;
8031 l_mcc_data->m_index = l_indix++;
8032 l_mcc_data->m_offset_array = l_mct_offset_data;
8033 ++p_tcp->m_nb_mcc_records;
8039 * Builds the cp decoder parameters to use to decode tile.
8041 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8042 opj_stream_private_t *p_stream,
8043 opj_event_mgr_t * p_manager )
8045 /* add here initialization of cp
8046 copy paste of setup_decoder */
8054 * Builds the cp encoder parameters to use to encode tile.
8056 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8057 opj_stream_private_t *p_stream,
8058 opj_event_mgr_t * p_manager )
8060 /* add here initialization of cp
8061 copy paste of setup_encoder */
8069 * The default encoding validation procedure without any extension.
8071 * @param p_j2k the jpeg2000 codec to validate.
8072 * @param p_stream the input stream to validate.
8073 * @param p_manager the user event manager.
8075 * @return true if the parameters are correct.
8077 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8078 opj_stream_private_t *p_stream,
8079 opj_event_mgr_t * p_manager )
8081 opj_bool l_is_valid = OPJ_TRUE;
8084 assert(p_j2k != 00);
8085 assert(p_stream != 00);
8086 assert(p_manager != 00);
8088 /* STATE checking */
8089 /* make sure the state is at 0 */
8090 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8092 /* POINTER validation */
8093 /* make sure a p_j2k codec is present */
8094 l_is_valid &= (p_j2k->m_procedure_list != 00);
8095 /* make sure a validation list is present */
8096 l_is_valid &= (p_j2k->m_validation_list != 00);
8098 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8099 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8103 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8104 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8108 /* PARAMETER VALIDATION */
8113 * The default decoding validation procedure without any extension.
8115 * @param p_j2k the jpeg2000 codec to validate.
8116 * @param p_stream the input stream to validate.
8117 * @param p_manager the user event manager.
8119 * @return true if the parameters are correct.
8121 opj_bool j2k_decoding_validation (
8122 opj_j2k_v2_t *p_j2k,
8123 opj_stream_private_t *p_stream,
8124 opj_event_mgr_t * p_manager
8127 opj_bool l_is_valid = OPJ_TRUE;
8130 assert(p_j2k != 00);
8131 assert(p_stream != 00);
8132 assert(p_manager != 00);
8135 /* STATE checking */
8136 /* make sure the state is at 0 */
8138 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8140 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8142 /* POINTER validation */
8143 /* make sure a p_j2k codec is present */
8144 /* make sure a procedure list is present */
8145 l_is_valid &= (p_j2k->m_procedure_list != 00);
8146 /* make sure a validation list is present */
8147 l_is_valid &= (p_j2k->m_validation_list != 00);
8149 /* PARAMETER VALIDATION */
8153 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8154 struct opj_stream_private *p_stream,
8155 struct opj_event_mgr * p_manager)
8157 OPJ_UINT32 l_current_marker;
8158 OPJ_UINT32 l_marker_size;
8159 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8162 assert(p_stream != 00);
8163 assert(p_j2k != 00);
8164 assert(p_manager != 00);
8166 /* We enter in the main header */
8167 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8169 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8170 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8171 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8175 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8176 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8177 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8181 /* Read 2 bytes as the new marker ID */
8182 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8184 /* Try to read until the SOT is detected */
8185 while (l_current_marker != J2K_MS_SOT) {
8187 /* Check if the current marker ID is valid */
8188 if (l_current_marker < 0xff00) {
8189 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8193 /* Get the marker handler from the marker ID */
8194 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8196 /* Manage case where marker is unknown */
8197 if (l_marker_handler->id == J2K_MS_UNK) {
8198 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8199 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8203 if (l_current_marker == J2K_MS_SOT)
8204 break; /* SOT marker is detected main header is completely read */
8205 else /* Get the marker handler from the marker ID */
8206 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8209 /* Check if the marker is known and if it is the right place to find it */
8210 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8211 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8215 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8216 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8217 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8221 /* read 2 bytes as the marker size */
8222 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8223 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8225 /* Check if the marker size is compatible with the header data size */
8226 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8227 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8228 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8229 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8232 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8235 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8236 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) {
8237 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8241 /* Read the marker segment with the correct marker handler */
8242 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8243 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8247 /* Add the marker to the codestream index*/
8248 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8249 l_marker_handler->id,
8250 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8251 l_marker_size + 4 );
8253 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8254 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8255 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8259 /* read 2 bytes as the new marker ID */
8260 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8263 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8265 /* Position of the last element if the main header */
8266 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8268 /* Next step: read a tile-part header */
8269 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8275 * Excutes the given procedures on the given codec.
8277 * @param p_procedure_list the list of procedures to execute
8278 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8279 * @param p_stream the stream to execute the procedures on.
8280 * @param p_manager the user manager.
8282 * @return true if all the procedures were successfully executed.
8284 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8285 opj_procedure_list_t * p_procedure_list,
8286 opj_stream_private_t *p_stream,
8287 opj_event_mgr_t * p_manager )
8289 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8290 opj_bool l_result = OPJ_TRUE;
8291 OPJ_UINT32 l_nb_proc, i;
8294 assert(p_procedure_list != 00);
8295 assert(p_j2k != 00);
8296 assert(p_stream != 00);
8297 assert(p_manager != 00);
8300 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8301 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);
8303 for (i=0;i<l_nb_proc;++i) {
8304 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8308 /* and clear the procedure list at the end.*/
8309 opj_procedure_list_clear(p_procedure_list);
8314 opj_bool j2k_copy_default_tcp_and_create_tcd
8316 opj_j2k_v2_t * p_j2k,
8317 opj_stream_private_t *p_stream,
8318 opj_event_mgr_t * p_manager
8321 opj_tcp_v2_t * l_tcp = 00;
8322 opj_tcp_v2_t * l_default_tcp = 00;
8323 OPJ_UINT32 l_nb_tiles;
8325 opj_tccp_t *l_current_tccp = 00;
8326 OPJ_UINT32 l_tccp_size;
8327 OPJ_UINT32 l_mct_size;
8328 opj_image_t * l_image;
8329 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8330 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8331 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8332 OPJ_UINT32 l_offset;
8335 assert(p_j2k != 00);
8336 assert(p_stream != 00);
8337 assert(p_manager != 00);
8339 l_image = p_j2k->m_private_image;
8340 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8341 l_tcp = p_j2k->m_cp.tcps;
8342 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8343 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8344 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8347 for (i=0; i<l_nb_tiles; ++i) {
8348 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8349 l_current_tccp = l_tcp->tccps;
8350 /*Copy default coding parameters into the current tile coding parameters*/
8351 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8352 /* Initialize some values of the current tile coding parameters*/
8354 l_tcp->ppt_data = 00;
8355 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8356 l_tcp->tccps = l_current_tccp;
8358 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8359 if (l_default_tcp->m_mct_decoding_matrix) {
8360 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8361 if (! l_tcp->m_mct_decoding_matrix ) {
8364 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8367 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8368 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8369 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8370 if (! l_tcp->m_mct_records) {
8373 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8375 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8376 l_src_mct_rec = l_default_tcp->m_mct_records;
8377 l_dest_mct_rec = l_tcp->m_mct_records;
8379 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8381 if (l_src_mct_rec->m_data) {
8383 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8384 if(! l_dest_mct_rec->m_data) {
8387 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8394 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8395 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8396 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8397 if (! l_tcp->m_mcc_records) {
8400 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8402 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8403 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8404 l_dest_mcc_rec = l_tcp->m_mcc_records;
8406 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8408 if (l_src_mcc_rec->m_decorrelation_array) {
8409 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8410 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8413 if (l_src_mcc_rec->m_offset_array) {
8414 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8415 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8422 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8423 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8425 /* Move to next tile cp*/
8429 /* Create the current tile decoder*/
8430 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8431 if (! p_j2k->m_tcd ) {
8435 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8436 tcd_destroy_v2(p_j2k->m_tcd);
8438 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8446 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8447 * with the marker value.
8448 * @param p_id Marker value to look up
8450 * @return the handler associated with the id.
8452 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8454 const opj_dec_memory_marker_handler_t *e;
8455 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8456 if (e->id == p_id) {
8457 break; /* we find a handler corresponding to the marker ID*/
8465 * Destroys a jpeg2000 codec.
8467 * @param p_j2k the jpeg20000 structure to destroy.
8469 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8475 if (p_j2k->m_is_decoder) {
8477 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8478 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8479 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8480 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8483 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8484 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8485 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8486 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8491 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8492 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8493 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8496 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8497 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8498 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8499 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8502 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8503 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8504 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8505 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8509 tcd_destroy_v2(p_j2k->m_tcd);
8511 j2k_cp_destroy(&(p_j2k->m_cp));
8512 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8514 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8515 p_j2k->m_procedure_list = 00;
8517 opj_procedure_list_destroy(p_j2k->m_validation_list);
8518 p_j2k->m_procedure_list = 00;
8520 j2k_destroy_cstr_index(p_j2k->cstr_index);
8521 p_j2k->cstr_index = NULL;
8523 opj_image_destroy(p_j2k->m_private_image);
8524 p_j2k->m_private_image = NULL;
8526 opj_image_destroy(p_j2k->m_output_image);
8527 p_j2k->m_output_image = NULL;
8532 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8536 if (p_cstr_ind->marker) {
8537 opj_free(p_cstr_ind->marker);
8538 p_cstr_ind->marker = NULL;
8541 if (p_cstr_ind->tile_index) {
8542 OPJ_UINT32 it_tile = 0;
8544 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8546 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8547 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8548 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8551 if(p_cstr_ind->tile_index[it_tile].tp_index){
8552 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8553 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8556 if(p_cstr_ind->tile_index[it_tile].marker){
8557 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8558 p_cstr_ind->tile_index[it_tile].marker = NULL;
8563 opj_free( p_cstr_ind->tile_index);
8564 p_cstr_ind->tile_index = NULL;
8567 opj_free(p_cstr_ind);
8574 * Destroys a tile coding parameter structure.
8576 * @param p_tcp the tile coding parameter to destroy.
8578 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8584 if (p_tcp->ppt_buffer != 00) {
8585 opj_free(p_tcp->ppt_buffer);
8586 p_tcp->ppt_buffer = 00;
8589 if (p_tcp->tccps != 00) {
8590 opj_free(p_tcp->tccps);
8594 if (p_tcp->m_mct_coding_matrix != 00) {
8595 opj_free(p_tcp->m_mct_coding_matrix);
8596 p_tcp->m_mct_coding_matrix = 00;
8599 if (p_tcp->m_mct_decoding_matrix != 00) {
8600 opj_free(p_tcp->m_mct_decoding_matrix);
8601 p_tcp->m_mct_decoding_matrix = 00;
8604 if (p_tcp->m_mcc_records) {
8605 opj_free(p_tcp->m_mcc_records);
8606 p_tcp->m_mcc_records = 00;
8607 p_tcp->m_nb_max_mcc_records = 0;
8608 p_tcp->m_nb_mcc_records = 0;
8611 if (p_tcp->m_mct_records) {
8612 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8615 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8616 if (l_mct_data->m_data) {
8617 opj_free(l_mct_data->m_data);
8618 l_mct_data->m_data = 00;
8624 opj_free(p_tcp->m_mct_records);
8625 p_tcp->m_mct_records = 00;
8628 if (p_tcp->mct_norms != 00) {
8629 opj_free(p_tcp->mct_norms);
8630 p_tcp->mct_norms = 00;
8633 j2k_tcp_data_destroy(p_tcp);
8638 * Destroys the data inside a tile coding parameter structure.
8640 * @param p_tcp the tile coding parameter which contain data to destroy.
8642 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8644 if (p_tcp->m_data) {
8645 opj_free(p_tcp->m_data);
8646 p_tcp->m_data = NULL;
8647 p_tcp->m_data_size = 0;
8652 * Destroys a coding parameter structure.
8654 * @param p_cp the coding parameter to destroy.
8656 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8658 OPJ_UINT32 l_nb_tiles;
8659 opj_tcp_v2_t * l_current_tile = 00;
8670 l_current_tile = p_cp->tcps;
8671 l_nb_tiles = p_cp->th * p_cp->tw;
8674 (i = 0; i < l_nb_tiles; ++i)
8676 j2k_tcp_destroy(l_current_tile);
8679 opj_free(p_cp->tcps);
8683 (p_cp->ppm_buffer != 00)
8685 opj_free(p_cp->ppm_buffer);
8686 p_cp->ppm_buffer = 00;
8689 (p_cp->comment != 00)
8691 opj_free(p_cp->comment);
8695 (! p_cp->m_is_decoder)
8698 (p_cp->m_specific_param.m_enc.m_matrice)
8700 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8701 p_cp->m_specific_param.m_enc.m_matrice = 00;
8709 * Reads a tile header.
8710 * @param p_j2k the jpeg2000 codec.
8711 * @param p_stream the stream to write data to.
8712 * @param p_manager the user event manager.
8714 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8715 OPJ_UINT32 * p_tile_index,
8716 OPJ_UINT32 * p_data_size,
8717 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8718 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8719 OPJ_UINT32 * p_nb_comps,
8721 opj_stream_private_t *p_stream,
8722 opj_event_mgr_t * p_manager )
8724 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8725 OPJ_UINT32 l_marker_size;
8726 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8727 opj_tcp_v2_t * l_tcp = NULL;
8728 OPJ_UINT32 l_nb_tiles;
8731 assert(p_stream != 00);
8732 assert(p_j2k != 00);
8733 assert(p_manager != 00);
8735 /* Reach the End Of Codestream ?*/
8736 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8737 l_current_marker = J2K_MS_EOC;
8739 /* We need to encounter a SOT marker (a new tile-part header) */
8740 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8744 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8745 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8747 /* Try to read until the Start Of Data is detected */
8748 while (l_current_marker != J2K_MS_SOD) {
8750 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8751 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8752 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8756 /* Read 2 bytes from the buffer as the marker size */
8757 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8759 /* Why this condition? FIXME */
8760 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8761 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8763 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8765 /* Get the marker handler from the marker ID */
8766 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8768 /* Check if the marker is known and if it is the right place to find it */
8769 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8770 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8773 /* FIXME manage case of unknown marker as in the main header ? */
8775 /* Check if the marker size is compatible with the header data size */
8776 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8777 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8778 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8779 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8782 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8785 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8786 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) {
8787 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8791 /* Read the marker segment with the correct marker handler */
8792 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8793 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8797 /* Add the marker to the codestream index*/
8798 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8800 l_marker_handler->id,
8801 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8802 l_marker_size + 4 );
8804 /* Keep the position of the last SOT marker read */
8805 if ( l_marker_handler->id == J2K_MS_SOT ) {
8806 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8807 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8809 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8814 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8815 /* Skip the rest of the tile part header*/
8816 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) {
8817 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8820 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8823 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8824 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8825 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8828 /* Read 2 bytes from the buffer as the new marker ID */
8829 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8833 /* If we didn't skip data before, we need to read the SOD marker*/
8834 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8835 /* Try to read the SOD marker and skip data ? FIXME */
8836 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8842 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8843 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8844 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8845 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8849 /* Read 2 bytes from buffer as the new marker ID */
8850 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8854 /* Indicate we will try to read a new tile-part header*/
8855 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8856 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8857 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8859 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8860 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8861 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8865 /* Read 2 bytes from buffer as the new marker ID */
8866 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8870 /* Current marker is the EOC marker ?*/
8871 if (l_current_marker == J2K_MS_EOC) {
8872 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8873 p_j2k->m_current_tile_number = 0;
8874 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8879 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8880 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8881 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8883 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8884 ++p_j2k->m_current_tile_number;
8888 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8889 *p_go_on = OPJ_FALSE;
8895 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8896 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8900 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8901 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8903 *p_tile_index = p_j2k->m_current_tile_number;
8904 *p_go_on = OPJ_TRUE;
8905 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8906 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8907 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8908 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8909 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8910 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8912 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8918 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8919 OPJ_UINT32 p_tile_index,
8921 OPJ_UINT32 p_data_size,
8922 opj_stream_private_t *p_stream,
8923 opj_event_mgr_t * p_manager )
8925 OPJ_UINT32 l_current_marker;
8926 OPJ_BYTE l_data [2];
8927 opj_tcp_v2_t * l_tcp;
8930 assert(p_stream != 00);
8931 assert(p_j2k != 00);
8932 assert(p_manager != 00);
8934 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8935 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8939 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8940 if (! l_tcp->m_data) {
8941 j2k_tcp_destroy(l_tcp);
8945 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8949 p_j2k->cstr_index) ) {
8950 j2k_tcp_destroy(l_tcp);
8951 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8955 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8959 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8960 * we destroy just the data which will be re-read in read_tile_header*/
8961 /*j2k_tcp_destroy(l_tcp);
8962 p_j2k->m_tcd->tcp = 0;*/
8963 j2k_tcp_data_destroy(l_tcp);
8965 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8966 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8968 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8969 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8970 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8974 opj_read_bytes(l_data,&l_current_marker,2);
8976 if (l_current_marker == J2K_MS_EOC) {
8977 p_j2k->m_current_tile_number = 0;
8978 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8980 else if (l_current_marker != J2K_MS_SOT)
8982 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8991 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8993 OPJ_UINT32 i,j,k = 0;
8994 OPJ_UINT32 l_width_src,l_height_src;
8995 OPJ_UINT32 l_width_dest,l_height_dest;
8996 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8997 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8998 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8999 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9000 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9002 opj_image_comp_t * l_img_comp_src = 00;
9003 opj_image_comp_t * l_img_comp_dest = 00;
9005 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9006 opj_image_t * l_image_src = 00;
9007 OPJ_UINT32 l_size_comp, l_remaining;
9008 OPJ_INT32 * l_dest_ptr;
9009 opj_tcd_resolution_v2_t* l_res= 00;
9011 l_tilec = p_tcd->tcd_image->tiles->comps;
9012 l_image_src = p_tcd->image;
9013 l_img_comp_src = l_image_src->comps;
9015 l_img_comp_dest = p_output_image->comps;
9017 for (i=0; i<l_image_src->numcomps; i++) {
9019 /* Allocate output component buffer if necessary */
9020 if (!l_img_comp_dest->data) {
9022 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9023 if (! l_img_comp_dest->data) {
9028 /* Copy info from decoded comp image to output image */
9029 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9032 /* Compute the precision of the output buffer */
9033 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9034 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9035 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9041 if (l_size_comp == 3) {
9046 /* Current tile component size*/
9048 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9049 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9052 l_width_src = (l_res->x1 - l_res->x0);
9053 l_height_src = (l_res->y1 - l_res->y0);
9055 /* Border of the current output component*/
9056 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9057 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9058 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9059 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9062 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9063 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9067 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9068 * of the input buffer (decoded tile component) which will be move
9069 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9070 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9071 * by this input area.
9073 assert( l_res->x0 >= 0);
9074 assert( l_res->x1 >= 0);
9075 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9076 l_start_x_dest = l_res->x0 - l_x0_dest;
9077 l_offset_x0_src = 0;
9079 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9080 l_width_dest = l_width_src;
9081 l_offset_x1_src = 0;
9084 l_width_dest = l_x1_dest - l_res->x0 ;
9085 l_offset_x1_src = l_width_src - l_width_dest;
9089 l_start_x_dest = 0 ;
9090 l_offset_x0_src = l_x0_dest - l_res->x0;
9092 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9093 l_width_dest = l_width_src - l_offset_x0_src;
9094 l_offset_x1_src = 0;
9097 l_width_dest = l_img_comp_dest->w ;
9098 l_offset_x1_src = l_res->x1 - l_x1_dest;
9102 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9103 l_start_y_dest = l_res->y0 - l_y0_dest;
9104 l_offset_y0_src = 0;
9106 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9107 l_height_dest = l_height_src;
9108 l_offset_y1_src = 0;
9111 l_height_dest = l_y1_dest - l_res->y0 ;
9112 l_offset_y1_src = l_height_src - l_height_dest;
9116 l_start_y_dest = 0 ;
9117 l_offset_y0_src = l_y0_dest - l_res->y0;
9119 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9120 l_height_dest = l_height_src - l_offset_y0_src;
9121 l_offset_y1_src = 0;
9124 l_height_dest = l_img_comp_dest->h ;
9125 l_offset_y1_src = l_res->y1 - l_y1_dest;
9129 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9134 /* Compute the input buffer offset */
9135 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9136 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9137 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9139 /* Compute the output buffer offset */
9140 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9141 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9143 /* Move the output buffer to the first place where we will write*/
9144 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9147 fprintf(stdout, "COMPO[%d]:\n",i);
9148 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9149 "\t tile offset:%d, %d, %d, %d\n"
9150 "\t buffer offset: %d; %d, %d\n",
9151 l_res->x0, l_res->y0, l_width_src, l_height_src,
9152 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9153 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9155 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9156 "\t start offset: %d, line offset= %d\n",
9157 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9161 switch (l_size_comp) {
9164 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9165 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9167 if (l_img_comp_src->sgnd) {
9168 for (j = 0 ; j < l_height_dest ; ++j) {
9169 for ( k = 0 ; k < l_width_dest ; ++k) {
9170 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9173 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9174 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9178 for ( j = 0 ; j < l_height_dest ; ++j ) {
9179 for ( k = 0 ; k < l_width_dest ; ++k) {
9180 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9183 l_dest_ptr+= l_line_offset_dest;
9184 l_src_ptr += l_line_offset_src;
9188 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9189 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9194 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9195 l_src_ptr += l_start_offset_src;
9197 if (l_img_comp_src->sgnd) {
9198 for (j=0;j<l_height_dest;++j) {
9199 for (k=0;k<l_width_dest;++k) {
9200 *(l_dest_ptr++) = *(l_src_ptr++);
9203 l_dest_ptr+= l_line_offset_dest;
9204 l_src_ptr += l_line_offset_src ;
9208 for (j=0;j<l_height_dest;++j) {
9209 for (k=0;k<l_width_dest;++k) {
9210 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9213 l_dest_ptr+= l_line_offset_dest;
9214 l_src_ptr += l_line_offset_src ;
9218 l_src_ptr += l_end_offset_src;
9219 p_data = (OPJ_BYTE*) l_src_ptr;
9224 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9225 l_src_ptr += l_start_offset_src;
9227 for (j=0;j<l_height_dest;++j) {
9228 for (k=0;k<l_width_dest;++k) {
9229 *(l_dest_ptr++) = (*(l_src_ptr++));
9232 l_dest_ptr+= l_line_offset_dest;
9233 l_src_ptr += l_line_offset_src ;
9236 l_src_ptr += l_end_offset_src;
9237 p_data = (OPJ_BYTE*) l_src_ptr;
9251 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9253 * @param p_j2k the jpeg2000 codec.
9254 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9255 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9256 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9257 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9258 * @param p_manager the user event manager
9260 * @return true if the area could be set.
9262 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9263 opj_image_t* p_image,
9264 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9265 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9266 struct opj_event_mgr * p_manager )
9268 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9269 opj_image_t * l_image = p_j2k->m_private_image;
9272 OPJ_INT32 l_comp_x1, l_comp_y1;
9273 opj_image_comp_t* l_img_comp = NULL;
9275 /* Check if we are read the main header */
9276 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9277 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9281 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9282 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9284 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9285 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9286 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9287 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9293 /* Check if the positions provided by the user are correct */
9296 assert(p_start_x >= 0 );
9297 assert(p_start_y >= 0 );
9299 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9300 opj_event_msg_v2(p_manager, EVT_ERROR,
9301 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9302 p_start_x, l_image->x1);
9305 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9306 opj_event_msg_v2(p_manager, EVT_WARNING,
9307 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9308 p_start_x, l_image->x0);
9309 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9310 p_image->x0 = l_image->x0;
9313 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9314 p_image->x0 = p_start_x;
9318 if ((OPJ_UINT32)p_start_y > l_image->y1){
9319 opj_event_msg_v2(p_manager, EVT_ERROR,
9320 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9321 p_start_y, l_image->y1);
9324 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9325 opj_event_msg_v2(p_manager, EVT_WARNING,
9326 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9327 p_start_y, l_image->y0);
9328 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9329 p_image->y0 = l_image->y0;
9332 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9333 p_image->y0 = p_start_y;
9337 assert((OPJ_UINT32)p_end_x > 0);
9338 assert((OPJ_UINT32)p_end_y > 0);
9339 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9340 opj_event_msg_v2(p_manager, EVT_ERROR,
9341 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9342 p_end_x, l_image->x0);
9345 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9346 opj_event_msg_v2(p_manager, EVT_WARNING,
9347 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9348 p_end_x, l_image->x1);
9349 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9350 p_image->x1 = l_image->x1;
9353 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9354 p_image->x1 = p_end_x;
9358 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9359 opj_event_msg_v2(p_manager, EVT_ERROR,
9360 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9361 p_end_y, l_image->y0);
9364 if ((OPJ_UINT32)p_end_y > l_image->y1){
9365 opj_event_msg_v2(p_manager, EVT_WARNING,
9366 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9367 p_end_y, l_image->y1);
9368 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9369 p_image->y1 = l_image->y1;
9372 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9373 p_image->y1 = p_end_y;
9377 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9379 l_img_comp = p_image->comps;
9380 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9384 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9385 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9386 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9387 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9389 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9390 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9392 opj_event_msg_v2(p_manager, EVT_ERROR,
9393 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9397 l_img_comp->w = l_w;
9399 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9400 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9402 opj_event_msg_v2(p_manager, EVT_ERROR,
9403 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9407 l_img_comp->h = l_h;
9412 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9413 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9420 /* ----------------------------------------------------------------------- */
9421 /* J2K / JPT decoder interface */
9422 /* ----------------------------------------------------------------------- */
9424 * Creates a J2K decompression structure.
9426 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9428 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9430 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9434 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9436 l_j2k->m_is_decoder = 1;
9437 l_j2k->m_cp.m_is_decoder = 1;
9439 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9440 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9444 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9446 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9447 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9452 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9454 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9456 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9458 /* codestream index creation */
9459 l_j2k->cstr_index = j2k_create_cstr_index();
9461 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9462 if (!l_j2k->cstr_index){
9467 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9470 /* validation list creation */
9471 l_j2k->m_validation_list = opj_procedure_list_create();
9472 if (! l_j2k->m_validation_list) {
9477 /* execution list creation */
9478 l_j2k->m_procedure_list = opj_procedure_list_create();
9479 if (! l_j2k->m_procedure_list) {
9488 opj_codestream_index_t* j2k_create_cstr_index(void)
9490 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9491 opj_calloc(1,sizeof(opj_codestream_index_t));
9495 cstr_index->maxmarknum = 100;
9496 cstr_index->marknum = 0;
9497 cstr_index->marker = (opj_marker_info_t*)
9498 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9499 if (!cstr_index-> marker)
9502 cstr_index->tile_index = NULL;
9509 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9511 * @param p_tile_no the tile index.
9512 * @param p_comp_no the component being outputted.
9513 * @param p_j2k the J2K codec.
9515 * @return the number of bytes taken by the SPCod element.
9517 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9518 OPJ_UINT32 p_tile_no,
9519 OPJ_UINT32 p_comp_no )
9521 opj_cp_v2_t *l_cp = 00;
9522 opj_tcp_v2_t *l_tcp = 00;
9523 opj_tccp_t *l_tccp = 00;
9526 assert(p_j2k != 00);
9528 l_cp = &(p_j2k->m_cp);
9529 l_tcp = &l_cp->tcps[p_tile_no];
9530 l_tccp = &l_tcp->tccps[p_comp_no];
9532 /* preconditions again */
9533 assert(p_tile_no < (l_cp->tw * l_cp->th));
9534 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9536 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9537 return 5 + l_tccp->numresolutions;
9545 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9547 * @param p_comp_no the component number to output.
9548 * @param p_stream the stream to write data to.
9549 * @param p_j2k J2K codec.
9550 * @param p_manager the user event manager.
9553 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9554 OPJ_UINT32 p_tile_no,
9555 OPJ_UINT32 p_comp_no,
9557 OPJ_UINT32 * p_header_size,
9558 struct opj_event_mgr * p_manager )
9561 opj_cp_v2_t *l_cp = 00;
9562 opj_tcp_v2_t *l_tcp = 00;
9563 opj_tccp_t *l_tccp = 00;
9566 assert(p_j2k != 00);
9567 assert(p_header_size != 00);
9568 assert(p_manager != 00);
9569 assert(p_data != 00);
9571 l_cp = &(p_j2k->m_cp);
9572 l_tcp = &l_cp->tcps[p_tile_no];
9573 l_tccp = &l_tcp->tccps[p_comp_no];
9575 /* preconditions again */
9576 assert(p_tile_no < (l_cp->tw * l_cp->th));
9577 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9579 if (*p_header_size < 5) {
9580 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9584 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9587 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9590 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9593 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9596 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9599 *p_header_size = *p_header_size - 5;
9601 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9603 if (*p_header_size < l_tccp->numresolutions) {
9604 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9608 for (i = 0; i < l_tccp->numresolutions; ++i) {
9609 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9613 *p_header_size = *p_header_size - l_tccp->numresolutions;
9620 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9621 * @param p_header_data the data contained in the COM box.
9622 * @param p_j2k the jpeg2000 codec.
9623 * @param p_header_size the size of the data contained in the COM marker.
9624 * @param p_manager the user event manager.
9626 opj_bool j2k_read_SPCod_SPCoc(
9627 opj_j2k_v2_t *p_j2k,
9629 OPJ_BYTE * p_header_data,
9630 OPJ_UINT32 * p_header_size,
9631 struct opj_event_mgr * p_manager
9634 OPJ_UINT32 i, l_tmp;
9635 opj_cp_v2_t *l_cp = NULL;
9636 opj_tcp_v2_t *l_tcp = NULL;
9637 opj_tccp_t *l_tccp = NULL;
9638 OPJ_BYTE * l_current_ptr = NULL;
9641 assert(p_j2k != 00);
9642 assert(p_manager != 00);
9643 assert(p_header_data != 00);
9645 l_cp = &(p_j2k->m_cp);
9646 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9647 &l_cp->tcps[p_j2k->m_current_tile_number] :
9648 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9650 /* precondition again */
9651 assert(compno < p_j2k->m_private_image->numcomps);
9653 l_tccp = &l_tcp->tccps[compno];
9654 l_current_ptr = p_header_data;
9656 /* make sure room is sufficient */
9657 if (*p_header_size < 5) {
9658 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9662 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9663 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9666 /* If user wants to remove more resolutions than the codestream contains, return error */
9667 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9668 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9669 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9670 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9674 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9678 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9682 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9685 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9688 *p_header_size = *p_header_size - 5;
9690 /* use custom precinct size ? */
9691 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9692 if (*p_header_size < l_tccp->numresolutions) {
9693 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9697 for (i = 0; i < l_tccp->numresolutions; ++i) {
9698 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9700 l_tccp->prcw[i] = l_tmp & 0xf;
9701 l_tccp->prch[i] = l_tmp >> 4;
9704 *p_header_size = *p_header_size - l_tccp->numresolutions;
9707 /* set default size for the precinct width and height */
9708 for (i = 0; i < l_tccp->numresolutions; ++i) {
9709 l_tccp->prcw[i] = 15;
9710 l_tccp->prch[i] = 15;
9714 #ifdef WIP_REMOVE_MSD
9716 if (p_j2k->cstr_info && compno == 0) {
9717 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9719 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9720 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9721 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9722 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9723 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9726 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9727 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9736 * Copies the tile component parameters of all the component from the first tile component.
9738 * @param p_j2k the J2k codec.
9740 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9744 opj_cp_v2_t *l_cp = NULL;
9745 opj_tcp_v2_t *l_tcp = NULL;
9746 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9747 OPJ_UINT32 l_prc_size;
9750 assert(p_j2k != 00);
9752 l_cp = &(p_j2k->m_cp);
9753 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9754 &l_cp->tcps[p_j2k->m_current_tile_number] :
9755 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9757 l_ref_tccp = &l_tcp->tccps[0];
9758 l_copied_tccp = l_ref_tccp + 1;
9759 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9761 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9762 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9763 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9764 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9765 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9766 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9767 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9768 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9774 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9776 * @param p_tile_no the tile index.
9777 * @param p_comp_no the component being outputted.
9778 * @param p_j2k the J2K codec.
9780 * @return the number of bytes taken by the SPCod element.
9782 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9783 OPJ_UINT32 p_tile_no,
9784 OPJ_UINT32 p_comp_no )
9786 OPJ_UINT32 l_num_bands;
9788 opj_cp_v2_t *l_cp = 00;
9789 opj_tcp_v2_t *l_tcp = 00;
9790 opj_tccp_t *l_tccp = 00;
9793 assert(p_j2k != 00);
9795 l_cp = &(p_j2k->m_cp);
9796 l_tcp = &l_cp->tcps[p_tile_no];
9797 l_tccp = &l_tcp->tccps[p_comp_no];
9799 /* preconditions again */
9800 assert(p_tile_no < l_cp->tw * l_cp->th);
9801 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9803 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9805 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9806 return 1 + l_num_bands;
9809 return 1 + 2*l_num_bands;
9814 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9816 * @param p_tile_no the tile to output.
9817 * @param p_comp_no the component number to output.
9818 * @param p_data the data buffer.
9819 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9820 * @param p_j2k J2K codec.
9821 * @param p_manager the user event manager.
9824 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9825 OPJ_UINT32 p_tile_no,
9826 OPJ_UINT32 p_comp_no,
9828 OPJ_UINT32 * p_header_size,
9829 struct opj_event_mgr * p_manager )
9831 OPJ_UINT32 l_header_size;
9832 OPJ_UINT32 l_band_no, l_num_bands;
9833 OPJ_UINT32 l_expn,l_mant;
9835 opj_cp_v2_t *l_cp = 00;
9836 opj_tcp_v2_t *l_tcp = 00;
9837 opj_tccp_t *l_tccp = 00;
9840 assert(p_j2k != 00);
9841 assert(p_header_size != 00);
9842 assert(p_manager != 00);
9843 assert(p_data != 00);
9845 l_cp = &(p_j2k->m_cp);
9846 l_tcp = &l_cp->tcps[p_tile_no];
9847 l_tccp = &l_tcp->tccps[p_comp_no];
9849 /* preconditions again */
9850 assert(p_tile_no < l_cp->tw * l_cp->th);
9851 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9853 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9855 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9856 l_header_size = 1 + l_num_bands;
9858 if (*p_header_size < l_header_size) {
9859 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9863 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9866 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9867 l_expn = l_tccp->stepsizes[l_band_no].expn;
9868 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9873 l_header_size = 1 + 2*l_num_bands;
9875 if (*p_header_size < l_header_size) {
9876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9880 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9883 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9884 l_expn = l_tccp->stepsizes[l_band_no].expn;
9885 l_mant = l_tccp->stepsizes[l_band_no].mant;
9887 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9892 *p_header_size = *p_header_size - l_header_size;
9898 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9900 * @param p_comp_no the component being targeted.
9901 * @param p_header_data the data contained in the COM box.
9902 * @param p_j2k the jpeg2000 codec.
9903 * @param p_header_size the size of the data contained in the COM marker.
9904 * @param p_manager the user event manager.
9906 opj_bool j2k_read_SQcd_SQcc(
9907 opj_j2k_v2_t *p_j2k,
9908 OPJ_UINT32 p_comp_no,
9909 OPJ_BYTE* p_header_data,
9910 OPJ_UINT32 * p_header_size,
9911 struct opj_event_mgr * p_manager
9915 OPJ_UINT32 l_band_no;
9916 opj_cp_v2_t *l_cp = 00;
9917 opj_tcp_v2_t *l_tcp = 00;
9918 opj_tccp_t *l_tccp = 00;
9919 OPJ_BYTE * l_current_ptr = 00;
9920 OPJ_UINT32 l_tmp, l_num_band;
9923 assert(p_j2k != 00);
9924 assert(p_manager != 00);
9925 assert(p_header_data != 00);
9927 l_cp = &(p_j2k->m_cp);
9928 /* come from tile part header or main header ?*/
9929 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9930 &l_cp->tcps[p_j2k->m_current_tile_number] :
9931 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9933 /* precondition again*/
9934 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9936 l_tccp = &l_tcp->tccps[p_comp_no];
9937 l_current_ptr = p_header_data;
9939 if (*p_header_size < 1) {
9940 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9943 *p_header_size -= 1;
9945 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9948 l_tccp->qntsty = l_tmp & 0x1f;
9949 l_tccp->numgbits = l_tmp >> 5;
9950 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9954 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9956 (*p_header_size) / 2;
9958 if( l_num_band > J2K_MAXBANDS ) {
9959 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9960 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9961 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9962 /*return OPJ_FALSE;*/
9967 if (l_cp->correct) {
9969 /* if JPWL is on, we check whether there are too many subbands */
9970 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9971 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9972 "JPWL: bad number of subbands in Sqcx (%d)\n",
9975 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9978 /* we try to correct */
9980 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9981 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9986 #endif /* USE_JPWL */
9988 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9989 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9990 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9992 if (l_band_no < J2K_MAXBANDS){
9993 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9994 l_tccp->stepsizes[l_band_no].mant = 0;
9997 *p_header_size = *p_header_size - l_num_band;
10000 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10001 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10003 if (l_band_no < J2K_MAXBANDS){
10004 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10005 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10008 *p_header_size = *p_header_size - 2*l_num_band;
10011 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10012 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10013 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10014 l_tccp->stepsizes[l_band_no].expn =
10015 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10016 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10017 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10025 * Copies the tile component parameters of all the component from the first tile component.
10027 * @param p_j2k the J2k codec.
10029 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10032 opj_cp_v2_t *l_cp = NULL;
10033 opj_tcp_v2_t *l_tcp = NULL;
10034 opj_tccp_t *l_ref_tccp = NULL;
10035 opj_tccp_t *l_copied_tccp = NULL;
10038 /* preconditions */
10039 assert(p_j2k != 00);
10041 l_cp = &(p_j2k->m_cp);
10042 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10043 &l_cp->tcps[p_j2k->m_current_tile_number] :
10044 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10046 l_ref_tccp = &l_tcp->tccps[0];
10047 l_copied_tccp = l_ref_tccp + 1;
10048 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10050 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10051 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10052 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10053 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10059 * Dump some elements from the J2K decompression structure .
10061 *@param p_j2k the jpeg2000 codec.
10062 *@param flag flag to describe what elments are dump.
10063 *@param out_stream output stream where dump the elements.
10066 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10068 /* Check if the flag is compatible with j2k file*/
10069 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10070 fprintf(out_stream, "Wrong flag\n");
10074 /* Dump the image_header */
10075 if (flag & OPJ_IMG_INFO){
10076 if (p_j2k->m_private_image)
10077 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10080 /* Dump the codestream info from main header */
10081 if (flag & OPJ_J2K_MH_INFO){
10082 j2k_dump_MH_info(p_j2k, out_stream);
10086 /* Dump the codestream info of the current tile */
10087 if (flag & OPJ_J2K_TH_INFO){
10091 /* Dump the codestream index from main header */
10092 if (flag & OPJ_J2K_MH_IND){
10093 j2k_dump_MH_index(p_j2k, out_stream);
10096 /* Dump the codestream index of the current tile */
10097 if (flag & OPJ_J2K_TH_IND){
10104 * Dump index elements of the codestream extract from the main header.
10106 *@param p_j2k the jpeg2000 codec.
10107 *@param out_stream output stream where dump the elements.
10110 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10112 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10113 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10115 fprintf(out_stream, "Codestream index from main header: {\n");
10117 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10118 "\t Main header end position=%" PRIi64 "\n",
10119 cstr_index->main_head_start, cstr_index->main_head_end);
10121 fprintf(out_stream, "\t Marker list: {\n");
10123 if (cstr_index->marker){
10124 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10125 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10126 cstr_index->marker[it_marker].type,
10127 cstr_index->marker[it_marker].pos,
10128 cstr_index->marker[it_marker].len );
10132 fprintf(out_stream, "\t }\n");
10135 if (cstr_index->tile_index){
10137 /* Simple test to avoid to write empty information*/
10138 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10139 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10140 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10143 if (l_acc_nb_of_tile_part)
10145 fprintf(out_stream, "\t Tile index: {\n");
10147 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10148 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10150 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10152 if (cstr_index->tile_index[it_tile].tp_index){
10153 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10154 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10156 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10157 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10158 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10162 if (cstr_index->tile_index[it_tile].marker){
10163 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10164 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10165 cstr_index->tile_index[it_tile].marker[it_marker].type,
10166 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10167 cstr_index->tile_index[it_tile].marker[it_marker].len );
10171 fprintf(out_stream,"\t }\n");
10175 fprintf(out_stream,"}\n");
10180 * Dump info elements of the codestream extract from the main header.
10182 *@param p_j2k the jpeg2000 codec.
10183 *@param out_stream output stream where dump the elements.
10186 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10188 opj_tcp_v2_t * l_default_tile=NULL;
10190 fprintf(out_stream, "Codestream info from main header: {\n");
10192 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10193 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10194 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10196 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10197 if (l_default_tile)
10200 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10202 fprintf(out_stream, "\t default tile {\n");
10203 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10204 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10205 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10206 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10208 for (compno = 0; compno < numcomps; compno++) {
10209 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10211 OPJ_INT32 bandno, numbands;
10214 fprintf(out_stream, "\t\t comp %d {\n", compno);
10215 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10216 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10217 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10218 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10219 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10220 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10222 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10223 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10224 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10226 fprintf(out_stream, "\n");
10228 /* quantization style*/
10229 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10230 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10231 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10232 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10233 for (bandno = 0; bandno < numbands; bandno++) {
10234 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10235 l_tccp->stepsizes[bandno].expn);
10237 fprintf(out_stream, "\n");
10240 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10242 fprintf(out_stream, "\t\t }\n");
10243 } /*end of component of default tile*/
10244 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10248 fprintf(out_stream, "}\n");
10253 * Dump an image header structure.
10255 *@param img_header the image header to dump.
10256 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10257 *@param out_stream output stream where dump the elements.
10259 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10263 if (dev_dump_flag){
10264 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10268 fprintf(out_stream, "Image info {\n");
10269 tab[0] = '\t';tab[1] = '\0';
10272 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10273 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10274 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10276 if (img_header->comps){
10278 for (compno = 0; compno < img_header->numcomps; compno++) {
10279 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10280 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10281 fprintf(out_stream,"%s}\n",tab);
10285 fprintf(out_stream, "}\n");
10289 * Dump a component image header structure.
10291 *@param comp_header the component image header to dump.
10292 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10293 *@param out_stream output stream where dump the elements.
10295 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10299 if (dev_dump_flag){
10300 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10303 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10306 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10307 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10308 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10311 fprintf(out_stream, "}\n");
10316 * Get the codestream info from a JPEG2000 codec.
10318 *@param p_j2k the component image header to dump.
10320 *@return the codestream information extract from the jpg2000 codec
10322 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10325 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10326 opj_tcp_v2_t *l_default_tile;
10327 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10329 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10331 cstr_info->tx0 = p_j2k->m_cp.tx0;
10332 cstr_info->ty0 = p_j2k->m_cp.ty0;
10333 cstr_info->tdx = p_j2k->m_cp.tdx;
10334 cstr_info->tdy = p_j2k->m_cp.tdy;
10335 cstr_info->tw = p_j2k->m_cp.tw;
10336 cstr_info->th = p_j2k->m_cp.th;
10338 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10340 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10342 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10343 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10344 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10345 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10347 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10349 for (compno = 0; compno < numcomps; compno++) {
10350 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10351 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10352 OPJ_INT32 bandno, numbands;
10355 l_tccp_info->csty = l_tccp->csty;
10356 l_tccp_info->numresolutions = l_tccp->numresolutions;
10357 l_tccp_info->cblkw = l_tccp->cblkw;
10358 l_tccp_info->cblkh = l_tccp->cblkh;
10359 l_tccp_info->cblksty = l_tccp->cblksty;
10360 l_tccp_info->qmfbid = l_tccp->qmfbid;
10361 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10363 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10364 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10367 /* quantization style*/
10368 l_tccp_info->qntsty = l_tccp->qntsty;
10369 l_tccp_info->numgbits = l_tccp->numgbits;
10371 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10372 if (numbands < J2K_MAXBANDS) {
10373 for (bandno = 0; bandno < numbands; bandno++) {
10374 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10375 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10380 l_tccp_info->roishift = l_tccp->roishift;
10388 * Get the codestream index from a JPEG2000 codec.
10390 *@param p_j2k the component image header to dump.
10392 *@return the codestream index extract from the jpg2000 codec
10394 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10396 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10397 opj_calloc(1,sizeof(opj_codestream_index_t));
10401 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10402 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10403 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10405 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10406 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10407 if (!l_cstr_index->marker){
10408 opj_free( l_cstr_index);
10412 if (p_j2k->cstr_index->marker)
10413 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10415 opj_free(l_cstr_index->marker);
10416 l_cstr_index->marker = NULL;
10419 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10420 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10421 if (!l_cstr_index->tile_index){
10422 opj_free( l_cstr_index->marker);
10423 opj_free( l_cstr_index);
10427 if (!p_j2k->cstr_index->tile_index){
10428 opj_free(l_cstr_index->tile_index);
10429 l_cstr_index->tile_index = NULL;
10432 OPJ_UINT32 it_tile = 0;
10433 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10436 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10438 l_cstr_index->tile_index[it_tile].marker =
10439 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10441 if (!l_cstr_index->tile_index[it_tile].marker) {
10442 OPJ_UINT32 it_tile_free;
10444 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10445 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10448 opj_free( l_cstr_index->tile_index);
10449 opj_free( l_cstr_index->marker);
10450 opj_free( l_cstr_index);
10454 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10455 memcpy( l_cstr_index->tile_index[it_tile].marker,
10456 p_j2k->cstr_index->tile_index[it_tile].marker,
10457 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10459 opj_free(l_cstr_index->tile_index[it_tile].marker);
10460 l_cstr_index->tile_index[it_tile].marker = NULL;
10463 /* Tile part index*/
10464 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10466 l_cstr_index->tile_index[it_tile].tp_index =
10467 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10469 if(!l_cstr_index->tile_index[it_tile].tp_index){
10470 OPJ_UINT32 it_tile_free;
10472 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10473 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10474 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10477 opj_free( l_cstr_index->tile_index);
10478 opj_free( l_cstr_index->marker);
10479 opj_free( l_cstr_index);
10483 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10484 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10485 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10486 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10489 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10490 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10493 /* Packet index (NOT USED)*/
10494 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10495 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10500 return l_cstr_index;
10503 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10505 OPJ_UINT32 it_tile=0;
10507 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10508 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10509 if (!p_j2k->cstr_index->tile_index)
10512 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10513 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10514 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10515 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10516 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10517 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10527 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10528 opj_stream_private_t *p_stream,
10529 opj_event_mgr_t * p_manager)
10531 opj_bool l_go_on = OPJ_TRUE;
10532 OPJ_UINT32 l_current_tile_no;
10533 OPJ_UINT32 l_data_size,l_max_data_size;
10534 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10535 OPJ_UINT32 l_nb_comps;
10536 OPJ_BYTE * l_current_data;
10538 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10539 if (! l_current_data) {
10542 l_max_data_size = 1000;
10547 if (! j2k_read_tile_header( p_j2k,
10548 &l_current_tile_no,
10550 &l_tile_x0, &l_tile_y0,
10551 &l_tile_x1, &l_tile_y1,
10556 opj_free(l_current_data);
10564 if (l_data_size > l_max_data_size) {
10565 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10566 if (! l_current_data) {
10567 opj_free(l_current_data);
10571 l_max_data_size = l_data_size;
10574 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10575 opj_free(l_current_data);
10578 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);
10580 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10581 opj_free(l_current_data);
10584 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10588 opj_free(l_current_data);
10594 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10596 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10599 assert(p_j2k != 00);
10601 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10602 /* DEVELOPER CORNER, add your custom procedures */
10607 * Read and decode one tile.
10609 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10610 opj_stream_private_t *p_stream,
10611 opj_event_mgr_t * p_manager)
10613 opj_bool l_go_on = OPJ_TRUE;
10614 OPJ_UINT32 l_current_tile_no;
10615 OPJ_UINT32 l_tile_no_to_dec;
10616 OPJ_UINT32 l_data_size,l_max_data_size;
10617 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10618 OPJ_UINT32 l_nb_comps;
10619 OPJ_BYTE * l_current_data;
10621 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10622 if (! l_current_data) {
10625 l_max_data_size = 1000;
10627 /*Allocate and initialize some elements of codestrem index if not already done*/
10628 if( !p_j2k->cstr_index->tile_index)
10630 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10631 opj_free(l_current_data);
10635 /* Move into the codestream to the first SOT used to decode the desired tile */
10636 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10637 if (p_j2k->cstr_index->tile_index)
10638 if(p_j2k->cstr_index->tile_index->tp_index)
10640 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10641 /* the index for this tile has not been built,
10642 * so move to the last SOT read */
10643 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10644 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10649 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)) {
10650 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10654 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10655 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10656 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10660 if (! j2k_read_tile_header( p_j2k,
10661 &l_current_tile_no,
10663 &l_tile_x0, &l_tile_y0,
10664 &l_tile_x1, &l_tile_y1,
10669 opj_free(l_current_data);
10678 if (l_data_size > l_max_data_size) {
10679 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10680 if (! l_current_data) {
10681 opj_free(l_current_data);
10685 l_max_data_size = l_data_size;
10690 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10691 opj_free(l_current_data);
10694 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);
10696 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10697 opj_free(l_current_data);
10700 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10702 if(l_current_tile_no == l_tile_no_to_dec)
10704 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10705 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10706 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10712 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);
10717 opj_free(l_current_data);
10724 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10726 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10729 assert(p_j2k != 00);
10731 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10732 /* DEVELOPER CORNER, add your custom procedures */
10738 * Decodes the tiles of the stream.
10740 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10741 opj_stream_private_t * p_stream,
10742 opj_image_t * p_image,
10743 opj_event_mgr_t * p_manager)
10750 p_j2k->m_output_image = opj_image_create0();
10751 if (! (p_j2k->m_output_image)) {
10754 opj_copy_image_header(p_image, p_j2k->m_output_image);
10756 /* customization of the decoding */
10757 j2k_setup_decoding(p_j2k);
10759 /* Decode the codestream */
10760 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10761 opj_image_destroy(p_j2k->m_private_image);
10762 p_j2k->m_private_image = NULL;
10766 /* Move data and copy one information from codec to output image*/
10767 for (compno = 0; compno < p_image->numcomps; compno++) {
10768 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10769 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10770 p_j2k->m_output_image->comps[compno].data = NULL;
10778 * Get the decoded tile.
10780 * @param p_j2k the jpeg2000 codestream codec.
10781 * @param p_stream input_stream
10782 * @param p_image output image. .
10783 * @param p_manager the user event manager
10784 * @param tile_index index of the tile we want decode
10786 * @return true if succeed.
10788 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10789 opj_stream_private_t *p_stream,
10790 opj_image_t* p_image,
10791 struct opj_event_mgr * p_manager,
10792 OPJ_UINT32 tile_index )
10795 OPJ_UINT32 l_tile_x, l_tile_y;
10796 opj_image_comp_t* l_img_comp;
10799 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10803 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10804 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);
10808 /* Compute the dimension of the desired tile*/
10809 l_tile_x = tile_index % p_j2k->m_cp.tw;
10810 l_tile_y = tile_index / p_j2k->m_cp.tw;
10812 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10813 if (p_image->x0 < p_j2k->m_private_image->x0)
10814 p_image->x0 = p_j2k->m_private_image->x0;
10815 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10816 if (p_image->x1 > p_j2k->m_private_image->x1)
10817 p_image->x1 = p_j2k->m_private_image->x1;
10819 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10820 if (p_image->y0 < p_j2k->m_private_image->y0)
10821 p_image->y0 = p_j2k->m_private_image->y0;
10822 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10823 if (p_image->y1 > p_j2k->m_private_image->y1)
10824 p_image->y1 = p_j2k->m_private_image->y1;
10826 l_img_comp = p_image->comps;
10827 for (compno=0; compno < p_image->numcomps; ++compno)
10829 OPJ_INT32 l_comp_x1, l_comp_y1;
10831 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10833 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10834 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10835 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10836 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10838 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10839 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10844 /* Destroy the previous output image*/
10845 if (p_j2k->m_output_image)
10846 opj_image_destroy(p_j2k->m_output_image);
10848 /* Create the ouput image from the information previously computed*/
10849 p_j2k->m_output_image = opj_image_create0();
10850 if (! (p_j2k->m_output_image)) {
10853 opj_copy_image_header(p_image, p_j2k->m_output_image);
10855 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10857 /* customization of the decoding */
10858 j2k_setup_decoding_tile(p_j2k);
10860 /* Decode the codestream */
10861 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10862 opj_image_destroy(p_j2k->m_private_image);
10863 p_j2k->m_private_image = NULL;
10867 /* Move data and copy one information from codec to output image*/
10868 for (compno = 0; compno < p_image->numcomps; compno++) {
10869 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10871 if (p_image->comps[compno].data)
10872 opj_free(p_image->comps[compno].data);
10874 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10876 p_j2k->m_output_image->comps[compno].data = NULL;
10882 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10884 OPJ_UINT32 it_comp;
10886 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10888 if (p_j2k->m_private_image) {
10889 if (p_j2k->m_private_image->comps) {
10890 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10891 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10892 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10893 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10894 if ( res_factor >= max_res){
10895 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10898 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10911 * Encodes all the tiles in a row.
10913 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10914 opj_stream_private_t *p_stream,
10915 opj_event_mgr_t * p_manager )
10918 OPJ_UINT32 l_nb_tiles;
10919 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10920 OPJ_BYTE * l_current_data;
10922 /* preconditions */
10923 assert(p_j2k != 00);
10924 assert(p_stream != 00);
10925 assert(p_manager != 00);
10927 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10928 if (! l_current_data) {
10931 l_max_tile_size = 1000;
10933 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10934 for (i=0;i<l_nb_tiles;++i) {
10935 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10936 opj_free(l_current_data);
10940 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10941 if (l_current_tile_size > l_max_tile_size) {
10942 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10943 if (! l_current_data) {
10946 l_max_tile_size = l_current_tile_size;
10949 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10951 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10956 opj_free(l_current_data);
10961 * Ends the compression procedures and possibility add data to be read after the
10964 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
10965 opj_stream_private_t *p_stream,
10966 struct opj_event_mgr * p_manager)
10968 /* customization of the encoding */
10969 j2k_setup_end_compress(p_j2k);
10971 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10981 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10983 * @param p_j2k the jpeg2000 codec.
10984 * @param p_stream the stream object.
10985 * @param p_manager the user event manager.
10987 * @return true if the codec is valid.
10989 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
10990 opj_stream_private_t *p_stream,
10991 opj_image_t * p_image,
10992 opj_event_mgr_t * p_manager)
10994 /* preconditions */
10995 assert(p_j2k != 00);
10996 assert(p_stream != 00);
10997 assert(p_manager != 00);
10999 p_j2k->m_private_image = opj_image_create0();
11000 opj_copy_image_header(p_image, p_j2k->m_private_image);
11002 // TODO_MSD: Find a better way
11003 if (p_image->comps) {
11004 OPJ_UINT32 it_comp;
11005 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11006 if (p_image->comps[it_comp].data) {
11007 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11008 p_image->comps[it_comp].data = NULL;
11014 /* customization of the validation */
11015 j2k_setup_encoding_validation (p_j2k);
11017 /* validation of the parameters codec */
11018 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11022 /* customization of the encoding */
11023 j2k_setup_header_writting(p_j2k);
11026 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11036 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11037 OPJ_UINT32 p_tile_index,
11038 opj_stream_private_t *p_stream,
11039 opj_event_mgr_t * p_manager )
11042 if (p_tile_index != p_j2k->m_current_tile_number) {
11043 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11047 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);
11049 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11050 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11051 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11053 /* initialisation before tile encoding */
11054 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11064 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11066 OPJ_UINT32 i,j,k = 0;
11067 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11068 opj_image_comp_t * l_img_comp = 00;
11069 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11070 opj_image_t * l_image = 00;
11071 OPJ_UINT32 l_size_comp, l_remaining;
11072 OPJ_INT32 * l_src_ptr;
11073 l_tilec = p_tcd->tcd_image->tiles->comps;
11074 l_image = p_tcd->image;
11075 l_img_comp = l_image->comps;
11077 for (i=0;i<p_tcd->image->numcomps;++i) {
11078 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11079 l_remaining = l_img_comp->prec & 7; /* (%8) */
11084 if (l_size_comp == 3) {
11088 l_width = (l_tilec->x1 - l_tilec->x0);
11089 l_height = (l_tilec->y1 - l_tilec->y0);
11090 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11091 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11092 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11093 l_stride = l_image_width - l_width;
11094 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11096 switch (l_size_comp) {
11099 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11100 if (l_img_comp->sgnd) {
11101 for (j=0;j<l_height;++j) {
11102 for (k=0;k<l_width;++k) {
11103 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11107 l_src_ptr += l_stride;
11111 for (j=0;j<l_height;++j) {
11112 for (k=0;k<l_width;++k) {
11113 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11117 l_src_ptr += l_stride;
11121 p_data = (OPJ_BYTE*) l_dest_ptr;
11126 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11127 if (l_img_comp->sgnd) {
11128 for (j=0;j<l_height;++j) {
11129 for (k=0;k<l_width;++k) {
11130 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11132 l_src_ptr += l_stride;
11136 for (j=0;j<l_height;++j) {
11137 for (k=0;k<l_width;++k) {
11138 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11140 l_src_ptr += l_stride;
11144 p_data = (OPJ_BYTE*) l_dest_ptr;
11149 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11150 for (j=0;j<l_height;++j) {
11151 for (k=0;k<l_width;++k) {
11152 *(l_dest_ptr++) = *(l_src_ptr++);
11154 l_src_ptr += l_stride;
11157 p_data = (OPJ_BYTE*) l_dest_ptr;
11170 * @param p_j2k the jpeg2000 codec.
11171 * @param p_stream the stream to write data to.
11172 * @param p_manager the user event manager.
11174 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11176 OPJ_UINT32 p_data_size,
11177 opj_stream_private_t *p_stream,
11178 opj_event_mgr_t * p_manager )
11180 opj_tcd_v2_t * l_tcd = 00;
11181 opj_cp_v2_t * l_cp = 00;
11182 opj_tcp_v2_t * l_tcp = 00;
11183 OPJ_UINT32 l_nb_bytes_written;
11184 OPJ_BYTE * l_current_data = 00;
11185 OPJ_UINT32 l_tile_size = 0;
11186 OPJ_UINT32 l_available_data;
11188 /* preconditions */
11189 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11191 l_tcd = p_j2k->m_tcd;
11192 l_cp = &(p_j2k->m_cp);
11193 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11195 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11196 l_available_data = l_tile_size;
11197 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11199 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11200 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11204 l_nb_bytes_written = 0;
11205 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11208 l_current_data += l_nb_bytes_written;
11209 l_available_data -= l_nb_bytes_written;
11211 l_nb_bytes_written = 0;
11212 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11216 l_available_data -= l_nb_bytes_written;
11217 l_nb_bytes_written = l_tile_size - l_available_data;
11219 if ( opj_stream_write_data( p_stream,
11220 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11221 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11225 ++p_j2k->m_current_tile_number;
11232 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11233 * are valid. Developers wanting to extend the library can add their own validation procedures.
11235 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11237 /* preconditions */
11238 assert(p_j2k != 00);
11240 /* DEVELOPER CORNER, insert your custom procedures */
11241 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11243 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11244 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11247 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11248 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11249 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11253 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11254 * are valid. Developers wanting to extend the library can add their own validation procedures.
11256 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11258 /* preconditions */
11259 assert(p_j2k != 00);
11261 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11262 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11264 /* DEVELOPER CORNER, add your custom validation procedure */
11265 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11270 * Sets up the procedures to do on writing header.
11271 * Developers wanting to extend the library can add their own writing procedures.
11273 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11275 /* preconditions */
11276 assert(p_j2k != 00);
11278 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11279 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11280 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11281 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11282 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11285 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11286 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11287 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11289 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11290 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11294 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11296 if (p_j2k->m_cp.comment != 00) {
11297 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11300 /* DEVELOPER CORNER, insert your custom procedures */
11301 if (p_j2k->m_cp.rsiz & MCT) {
11302 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11304 /* End of Developer Corner */
11306 if (p_j2k->cstr_index) {
11307 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11310 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11311 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11315 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11317 OPJ_UINT32 * p_data_written,
11318 OPJ_UINT32 p_total_data_size,
11319 opj_stream_private_t *p_stream,
11320 struct opj_event_mgr * p_manager )
11323 OPJ_UINT32 l_nb_bytes_written = 0;
11324 OPJ_UINT32 l_current_nb_bytes_written;
11325 OPJ_BYTE * l_begin_data = 00;
11327 opj_tcp_v2_t *l_tcp = 00;
11328 opj_tcd_v2_t * l_tcd = 00;
11329 opj_cp_v2_t * l_cp = 00;
11331 l_tcd = p_j2k->m_tcd;
11332 l_cp = &(p_j2k->m_cp);
11333 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11335 l_tcd->cur_pino = 0;
11337 /*Get number of tile parts*/
11338 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11343 l_current_nb_bytes_written = 0;
11344 l_begin_data = p_data;
11345 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11350 l_nb_bytes_written += l_current_nb_bytes_written;
11351 p_data += l_current_nb_bytes_written;
11352 p_total_data_size -= l_current_nb_bytes_written;
11354 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11355 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11356 l_current_nb_bytes_written = 0;
11357 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11358 l_nb_bytes_written += l_current_nb_bytes_written;
11359 p_data += l_current_nb_bytes_written;
11360 p_total_data_size -= l_current_nb_bytes_written;
11362 l_current_nb_bytes_written = 0;
11363 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11364 l_nb_bytes_written += l_current_nb_bytes_written;
11365 p_data += l_current_nb_bytes_written;
11366 p_total_data_size -= l_current_nb_bytes_written;
11369 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11370 l_current_nb_bytes_written = 0;
11371 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11372 l_nb_bytes_written += l_current_nb_bytes_written;
11373 p_data += l_current_nb_bytes_written;
11374 p_total_data_size -= l_current_nb_bytes_written;
11378 l_current_nb_bytes_written = 0;
11379 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11383 l_nb_bytes_written += l_current_nb_bytes_written;
11384 * p_data_written = l_nb_bytes_written;
11386 /* Writing Psot in SOT marker */
11387 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11389 if (l_cp->m_specific_param.m_enc.m_cinema){
11390 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11396 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11398 OPJ_UINT32 * p_data_written,
11399 OPJ_UINT32 p_total_data_size,
11400 opj_stream_private_t *p_stream,
11401 struct opj_event_mgr * p_manager
11404 OPJ_UINT32 tilepartno=0;
11405 OPJ_UINT32 l_nb_bytes_written = 0;
11406 OPJ_UINT32 l_current_nb_bytes_written;
11407 OPJ_UINT32 l_part_tile_size;
11408 OPJ_UINT32 tot_num_tp;
11411 OPJ_BYTE * l_begin_data;
11412 opj_tcp_v2_t *l_tcp = 00;
11413 opj_tcd_v2_t * l_tcd = 00;
11414 opj_cp_v2_t * l_cp = 00;
11417 l_tcd = p_j2k->m_tcd;
11418 l_cp = &(p_j2k->m_cp);
11419 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11421 /*Get number of tile parts*/
11422 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11424 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11425 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11426 l_current_nb_bytes_written = 0;
11427 l_part_tile_size = 0;
11428 l_begin_data = p_data;
11430 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11434 l_nb_bytes_written += l_current_nb_bytes_written;
11435 p_data += l_current_nb_bytes_written;
11436 p_total_data_size -= l_current_nb_bytes_written;
11437 l_part_tile_size += l_nb_bytes_written;
11439 l_current_nb_bytes_written = 0;
11440 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11444 p_data += l_current_nb_bytes_written;
11445 l_nb_bytes_written += l_current_nb_bytes_written;
11446 p_total_data_size -= l_current_nb_bytes_written;
11447 l_part_tile_size += l_nb_bytes_written;
11449 /* Writing Psot in SOT marker */
11450 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11452 if (l_cp->m_specific_param.m_enc.m_cinema) {
11453 j2k_update_tlm(p_j2k,l_part_tile_size);
11456 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11459 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11460 l_tcd->cur_pino = pino;
11462 /*Get number of tile parts*/
11463 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11464 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11465 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11466 l_current_nb_bytes_written = 0;
11467 l_part_tile_size = 0;
11468 l_begin_data = p_data;
11470 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11474 l_nb_bytes_written += l_current_nb_bytes_written;
11475 p_data += l_current_nb_bytes_written;
11476 p_total_data_size -= l_current_nb_bytes_written;
11477 l_part_tile_size += l_current_nb_bytes_written;
11479 l_current_nb_bytes_written = 0;
11481 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11485 l_nb_bytes_written += l_current_nb_bytes_written;
11486 p_data += l_current_nb_bytes_written;
11487 p_total_data_size -= l_current_nb_bytes_written;
11488 l_part_tile_size += l_current_nb_bytes_written;
11490 /* Writing Psot in SOT marker */
11491 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11493 if (l_cp->m_specific_param.m_enc.m_cinema) {
11494 j2k_update_tlm(p_j2k,l_part_tile_size);
11497 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11501 *p_data_written = l_nb_bytes_written;
11507 * Writes the updated tlm.
11509 * @param p_stream the stream to write data to.
11510 * @param p_j2k J2K codec.
11511 * @param p_manager the user event manager.
11513 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11514 struct opj_stream_private *p_stream,
11515 struct opj_event_mgr * p_manager )
11517 OPJ_UINT32 l_tlm_size;
11518 OPJ_SIZE_T l_tlm_position, l_current_position;
11520 /* preconditions */
11521 assert(p_j2k != 00);
11522 assert(p_manager != 00);
11523 assert(p_stream != 00);
11525 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11526 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11527 l_current_position = opj_stream_tell(p_stream);
11529 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11533 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) {
11537 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11546 * Ends the encoding, i.e. frees memory.
11548 * @param p_stream the stream to write data to.
11549 * @param p_j2k J2K codec.
11550 * @param p_manager the user event manager.
11552 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11553 struct opj_stream_private *p_stream,
11554 struct opj_event_mgr * p_manager )
11556 /* preconditions */
11557 assert(p_j2k != 00);
11558 assert(p_manager != 00);
11559 assert(p_stream != 00);
11561 tcd_destroy_v2(p_j2k->m_tcd);
11564 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11565 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11566 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11567 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11570 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11571 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11572 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11575 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11581 * Destroys the memory associated with the decoding of headers.
11583 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11584 opj_stream_private_t *p_stream,
11585 opj_event_mgr_t * p_manager )
11587 /* preconditions */
11588 assert(p_j2k != 00);
11589 assert(p_stream != 00);
11590 assert(p_manager != 00);
11592 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11593 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11594 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11597 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11606 * @param p_stream the stream to write data to.
11607 * @param p_j2k J2K codec.
11608 * @param p_manager the user event manager.
11610 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11611 struct opj_stream_private *p_stream,
11612 struct opj_event_mgr * p_manager )
11614 opj_codestream_info_t * l_cstr_info = 00;
11616 /* preconditions */
11617 assert(p_j2k != 00);
11618 assert(p_manager != 00);
11619 assert(p_stream != 00);
11622 /* TODO mergeV2: check this part which use cstr_info */
11623 /*l_cstr_info = p_j2k->cstr_info;
11627 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));
11629 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11630 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11632 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11634 l_cstr_info->tw = p_j2k->m_cp.tw;
11635 l_cstr_info->th = p_j2k->m_cp.th;
11637 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11638 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11639 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11640 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11642 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11644 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11646 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11648 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11649 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11652 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11654 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11656 /*l_cstr_info->maxmarknum = 100;
11657 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11658 l_cstr_info->marknum = 0;
11661 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);
11665 * Creates a tile-coder decoder.
11667 * @param p_stream the stream to write data to.
11668 * @param p_j2k J2K codec.
11669 * @param p_manager the user event manager.
11671 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11672 struct opj_stream_private *p_stream,
11673 struct opj_event_mgr * p_manager )
11675 /* preconditions */
11676 assert(p_j2k != 00);
11677 assert(p_manager != 00);
11678 assert(p_stream != 00);
11680 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11682 if (! p_j2k->m_tcd) {
11683 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11687 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11688 tcd_destroy_v2(p_j2k->m_tcd);
11699 * @param p_j2k the jpeg2000 codec.
11700 * @param p_stream the stream to write data to.
11701 * @param p_manager the user event manager.
11703 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11704 OPJ_UINT32 p_tile_index,
11706 OPJ_UINT32 p_data_size,
11707 opj_stream_private_t *p_stream,
11708 opj_event_mgr_t * p_manager )
11710 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11711 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11715 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11716 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);