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 Add main header marker information
998 @param cstr_info Codestream information structure
999 @param type marker type
1000 @param pos byte offset of marker segment
1001 @param len length of marker segment
1003 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1005 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1007 Add tile header marker information
1008 @param tileno tile index number
1009 @param cstr_info Codestream information structure
1010 @param type marker type
1011 @param pos byte offset of marker segment
1012 @param len length of marker segment
1014 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1016 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1019 * Reads an unknown marker
1021 * @param p_stream the stream object to read from.
1022 * @param p_j2k the jpeg2000 codec.
1023 * @param p_manager the user event manager.
1025 * @return true if the marker could be deduced.
1027 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
1028 opj_stream_private_t *p_stream,
1029 OPJ_UINT32 *output_marker,
1030 opj_event_mgr_t * p_manager );
1033 * Writes the MCT marker (Multiple Component Transform)
1035 * @param p_stream the stream to write data to.
1036 * @param p_j2k J2K codec.
1037 * @param p_manager the user event manager.
1039 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1040 opj_mct_data_t * p_mct_record,
1041 struct opj_stream_private *p_stream,
1042 struct opj_event_mgr * p_manager );
1045 * Reads a MCT marker (Multiple Component Transform)
1047 * @param p_header_data the data contained in the MCT box.
1048 * @param p_j2k the jpeg2000 codec.
1049 * @param p_header_size the size of the data contained in the MCT marker.
1050 * @param p_manager the user event manager.
1052 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1053 OPJ_BYTE * p_header_data,
1054 OPJ_UINT32 p_header_size,
1055 opj_event_mgr_t * p_manager );
1058 * Writes the MCC marker (Multiple Component Collection)
1060 * @param p_stream the stream to write data to.
1061 * @param p_j2k J2K codec.
1062 * @param p_manager the user event manager.
1064 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1065 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1066 struct opj_stream_private *p_stream,
1067 struct opj_event_mgr * p_manager );
1071 * Reads a MCC marker (Multiple Component Collection)
1073 * @param p_header_data the data contained in the MCC box.
1074 * @param p_j2k the jpeg2000 codec.
1075 * @param p_header_size the size of the data contained in the MCC marker.
1076 * @param p_manager the user event manager.
1078 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1079 OPJ_BYTE * p_header_data,
1080 OPJ_UINT32 p_header_size,
1081 opj_event_mgr_t * p_manager );
1084 * Writes the MCO marker (Multiple component transformation ordering)
1086 * @param p_stream the stream to write data to.
1087 * @param p_j2k J2K codec.
1088 * @param p_manager the user event manager.
1090 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1091 struct opj_stream_private *p_stream,
1092 struct opj_event_mgr * p_manager );
1095 * Reads a MCO marker (Multiple Component Transform Ordering)
1097 * @param p_header_data the data contained in the MCO box.
1098 * @param p_j2k the jpeg2000 codec.
1099 * @param p_header_size the size of the data contained in the MCO marker.
1100 * @param p_manager the user event manager.
1102 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1103 OPJ_BYTE * p_header_data,
1104 OPJ_UINT32 p_header_size,
1105 opj_event_mgr_t * p_manager );
1107 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1109 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1110 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1111 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1112 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1114 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1115 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1116 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1117 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1119 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1120 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1121 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 * Ends the encoding, i.e. frees memory.
1128 * @param p_stream the stream to write data to.
1129 * @param p_j2k J2K codec.
1130 * @param p_manager the user event manager.
1132 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1133 struct opj_stream_private *p_stream,
1134 struct opj_event_mgr * p_manager );
1137 * Writes the CBD marker (Component bit depth definition)
1139 * @param p_stream the stream to write data to.
1140 * @param p_j2k J2K codec.
1141 * @param p_manager the user event manager.
1143 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1144 struct opj_stream_private *p_stream,
1145 struct opj_event_mgr * p_manager );
1148 * Reads a CBD marker (Component bit depth definition)
1149 * @param p_header_data the data contained in the CBD box.
1150 * @param p_j2k the jpeg2000 codec.
1151 * @param p_header_size the size of the data contained in the CBD marker.
1152 * @param p_manager the user event manager.
1154 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1155 OPJ_BYTE * p_header_data,
1156 OPJ_UINT32 p_header_size,
1157 opj_event_mgr_t * p_manager);
1160 * Writes the image components.
1162 * @param p_stream the stream to write data to.
1163 * @param p_j2k J2K codec.
1164 * @param p_manager the user event manager.
1166 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1167 struct opj_stream_private *p_stream,
1168 struct opj_event_mgr * p_manager );
1171 * Writes regions of interests.
1173 * @param p_stream the stream to write data to.
1174 * @param p_j2k J2K codec.
1175 * @param p_manager the user event manager.
1177 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1178 struct opj_stream_private *p_stream,
1179 struct opj_event_mgr * p_manager );
1184 * @param p_stream the stream to write data to.
1185 * @param p_j2k J2K codec.
1186 * @param p_manager the user event manager.
1188 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1189 struct opj_stream_private *p_stream,
1190 struct opj_event_mgr * p_manager );
1193 * Checks the progression order changes values. Tells of the poc given as input are valid.
1194 * A nice message is outputted at errors.
1196 * @param p_pocs the progression order changes.
1197 * @param p_nb_pocs the number of progression order changes.
1198 * @param p_nb_resolutions the number of resolutions.
1199 * @param numcomps the number of components
1200 * @param numlayers the number of layers.
1202 * @return true if the pocs are valid.
1204 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1205 OPJ_UINT32 p_nb_pocs,
1206 OPJ_UINT32 p_nb_resolutions,
1207 OPJ_UINT32 numcomps,
1208 OPJ_UINT32 numlayers,
1209 opj_event_mgr_t * p_manager);
1212 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1214 * @param cp the coding parameters.
1215 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1216 * @param tileno the given tile.
1218 * @return the number of tile parts.
1220 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1222 /** mem allocation for TLM marker*/
1223 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1226 * Calculates the total number of tile parts needed by the encoder to
1227 * encode such an image. If not enough memory is available, then the function return false.
1229 * @param p_nb_tiles pointer that will hold the number of tile parts.
1230 * @param cp the coding parameters for the image.
1231 * @param image the image to encode.
1232 * @param p_j2k the p_j2k encoder.
1233 * @param p_manager the user event manager.
1235 * @return true if the function was successful, false else.
1237 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1239 OPJ_UINT32 * p_nb_tiles,
1241 opj_event_mgr_t * p_manager);
1243 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1245 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1247 static opj_codestream_index_t* j2k_create_cstr_index(void);
1253 /* ----------------------------------------------------------------------- */
1254 typedef struct j2k_prog_order{
1255 OPJ_PROG_ORDER enum_prog;
1259 j2k_prog_order_t j2k_prog_order_list[] = {
1265 {(OPJ_PROG_ORDER)-1, ""}
1273 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1281 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1283 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1285 j2k_read_int16_to_float,
1286 j2k_read_int32_to_float,
1287 j2k_read_float32_to_float,
1288 j2k_read_float64_to_float
1291 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1293 j2k_read_int16_to_int32,
1294 j2k_read_int32_to_int32,
1295 j2k_read_float32_to_int32,
1296 j2k_read_float64_to_int32
1299 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1301 j2k_write_float_to_int16,
1302 j2k_write_float_to_int32,
1303 j2k_write_float_to_float,
1304 j2k_write_float_to_float64
1307 typedef struct opj_dec_memory_marker_handler
1311 /** value of the state when the marker can appear */
1313 /** action linked to the marker */
1314 opj_bool (*handler) (
1315 opj_j2k_v2_t *p_j2k,
1316 OPJ_BYTE * p_header_data,
1317 OPJ_UINT32 p_header_size,
1318 struct opj_event_mgr * p_manager
1321 opj_dec_memory_marker_handler_t;
1323 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1326 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1327 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1328 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1329 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1330 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1331 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1332 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1333 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1334 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1335 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1336 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1337 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1338 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1340 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1341 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1342 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1343 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1344 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1345 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1347 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1348 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1349 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1350 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1351 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1352 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1353 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1354 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1355 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1356 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1357 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1358 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1359 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1361 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1362 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1363 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1364 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1365 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1366 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1368 #ifdef TODO_MS /* FIXME */
1369 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1370 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1371 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1372 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1374 #endif /* USE_JPWL */
1376 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1377 {J2K_MS_INSEC, 0, j2k_read_insec}
1378 #endif /* USE_JPSEC */
1379 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1384 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1386 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1387 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1391 for (i=0;i<p_nb_elem;++i) {
1392 opj_read_bytes(l_src_data,&l_temp,2);
1394 l_src_data+=sizeof(OPJ_INT16);
1396 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1400 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1402 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1403 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1407 for (i=0;i<p_nb_elem;++i) {
1408 opj_read_bytes(l_src_data,&l_temp,4);
1410 l_src_data+=sizeof(OPJ_INT32);
1412 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1416 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1418 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1419 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1423 for (i=0;i<p_nb_elem;++i) {
1424 opj_read_float(l_src_data,&l_temp);
1426 l_src_data+=sizeof(OPJ_FLOAT32);
1428 *(l_dest_data++) = l_temp;
1432 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1434 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1435 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1439 for (i=0;i<p_nb_elem;++i) {
1440 opj_read_double(l_src_data,&l_temp);
1442 l_src_data+=sizeof(OPJ_FLOAT64);
1444 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1448 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1450 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1451 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1455 for (i=0;i<p_nb_elem;++i) {
1456 opj_read_bytes(l_src_data,&l_temp,2);
1458 l_src_data+=sizeof(OPJ_INT16);
1460 *(l_dest_data++) = (OPJ_INT32) l_temp;
1464 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1466 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1467 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1471 for (i=0;i<p_nb_elem;++i) {
1472 opj_read_bytes(l_src_data,&l_temp,4);
1474 l_src_data+=sizeof(OPJ_INT32);
1476 *(l_dest_data++) = (OPJ_INT32) l_temp;
1480 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1482 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1483 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1487 for (i=0;i<p_nb_elem;++i) {
1488 opj_read_float(l_src_data,&l_temp);
1490 l_src_data+=sizeof(OPJ_FLOAT32);
1492 *(l_dest_data++) = (OPJ_INT32) l_temp;
1496 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1498 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1499 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1503 for (i=0;i<p_nb_elem;++i) {
1504 opj_read_double(l_src_data,&l_temp);
1506 l_src_data+=sizeof(OPJ_FLOAT64);
1508 *(l_dest_data++) = (OPJ_INT32) l_temp;
1512 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1514 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1515 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1519 for (i=0;i<p_nb_elem;++i) {
1520 l_temp = (OPJ_UINT32) *(l_src_data++);
1522 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1524 l_dest_data+=sizeof(OPJ_INT16);
1528 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1530 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1531 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1535 for (i=0;i<p_nb_elem;++i) {
1536 l_temp = (OPJ_UINT32) *(l_src_data++);
1538 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1540 l_dest_data+=sizeof(OPJ_INT32);
1544 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1546 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1547 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1551 for (i=0;i<p_nb_elem;++i) {
1552 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1554 opj_write_float(l_dest_data,l_temp);
1556 l_dest_data+=sizeof(OPJ_FLOAT32);
1560 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1562 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1563 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1567 for (i=0;i<p_nb_elem;++i) {
1568 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1570 opj_write_double(l_dest_data,l_temp);
1572 l_dest_data+=sizeof(OPJ_FLOAT64);
1578 * Converts an enum type progression order to string type.
1580 * @param prg_order the progression order to get.
1582 * @return the string representation of the given progression order.
1584 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1585 j2k_prog_order_t *po;
1586 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1587 if(po->enum_prog == prg_order){
1588 return po->str_prog;
1591 return po->str_prog;
1595 * Checks the progression order changes values. Tells if the poc given as input are valid.
1597 * @param p_pocs the progression order changes.
1598 * @param p_nb_pocs the number of progression order changes.
1599 * @param p_nb_resolutions the number of resolutions.
1600 * @param numcomps the number of components
1601 * @param numlayers the number of layers.
1602 * @param p_manager the user event manager.
1604 * @return true if the pocs are valid.
1606 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1607 OPJ_UINT32 p_nb_pocs,
1608 OPJ_UINT32 p_nb_resolutions,
1609 OPJ_UINT32 p_num_comps,
1610 OPJ_UINT32 p_num_layers,
1611 opj_event_mgr_t * p_manager)
1613 OPJ_UINT32* packet_array;
1614 OPJ_UINT32 index , resno, compno, layno;
1616 OPJ_UINT32 step_c = 1;
1617 OPJ_UINT32 step_r = p_num_comps * step_c;
1618 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1619 opj_bool loss = OPJ_FALSE;
1620 OPJ_UINT32 layno0 = 0;
1622 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1623 if (packet_array == 00) {
1624 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1627 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1629 if (p_nb_pocs == 0) {
1633 index = step_r * p_pocs->resno0;
1634 // take each resolution for each poc
1635 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1637 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1639 // take each comp of each resolution for each poc
1640 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1641 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1643 // and finally take each layer of each res of ...
1644 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1645 //index = step_r * resno + step_c * compno + step_l * layno;
1646 packet_array[comp_index] = 1;
1647 comp_index += step_l;
1650 res_index += step_c;
1657 // iterate through all the pocs
1658 for (i = 1; i < p_nb_pocs ; ++i) {
1659 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1661 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1662 index = step_r * p_pocs->resno0;
1664 // take each resolution for each poc
1665 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1666 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1668 // take each comp of each resolution for each poc
1669 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1670 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1672 // and finally take each layer of each res of ...
1673 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1674 //index = step_r * resno + step_c * compno + step_l * layno;
1675 packet_array[comp_index] = 1;
1676 comp_index += step_l;
1679 res_index += step_c;
1689 for (layno = 0; layno < p_num_layers ; ++layno) {
1690 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1691 for (compno = 0; compno < p_num_comps; ++compno) {
1692 loss |= (packet_array[index]!=1);
1693 //index = step_r * resno + step_c * compno + step_l * layno;
1700 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1703 opj_free(packet_array);
1708 /* ----------------------------------------------------------------------- */
1709 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1712 int tpnum=1,tpend=0;
1713 opj_tcp_t *tcp = &cp->tcps[tileno];
1714 prog = j2k_convert_progression_order(tcp->prg);
1719 if( cp->tp_flag == prog[i] ){
1720 tpend=1;cp->tp_pos=i;
1724 tpnum= tpnum * tcp->pocs[pino].compE;
1727 tpnum= tpnum * tcp->pocs[pino].resE;
1730 tpnum= tpnum * tcp->pocs[pino].prcE;
1733 tpnum= tpnum * tcp->pocs[pino].layE;
1745 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1747 * @param cp the coding parameters.
1748 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1749 * @param tileno the given tile.
1751 * @return the number of tile parts.
1753 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1755 const OPJ_CHAR *prog = 00;
1757 OPJ_UINT32 tpnum = 1;
1758 opj_tcp_v2_t *tcp = 00;
1759 opj_poc_t * l_current_poc = 00;
1762 assert(tileno < (cp->tw * cp->th));
1763 assert(pino < (cp->tcps[tileno].numpocs + 1));
1765 /* get the given tile coding parameter */
1766 tcp = &cp->tcps[tileno];
1769 l_current_poc = &(tcp->pocs[pino]);
1770 assert(l_current_poc != 0);
1772 /* get the progression order as a character string */
1773 prog = j2k_convert_progression_order(tcp->prg);
1774 assert(strlen(prog) > 0);
1776 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1780 /* component wise */
1782 tpnum *= l_current_poc->compE;
1784 /* resolution wise */
1786 tpnum *= l_current_poc->resE;
1790 tpnum *= l_current_poc->prcE;
1794 tpnum *= l_current_poc->layE;
1797 /* whould we split here ? */
1798 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1799 cp->m_specific_param.m_enc.m_tp_pos=i;
1811 /** mem allocation for TLM marker*/
1812 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1813 int pino,tileno,totnum_tp=0;
1815 OPJ_ARG_NOT_USED(img_numcomp);
1817 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1818 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1819 int cur_totnum_tp = 0;
1820 opj_tcp_t *tcp = &cp->tcps[tileno];
1821 for(pino = 0; pino <= tcp->numpocs; pino++) {
1823 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1824 if(!pi) { return -1;}
1825 tp_num = j2k_get_num_tp(cp,pino,tileno);
1826 totnum_tp = totnum_tp + tp_num;
1827 cur_totnum_tp = cur_totnum_tp + tp_num;
1828 pi_destroy(pi, cp, tileno);
1830 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1832 if (j2k->cstr_info) {
1833 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1834 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1842 * Calculates the total number of tile parts needed by the encoder to
1843 * encode such an image. If not enough memory is available, then the function return false.
1845 * @param p_nb_tiles pointer that will hold the number of tile parts.
1846 * @param cp the coding parameters for the image.
1847 * @param image the image to encode.
1848 * @param p_j2k the p_j2k encoder.
1849 * @param p_manager the user event manager.
1851 * @return true if the function was successful, false else.
1853 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1855 OPJ_UINT32 * p_nb_tiles,
1857 opj_event_mgr_t * p_manager)
1859 OPJ_UINT32 pino,tileno;
1860 OPJ_UINT32 l_nb_tiles;
1864 assert(p_nb_tiles != 00);
1866 assert(image != 00);
1867 assert(p_j2k != 00);
1868 assert(p_manager != 00);
1870 l_nb_tiles = cp->tw * cp->th;
1875 /* TODO mergeV2: check this part which use cstr_info */
1876 /*if (p_j2k->cstr_info) {
1877 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1879 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1880 OPJ_UINT32 cur_totnum_tp = 0;
1882 pi_update_encoding_parameters(image,cp,tileno);
1884 for (pino = 0; pino <= tcp->numpocs; ++pino)
1886 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1888 *p_nb_tiles = *p_nb_tiles + tp_num;
1890 cur_totnum_tp += tp_num;
1893 tcp->m_nb_tile_parts = cur_totnum_tp;
1895 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1896 if (l_info_tile_ptr->tp == 00) {
1900 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1902 l_info_tile_ptr->num_tps = cur_totnum_tp;
1909 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1910 OPJ_UINT32 cur_totnum_tp = 0;
1912 pi_update_encoding_parameters(image,cp,tileno);
1914 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1915 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1917 *p_nb_tiles = *p_nb_tiles + tp_num;
1919 cur_totnum_tp += tp_num;
1921 tcp->m_nb_tile_parts = cur_totnum_tp;
1930 static void j2k_write_soc(opj_j2k_t *j2k) {
1931 opj_cio_t *cio = j2k->cio;
1932 cio_write(cio, J2K_MS_SOC, 2);
1935 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1940 /* update markers struct */
1941 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1942 #endif /* USE_JPWL */
1947 * Writes the SOC marker (Start Of Codestream)
1949 * @param p_stream the stream to write data to.
1950 * @param p_j2k J2K codec.
1951 * @param p_manager the user event manager.
1953 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1954 struct opj_stream_private *p_stream,
1955 struct opj_event_mgr * p_manager )
1957 /* 2 bytes will be written */
1958 OPJ_BYTE * l_start_stream = 00;
1961 assert(p_stream != 00);
1962 assert(p_j2k != 00);
1963 assert(p_manager != 00);
1965 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1967 /* write SOC identifier */
1968 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1970 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1976 /* update markers struct */
1978 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1980 assert( 0 && "TODO" );
1981 #endif /* USE_JPWL */
1990 * Reads a SOC marker (Start of Codestream)
1991 * @param p_header_data the data contained in the SOC box.
1992 * @param jp2 the jpeg2000 file codec.
1993 * @param p_header_size the size of the data contained in the SOC marker.
1994 * @param p_manager the user event manager.
1996 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1997 struct opj_stream_private *p_stream,
1998 struct opj_event_mgr * p_manager )
2000 OPJ_BYTE l_data [2];
2001 OPJ_UINT32 l_marker;
2004 assert(p_j2k != 00);
2005 assert(p_manager != 00);
2006 assert(p_stream != 00);
2008 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2012 opj_read_bytes(l_data,&l_marker,2);
2013 if (l_marker != J2K_MS_SOC) {
2017 /* Next marker should be a SIZ marker in the main header */
2018 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2020 /* FIXME move it in a index structure included in p_j2k*/
2021 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2023 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2025 /* Add the marker to the codestream index*/
2026 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2031 static void j2k_write_siz(opj_j2k_t *j2k) {
2035 opj_cio_t *cio = j2k->cio;
2036 opj_image_t *image = j2k->image;
2037 opj_cp_t *cp = j2k->cp;
2039 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2040 lenp = cio_tell(cio);
2042 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2043 cio_write(cio, image->x1, 4); /* Xsiz */
2044 cio_write(cio, image->y1, 4); /* Ysiz */
2045 cio_write(cio, image->x0, 4); /* X0siz */
2046 cio_write(cio, image->y0, 4); /* Y0siz */
2047 cio_write(cio, cp->tdx, 4); /* XTsiz */
2048 cio_write(cio, cp->tdy, 4); /* YTsiz */
2049 cio_write(cio, cp->tx0, 4); /* XT0siz */
2050 cio_write(cio, cp->ty0, 4); /* YT0siz */
2051 cio_write(cio, image->numcomps, 2); /* Csiz */
2052 for (i = 0; i < image->numcomps; i++) {
2053 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2054 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2055 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2057 len = cio_tell(cio) - lenp;
2058 cio_seek(cio, lenp);
2059 cio_write(cio, len, 2); /* Lsiz */
2060 cio_seek(cio, lenp + len);
2063 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2067 * Writes the SIZ marker (image and tile size)
2069 * @param p_stream the stream to write data to.
2070 * @param p_j2k J2K codec.
2071 * @param p_manager the user event manager.
2073 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2074 struct opj_stream_private *p_stream,
2075 struct opj_event_mgr * p_manager )
2078 OPJ_UINT32 l_size_len;
2079 OPJ_BYTE * l_current_ptr;
2080 opj_image_t * l_image = 00;
2081 opj_cp_v2_t *cp = 00;
2082 opj_image_comp_t * l_img_comp = 00;
2085 assert(p_stream != 00);
2086 assert(p_j2k != 00);
2087 assert(p_manager != 00);
2089 l_image = p_j2k->m_private_image;
2090 cp = &(p_j2k->m_cp);
2091 l_size_len = 40 + 3 * l_image->numcomps;
2092 l_img_comp = l_image->comps;
2094 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2096 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2097 = (OPJ_BYTE*)opj_realloc(
2098 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2100 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2104 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2107 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2109 /* write SOC identifier */
2110 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2113 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2116 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2119 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2122 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2125 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2128 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2131 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2134 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2137 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2140 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2143 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2146 for (i = 0; i < l_image->numcomps; ++i) {
2147 /* TODO here with MCT ? */
2148 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2151 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2154 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2160 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) {
2170 * Reads a SIZ marker (image and tile size)
2171 * @param p_header_data the data contained in the SIZ box.
2172 * @param jp2 the jpeg2000 file codec.
2173 * @param p_header_size the size of the data contained in the SIZ marker.
2174 * @param p_manager the user event manager.
2176 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2177 OPJ_BYTE * p_header_data,
2178 OPJ_UINT32 p_header_size,
2179 opj_event_mgr_t * p_manager
2182 OPJ_UINT32 l_size, i;
2183 OPJ_UINT32 l_nb_comp;
2184 OPJ_UINT32 l_nb_comp_remain;
2185 OPJ_UINT32 l_remaining_size;
2186 OPJ_UINT32 l_nb_tiles;
2188 opj_image_t *l_image = 00;
2189 opj_cp_v2_t *l_cp = 00;
2190 opj_image_comp_t * l_img_comp = 00;
2191 opj_tcp_v2_t * l_current_tile_param = 00;
2194 assert(p_j2k != 00);
2195 assert(p_manager != 00);
2196 assert(p_header_data != 00);
2198 l_image = p_j2k->m_private_image;
2199 l_cp = &(p_j2k->m_cp);
2201 /* minimum size == 39 - 3 (= minimum component parameter) */
2202 if (p_header_size < 36) {
2203 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2207 l_remaining_size = p_header_size - 36;
2208 l_nb_comp = l_remaining_size / 3;
2209 l_nb_comp_remain = l_remaining_size % 3;
2210 if (l_nb_comp_remain != 0){
2211 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2215 l_size = p_header_size + 2; /* Lsiz */
2217 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2219 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2220 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2222 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2224 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2226 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2228 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2230 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2232 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2234 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2236 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2239 l_image->numcomps = (OPJ_UINT16) l_tmp;
2241 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2245 if (l_image->numcomps != l_nb_comp) {
2246 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp);
2251 if (l_cp->correct) {
2252 /* if JPWL is on, we check whether TX errors have damaged
2253 too much the SIZ parameters */
2254 if (!(l_image->x1 * l_image->y1)) {
2255 opj_event_msg_v2(p_manager, EVT_ERROR,
2256 "JPWL: bad image size (%d x %d)\n",
2257 l_image->x1, l_image->y1);
2258 if (!JPWL_ASSUME || JPWL_ASSUME) {
2259 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2264 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2265 if (l_image->numcomps != ((len - 38) / 3)) {
2266 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2267 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2268 l_image->numcomps, ((len - 38) / 3));
2270 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2273 */ /* we try to correct */
2274 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2275 if (l_image->numcomps < ((len - 38) / 3)) {
2276 len = 38 + 3 * l_image->numcomps;
2277 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2280 l_image->numcomps = ((len - 38) / 3);
2281 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2287 /* update components number in the jpwl_exp_comps filed */
2288 l_cp->exp_comps = l_image->numcomps;
2290 #endif /* USE_JPWL */
2292 /* Allocate the resulting image components */
2293 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2294 if (l_image->comps == 00){
2295 l_image->numcomps = 0;
2296 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2300 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2301 l_img_comp = l_image->comps;
2303 /* Read the component information */
2304 for (i = 0; i < l_image->numcomps; ++i){
2306 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2308 l_img_comp->prec = (tmp & 0x7f) + 1;
2309 l_img_comp->sgnd = tmp >> 7;
2310 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2312 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2313 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2315 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2318 if (l_cp->correct) {
2319 /* if JPWL is on, we check whether TX errors have damaged
2320 too much the SIZ parameters, again */
2321 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2322 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2323 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2324 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2326 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2329 /* we try to correct */
2330 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2331 if (!l_image->comps[i].dx) {
2332 l_image->comps[i].dx = 1;
2333 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2334 i, l_image->comps[i].dx);
2336 if (!l_image->comps[i].dy) {
2337 l_image->comps[i].dy = 1;
2338 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2339 i, l_image->comps[i].dy);
2343 #endif /* USE_JPWL */
2344 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2345 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2349 /* Compute the number of tiles */
2350 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2351 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2352 l_nb_tiles = l_cp->tw * l_cp->th;
2354 /* Define the tiles which will be decoded */
2355 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2356 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
2357 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
2358 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
2359 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
2362 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2363 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2364 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2365 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2369 if (l_cp->correct) {
2370 /* if JPWL is on, we check whether TX errors have damaged
2371 too much the SIZ parameters */
2372 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2373 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2374 "JPWL: bad number of tiles (%d x %d)\n",
2375 l_cp->tw, l_cp->th);
2377 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2380 /* we try to correct */
2381 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2384 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2387 if (l_cp->tw > l_cp->max_tiles) {
2389 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2390 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2391 l_cp->max_tiles, l_cp->tw);
2395 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2398 if (l_cp->th > l_cp->max_tiles) {
2400 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2401 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2402 l_cp->max_tiles, l_cp->th);
2406 #endif /* USE_JPWL */
2408 /* memory allocations */
2409 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2410 if (l_cp->tcps == 00) {
2411 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2414 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2417 if (l_cp->correct) {
2419 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2420 "JPWL: could not alloc tcps field of cp\n");
2421 if (!JPWL_ASSUME || JPWL_ASSUME) {
2422 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2427 #endif /* USE_JPWL */
2429 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2430 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2431 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2435 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2437 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2438 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2440 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2441 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2444 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2445 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2447 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2448 (opj_simple_mcc_decorrelation_data_t*)
2449 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2451 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2452 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2455 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2456 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2458 /* set up default dc level shift */
2459 for (i=0;i<l_image->numcomps;++i) {
2460 if (! l_image->comps[i].sgnd) {
2461 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2465 l_current_tile_param = l_cp->tcps;
2466 for (i = 0; i < l_nb_tiles; ++i) {
2467 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2468 if (l_current_tile_param->tccps == 00) {
2469 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2472 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2474 ++l_current_tile_param;
2477 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2478 opj_image_comp_header_update(l_image,l_cp);
2485 static void j2k_write_com(opj_j2k_t *j2k) {
2489 if(j2k->cp->comment) {
2490 opj_cio_t *cio = j2k->cio;
2491 char *comment = j2k->cp->comment;
2493 cio_write(cio, J2K_MS_COM, 2);
2494 lenp = cio_tell(cio);
2496 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2497 for (i = 0; i < strlen(comment); i++) {
2498 cio_write(cio, comment[i], 1);
2500 len = cio_tell(cio) - lenp;
2501 cio_seek(cio, lenp);
2502 cio_write(cio, len, 2);
2503 cio_seek(cio, lenp + len);
2507 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2513 * Writes the COM marker (comment)
2515 * @param p_stream the stream to write data to.
2516 * @param p_j2k J2K codec.
2517 * @param p_manager the user event manager.
2519 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2520 struct opj_stream_private *p_stream,
2521 struct opj_event_mgr * p_manager )
2523 OPJ_UINT32 l_comment_size;
2524 OPJ_UINT32 l_total_com_size;
2525 const OPJ_CHAR *l_comment;
2526 OPJ_BYTE * l_current_ptr = 00;
2529 assert(p_j2k != 00);
2530 assert(p_stream != 00);
2531 assert(p_manager != 00);
2533 l_comment = p_j2k->m_cp.comment;
2534 l_comment_size = strlen(l_comment);
2535 l_total_com_size = l_comment_size + 6;
2537 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2538 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2539 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2542 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2546 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2549 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2551 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2554 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2557 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2560 memcpy( l_current_ptr,l_comment,l_comment_size);
2562 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2570 * Reads a COM marker (comments)
2571 * @param p_header_data the data contained in the COM box.
2572 * @param jp2 the jpeg2000 file codec.
2573 * @param p_header_size the size of the data contained in the COM marker.
2574 * @param p_manager the user event manager.
2576 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2577 OPJ_BYTE * p_header_data,
2578 OPJ_UINT32 p_header_size,
2579 opj_event_mgr_t * p_manager
2583 assert(p_j2k != 00);
2584 assert(p_manager != 00);
2585 assert(p_header_data != 00);
2586 (void)p_header_size;
2591 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2594 opj_cp_t *cp = j2k->cp;
2595 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2596 opj_tccp_t *tccp = &tcp->tccps[compno];
2597 opj_cio_t *cio = j2k->cio;
2599 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2600 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2601 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2602 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2603 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2605 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2606 for (i = 0; i < tccp->numresolutions; i++) {
2607 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2613 static void j2k_write_cod(opj_j2k_t *j2k) {
2614 opj_cp_t *cp = NULL;
2615 opj_tcp_t *tcp = NULL;
2618 opj_cio_t *cio = j2k->cio;
2620 cio_write(cio, J2K_MS_COD, 2); /* COD */
2622 lenp = cio_tell(cio);
2626 tcp = &cp->tcps[j2k->curtileno];
2628 cio_write(cio, tcp->csty, 1); /* Scod */
2629 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2630 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2631 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2633 j2k_write_cox(j2k, 0);
2634 len = cio_tell(cio) - lenp;
2635 cio_seek(cio, lenp);
2636 cio_write(cio, len, 2); /* Lcod */
2637 cio_seek(cio, lenp + len);
2640 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2645 * Writes the COD marker (Coding style default)
2647 * @param p_stream the stream to write data to.
2648 * @param p_j2k J2K codec.
2649 * @param p_manager the user event manager.
2651 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2652 struct opj_stream_private *p_stream,
2653 struct opj_event_mgr * p_manager )
2655 opj_cp_v2_t *l_cp = 00;
2656 opj_tcp_v2_t *l_tcp = 00;
2657 OPJ_UINT32 l_code_size,l_remaining_size;
2658 OPJ_BYTE * l_current_data = 00;
2661 assert(p_j2k != 00);
2662 assert(p_manager != 00);
2663 assert(p_stream != 00);
2665 l_cp = &(p_j2k->m_cp);
2666 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2667 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2668 l_remaining_size = l_code_size;
2670 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2671 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2672 = (OPJ_BYTE*)opj_realloc(
2673 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2676 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2680 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2683 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2685 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2686 l_current_data += 2;
2688 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2689 l_current_data += 2;
2691 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2694 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2697 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2700 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2703 l_remaining_size -= 9;
2705 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2710 if (l_remaining_size != 0) {
2711 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2715 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) {
2724 * Reads a COD marker (Coding Styke defaults)
2725 * @param p_header_data the data contained in the COD box.
2726 * @param p_j2k the jpeg2000 codec.
2727 * @param p_header_size the size of the data contained in the COD marker.
2728 * @param p_manager the user event manager.
2730 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2731 OPJ_BYTE * p_header_data,
2732 OPJ_UINT32 p_header_size,
2733 opj_event_mgr_t * p_manager
2739 opj_cp_v2_t *l_cp = 00;
2740 opj_tcp_v2_t *l_tcp = 00;
2741 opj_image_t *l_image = 00;
2744 assert(p_header_data != 00);
2745 assert(p_j2k != 00);
2746 assert(p_manager != 00);
2748 l_image = p_j2k->m_private_image;
2749 l_cp = &(p_j2k->m_cp);
2751 /* If we are in the first tile-part header of the current tile */
2752 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2753 &l_cp->tcps[p_j2k->m_current_tile_number] :
2754 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2756 /* Make sure room is sufficient */
2757 if (p_header_size < 5) {
2758 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2762 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2764 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2766 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2767 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2770 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2771 if (l_cp->m_specific_param.m_dec.m_layer) {
2772 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2775 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2778 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2782 for (i = 0; i < l_image->numcomps; ++i) {
2783 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2786 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2787 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2791 if (p_header_size != 0) {
2792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2796 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2797 j2k_copy_tile_component_parameters(p_j2k);
2800 #ifdef WIP_REMOVE_MSD
2801 if (p_j2k->cstr_info) {
2802 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2803 p_j2k->cstr_info->prog = l_tcp->prg;
2804 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2805 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2806 for (i = 0; i < l_image->numcomps; ++i) {
2807 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2815 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2818 opj_cp_t *cp = j2k->cp;
2819 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2820 opj_image_t *image = j2k->image;
2821 opj_cio_t *cio = j2k->cio;
2823 cio_write(cio, J2K_MS_COC, 2); /* COC */
2824 lenp = cio_tell(cio);
2826 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2827 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2828 j2k_write_cox(j2k, compno);
2829 len = cio_tell(cio) - lenp;
2830 cio_seek(cio, lenp);
2831 cio_write(cio, len, 2); /* Lcoc */
2832 cio_seek(cio, lenp + len);
2836 * Writes the COC marker (Coding style component)
2838 * @param p_comp_no the index of the component to output.
2839 * @param p_stream the stream to write data to.
2840 * @param p_j2k J2K codec.
2841 * @param p_manager the user event manager.
2843 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2844 OPJ_UINT32 p_comp_no,
2845 struct opj_stream_private *p_stream,
2846 struct opj_event_mgr * p_manager )
2848 OPJ_UINT32 l_coc_size,l_remaining_size;
2849 OPJ_UINT32 l_comp_room;
2852 assert(p_j2k != 00);
2853 assert(p_manager != 00);
2854 assert(p_stream != 00);
2856 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2858 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2860 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2861 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2862 = (OPJ_BYTE*)opj_realloc(
2863 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2865 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2869 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2872 j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
2874 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) {
2882 * Writes the COC marker (Coding style component)
2884 * @param p_comp_no the index of the component to output.
2885 * @param p_stream the stream to write data to.
2886 * @param p_j2k J2K codec.
2887 * @param p_manager the user event manager.
2889 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2890 OPJ_UINT32 p_comp_no,
2892 OPJ_UINT32 * p_data_written,
2893 struct opj_event_mgr * p_manager )
2895 opj_cp_v2_t *l_cp = 00;
2896 opj_tcp_v2_t *l_tcp = 00;
2897 OPJ_UINT32 l_coc_size,l_remaining_size;
2898 OPJ_BYTE * l_current_data = 00;
2899 opj_image_t *l_image = 00;
2900 OPJ_UINT32 l_comp_room;
2903 assert(p_j2k != 00);
2904 assert(p_manager != 00);
2906 l_cp = &(p_j2k->m_cp);
2907 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2908 l_image = p_j2k->m_private_image;
2909 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2911 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2912 l_remaining_size = l_coc_size;
2914 l_current_data = p_data;
2916 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2917 l_current_data += 2;
2919 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2920 l_current_data += 2;
2922 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2923 l_current_data+=l_comp_room;
2925 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2928 l_remaining_size -= (5 + l_comp_room);
2929 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2930 * p_data_written = l_coc_size;
2934 * Gets the maximum size taken by a coc.
2936 * @param p_j2k the jpeg2000 codec to use.
2938 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2941 OPJ_UINT32 l_nb_comp;
2942 OPJ_UINT32 l_nb_tiles;
2943 OPJ_UINT32 l_max = 0;
2947 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2948 l_nb_comp = p_j2k->m_private_image->numcomps;
2950 for (i=0;i<l_nb_tiles;++i) {
2951 for (j=0;j<l_nb_comp;++j) {
2952 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2961 * Reads a COC marker (Coding Style Component)
2962 * @param p_header_data the data contained in the COC box.
2963 * @param p_j2k the jpeg2000 codec.
2964 * @param p_header_size the size of the data contained in the COC marker.
2965 * @param p_manager the user event manager.
2967 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2968 OPJ_BYTE * p_header_data,
2969 OPJ_UINT32 p_header_size,
2970 opj_event_mgr_t * p_manager
2973 opj_cp_v2_t *l_cp = NULL;
2974 opj_tcp_v2_t *l_tcp = NULL;
2975 opj_image_t *l_image = NULL;
2976 OPJ_UINT32 l_comp_room;
2977 OPJ_UINT32 l_comp_no;
2980 assert(p_header_data != 00);
2981 assert(p_j2k != 00);
2982 assert(p_manager != 00);
2984 l_cp = &(p_j2k->m_cp);
2985 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2986 &l_cp->tcps[p_j2k->m_current_tile_number] :
2987 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2988 l_image = p_j2k->m_private_image;
2990 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2992 /* make sure room is sufficient*/
2993 if (p_header_size < l_comp_room + 1) {
2994 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2997 p_header_size -= l_comp_room + 1;
2999 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3000 p_header_data += l_comp_room;
3001 if (l_comp_no >= l_image->numcomps) {
3002 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3006 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3009 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3010 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3014 if (p_header_size != 0) {
3015 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3021 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3022 int bandno, numbands;
3025 opj_cp_t *cp = j2k->cp;
3026 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3027 opj_tccp_t *tccp = &tcp->tccps[compno];
3028 opj_cio_t *cio = j2k->cio;
3030 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3031 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3033 for (bandno = 0; bandno < numbands; bandno++) {
3034 expn = tccp->stepsizes[bandno].expn;
3035 mant = tccp->stepsizes[bandno].mant;
3037 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3038 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3040 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3046 static void j2k_write_qcd(opj_j2k_t *j2k) {
3049 opj_cio_t *cio = j2k->cio;
3051 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3052 lenp = cio_tell(cio);
3054 j2k_write_qcx(j2k, 0);
3055 len = cio_tell(cio) - lenp;
3056 cio_seek(cio, lenp);
3057 cio_write(cio, len, 2); /* Lqcd */
3058 cio_seek(cio, lenp + len);
3061 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3065 * Writes the QCD marker (quantization default)
3067 * @param p_comp_number the index of the component to output.
3068 * @param p_stream the stream to write data to.
3069 * @param p_j2k J2K codec.
3070 * @param p_manager the user event manager.
3072 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3073 struct opj_stream_private *p_stream,
3074 struct opj_event_mgr * p_manager )
3076 opj_cp_v2_t *l_cp = 00;
3077 opj_tcp_v2_t *l_tcp = 00;
3078 OPJ_UINT32 l_qcd_size,l_remaining_size;
3079 OPJ_BYTE * l_current_data = 00;
3082 assert(p_j2k != 00);
3083 assert(p_manager != 00);
3084 assert(p_stream != 00);
3086 l_cp = &(p_j2k->m_cp);
3087 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3088 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3089 l_remaining_size = l_qcd_size;
3091 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3092 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3093 = (OPJ_BYTE*)opj_realloc(
3094 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3097 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3101 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3104 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3106 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3107 l_current_data += 2;
3109 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3110 l_current_data += 2;
3112 l_remaining_size -= 4;
3114 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3115 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3119 if (l_remaining_size != 0) {
3120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3124 if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) {
3132 * Reads a QCD marker (Quantization defaults)
3133 * @param p_header_data the data contained in the QCD box.
3134 * @param p_j2k the jpeg2000 codec.
3135 * @param p_header_size the size of the data contained in the QCD marker.
3136 * @param p_manager the user event manager.
3138 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3139 OPJ_BYTE * p_header_data,
3140 OPJ_UINT32 p_header_size,
3141 opj_event_mgr_t * p_manager
3145 assert(p_header_data != 00);
3146 assert(p_j2k != 00);
3147 assert(p_manager != 00);
3149 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3150 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3154 if (p_header_size != 0) {
3155 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3159 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3160 j2k_copy_tile_quantization_parameters(p_j2k);
3165 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3168 opj_cio_t *cio = j2k->cio;
3170 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3171 lenp = cio_tell(cio);
3173 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3174 j2k_write_qcx(j2k, compno);
3175 len = cio_tell(cio) - lenp;
3176 cio_seek(cio, lenp);
3177 cio_write(cio, len, 2); /* Lqcc */
3178 cio_seek(cio, lenp + len);
3182 * Writes the QCC marker (quantization component)
3184 * @param p_comp_no the index of the component to output.
3185 * @param p_stream the stream to write data to.
3186 * @param p_j2k J2K codec.
3187 * @param p_manager the user event manager.
3189 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3190 OPJ_UINT32 p_comp_no,
3191 struct opj_stream_private *p_stream,
3192 struct opj_event_mgr * p_manager )
3194 OPJ_UINT32 l_qcc_size,l_remaining_size;
3197 assert(p_j2k != 00);
3198 assert(p_manager != 00);
3199 assert(p_stream != 00);
3201 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3202 l_remaining_size = l_qcc_size;
3204 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3205 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3206 = (OPJ_BYTE*)opj_realloc(
3207 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3209 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3213 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3216 j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3218 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) {
3226 * Writes the QCC marker (quantization component)
3228 * @param p_comp_no the index of the component to output.
3229 * @param p_stream the stream to write data to.
3230 * @param p_j2k J2K codec.
3231 * @param p_manager the user event manager.
3233 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3234 OPJ_UINT32 p_comp_no,
3236 OPJ_UINT32 * p_data_written,
3237 struct opj_event_mgr * p_manager )
3239 OPJ_UINT32 l_qcc_size,l_remaining_size;
3240 OPJ_BYTE * l_current_data = 00;
3243 assert(p_j2k != 00);
3244 assert(p_manager != 00);
3246 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3247 l_remaining_size = l_qcc_size;
3249 l_current_data = p_data;
3251 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3252 l_current_data += 2;
3254 if (p_j2k->m_private_image->numcomps <= 256) {
3257 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3258 l_current_data += 2;
3260 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3263 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3264 l_remaining_size -= 6;
3267 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3268 l_current_data += 2;
3270 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3273 l_remaining_size -= 6;
3276 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3278 *p_data_written = l_qcc_size;
3282 * Gets the maximum size taken by a qcc.
3284 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3286 return j2k_get_max_coc_size(p_j2k);
3290 * Reads a QCC marker (Quantization component)
3291 * @param p_header_data the data contained in the QCC box.
3292 * @param p_j2k the jpeg2000 codec.
3293 * @param p_header_size the size of the data contained in the QCC marker.
3294 * @param p_manager the user event manager.
3296 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3297 OPJ_BYTE * p_header_data,
3298 OPJ_UINT32 p_header_size,
3299 opj_event_mgr_t * p_manager
3302 OPJ_UINT32 l_num_comp,l_comp_no;
3305 assert(p_header_data != 00);
3306 assert(p_j2k != 00);
3307 assert(p_manager != 00);
3309 l_num_comp = p_j2k->m_private_image->numcomps;
3311 if (l_num_comp <= 256) {
3312 if (p_header_size < 1) {
3313 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3316 opj_read_bytes(p_header_data,&l_comp_no,1);
3321 if (p_header_size < 2) {
3322 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3325 opj_read_bytes(p_header_data,&l_comp_no,2);
3331 if (p_j2k->m_cp.correct) {
3333 static OPJ_UINT32 backup_compno = 0;
3335 /* compno is negative or larger than the number of components!!! */
3336 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3337 opj_event_msg_v2(p_manager, EVT_ERROR,
3338 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3339 l_comp_no, l_num_comp);
3341 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3344 /* we try to correct */
3345 l_comp_no = backup_compno % l_num_comp;
3346 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3347 "- setting component number to %d\n",
3351 /* keep your private count of tiles */
3354 #endif /* USE_JPWL */
3356 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3357 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3361 if (p_header_size != 0) {
3362 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3370 static void j2k_write_poc(opj_j2k_t *j2k) {
3371 int len, numpchgs, i;
3373 int numcomps = j2k->image->numcomps;
3375 opj_cp_t *cp = j2k->cp;
3376 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3377 opj_tccp_t *tccp = &tcp->tccps[0];
3378 opj_cio_t *cio = j2k->cio;
3380 numpchgs = 1 + tcp->numpocs;
3381 cio_write(cio, J2K_MS_POC, 2); /* POC */
3382 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3383 cio_write(cio, len, 2); /* Lpoc */
3384 for (i = 0; i < numpchgs; i++) {
3385 opj_poc_t *poc = &tcp->pocs[i];
3386 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3387 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3388 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3389 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3390 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3391 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3392 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3393 poc->compno1 = int_min(poc->compno1, numcomps);
3394 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3399 * Writes the POC marker (Progression Order Change)
3401 * @param p_stream the stream to write data to.
3402 * @param p_j2k J2K codec.
3403 * @param p_manager the user event manager.
3405 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3406 struct opj_stream_private *p_stream,
3407 struct opj_event_mgr * p_manager )
3409 OPJ_UINT32 l_nb_comp;
3410 OPJ_UINT32 l_nb_poc;
3411 OPJ_UINT32 l_poc_size;
3412 OPJ_UINT32 l_written_size = 0;
3413 opj_tcp_v2_t *l_tcp = 00;
3414 opj_tccp_t *l_tccp = 00;
3415 OPJ_UINT32 l_poc_room;
3418 assert(p_j2k != 00);
3419 assert(p_manager != 00);
3420 assert(p_stream != 00);
3422 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3423 l_tccp = &l_tcp->tccps[0];
3424 l_nb_comp = p_j2k->m_private_image->numcomps;
3425 l_nb_poc = 1 + l_tcp->numpocs;
3427 if (l_nb_comp <= 256) {
3433 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3435 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3436 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3437 = (OPJ_BYTE*)opj_realloc(
3438 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3441 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3445 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3448 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3450 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
3459 * Writes the POC marker (Progression Order Change)
3461 * @param p_stream the stream to write data to.
3462 * @param p_j2k J2K codec.
3463 * @param p_manager the user event manager.
3465 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3467 OPJ_UINT32 * p_data_written,
3468 struct opj_event_mgr * p_manager )
3471 OPJ_BYTE * l_current_data = 00;
3472 OPJ_UINT32 l_nb_comp;
3473 OPJ_UINT32 l_nb_poc;
3474 OPJ_UINT32 l_poc_size;
3475 opj_image_t *l_image = 00;
3476 opj_tcp_v2_t *l_tcp = 00;
3477 opj_tccp_t *l_tccp = 00;
3478 opj_poc_t *l_current_poc = 00;
3479 OPJ_UINT32 l_poc_room;
3482 assert(p_j2k != 00);
3483 assert(p_manager != 00);
3485 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3486 l_tccp = &l_tcp->tccps[0];
3487 l_image = p_j2k->m_private_image;
3488 l_nb_comp = l_image->numcomps;
3489 l_nb_poc = 1 + l_tcp->numpocs;
3491 if (l_nb_comp <= 256) {
3498 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3500 l_current_data = p_data;
3502 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3503 l_current_data += 2;
3505 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3506 l_current_data += 2;
3508 l_current_poc = l_tcp->pocs;
3509 for (i = 0; i < l_nb_poc; ++i) {
3510 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3513 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3514 l_current_data+=l_poc_room;
3516 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3519 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3522 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3523 l_current_data+=l_poc_room;
3525 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3528 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3529 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3530 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3531 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3536 *p_data_written = l_poc_size;
3540 * Gets the maximum size taken by the writing of a POC.
3542 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3544 opj_tcp_v2_t * l_tcp = 00;
3545 OPJ_UINT32 l_nb_tiles = 0;
3546 OPJ_UINT32 l_max_poc = 0;
3549 l_tcp = p_j2k->m_cp.tcps;
3550 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3552 for (i=0;i<l_nb_tiles;++i) {
3553 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3559 return 4 + 9 * l_max_poc;
3563 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3565 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3568 OPJ_UINT32 l_nb_tiles;
3569 OPJ_UINT32 l_max = 0;
3570 opj_tcp_v2_t * l_tcp = 00;
3572 l_tcp = p_j2k->m_cp.tcps;
3573 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3575 for (i=0;i<l_nb_tiles;++i) {
3576 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3586 * Gets the maximum size taken by the headers of the SOT.
3588 * @param p_j2k the jpeg2000 codec to use.
3590 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3592 OPJ_UINT32 l_nb_bytes = 0;
3593 OPJ_UINT32 l_nb_comps;
3594 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3596 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3597 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3599 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3600 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3601 l_nb_bytes += l_nb_comps * l_coc_bytes;
3603 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3604 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3607 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3609 /*** DEVELOPER CORNER, Add room for your headers ***/
3616 * Reads a POC marker (Progression Order Change)
3618 * @param p_header_data the data contained in the POC box.
3619 * @param p_j2k the jpeg2000 codec.
3620 * @param p_header_size the size of the data contained in the POC marker.
3621 * @param p_manager the user event manager.
3623 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3624 OPJ_BYTE * p_header_data,
3625 OPJ_UINT32 p_header_size,
3626 opj_event_mgr_t * p_manager
3629 OPJ_UINT32 i, l_nb_comp, l_tmp;
3630 opj_image_t * l_image = 00;
3631 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3632 OPJ_UINT32 l_chunk_size, l_comp_room;
3634 opj_cp_v2_t *l_cp = 00;
3635 opj_tcp_v2_t *l_tcp = 00;
3636 opj_poc_t *l_current_poc = 00;
3639 assert(p_header_data != 00);
3640 assert(p_j2k != 00);
3641 assert(p_manager != 00);
3643 l_image = p_j2k->m_private_image;
3644 l_nb_comp = l_image->numcomps;
3645 if (l_nb_comp <= 256) {
3651 l_chunk_size = 5 + 2 * l_comp_room;
3652 l_current_poc_nb = p_header_size / l_chunk_size;
3653 l_current_poc_remaining = p_header_size % l_chunk_size;
3655 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3656 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3660 l_cp = &(p_j2k->m_cp);
3661 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3662 &l_cp->tcps[p_j2k->m_current_tile_number] :
3663 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3664 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3665 l_current_poc_nb += l_old_poc_nb;
3667 assert(l_current_poc_nb < 32);
3669 /* now poc is in use.*/
3672 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3673 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3674 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3676 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3677 p_header_data+=l_comp_room;
3678 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3680 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3682 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3683 p_header_data+=l_comp_room;
3684 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3686 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3687 /* make sure comp is in acceptable bounds */
3688 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3692 l_tcp->numpocs = l_current_poc_nb - 1;
3697 * Reads a CRG marker (Component registration)
3699 * @param p_header_data the data contained in the TLM box.
3700 * @param p_j2k the jpeg2000 codec.
3701 * @param p_header_size the size of the data contained in the TLM marker.
3702 * @param p_manager the user event manager.
3704 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3705 OPJ_BYTE * p_header_data,
3706 OPJ_UINT32 p_header_size,
3707 opj_event_mgr_t * p_manager
3710 OPJ_UINT32 l_nb_comp;
3712 assert(p_header_data != 00);
3713 assert(p_j2k != 00);
3714 assert(p_manager != 00);
3716 l_nb_comp = p_j2k->m_private_image->numcomps;
3718 if (p_header_size != l_nb_comp *4) {
3719 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3722 /* Do not care of this at the moment since only local variables are set here */
3725 (i = 0; i < l_nb_comp; ++i)
3727 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3729 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3737 * Reads a TLM marker (Tile Length Marker)
3739 * @param p_header_data the data contained in the TLM box.
3740 * @param p_j2k the jpeg2000 codec.
3741 * @param p_header_size the size of the data contained in the TLM marker.
3742 * @param p_manager the user event manager.
3744 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3745 OPJ_BYTE * p_header_data,
3746 OPJ_UINT32 p_header_size,
3747 opj_event_mgr_t * p_manager
3750 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3752 assert(p_header_data != 00);
3753 assert(p_j2k != 00);
3754 assert(p_manager != 00);
3756 if (p_header_size < 2) {
3757 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3762 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3764 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3767 l_ST = ((l_Stlm >> 4) & 0x3);
3768 l_SP = (l_Stlm >> 6) & 0x1;
3770 l_Ptlm_size = (l_SP + 1) * 2;
3771 l_quotient = l_Ptlm_size + l_ST;
3773 l_tot_num_tp = p_header_size / l_quotient;
3774 l_tot_num_tp_remaining = p_header_size % l_quotient;
3776 if (l_tot_num_tp_remaining != 0) {
3777 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3780 /* FIXME Do not care of this at the moment since only local variables are set here */
3783 (i = 0; i < l_tot_num_tp; ++i)
3785 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3786 p_header_data += l_ST;
3787 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3788 p_header_data += l_Ptlm_size;
3795 * Reads a PLM marker (Packet length, main header marker)
3797 * @param p_header_data the data contained in the TLM box.
3798 * @param p_j2k the jpeg2000 codec.
3799 * @param p_header_size the size of the data contained in the TLM marker.
3800 * @param p_manager the user event manager.
3802 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3803 OPJ_BYTE * p_header_data,
3804 OPJ_UINT32 p_header_size,
3805 opj_event_mgr_t * p_manager
3809 assert(p_header_data != 00);
3810 assert(p_j2k != 00);
3811 assert(p_manager != 00);
3813 if (p_header_size < 1) {
3814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3817 /* Do not care of this at the moment since only local variables are set here */
3819 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3826 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3828 p_header_size -= (1+l_Nplm);
3832 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3836 (i = 0; i < l_Nplm; ++i)
3838 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3840 // take only the last seven bytes
3841 l_packet_len |= (l_tmp & 0x7f);
3849 // store packet length and proceed to next packet
3856 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3865 * Reads a PLT marker (Packet length, tile-part header)
3867 * @param p_header_data the data contained in the PLT box.
3868 * @param p_j2k the jpeg2000 codec.
3869 * @param p_header_size the size of the data contained in the PLT marker.
3870 * @param p_manager the user event manager.
3872 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3873 OPJ_BYTE * p_header_data,
3874 OPJ_UINT32 p_header_size,
3875 opj_event_mgr_t * p_manager
3878 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3881 assert(p_header_data != 00);
3882 assert(p_j2k != 00);
3883 assert(p_manager != 00);
3885 if (p_header_size < 1) {
3886 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3890 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3894 for (i = 0; i < p_header_size; ++i) {
3895 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3897 /* take only the last seven bytes */
3898 l_packet_len |= (l_tmp & 0x7f);
3903 /* store packet length and proceed to next packet */
3908 if (l_packet_len != 0) {
3909 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3918 * Reads a PPM marker (Packed packet headers, main header)
3920 * @param p_header_data the data contained in the POC box.
3921 * @param p_j2k the jpeg2000 codec.
3922 * @param p_header_size the size of the data contained in the POC marker.
3923 * @param p_manager the user event manager.
3926 opj_bool j2k_read_ppm_v2 (
3927 opj_j2k_v2_t *p_j2k,
3928 OPJ_BYTE * p_header_data,
3929 OPJ_UINT32 p_header_size,
3930 struct opj_event_mgr * p_manager
3934 opj_cp_v2_t *l_cp = 00;
3935 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3938 assert(p_header_data != 00);
3939 assert(p_j2k != 00);
3940 assert(p_manager != 00);
3942 if (p_header_size < 1) {
3943 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3947 l_cp = &(p_j2k->m_cp);
3950 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3954 /* First PPM marker */
3956 if (p_header_size < 4) {
3957 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3961 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3965 /* First PPM marker: Initialization */
3966 l_cp->ppm_len = l_N_ppm;
3967 l_cp->ppm_data_size = 0;
3969 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3970 if (l_cp->ppm_buffer == 00) {
3971 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3974 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3976 l_cp->ppm_data = l_cp->ppm_buffer;
3980 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3981 if (p_header_size >= 4) {
3983 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3986 l_cp->ppm_len += l_N_ppm ;
3988 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3989 if (l_cp->ppm_buffer == 00) {
3990 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3993 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3995 l_cp->ppm_data = l_cp->ppm_buffer;
4002 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4004 if (l_remaining_data <= p_header_size) {
4005 /* we must store less information than available in the packet */
4006 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4007 l_cp->ppm_data_size = l_cp->ppm_len;
4008 p_header_size -= l_remaining_data;
4009 p_header_data += l_remaining_data;
4012 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4013 l_cp->ppm_data_size += p_header_size;
4014 p_header_data += p_header_size;
4027 * Reads a PPM marker (Packed packet headers, main header)
4029 * @param p_header_data the data contained in the POC box.
4030 * @param p_j2k the jpeg2000 codec.
4031 * @param p_header_size the size of the data contained in the POC marker.
4032 * @param p_manager the user event manager.
4034 opj_bool j2k_read_ppm_v3 (
4035 opj_j2k_v2_t *p_j2k,
4036 OPJ_BYTE * p_header_data,
4037 OPJ_UINT32 p_header_size,
4038 struct opj_event_mgr * p_manager
4041 opj_cp_v2_t *l_cp = 00;
4042 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4045 assert(p_header_data != 00);
4046 assert(p_j2k != 00);
4047 assert(p_manager != 00);
4049 /* Minimum size of PPM marker is equal to the size of Zppm element */
4050 if (p_header_size < 1) {
4051 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4055 l_cp = &(p_j2k->m_cp);
4058 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4062 /* First PPM marker */
4064 /* We need now at least the Nppm^0 element */
4065 if (p_header_size < 4) {
4066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4070 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4074 /* First PPM marker: Initialization */
4075 l_cp->ppm_len = l_N_ppm;
4076 l_cp->ppm_data_read = 0;
4078 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4079 if (l_cp->ppm_data == 00) {
4080 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4083 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4085 l_cp->ppm_data_current = l_cp->ppm_data;
4087 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4090 if (p_header_size < 4) {
4091 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4095 /* Uncompleted Ippm series in the previous PPM marker?*/
4096 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4097 /* Get the place where add the remaining Ippm series*/
4098 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4099 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4102 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4106 /* Increase the size of ppm_data to add the new Ippm series*/
4107 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4109 /* Keep the position of the place where concatenate the new series*/
4110 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4111 l_cp->ppm_len += l_N_ppm;
4116 l_remaining_data = p_header_size;
4118 while (l_remaining_data >= l_N_ppm) {
4119 /* read a complete Ippm series*/
4120 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4121 p_header_size -= l_N_ppm;
4122 p_header_data += l_N_ppm;
4124 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4128 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4133 l_remaining_data = p_header_size;
4137 l_remaining_data = p_header_size;
4139 /* Next Ippm series is a complete series ?*/
4140 if (l_remaining_data > l_N_ppm) {
4141 /* Increase the size of ppm_data to add the new Ippm series*/
4142 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4144 /* Keep the position of the place where concatenate the new series */
4145 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4146 l_cp->ppm_len += l_N_ppm;
4151 /* Need to read an incomplete Ippm series*/
4152 if (l_remaining_data) {
4153 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4155 /* Keep the position of the place where concatenate the new series*/
4156 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4157 l_cp->ppm_len += l_N_ppm;
4159 /* Read incomplete Ippm series*/
4160 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4161 p_header_size -= l_remaining_data;
4162 p_header_data += l_remaining_data;
4164 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4169 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4170 if (p_header_size >= 4) {
4172 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4175 l_cp->ppm_len += l_N_ppm ;
4177 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4178 if (l_cp->ppm_buffer == 00) {
4179 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4182 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4184 l_cp->ppm_data = l_cp->ppm_buffer;
4191 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4193 if (l_remaining_data <= p_header_size) {
4194 /* we must store less information than available in the packet */
4195 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4196 l_cp->ppm_data_size = l_cp->ppm_len;
4197 p_header_size -= l_remaining_data;
4198 p_header_data += l_remaining_data;
4201 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4202 l_cp->ppm_data_size += p_header_size;
4203 p_header_data += p_header_size;
4213 * Reads a PPT marker (Packed packet headers, tile-part header)
4215 * @param p_header_data the data contained in the PPT box.
4216 * @param p_j2k the jpeg2000 codec.
4217 * @param p_header_size the size of the data contained in the PPT marker.
4218 * @param p_manager the user event manager.
4220 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4221 OPJ_BYTE * p_header_data,
4222 OPJ_UINT32 p_header_size,
4223 opj_event_mgr_t * p_manager
4226 opj_cp_v2_t *l_cp = 00;
4227 opj_tcp_v2_t *l_tcp = 00;
4231 assert(p_header_data != 00);
4232 assert(p_j2k != 00);
4233 assert(p_manager != 00);
4235 /* We need to have the Z_ppt element at minimum */
4236 if (p_header_size < 1) {
4237 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4241 l_cp = &(p_j2k->m_cp);
4243 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
4247 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4250 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4254 /* Allocate buffer to read the packet header */
4256 /* First PPT marker */
4257 l_tcp->ppt_data_size = 0;
4258 l_tcp->ppt_len = p_header_size;
4260 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4261 if (l_tcp->ppt_buffer == 00) {
4262 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4265 l_tcp->ppt_data = l_tcp->ppt_buffer;
4267 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4270 l_tcp->ppt_len += p_header_size;
4272 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4273 if (l_tcp->ppt_buffer == 00) {
4274 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4277 l_tcp->ppt_data = l_tcp->ppt_buffer;
4279 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4282 /* Read packet header from buffer */
4283 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4285 l_tcp->ppt_data_size += p_header_size;
4291 * Writes the TLM marker (Tile Length Marker)
4293 * @param p_stream the stream to write data to.
4294 * @param p_j2k J2K codec.
4295 * @param p_manager the user event manager.
4297 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4298 struct opj_stream_private *p_stream,
4299 struct opj_event_mgr * p_manager )
4301 OPJ_BYTE * l_current_data = 00;
4302 OPJ_UINT32 l_tlm_size;
4305 assert(p_j2k != 00);
4306 assert(p_manager != 00);
4307 assert(p_stream != 00);
4309 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4311 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4312 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4313 = (OPJ_BYTE*)opj_realloc(
4314 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4317 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4321 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4324 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4326 /* change the way data is written to avoid seeking if possible */
4328 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4330 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4331 l_current_data += 2;
4333 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4334 l_current_data += 2;
4336 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4339 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4342 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4343 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
4350 static void j2k_write_tlm(opj_j2k_t *j2k){
4352 opj_cio_t *cio = j2k->cio;
4353 j2k->tlm_start = cio_tell(cio);
4354 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4355 lenp = 4 + (5*j2k->totnum_tp);
4356 cio_write(cio,lenp,2); /* Ltlm */
4357 cio_write(cio, 0,1); /* Ztlm=0*/
4358 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4359 cio_skip(cio,5*j2k->totnum_tp);
4362 static void j2k_write_sot(opj_j2k_t *j2k) {
4365 opj_cio_t *cio = j2k->cio;
4367 j2k->sot_start = cio_tell(cio);
4368 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4369 lenp = cio_tell(cio);
4370 cio_skip(cio, 2); /* Lsot (further) */
4371 cio_write(cio, j2k->curtileno, 2); /* Isot */
4372 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4373 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4374 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4375 len = cio_tell(cio) - lenp;
4376 cio_seek(cio, lenp);
4377 cio_write(cio, len, 2); /* Lsot */
4378 cio_seek(cio, lenp + len);
4382 /* update markers struct */
4383 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4384 #endif /* USE_JPWL */
4387 if( j2k->cstr_info && j2k->cur_tp_num==0){
4388 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4393 * Writes the SOT marker (Start of tile-part)
4395 * @param p_stream the stream to write data to.
4396 * @param p_j2k J2K codec.
4397 * @param p_manager the user event manager.
4399 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4401 OPJ_UINT32 * p_data_written,
4402 const struct opj_stream_private *p_stream,
4403 struct opj_event_mgr * p_manager )
4406 assert(p_j2k != 00);
4407 assert(p_manager != 00);
4408 assert(p_stream != 00);
4410 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4413 opj_write_bytes(p_data,10,2); /* Lsot */
4416 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4422 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4425 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4430 /* update markers struct */
4432 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4434 assert( 0 && "TODO" );
4435 #endif /* USE_JPWL */
4437 * p_data_written = 12;
4446 * Reads a PPT marker (Packed packet headers, tile-part header)
4448 * @param p_header_data the data contained in the PPT box.
4449 * @param p_j2k the jpeg2000 codec.
4450 * @param p_header_size the size of the data contained in the PPT marker.
4451 * @param p_manager the user event manager.
4453 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4454 OPJ_BYTE * p_header_data,
4455 OPJ_UINT32 p_header_size,
4456 opj_event_mgr_t * p_manager )
4458 opj_cp_v2_t *l_cp = 00;
4459 opj_tcp_v2_t *l_tcp = 00;
4460 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4461 OPJ_UINT32 l_current_part;
4462 OPJ_UINT32 l_tile_x,l_tile_y;
4465 assert(p_header_data != 00);
4466 assert(p_j2k != 00);
4467 assert(p_manager != 00);
4469 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4470 if (p_header_size != 8) {
4471 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4475 l_cp = &(p_j2k->m_cp);
4476 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4479 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4480 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4481 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4484 if (l_cp->correct) {
4486 int tileno = p_j2k->m_current_tile_number;
4487 static int backup_tileno = 0;
4489 /* tileno is negative or larger than the number of tiles!!! */
4490 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4491 opj_event_msg_v2(p_manager, EVT_ERROR,
4492 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4493 tileno, (l_cp->tw * l_cp->th));
4495 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4498 /* we try to correct */
4499 tileno = backup_tileno;
4500 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4501 "- setting tile number to %d\n",
4505 /* keep your private count of tiles */
4508 #endif /* USE_JPWL */
4510 /* look for the tile in the list of already processed tile (in parts). */
4511 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4512 /* since the time taken by this function can only grow at the time */
4514 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4517 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4518 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4520 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4526 if (l_cp->correct) {
4528 /* totlen is negative or larger than the bytes left!!! */
4529 if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
4530 opj_event_msg_v2(p_manager, EVT_ERROR,
4531 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4532 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4534 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4537 /* we try to correct */
4539 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4540 "- setting Psot to %d => assuming it is the last tile\n",
4544 #endif /* USE_JPWL */
4546 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4548 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4549 "we assuming it is the last tile-part of the codestream.\n");
4550 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4553 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4556 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4559 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4560 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4561 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4562 if (l_tcp->m_nb_tile_parts) {
4563 if (l_current_part >= l_tcp->m_nb_tile_parts){
4564 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4565 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4566 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4570 l_tcp->m_nb_tile_parts = l_num_parts;
4573 /* If know the number of tile part header we will check if we didn't read the last*/
4574 if (l_tcp->m_nb_tile_parts) {
4575 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4576 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4580 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4581 /* Keep the size of data to skip after this marker */
4582 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4585 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4586 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4589 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4591 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4592 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4593 p_j2k->m_specific_param.m_decoder.m_skip_data =
4594 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4595 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4596 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4597 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4600 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4601 p_j2k->m_specific_param.m_decoder.m_skip_data =
4602 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4606 if (p_j2k->cstr_index)
4608 assert(p_j2k->cstr_index->tile_index != 00);
4609 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4610 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4612 if (l_num_parts != 0){
4613 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4614 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4617 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4618 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4619 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4621 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4622 (opj_tp_index_t*)opj_realloc(
4623 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4624 l_num_parts* sizeof(opj_tp_index_t));
4627 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4629 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4630 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4631 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4632 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4633 sizeof(opj_tp_index_t));
4636 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4637 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4638 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4639 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4640 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4641 * sizeof(opj_tp_index_t));
4650 /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
4651 /* if (p_j2k->cstr_info) {
4654 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4657 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4658 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4659 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4660 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4663 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4666 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4670 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4673 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4674 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4675 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4680 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4683 opj_tcp_t *tcp = NULL;
4684 opj_codestream_info_t *cstr_info = NULL;
4686 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4687 opj_cp_t *cp = j2k->cp;
4688 opj_cio_t *cio = j2k->cio;
4690 tcd->tp_num = j2k->tp_num ;
4691 tcd->cur_tp_num = j2k->cur_tp_num;
4693 cio_write(cio, J2K_MS_SOD, 2);
4695 if( j2k->cstr_info && j2k->cur_tp_num==0){
4696 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4699 if (j2k->curtileno == 0) {
4700 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4704 cstr_info = j2k->cstr_info;
4706 if (!j2k->cur_tp_num ) {
4707 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4708 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4711 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4712 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4716 /* update markers struct */
4717 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4718 #endif /* USE_JPWL */
4723 tcp = &cp->tcps[j2k->curtileno];
4724 for (layno = 0; layno < tcp->numlayers; layno++) {
4725 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4726 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4727 } else if (tcp->rates[layno]) {
4728 tcp->rates[layno]=1;
4731 if(j2k->cur_tp_num == 0){
4732 tcd->tcd_image->tiles->packno = 0;
4734 cstr_info->packno = 0;
4737 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4739 /* Writing Psot in SOT marker */
4740 totlen = cio_tell(cio) + l - j2k->sot_start;
4741 cio_seek(cio, j2k->sot_start + 6);
4742 cio_write(cio, totlen, 4);
4743 cio_seek(cio, j2k->sot_start + totlen);
4744 /* Writing Ttlm and Ptlm in TLM marker */
4746 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4747 cio_write(cio, j2k->curtileno, 1);
4748 cio_write(cio, totlen, 4);
4750 cio_seek(cio, j2k->sot_start + totlen);
4754 * Writes the SOD marker (Start of data)
4756 * @param p_stream the stream to write data to.
4757 * @param p_j2k J2K codec.
4758 * @param p_manager the user event manager.
4760 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4761 struct opj_tcd_v2 * p_tile_coder,
4763 OPJ_UINT32 * p_data_written,
4764 OPJ_UINT32 p_total_data_size,
4765 const struct opj_stream_private *p_stream,
4766 struct opj_event_mgr * p_manager )
4768 opj_tcp_v2_t *l_tcp = 00;
4769 opj_codestream_info_t *l_cstr_info = 00;
4770 opj_cp_v2_t *l_cp = 00;
4772 OPJ_UINT32 l_size_tile;
4773 OPJ_UINT32 l_remaining_data;
4776 assert(p_j2k != 00);
4777 assert(p_manager != 00);
4778 assert(p_stream != 00);
4780 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4783 /* make room for the EOF marker */
4784 l_remaining_data = p_total_data_size - 4;
4786 l_cp = &(p_j2k->m_cp);
4787 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4790 /* update tile coder */
4791 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4792 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4794 l_size_tile = l_cp->th * l_cp->tw;
4797 /* TODO mergeV2: check this part which use cstr_info */
4798 /*l_cstr_info = p_j2k->cstr_info;
4800 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4801 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4802 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4808 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4810 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4815 /* update markers struct */
4816 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4818 assert( 0 && "TODO" );
4819 #endif /* USE_JPWL */
4824 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4825 p_tile_coder->tcd_image->tiles->packno = 0;
4827 l_cstr_info->packno = 0;
4831 *p_data_written = 0;
4833 if (! tcd_encode_tile_v2(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
4834 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4838 *p_data_written += 2;
4847 * Reads a SOD marker (Start Of Data)
4849 * @param p_header_data the data contained in the SOD box.
4850 * @param p_j2k the jpeg2000 codec.
4851 * @param p_header_size the size of the data contained in the SOD marker.
4852 * @param p_manager the user event manager.
4854 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4855 opj_stream_private_t *p_stream,
4856 opj_event_mgr_t * p_manager
4859 OPJ_UINT32 l_current_read_size;
4860 opj_codestream_index_t * l_cstr_index = 00;
4861 OPJ_BYTE ** l_current_data = 00;
4862 opj_tcp_v2_t * l_tcp = 00;
4863 OPJ_UINT32 * l_tile_len = 00;
4866 assert(p_j2k != 00);
4867 assert(p_manager != 00);
4868 assert(p_stream != 00);
4870 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4872 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4873 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4874 // but we are in the last tile part,
4875 // so its result will fit on OPJ_UINT32 unless we find
4876 // a file with a single tile part of more than 4 GB...*/
4877 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4880 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4882 l_current_data = &(l_tcp->m_data);
4883 l_tile_len = &l_tcp->m_data_size;
4885 if (! *l_current_data) {
4886 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4889 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4892 if (*l_current_data == 00) {
4893 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4899 l_cstr_index = p_j2k->cstr_index;
4901 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4903 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4904 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4906 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4907 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4909 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4913 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4915 /*l_cstr_index->packno = 0;*/
4918 l_current_read_size = opj_stream_read_data( p_stream,
4919 *l_current_data + *l_tile_len,
4920 p_j2k->m_specific_param.m_decoder.m_sot_length,
4923 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4924 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4927 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4930 *l_tile_len += l_current_read_size;
4936 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4937 opj_cp_t *cp = j2k->cp;
4938 opj_tcp_t *tcp = &cp->tcps[tileno];
4939 opj_cio_t *cio = j2k->cio;
4940 int numcomps = j2k->image->numcomps;
4942 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4943 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4944 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4945 cio_write(cio, 0, 1); /* Srgn */
4946 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4950 * Writes the RGN marker (Region Of Interest)
4952 * @param p_tile_no the tile to output
4953 * @param p_comp_no the component to output
4954 * @param p_stream the stream to write data to.
4955 * @param p_j2k J2K codec.
4956 * @param p_manager the user event manager.
4958 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4959 OPJ_UINT32 p_tile_no,
4960 OPJ_UINT32 p_comp_no,
4961 struct opj_stream_private *p_stream,
4962 struct opj_event_mgr * p_manager )
4964 OPJ_BYTE * l_current_data = 00;
4965 OPJ_UINT32 l_nb_comp;
4966 OPJ_UINT32 l_rgn_size;
4967 opj_image_t *l_image = 00;
4968 opj_cp_v2_t *l_cp = 00;
4969 opj_tcp_v2_t *l_tcp = 00;
4970 opj_tccp_t *l_tccp = 00;
4971 OPJ_UINT32 l_comp_room;
4974 assert(p_j2k != 00);
4975 assert(p_manager != 00);
4976 assert(p_stream != 00);
4978 l_cp = &(p_j2k->m_cp);
4979 l_tcp = &l_cp->tcps[p_tile_no];
4980 l_tccp = &l_tcp->tccps[p_comp_no];
4982 l_nb_comp = l_image->numcomps;
4984 if (l_nb_comp <= 256) {
4991 l_rgn_size = 6 + l_comp_room;
4993 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4995 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4996 l_current_data += 2;
4998 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4999 l_current_data += 2;
5001 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5002 l_current_data+=l_comp_room;
5004 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5007 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5010 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
5018 static void j2k_write_eoc(opj_j2k_t *j2k) {
5019 opj_cio_t *cio = j2k->cio;
5020 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5021 cio_write(cio, J2K_MS_EOC, 2);
5025 /* update markers struct */
5026 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5027 #endif /* USE_JPWL */
5032 * Writes the EOC marker (End of Codestream)
5034 * @param p_stream the stream to write data to.
5035 * @param p_j2k J2K codec.
5036 * @param p_manager the user event manager.
5038 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5039 struct opj_stream_private *p_stream,
5040 struct opj_event_mgr * p_manager )
5043 assert(p_j2k != 00);
5044 assert(p_manager != 00);
5045 assert(p_stream != 00);
5047 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5052 /* update markers struct */
5054 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5056 #endif /* USE_JPWL */
5058 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5062 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5071 * Reads a RGN marker (Region Of Interest)
5073 * @param p_header_data the data contained in the POC box.
5074 * @param p_j2k the jpeg2000 codec.
5075 * @param p_header_size the size of the data contained in the POC marker.
5076 * @param p_manager the user event manager.
5078 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5079 OPJ_BYTE * p_header_data,
5080 OPJ_UINT32 p_header_size,
5081 opj_event_mgr_t * p_manager
5084 OPJ_UINT32 l_nb_comp;
5085 opj_image_t * l_image = 00;
5087 opj_cp_v2_t *l_cp = 00;
5088 opj_tcp_v2_t *l_tcp = 00;
5089 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5092 assert(p_header_data != 00);
5093 assert(p_j2k != 00);
5094 assert(p_manager != 00);
5096 l_image = p_j2k->m_private_image;
5097 l_nb_comp = l_image->numcomps;
5099 if (l_nb_comp <= 256) {
5104 if (p_header_size != 2 + l_comp_room) {
5105 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5109 l_cp = &(p_j2k->m_cp);
5110 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5111 &l_cp->tcps[p_j2k->m_current_tile_number] :
5112 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5114 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5115 p_header_data+=l_comp_room;
5116 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5120 if (l_cp->correct) {
5121 /* totlen is negative or larger than the bytes left!!! */
5122 if (l_comp_room >= l_nb_comp) {
5123 opj_event_msg_v2(p_manager, EVT_ERROR,
5124 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5125 l_comp_room, l_nb_comp);
5126 if (!JPWL_ASSUME || JPWL_ASSUME) {
5127 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5132 #endif /* USE_JPWL */
5134 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5141 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5143 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5146 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5153 * Updates the rates of the tcp.
5155 * @param p_stream the stream to write data to.
5156 * @param p_j2k J2K codec.
5157 * @param p_manager the user event manager.
5159 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5160 struct opj_stream_private *p_stream,
5161 struct opj_event_mgr * p_manager )
5163 opj_cp_v2_t * l_cp = 00;
5164 opj_image_t * l_image = 00;
5165 opj_tcp_v2_t * l_tcp = 00;
5166 opj_image_comp_t * l_img_comp = 00;
5169 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5170 OPJ_FLOAT32 * l_rates = 0;
5171 OPJ_FLOAT32 l_sot_remove;
5172 OPJ_UINT32 l_bits_empty, l_size_pixel;
5173 OPJ_UINT32 l_tile_size = 0;
5174 OPJ_UINT32 l_last_res;
5175 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5178 assert(p_j2k != 00);
5179 assert(p_manager != 00);
5180 assert(p_stream != 00);
5183 l_cp = &(p_j2k->m_cp);
5184 l_image = p_j2k->m_private_image;
5187 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5188 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5189 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5191 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5192 l_tp_stride_func = get_tp_stride;
5195 l_tp_stride_func = get_default_stride;
5198 for (i=0;i<l_cp->th;++i) {
5199 for (j=0;j<l_cp->tw;++j) {
5200 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5202 /* 4 borders of the tile rescale on the image if necessary */
5203 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5204 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5205 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5206 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5208 l_rates = l_tcp->rates;
5210 /* Modification of the RATE >> */
5212 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5214 ((*l_rates) * l_bits_empty)
5222 for (k = 1; k < l_tcp->numlayers; ++k) {
5224 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5226 ((*l_rates) * l_bits_empty)
5242 for (i=0;i<l_cp->th;++i) {
5243 for (j=0;j<l_cp->tw;++j) {
5244 l_rates = l_tcp->rates;
5247 *l_rates -= l_sot_remove;
5249 if (*l_rates < 30) {
5256 l_last_res = l_tcp->numlayers - 1;
5258 for (k = 1; k < l_last_res; ++k) {
5261 *l_rates -= l_sot_remove;
5263 if (*l_rates < *(l_rates - 1) + 10) {
5264 *l_rates = (*(l_rates - 1)) + 20;
5272 *l_rates -= (l_sot_remove + 2.f);
5274 if (*l_rates < *(l_rates - 1) + 10) {
5275 *l_rates = (*(l_rates - 1)) + 20;
5283 l_img_comp = l_image->comps;
5286 for (i=0;i<l_image->numcomps;++i) {
5287 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5289 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5297 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5299 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5301 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5302 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5303 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5304 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5308 if (l_cp->m_specific_param.m_enc.m_cinema) {
5309 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5310 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5311 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5315 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5316 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5322 static void j2k_read_eoc(opj_j2k_t *j2k) {
5324 opj_bool success = OPJ_FALSE;
5326 /* if packets should be decoded */
5327 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5328 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5329 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5330 for (i = 0; i < j2k->cp->tileno_size; i++) {
5331 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5332 if (j2k->cp->tileno[i] != -1)
5334 tileno = j2k->cp->tileno[i];
5335 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5336 opj_free(j2k->tile_data[tileno]);
5337 j2k->tile_data[tileno] = NULL;
5338 tcd_free_decode_tile(tcd, i);
5341 success = OPJ_FALSE;
5342 if (success == OPJ_FALSE) {
5343 j2k->state |= J2K_STATE_ERR;
5347 tcd_free_decode(tcd);
5350 /* if packets should not be decoded */
5352 for (i = 0; i < j2k->cp->tileno_size; i++) {
5353 tileno = j2k->cp->tileno[i];
5354 opj_free(j2k->tile_data[tileno]);
5355 j2k->tile_data[tileno] = NULL;
5358 if (j2k->state & J2K_STATE_ERR)
5359 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5361 j2k->state = J2K_STATE_MT;
5365 * Reads a EOC marker (End Of Codestream)
5367 * @param p_header_data the data contained in the SOD box.
5368 * @param p_j2k the jpeg2000 codec.
5369 * @param p_header_size the size of the data contained in the SOD marker.
5370 * @param p_manager the user event manager.
5373 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5374 struct opj_stream_private *p_stream,
5375 struct opj_event_mgr * p_manager )
5378 opj_tcd_v2_t * l_tcd = 00;
5379 OPJ_UINT32 l_nb_tiles;
5380 opj_tcp_v2_t * l_tcp = 00;
5384 assert(p_j2k != 00);
5385 assert(p_manager != 00);
5386 assert(p_stream != 00);
5388 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5389 l_tcp = p_j2k->m_cp.tcps;
5391 l_tcd = tcd_create_v2(OPJ_TRUE);
5393 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5397 for (i = 0; i < l_nb_tiles; ++i) {
5398 if (l_tcp->m_data) {
5399 if (! tcd_init_decode_tile(l_tcd, i)) {
5400 tcd_destroy_v2(l_tcd);
5401 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5405 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5409 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5414 j2k_tcp_destroy(l_tcp);
5418 tcd_destroy_v2(l_tcd);
5424 * Gets the offset of the header.
5426 * @param p_stream the stream to write data to.
5427 * @param p_j2k J2K codec.
5428 * @param p_manager the user event manager.
5430 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5431 struct opj_stream_private *p_stream,
5432 struct opj_event_mgr * p_manager )
5435 assert(p_j2k != 00);
5436 assert(p_manager != 00);
5437 assert(p_stream != 00);
5439 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5445 * Writes the MCT marker (Multiple Component Transform)
5447 * @param p_stream the stream to write data to.
5448 * @param p_j2k J2K codec.
5449 * @param p_manager the user event manager.
5451 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5452 struct opj_stream_private *p_stream,
5453 struct opj_event_mgr * p_manager )
5456 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5457 opj_mct_data_t * l_mct_record;
5458 opj_tcp_v2_t * l_tcp;
5461 assert(p_j2k != 00);
5462 assert(p_stream != 00);
5463 assert(p_manager != 00);
5465 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5469 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5470 l_mct_record = l_tcp->m_mct_records;
5472 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5474 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5481 l_mcc_record = l_tcp->m_mcc_records;
5483 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5485 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5492 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5500 * Writes the image components.
5502 * @param p_stream the stream to write data to.
5503 * @param p_j2k J2K codec.
5504 * @param p_manager the user event manager.
5506 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5507 struct opj_stream_private *p_stream,
5508 struct opj_event_mgr * p_manager )
5513 assert(p_j2k != 00);
5514 assert(p_manager != 00);
5515 assert(p_stream != 00);
5517 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5519 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5523 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5532 * Writes regions of interests.
5534 * @param p_stream the stream to write data to.
5535 * @param p_j2k J2K codec.
5536 * @param p_manager the user event manager.
5538 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5539 struct opj_stream_private *p_stream,
5540 struct opj_event_mgr * p_manager )
5543 const opj_tccp_t *l_tccp = 00;
5546 assert(p_j2k != 00);
5547 assert(p_manager != 00);
5548 assert(p_stream != 00);
5550 l_tccp = p_j2k->m_cp.tcps->tccps;
5552 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5553 if (l_tccp->roishift) {
5555 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5569 * @param p_stream the stream to write data to.
5570 * @param p_j2k J2K codec.
5571 * @param p_manager the user event manager.
5573 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5574 struct opj_stream_private *p_stream,
5575 struct opj_event_mgr * p_manager )
5577 opj_codestream_index_t * l_cstr_index = 00;
5580 assert(p_j2k != 00);
5581 assert(p_manager != 00);
5582 assert(p_stream != 00);
5584 l_cstr_index = p_j2k->cstr_index;
5586 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5588 /* The following adjustment is done to adjust the codestream size */
5589 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5590 /* the first bunch of bytes is not in the codestream */
5591 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5596 /* preparation of JPWL marker segments */
5600 /* encode according to JPWL */
5601 jpwl_encode(p_j2k, p_stream, image);
5605 assert( 0 && "TODO" );
5606 #endif /* USE_JPWL */
5611 typedef struct opj_dec_mstabent {
5614 /** value of the state when the marker can appear */
5616 /** action linked to the marker */
5617 void (*handler) (opj_j2k_t *j2k);
5618 } opj_dec_mstabent_t;
5620 opj_dec_mstabent_t j2k_dec_mstab[] = {
5621 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5622 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5623 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5624 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5625 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5626 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5627 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5628 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5629 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5630 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5631 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5632 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5633 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5634 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5635 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5636 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5638 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5639 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5642 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5643 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5644 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5645 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5646 #endif /* USE_JPWL */
5648 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5649 {J2K_MS_INSEC, 0, j2k_read_insec},
5650 #endif /* USE_JPSEC */
5652 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5658 * Reads an unknown marker
5660 * @param p_stream the stream object to read from.
5661 * @param p_j2k the jpeg2000 codec.
5662 * @param p_manager the user event manager.
5664 * @return true if the marker could be deduced.
5666 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5667 opj_stream_private_t *p_stream,
5668 OPJ_UINT32 *output_marker,
5669 opj_event_mgr_t * p_manager
5672 OPJ_UINT32 l_unknown_marker;
5673 const opj_dec_memory_marker_handler_t * l_marker_handler;
5674 OPJ_UINT32 l_size_unk = 2;
5677 assert(p_j2k != 00);
5678 assert(p_manager != 00);
5679 assert(p_stream != 00);
5681 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5684 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5685 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5686 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5690 /* read 2 bytes as the new marker ID*/
5691 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5693 if (!(l_unknown_marker < 0xff00)) {
5695 /* Get the marker handler from the marker ID*/
5696 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5698 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5699 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5703 if (l_marker_handler->id != J2K_MS_UNK) {
5704 /* Add the marker to the codestream index*/
5705 if (l_marker_handler->id != J2K_MS_SOT)
5706 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5707 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5709 break; /* next marker is known and well located */
5717 *output_marker = l_marker_handler->id ;
5723 Read the lookup table containing all the marker, status and action
5724 @param id Marker value
5726 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5727 opj_dec_mstabent_t *e;
5728 for (e = j2k_dec_mstab; e->id != 0; e++) {
5737 * Writes the MCT marker (Multiple Component Transform)
5739 * @param p_stream the stream to write data to.
5740 * @param p_j2k J2K codec.
5741 * @param p_manager the user event manager.
5743 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5744 opj_mct_data_t * p_mct_record,
5745 struct opj_stream_private *p_stream,
5746 struct opj_event_mgr * p_manager )
5748 OPJ_UINT32 l_mct_size;
5749 OPJ_BYTE * l_current_data = 00;
5753 assert(p_j2k != 00);
5754 assert(p_manager != 00);
5755 assert(p_stream != 00);
5757 l_mct_size = 10 + p_mct_record->m_data_size;
5759 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5760 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5761 = (OPJ_BYTE*)opj_realloc(
5762 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5765 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5769 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5772 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5774 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5775 l_current_data += 2;
5777 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5778 l_current_data += 2;
5780 opj_write_bytes(l_current_data,0,2); /* Zmct */
5781 l_current_data += 2;
5783 /* only one marker atm */
5784 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5786 opj_write_bytes(l_current_data,l_tmp,2);
5787 l_current_data += 2;
5789 opj_write_bytes(l_current_data,0,2); /* Ymct */
5792 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5794 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
5802 * Reads a MCT marker (Multiple Component Transform)
5804 * @param p_header_data the data contained in the MCT box.
5805 * @param p_j2k the jpeg2000 codec.
5806 * @param p_header_size the size of the data contained in the MCT marker.
5807 * @param p_manager the user event manager.
5809 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5810 OPJ_BYTE * p_header_data,
5811 OPJ_UINT32 p_header_size,
5812 opj_event_mgr_t * p_manager
5816 opj_tcp_v2_t *l_tcp = 00;
5819 opj_mct_data_t * l_mct_data;
5822 assert(p_header_data != 00);
5823 assert(p_j2k != 00);
5825 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5826 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5827 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5829 if (p_header_size < 2) {
5830 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5835 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5838 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5842 if(p_header_size <= 6) {
5843 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5847 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5848 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5851 l_indix = l_tmp & 0xff;
5852 l_mct_data = l_tcp->m_mct_records;
5854 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5855 if (l_mct_data->m_index == l_indix) {
5862 if (i == l_tcp->m_nb_mct_records) {
5863 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5864 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5866 l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
5867 if(! l_tcp->m_mct_records) {
5868 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5872 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5873 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5876 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5879 if (l_mct_data->m_data) {
5880 opj_free(l_mct_data->m_data);
5881 l_mct_data->m_data = 00;
5884 l_mct_data->m_index = l_indix;
5885 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5886 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5888 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5891 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5897 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5898 if (! l_mct_data->m_data) {
5899 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5902 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5904 l_mct_data->m_data_size = p_header_size;
5905 ++l_tcp->m_nb_mct_records;
5911 * Writes the MCC marker (Multiple Component Collection)
5913 * @param p_stream the stream to write data to.
5914 * @param p_j2k J2K codec.
5915 * @param p_manager the user event manager.
5917 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5918 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5919 struct opj_stream_private *p_stream,
5920 struct opj_event_mgr * p_manager )
5923 OPJ_UINT32 l_mcc_size;
5924 OPJ_BYTE * l_current_data = 00;
5925 OPJ_UINT32 l_nb_bytes_for_comp;
5930 assert(p_j2k != 00);
5931 assert(p_manager != 00);
5932 assert(p_stream != 00);
5934 if (p_mcc_record->m_nb_comps > 255 ) {
5935 l_nb_bytes_for_comp = 2;
5939 l_nb_bytes_for_comp = 1;
5943 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5944 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5946 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5947 = (OPJ_BYTE*)opj_realloc(
5948 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5950 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5954 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5957 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5959 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5960 l_current_data += 2;
5962 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5963 l_current_data += 2;
5966 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5967 l_current_data += 2;
5969 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5972 /* only one marker atm */
5973 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5976 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5979 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5982 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
5985 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5986 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5987 l_current_data+=l_nb_bytes_for_comp;
5990 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
5993 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5995 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5996 l_current_data+=l_nb_bytes_for_comp;
5999 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6001 if (p_mcc_record->m_decorrelation_array) {
6002 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6005 if (p_mcc_record->m_offset_array) {
6006 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6009 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6012 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
6020 * Reads a MCC marker (Multiple Component Collection)
6022 * @param p_header_data the data contained in the MCC box.
6023 * @param p_j2k the jpeg2000 codec.
6024 * @param p_header_size the size of the data contained in the MCC marker.
6025 * @param p_manager the user event manager.
6027 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6028 OPJ_BYTE * p_header_data,
6029 OPJ_UINT32 p_header_size,
6030 opj_event_mgr_t * p_manager )
6035 opj_tcp_v2_t * l_tcp;
6036 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6037 opj_mct_data_t * l_mct_data;
6038 OPJ_UINT32 l_nb_collections;
6039 OPJ_UINT32 l_nb_comps;
6040 OPJ_UINT32 l_nb_bytes_by_comp;
6044 assert(p_header_data != 00);
6045 assert(p_j2k != 00);
6046 assert(p_manager != 00);
6048 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6049 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6050 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6052 if (p_header_size < 2) {
6053 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6058 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6061 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6065 if (p_header_size < 7) {
6066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6070 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6073 l_mcc_record = l_tcp->m_mcc_records;
6075 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6076 if (l_mcc_record->m_index == l_indix) {
6083 if (i == l_tcp->m_nb_mcc_records) {
6084 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6085 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6087 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6088 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6089 if (! l_tcp->m_mcc_records) {
6090 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6093 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6094 memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
6096 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6098 l_mcc_record->m_index = l_indix;
6100 /* only one marker atm */
6101 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6104 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6108 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6111 if (l_nb_collections > 1) {
6112 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6118 for (i=0;i<l_nb_collections;++i) {
6119 if (p_header_size < 3) {
6120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6124 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6128 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6132 opj_read_bytes(p_header_data,&l_nb_comps,2);
6137 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6138 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6140 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6141 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6145 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6147 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6148 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6149 p_header_data+=l_nb_bytes_by_comp;
6152 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6157 opj_read_bytes(p_header_data,&l_nb_comps,2);
6160 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6161 l_nb_comps &= 0x7fff;
6163 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6164 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6168 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6169 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6173 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6175 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6176 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6177 p_header_data+=l_nb_bytes_by_comp;
6180 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6185 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6188 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6189 l_mcc_record->m_decorrelation_array = 00;
6190 l_mcc_record->m_offset_array = 00;
6192 l_indix = l_tmp & 0xff;
6194 l_mct_data = l_tcp->m_mct_records;
6195 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6196 if (l_mct_data->m_index == l_indix) {
6197 l_mcc_record->m_decorrelation_array = l_mct_data;
6203 if (l_mcc_record->m_decorrelation_array == 00) {
6204 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6209 l_indix = (l_tmp >> 8) & 0xff;
6211 l_mct_data = l_tcp->m_mct_records;
6212 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6213 if (l_mct_data->m_index == l_indix) {
6214 l_mcc_record->m_offset_array = l_mct_data;
6220 if (l_mcc_record->m_offset_array == 00) {
6221 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6227 if (p_header_size != 0) {
6228 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6232 ++l_tcp->m_nb_mcc_records;
6239 * Writes the MCO marker (Multiple component transformation ordering)
6241 * @param p_stream the stream to write data to.
6242 * @param p_j2k J2K codec.
6243 * @param p_manager the user event manager.
6245 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6246 struct opj_stream_private *p_stream,
6247 struct opj_event_mgr * p_manager
6250 OPJ_BYTE * l_current_data = 00;
6251 OPJ_UINT32 l_mco_size;
6252 opj_tcp_v2_t * l_tcp = 00;
6253 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6257 assert(p_j2k != 00);
6258 assert(p_manager != 00);
6259 assert(p_stream != 00);
6261 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6262 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6264 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6265 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6267 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6268 = (OPJ_BYTE*)opj_realloc(
6269 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6271 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6276 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6279 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6280 l_current_data += 2;
6282 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6283 l_current_data += 2;
6285 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6288 l_mcc_record = l_tcp->m_mcc_records;
6289 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6290 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6296 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
6304 * Reads a MCO marker (Multiple Component Transform Ordering)
6306 * @param p_header_data the data contained in the MCO box.
6307 * @param p_j2k the jpeg2000 codec.
6308 * @param p_header_size the size of the data contained in the MCO marker.
6309 * @param p_manager the user event manager.
6311 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6312 OPJ_BYTE * p_header_data,
6313 OPJ_UINT32 p_header_size,
6314 opj_event_mgr_t * p_manager
6317 OPJ_UINT32 l_tmp, i;
6318 OPJ_UINT32 l_nb_stages;
6319 opj_tcp_v2_t * l_tcp;
6320 opj_tccp_t * l_tccp;
6321 opj_image_t * l_image;
6322 opj_image_comp_t * l_img_comp;
6325 assert(p_header_data != 00);
6326 assert(p_j2k != 00);
6327 assert(p_manager != 00);
6329 l_image = p_j2k->m_private_image;
6330 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6331 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6332 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6334 if (p_header_size < 1) {
6335 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6339 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6342 if (l_nb_stages > 1) {
6343 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6347 if (p_header_size != l_nb_stages + 1) {
6348 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6352 l_tccp = l_tcp->tccps;
6353 l_img_comp = l_image->comps;
6355 for (i=0;i<l_image->numcomps;++i) {
6356 l_tccp->m_dc_level_shift = 0;
6360 if (l_tcp->m_mct_decoding_matrix) {
6361 opj_free(l_tcp->m_mct_decoding_matrix);
6362 l_tcp->m_mct_decoding_matrix = 00;
6365 for (i=0;i<l_nb_stages;++i) {
6366 opj_read_bytes(p_header_data,&l_tmp,1);
6369 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6377 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6380 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6381 opj_mct_data_t * l_deco_array, * l_offset_array;
6382 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6383 OPJ_UINT32 l_nb_elem;
6384 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6385 opj_tccp_t * l_tccp;
6388 assert(p_tcp != 00);
6390 l_mcc_record = p_tcp->m_mcc_records;
6392 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6393 if (l_mcc_record->m_index == p_index) {
6398 if (i==p_tcp->m_nb_mcc_records) {
6399 /** element discarded **/
6403 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6404 /** do not support number of comps != image */
6408 l_deco_array = l_mcc_record->m_decorrelation_array;
6411 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6412 if (l_deco_array->m_data_size != l_data_size) {
6416 l_nb_elem = p_image->numcomps * p_image->numcomps;
6417 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6418 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6420 if (! p_tcp->m_mct_decoding_matrix ) {
6424 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
6427 l_offset_array = l_mcc_record->m_offset_array;
6429 if (l_offset_array) {
6430 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6431 if (l_offset_array->m_data_size != l_data_size) {
6435 l_nb_elem = p_image->numcomps;
6436 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6437 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6439 if (! l_offset_data ) {
6443 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6445 l_tccp = p_tcp->tccps;
6446 l_current_offset_data = l_offset_data;
6448 for (i=0;i<p_image->numcomps;++i) {
6449 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6453 opj_free(l_offset_data);
6460 * Writes the CBD marker (Component bit depth definition)
6462 * @param p_stream the stream to write data to.
6463 * @param p_j2k J2K codec.
6464 * @param p_manager the user event manager.
6466 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6467 struct opj_stream_private *p_stream,
6468 struct opj_event_mgr * p_manager )
6471 OPJ_UINT32 l_cbd_size;
6472 OPJ_BYTE * l_current_data = 00;
6473 opj_image_t *l_image = 00;
6474 opj_image_comp_t * l_comp = 00;
6477 assert(p_j2k != 00);
6478 assert(p_manager != 00);
6479 assert(p_stream != 00);
6481 l_image = p_j2k->m_private_image;
6482 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6484 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6485 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6486 = (OPJ_BYTE*)opj_realloc(
6487 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6490 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6494 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6497 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6499 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6500 l_current_data += 2;
6502 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6503 l_current_data += 2;
6505 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6508 l_comp = l_image->comps;
6510 for (i=0;i<l_image->numcomps;++i) {
6511 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6517 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
6525 * Reads a CBD marker (Component bit depth definition)
6526 * @param p_header_data the data contained in the CBD box.
6527 * @param p_j2k the jpeg2000 codec.
6528 * @param p_header_size the size of the data contained in the CBD marker.
6529 * @param p_manager the user event manager.
6531 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6532 OPJ_BYTE * p_header_data,
6533 OPJ_UINT32 p_header_size,
6534 opj_event_mgr_t * p_manager
6537 OPJ_UINT32 l_nb_comp,l_num_comp;
6538 OPJ_UINT32 l_comp_def;
6540 opj_image_comp_t * l_comp = 00;
6543 assert(p_header_data != 00);
6544 assert(p_j2k != 00);
6545 assert(p_manager != 00);
6547 l_num_comp = p_j2k->m_private_image->numcomps;
6549 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6550 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6554 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6557 if (l_nb_comp != l_num_comp) {
6558 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6562 l_comp = p_j2k->m_private_image->comps;
6563 for (i=0;i<l_num_comp;++i) {
6564 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6566 l_comp->sgnd = (l_comp_def>>7) & 1;
6567 l_comp->prec = (l_comp_def&0x7f) + 1;
6575 /* ----------------------------------------------------------------------- */
6576 /* J2K / JPT decoder interface */
6577 /* ----------------------------------------------------------------------- */
6581 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6584 if(j2k->tile_len != NULL) {
6585 opj_free(j2k->tile_len);
6587 if(j2k->tile_data != NULL) {
6588 opj_free(j2k->tile_data);
6590 if(j2k->default_tcp != NULL) {
6591 opj_tcp_t *default_tcp = j2k->default_tcp;
6592 if(default_tcp->ppt_data_first != NULL) {
6593 opj_free(default_tcp->ppt_data_first);
6595 if(j2k->default_tcp->tccps != NULL) {
6596 opj_free(j2k->default_tcp->tccps);
6598 opj_free(j2k->default_tcp);
6600 if(j2k->cp != NULL) {
6601 opj_cp_t *cp = j2k->cp;
6602 if(cp->tcps != NULL) {
6603 for(i = 0; i < cp->tw * cp->th; i++) {
6604 if(cp->tcps[i].ppt_data_first != NULL) {
6605 opj_free(cp->tcps[i].ppt_data_first);
6607 if(cp->tcps[i].tccps != NULL) {
6608 opj_free(cp->tcps[i].tccps);
6613 if(cp->ppm_data_first != NULL) {
6614 opj_free(cp->ppm_data_first);
6616 if(cp->tileno != NULL) {
6617 opj_free(cp->tileno);
6619 if(cp->comment != NULL) {
6620 opj_free(cp->comment);
6629 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6631 if(j2k && parameters) {
6632 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6633 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6636 j2k->m_cp.correct = parameters->jpwl_correct;
6637 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6638 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6639 #endif /* USE_JPWL */
6646 * Read a JPT-stream and decode file
6649 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6650 opj_image_t *image = NULL;
6651 opj_jpt_msg_header_t header;
6653 opj_common_ptr cinfo = j2k->cinfo;
6655 OPJ_ARG_NOT_USED(cstr_info);
6659 /* create an empty image */
6660 image = opj_image_create0();
6663 j2k->state = J2K_STATE_MHSOC;
6665 /* Initialize the header */
6666 jpt_init_msg_header(&header);
6667 /* Read the first header of the message */
6668 jpt_read_msg_header(cinfo, cio, &header);
6670 position = cio_tell(cio);
6671 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6672 opj_image_destroy(image);
6673 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6678 opj_dec_mstabent_t *e = NULL;
6681 if (!cio_numbytesleft(cio)) {
6685 /* data-bin read -> need to read a new header */
6686 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6687 jpt_read_msg_header(cinfo, cio, &header);
6688 position = cio_tell(cio);
6689 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6690 opj_image_destroy(image);
6691 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6696 id = cio_read(cio, 2);
6697 if (id >> 8 != 0xff) {
6698 opj_image_destroy(image);
6699 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6702 e = j2k_dec_mstab_lookup(id);
6703 if (!(j2k->state & e->states)) {
6704 opj_image_destroy(image);
6705 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6711 if (j2k->state == J2K_STATE_MT) {
6714 if (j2k->state == J2K_STATE_NEOC) {
6718 if (j2k->state == J2K_STATE_NEOC) {
6722 if (j2k->state != J2K_STATE_MT) {
6723 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6729 /* ----------------------------------------------------------------------- */
6730 /* J2K encoder interface */
6731 /* ----------------------------------------------------------------------- */
6733 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6734 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6741 opj_j2k_v2_t* j2k_create_compress_v2(void)
6743 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6748 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6750 l_j2k->m_is_decoder = 0;
6751 l_j2k->m_cp.m_is_decoder = 0;
6753 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6754 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6759 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6761 /* validation list creation*/
6762 l_j2k->m_validation_list = opj_procedure_list_create();
6763 if (! l_j2k->m_validation_list) {
6768 /* execution list creation*/
6769 l_j2k->m_procedure_list = opj_procedure_list_create();
6770 if (! l_j2k->m_procedure_list) {
6778 void j2k_destroy_compress(opj_j2k_t *j2k) {
6782 if(j2k->cp != NULL) {
6783 opj_cp_t *cp = j2k->cp;
6786 opj_free(cp->comment);
6789 opj_free(cp->matrice);
6791 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6792 opj_free(cp->tcps[tileno].tccps);
6801 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6803 int tileno, numpocs_tile;
6804 opj_cp_t *cp = NULL;
6806 if(!j2k || !parameters || ! image) {
6810 /* create and initialize the coding parameters structure */
6811 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6813 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6816 /* set default values for cp */
6821 copy user encoding parameters
6823 cp->cinema = parameters->cp_cinema;
6824 cp->max_comp_size = parameters->max_comp_size;
6825 cp->rsiz = parameters->cp_rsiz;
6826 cp->disto_alloc = parameters->cp_disto_alloc;
6827 cp->fixed_alloc = parameters->cp_fixed_alloc;
6828 cp->fixed_quality = parameters->cp_fixed_quality;
6830 /* mod fixed_quality */
6831 if(parameters->cp_matrice) {
6832 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6833 cp->matrice = (int *) opj_malloc(array_size);
6834 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6838 cp->tdx = parameters->cp_tdx;
6839 cp->tdy = parameters->cp_tdy;
6842 cp->tx0 = parameters->cp_tx0;
6843 cp->ty0 = parameters->cp_ty0;
6845 /* comment string */
6846 if(parameters->cp_comment) {
6847 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6849 strcpy(cp->comment, parameters->cp_comment);
6854 calculate other encoding parameters
6857 if (parameters->tile_size_on) {
6858 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6859 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6861 cp->tdx = image->x1 - cp->tx0;
6862 cp->tdy = image->y1 - cp->ty0;
6865 if(parameters->tp_on){
6866 cp->tp_flag = parameters->tp_flag;
6871 for(i=0;i<image->numcomps ;i++){
6872 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6878 calculate JPWL encoding parameters
6881 if (parameters->jpwl_epc_on) {
6885 cp->epc_on = OPJ_TRUE;
6886 cp->info_on = OPJ_FALSE; /* no informative technique */
6889 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6890 cp->epb_on = OPJ_TRUE;
6892 cp->hprot_MH = parameters->jpwl_hprot_MH;
6893 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6894 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6895 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6897 /* if tile specs are not specified, copy MH specs */
6898 if (cp->hprot_TPH[0] == -1) {
6899 cp->hprot_TPH_tileno[0] = 0;
6900 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6902 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6903 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6904 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6905 cp->pprot[i] = parameters->jpwl_pprot[i];
6909 /* set ESD writing */
6910 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6911 cp->esd_on = OPJ_TRUE;
6913 cp->sens_size = parameters->jpwl_sens_size;
6914 cp->sens_addr = parameters->jpwl_sens_addr;
6915 cp->sens_range = parameters->jpwl_sens_range;
6917 cp->sens_MH = parameters->jpwl_sens_MH;
6918 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6919 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6920 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6924 /* always set RED writing to false: we are at the encoder */
6925 cp->red_on = OPJ_FALSE;
6928 cp->epc_on = OPJ_FALSE;
6930 #endif /* USE_JPWL */
6933 /* initialize the mutiple tiles */
6934 /* ---------------------------- */
6935 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6937 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6938 opj_tcp_t *tcp = &cp->tcps[tileno];
6939 tcp->numlayers = parameters->tcp_numlayers;
6940 assert ( tcp->numlayers >= 0 );
6941 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6943 if (cp->fixed_quality) {
6944 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6946 tcp->rates[j] = parameters->tcp_rates[j];
6948 if (cp->fixed_quality) { /* add fixed_quality */
6949 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6951 tcp->rates[j] = parameters->tcp_rates[j];
6955 tcp->csty = parameters->csty;
6956 tcp->prg = parameters->prog_order;
6957 tcp->mct = parameters->tcp_mct;
6961 if (parameters->numpocs) {
6962 /* initialisation of POC */
6964 assert( parameters->numpocs >= 0 );
6965 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6966 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6967 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6968 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6969 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6970 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6971 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6972 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6973 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6974 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6978 tcp->numpocs = numpocs_tile -1 ;
6983 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6985 for (i = 0; i < image->numcomps; i++) {
6986 opj_tccp_t *tccp = &tcp->tccps[i];
6987 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6988 tccp->numresolutions = parameters->numresolution;
6989 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6990 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6991 tccp->cblksty = parameters->mode;
6992 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6993 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6995 assert(parameters->roi_compno >= 0);
6996 if (i == (OPJ_UINT32)parameters->roi_compno) {
6997 tccp->roishift = parameters->roi_shift;
7002 if(parameters->cp_cinema)
7004 /*Precinct size for lowest frequency subband=128*/
7007 /*Precinct size at all other resolutions = 256*/
7008 for (j = 1; j < tccp->numresolutions; j++) {
7013 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7015 assert(tccp->numresolutions > 0);
7016 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7017 if (p < parameters->res_spec) {
7019 if (parameters->prcw_init[p] < 1) {
7022 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7025 if (parameters->prch_init[p] < 1) {
7028 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7032 int res_spec = parameters->res_spec;
7033 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7034 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7036 if (size_prcw < 1) {
7039 tccp->prcw[j] = int_floorlog2(size_prcw);
7042 if (size_prch < 1) {
7045 tccp->prch[j] = int_floorlog2(size_prch);
7049 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7052 for (j = 0; j < tccp->numresolutions; j++) {
7059 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7064 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7065 opj_cparameters_t *parameters,
7067 struct opj_event_mgr * p_manager)
7069 OPJ_UINT32 i, j, tileno, numpocs_tile;
7070 opj_cp_v2_t *cp = 00;
7073 if(!p_j2k || !parameters || ! image) {
7077 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7078 cp = &(p_j2k->m_cp);
7080 /* set default values for cp */
7085 copy user encoding parameters
7087 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7088 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7089 cp->rsiz = parameters->cp_rsiz;
7090 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7091 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7092 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7094 /* mod fixed_quality */
7095 if (parameters->cp_matrice) {
7096 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7097 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7098 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7102 cp->tdx = parameters->cp_tdx;
7103 cp->tdy = parameters->cp_tdy;
7106 cp->tx0 = parameters->cp_tx0;
7107 cp->ty0 = parameters->cp_ty0;
7109 /* comment string */
7110 if(parameters->cp_comment) {
7111 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7113 strcpy(cp->comment, parameters->cp_comment);
7118 calculate other encoding parameters
7121 if (parameters->tile_size_on) {
7122 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7123 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7125 cp->tdx = image->x1 - cp->tx0;
7126 cp->tdy = image->y1 - cp->ty0;
7129 if (parameters->tp_on) {
7130 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7131 cp->m_specific_param.m_enc.m_tp_on = 1;
7136 calculate JPWL encoding parameters
7139 if (parameters->jpwl_epc_on) {
7143 cp->epc_on = OPJ_TRUE;
7144 cp->info_on = OPJ_FALSE; /* no informative technique */
7147 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7148 cp->epb_on = OPJ_TRUE;
7150 cp->hprot_MH = parameters->jpwl_hprot_MH;
7151 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7152 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7153 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7155 /* if tile specs are not specified, copy MH specs */
7156 if (cp->hprot_TPH[0] == -1) {
7157 cp->hprot_TPH_tileno[0] = 0;
7158 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7160 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7161 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7162 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7163 cp->pprot[i] = parameters->jpwl_pprot[i];
7167 /* set ESD writing */
7168 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7169 cp->esd_on = OPJ_TRUE;
7171 cp->sens_size = parameters->jpwl_sens_size;
7172 cp->sens_addr = parameters->jpwl_sens_addr;
7173 cp->sens_range = parameters->jpwl_sens_range;
7175 cp->sens_MH = parameters->jpwl_sens_MH;
7176 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7177 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7178 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7182 /* always set RED writing to false: we are at the encoder */
7183 cp->red_on = OPJ_FALSE;
7186 cp->epc_on = OPJ_FALSE;
7188 #endif /* USE_JPWL */
7191 /* initialize the mutiple tiles */
7192 /* ---------------------------- */
7193 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7194 if (parameters->numpocs) {
7195 /* initialisation of POC */
7196 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7200 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7201 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7202 tcp->numlayers = parameters->tcp_numlayers;
7204 for (j = 0; j < tcp->numlayers; j++) {
7205 if(cp->m_specific_param.m_enc.m_cinema){
7206 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7207 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7209 tcp->rates[j] = parameters->tcp_rates[j];
7211 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7212 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7214 tcp->rates[j] = parameters->tcp_rates[j];
7219 tcp->csty = parameters->csty;
7220 tcp->prg = parameters->prog_order;
7221 tcp->mct = parameters->tcp_mct;
7226 if (parameters->numpocs) {
7227 /* initialisation of POC */
7230 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7231 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7232 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7234 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7235 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7236 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7237 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7238 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7239 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7240 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7246 tcp->numpocs = numpocs_tile -1 ;
7251 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7253 if (parameters->mct_data) {
7255 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7258 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7259 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7260 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7261 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7264 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7265 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7266 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7268 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7269 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7271 tcp->mct_norms = (OPJ_FLOAT64*)
7272 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7274 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7277 for (i = 0; i < image->numcomps; i++) {
7278 opj_tccp_t *tccp = &tcp->tccps[i];
7279 tccp->m_dc_level_shift = l_dc_shift[i];
7282 j2k_setup_mct_encoding(tcp,image);
7286 for (i = 0; i < image->numcomps; i++) {
7287 opj_tccp_t *tccp = &tcp->tccps[i];
7288 opj_image_comp_t * l_comp = &(image->comps[i]);
7290 if (! l_comp->sgnd) {
7291 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7296 for (i = 0; i < image->numcomps; i++) {
7297 opj_tccp_t *tccp = &tcp->tccps[i];
7299 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7300 tccp->numresolutions = parameters->numresolution;
7301 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7302 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7303 tccp->cblksty = parameters->mode;
7304 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7305 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7308 if (i == parameters->roi_compno) {
7309 tccp->roishift = parameters->roi_shift;
7314 if(parameters->cp_cinema) {
7315 //Precinct size for lowest frequency subband=128
7318 //Precinct size at all other resolutions = 256
7319 for (j = 1; j < tccp->numresolutions; j++) {
7324 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7325 OPJ_INT32 p = 0, it_res;
7326 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7327 if (p < parameters->res_spec) {
7329 if (parameters->prcw_init[p] < 1) {
7330 tccp->prcw[it_res] = 1;
7332 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7335 if (parameters->prch_init[p] < 1) {
7336 tccp->prch[it_res] = 1;
7338 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7342 int res_spec = parameters->res_spec;
7343 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7344 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7346 if (size_prcw < 1) {
7347 tccp->prcw[it_res] = 1;
7349 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7352 if (size_prch < 1) {
7353 tccp->prch[it_res] = 1;
7355 tccp->prch[it_res] = int_floorlog2(size_prch);
7359 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7362 for (j = 0; j < tccp->numresolutions; j++) {
7369 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7373 if (parameters->mct_data) {
7374 opj_free(parameters->mct_data);
7375 parameters->mct_data = 00;
7380 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7383 opj_cp_t *cp = NULL;
7385 opj_tcd_t *tcd = NULL; /* TCD component */
7393 j2k->cstr_info = cstr_info;
7396 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7397 cstr_info->image_w = image->x1 - image->x0;
7398 cstr_info->image_h = image->y1 - image->y0;
7399 cstr_info->prog = (&cp->tcps[0])->prg;
7400 cstr_info->tw = cp->tw;
7401 cstr_info->th = cp->th;
7402 cstr_info->tile_x = cp->tdx; /* new version parser */
7403 cstr_info->tile_y = cp->tdy; /* new version parser */
7404 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7405 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7406 cstr_info->numcomps = image->numcomps;
7407 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7408 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7409 for (compno=0; compno < image->numcomps; compno++) {
7410 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7412 cstr_info->D_max = 0.0; /* ADD Marcela */
7413 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7414 cstr_info->maxmarknum = 100;
7415 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7416 cstr_info->marknum = 0;
7426 for (compno = 1; compno < image->numcomps; compno++) {
7427 j2k_write_coc(j2k, compno);
7428 j2k_write_qcc(j2k, compno);
7432 for (compno = 0; compno < image->numcomps; compno++) {
7433 opj_tcp_t *tcp = &cp->tcps[0];
7434 if (tcp->tccps[compno].roishift)
7435 j2k_write_rgn(j2k, compno, 0);
7437 if (cp->comment != NULL) {
7441 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7445 if (cp->cinema == CINEMA4K_24) {
7450 /* uncomment only for testing JPSEC marker writing */
7451 /* j2k_write_sec(j2k); */
7455 cstr_info->main_head_end = cio_tell(cio) - 1;
7458 /**** Main Header ENDS here ***/
7460 /* create the tile encoder */
7461 tcd = tcd_create(j2k->cinfo);
7463 /* encode each tile */
7464 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7468 int acc_pack_num = 0;
7472 opj_tcp_t *tcp = &cp->tcps[tileno];
7473 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7475 j2k->curtileno = tileno;
7476 j2k->cur_tp_num = 0;
7477 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7478 /* initialisation before tile encoding */
7480 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7482 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7487 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7488 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7489 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7490 cstr_info->tile[j2k->curtileno].marknum = 0;
7494 for(pino = 0; pino <= tcp->numpocs; pino++) {
7498 /*Get number of tile parts*/
7499 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7500 tcd->tp_pos = cp->tp_pos;
7502 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7503 j2k->tp_num = tilepartno;
7506 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7507 cio_tell(cio) + j2k->pos_correction;
7511 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7512 for (compno = 1; compno < image->numcomps; compno++) {
7513 j2k_write_coc(j2k, compno);
7514 j2k_write_qcc(j2k, compno);
7516 if (cp->tcps[tileno].numpocs) {
7523 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7524 cio_tell(cio) + j2k->pos_correction + 1;
7527 j2k_write_sod(j2k, tcd);
7531 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7532 cio_tell(cio) + j2k->pos_correction - 1;
7533 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7535 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7536 cstr_info->packno - acc_pack_num;
7537 acc_pack_num = cstr_info->packno;
7545 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7550 if (tile->PPT) { // BAD PPT !!!
7553 PPT_file=fopen("PPT","rb");
7554 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7555 for (i=0;i<tile->len_ppt;i++) {
7557 fread(&elmt, 1, 1, PPT_file);
7558 fwrite(&elmt,1,1,f);
7567 /* destroy the tile encoder */
7568 tcd_free_encode(tcd);
7571 opj_free(j2k->cur_totnum_tp);
7576 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7578 /* The following adjustment is done to adjust the codestream size */
7579 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7580 /* the first bunch of bytes is not in the codestream */
7581 cstr_info->codestream_size -= cstr_info->main_head_start;
7587 preparation of JPWL marker segments
7591 /* encode according to JPWL */
7592 jpwl_encode(j2k, cio, image);
7595 #endif /* USE_JPWL */
7600 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7602 assert(cstr_info != 00);
7604 /* expand the list? */
7605 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7606 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7607 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7610 /* add the marker */
7611 cstr_info->marker[cstr_info->marknum].type = type;
7612 cstr_info->marker[cstr_info->marknum].pos = pos;
7613 cstr_info->marker[cstr_info->marknum].len = len;
7614 cstr_info->marknum++;
7618 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7620 assert(cstr_index != 00);
7622 /* expand the list? */
7623 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7624 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7625 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7628 /* add the marker */
7629 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7630 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7631 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7632 cstr_index->marknum++;
7636 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7638 opj_marker_info_t *marker;
7640 assert(cstr_info != 00);
7642 /* expand the list? */
7643 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7644 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7645 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7648 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7650 /* add the marker */
7651 marker->type = type;
7654 cstr_info->tile[tileno].marknum++;
7657 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7659 assert(cstr_index != 00);
7660 assert(cstr_index->tile_index != 00);
7662 /* expand the list? */
7663 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7664 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7665 cstr_index->tile_index[tileno].marker =
7666 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7667 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7670 /* add the marker */
7671 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7672 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7673 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7674 cstr_index->tile_index[tileno].marknum++;
7676 if (type == J2K_MS_SOT) {
7677 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7679 if (cstr_index->tile_index[tileno].tp_index)
7680 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7687 * -----------------------------------------------------------------------
7688 * -----------------------------------------------------------------------
7689 * -----------------------------------------------------------------------
7693 * Ends the decompression procedures and possibiliy add data to be read after the
7696 opj_bool j2k_end_decompress(
7697 opj_j2k_v2_t *p_j2k,
7698 opj_stream_private_t *p_stream,
7699 opj_event_mgr_t * p_manager)
7708 * Reads a jpeg2000 codestream header structure.
7711 * @param p_stream the stream to read data from.
7712 * @param p_j2k the jpeg2000 codec.
7713 * @param p_manager the user event manager.
7715 * @return true if the box is valid.
7717 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7718 opj_j2k_v2_t* p_j2k,
7719 opj_image_t** p_image,
7720 struct opj_event_mgr* p_manager )
7723 assert(p_j2k != 00);
7724 assert(p_stream != 00);
7725 assert(p_manager != 00);
7727 /* create an empty image header */
7728 p_j2k->m_private_image = opj_image_create0();
7729 if (! p_j2k->m_private_image) {
7733 /* customization of the validation */
7734 j2k_setup_decoding_validation(p_j2k);
7736 /* validation of the parameters codec */
7737 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7738 opj_image_destroy(p_j2k->m_private_image);
7739 p_j2k->m_private_image = NULL;
7743 /* customization of the encoding */
7744 j2k_setup_header_reading(p_j2k);
7747 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7748 opj_image_destroy(p_j2k->m_private_image);
7749 p_j2k->m_private_image = NULL;
7753 *p_image = opj_image_create0();
7758 /* Copy codestream image information to the output image */
7759 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7761 /*Allocate and initialize some elements of codestrem index*/
7762 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7770 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7772 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7775 assert(p_j2k != 00);
7777 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7779 /* DEVELOPER CORNER, add your custom procedures */
7780 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7785 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7786 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7788 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7791 assert(p_j2k != 00);
7793 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7794 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7795 /* DEVELOPER CORNER, add your custom validation procedure */
7801 * The mct encoding validation procedure.
7803 * @param p_j2k the jpeg2000 codec to validate.
7804 * @param p_stream the input stream to validate.
7805 * @param p_manager the user event manager.
7807 * @return true if the parameters are correct.
7809 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7810 opj_stream_private_t *p_stream,
7811 opj_event_mgr_t * p_manager )
7813 opj_bool l_is_valid = OPJ_TRUE;
7817 assert(p_j2k != 00);
7818 assert(p_stream != 00);
7819 assert(p_manager != 00);
7821 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7822 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7823 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7825 for (i=0;i<l_nb_tiles;++i) {
7826 if (l_tcp->mct == 2) {
7827 opj_tccp_t * l_tccp = l_tcp->tccps;
7828 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7830 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7831 l_is_valid &= ! (l_tccp->qmfbid & 1);
7842 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7845 OPJ_UINT32 l_indix = 1;
7846 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7847 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7848 OPJ_UINT32 l_mct_size,l_nb_elem;
7849 OPJ_FLOAT32 * l_data, * l_current_data;
7850 opj_tccp_t * l_tccp;
7853 assert(p_tcp != 00);
7855 if (p_tcp->mct != 2) {
7859 if (p_tcp->m_mct_decoding_matrix) {
7860 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7861 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7863 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7864 if (! p_tcp->m_mct_records) {
7867 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7869 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7871 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7873 if (l_mct_deco_data->m_data) {
7874 opj_free(l_mct_deco_data->m_data);
7875 l_mct_deco_data->m_data = 00;
7878 l_mct_deco_data->m_index = l_indix++;
7879 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7880 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7881 l_nb_elem = p_image->numcomps * p_image->numcomps;
7882 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7883 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7885 if (! l_mct_deco_data->m_data) {
7889 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
7891 l_mct_deco_data->m_data_size = l_mct_size;
7892 ++p_tcp->m_nb_mct_records;
7895 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7896 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7897 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7899 if (! p_tcp->m_mct_records) {
7903 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7904 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7906 if (l_mct_deco_data) {
7907 l_mct_deco_data = l_mct_offset_data - 1;
7911 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7913 if (l_mct_offset_data->m_data) {
7914 opj_free(l_mct_offset_data->m_data);
7915 l_mct_offset_data->m_data = 00;
7918 l_mct_offset_data->m_index = l_indix++;
7919 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7920 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7921 l_nb_elem = p_image->numcomps;
7922 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7923 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7925 if (! l_mct_offset_data->m_data) {
7929 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7931 opj_free(l_mct_offset_data->m_data);
7932 l_mct_offset_data->m_data = 00;
7936 l_tccp = p_tcp->tccps;
7937 l_current_data = l_data;
7939 for (i=0;i<l_nb_elem;++i) {
7940 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7944 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7948 l_mct_offset_data->m_data_size = l_mct_size;
7950 ++p_tcp->m_nb_mct_records;
7952 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7953 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7954 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7955 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7957 if (! p_tcp->m_mcc_records) {
7960 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7961 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
7965 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7966 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7967 l_mcc_data->m_is_irreversible = 1;
7968 l_mcc_data->m_nb_comps = p_image->numcomps;
7969 l_mcc_data->m_index = l_indix++;
7970 l_mcc_data->m_offset_array = l_mct_offset_data;
7971 ++p_tcp->m_nb_mcc_records;
7977 * Builds the cp decoder parameters to use to decode tile.
7979 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7980 opj_stream_private_t *p_stream,
7981 opj_event_mgr_t * p_manager )
7983 /* add here initialization of cp
7984 copy paste of setup_decoder */
7992 * Builds the cp encoder parameters to use to encode tile.
7994 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7995 opj_stream_private_t *p_stream,
7996 opj_event_mgr_t * p_manager )
7998 /* add here initialization of cp
7999 copy paste of setup_encoder */
8007 * The default encoding validation procedure without any extension.
8009 * @param p_j2k the jpeg2000 codec to validate.
8010 * @param p_stream the input stream to validate.
8011 * @param p_manager the user event manager.
8013 * @return true if the parameters are correct.
8015 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8016 opj_stream_private_t *p_stream,
8017 opj_event_mgr_t * p_manager )
8019 opj_bool l_is_valid = OPJ_TRUE;
8022 assert(p_j2k != 00);
8023 assert(p_stream != 00);
8024 assert(p_manager != 00);
8026 /* STATE checking */
8027 /* make sure the state is at 0 */
8028 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8030 /* POINTER validation */
8031 /* make sure a p_j2k codec is present */
8032 l_is_valid &= (p_j2k->m_procedure_list != 00);
8033 /* make sure a validation list is present */
8034 l_is_valid &= (p_j2k->m_validation_list != 00);
8036 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8037 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8041 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8042 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8046 /* PARAMETER VALIDATION */
8051 * The default decoding validation procedure without any extension.
8053 * @param p_j2k the jpeg2000 codec to validate.
8054 * @param p_stream the input stream to validate.
8055 * @param p_manager the user event manager.
8057 * @return true if the parameters are correct.
8059 opj_bool j2k_decoding_validation (
8060 opj_j2k_v2_t *p_j2k,
8061 opj_stream_private_t *p_stream,
8062 opj_event_mgr_t * p_manager
8065 opj_bool l_is_valid = OPJ_TRUE;
8068 assert(p_j2k != 00);
8069 assert(p_stream != 00);
8070 assert(p_manager != 00);
8073 /* STATE checking */
8074 /* make sure the state is at 0 */
8076 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8078 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8080 /* POINTER validation */
8081 /* make sure a p_j2k codec is present */
8082 /* make sure a procedure list is present */
8083 l_is_valid &= (p_j2k->m_procedure_list != 00);
8084 /* make sure a validation list is present */
8085 l_is_valid &= (p_j2k->m_validation_list != 00);
8087 /* PARAMETER VALIDATION */
8091 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8092 struct opj_stream_private *p_stream,
8093 struct opj_event_mgr * p_manager)
8095 OPJ_UINT32 l_current_marker;
8096 OPJ_UINT32 l_marker_size;
8097 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8100 assert(p_stream != 00);
8101 assert(p_j2k != 00);
8102 assert(p_manager != 00);
8104 /* We enter in the main header */
8105 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8107 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8108 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8109 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8113 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8114 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8115 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8119 /* Read 2 bytes as the new marker ID */
8120 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8122 /* Try to read until the SOT is detected */
8123 while (l_current_marker != J2K_MS_SOT) {
8125 /* Check if the current marker ID is valid */
8126 if (l_current_marker < 0xff00) {
8127 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8131 /* Get the marker handler from the marker ID */
8132 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8134 /* Manage case where marker is unknown */
8135 if (l_marker_handler->id == J2K_MS_UNK) {
8136 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
8137 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8141 if (l_current_marker == J2K_MS_SOT)
8142 break; /* SOT marker is detected main header is completely read */
8143 else /* Get the marker handler from the marker ID */
8144 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8147 /* Check if the marker is known and if it is the right place to find it */
8148 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8149 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8153 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8154 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8155 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8159 /* read 2 bytes as the marker size */
8160 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8161 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8163 /* Check if the marker size is compatible with the header data size */
8164 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8165 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8166 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8167 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8170 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8173 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8174 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) {
8175 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8179 /* Read the marker segment with the correct marker handler */
8180 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8181 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8185 /* Add the marker to the codestream index*/
8186 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8187 l_marker_handler->id,
8188 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8189 l_marker_size + 4 );
8191 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8192 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8193 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8197 /* read 2 bytes as the new marker ID */
8198 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8201 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8203 /* Position of the last element if the main header */
8204 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8206 /* Next step: read a tile-part header */
8207 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8213 * Excutes the given procedures on the given codec.
8215 * @param p_procedure_list the list of procedures to execute
8216 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8217 * @param p_stream the stream to execute the procedures on.
8218 * @param p_manager the user manager.
8220 * @return true if all the procedures were successfully executed.
8222 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8223 opj_procedure_list_t * p_procedure_list,
8224 opj_stream_private_t *p_stream,
8225 opj_event_mgr_t * p_manager )
8227 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8228 opj_bool l_result = OPJ_TRUE;
8229 OPJ_UINT32 l_nb_proc, i;
8232 assert(p_procedure_list != 00);
8233 assert(p_j2k != 00);
8234 assert(p_stream != 00);
8235 assert(p_manager != 00);
8238 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8239 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);
8241 for (i=0;i<l_nb_proc;++i) {
8242 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8246 /* and clear the procedure list at the end.*/
8247 opj_procedure_list_clear(p_procedure_list);
8252 opj_bool j2k_copy_default_tcp_and_create_tcd
8254 opj_j2k_v2_t * p_j2k,
8255 opj_stream_private_t *p_stream,
8256 opj_event_mgr_t * p_manager
8259 opj_tcp_v2_t * l_tcp = 00;
8260 opj_tcp_v2_t * l_default_tcp = 00;
8261 OPJ_UINT32 l_nb_tiles;
8263 opj_tccp_t *l_current_tccp = 00;
8264 OPJ_UINT32 l_tccp_size;
8265 OPJ_UINT32 l_mct_size;
8266 opj_image_t * l_image;
8267 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8268 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8269 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8270 OPJ_UINT32 l_offset;
8273 assert(p_j2k != 00);
8274 assert(p_stream != 00);
8275 assert(p_manager != 00);
8277 l_image = p_j2k->m_private_image;
8278 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8279 l_tcp = p_j2k->m_cp.tcps;
8280 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8281 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8282 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8285 for (i=0; i<l_nb_tiles; ++i) {
8286 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8287 l_current_tccp = l_tcp->tccps;
8288 /*Copy default coding parameters into the current tile coding parameters*/
8289 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8290 /* Initialize some values of the current tile coding parameters*/
8292 l_tcp->ppt_data = 00;
8293 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8294 l_tcp->tccps = l_current_tccp;
8296 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8297 if (l_default_tcp->m_mct_decoding_matrix) {
8298 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8299 if (! l_tcp->m_mct_decoding_matrix ) {
8302 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8305 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8306 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8307 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8308 if (! l_tcp->m_mct_records) {
8311 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8313 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8314 l_src_mct_rec = l_default_tcp->m_mct_records;
8315 l_dest_mct_rec = l_tcp->m_mct_records;
8317 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8319 if (l_src_mct_rec->m_data) {
8321 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8322 if(! l_dest_mct_rec->m_data) {
8325 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8332 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8333 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8334 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8335 if (! l_tcp->m_mcc_records) {
8338 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8340 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8341 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8342 l_dest_mcc_rec = l_tcp->m_mcc_records;
8344 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8346 if (l_src_mcc_rec->m_decorrelation_array) {
8347 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8348 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8351 if (l_src_mcc_rec->m_offset_array) {
8352 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8353 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8360 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8361 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8363 /* Move to next tile cp*/
8367 /* Create the current tile decoder*/
8368 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8369 if (! p_j2k->m_tcd ) {
8373 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8374 tcd_destroy_v2(p_j2k->m_tcd);
8376 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8384 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8385 * with the marker value.
8386 * @param p_id Marker value to look up
8388 * @return the handler associated with the id.
8390 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8392 const opj_dec_memory_marker_handler_t *e;
8393 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8394 if (e->id == p_id) {
8395 break; /* we find a handler corresponding to the marker ID*/
8403 * Destroys a jpeg2000 codec.
8405 * @param p_j2k the jpeg20000 structure to destroy.
8407 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8413 if (p_j2k->m_is_decoder) {
8415 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8416 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8417 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8418 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8421 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8422 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8423 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8424 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8429 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8430 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8431 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8434 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8435 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8436 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8437 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8440 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8441 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8442 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8443 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8447 tcd_destroy_v2(p_j2k->m_tcd);
8449 j2k_cp_destroy(&(p_j2k->m_cp));
8450 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8452 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8453 p_j2k->m_procedure_list = 00;
8455 opj_procedure_list_destroy(p_j2k->m_validation_list);
8456 p_j2k->m_procedure_list = 00;
8458 j2k_destroy_cstr_index(p_j2k->cstr_index);
8459 p_j2k->cstr_index = NULL;
8461 opj_image_destroy(p_j2k->m_private_image);
8462 p_j2k->m_private_image = NULL;
8464 opj_image_destroy(p_j2k->m_output_image);
8465 p_j2k->m_output_image = NULL;
8470 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8474 if (p_cstr_ind->marker) {
8475 opj_free(p_cstr_ind->marker);
8476 p_cstr_ind->marker = NULL;
8479 if (p_cstr_ind->tile_index) {
8480 OPJ_UINT32 it_tile = 0;
8482 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8484 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8485 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8486 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8489 if(p_cstr_ind->tile_index[it_tile].tp_index){
8490 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8491 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8494 if(p_cstr_ind->tile_index[it_tile].marker){
8495 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8496 p_cstr_ind->tile_index[it_tile].marker = NULL;
8501 opj_free( p_cstr_ind->tile_index);
8502 p_cstr_ind->tile_index = NULL;
8505 opj_free(p_cstr_ind);
8512 * Destroys a tile coding parameter structure.
8514 * @param p_tcp the tile coding parameter to destroy.
8516 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8522 if (p_tcp->ppt_buffer != 00) {
8523 opj_free(p_tcp->ppt_buffer);
8524 p_tcp->ppt_buffer = 00;
8527 if (p_tcp->tccps != 00) {
8528 opj_free(p_tcp->tccps);
8532 if (p_tcp->m_mct_coding_matrix != 00) {
8533 opj_free(p_tcp->m_mct_coding_matrix);
8534 p_tcp->m_mct_coding_matrix = 00;
8537 if (p_tcp->m_mct_decoding_matrix != 00) {
8538 opj_free(p_tcp->m_mct_decoding_matrix);
8539 p_tcp->m_mct_decoding_matrix = 00;
8542 if (p_tcp->m_mcc_records) {
8543 opj_free(p_tcp->m_mcc_records);
8544 p_tcp->m_mcc_records = 00;
8545 p_tcp->m_nb_max_mcc_records = 0;
8546 p_tcp->m_nb_mcc_records = 0;
8549 if (p_tcp->m_mct_records) {
8550 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8553 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8554 if (l_mct_data->m_data) {
8555 opj_free(l_mct_data->m_data);
8556 l_mct_data->m_data = 00;
8562 opj_free(p_tcp->m_mct_records);
8563 p_tcp->m_mct_records = 00;
8566 if (p_tcp->mct_norms != 00) {
8567 opj_free(p_tcp->mct_norms);
8568 p_tcp->mct_norms = 00;
8571 j2k_tcp_data_destroy(p_tcp);
8576 * Destroys the data inside a tile coding parameter structure.
8578 * @param p_tcp the tile coding parameter which contain data to destroy.
8580 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8582 if (p_tcp->m_data) {
8583 opj_free(p_tcp->m_data);
8584 p_tcp->m_data = NULL;
8585 p_tcp->m_data_size = 0;
8590 * Destroys a coding parameter structure.
8592 * @param p_cp the coding parameter to destroy.
8594 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8596 OPJ_UINT32 l_nb_tiles;
8597 opj_tcp_v2_t * l_current_tile = 00;
8608 l_current_tile = p_cp->tcps;
8609 l_nb_tiles = p_cp->th * p_cp->tw;
8612 (i = 0; i < l_nb_tiles; ++i)
8614 j2k_tcp_destroy(l_current_tile);
8617 opj_free(p_cp->tcps);
8621 (p_cp->ppm_buffer != 00)
8623 opj_free(p_cp->ppm_buffer);
8624 p_cp->ppm_buffer = 00;
8627 (p_cp->comment != 00)
8629 opj_free(p_cp->comment);
8633 (! p_cp->m_is_decoder)
8636 (p_cp->m_specific_param.m_enc.m_matrice)
8638 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8639 p_cp->m_specific_param.m_enc.m_matrice = 00;
8647 * Reads a tile header.
8648 * @param p_j2k the jpeg2000 codec.
8649 * @param p_stream the stream to write data to.
8650 * @param p_manager the user event manager.
8652 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8653 OPJ_UINT32 * p_tile_index,
8654 OPJ_UINT32 * p_data_size,
8655 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8656 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8657 OPJ_UINT32 * p_nb_comps,
8659 opj_stream_private_t *p_stream,
8660 opj_event_mgr_t * p_manager )
8662 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8663 OPJ_UINT32 l_marker_size;
8664 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8665 opj_tcp_v2_t * l_tcp = NULL;
8666 OPJ_UINT32 l_nb_tiles;
8669 assert(p_stream != 00);
8670 assert(p_j2k != 00);
8671 assert(p_manager != 00);
8673 /* Reach the End Of Codestream ?*/
8674 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8675 l_current_marker = J2K_MS_EOC;
8677 /* We need to encounter a SOT marker (a new tile-part header) */
8678 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8682 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8683 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8685 /* Try to read until the Start Of Data is detected */
8686 while (l_current_marker != J2K_MS_SOD) {
8688 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8689 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8690 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8694 /* Read 2 bytes from the buffer as the marker size */
8695 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8697 /* Why this condition? FIXME */
8698 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8699 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8701 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8703 /* Get the marker handler from the marker ID */
8704 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8706 /* Check if the marker is known and if it is the right place to find it */
8707 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8708 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8711 /* FIXME manage case of unknown marker as in the main header ? */
8713 /* Check if the marker size is compatible with the header data size */
8714 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8715 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8716 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8717 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8720 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8723 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8724 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) {
8725 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8729 /* Read the marker segment with the correct marker handler */
8730 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8731 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8735 /* Add the marker to the codestream index*/
8736 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8738 l_marker_handler->id,
8739 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8740 l_marker_size + 4 );
8742 /* Keep the position of the last SOT marker read */
8743 if ( l_marker_handler->id == J2K_MS_SOT ) {
8744 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8745 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8747 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8752 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8753 /* Skip the rest of the tile part header*/
8754 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) {
8755 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8758 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8761 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8762 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8763 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8766 /* Read 2 bytes from the buffer as the new marker ID */
8767 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8771 /* If we didn't skip data before, we need to read the SOD marker*/
8772 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8773 /* Try to read the SOD marker and skip data ? FIXME */
8774 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8780 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8781 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8782 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8783 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8787 /* Read 2 bytes from buffer as the new marker ID */
8788 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8792 /* Indicate we will try to read a new tile-part header*/
8793 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8794 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8795 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8797 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8798 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8799 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8803 /* Read 2 bytes from buffer as the new marker ID */
8804 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8808 /* Current marker is the EOC marker ?*/
8809 if (l_current_marker == J2K_MS_EOC) {
8810 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8811 p_j2k->m_current_tile_number = 0;
8812 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8817 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8818 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8819 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8821 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8822 ++p_j2k->m_current_tile_number;
8826 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8827 *p_go_on = OPJ_FALSE;
8833 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8834 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8838 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8839 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8841 *p_tile_index = p_j2k->m_current_tile_number;
8842 *p_go_on = OPJ_TRUE;
8843 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8844 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8845 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8846 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8847 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8848 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8850 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8856 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8857 OPJ_UINT32 p_tile_index,
8859 OPJ_UINT32 p_data_size,
8860 opj_stream_private_t *p_stream,
8861 opj_event_mgr_t * p_manager )
8863 OPJ_UINT32 l_current_marker;
8864 OPJ_BYTE l_data [2];
8865 opj_tcp_v2_t * l_tcp;
8868 assert(p_stream != 00);
8869 assert(p_j2k != 00);
8870 assert(p_manager != 00);
8872 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8873 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8877 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8878 if (! l_tcp->m_data) {
8879 j2k_tcp_destroy(l_tcp);
8883 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8887 p_j2k->cstr_index) ) {
8888 j2k_tcp_destroy(l_tcp);
8889 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8893 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8897 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8898 * we destroy just the data which will be re-read in read_tile_header*/
8899 /*j2k_tcp_destroy(l_tcp);
8900 p_j2k->m_tcd->tcp = 0;*/
8901 j2k_tcp_data_destroy(l_tcp);
8903 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8904 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8906 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8907 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8908 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8912 opj_read_bytes(l_data,&l_current_marker,2);
8914 if (l_current_marker == J2K_MS_EOC) {
8915 p_j2k->m_current_tile_number = 0;
8916 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8918 else if (l_current_marker != J2K_MS_SOT)
8920 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8929 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8931 OPJ_UINT32 i,j,k = 0;
8932 OPJ_UINT32 l_width_src,l_height_src;
8933 OPJ_UINT32 l_width_dest,l_height_dest;
8934 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8935 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8936 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8937 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8938 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8940 opj_image_comp_t * l_img_comp_src = 00;
8941 opj_image_comp_t * l_img_comp_dest = 00;
8943 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8944 opj_image_t * l_image_src = 00;
8945 OPJ_UINT32 l_size_comp, l_remaining;
8946 OPJ_INT32 * l_dest_ptr;
8947 opj_tcd_resolution_v2_t* l_res= 00;
8949 l_tilec = p_tcd->tcd_image->tiles->comps;
8950 l_image_src = p_tcd->image;
8951 l_img_comp_src = l_image_src->comps;
8953 l_img_comp_dest = p_output_image->comps;
8955 for (i=0; i<l_image_src->numcomps; i++) {
8957 /* Allocate output component buffer if necessary */
8958 if (!l_img_comp_dest->data) {
8960 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8961 if (! l_img_comp_dest->data) {
8966 /* Copy info from decoded comp image to output image */
8967 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8970 /* Compute the precision of the output buffer */
8971 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8972 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8973 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8979 if (l_size_comp == 3) {
8984 /* Current tile component size*/
8986 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8987 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8990 l_width_src = (l_res->x1 - l_res->x0);
8991 l_height_src = (l_res->y1 - l_res->y0);
8993 /* Border of the current output component*/
8994 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8995 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8996 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8997 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9000 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9001 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9005 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9006 * of the input buffer (decoded tile component) which will be move
9007 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9008 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9009 * by this input area.
9011 assert( l_res->x0 >= 0);
9012 assert( l_res->x1 >= 0);
9013 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9014 l_start_x_dest = l_res->x0 - l_x0_dest;
9015 l_offset_x0_src = 0;
9017 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9018 l_width_dest = l_width_src;
9019 l_offset_x1_src = 0;
9022 l_width_dest = l_x1_dest - l_res->x0 ;
9023 l_offset_x1_src = l_width_src - l_width_dest;
9027 l_start_x_dest = 0 ;
9028 l_offset_x0_src = l_x0_dest - l_res->x0;
9030 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9031 l_width_dest = l_width_src - l_offset_x0_src;
9032 l_offset_x1_src = 0;
9035 l_width_dest = l_img_comp_dest->w ;
9036 l_offset_x1_src = l_res->x1 - l_x1_dest;
9040 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9041 l_start_y_dest = l_res->y0 - l_y0_dest;
9042 l_offset_y0_src = 0;
9044 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9045 l_height_dest = l_height_src;
9046 l_offset_y1_src = 0;
9049 l_height_dest = l_y1_dest - l_res->y0 ;
9050 l_offset_y1_src = l_height_src - l_height_dest;
9054 l_start_y_dest = 0 ;
9055 l_offset_y0_src = l_y0_dest - l_res->y0;
9057 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9058 l_height_dest = l_height_src - l_offset_y0_src;
9059 l_offset_y1_src = 0;
9062 l_height_dest = l_img_comp_dest->h ;
9063 l_offset_y1_src = l_res->y1 - l_y1_dest;
9067 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9072 /* Compute the input buffer offset */
9073 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9074 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9075 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9077 /* Compute the output buffer offset */
9078 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9079 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9081 /* Move the output buffer to the first place where we will write*/
9082 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9085 fprintf(stdout, "COMPO[%d]:\n",i);
9086 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9087 "\t tile offset:%d, %d, %d, %d\n"
9088 "\t buffer offset: %d; %d, %d\n",
9089 l_res->x0, l_res->y0, l_width_src, l_height_src,
9090 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9091 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9093 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9094 "\t start offset: %d, line offset= %d\n",
9095 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9099 switch (l_size_comp) {
9102 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9103 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9105 if (l_img_comp_src->sgnd) {
9106 for (j = 0 ; j < l_height_dest ; ++j) {
9107 for ( k = 0 ; k < l_width_dest ; ++k) {
9108 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9111 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9112 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9116 for ( j = 0 ; j < l_height_dest ; ++j ) {
9117 for ( k = 0 ; k < l_width_dest ; ++k) {
9118 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9121 l_dest_ptr+= l_line_offset_dest;
9122 l_src_ptr += l_line_offset_src;
9126 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9127 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9132 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9133 l_src_ptr += l_start_offset_src;
9135 if (l_img_comp_src->sgnd) {
9136 for (j=0;j<l_height_dest;++j) {
9137 for (k=0;k<l_width_dest;++k) {
9138 *(l_dest_ptr++) = *(l_src_ptr++);
9141 l_dest_ptr+= l_line_offset_dest;
9142 l_src_ptr += l_line_offset_src ;
9146 for (j=0;j<l_height_dest;++j) {
9147 for (k=0;k<l_width_dest;++k) {
9148 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9151 l_dest_ptr+= l_line_offset_dest;
9152 l_src_ptr += l_line_offset_src ;
9156 l_src_ptr += l_end_offset_src;
9157 p_data = (OPJ_BYTE*) l_src_ptr;
9162 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9163 l_src_ptr += l_start_offset_src;
9165 for (j=0;j<l_height_dest;++j) {
9166 for (k=0;k<l_width_dest;++k) {
9167 *(l_dest_ptr++) = (*(l_src_ptr++));
9170 l_dest_ptr+= l_line_offset_dest;
9171 l_src_ptr += l_line_offset_src ;
9174 l_src_ptr += l_end_offset_src;
9175 p_data = (OPJ_BYTE*) l_src_ptr;
9189 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9191 * @param p_j2k the jpeg2000 codec.
9192 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9193 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9194 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9195 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9196 * @param p_manager the user event manager
9198 * @return true if the area could be set.
9200 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9201 opj_image_t* p_image,
9202 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9203 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9204 struct opj_event_mgr * p_manager )
9206 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9207 opj_image_t * l_image = p_j2k->m_private_image;
9210 OPJ_INT32 l_comp_x1, l_comp_y1;
9211 opj_image_comp_t* l_img_comp = NULL;
9213 /* Check if we are read the main header */
9214 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9215 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9219 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9220 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9222 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9223 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9224 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9225 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9231 /* Check if the positions provided by the user are correct */
9234 assert(p_start_x >= 0 );
9235 assert(p_start_y >= 0 );
9237 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9238 opj_event_msg_v2(p_manager, EVT_ERROR,
9239 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9240 p_start_x, l_image->x1);
9243 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9244 opj_event_msg_v2(p_manager, EVT_WARNING,
9245 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9246 p_start_x, l_image->x0);
9247 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9248 p_image->x0 = l_image->x0;
9251 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9252 p_image->x0 = p_start_x;
9256 if ((OPJ_UINT32)p_start_y > l_image->y1){
9257 opj_event_msg_v2(p_manager, EVT_ERROR,
9258 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9259 p_start_y, l_image->y1);
9262 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9263 opj_event_msg_v2(p_manager, EVT_WARNING,
9264 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9265 p_start_y, l_image->y0);
9266 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9267 p_image->y0 = l_image->y0;
9270 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9271 p_image->y0 = p_start_y;
9275 assert((OPJ_UINT32)p_end_x > 0);
9276 assert((OPJ_UINT32)p_end_y > 0);
9277 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9278 opj_event_msg_v2(p_manager, EVT_ERROR,
9279 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9280 p_end_x, l_image->x0);
9283 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9284 opj_event_msg_v2(p_manager, EVT_WARNING,
9285 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9286 p_end_x, l_image->x1);
9287 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9288 p_image->x1 = l_image->x1;
9291 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9292 p_image->x1 = p_end_x;
9296 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9297 opj_event_msg_v2(p_manager, EVT_ERROR,
9298 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9299 p_end_y, l_image->y0);
9302 if ((OPJ_UINT32)p_end_y > l_image->y1){
9303 opj_event_msg_v2(p_manager, EVT_WARNING,
9304 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9305 p_end_y, l_image->y1);
9306 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9307 p_image->y1 = l_image->y1;
9310 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9311 p_image->y1 = p_end_y;
9315 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9317 l_img_comp = p_image->comps;
9318 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9322 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9323 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9324 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9325 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9327 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9328 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9330 opj_event_msg_v2(p_manager, EVT_ERROR,
9331 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9335 l_img_comp->w = l_w;
9337 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9338 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9340 opj_event_msg_v2(p_manager, EVT_ERROR,
9341 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9345 l_img_comp->h = l_h;
9350 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9351 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9358 /* ----------------------------------------------------------------------- */
9359 /* J2K / JPT decoder interface */
9360 /* ----------------------------------------------------------------------- */
9362 * Creates a J2K decompression structure.
9364 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9366 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9368 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9372 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9374 l_j2k->m_is_decoder = 1;
9375 l_j2k->m_cp.m_is_decoder = 1;
9377 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9378 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9382 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9384 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9385 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9390 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9392 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9394 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9396 /* codestream index creation */
9397 l_j2k->cstr_index = j2k_create_cstr_index();
9399 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9400 if (!l_j2k->cstr_index){
9405 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9408 /* validation list creation */
9409 l_j2k->m_validation_list = opj_procedure_list_create();
9410 if (! l_j2k->m_validation_list) {
9415 /* execution list creation */
9416 l_j2k->m_procedure_list = opj_procedure_list_create();
9417 if (! l_j2k->m_procedure_list) {
9426 opj_codestream_index_t* j2k_create_cstr_index(void)
9428 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9429 opj_calloc(1,sizeof(opj_codestream_index_t));
9433 cstr_index->maxmarknum = 100;
9434 cstr_index->marknum = 0;
9435 cstr_index->marker = (opj_marker_info_t*)
9436 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9437 if (!cstr_index-> marker)
9440 cstr_index->tile_index = NULL;
9447 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9449 * @param p_tile_no the tile index.
9450 * @param p_comp_no the component being outputted.
9451 * @param p_j2k the J2K codec.
9453 * @return the number of bytes taken by the SPCod element.
9455 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9456 OPJ_UINT32 p_tile_no,
9457 OPJ_UINT32 p_comp_no )
9459 opj_cp_v2_t *l_cp = 00;
9460 opj_tcp_v2_t *l_tcp = 00;
9461 opj_tccp_t *l_tccp = 00;
9464 assert(p_j2k != 00);
9466 l_cp = &(p_j2k->m_cp);
9467 l_tcp = &l_cp->tcps[p_tile_no];
9468 l_tccp = &l_tcp->tccps[p_comp_no];
9470 /* preconditions again */
9471 assert(p_tile_no < (l_cp->tw * l_cp->th));
9472 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9474 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9475 return 5 + l_tccp->numresolutions;
9483 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9485 * @param p_comp_no the component number to output.
9486 * @param p_stream the stream to write data to.
9487 * @param p_j2k J2K codec.
9488 * @param p_manager the user event manager.
9491 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9492 OPJ_UINT32 p_tile_no,
9493 OPJ_UINT32 p_comp_no,
9495 OPJ_UINT32 * p_header_size,
9496 struct opj_event_mgr * p_manager )
9499 opj_cp_v2_t *l_cp = 00;
9500 opj_tcp_v2_t *l_tcp = 00;
9501 opj_tccp_t *l_tccp = 00;
9504 assert(p_j2k != 00);
9505 assert(p_header_size != 00);
9506 assert(p_manager != 00);
9507 assert(p_data != 00);
9509 l_cp = &(p_j2k->m_cp);
9510 l_tcp = &l_cp->tcps[p_tile_no];
9511 l_tccp = &l_tcp->tccps[p_comp_no];
9513 /* preconditions again */
9514 assert(p_tile_no < (l_cp->tw * l_cp->th));
9515 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9517 if (*p_header_size < 5) {
9518 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9522 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9525 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9528 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9531 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9534 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9537 *p_header_size = *p_header_size - 5;
9539 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9541 if (*p_header_size < l_tccp->numresolutions) {
9542 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9546 for (i = 0; i < l_tccp->numresolutions; ++i) {
9547 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9551 *p_header_size = *p_header_size - l_tccp->numresolutions;
9558 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9559 * @param p_header_data the data contained in the COM box.
9560 * @param p_j2k the jpeg2000 codec.
9561 * @param p_header_size the size of the data contained in the COM marker.
9562 * @param p_manager the user event manager.
9564 opj_bool j2k_read_SPCod_SPCoc(
9565 opj_j2k_v2_t *p_j2k,
9567 OPJ_BYTE * p_header_data,
9568 OPJ_UINT32 * p_header_size,
9569 struct opj_event_mgr * p_manager
9572 OPJ_UINT32 i, l_tmp;
9573 opj_cp_v2_t *l_cp = NULL;
9574 opj_tcp_v2_t *l_tcp = NULL;
9575 opj_tccp_t *l_tccp = NULL;
9576 OPJ_BYTE * l_current_ptr = NULL;
9579 assert(p_j2k != 00);
9580 assert(p_manager != 00);
9581 assert(p_header_data != 00);
9583 l_cp = &(p_j2k->m_cp);
9584 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9585 &l_cp->tcps[p_j2k->m_current_tile_number] :
9586 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9588 /* precondition again */
9589 assert(compno < p_j2k->m_private_image->numcomps);
9591 l_tccp = &l_tcp->tccps[compno];
9592 l_current_ptr = p_header_data;
9594 /* make sure room is sufficient */
9595 if (*p_header_size < 5) {
9596 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9600 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9601 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9604 /* If user wants to remove more resolutions than the codestream contains, return error */
9605 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9606 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9607 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9608 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9612 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9616 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9620 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9623 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9626 *p_header_size = *p_header_size - 5;
9628 /* use custom precinct size ? */
9629 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9630 if (*p_header_size < l_tccp->numresolutions) {
9631 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9635 for (i = 0; i < l_tccp->numresolutions; ++i) {
9636 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9638 l_tccp->prcw[i] = l_tmp & 0xf;
9639 l_tccp->prch[i] = l_tmp >> 4;
9642 *p_header_size = *p_header_size - l_tccp->numresolutions;
9645 /* set default size for the precinct width and height */
9646 for (i = 0; i < l_tccp->numresolutions; ++i) {
9647 l_tccp->prcw[i] = 15;
9648 l_tccp->prch[i] = 15;
9652 #ifdef WIP_REMOVE_MSD
9654 if (p_j2k->cstr_info && compno == 0) {
9655 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9657 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9658 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9659 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9660 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9661 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9664 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9665 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9674 * Copies the tile component parameters of all the component from the first tile component.
9676 * @param p_j2k the J2k codec.
9678 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9682 opj_cp_v2_t *l_cp = NULL;
9683 opj_tcp_v2_t *l_tcp = NULL;
9684 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9685 OPJ_UINT32 l_prc_size;
9688 assert(p_j2k != 00);
9690 l_cp = &(p_j2k->m_cp);
9691 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9692 &l_cp->tcps[p_j2k->m_current_tile_number] :
9693 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9695 l_ref_tccp = &l_tcp->tccps[0];
9696 l_copied_tccp = l_ref_tccp + 1;
9697 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9699 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9700 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9701 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9702 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9703 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9704 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9705 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9706 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9712 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9714 * @param p_tile_no the tile index.
9715 * @param p_comp_no the component being outputted.
9716 * @param p_j2k the J2K codec.
9718 * @return the number of bytes taken by the SPCod element.
9720 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9721 OPJ_UINT32 p_tile_no,
9722 OPJ_UINT32 p_comp_no )
9724 OPJ_UINT32 l_num_bands;
9726 opj_cp_v2_t *l_cp = 00;
9727 opj_tcp_v2_t *l_tcp = 00;
9728 opj_tccp_t *l_tccp = 00;
9731 assert(p_j2k != 00);
9733 l_cp = &(p_j2k->m_cp);
9734 l_tcp = &l_cp->tcps[p_tile_no];
9735 l_tccp = &l_tcp->tccps[p_comp_no];
9737 /* preconditions again */
9738 assert(p_tile_no < l_cp->tw * l_cp->th);
9739 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9741 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9743 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9744 return 1 + l_num_bands;
9747 return 1 + 2*l_num_bands;
9752 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9754 * @param p_tile_no the tile to output.
9755 * @param p_comp_no the component number to output.
9756 * @param p_data the data buffer.
9757 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9758 * @param p_j2k J2K codec.
9759 * @param p_manager the user event manager.
9762 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9763 OPJ_UINT32 p_tile_no,
9764 OPJ_UINT32 p_comp_no,
9766 OPJ_UINT32 * p_header_size,
9767 struct opj_event_mgr * p_manager )
9769 OPJ_UINT32 l_header_size;
9770 OPJ_UINT32 l_band_no, l_num_bands;
9771 OPJ_UINT32 l_expn,l_mant;
9773 opj_cp_v2_t *l_cp = 00;
9774 opj_tcp_v2_t *l_tcp = 00;
9775 opj_tccp_t *l_tccp = 00;
9778 assert(p_j2k != 00);
9779 assert(p_header_size != 00);
9780 assert(p_manager != 00);
9781 assert(p_data != 00);
9783 l_cp = &(p_j2k->m_cp);
9784 l_tcp = &l_cp->tcps[p_tile_no];
9785 l_tccp = &l_tcp->tccps[p_comp_no];
9787 /* preconditions again */
9788 assert(p_tile_no < l_cp->tw * l_cp->th);
9789 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9791 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9793 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9794 l_header_size = 1 + l_num_bands;
9796 if (*p_header_size < l_header_size) {
9797 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9801 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9804 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9805 l_expn = l_tccp->stepsizes[l_band_no].expn;
9806 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9811 l_header_size = 1 + 2*l_num_bands;
9813 if (*p_header_size < l_header_size) {
9814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9818 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9821 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9822 l_expn = l_tccp->stepsizes[l_band_no].expn;
9823 l_mant = l_tccp->stepsizes[l_band_no].mant;
9825 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9830 *p_header_size = *p_header_size - l_header_size;
9836 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9838 * @param p_comp_no the component being targeted.
9839 * @param p_header_data the data contained in the COM box.
9840 * @param p_j2k the jpeg2000 codec.
9841 * @param p_header_size the size of the data contained in the COM marker.
9842 * @param p_manager the user event manager.
9844 opj_bool j2k_read_SQcd_SQcc(
9845 opj_j2k_v2_t *p_j2k,
9846 OPJ_UINT32 p_comp_no,
9847 OPJ_BYTE* p_header_data,
9848 OPJ_UINT32 * p_header_size,
9849 struct opj_event_mgr * p_manager
9853 OPJ_UINT32 l_band_no;
9854 opj_cp_v2_t *l_cp = 00;
9855 opj_tcp_v2_t *l_tcp = 00;
9856 opj_tccp_t *l_tccp = 00;
9857 OPJ_BYTE * l_current_ptr = 00;
9858 OPJ_UINT32 l_tmp, l_num_band;
9861 assert(p_j2k != 00);
9862 assert(p_manager != 00);
9863 assert(p_header_data != 00);
9865 l_cp = &(p_j2k->m_cp);
9866 /* come from tile part header or main header ?*/
9867 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9868 &l_cp->tcps[p_j2k->m_current_tile_number] :
9869 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9871 /* precondition again*/
9872 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9874 l_tccp = &l_tcp->tccps[p_comp_no];
9875 l_current_ptr = p_header_data;
9877 if (*p_header_size < 1) {
9878 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9881 *p_header_size -= 1;
9883 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9886 l_tccp->qntsty = l_tmp & 0x1f;
9887 l_tccp->numgbits = l_tmp >> 5;
9888 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9892 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9894 (*p_header_size) / 2;
9896 if( l_num_band > J2K_MAXBANDS ) {
9897 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9898 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9899 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9900 /*return OPJ_FALSE;*/
9905 if (l_cp->correct) {
9907 /* if JPWL is on, we check whether there are too many subbands */
9908 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9909 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9910 "JPWL: bad number of subbands in Sqcx (%d)\n",
9913 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9916 /* we try to correct */
9918 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9919 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9924 #endif /* USE_JPWL */
9926 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9927 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9928 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9930 if (l_band_no < J2K_MAXBANDS){
9931 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9932 l_tccp->stepsizes[l_band_no].mant = 0;
9935 *p_header_size = *p_header_size - l_num_band;
9938 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9939 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9941 if (l_band_no < J2K_MAXBANDS){
9942 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9943 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9946 *p_header_size = *p_header_size - 2*l_num_band;
9949 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9950 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9951 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9952 l_tccp->stepsizes[l_band_no].expn =
9953 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9954 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9955 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9963 * Copies the tile component parameters of all the component from the first tile component.
9965 * @param p_j2k the J2k codec.
9967 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9970 opj_cp_v2_t *l_cp = NULL;
9971 opj_tcp_v2_t *l_tcp = NULL;
9972 opj_tccp_t *l_ref_tccp = NULL;
9973 opj_tccp_t *l_copied_tccp = NULL;
9977 assert(p_j2k != 00);
9979 l_cp = &(p_j2k->m_cp);
9980 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9981 &l_cp->tcps[p_j2k->m_current_tile_number] :
9982 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9984 l_ref_tccp = &l_tcp->tccps[0];
9985 l_copied_tccp = l_ref_tccp + 1;
9986 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9988 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9989 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9990 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9991 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9997 * Dump some elements from the J2K decompression structure .
9999 *@param p_j2k the jpeg2000 codec.
10000 *@param flag flag to describe what elments are dump.
10001 *@param out_stream output stream where dump the elements.
10004 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10006 /* Check if the flag is compatible with j2k file*/
10007 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10008 fprintf(out_stream, "Wrong flag\n");
10012 /* Dump the image_header */
10013 if (flag & OPJ_IMG_INFO){
10014 if (p_j2k->m_private_image)
10015 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10018 /* Dump the codestream info from main header */
10019 if (flag & OPJ_J2K_MH_INFO){
10020 j2k_dump_MH_info(p_j2k, out_stream);
10024 /* Dump the codestream info of the current tile */
10025 if (flag & OPJ_J2K_TH_INFO){
10029 /* Dump the codestream index from main header */
10030 if (flag & OPJ_J2K_MH_IND){
10031 j2k_dump_MH_index(p_j2k, out_stream);
10034 /* Dump the codestream index of the current tile */
10035 if (flag & OPJ_J2K_TH_IND){
10042 * Dump index elements of the codestream extract from the main header.
10044 *@param p_j2k the jpeg2000 codec.
10045 *@param out_stream output stream where dump the elements.
10048 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10050 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10051 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10053 fprintf(out_stream, "Codestream index from main header: {\n");
10055 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10056 "\t Main header end position=%" PRIi64 "\n",
10057 cstr_index->main_head_start, cstr_index->main_head_end);
10059 fprintf(out_stream, "\t Marker list: {\n");
10061 if (cstr_index->marker){
10062 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10063 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10064 cstr_index->marker[it_marker].type,
10065 cstr_index->marker[it_marker].pos,
10066 cstr_index->marker[it_marker].len );
10070 fprintf(out_stream, "\t }\n");
10073 if (cstr_index->tile_index){
10075 /* Simple test to avoid to write empty information*/
10076 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10077 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10078 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10081 if (l_acc_nb_of_tile_part)
10083 fprintf(out_stream, "\t Tile index: {\n");
10085 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10086 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10088 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10090 if (cstr_index->tile_index[it_tile].tp_index){
10091 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10092 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10094 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10095 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10096 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10100 if (cstr_index->tile_index[it_tile].marker){
10101 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10102 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10103 cstr_index->tile_index[it_tile].marker[it_marker].type,
10104 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10105 cstr_index->tile_index[it_tile].marker[it_marker].len );
10109 fprintf(out_stream,"\t }\n");
10113 fprintf(out_stream,"}\n");
10118 * Dump info elements of the codestream extract from the main header.
10120 *@param p_j2k the jpeg2000 codec.
10121 *@param out_stream output stream where dump the elements.
10124 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10126 opj_tcp_v2_t * l_default_tile=NULL;
10128 fprintf(out_stream, "Codestream info from main header: {\n");
10130 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10131 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10132 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10134 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10135 if (l_default_tile)
10138 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10140 fprintf(out_stream, "\t default tile {\n");
10141 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10142 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10143 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10144 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10146 for (compno = 0; compno < numcomps; compno++) {
10147 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10149 OPJ_INT32 bandno, numbands;
10152 fprintf(out_stream, "\t\t comp %d {\n", compno);
10153 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10154 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10155 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10156 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10157 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10158 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10160 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10161 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10162 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10164 fprintf(out_stream, "\n");
10166 /* quantization style*/
10167 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10168 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10169 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10170 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10171 for (bandno = 0; bandno < numbands; bandno++) {
10172 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10173 l_tccp->stepsizes[bandno].expn);
10175 fprintf(out_stream, "\n");
10178 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10180 fprintf(out_stream, "\t\t }\n");
10181 } /*end of component of default tile*/
10182 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10186 fprintf(out_stream, "}\n");
10191 * Dump an image header structure.
10193 *@param img_header the image header to dump.
10194 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10195 *@param out_stream output stream where dump the elements.
10197 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10201 if (dev_dump_flag){
10202 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10206 fprintf(out_stream, "Image info {\n");
10207 tab[0] = '\t';tab[1] = '\0';
10210 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10211 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10212 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10214 if (img_header->comps){
10216 for (compno = 0; compno < img_header->numcomps; compno++) {
10217 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10218 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10219 fprintf(out_stream,"%s}\n",tab);
10223 fprintf(out_stream, "}\n");
10227 * Dump a component image header structure.
10229 *@param comp_header the component image header to dump.
10230 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10231 *@param out_stream output stream where dump the elements.
10233 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10237 if (dev_dump_flag){
10238 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10241 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10244 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10245 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10246 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10249 fprintf(out_stream, "}\n");
10254 * Get the codestream info from a JPEG2000 codec.
10256 *@param p_j2k the component image header to dump.
10258 *@return the codestream information extract from the jpg2000 codec
10260 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10263 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10264 opj_tcp_v2_t *l_default_tile;
10265 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10267 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10269 cstr_info->tx0 = p_j2k->m_cp.tx0;
10270 cstr_info->ty0 = p_j2k->m_cp.ty0;
10271 cstr_info->tdx = p_j2k->m_cp.tdx;
10272 cstr_info->tdy = p_j2k->m_cp.tdy;
10273 cstr_info->tw = p_j2k->m_cp.tw;
10274 cstr_info->th = p_j2k->m_cp.th;
10276 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10278 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10280 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10281 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10282 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10283 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10285 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10287 for (compno = 0; compno < numcomps; compno++) {
10288 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10289 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10290 OPJ_INT32 bandno, numbands;
10293 l_tccp_info->csty = l_tccp->csty;
10294 l_tccp_info->numresolutions = l_tccp->numresolutions;
10295 l_tccp_info->cblkw = l_tccp->cblkw;
10296 l_tccp_info->cblkh = l_tccp->cblkh;
10297 l_tccp_info->cblksty = l_tccp->cblksty;
10298 l_tccp_info->qmfbid = l_tccp->qmfbid;
10299 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10301 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10302 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10305 /* quantization style*/
10306 l_tccp_info->qntsty = l_tccp->qntsty;
10307 l_tccp_info->numgbits = l_tccp->numgbits;
10309 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10310 if (numbands < J2K_MAXBANDS) {
10311 for (bandno = 0; bandno < numbands; bandno++) {
10312 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10313 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10318 l_tccp_info->roishift = l_tccp->roishift;
10326 * Get the codestream index from a JPEG2000 codec.
10328 *@param p_j2k the component image header to dump.
10330 *@return the codestream index extract from the jpg2000 codec
10332 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10334 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10335 opj_calloc(1,sizeof(opj_codestream_index_t));
10339 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10340 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10341 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10343 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10344 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10345 if (!l_cstr_index->marker){
10346 opj_free( l_cstr_index);
10350 if (p_j2k->cstr_index->marker)
10351 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10353 opj_free(l_cstr_index->marker);
10354 l_cstr_index->marker = NULL;
10357 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10358 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10359 if (!l_cstr_index->tile_index){
10360 opj_free( l_cstr_index->marker);
10361 opj_free( l_cstr_index);
10365 if (!p_j2k->cstr_index->tile_index){
10366 opj_free(l_cstr_index->tile_index);
10367 l_cstr_index->tile_index = NULL;
10370 OPJ_UINT32 it_tile = 0;
10371 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10374 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10376 l_cstr_index->tile_index[it_tile].marker =
10377 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10379 if (!l_cstr_index->tile_index[it_tile].marker) {
10380 OPJ_UINT32 it_tile_free;
10382 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10383 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10386 opj_free( l_cstr_index->tile_index);
10387 opj_free( l_cstr_index->marker);
10388 opj_free( l_cstr_index);
10392 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10393 memcpy( l_cstr_index->tile_index[it_tile].marker,
10394 p_j2k->cstr_index->tile_index[it_tile].marker,
10395 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10397 opj_free(l_cstr_index->tile_index[it_tile].marker);
10398 l_cstr_index->tile_index[it_tile].marker = NULL;
10401 /* Tile part index*/
10402 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10404 l_cstr_index->tile_index[it_tile].tp_index =
10405 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10407 if(!l_cstr_index->tile_index[it_tile].tp_index){
10408 OPJ_UINT32 it_tile_free;
10410 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10411 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10412 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10415 opj_free( l_cstr_index->tile_index);
10416 opj_free( l_cstr_index->marker);
10417 opj_free( l_cstr_index);
10421 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10422 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10423 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10424 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10427 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10428 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10431 /* Packet index (NOT USED)*/
10432 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10433 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10438 return l_cstr_index;
10441 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10443 OPJ_UINT32 it_tile=0;
10445 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10446 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10447 if (!p_j2k->cstr_index->tile_index)
10450 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10451 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10452 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10453 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10454 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10455 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10465 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10466 opj_stream_private_t *p_stream,
10467 opj_event_mgr_t * p_manager)
10469 opj_bool l_go_on = OPJ_TRUE;
10470 OPJ_UINT32 l_current_tile_no;
10471 OPJ_UINT32 l_data_size,l_max_data_size;
10472 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10473 OPJ_UINT32 l_nb_comps;
10474 OPJ_BYTE * l_current_data;
10476 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10477 if (! l_current_data) {
10480 l_max_data_size = 1000;
10485 if (! j2k_read_tile_header( p_j2k,
10486 &l_current_tile_no,
10488 &l_tile_x0, &l_tile_y0,
10489 &l_tile_x1, &l_tile_y1,
10494 opj_free(l_current_data);
10502 if (l_data_size > l_max_data_size) {
10503 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10504 if (! l_current_data) {
10505 opj_free(l_current_data);
10509 l_max_data_size = l_data_size;
10512 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10513 opj_free(l_current_data);
10516 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);
10518 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10519 opj_free(l_current_data);
10522 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10526 opj_free(l_current_data);
10532 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10534 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10537 assert(p_j2k != 00);
10539 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10540 /* DEVELOPER CORNER, add your custom procedures */
10545 * Read and decode one tile.
10547 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10548 opj_stream_private_t *p_stream,
10549 opj_event_mgr_t * p_manager)
10551 opj_bool l_go_on = OPJ_TRUE;
10552 OPJ_UINT32 l_current_tile_no;
10553 OPJ_UINT32 l_tile_no_to_dec;
10554 OPJ_UINT32 l_data_size,l_max_data_size;
10555 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10556 OPJ_UINT32 l_nb_comps;
10557 OPJ_BYTE * l_current_data;
10559 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10560 if (! l_current_data) {
10563 l_max_data_size = 1000;
10565 /*Allocate and initialize some elements of codestrem index if not already done*/
10566 if( !p_j2k->cstr_index->tile_index)
10568 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10569 opj_free(l_current_data);
10573 /* Move into the codestream to the first SOT used to decode the desired tile */
10574 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10575 if (p_j2k->cstr_index->tile_index)
10576 if(p_j2k->cstr_index->tile_index->tp_index)
10578 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10579 /* the index for this tile has not been built,
10580 * so move to the last SOT read */
10581 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10582 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10587 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)) {
10588 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10592 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10593 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10594 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10598 if (! j2k_read_tile_header( p_j2k,
10599 &l_current_tile_no,
10601 &l_tile_x0, &l_tile_y0,
10602 &l_tile_x1, &l_tile_y1,
10607 opj_free(l_current_data);
10616 if (l_data_size > l_max_data_size) {
10617 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10618 if (! l_current_data) {
10619 opj_free(l_current_data);
10623 l_max_data_size = l_data_size;
10628 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10629 opj_free(l_current_data);
10632 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);
10634 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10635 opj_free(l_current_data);
10638 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10640 if(l_current_tile_no == l_tile_no_to_dec)
10642 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10643 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10644 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10650 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);
10655 opj_free(l_current_data);
10662 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10664 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10667 assert(p_j2k != 00);
10669 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10670 /* DEVELOPER CORNER, add your custom procedures */
10676 * Decodes the tiles of the stream.
10678 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10679 opj_stream_private_t * p_stream,
10680 opj_image_t * p_image,
10681 opj_event_mgr_t * p_manager)
10688 p_j2k->m_output_image = opj_image_create0();
10689 if (! (p_j2k->m_output_image)) {
10692 opj_copy_image_header(p_image, p_j2k->m_output_image);
10694 /* customization of the decoding */
10695 j2k_setup_decoding(p_j2k);
10697 /* Decode the codestream */
10698 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10699 opj_image_destroy(p_j2k->m_private_image);
10700 p_j2k->m_private_image = NULL;
10704 /* Move data and copy one information from codec to output image*/
10705 for (compno = 0; compno < p_image->numcomps; compno++) {
10706 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10707 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10708 p_j2k->m_output_image->comps[compno].data = NULL;
10716 * Get the decoded tile.
10718 * @param p_j2k the jpeg2000 codestream codec.
10719 * @param p_stream input_stream
10720 * @param p_image output image. .
10721 * @param p_manager the user event manager
10722 * @param tile_index index of the tile we want decode
10724 * @return true if succeed.
10726 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10727 opj_stream_private_t *p_stream,
10728 opj_image_t* p_image,
10729 struct opj_event_mgr * p_manager,
10730 OPJ_UINT32 tile_index )
10733 OPJ_UINT32 l_tile_x, l_tile_y;
10734 opj_image_comp_t* l_img_comp;
10737 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10741 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10742 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);
10746 /* Compute the dimension of the desired tile*/
10747 l_tile_x = tile_index % p_j2k->m_cp.tw;
10748 l_tile_y = tile_index / p_j2k->m_cp.tw;
10750 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10751 if (p_image->x0 < p_j2k->m_private_image->x0)
10752 p_image->x0 = p_j2k->m_private_image->x0;
10753 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10754 if (p_image->x1 > p_j2k->m_private_image->x1)
10755 p_image->x1 = p_j2k->m_private_image->x1;
10757 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10758 if (p_image->y0 < p_j2k->m_private_image->y0)
10759 p_image->y0 = p_j2k->m_private_image->y0;
10760 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10761 if (p_image->y1 > p_j2k->m_private_image->y1)
10762 p_image->y1 = p_j2k->m_private_image->y1;
10764 l_img_comp = p_image->comps;
10765 for (compno=0; compno < p_image->numcomps; ++compno)
10767 OPJ_INT32 l_comp_x1, l_comp_y1;
10769 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10771 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10772 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10773 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10774 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10776 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10777 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10782 /* Destroy the previous output image*/
10783 if (p_j2k->m_output_image)
10784 opj_image_destroy(p_j2k->m_output_image);
10786 /* Create the ouput image from the information previously computed*/
10787 p_j2k->m_output_image = opj_image_create0();
10788 if (! (p_j2k->m_output_image)) {
10791 opj_copy_image_header(p_image, p_j2k->m_output_image);
10793 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10795 /* customization of the decoding */
10796 j2k_setup_decoding_tile(p_j2k);
10798 /* Decode the codestream */
10799 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10800 opj_image_destroy(p_j2k->m_private_image);
10801 p_j2k->m_private_image = NULL;
10805 /* Move data and copy one information from codec to output image*/
10806 for (compno = 0; compno < p_image->numcomps; compno++) {
10807 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10809 if (p_image->comps[compno].data)
10810 opj_free(p_image->comps[compno].data);
10812 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10814 p_j2k->m_output_image->comps[compno].data = NULL;
10820 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10822 OPJ_UINT32 it_comp;
10824 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10826 if (p_j2k->m_private_image) {
10827 if (p_j2k->m_private_image->comps) {
10828 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10829 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10830 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10831 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10832 if ( res_factor >= max_res){
10833 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10836 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10849 * Encodes all the tiles in a row.
10851 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10852 opj_stream_private_t *p_stream,
10853 opj_event_mgr_t * p_manager )
10856 OPJ_UINT32 l_nb_tiles;
10857 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10858 OPJ_BYTE * l_current_data;
10860 /* preconditions */
10861 assert(p_j2k != 00);
10862 assert(p_stream != 00);
10863 assert(p_manager != 00);
10865 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10866 if (! l_current_data) {
10869 l_max_tile_size = 1000;
10871 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10872 for (i=0;i<l_nb_tiles;++i) {
10873 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10874 opj_free(l_current_data);
10878 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10879 if (l_current_tile_size > l_max_tile_size) {
10880 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10881 if (! l_current_data) {
10884 l_max_tile_size = l_current_tile_size;
10887 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10889 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10894 opj_free(l_current_data);
10899 * Ends the compression procedures and possibility add data to be read after the
10902 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
10903 opj_stream_private_t *p_stream,
10904 struct opj_event_mgr * p_manager)
10906 /* customization of the encoding */
10907 j2k_setup_end_compress(p_j2k);
10909 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10919 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10921 * @param p_j2k the jpeg2000 codec.
10922 * @param p_stream the stream object.
10923 * @param p_manager the user event manager.
10925 * @return true if the codec is valid.
10927 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
10928 opj_stream_private_t *p_stream,
10929 opj_image_t * p_image,
10930 opj_event_mgr_t * p_manager)
10932 /* preconditions */
10933 assert(p_j2k != 00);
10934 assert(p_stream != 00);
10935 assert(p_manager != 00);
10937 p_j2k->m_private_image = opj_image_create0();
10938 opj_copy_image_header(p_image, p_j2k->m_private_image);
10940 // TODO_MSD: Find a better way
10941 if (p_image->comps) {
10942 OPJ_UINT32 it_comp;
10943 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10944 if (p_image->comps[it_comp].data) {
10945 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10946 p_image->comps[it_comp].data = NULL;
10952 /* customization of the validation */
10953 j2k_setup_encoding_validation (p_j2k);
10955 /* validation of the parameters codec */
10956 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10960 /* customization of the encoding */
10961 j2k_setup_header_writting(p_j2k);
10964 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10974 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10975 OPJ_UINT32 p_tile_index,
10976 opj_stream_private_t *p_stream,
10977 opj_event_mgr_t * p_manager )
10980 if (p_tile_index != p_j2k->m_current_tile_number) {
10981 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10985 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);
10987 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10988 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10989 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10991 /* initialisation before tile encoding */
10992 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11002 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11004 OPJ_UINT32 i,j,k = 0;
11005 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11006 opj_image_comp_t * l_img_comp = 00;
11007 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11008 opj_image_t * l_image = 00;
11009 OPJ_UINT32 l_size_comp, l_remaining;
11010 OPJ_INT32 * l_src_ptr;
11011 l_tilec = p_tcd->tcd_image->tiles->comps;
11012 l_image = p_tcd->image;
11013 l_img_comp = l_image->comps;
11015 for (i=0;i<p_tcd->image->numcomps;++i) {
11016 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11017 l_remaining = l_img_comp->prec & 7; /* (%8) */
11022 if (l_size_comp == 3) {
11026 l_width = (l_tilec->x1 - l_tilec->x0);
11027 l_height = (l_tilec->y1 - l_tilec->y0);
11028 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11029 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11030 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11031 l_stride = l_image_width - l_width;
11032 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11034 switch (l_size_comp) {
11037 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11038 if (l_img_comp->sgnd) {
11039 for (j=0;j<l_height;++j) {
11040 for (k=0;k<l_width;++k) {
11041 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11045 l_src_ptr += l_stride;
11049 for (j=0;j<l_height;++j) {
11050 for (k=0;k<l_width;++k) {
11051 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11055 l_src_ptr += l_stride;
11059 p_data = (OPJ_BYTE*) l_dest_ptr;
11064 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11065 if (l_img_comp->sgnd) {
11066 for (j=0;j<l_height;++j) {
11067 for (k=0;k<l_width;++k) {
11068 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11070 l_src_ptr += l_stride;
11074 for (j=0;j<l_height;++j) {
11075 for (k=0;k<l_width;++k) {
11076 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11078 l_src_ptr += l_stride;
11082 p_data = (OPJ_BYTE*) l_dest_ptr;
11087 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11088 for (j=0;j<l_height;++j) {
11089 for (k=0;k<l_width;++k) {
11090 *(l_dest_ptr++) = *(l_src_ptr++);
11092 l_src_ptr += l_stride;
11095 p_data = (OPJ_BYTE*) l_dest_ptr;
11108 * @param p_j2k the jpeg2000 codec.
11109 * @param p_stream the stream to write data to.
11110 * @param p_manager the user event manager.
11112 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11114 OPJ_UINT32 p_data_size,
11115 opj_stream_private_t *p_stream,
11116 opj_event_mgr_t * p_manager )
11118 opj_tcd_v2_t * l_tcd = 00;
11119 opj_cp_v2_t * l_cp = 00;
11120 opj_tcp_v2_t * l_tcp = 00;
11121 OPJ_UINT32 l_nb_bytes_written;
11122 OPJ_BYTE * l_current_data = 00;
11123 OPJ_UINT32 l_tile_size = 0;
11124 OPJ_UINT32 l_available_data;
11126 /* preconditions */
11127 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11129 l_tcd = p_j2k->m_tcd;
11130 l_cp = &(p_j2k->m_cp);
11131 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11133 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11134 l_available_data = l_tile_size;
11135 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11137 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11138 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11142 l_nb_bytes_written = 0;
11143 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11146 l_current_data += l_nb_bytes_written;
11147 l_available_data -= l_nb_bytes_written;
11149 l_nb_bytes_written = 0;
11150 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11154 l_available_data -= l_nb_bytes_written;
11155 l_nb_bytes_written = l_tile_size - l_available_data;
11157 if ( opj_stream_write_data( p_stream,
11158 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11159 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11163 ++p_j2k->m_current_tile_number;
11170 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11171 * are valid. Developers wanting to extend the library can add their own validation procedures.
11173 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11175 /* preconditions */
11176 assert(p_j2k != 00);
11178 /* DEVELOPER CORNER, insert your custom procedures */
11179 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11181 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11182 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11185 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11186 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11187 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11191 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11192 * are valid. Developers wanting to extend the library can add their own validation procedures.
11194 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11196 /* preconditions */
11197 assert(p_j2k != 00);
11199 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11200 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11202 /* DEVELOPER CORNER, add your custom validation procedure */
11203 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11208 * Sets up the procedures to do on writing header.
11209 * Developers wanting to extend the library can add their own writing procedures.
11211 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11213 /* preconditions */
11214 assert(p_j2k != 00);
11216 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11217 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11218 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11219 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11220 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11223 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11224 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11225 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11227 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11228 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11232 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11234 if (p_j2k->m_cp.comment != 00) {
11235 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11238 /* DEVELOPER CORNER, insert your custom procedures */
11239 if (p_j2k->m_cp.rsiz & MCT) {
11240 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11242 /* End of Developer Corner */
11244 if (p_j2k->cstr_index) {
11245 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11248 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11249 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11253 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11255 OPJ_UINT32 * p_data_written,
11256 OPJ_UINT32 p_total_data_size,
11257 opj_stream_private_t *p_stream,
11258 struct opj_event_mgr * p_manager )
11261 OPJ_UINT32 l_nb_bytes_written = 0;
11262 OPJ_UINT32 l_current_nb_bytes_written;
11263 OPJ_BYTE * l_begin_data = 00;
11265 opj_tcp_v2_t *l_tcp = 00;
11266 opj_tcd_v2_t * l_tcd = 00;
11267 opj_cp_v2_t * l_cp = 00;
11269 l_tcd = p_j2k->m_tcd;
11270 l_cp = &(p_j2k->m_cp);
11271 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11273 l_tcd->cur_pino = 0;
11275 /*Get number of tile parts*/
11276 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11281 l_current_nb_bytes_written = 0;
11282 l_begin_data = p_data;
11283 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11288 l_nb_bytes_written += l_current_nb_bytes_written;
11289 p_data += l_current_nb_bytes_written;
11290 p_total_data_size -= l_current_nb_bytes_written;
11292 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11293 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11294 l_current_nb_bytes_written = 0;
11295 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11296 l_nb_bytes_written += l_current_nb_bytes_written;
11297 p_data += l_current_nb_bytes_written;
11298 p_total_data_size -= l_current_nb_bytes_written;
11300 l_current_nb_bytes_written = 0;
11301 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11302 l_nb_bytes_written += l_current_nb_bytes_written;
11303 p_data += l_current_nb_bytes_written;
11304 p_total_data_size -= l_current_nb_bytes_written;
11307 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11308 l_current_nb_bytes_written = 0;
11309 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11310 l_nb_bytes_written += l_current_nb_bytes_written;
11311 p_data += l_current_nb_bytes_written;
11312 p_total_data_size -= l_current_nb_bytes_written;
11316 l_current_nb_bytes_written = 0;
11317 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11321 l_nb_bytes_written += l_current_nb_bytes_written;
11322 * p_data_written = l_nb_bytes_written;
11324 /* Writing Psot in SOT marker */
11325 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11327 if (l_cp->m_specific_param.m_enc.m_cinema){
11328 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11334 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11336 OPJ_UINT32 * p_data_written,
11337 OPJ_UINT32 p_total_data_size,
11338 opj_stream_private_t *p_stream,
11339 struct opj_event_mgr * p_manager
11342 OPJ_UINT32 tilepartno=0;
11343 OPJ_UINT32 l_nb_bytes_written = 0;
11344 OPJ_UINT32 l_current_nb_bytes_written;
11345 OPJ_UINT32 l_part_tile_size;
11346 OPJ_UINT32 tot_num_tp;
11349 OPJ_BYTE * l_begin_data;
11350 opj_tcp_v2_t *l_tcp = 00;
11351 opj_tcd_v2_t * l_tcd = 00;
11352 opj_cp_v2_t * l_cp = 00;
11355 l_tcd = p_j2k->m_tcd;
11356 l_cp = &(p_j2k->m_cp);
11357 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11359 /*Get number of tile parts*/
11360 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11362 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11363 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11364 l_current_nb_bytes_written = 0;
11365 l_part_tile_size = 0;
11366 l_begin_data = p_data;
11368 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,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;
11375 l_part_tile_size += l_nb_bytes_written;
11377 l_current_nb_bytes_written = 0;
11378 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11382 p_data += l_current_nb_bytes_written;
11383 l_nb_bytes_written += l_current_nb_bytes_written;
11384 p_total_data_size -= l_current_nb_bytes_written;
11385 l_part_tile_size += l_nb_bytes_written;
11387 /* Writing Psot in SOT marker */
11388 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11390 if (l_cp->m_specific_param.m_enc.m_cinema) {
11391 j2k_update_tlm(p_j2k,l_part_tile_size);
11394 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11397 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11398 l_tcd->cur_pino = pino;
11400 /*Get number of tile parts*/
11401 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11402 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11403 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11404 l_current_nb_bytes_written = 0;
11405 l_part_tile_size = 0;
11406 l_begin_data = p_data;
11408 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11412 l_nb_bytes_written += l_current_nb_bytes_written;
11413 p_data += l_current_nb_bytes_written;
11414 p_total_data_size -= l_current_nb_bytes_written;
11415 l_part_tile_size += l_current_nb_bytes_written;
11417 l_current_nb_bytes_written = 0;
11419 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11423 l_nb_bytes_written += l_current_nb_bytes_written;
11424 p_data += l_current_nb_bytes_written;
11425 p_total_data_size -= l_current_nb_bytes_written;
11426 l_part_tile_size += l_current_nb_bytes_written;
11428 /* Writing Psot in SOT marker */
11429 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11431 if (l_cp->m_specific_param.m_enc.m_cinema) {
11432 j2k_update_tlm(p_j2k,l_part_tile_size);
11435 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11439 *p_data_written = l_nb_bytes_written;
11445 * Writes the updated tlm.
11447 * @param p_stream the stream to write data to.
11448 * @param p_j2k J2K codec.
11449 * @param p_manager the user event manager.
11451 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11452 struct opj_stream_private *p_stream,
11453 struct opj_event_mgr * p_manager )
11455 OPJ_UINT32 l_tlm_size;
11456 OPJ_SIZE_T l_tlm_position, l_current_position;
11458 /* preconditions */
11459 assert(p_j2k != 00);
11460 assert(p_manager != 00);
11461 assert(p_stream != 00);
11463 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11464 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11465 l_current_position = opj_stream_tell(p_stream);
11467 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11471 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) {
11475 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11484 * Ends the encoding, i.e. frees memory.
11486 * @param p_stream the stream to write data to.
11487 * @param p_j2k J2K codec.
11488 * @param p_manager the user event manager.
11490 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11491 struct opj_stream_private *p_stream,
11492 struct opj_event_mgr * p_manager )
11494 /* preconditions */
11495 assert(p_j2k != 00);
11496 assert(p_manager != 00);
11497 assert(p_stream != 00);
11499 tcd_destroy_v2(p_j2k->m_tcd);
11502 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11503 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11504 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11505 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11508 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11509 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11510 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11513 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11519 * Destroys the memory associated with the decoding of headers.
11521 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11522 opj_stream_private_t *p_stream,
11523 opj_event_mgr_t * p_manager )
11525 /* preconditions */
11526 assert(p_j2k != 00);
11527 assert(p_stream != 00);
11528 assert(p_manager != 00);
11530 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11531 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11532 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11535 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11544 * @param p_stream the stream to write data to.
11545 * @param p_j2k J2K codec.
11546 * @param p_manager the user event manager.
11548 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11549 struct opj_stream_private *p_stream,
11550 struct opj_event_mgr * p_manager )
11552 opj_codestream_info_t * l_cstr_info = 00;
11554 /* preconditions */
11555 assert(p_j2k != 00);
11556 assert(p_manager != 00);
11557 assert(p_stream != 00);
11560 /* TODO mergeV2: check this part which use cstr_info */
11561 /*l_cstr_info = p_j2k->cstr_info;
11565 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));
11567 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11568 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11570 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11572 l_cstr_info->tw = p_j2k->m_cp.tw;
11573 l_cstr_info->th = p_j2k->m_cp.th;
11575 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11576 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11577 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11578 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11580 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11582 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11584 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11586 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11587 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11590 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11592 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11594 /*l_cstr_info->maxmarknum = 100;
11595 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11596 l_cstr_info->marknum = 0;
11599 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);
11603 * Creates a tile-coder decoder.
11605 * @param p_stream the stream to write data to.
11606 * @param p_j2k J2K codec.
11607 * @param p_manager the user event manager.
11609 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11610 struct opj_stream_private *p_stream,
11611 struct opj_event_mgr * p_manager )
11613 /* preconditions */
11614 assert(p_j2k != 00);
11615 assert(p_manager != 00);
11616 assert(p_stream != 00);
11618 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11620 if (! p_j2k->m_tcd) {
11621 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11625 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11626 tcd_destroy_v2(p_j2k->m_tcd);
11637 * @param p_j2k the jpeg2000 codec.
11638 * @param p_stream the stream to write data to.
11639 * @param p_manager the user event manager.
11641 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11642 OPJ_UINT32 p_tile_index,
11644 OPJ_UINT32 p_data_size,
11645 opj_stream_private_t *p_stream,
11646 opj_event_mgr_t * p_manager )
11648 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11649 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11653 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11654 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);