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 Read the COM marker (comment)
458 @param j2k J2K handle
460 static void j2k_read_com(opj_j2k_t *j2k);
462 * Reads a COM marker (comments)
463 * @param p_header_data the data contained in the COM box.
464 * @param jp2 the jpeg2000 file codec.
465 * @param p_header_size the size of the data contained in the COM marker.
466 * @param p_manager the user event manager.
468 static opj_bool j2k_read_com_v2 (
470 OPJ_BYTE * p_header_data,
471 OPJ_UINT32 p_header_size,
472 struct opj_event_mgr * p_manager
475 Write the value concerning the specified component in the marker COD and COC
476 @param j2k J2K handle
477 @param compno Number of the component concerned by the information written
479 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
481 Read the value concerning the specified component in the marker COD and COC
482 @param j2k J2K handle
483 @param compno Number of the component concerned by the information read
485 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
487 Write the COD marker (coding style default)
488 @param j2k J2K handle
490 static void j2k_write_cod(opj_j2k_t *j2k);
493 * Writes the COD marker (Coding style default)
495 * @param p_stream the stream to write data to.
496 * @param p_j2k J2K codec.
497 * @param p_manager the user event manager.
499 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
500 struct opj_stream_private *p_stream,
501 struct opj_event_mgr * p_manager );
504 * Reads a COD marker (Coding Styke defaults)
505 * @param p_header_data the data contained in the COD box.
506 * @param p_j2k the jpeg2000 codec.
507 * @param p_header_size the size of the data contained in the COD marker.
508 * @param p_manager the user event manager.
510 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
511 OPJ_BYTE * p_header_data,
512 OPJ_UINT32 p_header_size,
513 opj_event_mgr_t * p_manager);
516 Write the COC marker (coding style component)
517 @param j2k J2K handle
518 @param compno Number of the component concerned by the information written
520 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
523 * Writes the COC marker (Coding style component)
525 * @param p_comp_number the index of the component to output.
526 * @param p_stream the stream to write data to.
527 * @param p_j2k J2K codec.
528 * @param p_manager the user event manager.
530 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
531 OPJ_UINT32 p_comp_number,
532 struct opj_stream_private *p_stream,
533 struct opj_event_mgr * p_manager
536 * Writes the COC marker (Coding style component)
538 * @param p_comp_no the index of the component to output.
539 * @param p_stream the stream to write data to.
540 * @param p_j2k J2K codec.
541 * @param p_manager the user event manager.
543 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
544 OPJ_UINT32 p_comp_no,
546 OPJ_UINT32 * p_data_written,
547 struct opj_event_mgr * p_manager );
550 * Gets the maximum size taken by a coc.
552 * @param p_j2k the jpeg2000 codec to use.
554 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
558 * Reads a COC marker (Coding Style Component)
559 * @param p_header_data the data contained in the COC box.
560 * @param p_j2k the jpeg2000 codec.
561 * @param p_header_size the size of the data contained in the COC marker.
562 * @param p_manager the user event manager.
564 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
565 OPJ_BYTE * p_header_data,
566 OPJ_UINT32 p_header_size,
567 opj_event_mgr_t * p_manager );
570 Write the value concerning the specified component in the marker QCD and QCC
571 @param j2k J2K handle
572 @param compno Number of the component concerned by the information written
574 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
577 Write the QCD marker (quantization default)
578 @param j2k J2K handle
580 static void j2k_write_qcd(opj_j2k_t *j2k);
583 * Writes the QCD marker (quantization default)
585 * @param p_comp_number the index of the component to output.
586 * @param p_stream the stream to write data to.
587 * @param p_j2k J2K codec.
588 * @param p_manager the user event manager.
590 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
591 struct opj_stream_private *p_stream,
592 struct opj_event_mgr * p_manager );
595 * Reads a QCD marker (Quantization defaults)
596 * @param p_header_data the data contained in the QCD box.
597 * @param p_j2k the jpeg2000 codec.
598 * @param p_header_size the size of the data contained in the QCD marker.
599 * @param p_manager the user event manager.
601 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
602 OPJ_BYTE * p_header_data,
603 OPJ_UINT32 p_header_size,
604 opj_event_mgr_t * p_manager );
607 Write the QCC marker (quantization component)
608 @param j2k J2K handle
609 @param compno Number of the component concerned by the information written
611 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
614 * Writes the QCC marker (quantization component)
616 * @param p_comp_no the index of the component to output.
617 * @param p_stream the stream to write data to.
618 * @param p_j2k J2K codec.
619 * @param p_manager the user event manager.
621 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
622 OPJ_UINT32 p_comp_no,
623 struct opj_stream_private *p_stream,
624 struct opj_event_mgr * p_manager );
627 * Writes the QCC marker (quantization component)
629 * @param p_comp_no the index of the component to output.
630 * @param p_stream the stream to write data to.
631 * @param p_j2k J2K codec.
632 * @param p_manager the user event manager.
634 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
635 OPJ_UINT32 p_comp_no,
637 OPJ_UINT32 * p_data_written,
638 struct opj_event_mgr * p_manager );
641 * Gets the maximum size taken by a qcc.
643 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
646 * Reads a QCC marker (Quantization component)
647 * @param p_header_data the data contained in the QCC box.
648 * @param p_j2k the jpeg2000 codec.
649 * @param p_header_size the size of the data contained in the QCC marker.
650 * @param p_manager the user event manager.
652 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
653 OPJ_BYTE * p_header_data,
654 OPJ_UINT32 p_header_size,
655 opj_event_mgr_t * p_manager);
658 Write the POC marker (progression order change)
659 @param j2k J2K handle
661 static void j2k_write_poc(opj_j2k_t *j2k);
664 * Writes the POC marker (Progression Order Change)
666 * @param p_stream the stream to write data to.
667 * @param p_j2k J2K codec.
668 * @param p_manager the user event manager.
670 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
671 struct opj_stream_private *p_stream,
672 struct opj_event_mgr * p_manager );
675 * Writes the POC marker (Progression Order Change)
677 * @param p_stream the stream to write data to.
678 * @param p_j2k J2K codec.
679 * @param p_manager the user event manager.
681 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
683 OPJ_UINT32 * p_data_written,
684 struct opj_event_mgr * p_manager );
687 * Gets the maximum size taken by the writting of a POC.
689 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
692 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
694 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
697 * Gets the maximum size taken by the headers of the SOT.
699 * @param p_j2k the jpeg2000 codec to use.
701 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
704 * Reads a POC marker (Progression Order Change)
706 * @param p_header_data the data contained in the POC box.
707 * @param p_j2k the jpeg2000 codec.
708 * @param p_header_size the size of the data contained in the POC marker.
709 * @param p_manager the user event manager.
711 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
712 OPJ_BYTE * p_header_data,
713 OPJ_UINT32 p_header_size,
714 opj_event_mgr_t * p_manager );
717 Read the CRG marker (component registration)
718 @param j2k J2K handle
720 static void j2k_read_crg(opj_j2k_t *j2k);
722 * Reads a CRG marker (Component registration)
724 * @param p_header_data the data contained in the TLM box.
725 * @param p_j2k the jpeg2000 codec.
726 * @param p_header_size the size of the data contained in the TLM marker.
727 * @param p_manager the user event manager.
729 static opj_bool j2k_read_crg_v2 (
731 OPJ_BYTE * p_header_data,
732 OPJ_UINT32 p_header_size,
733 struct opj_event_mgr * p_manager
736 * Reads a TLM marker (Tile Length Marker)
738 * @param p_header_data the data contained in the TLM box.
739 * @param p_j2k the jpeg2000 codec.
740 * @param p_header_size the size of the data contained in the TLM marker.
741 * @param p_manager the user event manager.
743 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
744 OPJ_BYTE * p_header_data,
745 OPJ_UINT32 p_header_size,
746 opj_event_mgr_t * p_manager);
749 * Writes the updated tlm.
751 * @param p_stream the stream to write data to.
752 * @param p_j2k J2K codec.
753 * @param p_manager the user event manager.
755 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
756 struct opj_stream_private *p_stream,
757 struct opj_event_mgr * p_manager );
760 * Reads a PLM marker (Packet length, main header marker)
762 * @param p_header_data the data contained in the TLM box.
763 * @param p_j2k the jpeg2000 codec.
764 * @param p_header_size the size of the data contained in the TLM marker.
765 * @param p_manager the user event manager.
767 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
768 OPJ_BYTE * p_header_data,
769 OPJ_UINT32 p_header_size,
770 opj_event_mgr_t * p_manager);
772 * Reads a PLT marker (Packet length, tile-part header)
774 * @param p_header_data the data contained in the PLT box.
775 * @param p_j2k the jpeg2000 codec.
776 * @param p_header_size the size of the data contained in the PLT marker.
777 * @param p_manager the user event manager.
779 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
780 OPJ_BYTE * p_header_data,
781 OPJ_UINT32 p_header_size,
782 opj_event_mgr_t * p_manager );
786 * Reads a PPM marker (Packed packet headers, main header)
788 * @param p_header_data the data contained in the POC box.
789 * @param p_j2k the jpeg2000 codec.
790 * @param p_header_size the size of the data contained in the POC marker.
791 * @param p_manager the user event manager.
794 static opj_bool j2k_read_ppm_v2 (
796 OPJ_BYTE * p_header_data,
797 OPJ_UINT32 p_header_size,
798 struct opj_event_mgr * p_manager
802 static opj_bool j2k_read_ppm_v3 (
804 OPJ_BYTE * p_header_data,
805 OPJ_UINT32 p_header_size,
806 struct opj_event_mgr * p_manager
810 Read the PPT marker (packet packet headers, tile-part header)
811 @param j2k J2K handle
813 static void j2k_read_ppt(opj_j2k_t *j2k);
815 * Reads a PPT marker (Packed packet headers, tile-part header)
817 * @param p_header_data the data contained in the PPT box.
818 * @param p_j2k the jpeg2000 codec.
819 * @param p_header_size the size of the data contained in the PPT marker.
820 * @param p_manager the user event manager.
822 static opj_bool j2k_read_ppt_v2 (
824 OPJ_BYTE * p_header_data,
825 OPJ_UINT32 p_header_size,
826 struct opj_event_mgr * p_manager
829 Write the TLM marker (Mainheader)
830 @param j2k J2K handle
832 static void j2k_write_tlm(opj_j2k_t *j2k);
835 * Writes the TLM marker (Tile Length Marker)
837 * @param p_stream the stream to write data to.
838 * @param p_j2k J2K codec.
839 * @param p_manager the user event manager.
841 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
842 struct opj_stream_private *p_stream,
843 struct opj_event_mgr * p_manager );
846 Write the SOT marker (start of tile-part)
847 @param j2k J2K handle
849 static void j2k_write_sot(opj_j2k_t *j2k);
852 * Writes the SOT marker (Start of tile-part)
854 * @param p_stream the stream to write data to.
855 * @param p_j2k J2K codec.
856 * @param p_manager the user event manager.
858 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
860 OPJ_UINT32 * p_data_written,
861 const struct opj_stream_private *p_stream,
862 struct opj_event_mgr * p_manager );
865 * Reads a PPT marker (Packed packet headers, tile-part header)
867 * @param p_header_data the data contained in the PPT box.
868 * @param p_j2k the jpeg2000 codec.
869 * @param p_header_size the size of the data contained in the PPT marker.
870 * @param p_manager the user event manager.
872 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
873 OPJ_BYTE * p_header_data,
874 OPJ_UINT32 p_header_size,
875 opj_event_mgr_t * p_manager );
878 Write the SOD marker (start of data)
879 @param j2k J2K handle
880 @param tile_coder Pointer to a TCD handle
882 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
885 * Writes the SOD marker (Start of data)
887 * @param p_stream the stream to write data to.
888 * @param p_j2k J2K codec.
889 * @param p_manager the user event manager.
891 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
892 struct opj_tcd_v2 * p_tile_coder,
894 OPJ_UINT32 * p_data_written,
895 OPJ_UINT32 p_total_data_size,
896 const struct opj_stream_private *p_stream,
897 struct opj_event_mgr * p_manager );
900 * Reads a SOD marker (Start Of Data)
902 * @param p_header_data the data contained in the SOD box.
903 * @param p_j2k the jpeg2000 codec.
904 * @param p_header_size the size of the data contained in the SOD marker.
905 * @param p_manager the user event manager.
907 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
908 opj_stream_private_t *p_stream,
909 opj_event_mgr_t * p_manager );
912 * Updates the Tile Length Marker.
914 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
916 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
917 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
919 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
920 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
924 Write the RGN marker (region-of-interest)
925 @param j2k J2K handle
926 @param compno Number of the component concerned by the information written
927 @param tileno Number of the tile concerned by the information written
929 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
932 * Writes the RGN marker (Region Of Interest)
934 * @param p_tile_no the tile to output
935 * @param p_comp_no the component to output
936 * @param p_stream the stream to write data to.
937 * @param p_j2k J2K codec.
938 * @param p_manager the user event manager.
940 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
941 OPJ_UINT32 p_tile_no,
942 OPJ_UINT32 p_comp_no,
943 struct opj_stream_private *p_stream,
944 struct opj_event_mgr * p_manager );
947 * Reads a RGN marker (Region Of Interest)
949 * @param p_header_data the data contained in the POC box.
950 * @param p_j2k the jpeg2000 codec.
951 * @param p_header_size the size of the data contained in the POC marker.
952 * @param p_manager the user event manager.
954 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
955 OPJ_BYTE * p_header_data,
956 OPJ_UINT32 p_header_size,
957 opj_event_mgr_t * p_manager );
960 * Writes the EOC marker (End of Codestream)
962 * @param p_stream the stream to write data to.
963 * @param p_j2k J2K codec.
964 * @param p_manager the user event manager.
966 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
967 struct opj_stream_private *p_stream,
968 struct opj_event_mgr * p_manager );
971 Write the EOC marker (end of codestream)
972 @param j2k J2K handle
974 static void j2k_write_eoc(opj_j2k_t *j2k);
976 Read the EOC marker (end of codestream)
977 @param j2k J2K handle
979 static void j2k_read_eoc(opj_j2k_t *j2k);
982 * Reads a EOC marker (End Of Codestream)
984 * @param p_header_data the data contained in the SOD box.
985 * @param p_j2k the jpeg2000 codec.
986 * @param p_header_size the size of the data contained in the SOD marker.
987 * @param p_manager the user event manager.
990 static opj_bool j2k_read_eoc_v2 (
992 struct opj_stream_private *p_stream,
993 struct opj_event_mgr * p_manager
1000 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1002 * @param p_stream the stream to write data to.
1003 * @param p_j2k J2K codec.
1004 * @param p_manager the user event manager.
1006 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1007 struct opj_stream_private *p_stream,
1008 struct opj_event_mgr * p_manager );
1013 * @param p_stream the stream to write data to.
1014 * @param p_j2k J2K codec.
1015 * @param p_manager the user event manager.
1017 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1018 struct opj_stream_private *p_stream,
1019 struct opj_event_mgr * p_manager );
1022 Read an unknown marker
1023 @param j2k J2K handle
1025 static void j2k_read_unk(opj_j2k_t *j2k);
1027 Add main header marker information
1028 @param cstr_info Codestream information structure
1029 @param type marker type
1030 @param pos byte offset of marker segment
1031 @param len length of marker segment
1033 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1035 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1037 Add tile header marker information
1038 @param tileno tile index number
1039 @param cstr_info Codestream information structure
1040 @param type marker type
1041 @param pos byte offset of marker segment
1042 @param len length of marker segment
1044 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1046 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);
1049 * Reads an unknown marker
1051 * @param p_stream the stream object to read from.
1052 * @param p_j2k the jpeg2000 codec.
1053 * @param p_manager the user event manager.
1055 * @return true if the marker could be deduced.
1057 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1058 struct opj_stream_private *p_stream,
1059 OPJ_UINT32 *output_marker,
1060 struct opj_event_mgr * p_manager );
1063 * Writes the MCT marker (Multiple Component Transform)
1065 * @param p_stream the stream to write data to.
1066 * @param p_j2k J2K codec.
1067 * @param p_manager the user event manager.
1069 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1070 opj_mct_data_t * p_mct_record,
1071 struct opj_stream_private *p_stream,
1072 struct opj_event_mgr * p_manager );
1075 * Reads a MCT marker (Multiple Component Transform)
1077 * @param p_header_data the data contained in the MCT box.
1078 * @param p_j2k the jpeg2000 codec.
1079 * @param p_header_size the size of the data contained in the MCT marker.
1080 * @param p_manager the user event manager.
1082 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1083 OPJ_BYTE * p_header_data,
1084 OPJ_UINT32 p_header_size,
1085 struct opj_event_mgr * p_manager );
1088 * Writes the MCC marker (Multiple Component Collection)
1090 * @param p_stream the stream to write data to.
1091 * @param p_j2k J2K codec.
1092 * @param p_manager the user event manager.
1094 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1095 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1096 struct opj_stream_private *p_stream,
1097 struct opj_event_mgr * p_manager );
1101 * Reads a MCC marker (Multiple Component Collection)
1103 * @param p_header_data the data contained in the MCC box.
1104 * @param p_j2k the jpeg2000 codec.
1105 * @param p_header_size the size of the data contained in the MCC marker.
1106 * @param p_manager the user event manager.
1108 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1109 OPJ_BYTE * p_header_data,
1110 OPJ_UINT32 p_header_size,
1111 struct opj_event_mgr * p_manager );
1114 * Writes the MCO marker (Multiple component transformation ordering)
1116 * @param p_stream the stream to write data to.
1117 * @param p_j2k J2K codec.
1118 * @param p_manager the user event manager.
1120 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1121 struct opj_stream_private *p_stream,
1122 struct opj_event_mgr * p_manager );
1125 * Reads a MCO marker (Multiple Component Transform Ordering)
1127 * @param p_header_data the data contained in the MCO box.
1128 * @param p_j2k the jpeg2000 codec.
1129 * @param p_header_size the size of the data contained in the MCO marker.
1130 * @param p_manager the user event manager.
1132 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1133 OPJ_BYTE * p_header_data,
1134 OPJ_UINT32 p_header_size,
1135 struct opj_event_mgr * p_manager );
1137 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1139 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1140 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1141 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1142 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1144 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1145 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1146 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1147 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1149 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1150 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1151 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1152 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1156 * Ends the encoding, i.e. frees memory.
1158 * @param p_stream the stream to write data to.
1159 * @param p_j2k J2K codec.
1160 * @param p_manager the user event manager.
1162 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1163 struct opj_stream_private *p_stream,
1164 struct opj_event_mgr * p_manager );
1167 * Writes the CBD marker (Component bit depth definition)
1169 * @param p_stream the stream to write data to.
1170 * @param p_j2k J2K codec.
1171 * @param p_manager the user event manager.
1173 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1174 struct opj_stream_private *p_stream,
1175 struct opj_event_mgr * p_manager );
1178 * Reads a CBD marker (Component bit depth definition)
1179 * @param p_header_data the data contained in the CBD box.
1180 * @param p_j2k the jpeg2000 codec.
1181 * @param p_header_size the size of the data contained in the CBD marker.
1182 * @param p_manager the user event manager.
1184 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1185 OPJ_BYTE * p_header_data,
1186 OPJ_UINT32 p_header_size,
1187 struct opj_event_mgr * p_manager);
1190 * Writes the image components.
1192 * @param p_stream the stream to write data to.
1193 * @param p_j2k J2K codec.
1194 * @param p_manager the user event manager.
1196 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1197 struct opj_stream_private *p_stream,
1198 struct opj_event_mgr * p_manager );
1201 * Writes regions of interests.
1203 * @param p_stream the stream to write data to.
1204 * @param p_j2k J2K codec.
1205 * @param p_manager the user event manager.
1207 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1208 struct opj_stream_private *p_stream,
1209 struct opj_event_mgr * p_manager );
1214 * @param p_stream the stream to write data to.
1215 * @param p_j2k J2K codec.
1216 * @param p_manager the user event manager.
1218 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1219 struct opj_stream_private *p_stream,
1220 struct opj_event_mgr * p_manager );
1223 * Checks the progression order changes values. Tells of the poc given as input are valid.
1224 * A nice message is outputted at errors.
1226 * @param p_pocs the progression order changes.
1227 * @param p_nb_pocs the number of progression order changes.
1228 * @param p_nb_resolutions the number of resolutions.
1229 * @param numcomps the number of components
1230 * @param numlayers the number of layers.
1232 * @return true if the pocs are valid.
1234 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1235 OPJ_UINT32 p_nb_pocs,
1236 OPJ_UINT32 p_nb_resolutions,
1237 OPJ_UINT32 numcomps,
1238 OPJ_UINT32 numlayers,
1239 opj_event_mgr_t * p_manager);
1242 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1244 * @param cp the coding parameters.
1245 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1246 * @param tileno the given tile.
1248 * @return the number of tile parts.
1250 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1252 /** mem allocation for TLM marker*/
1253 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1256 * Calculates the total number of tile parts needed by the encoder to
1257 * encode such an image. If not enough memory is available, then the function return false.
1259 * @param p_nb_tiles pointer that will hold the number of tile parts.
1260 * @param cp the coding parameters for the image.
1261 * @param image the image to encode.
1262 * @param p_j2k the p_j2k encoder.
1263 * @param p_manager the user event manager.
1265 * @return true if the function was successful, false else.
1267 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1269 OPJ_UINT32 * p_nb_tiles,
1271 opj_event_mgr_t * p_manager);
1273 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1275 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1277 static opj_codestream_index_t* j2k_create_cstr_index(void);
1283 /* ----------------------------------------------------------------------- */
1284 typedef struct j2k_prog_order{
1285 OPJ_PROG_ORDER enum_prog;
1289 j2k_prog_order_t j2k_prog_order_list[] = {
1295 {(OPJ_PROG_ORDER)-1, ""}
1303 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1311 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1313 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1315 j2k_read_int16_to_float,
1316 j2k_read_int32_to_float,
1317 j2k_read_float32_to_float,
1318 j2k_read_float64_to_float
1321 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1323 j2k_read_int16_to_int32,
1324 j2k_read_int32_to_int32,
1325 j2k_read_float32_to_int32,
1326 j2k_read_float64_to_int32
1329 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1331 j2k_write_float_to_int16,
1332 j2k_write_float_to_int32,
1333 j2k_write_float_to_float,
1334 j2k_write_float_to_float64
1337 typedef struct opj_dec_memory_marker_handler
1341 /** value of the state when the marker can appear */
1343 /** action linked to the marker */
1344 opj_bool (*handler) (
1345 opj_j2k_v2_t *p_j2k,
1346 OPJ_BYTE * p_header_data,
1347 OPJ_UINT32 p_header_size,
1348 struct opj_event_mgr * p_manager
1351 opj_dec_memory_marker_handler_t;
1353 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1356 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1357 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1358 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1359 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1360 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1361 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1362 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1363 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1364 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1365 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1366 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1367 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1368 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1370 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1371 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1372 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1373 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1374 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1375 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1377 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1378 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1379 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1380 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1381 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1382 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1383 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1384 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1385 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1386 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1387 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1388 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1389 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1391 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1392 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1393 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1394 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1395 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1396 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1398 #ifdef TODO_MS /* FIXME */
1399 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1400 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1401 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1402 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1404 #endif /* USE_JPWL */
1406 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1407 {J2K_MS_INSEC, 0, j2k_read_insec}
1408 #endif /* USE_JPSEC */
1409 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1414 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1416 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1417 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1421 for (i=0;i<p_nb_elem;++i) {
1422 opj_read_bytes(l_src_data,&l_temp,2);
1424 l_src_data+=sizeof(OPJ_INT16);
1426 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1430 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1432 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1433 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1437 for (i=0;i<p_nb_elem;++i) {
1438 opj_read_bytes(l_src_data,&l_temp,4);
1440 l_src_data+=sizeof(OPJ_INT32);
1442 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1446 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1448 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1449 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1453 for (i=0;i<p_nb_elem;++i) {
1454 opj_read_float(l_src_data,&l_temp);
1456 l_src_data+=sizeof(OPJ_FLOAT32);
1458 *(l_dest_data++) = l_temp;
1462 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1464 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1465 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1469 for (i=0;i<p_nb_elem;++i) {
1470 opj_read_double(l_src_data,&l_temp);
1472 l_src_data+=sizeof(OPJ_FLOAT64);
1474 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1478 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1480 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1481 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1485 for (i=0;i<p_nb_elem;++i) {
1486 opj_read_bytes(l_src_data,&l_temp,2);
1488 l_src_data+=sizeof(OPJ_INT16);
1490 *(l_dest_data++) = (OPJ_INT32) l_temp;
1494 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1496 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1497 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1501 for (i=0;i<p_nb_elem;++i) {
1502 opj_read_bytes(l_src_data,&l_temp,4);
1504 l_src_data+=sizeof(OPJ_INT32);
1506 *(l_dest_data++) = (OPJ_INT32) l_temp;
1510 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1512 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1513 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1517 for (i=0;i<p_nb_elem;++i) {
1518 opj_read_float(l_src_data,&l_temp);
1520 l_src_data+=sizeof(OPJ_FLOAT32);
1522 *(l_dest_data++) = (OPJ_INT32) l_temp;
1526 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1528 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1529 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1533 for (i=0;i<p_nb_elem;++i) {
1534 opj_read_double(l_src_data,&l_temp);
1536 l_src_data+=sizeof(OPJ_FLOAT64);
1538 *(l_dest_data++) = (OPJ_INT32) l_temp;
1542 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1544 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1545 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1549 for (i=0;i<p_nb_elem;++i) {
1550 l_temp = (OPJ_UINT32) *(l_src_data++);
1552 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1554 l_dest_data+=sizeof(OPJ_INT16);
1558 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1560 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1561 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1565 for (i=0;i<p_nb_elem;++i) {
1566 l_temp = (OPJ_UINT32) *(l_src_data++);
1568 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1570 l_dest_data+=sizeof(OPJ_INT32);
1574 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1576 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1577 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1581 for (i=0;i<p_nb_elem;++i) {
1582 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1584 opj_write_float(l_dest_data,l_temp);
1586 l_dest_data+=sizeof(OPJ_FLOAT32);
1590 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1592 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1593 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1597 for (i=0;i<p_nb_elem;++i) {
1598 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1600 opj_write_double(l_dest_data,l_temp);
1602 l_dest_data+=sizeof(OPJ_FLOAT64);
1608 * Converts an enum type progression order to string type.
1610 * @param prg_order the progression order to get.
1612 * @return the string representation of the given progression order.
1614 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1615 j2k_prog_order_t *po;
1616 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1617 if(po->enum_prog == prg_order){
1618 return po->str_prog;
1621 return po->str_prog;
1625 * Checks the progression order changes values. Tells if the poc given as input are valid.
1627 * @param p_pocs the progression order changes.
1628 * @param p_nb_pocs the number of progression order changes.
1629 * @param p_nb_resolutions the number of resolutions.
1630 * @param numcomps the number of components
1631 * @param numlayers the number of layers.
1632 * @param p_manager the user event manager.
1634 * @return true if the pocs are valid.
1636 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1637 OPJ_UINT32 p_nb_pocs,
1638 OPJ_UINT32 p_nb_resolutions,
1639 OPJ_UINT32 p_num_comps,
1640 OPJ_UINT32 p_num_layers,
1641 opj_event_mgr_t * p_manager)
1643 OPJ_UINT32* packet_array;
1644 OPJ_UINT32 index , resno, compno, layno;
1646 OPJ_UINT32 step_c = 1;
1647 OPJ_UINT32 step_r = p_num_comps * step_c;
1648 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1649 opj_bool loss = OPJ_FALSE;
1650 OPJ_UINT32 layno0 = 0;
1652 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1653 if (packet_array == 00) {
1654 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1657 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1659 if (p_nb_pocs == 0) {
1663 index = step_r * p_pocs->resno0;
1664 // take each resolution for each poc
1665 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1667 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1669 // take each comp of each resolution for each poc
1670 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1671 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1673 // and finally take each layer of each res of ...
1674 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1675 //index = step_r * resno + step_c * compno + step_l * layno;
1676 packet_array[comp_index] = 1;
1677 comp_index += step_l;
1680 res_index += step_c;
1687 // iterate through all the pocs
1688 for (i = 1; i < p_nb_pocs ; ++i) {
1689 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1691 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1692 index = step_r * p_pocs->resno0;
1694 // take each resolution for each poc
1695 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1696 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1698 // take each comp of each resolution for each poc
1699 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1700 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1702 // and finally take each layer of each res of ...
1703 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1704 //index = step_r * resno + step_c * compno + step_l * layno;
1705 packet_array[comp_index] = 1;
1706 comp_index += step_l;
1709 res_index += step_c;
1719 for (layno = 0; layno < p_num_layers ; ++layno) {
1720 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1721 for (compno = 0; compno < p_num_comps; ++compno) {
1722 loss |= (packet_array[index]!=1);
1723 //index = step_r * resno + step_c * compno + step_l * layno;
1730 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1733 opj_free(packet_array);
1738 /* ----------------------------------------------------------------------- */
1739 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1742 int tpnum=1,tpend=0;
1743 opj_tcp_t *tcp = &cp->tcps[tileno];
1744 prog = j2k_convert_progression_order(tcp->prg);
1749 if( cp->tp_flag == prog[i] ){
1750 tpend=1;cp->tp_pos=i;
1754 tpnum= tpnum * tcp->pocs[pino].compE;
1757 tpnum= tpnum * tcp->pocs[pino].resE;
1760 tpnum= tpnum * tcp->pocs[pino].prcE;
1763 tpnum= tpnum * tcp->pocs[pino].layE;
1775 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1777 * @param cp the coding parameters.
1778 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1779 * @param tileno the given tile.
1781 * @return the number of tile parts.
1783 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1785 const OPJ_CHAR *prog = 00;
1787 OPJ_UINT32 tpnum = 1;
1788 opj_tcp_v2_t *tcp = 00;
1789 opj_poc_t * l_current_poc = 00;
1792 assert(tileno < (cp->tw * cp->th));
1793 assert(pino < (cp->tcps[tileno].numpocs + 1));
1795 /* get the given tile coding parameter */
1796 tcp = &cp->tcps[tileno];
1799 l_current_poc = &(tcp->pocs[pino]);
1800 assert(l_current_poc != 0);
1802 /* get the progression order as a character string */
1803 prog = j2k_convert_progression_order(tcp->prg);
1804 assert(strlen(prog) > 0);
1806 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1810 /* component wise */
1812 tpnum *= l_current_poc->compE;
1814 /* resolution wise */
1816 tpnum *= l_current_poc->resE;
1820 tpnum *= l_current_poc->prcE;
1824 tpnum *= l_current_poc->layE;
1827 /* whould we split here ? */
1828 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1829 cp->m_specific_param.m_enc.m_tp_pos=i;
1841 /** mem allocation for TLM marker*/
1842 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1843 int pino,tileno,totnum_tp=0;
1845 OPJ_ARG_NOT_USED(img_numcomp);
1847 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1848 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1849 int cur_totnum_tp = 0;
1850 opj_tcp_t *tcp = &cp->tcps[tileno];
1851 for(pino = 0; pino <= tcp->numpocs; pino++) {
1853 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1854 if(!pi) { return -1;}
1855 tp_num = j2k_get_num_tp(cp,pino,tileno);
1856 totnum_tp = totnum_tp + tp_num;
1857 cur_totnum_tp = cur_totnum_tp + tp_num;
1858 pi_destroy(pi, cp, tileno);
1860 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1862 if (j2k->cstr_info) {
1863 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1864 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1872 * Calculates the total number of tile parts needed by the encoder to
1873 * encode such an image. If not enough memory is available, then the function return false.
1875 * @param p_nb_tiles pointer that will hold the number of tile parts.
1876 * @param cp the coding parameters for the image.
1877 * @param image the image to encode.
1878 * @param p_j2k the p_j2k encoder.
1879 * @param p_manager the user event manager.
1881 * @return true if the function was successful, false else.
1883 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1885 OPJ_UINT32 * p_nb_tiles,
1887 opj_event_mgr_t * p_manager)
1889 OPJ_UINT32 pino,tileno;
1890 OPJ_UINT32 l_nb_tiles;
1894 assert(p_nb_tiles != 00);
1896 assert(image != 00);
1897 assert(p_j2k != 00);
1898 assert(p_manager != 00);
1900 l_nb_tiles = cp->tw * cp->th;
1905 /* TODO mergeV2: check this part which use cstr_info */
1906 /*if (p_j2k->cstr_info) {
1907 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
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)
1916 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1918 *p_nb_tiles = *p_nb_tiles + tp_num;
1920 cur_totnum_tp += tp_num;
1923 tcp->m_nb_tile_parts = cur_totnum_tp;
1925 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1926 if (l_info_tile_ptr->tp == 00) {
1930 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1932 l_info_tile_ptr->num_tps = cur_totnum_tp;
1939 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1940 OPJ_UINT32 cur_totnum_tp = 0;
1942 pi_update_encoding_parameters(image,cp,tileno);
1944 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1945 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1947 *p_nb_tiles = *p_nb_tiles + tp_num;
1949 cur_totnum_tp += tp_num;
1951 tcp->m_nb_tile_parts = cur_totnum_tp;
1960 static void j2k_write_soc(opj_j2k_t *j2k) {
1961 opj_cio_t *cio = j2k->cio;
1962 cio_write(cio, J2K_MS_SOC, 2);
1965 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1970 /* update markers struct */
1971 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1972 #endif /* USE_JPWL */
1977 * Writes the SOC marker (Start Of Codestream)
1979 * @param p_stream the stream to write data to.
1980 * @param p_j2k J2K codec.
1981 * @param p_manager the user event manager.
1983 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1984 struct opj_stream_private *p_stream,
1985 struct opj_event_mgr * p_manager )
1987 /* 2 bytes will be written */
1988 OPJ_BYTE * l_start_stream = 00;
1991 assert(p_stream != 00);
1992 assert(p_j2k != 00);
1993 assert(p_manager != 00);
1995 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1997 /* write SOC identifier */
1998 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2000 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2006 /* update markers struct */
2008 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2010 assert( 0 && "TODO" );
2011 #endif /* USE_JPWL */
2020 * Reads a SOC marker (Start of Codestream)
2021 * @param p_header_data the data contained in the SOC box.
2022 * @param jp2 the jpeg2000 file codec.
2023 * @param p_header_size the size of the data contained in the SOC marker.
2024 * @param p_manager the user event manager.
2026 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2027 struct opj_stream_private *p_stream,
2028 struct opj_event_mgr * p_manager )
2030 OPJ_BYTE l_data [2];
2031 OPJ_UINT32 l_marker;
2034 assert(p_j2k != 00);
2035 assert(p_manager != 00);
2036 assert(p_stream != 00);
2038 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2042 opj_read_bytes(l_data,&l_marker,2);
2043 if (l_marker != J2K_MS_SOC) {
2047 /* Next marker should be a SIZ marker in the main header */
2048 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2050 /* FIXME move it in a index structure included in p_j2k*/
2051 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2053 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2055 /* Add the marker to the codestream index*/
2056 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2061 static void j2k_write_siz(opj_j2k_t *j2k) {
2065 opj_cio_t *cio = j2k->cio;
2066 opj_image_t *image = j2k->image;
2067 opj_cp_t *cp = j2k->cp;
2069 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2070 lenp = cio_tell(cio);
2072 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2073 cio_write(cio, image->x1, 4); /* Xsiz */
2074 cio_write(cio, image->y1, 4); /* Ysiz */
2075 cio_write(cio, image->x0, 4); /* X0siz */
2076 cio_write(cio, image->y0, 4); /* Y0siz */
2077 cio_write(cio, cp->tdx, 4); /* XTsiz */
2078 cio_write(cio, cp->tdy, 4); /* YTsiz */
2079 cio_write(cio, cp->tx0, 4); /* XT0siz */
2080 cio_write(cio, cp->ty0, 4); /* YT0siz */
2081 cio_write(cio, image->numcomps, 2); /* Csiz */
2082 for (i = 0; i < image->numcomps; i++) {
2083 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2084 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2085 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2087 len = cio_tell(cio) - lenp;
2088 cio_seek(cio, lenp);
2089 cio_write(cio, len, 2); /* Lsiz */
2090 cio_seek(cio, lenp + len);
2093 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2097 * Writes the SIZ marker (image and tile size)
2099 * @param p_stream the stream to write data to.
2100 * @param p_j2k J2K codec.
2101 * @param p_manager the user event manager.
2103 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2104 struct opj_stream_private *p_stream,
2105 struct opj_event_mgr * p_manager )
2108 OPJ_UINT32 l_size_len;
2109 OPJ_BYTE * l_current_ptr;
2110 opj_image_t * l_image = 00;
2111 opj_cp_v2_t *cp = 00;
2112 opj_image_comp_t * l_img_comp = 00;
2115 assert(p_stream != 00);
2116 assert(p_j2k != 00);
2117 assert(p_manager != 00);
2119 l_image = p_j2k->m_private_image;
2120 cp = &(p_j2k->m_cp);
2121 l_size_len = 40 + 3 * l_image->numcomps;
2122 l_img_comp = l_image->comps;
2124 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2126 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2127 = (OPJ_BYTE*)opj_realloc(
2128 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2130 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2134 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2137 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2139 /* write SOC identifier */
2140 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2143 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2146 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2149 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2152 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2155 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2158 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2161 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2164 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2167 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2170 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2173 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2176 for (i = 0; i < l_image->numcomps; ++i) {
2177 /* TODO here with MCT ? */
2178 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2181 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2184 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2190 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) {
2200 * Reads a SIZ marker (image and tile size)
2201 * @param p_header_data the data contained in the SIZ box.
2202 * @param jp2 the jpeg2000 file codec.
2203 * @param p_header_size the size of the data contained in the SIZ marker.
2204 * @param p_manager the user event manager.
2206 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2207 OPJ_BYTE * p_header_data,
2208 OPJ_UINT32 p_header_size,
2209 opj_event_mgr_t * p_manager
2212 OPJ_UINT32 l_size, i;
2213 OPJ_UINT32 l_nb_comp;
2214 OPJ_UINT32 l_nb_comp_remain;
2215 OPJ_UINT32 l_remaining_size;
2216 OPJ_UINT32 l_nb_tiles;
2218 opj_image_t *l_image = 00;
2219 opj_cp_v2_t *l_cp = 00;
2220 opj_image_comp_t * l_img_comp = 00;
2221 opj_tcp_v2_t * l_current_tile_param = 00;
2224 assert(p_j2k != 00);
2225 assert(p_manager != 00);
2226 assert(p_header_data != 00);
2228 l_image = p_j2k->m_private_image;
2229 l_cp = &(p_j2k->m_cp);
2231 /* minimum size == 39 - 3 (= minimum component parameter) */
2232 if (p_header_size < 36) {
2233 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2237 l_remaining_size = p_header_size - 36;
2238 l_nb_comp = l_remaining_size / 3;
2239 l_nb_comp_remain = l_remaining_size % 3;
2240 if (l_nb_comp_remain != 0){
2241 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2245 l_size = p_header_size + 2; /* Lsiz */
2247 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2249 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2250 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2252 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2254 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2256 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2258 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2260 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2262 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2264 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2266 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2269 l_image->numcomps = (OPJ_UINT16) l_tmp;
2271 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2275 if (l_image->numcomps != l_nb_comp) {
2276 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);
2281 if (l_cp->correct) {
2282 /* if JPWL is on, we check whether TX errors have damaged
2283 too much the SIZ parameters */
2284 if (!(l_image->x1 * l_image->y1)) {
2285 opj_event_msg_v2(p_manager, EVT_ERROR,
2286 "JPWL: bad image size (%d x %d)\n",
2287 l_image->x1, l_image->y1);
2288 if (!JPWL_ASSUME || JPWL_ASSUME) {
2289 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2294 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2295 if (l_image->numcomps != ((len - 38) / 3)) {
2296 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2297 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2298 l_image->numcomps, ((len - 38) / 3));
2300 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2303 */ /* we try to correct */
2304 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2305 if (l_image->numcomps < ((len - 38) / 3)) {
2306 len = 38 + 3 * l_image->numcomps;
2307 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2310 l_image->numcomps = ((len - 38) / 3);
2311 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2317 /* update components number in the jpwl_exp_comps filed */
2318 l_cp->exp_comps = l_image->numcomps;
2320 #endif /* USE_JPWL */
2322 /* Allocate the resulting image components */
2323 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2324 if (l_image->comps == 00){
2325 l_image->numcomps = 0;
2326 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2330 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2331 l_img_comp = l_image->comps;
2333 /* Read the component information */
2334 for (i = 0; i < l_image->numcomps; ++i){
2336 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2338 l_img_comp->prec = (tmp & 0x7f) + 1;
2339 l_img_comp->sgnd = tmp >> 7;
2340 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2342 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2343 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2345 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2348 if (l_cp->correct) {
2349 /* if JPWL is on, we check whether TX errors have damaged
2350 too much the SIZ parameters, again */
2351 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2352 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2353 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2354 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2356 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2359 /* we try to correct */
2360 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2361 if (!l_image->comps[i].dx) {
2362 l_image->comps[i].dx = 1;
2363 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2364 i, l_image->comps[i].dx);
2366 if (!l_image->comps[i].dy) {
2367 l_image->comps[i].dy = 1;
2368 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2369 i, l_image->comps[i].dy);
2373 #endif /* USE_JPWL */
2374 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2375 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2379 /* Compute the number of tiles */
2380 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2381 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2382 l_nb_tiles = l_cp->tw * l_cp->th;
2384 /* Define the tiles which will be decoded */
2385 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2386 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;
2387 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;
2388 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);
2389 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);
2392 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2393 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2394 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2395 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2399 if (l_cp->correct) {
2400 /* if JPWL is on, we check whether TX errors have damaged
2401 too much the SIZ parameters */
2402 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2403 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2404 "JPWL: bad number of tiles (%d x %d)\n",
2405 l_cp->tw, l_cp->th);
2407 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2410 /* we try to correct */
2411 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2414 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2417 if (l_cp->tw > l_cp->max_tiles) {
2419 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2420 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2421 l_cp->max_tiles, l_cp->tw);
2425 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2428 if (l_cp->th > l_cp->max_tiles) {
2430 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2431 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2432 l_cp->max_tiles, l_cp->th);
2436 #endif /* USE_JPWL */
2438 /* memory allocations */
2439 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2440 if (l_cp->tcps == 00) {
2441 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2444 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2447 if (l_cp->correct) {
2449 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2450 "JPWL: could not alloc tcps field of cp\n");
2451 if (!JPWL_ASSUME || JPWL_ASSUME) {
2452 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2457 #endif /* USE_JPWL */
2459 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2460 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2461 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2462 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2465 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2467 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2468 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2470 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2471 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2474 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));
2475 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2477 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2478 (opj_simple_mcc_decorrelation_data_t*)
2479 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2481 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2482 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2485 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));
2486 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2488 /* set up default dc level shift */
2489 for (i=0;i<l_image->numcomps;++i) {
2490 if (! l_image->comps[i].sgnd) {
2491 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2495 l_current_tile_param = l_cp->tcps;
2496 for (i = 0; i < l_nb_tiles; ++i) {
2497 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2498 if (l_current_tile_param->tccps == 00) {
2499 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2502 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2504 ++l_current_tile_param;
2507 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2508 opj_image_comp_header_update(l_image,l_cp);
2515 static void j2k_write_com(opj_j2k_t *j2k) {
2519 if(j2k->cp->comment) {
2520 opj_cio_t *cio = j2k->cio;
2521 char *comment = j2k->cp->comment;
2523 cio_write(cio, J2K_MS_COM, 2);
2524 lenp = cio_tell(cio);
2526 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2527 for (i = 0; i < strlen(comment); i++) {
2528 cio_write(cio, comment[i], 1);
2530 len = cio_tell(cio) - lenp;
2531 cio_seek(cio, lenp);
2532 cio_write(cio, len, 2);
2533 cio_seek(cio, lenp + len);
2537 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2543 * Writes the COM marker (comment)
2545 * @param p_stream the stream to write data to.
2546 * @param p_j2k J2K codec.
2547 * @param p_manager the user event manager.
2549 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2550 struct opj_stream_private *p_stream,
2551 struct opj_event_mgr * p_manager )
2553 OPJ_UINT32 l_comment_size;
2554 OPJ_UINT32 l_total_com_size;
2555 const OPJ_CHAR *l_comment;
2556 OPJ_BYTE * l_current_ptr = 00;
2559 assert(p_j2k != 00);
2560 assert(p_stream != 00);
2561 assert(p_manager != 00);
2563 l_comment = p_j2k->m_cp.comment;
2564 l_comment_size = strlen(l_comment);
2565 l_total_com_size = l_comment_size + 6;
2567 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2568 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2569 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2572 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2576 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2579 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2581 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2584 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2587 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2590 memcpy( l_current_ptr,l_comment,l_comment_size);
2592 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) {
2599 static void j2k_read_com(opj_j2k_t *j2k) {
2602 opj_cio_t *cio = j2k->cio;
2604 len = cio_read(cio, 2);
2605 cio_skip(cio, len - 2);
2608 * Reads a COM marker (comments)
2609 * @param p_header_data the data contained in the COM box.
2610 * @param jp2 the jpeg2000 file codec.
2611 * @param p_header_size the size of the data contained in the COM marker.
2612 * @param p_manager the user event manager.
2614 opj_bool j2k_read_com_v2 (
2615 opj_j2k_v2_t *p_j2k,
2616 OPJ_BYTE * p_header_data,
2617 OPJ_UINT32 p_header_size,
2618 struct opj_event_mgr * p_manager
2622 assert(p_j2k != 00);
2623 assert(p_manager != 00);
2624 assert(p_header_data != 00);
2625 (void)p_header_size;
2630 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2633 opj_cp_t *cp = j2k->cp;
2634 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2635 opj_tccp_t *tccp = &tcp->tccps[compno];
2636 opj_cio_t *cio = j2k->cio;
2638 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2639 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2640 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2641 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2642 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2644 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2645 for (i = 0; i < tccp->numresolutions; i++) {
2646 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2651 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2654 opj_cp_t *cp = j2k->cp;
2655 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2656 opj_tccp_t *tccp = &tcp->tccps[compno];
2657 opj_cio_t *cio = j2k->cio;
2659 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2661 /* If user wants to remove more resolutions than the codestream contains, return error*/
2662 assert(cp->reduce >= 0);
2663 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2664 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2665 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2666 j2k->state |= J2K_STATE_ERR;
2669 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2670 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2671 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2672 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2673 if (tccp->csty & J2K_CP_CSTY_PRT) {
2674 for (i = 0; i < tccp->numresolutions; i++) {
2675 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2676 tccp->prcw[i] = tmp & 0xf;
2677 tccp->prch[i] = tmp >> 4;
2682 if(j2k->cstr_info && compno == 0) {
2683 for (i = 0; i < tccp->numresolutions; i++) {
2684 if (tccp->csty & J2K_CP_CSTY_PRT) {
2685 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2686 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2689 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2690 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2697 static void j2k_write_cod(opj_j2k_t *j2k) {
2698 opj_cp_t *cp = NULL;
2699 opj_tcp_t *tcp = NULL;
2702 opj_cio_t *cio = j2k->cio;
2704 cio_write(cio, J2K_MS_COD, 2); /* COD */
2706 lenp = cio_tell(cio);
2710 tcp = &cp->tcps[j2k->curtileno];
2712 cio_write(cio, tcp->csty, 1); /* Scod */
2713 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2714 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2715 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2717 j2k_write_cox(j2k, 0);
2718 len = cio_tell(cio) - lenp;
2719 cio_seek(cio, lenp);
2720 cio_write(cio, len, 2); /* Lcod */
2721 cio_seek(cio, lenp + len);
2724 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2729 * Writes the COD marker (Coding style default)
2731 * @param p_stream the stream to write data to.
2732 * @param p_j2k J2K codec.
2733 * @param p_manager the user event manager.
2735 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2736 struct opj_stream_private *p_stream,
2737 struct opj_event_mgr * p_manager )
2739 opj_cp_v2_t *l_cp = 00;
2740 opj_tcp_v2_t *l_tcp = 00;
2741 OPJ_UINT32 l_code_size,l_remaining_size;
2742 OPJ_BYTE * l_current_data = 00;
2745 assert(p_j2k != 00);
2746 assert(p_manager != 00);
2747 assert(p_stream != 00);
2749 l_cp = &(p_j2k->m_cp);
2750 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2751 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2752 l_remaining_size = l_code_size;
2754 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2755 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2756 = (OPJ_BYTE*)opj_realloc(
2757 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2760 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2764 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2767 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2769 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2770 l_current_data += 2;
2772 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2773 l_current_data += 2;
2775 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2778 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2781 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2784 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2787 l_remaining_size -= 9;
2789 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2790 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2794 if (l_remaining_size != 0) {
2795 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2799 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) {
2808 * Reads a COD marker (Coding Styke defaults)
2809 * @param p_header_data the data contained in the COD box.
2810 * @param p_j2k the jpeg2000 codec.
2811 * @param p_header_size the size of the data contained in the COD marker.
2812 * @param p_manager the user event manager.
2814 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2815 OPJ_BYTE * p_header_data,
2816 OPJ_UINT32 p_header_size,
2817 opj_event_mgr_t * p_manager
2823 opj_cp_v2_t *l_cp = 00;
2824 opj_tcp_v2_t *l_tcp = 00;
2825 opj_image_t *l_image = 00;
2828 assert(p_header_data != 00);
2829 assert(p_j2k != 00);
2830 assert(p_manager != 00);
2832 l_image = p_j2k->m_private_image;
2833 l_cp = &(p_j2k->m_cp);
2835 /* If we are in the first tile-part header of the current tile */
2836 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2837 &l_cp->tcps[p_j2k->m_current_tile_number] :
2838 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2840 /* Make sure room is sufficient */
2841 if (p_header_size < 5) {
2842 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2846 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2848 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2850 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2851 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2854 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2855 if (l_cp->m_specific_param.m_dec.m_layer) {
2856 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2859 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2862 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2866 for (i = 0; i < l_image->numcomps; ++i) {
2867 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2870 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2871 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2875 if (p_header_size != 0) {
2876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2880 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2881 j2k_copy_tile_component_parameters(p_j2k);
2884 #ifdef WIP_REMOVE_MSD
2885 if (p_j2k->cstr_info) {
2886 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2887 p_j2k->cstr_info->prog = l_tcp->prg;
2888 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2889 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2890 for (i = 0; i < l_image->numcomps; ++i) {
2891 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2899 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2902 opj_cp_t *cp = j2k->cp;
2903 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2904 opj_image_t *image = j2k->image;
2905 opj_cio_t *cio = j2k->cio;
2907 cio_write(cio, J2K_MS_COC, 2); /* COC */
2908 lenp = cio_tell(cio);
2910 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2911 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2912 j2k_write_cox(j2k, compno);
2913 len = cio_tell(cio) - lenp;
2914 cio_seek(cio, lenp);
2915 cio_write(cio, len, 2); /* Lcoc */
2916 cio_seek(cio, lenp + len);
2920 * Writes the COC marker (Coding style component)
2922 * @param p_comp_no the index of the component to output.
2923 * @param p_stream the stream to write data to.
2924 * @param p_j2k J2K codec.
2925 * @param p_manager the user event manager.
2927 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2928 OPJ_UINT32 p_comp_no,
2929 struct opj_stream_private *p_stream,
2930 struct opj_event_mgr * p_manager )
2932 OPJ_UINT32 l_coc_size,l_remaining_size;
2933 OPJ_UINT32 l_comp_room;
2936 assert(p_j2k != 00);
2937 assert(p_manager != 00);
2938 assert(p_stream != 00);
2940 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2942 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2944 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2945 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2946 = (OPJ_BYTE*)opj_realloc(
2947 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2949 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2953 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2956 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);
2958 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) {
2966 * Writes the COC marker (Coding style component)
2968 * @param p_comp_no the index of the component to output.
2969 * @param p_stream the stream to write data to.
2970 * @param p_j2k J2K codec.
2971 * @param p_manager the user event manager.
2973 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2974 OPJ_UINT32 p_comp_no,
2976 OPJ_UINT32 * p_data_written,
2977 struct opj_event_mgr * p_manager )
2979 opj_cp_v2_t *l_cp = 00;
2980 opj_tcp_v2_t *l_tcp = 00;
2981 OPJ_UINT32 l_coc_size,l_remaining_size;
2982 OPJ_BYTE * l_current_data = 00;
2983 opj_image_t *l_image = 00;
2984 OPJ_UINT32 l_comp_room;
2987 assert(p_j2k != 00);
2988 assert(p_manager != 00);
2990 l_cp = &(p_j2k->m_cp);
2991 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2992 l_image = p_j2k->m_private_image;
2993 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2995 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2996 l_remaining_size = l_coc_size;
2998 l_current_data = p_data;
3000 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3001 l_current_data += 2;
3003 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3004 l_current_data += 2;
3006 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3007 l_current_data+=l_comp_room;
3009 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3012 l_remaining_size -= (5 + l_comp_room);
3013 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3014 * p_data_written = l_coc_size;
3018 * Gets the maximum size taken by a coc.
3020 * @param p_j2k the jpeg2000 codec to use.
3022 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3025 OPJ_UINT32 l_nb_comp;
3026 OPJ_UINT32 l_nb_tiles;
3027 OPJ_UINT32 l_max = 0;
3031 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3032 l_nb_comp = p_j2k->m_private_image->numcomps;
3034 for (i=0;i<l_nb_tiles;++i) {
3035 for (j=0;j<l_nb_comp;++j) {
3036 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3045 * Reads a COC marker (Coding Style Component)
3046 * @param p_header_data the data contained in the COC box.
3047 * @param p_j2k the jpeg2000 codec.
3048 * @param p_header_size the size of the data contained in the COC marker.
3049 * @param p_manager the user event manager.
3051 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3052 OPJ_BYTE * p_header_data,
3053 OPJ_UINT32 p_header_size,
3054 opj_event_mgr_t * p_manager
3057 opj_cp_v2_t *l_cp = NULL;
3058 opj_tcp_v2_t *l_tcp = NULL;
3059 opj_image_t *l_image = NULL;
3060 OPJ_UINT32 l_comp_room;
3061 OPJ_UINT32 l_comp_no;
3064 assert(p_header_data != 00);
3065 assert(p_j2k != 00);
3066 assert(p_manager != 00);
3068 l_cp = &(p_j2k->m_cp);
3069 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3070 &l_cp->tcps[p_j2k->m_current_tile_number] :
3071 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3072 l_image = p_j2k->m_private_image;
3074 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3076 /* make sure room is sufficient*/
3077 if (p_header_size < l_comp_room + 1) {
3078 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3081 p_header_size -= l_comp_room + 1;
3083 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3084 p_header_data += l_comp_room;
3085 if (l_comp_no >= l_image->numcomps) {
3086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3090 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3093 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3094 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3098 if (p_header_size != 0) {
3099 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3105 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3106 int bandno, numbands;
3109 opj_cp_t *cp = j2k->cp;
3110 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3111 opj_tccp_t *tccp = &tcp->tccps[compno];
3112 opj_cio_t *cio = j2k->cio;
3114 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3115 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3117 for (bandno = 0; bandno < numbands; bandno++) {
3118 expn = tccp->stepsizes[bandno].expn;
3119 mant = tccp->stepsizes[bandno].mant;
3121 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3122 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3124 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3130 static void j2k_write_qcd(opj_j2k_t *j2k) {
3133 opj_cio_t *cio = j2k->cio;
3135 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3136 lenp = cio_tell(cio);
3138 j2k_write_qcx(j2k, 0);
3139 len = cio_tell(cio) - lenp;
3140 cio_seek(cio, lenp);
3141 cio_write(cio, len, 2); /* Lqcd */
3142 cio_seek(cio, lenp + len);
3145 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3149 * Writes the QCD marker (quantization default)
3151 * @param p_comp_number the index of the component to output.
3152 * @param p_stream the stream to write data to.
3153 * @param p_j2k J2K codec.
3154 * @param p_manager the user event manager.
3156 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3157 struct opj_stream_private *p_stream,
3158 struct opj_event_mgr * p_manager )
3160 opj_cp_v2_t *l_cp = 00;
3161 opj_tcp_v2_t *l_tcp = 00;
3162 OPJ_UINT32 l_qcd_size,l_remaining_size;
3163 OPJ_BYTE * l_current_data = 00;
3166 assert(p_j2k != 00);
3167 assert(p_manager != 00);
3168 assert(p_stream != 00);
3170 l_cp = &(p_j2k->m_cp);
3171 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3172 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3173 l_remaining_size = l_qcd_size;
3175 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3176 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3177 = (OPJ_BYTE*)opj_realloc(
3178 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3181 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3185 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3188 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3190 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3191 l_current_data += 2;
3193 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3194 l_current_data += 2;
3196 l_remaining_size -= 4;
3198 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3199 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3203 if (l_remaining_size != 0) {
3204 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3208 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) {
3216 * Reads a QCD marker (Quantization defaults)
3217 * @param p_header_data the data contained in the QCD box.
3218 * @param p_j2k the jpeg2000 codec.
3219 * @param p_header_size the size of the data contained in the QCD marker.
3220 * @param p_manager the user event manager.
3222 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3223 OPJ_BYTE * p_header_data,
3224 OPJ_UINT32 p_header_size,
3225 opj_event_mgr_t * p_manager
3229 assert(p_header_data != 00);
3230 assert(p_j2k != 00);
3231 assert(p_manager != 00);
3233 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3234 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3238 if (p_header_size != 0) {
3239 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3243 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3244 j2k_copy_tile_quantization_parameters(p_j2k);
3249 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3252 opj_cio_t *cio = j2k->cio;
3254 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3255 lenp = cio_tell(cio);
3257 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3258 j2k_write_qcx(j2k, compno);
3259 len = cio_tell(cio) - lenp;
3260 cio_seek(cio, lenp);
3261 cio_write(cio, len, 2); /* Lqcc */
3262 cio_seek(cio, lenp + len);
3266 * Writes the QCC marker (quantization component)
3268 * @param p_comp_no the index of the component to output.
3269 * @param p_stream the stream to write data to.
3270 * @param p_j2k J2K codec.
3271 * @param p_manager the user event manager.
3273 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3274 OPJ_UINT32 p_comp_no,
3275 struct opj_stream_private *p_stream,
3276 struct opj_event_mgr * p_manager )
3278 OPJ_UINT32 l_qcc_size,l_remaining_size;
3281 assert(p_j2k != 00);
3282 assert(p_manager != 00);
3283 assert(p_stream != 00);
3285 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3286 l_remaining_size = l_qcc_size;
3288 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3289 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3290 = (OPJ_BYTE*)opj_realloc(
3291 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3293 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3297 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3300 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);
3302 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) {
3310 * Writes the QCC marker (quantization component)
3312 * @param p_comp_no the index of the component to output.
3313 * @param p_stream the stream to write data to.
3314 * @param p_j2k J2K codec.
3315 * @param p_manager the user event manager.
3317 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3318 OPJ_UINT32 p_comp_no,
3320 OPJ_UINT32 * p_data_written,
3321 struct opj_event_mgr * p_manager )
3323 OPJ_UINT32 l_qcc_size,l_remaining_size;
3324 OPJ_BYTE * l_current_data = 00;
3327 assert(p_j2k != 00);
3328 assert(p_manager != 00);
3330 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3331 l_remaining_size = l_qcc_size;
3333 l_current_data = p_data;
3335 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3336 l_current_data += 2;
3338 if (p_j2k->m_private_image->numcomps <= 256) {
3341 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3342 l_current_data += 2;
3344 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3347 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3348 l_remaining_size -= 6;
3351 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3352 l_current_data += 2;
3354 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3357 l_remaining_size -= 6;
3360 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3362 *p_data_written = l_qcc_size;
3366 * Gets the maximum size taken by a qcc.
3368 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3370 return j2k_get_max_coc_size(p_j2k);
3374 * Reads a QCC marker (Quantization component)
3375 * @param p_header_data the data contained in the QCC box.
3376 * @param p_j2k the jpeg2000 codec.
3377 * @param p_header_size the size of the data contained in the QCC marker.
3378 * @param p_manager the user event manager.
3380 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3381 OPJ_BYTE * p_header_data,
3382 OPJ_UINT32 p_header_size,
3383 opj_event_mgr_t * p_manager
3386 OPJ_UINT32 l_num_comp,l_comp_no;
3389 assert(p_header_data != 00);
3390 assert(p_j2k != 00);
3391 assert(p_manager != 00);
3393 l_num_comp = p_j2k->m_private_image->numcomps;
3395 if (l_num_comp <= 256) {
3396 if (p_header_size < 1) {
3397 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3400 opj_read_bytes(p_header_data,&l_comp_no,1);
3405 if (p_header_size < 2) {
3406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3409 opj_read_bytes(p_header_data,&l_comp_no,2);
3415 if (p_j2k->m_cp.correct) {
3417 static OPJ_UINT32 backup_compno = 0;
3419 /* compno is negative or larger than the number of components!!! */
3420 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3421 opj_event_msg_v2(p_manager, EVT_ERROR,
3422 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3423 l_comp_no, l_num_comp);
3425 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3428 /* we try to correct */
3429 l_comp_no = backup_compno % l_num_comp;
3430 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3431 "- setting component number to %d\n",
3435 /* keep your private count of tiles */
3438 #endif /* USE_JPWL */
3440 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3441 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3445 if (p_header_size != 0) {
3446 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3454 static void j2k_write_poc(opj_j2k_t *j2k) {
3455 int len, numpchgs, i;
3457 int numcomps = j2k->image->numcomps;
3459 opj_cp_t *cp = j2k->cp;
3460 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3461 opj_tccp_t *tccp = &tcp->tccps[0];
3462 opj_cio_t *cio = j2k->cio;
3464 numpchgs = 1 + tcp->numpocs;
3465 cio_write(cio, J2K_MS_POC, 2); /* POC */
3466 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3467 cio_write(cio, len, 2); /* Lpoc */
3468 for (i = 0; i < numpchgs; i++) {
3469 opj_poc_t *poc = &tcp->pocs[i];
3470 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3471 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3472 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3473 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3474 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3475 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3476 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3477 poc->compno1 = int_min(poc->compno1, numcomps);
3478 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3483 * Writes the POC marker (Progression Order Change)
3485 * @param p_stream the stream to write data to.
3486 * @param p_j2k J2K codec.
3487 * @param p_manager the user event manager.
3489 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3490 struct opj_stream_private *p_stream,
3491 struct opj_event_mgr * p_manager )
3493 OPJ_UINT32 l_nb_comp;
3494 OPJ_UINT32 l_nb_poc;
3495 OPJ_UINT32 l_poc_size;
3496 OPJ_UINT32 l_written_size = 0;
3497 opj_tcp_v2_t *l_tcp = 00;
3498 opj_tccp_t *l_tccp = 00;
3499 OPJ_UINT32 l_poc_room;
3502 assert(p_j2k != 00);
3503 assert(p_manager != 00);
3504 assert(p_stream != 00);
3506 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3507 l_tccp = &l_tcp->tccps[0];
3508 l_nb_comp = p_j2k->m_private_image->numcomps;
3509 l_nb_poc = 1 + l_tcp->numpocs;
3511 if (l_nb_comp <= 256) {
3517 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3519 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3520 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3521 = (OPJ_BYTE*)opj_realloc(
3522 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3525 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3529 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3532 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3534 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) {
3543 * Writes the POC marker (Progression Order Change)
3545 * @param p_stream the stream to write data to.
3546 * @param p_j2k J2K codec.
3547 * @param p_manager the user event manager.
3549 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3551 OPJ_UINT32 * p_data_written,
3552 struct opj_event_mgr * p_manager )
3555 OPJ_BYTE * l_current_data = 00;
3556 OPJ_UINT32 l_nb_comp;
3557 OPJ_UINT32 l_nb_poc;
3558 OPJ_UINT32 l_poc_size;
3559 opj_image_t *l_image = 00;
3560 opj_tcp_v2_t *l_tcp = 00;
3561 opj_tccp_t *l_tccp = 00;
3562 opj_poc_t *l_current_poc = 00;
3563 OPJ_UINT32 l_poc_room;
3566 assert(p_j2k != 00);
3567 assert(p_manager != 00);
3569 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3570 l_tccp = &l_tcp->tccps[0];
3571 l_image = p_j2k->m_private_image;
3572 l_nb_comp = l_image->numcomps;
3573 l_nb_poc = 1 + l_tcp->numpocs;
3575 if (l_nb_comp <= 256) {
3582 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3584 l_current_data = p_data;
3586 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3587 l_current_data += 2;
3589 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3590 l_current_data += 2;
3592 l_current_poc = l_tcp->pocs;
3593 for (i = 0; i < l_nb_poc; ++i) {
3594 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3597 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3598 l_current_data+=l_poc_room;
3600 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3603 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3606 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3607 l_current_data+=l_poc_room;
3609 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3612 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3613 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3614 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3615 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3620 *p_data_written = l_poc_size;
3624 * Gets the maximum size taken by the writing of a POC.
3626 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3628 opj_tcp_v2_t * l_tcp = 00;
3629 OPJ_UINT32 l_nb_tiles = 0;
3630 OPJ_UINT32 l_max_poc = 0;
3633 l_tcp = p_j2k->m_cp.tcps;
3634 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3636 for (i=0;i<l_nb_tiles;++i) {
3637 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3643 return 4 + 9 * l_max_poc;
3647 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3649 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3652 OPJ_UINT32 l_nb_tiles;
3653 OPJ_UINT32 l_max = 0;
3654 opj_tcp_v2_t * l_tcp = 00;
3656 l_tcp = p_j2k->m_cp.tcps;
3657 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3659 for (i=0;i<l_nb_tiles;++i) {
3660 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3670 * Gets the maximum size taken by the headers of the SOT.
3672 * @param p_j2k the jpeg2000 codec to use.
3674 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3676 OPJ_UINT32 l_nb_bytes = 0;
3677 OPJ_UINT32 l_nb_comps;
3678 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3680 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3681 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3683 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3684 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3685 l_nb_bytes += l_nb_comps * l_coc_bytes;
3687 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3688 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3691 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3693 /*** DEVELOPER CORNER, Add room for your headers ***/
3700 * Reads a POC marker (Progression Order Change)
3702 * @param p_header_data the data contained in the POC box.
3703 * @param p_j2k the jpeg2000 codec.
3704 * @param p_header_size the size of the data contained in the POC marker.
3705 * @param p_manager the user event manager.
3707 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3708 OPJ_BYTE * p_header_data,
3709 OPJ_UINT32 p_header_size,
3710 opj_event_mgr_t * p_manager
3713 OPJ_UINT32 i, l_nb_comp, l_tmp;
3714 opj_image_t * l_image = 00;
3715 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3716 OPJ_UINT32 l_chunk_size, l_comp_room;
3718 opj_cp_v2_t *l_cp = 00;
3719 opj_tcp_v2_t *l_tcp = 00;
3720 opj_poc_t *l_current_poc = 00;
3723 assert(p_header_data != 00);
3724 assert(p_j2k != 00);
3725 assert(p_manager != 00);
3727 l_image = p_j2k->m_private_image;
3728 l_nb_comp = l_image->numcomps;
3729 if (l_nb_comp <= 256) {
3735 l_chunk_size = 5 + 2 * l_comp_room;
3736 l_current_poc_nb = p_header_size / l_chunk_size;
3737 l_current_poc_remaining = p_header_size % l_chunk_size;
3739 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3740 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3744 l_cp = &(p_j2k->m_cp);
3745 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3746 &l_cp->tcps[p_j2k->m_current_tile_number] :
3747 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3748 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3749 l_current_poc_nb += l_old_poc_nb;
3751 assert(l_current_poc_nb < 32);
3753 /* now poc is in use.*/
3756 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3757 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3758 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3760 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3761 p_header_data+=l_comp_room;
3762 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3764 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3766 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3767 p_header_data+=l_comp_room;
3768 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3770 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3771 /* make sure comp is in acceptable bounds */
3772 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3776 l_tcp->numpocs = l_current_poc_nb - 1;
3780 static void j2k_read_crg(opj_j2k_t *j2k) {
3781 int len, i, Xcrg_i, Ycrg_i;
3783 opj_cio_t *cio = j2k->cio;
3784 int numcomps = j2k->image->numcomps;
3786 len = cio_read(cio, 2); /* Lcrg */
3787 for (i = 0; i < numcomps; i++) {
3788 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3789 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
3794 * Reads a CRG marker (Component registration)
3796 * @param p_header_data the data contained in the TLM box.
3797 * @param p_j2k the jpeg2000 codec.
3798 * @param p_header_size the size of the data contained in the TLM marker.
3799 * @param p_manager the user event manager.
3801 opj_bool j2k_read_crg_v2 (
3802 opj_j2k_v2_t *p_j2k,
3803 OPJ_BYTE * p_header_data,
3804 OPJ_UINT32 p_header_size,
3805 struct opj_event_mgr * p_manager
3808 OPJ_UINT32 l_nb_comp;
3810 assert(p_header_data != 00);
3811 assert(p_j2k != 00);
3812 assert(p_manager != 00);
3814 l_nb_comp = p_j2k->m_private_image->numcomps;
3816 if (p_header_size != l_nb_comp *4) {
3817 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3820 /* Do not care of this at the moment since only local variables are set here */
3823 (i = 0; i < l_nb_comp; ++i)
3825 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3827 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3835 * Reads a TLM marker (Tile Length Marker)
3837 * @param p_header_data the data contained in the TLM box.
3838 * @param p_j2k the jpeg2000 codec.
3839 * @param p_header_size the size of the data contained in the TLM marker.
3840 * @param p_manager the user event manager.
3842 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3843 OPJ_BYTE * p_header_data,
3844 OPJ_UINT32 p_header_size,
3845 opj_event_mgr_t * p_manager
3848 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3850 assert(p_header_data != 00);
3851 assert(p_j2k != 00);
3852 assert(p_manager != 00);
3854 if (p_header_size < 2) {
3855 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3860 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3862 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3865 l_ST = ((l_Stlm >> 4) & 0x3);
3866 l_SP = (l_Stlm >> 6) & 0x1;
3868 l_Ptlm_size = (l_SP + 1) * 2;
3869 l_quotient = l_Ptlm_size + l_ST;
3871 l_tot_num_tp = p_header_size / l_quotient;
3872 l_tot_num_tp_remaining = p_header_size % l_quotient;
3874 if (l_tot_num_tp_remaining != 0) {
3875 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3878 /* FIXME Do not care of this at the moment since only local variables are set here */
3881 (i = 0; i < l_tot_num_tp; ++i)
3883 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3884 p_header_data += l_ST;
3885 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3886 p_header_data += l_Ptlm_size;
3893 * Reads a PLM marker (Packet length, main header marker)
3895 * @param p_header_data the data contained in the TLM box.
3896 * @param p_j2k the jpeg2000 codec.
3897 * @param p_header_size the size of the data contained in the TLM marker.
3898 * @param p_manager the user event manager.
3900 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3901 OPJ_BYTE * p_header_data,
3902 OPJ_UINT32 p_header_size,
3903 opj_event_mgr_t * p_manager
3907 assert(p_header_data != 00);
3908 assert(p_j2k != 00);
3909 assert(p_manager != 00);
3911 if (p_header_size < 1) {
3912 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3915 /* Do not care of this at the moment since only local variables are set here */
3917 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3924 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3926 p_header_size -= (1+l_Nplm);
3930 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3934 (i = 0; i < l_Nplm; ++i)
3936 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3938 // take only the last seven bytes
3939 l_packet_len |= (l_tmp & 0x7f);
3947 // store packet length and proceed to next packet
3954 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3963 * Reads a PLT marker (Packet length, tile-part header)
3965 * @param p_header_data the data contained in the PLT box.
3966 * @param p_j2k the jpeg2000 codec.
3967 * @param p_header_size the size of the data contained in the PLT marker.
3968 * @param p_manager the user event manager.
3970 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3971 OPJ_BYTE * p_header_data,
3972 OPJ_UINT32 p_header_size,
3973 opj_event_mgr_t * p_manager
3976 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3979 assert(p_header_data != 00);
3980 assert(p_j2k != 00);
3981 assert(p_manager != 00);
3983 if (p_header_size < 1) {
3984 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3988 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3992 for (i = 0; i < p_header_size; ++i) {
3993 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3995 /* take only the last seven bytes */
3996 l_packet_len |= (l_tmp & 0x7f);
4001 /* store packet length and proceed to next packet */
4006 if (l_packet_len != 0) {
4007 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4016 * Reads a PPM marker (Packed packet headers, main header)
4018 * @param p_header_data the data contained in the POC box.
4019 * @param p_j2k the jpeg2000 codec.
4020 * @param p_header_size the size of the data contained in the POC marker.
4021 * @param p_manager the user event manager.
4024 opj_bool j2k_read_ppm_v2 (
4025 opj_j2k_v2_t *p_j2k,
4026 OPJ_BYTE * p_header_data,
4027 OPJ_UINT32 p_header_size,
4028 struct opj_event_mgr * p_manager
4032 opj_cp_v2_t *l_cp = 00;
4033 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4036 assert(p_header_data != 00);
4037 assert(p_j2k != 00);
4038 assert(p_manager != 00);
4040 if (p_header_size < 1) {
4041 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4045 l_cp = &(p_j2k->m_cp);
4048 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4052 /* First PPM marker */
4054 if (p_header_size < 4) {
4055 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4059 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4063 /* First PPM marker: Initialization */
4064 l_cp->ppm_len = l_N_ppm;
4065 l_cp->ppm_data_size = 0;
4067 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4068 if (l_cp->ppm_buffer == 00) {
4069 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4072 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4074 l_cp->ppm_data = l_cp->ppm_buffer;
4078 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4079 if (p_header_size >= 4) {
4081 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4084 l_cp->ppm_len += l_N_ppm ;
4086 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4087 if (l_cp->ppm_buffer == 00) {
4088 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4091 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4093 l_cp->ppm_data = l_cp->ppm_buffer;
4100 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4102 if (l_remaining_data <= p_header_size) {
4103 /* we must store less information than available in the packet */
4104 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4105 l_cp->ppm_data_size = l_cp->ppm_len;
4106 p_header_size -= l_remaining_data;
4107 p_header_data += l_remaining_data;
4110 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4111 l_cp->ppm_data_size += p_header_size;
4112 p_header_data += p_header_size;
4125 * Reads a PPM marker (Packed packet headers, main header)
4127 * @param p_header_data the data contained in the POC box.
4128 * @param p_j2k the jpeg2000 codec.
4129 * @param p_header_size the size of the data contained in the POC marker.
4130 * @param p_manager the user event manager.
4132 opj_bool j2k_read_ppm_v3 (
4133 opj_j2k_v2_t *p_j2k,
4134 OPJ_BYTE * p_header_data,
4135 OPJ_UINT32 p_header_size,
4136 struct opj_event_mgr * p_manager
4139 opj_cp_v2_t *l_cp = 00;
4140 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4143 assert(p_header_data != 00);
4144 assert(p_j2k != 00);
4145 assert(p_manager != 00);
4147 /* Minimum size of PPM marker is equal to the size of Zppm element */
4148 if (p_header_size < 1) {
4149 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4153 l_cp = &(p_j2k->m_cp);
4156 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4160 /* First PPM marker */
4162 /* We need now at least the Nppm^0 element */
4163 if (p_header_size < 4) {
4164 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4168 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4172 /* First PPM marker: Initialization */
4173 l_cp->ppm_len = l_N_ppm;
4174 l_cp->ppm_data_read = 0;
4176 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4177 if (l_cp->ppm_data == 00) {
4178 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4181 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4183 l_cp->ppm_data_current = l_cp->ppm_data;
4185 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4188 if (p_header_size < 4) {
4189 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4193 /* Uncompleted Ippm series in the previous PPM marker?*/
4194 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4195 /* Get the place where add the remaining Ippm series*/
4196 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4197 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4200 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4204 /* Increase the size of ppm_data to add the new Ippm series*/
4205 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4207 /* Keep the position of the place where concatenate the new series*/
4208 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4209 l_cp->ppm_len += l_N_ppm;
4214 l_remaining_data = p_header_size;
4216 while (l_remaining_data >= l_N_ppm) {
4217 /* read a complete Ippm series*/
4218 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4219 p_header_size -= l_N_ppm;
4220 p_header_data += l_N_ppm;
4222 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4226 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4231 l_remaining_data = p_header_size;
4235 l_remaining_data = p_header_size;
4237 /* Next Ippm series is a complete series ?*/
4238 if (l_remaining_data > l_N_ppm) {
4239 /* Increase the size of ppm_data to add the new Ippm series*/
4240 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4242 /* Keep the position of the place where concatenate the new series */
4243 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4244 l_cp->ppm_len += l_N_ppm;
4249 /* Need to read an incomplete Ippm series*/
4250 if (l_remaining_data) {
4251 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4253 /* Keep the position of the place where concatenate the new series*/
4254 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4255 l_cp->ppm_len += l_N_ppm;
4257 /* Read incomplete Ippm series*/
4258 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4259 p_header_size -= l_remaining_data;
4260 p_header_data += l_remaining_data;
4262 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4267 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4268 if (p_header_size >= 4) {
4270 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4273 l_cp->ppm_len += l_N_ppm ;
4275 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4276 if (l_cp->ppm_buffer == 00) {
4277 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4280 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4282 l_cp->ppm_data = l_cp->ppm_buffer;
4289 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4291 if (l_remaining_data <= p_header_size) {
4292 /* we must store less information than available in the packet */
4293 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4294 l_cp->ppm_data_size = l_cp->ppm_len;
4295 p_header_size -= l_remaining_data;
4296 p_header_data += l_remaining_data;
4299 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4300 l_cp->ppm_data_size += p_header_size;
4301 p_header_data += p_header_size;
4310 static void j2k_read_ppt(opj_j2k_t *j2k) {
4311 int len, Z_ppt, i, j = 0;
4313 opj_cp_t *cp = j2k->cp;
4314 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4315 opj_cio_t *cio = j2k->cio;
4317 len = cio_read(cio, 2);
4318 Z_ppt = cio_read(cio, 1);
4320 if (Z_ppt == 0) { /* First PPT marker */
4321 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4322 tcp->ppt_data_first = tcp->ppt_data;
4324 tcp->ppt_len = len - 3;
4325 } else { /* NON-first PPT marker */
4326 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4327 tcp->ppt_data_first = tcp->ppt_data;
4328 tcp->ppt_len = len - 3 + tcp->ppt_store;
4331 for (i = len - 3; i > 0; i--) {
4332 tcp->ppt_data[j] = cio_read(cio, 1);
4339 * Reads a PPT marker (Packed packet headers, tile-part header)
4341 * @param p_header_data the data contained in the PPT box.
4342 * @param p_j2k the jpeg2000 codec.
4343 * @param p_header_size the size of the data contained in the PPT marker.
4344 * @param p_manager the user event manager.
4346 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4347 OPJ_BYTE * p_header_data,
4348 OPJ_UINT32 p_header_size,
4349 struct opj_event_mgr * p_manager )
4351 opj_cp_v2_t *l_cp = 00;
4352 opj_tcp_v2_t *l_tcp = 00;
4356 assert(p_header_data != 00);
4357 assert(p_j2k != 00);
4358 assert(p_manager != 00);
4360 /* We need to have the Z_ppt element at minimum */
4361 if (p_header_size < 1) {
4362 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4366 l_cp = &(p_j2k->m_cp);
4368 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");
4372 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4375 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4379 /* Allocate buffer to read the packet header */
4381 /* First PPT marker */
4382 l_tcp->ppt_data_size = 0;
4383 l_tcp->ppt_len = p_header_size;
4385 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4386 if (l_tcp->ppt_buffer == 00) {
4387 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4390 l_tcp->ppt_data = l_tcp->ppt_buffer;
4392 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4395 l_tcp->ppt_len += p_header_size;
4397 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4398 if (l_tcp->ppt_buffer == 00) {
4399 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4402 l_tcp->ppt_data = l_tcp->ppt_buffer;
4404 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4407 /* Read packet header from buffer */
4408 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4410 l_tcp->ppt_data_size += p_header_size;
4416 * Writes the TLM marker (Tile Length Marker)
4418 * @param p_stream the stream to write data to.
4419 * @param p_j2k J2K codec.
4420 * @param p_manager the user event manager.
4422 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4423 struct opj_stream_private *p_stream,
4424 struct opj_event_mgr * p_manager )
4426 OPJ_BYTE * l_current_data = 00;
4427 OPJ_UINT32 l_tlm_size;
4430 assert(p_j2k != 00);
4431 assert(p_manager != 00);
4432 assert(p_stream != 00);
4434 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4436 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4437 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4438 = (OPJ_BYTE*)opj_realloc(
4439 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4442 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4446 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4449 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4451 /* change the way data is written to avoid seeking if possible */
4453 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4455 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4456 l_current_data += 2;
4458 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4459 l_current_data += 2;
4461 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4464 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4467 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4468 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) {
4475 static void j2k_write_tlm(opj_j2k_t *j2k){
4477 opj_cio_t *cio = j2k->cio;
4478 j2k->tlm_start = cio_tell(cio);
4479 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4480 lenp = 4 + (5*j2k->totnum_tp);
4481 cio_write(cio,lenp,2); /* Ltlm */
4482 cio_write(cio, 0,1); /* Ztlm=0*/
4483 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4484 cio_skip(cio,5*j2k->totnum_tp);
4487 static void j2k_write_sot(opj_j2k_t *j2k) {
4490 opj_cio_t *cio = j2k->cio;
4492 j2k->sot_start = cio_tell(cio);
4493 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4494 lenp = cio_tell(cio);
4495 cio_skip(cio, 2); /* Lsot (further) */
4496 cio_write(cio, j2k->curtileno, 2); /* Isot */
4497 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4498 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4499 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4500 len = cio_tell(cio) - lenp;
4501 cio_seek(cio, lenp);
4502 cio_write(cio, len, 2); /* Lsot */
4503 cio_seek(cio, lenp + len);
4507 /* update markers struct */
4508 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4509 #endif /* USE_JPWL */
4512 if( j2k->cstr_info && j2k->cur_tp_num==0){
4513 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4518 * Writes the SOT marker (Start of tile-part)
4520 * @param p_stream the stream to write data to.
4521 * @param p_j2k J2K codec.
4522 * @param p_manager the user event manager.
4524 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4526 OPJ_UINT32 * p_data_written,
4527 const struct opj_stream_private *p_stream,
4528 struct opj_event_mgr * p_manager )
4531 assert(p_j2k != 00);
4532 assert(p_manager != 00);
4533 assert(p_stream != 00);
4535 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4538 opj_write_bytes(p_data,10,2); /* Lsot */
4541 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4547 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4550 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4555 /* update markers struct */
4557 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4559 assert( 0 && "TODO" );
4560 #endif /* USE_JPWL */
4562 * p_data_written = 12;
4571 * Reads a PPT marker (Packed packet headers, tile-part header)
4573 * @param p_header_data the data contained in the PPT box.
4574 * @param p_j2k the jpeg2000 codec.
4575 * @param p_header_size the size of the data contained in the PPT marker.
4576 * @param p_manager the user event manager.
4578 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4579 OPJ_BYTE * p_header_data,
4580 OPJ_UINT32 p_header_size,
4581 opj_event_mgr_t * p_manager )
4583 opj_cp_v2_t *l_cp = 00;
4584 opj_tcp_v2_t *l_tcp = 00;
4585 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4586 OPJ_UINT32 l_current_part;
4587 OPJ_UINT32 l_tile_x,l_tile_y;
4590 assert(p_header_data != 00);
4591 assert(p_j2k != 00);
4592 assert(p_manager != 00);
4594 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4595 if (p_header_size != 8) {
4596 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4600 l_cp = &(p_j2k->m_cp);
4601 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4604 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4605 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4606 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4609 if (l_cp->correct) {
4611 int tileno = p_j2k->m_current_tile_number;
4612 static int backup_tileno = 0;
4614 /* tileno is negative or larger than the number of tiles!!! */
4615 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4616 opj_event_msg_v2(p_manager, EVT_ERROR,
4617 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4618 tileno, (l_cp->tw * l_cp->th));
4620 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4623 /* we try to correct */
4624 tileno = backup_tileno;
4625 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4626 "- setting tile number to %d\n",
4630 /* keep your private count of tiles */
4633 #endif /* USE_JPWL */
4635 /* look for the tile in the list of already processed tile (in parts). */
4636 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4637 /* since the time taken by this function can only grow at the time */
4639 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4642 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4643 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4645 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4651 if (l_cp->correct) {
4653 /* totlen is negative or larger than the bytes left!!! */
4654 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))) { */
4655 opj_event_msg_v2(p_manager, EVT_ERROR,
4656 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4657 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4659 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4662 /* we try to correct */
4664 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4665 "- setting Psot to %d => assuming it is the last tile\n",
4669 #endif /* USE_JPWL */
4671 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4673 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4674 "we assuming it is the last tile-part of the codestream.\n");
4675 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4678 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4681 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4684 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4685 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4686 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4687 if (l_tcp->m_nb_tile_parts) {
4688 if (l_current_part >= l_tcp->m_nb_tile_parts){
4689 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4690 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4691 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4695 l_tcp->m_nb_tile_parts = l_num_parts;
4698 /* If know the number of tile part header we will check if we didn't read the last*/
4699 if (l_tcp->m_nb_tile_parts) {
4700 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4701 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4705 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4706 /* Keep the size of data to skip after this marker */
4707 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4710 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4711 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4714 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4716 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4717 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4718 p_j2k->m_specific_param.m_decoder.m_skip_data =
4719 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4720 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4721 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4722 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4725 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4726 p_j2k->m_specific_param.m_decoder.m_skip_data =
4727 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4731 if (p_j2k->cstr_index)
4733 assert(p_j2k->cstr_index->tile_index != 00);
4734 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4735 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4737 if (l_num_parts != 0){
4738 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4739 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4742 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4743 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4744 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4746 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4747 (opj_tp_index_t*)opj_realloc(
4748 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4749 l_num_parts* sizeof(opj_tp_index_t));
4752 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4754 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4755 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4756 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4757 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4758 sizeof(opj_tp_index_t));
4761 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4762 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4763 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4764 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4765 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4766 * sizeof(opj_tp_index_t));
4775 /* 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 */
4776 /* if (p_j2k->cstr_info) {
4779 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4782 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4783 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4784 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4785 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4788 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4791 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4795 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4798 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4799 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4800 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4805 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4808 opj_tcp_t *tcp = NULL;
4809 opj_codestream_info_t *cstr_info = NULL;
4811 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4812 opj_cp_t *cp = j2k->cp;
4813 opj_cio_t *cio = j2k->cio;
4815 tcd->tp_num = j2k->tp_num ;
4816 tcd->cur_tp_num = j2k->cur_tp_num;
4818 cio_write(cio, J2K_MS_SOD, 2);
4820 if( j2k->cstr_info && j2k->cur_tp_num==0){
4821 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4824 if (j2k->curtileno == 0) {
4825 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4829 cstr_info = j2k->cstr_info;
4831 if (!j2k->cur_tp_num ) {
4832 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4833 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4836 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4837 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4841 /* update markers struct */
4842 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4843 #endif /* USE_JPWL */
4848 tcp = &cp->tcps[j2k->curtileno];
4849 for (layno = 0; layno < tcp->numlayers; layno++) {
4850 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4851 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4852 } else if (tcp->rates[layno]) {
4853 tcp->rates[layno]=1;
4856 if(j2k->cur_tp_num == 0){
4857 tcd->tcd_image->tiles->packno = 0;
4859 cstr_info->packno = 0;
4862 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4864 /* Writing Psot in SOT marker */
4865 totlen = cio_tell(cio) + l - j2k->sot_start;
4866 cio_seek(cio, j2k->sot_start + 6);
4867 cio_write(cio, totlen, 4);
4868 cio_seek(cio, j2k->sot_start + totlen);
4869 /* Writing Ttlm and Ptlm in TLM marker */
4871 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4872 cio_write(cio, j2k->curtileno, 1);
4873 cio_write(cio, totlen, 4);
4875 cio_seek(cio, j2k->sot_start + totlen);
4879 * Writes the SOD marker (Start of data)
4881 * @param p_stream the stream to write data to.
4882 * @param p_j2k J2K codec.
4883 * @param p_manager the user event manager.
4885 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4886 struct opj_tcd_v2 * p_tile_coder,
4888 OPJ_UINT32 * p_data_written,
4889 OPJ_UINT32 p_total_data_size,
4890 const struct opj_stream_private *p_stream,
4891 struct opj_event_mgr * p_manager )
4893 opj_tcp_v2_t *l_tcp = 00;
4894 opj_codestream_info_t *l_cstr_info = 00;
4895 opj_cp_v2_t *l_cp = 00;
4897 OPJ_UINT32 l_size_tile;
4898 OPJ_UINT32 l_remaining_data;
4901 assert(p_j2k != 00);
4902 assert(p_manager != 00);
4903 assert(p_stream != 00);
4905 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4908 /* make room for the EOF marker */
4909 l_remaining_data = p_total_data_size - 4;
4911 l_cp = &(p_j2k->m_cp);
4912 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4915 /* update tile coder */
4916 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4917 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4919 l_size_tile = l_cp->th * l_cp->tw;
4922 /* TODO mergeV2: check this part which use cstr_info */
4923 /*l_cstr_info = p_j2k->cstr_info;
4925 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4926 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4927 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4933 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4935 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4940 /* update markers struct */
4941 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4943 assert( 0 && "TODO" );
4944 #endif /* USE_JPWL */
4949 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4950 p_tile_coder->tcd_image->tiles->packno = 0;
4952 l_cstr_info->packno = 0;
4956 *p_data_written = 0;
4958 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)) {
4959 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4963 *p_data_written += 2;
4972 * Reads a SOD marker (Start Of Data)
4974 * @param p_header_data the data contained in the SOD box.
4975 * @param p_j2k the jpeg2000 codec.
4976 * @param p_header_size the size of the data contained in the SOD marker.
4977 * @param p_manager the user event manager.
4979 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4980 opj_stream_private_t *p_stream,
4981 opj_event_mgr_t * p_manager
4984 OPJ_UINT32 l_current_read_size;
4985 opj_codestream_index_t * l_cstr_index = 00;
4986 OPJ_BYTE ** l_current_data = 00;
4987 opj_tcp_v2_t * l_tcp = 00;
4988 OPJ_UINT32 * l_tile_len = 00;
4991 assert(p_j2k != 00);
4992 assert(p_manager != 00);
4993 assert(p_stream != 00);
4995 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4997 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4998 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4999 // but we are in the last tile part,
5000 // so its result will fit on OPJ_UINT32 unless we find
5001 // a file with a single tile part of more than 4 GB...*/
5002 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5005 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5007 l_current_data = &(l_tcp->m_data);
5008 l_tile_len = &l_tcp->m_data_size;
5010 if (! *l_current_data) {
5011 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5014 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5017 if (*l_current_data == 00) {
5018 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5024 l_cstr_index = p_j2k->cstr_index;
5026 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5028 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5029 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5031 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5032 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5034 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5038 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5040 /*l_cstr_index->packno = 0;*/
5043 l_current_read_size = opj_stream_read_data( p_stream,
5044 *l_current_data + *l_tile_len,
5045 p_j2k->m_specific_param.m_decoder.m_sot_length,
5048 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5049 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5052 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5055 *l_tile_len += l_current_read_size;
5061 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5062 opj_cp_t *cp = j2k->cp;
5063 opj_tcp_t *tcp = &cp->tcps[tileno];
5064 opj_cio_t *cio = j2k->cio;
5065 int numcomps = j2k->image->numcomps;
5067 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5068 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5069 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5070 cio_write(cio, 0, 1); /* Srgn */
5071 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5075 * Writes the RGN marker (Region Of Interest)
5077 * @param p_tile_no the tile to output
5078 * @param p_comp_no the component to output
5079 * @param p_stream the stream to write data to.
5080 * @param p_j2k J2K codec.
5081 * @param p_manager the user event manager.
5083 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5084 OPJ_UINT32 p_tile_no,
5085 OPJ_UINT32 p_comp_no,
5086 struct opj_stream_private *p_stream,
5087 struct opj_event_mgr * p_manager )
5089 OPJ_BYTE * l_current_data = 00;
5090 OPJ_UINT32 l_nb_comp;
5091 OPJ_UINT32 l_rgn_size;
5092 opj_image_t *l_image = 00;
5093 opj_cp_v2_t *l_cp = 00;
5094 opj_tcp_v2_t *l_tcp = 00;
5095 opj_tccp_t *l_tccp = 00;
5096 OPJ_UINT32 l_comp_room;
5099 assert(p_j2k != 00);
5100 assert(p_manager != 00);
5101 assert(p_stream != 00);
5103 l_cp = &(p_j2k->m_cp);
5104 l_tcp = &l_cp->tcps[p_tile_no];
5105 l_tccp = &l_tcp->tccps[p_comp_no];
5107 l_nb_comp = l_image->numcomps;
5109 if (l_nb_comp <= 256) {
5116 l_rgn_size = 6 + l_comp_room;
5118 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5120 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5121 l_current_data += 2;
5123 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5124 l_current_data += 2;
5126 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5127 l_current_data+=l_comp_room;
5129 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5132 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5135 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) {
5143 static void j2k_write_eoc(opj_j2k_t *j2k) {
5144 opj_cio_t *cio = j2k->cio;
5145 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5146 cio_write(cio, J2K_MS_EOC, 2);
5150 /* update markers struct */
5151 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5152 #endif /* USE_JPWL */
5157 * Writes the EOC marker (End of Codestream)
5159 * @param p_stream the stream to write data to.
5160 * @param p_j2k J2K codec.
5161 * @param p_manager the user event manager.
5163 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5164 struct opj_stream_private *p_stream,
5165 struct opj_event_mgr * p_manager )
5168 assert(p_j2k != 00);
5169 assert(p_manager != 00);
5170 assert(p_stream != 00);
5172 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5177 /* update markers struct */
5179 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5181 #endif /* USE_JPWL */
5183 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5187 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5196 * Reads a RGN marker (Region Of Interest)
5198 * @param p_header_data the data contained in the POC box.
5199 * @param p_j2k the jpeg2000 codec.
5200 * @param p_header_size the size of the data contained in the POC marker.
5201 * @param p_manager the user event manager.
5203 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5204 OPJ_BYTE * p_header_data,
5205 OPJ_UINT32 p_header_size,
5206 opj_event_mgr_t * p_manager
5209 OPJ_UINT32 l_nb_comp;
5210 opj_image_t * l_image = 00;
5212 opj_cp_v2_t *l_cp = 00;
5213 opj_tcp_v2_t *l_tcp = 00;
5214 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5217 assert(p_header_data != 00);
5218 assert(p_j2k != 00);
5219 assert(p_manager != 00);
5221 l_image = p_j2k->m_private_image;
5222 l_nb_comp = l_image->numcomps;
5224 if (l_nb_comp <= 256) {
5229 if (p_header_size != 2 + l_comp_room) {
5230 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5234 l_cp = &(p_j2k->m_cp);
5235 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5236 &l_cp->tcps[p_j2k->m_current_tile_number] :
5237 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5239 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5240 p_header_data+=l_comp_room;
5241 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5245 if (l_cp->correct) {
5246 /* totlen is negative or larger than the bytes left!!! */
5247 if (l_comp_room >= l_nb_comp) {
5248 opj_event_msg_v2(p_manager, EVT_ERROR,
5249 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5250 l_comp_room, l_nb_comp);
5251 if (!JPWL_ASSUME || JPWL_ASSUME) {
5252 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5257 #endif /* USE_JPWL */
5259 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5266 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5268 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5271 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5278 * Updates the rates of the tcp.
5280 * @param p_stream the stream to write data to.
5281 * @param p_j2k J2K codec.
5282 * @param p_manager the user event manager.
5284 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5285 struct opj_stream_private *p_stream,
5286 struct opj_event_mgr * p_manager )
5288 opj_cp_v2_t * l_cp = 00;
5289 opj_image_t * l_image = 00;
5290 opj_tcp_v2_t * l_tcp = 00;
5291 opj_image_comp_t * l_img_comp = 00;
5294 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5295 OPJ_FLOAT32 * l_rates = 0;
5296 OPJ_FLOAT32 l_sot_remove;
5297 OPJ_UINT32 l_bits_empty, l_size_pixel;
5298 OPJ_UINT32 l_tile_size = 0;
5299 OPJ_UINT32 l_last_res;
5300 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5303 assert(p_j2k != 00);
5304 assert(p_manager != 00);
5305 assert(p_stream != 00);
5308 l_cp = &(p_j2k->m_cp);
5309 l_image = p_j2k->m_private_image;
5312 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5313 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5314 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5316 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5317 l_tp_stride_func = get_tp_stride;
5320 l_tp_stride_func = get_default_stride;
5323 for (i=0;i<l_cp->th;++i) {
5324 for (j=0;j<l_cp->tw;++j) {
5325 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5327 /* 4 borders of the tile rescale on the image if necessary */
5328 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5329 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5330 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5331 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5333 l_rates = l_tcp->rates;
5335 /* Modification of the RATE >> */
5337 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5339 ((*l_rates) * l_bits_empty)
5347 for (k = 1; k < l_tcp->numlayers; ++k) {
5349 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5351 ((*l_rates) * l_bits_empty)
5367 for (i=0;i<l_cp->th;++i) {
5368 for (j=0;j<l_cp->tw;++j) {
5369 l_rates = l_tcp->rates;
5372 *l_rates -= l_sot_remove;
5374 if (*l_rates < 30) {
5381 l_last_res = l_tcp->numlayers - 1;
5383 for (k = 1; k < l_last_res; ++k) {
5386 *l_rates -= l_sot_remove;
5388 if (*l_rates < *(l_rates - 1) + 10) {
5389 *l_rates = (*(l_rates - 1)) + 20;
5397 *l_rates -= (l_sot_remove + 2.f);
5399 if (*l_rates < *(l_rates - 1) + 10) {
5400 *l_rates = (*(l_rates - 1)) + 20;
5408 l_img_comp = l_image->comps;
5411 for (i=0;i<l_image->numcomps;++i) {
5412 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5414 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5422 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5424 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5426 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5427 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5428 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5429 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5433 if (l_cp->m_specific_param.m_enc.m_cinema) {
5434 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5435 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5436 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5440 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5441 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5447 static void j2k_read_eoc(opj_j2k_t *j2k) {
5449 opj_bool success = OPJ_FALSE;
5451 /* if packets should be decoded */
5452 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5453 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5454 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5455 for (i = 0; i < j2k->cp->tileno_size; i++) {
5456 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5457 if (j2k->cp->tileno[i] != -1)
5459 tileno = j2k->cp->tileno[i];
5460 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5461 opj_free(j2k->tile_data[tileno]);
5462 j2k->tile_data[tileno] = NULL;
5463 tcd_free_decode_tile(tcd, i);
5466 success = OPJ_FALSE;
5467 if (success == OPJ_FALSE) {
5468 j2k->state |= J2K_STATE_ERR;
5472 tcd_free_decode(tcd);
5475 /* if packets should not be decoded */
5477 for (i = 0; i < j2k->cp->tileno_size; i++) {
5478 tileno = j2k->cp->tileno[i];
5479 opj_free(j2k->tile_data[tileno]);
5480 j2k->tile_data[tileno] = NULL;
5483 if (j2k->state & J2K_STATE_ERR)
5484 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5486 j2k->state = J2K_STATE_MT;
5490 * Reads a EOC marker (End Of Codestream)
5492 * @param p_header_data the data contained in the SOD box.
5493 * @param p_j2k the jpeg2000 codec.
5494 * @param p_header_size the size of the data contained in the SOD marker.
5495 * @param p_manager the user event manager.
5498 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5499 struct opj_stream_private *p_stream,
5500 struct opj_event_mgr * p_manager )
5503 opj_tcd_v2_t * l_tcd = 00;
5504 OPJ_UINT32 l_nb_tiles;
5505 opj_tcp_v2_t * l_tcp = 00;
5509 assert(p_j2k != 00);
5510 assert(p_manager != 00);
5511 assert(p_stream != 00);
5513 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5514 l_tcp = p_j2k->m_cp.tcps;
5516 l_tcd = tcd_create_v2(OPJ_TRUE);
5518 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5522 for (i = 0; i < l_nb_tiles; ++i) {
5523 if (l_tcp->m_data) {
5524 if (! tcd_init_decode_tile(l_tcd, i)) {
5525 tcd_destroy_v2(l_tcd);
5526 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5530 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5534 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5539 j2k_tcp_destroy(l_tcp);
5543 tcd_destroy_v2(l_tcd);
5549 * Gets the offset of the header.
5551 * @param p_stream the stream to write data to.
5552 * @param p_j2k J2K codec.
5553 * @param p_manager the user event manager.
5555 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5556 struct opj_stream_private *p_stream,
5557 struct opj_event_mgr * p_manager )
5560 assert(p_j2k != 00);
5561 assert(p_manager != 00);
5562 assert(p_stream != 00);
5564 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5570 * Writes the MCT marker (Multiple Component Transform)
5572 * @param p_stream the stream to write data to.
5573 * @param p_j2k J2K codec.
5574 * @param p_manager the user event manager.
5576 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5577 struct opj_stream_private *p_stream,
5578 struct opj_event_mgr * p_manager )
5581 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5582 opj_mct_data_t * l_mct_record;
5583 opj_tcp_v2_t * l_tcp;
5586 assert(p_j2k != 00);
5587 assert(p_stream != 00);
5588 assert(p_manager != 00);
5590 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5594 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5595 l_mct_record = l_tcp->m_mct_records;
5597 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5599 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5606 l_mcc_record = l_tcp->m_mcc_records;
5608 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5610 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5617 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5625 * Writes the image components.
5627 * @param p_stream the stream to write data to.
5628 * @param p_j2k J2K codec.
5629 * @param p_manager the user event manager.
5631 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5632 struct opj_stream_private *p_stream,
5633 struct opj_event_mgr * p_manager )
5638 assert(p_j2k != 00);
5639 assert(p_manager != 00);
5640 assert(p_stream != 00);
5642 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5644 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5648 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5657 * Writes regions of interests.
5659 * @param p_stream the stream to write data to.
5660 * @param p_j2k J2K codec.
5661 * @param p_manager the user event manager.
5663 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5664 struct opj_stream_private *p_stream,
5665 struct opj_event_mgr * p_manager )
5668 const opj_tccp_t *l_tccp = 00;
5671 assert(p_j2k != 00);
5672 assert(p_manager != 00);
5673 assert(p_stream != 00);
5675 l_tccp = p_j2k->m_cp.tcps->tccps;
5677 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5678 if (l_tccp->roishift) {
5680 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5694 * @param p_stream the stream to write data to.
5695 * @param p_j2k J2K codec.
5696 * @param p_manager the user event manager.
5698 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5699 struct opj_stream_private *p_stream,
5700 struct opj_event_mgr * p_manager )
5702 opj_codestream_index_t * l_cstr_index = 00;
5705 assert(p_j2k != 00);
5706 assert(p_manager != 00);
5707 assert(p_stream != 00);
5709 l_cstr_index = p_j2k->cstr_index;
5711 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5713 /* The following adjustment is done to adjust the codestream size */
5714 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5715 /* the first bunch of bytes is not in the codestream */
5716 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5721 /* preparation of JPWL marker segments */
5725 /* encode according to JPWL */
5726 jpwl_encode(p_j2k, p_stream, image);
5730 assert( 0 && "TODO" );
5731 #endif /* USE_JPWL */
5736 typedef struct opj_dec_mstabent {
5739 /** value of the state when the marker can appear */
5741 /** action linked to the marker */
5742 void (*handler) (opj_j2k_t *j2k);
5743 } opj_dec_mstabent_t;
5745 opj_dec_mstabent_t j2k_dec_mstab[] = {
5746 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5747 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5748 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5749 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5750 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5751 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5752 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5753 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5754 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5755 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5756 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5757 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5758 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5759 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5760 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5761 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
5763 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
5764 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
5767 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5768 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5769 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5770 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5771 #endif /* USE_JPWL */
5773 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5774 {J2K_MS_INSEC, 0, j2k_read_insec},
5775 #endif /* USE_JPSEC */
5777 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5780 static void j2k_read_unk(opj_j2k_t *j2k) {
5781 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5784 if (j2k->cp->correct) {
5786 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5787 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5788 id = cio_read(j2k->cio, 2);
5789 opj_event_msg(j2k->cinfo, EVT_ERROR,
5790 "JPWL: really don't know this marker %x\n",
5793 opj_event_msg(j2k->cinfo, EVT_ERROR,
5794 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5797 /* OK, activate this at your own risk!!! */
5798 /* we look for the marker at the minimum hamming distance from this */
5799 while (j2k_dec_mstab[m].id) {
5801 /* 1's where they differ */
5802 tmp_id = j2k_dec_mstab[m].id ^ id;
5804 /* compute the hamming distance between our id and the current */
5806 for (i = 0; i < 16; i++) {
5807 if ((tmp_id >> i) & 0x0001) {
5812 /* if current distance is smaller, set the minimum */
5813 if (cur_dist < min_dist) {
5814 min_dist = cur_dist;
5815 min_id = j2k_dec_mstab[m].id;
5818 /* jump to the next marker */
5822 /* do we substitute the marker? */
5823 if (min_dist < JPWL_MAXIMUM_HAMMING) {
5824 opj_event_msg(j2k->cinfo, EVT_ERROR,
5825 "- marker %x is at distance %d from the read %x\n",
5826 min_id, min_dist, id);
5827 opj_event_msg(j2k->cinfo, EVT_ERROR,
5828 "- trying to substitute in place and crossing fingers!\n");
5829 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5830 cio_write(j2k->cio, min_id, 2);
5833 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5838 #endif /* USE_JPWL */
5843 * Reads an unknown marker
5845 * @param p_stream the stream object to read from.
5846 * @param p_j2k the jpeg2000 codec.
5847 * @param p_manager the user event manager.
5849 * @return true if the marker could be deduced.
5851 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
5852 struct opj_stream_private *p_stream,
5853 OPJ_UINT32 *output_marker,
5854 struct opj_event_mgr * p_manager
5857 OPJ_UINT32 l_unknown_marker;
5858 const opj_dec_memory_marker_handler_t * l_marker_handler;
5859 OPJ_UINT32 l_size_unk = 2;
5862 assert(p_j2k != 00);
5863 assert(p_manager != 00);
5864 assert(p_stream != 00);
5866 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5869 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5870 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5871 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5875 /* read 2 bytes as the new marker ID*/
5876 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5878 if (!(l_unknown_marker < 0xff00)) {
5880 /* Get the marker handler from the marker ID*/
5881 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5883 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5884 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5888 if (l_marker_handler->id != J2K_MS_UNK) {
5889 /* Add the marker to the codestream index*/
5890 if (l_marker_handler->id != J2K_MS_SOT)
5891 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5892 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5894 break; /* next marker is known and well located */
5902 *output_marker = l_marker_handler->id ;
5908 Read the lookup table containing all the marker, status and action
5909 @param id Marker value
5911 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5912 opj_dec_mstabent_t *e;
5913 for (e = j2k_dec_mstab; e->id != 0; e++) {
5922 * Writes the MCT marker (Multiple Component Transform)
5924 * @param p_stream the stream to write data to.
5925 * @param p_j2k J2K codec.
5926 * @param p_manager the user event manager.
5928 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5929 opj_mct_data_t * p_mct_record,
5930 struct opj_stream_private *p_stream,
5931 struct opj_event_mgr * p_manager )
5933 OPJ_UINT32 l_mct_size;
5934 OPJ_BYTE * l_current_data = 00;
5938 assert(p_j2k != 00);
5939 assert(p_manager != 00);
5940 assert(p_stream != 00);
5942 l_mct_size = 10 + p_mct_record->m_data_size;
5944 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5945 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5946 = (OPJ_BYTE*)opj_realloc(
5947 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_mct_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_MCT,2); /* MCT */
5960 l_current_data += 2;
5962 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5963 l_current_data += 2;
5965 opj_write_bytes(l_current_data,0,2); /* Zmct */
5966 l_current_data += 2;
5968 /* only one marker atm */
5969 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5971 opj_write_bytes(l_current_data,l_tmp,2);
5972 l_current_data += 2;
5974 opj_write_bytes(l_current_data,0,2); /* Ymct */
5977 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5979 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) {
5987 * Reads a MCT marker (Multiple Component Transform)
5989 * @param p_header_data the data contained in the MCT box.
5990 * @param p_j2k the jpeg2000 codec.
5991 * @param p_header_size the size of the data contained in the MCT marker.
5992 * @param p_manager the user event manager.
5994 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5995 OPJ_BYTE * p_header_data,
5996 OPJ_UINT32 p_header_size,
5997 struct opj_event_mgr * p_manager )
6000 opj_tcp_v2_t *l_tcp = 00;
6003 opj_mct_data_t * l_mct_data;
6006 assert(p_header_data != 00);
6007 assert(p_j2k != 00);
6009 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6010 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6011 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6013 if (p_header_size < 2) {
6014 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6019 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6022 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6026 if(p_header_size <= 6) {
6027 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6031 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6032 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6035 l_indix = l_tmp & 0xff;
6036 l_mct_data = l_tcp->m_mct_records;
6038 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6039 if (l_mct_data->m_index == l_indix) {
6046 if (i == l_tcp->m_nb_mct_records) {
6047 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6048 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6050 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));
6051 if(! l_tcp->m_mct_records) {
6052 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6056 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6057 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6060 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6063 if (l_mct_data->m_data) {
6064 opj_free(l_mct_data->m_data);
6065 l_mct_data->m_data = 00;
6068 l_mct_data->m_index = l_indix;
6069 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6070 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6072 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6075 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6081 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6082 if (! l_mct_data->m_data) {
6083 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6086 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6088 l_mct_data->m_data_size = p_header_size;
6089 ++l_tcp->m_nb_mct_records;
6095 * Writes the MCC marker (Multiple Component Collection)
6097 * @param p_stream the stream to write data to.
6098 * @param p_j2k J2K codec.
6099 * @param p_manager the user event manager.
6101 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6102 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6103 struct opj_stream_private *p_stream,
6104 struct opj_event_mgr * p_manager )
6107 OPJ_UINT32 l_mcc_size;
6108 OPJ_BYTE * l_current_data = 00;
6109 OPJ_UINT32 l_nb_bytes_for_comp;
6114 assert(p_j2k != 00);
6115 assert(p_manager != 00);
6116 assert(p_stream != 00);
6118 if (p_mcc_record->m_nb_comps > 255 ) {
6119 l_nb_bytes_for_comp = 2;
6123 l_nb_bytes_for_comp = 1;
6127 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6128 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6130 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6131 = (OPJ_BYTE*)opj_realloc(
6132 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6134 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6138 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6141 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6143 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6144 l_current_data += 2;
6146 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6147 l_current_data += 2;
6150 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6151 l_current_data += 2;
6153 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6156 /* only one marker atm */
6157 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6160 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6163 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6166 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 */
6169 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6170 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6171 l_current_data+=l_nb_bytes_for_comp;
6174 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 */
6177 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6179 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6180 l_current_data+=l_nb_bytes_for_comp;
6183 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6185 if (p_mcc_record->m_decorrelation_array) {
6186 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6189 if (p_mcc_record->m_offset_array) {
6190 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6193 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6196 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) {
6204 * Reads a MCC marker (Multiple Component Collection)
6206 * @param p_header_data the data contained in the MCC box.
6207 * @param p_j2k the jpeg2000 codec.
6208 * @param p_header_size the size of the data contained in the MCC marker.
6209 * @param p_manager the user event manager.
6211 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6212 OPJ_BYTE * p_header_data,
6213 OPJ_UINT32 p_header_size,
6214 struct opj_event_mgr * p_manager )
6219 opj_tcp_v2_t * l_tcp;
6220 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6221 opj_mct_data_t * l_mct_data;
6222 OPJ_UINT32 l_nb_collections;
6223 OPJ_UINT32 l_nb_comps;
6224 OPJ_UINT32 l_nb_bytes_by_comp;
6228 assert(p_header_data != 00);
6229 assert(p_j2k != 00);
6230 assert(p_manager != 00);
6232 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6233 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6234 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6236 if (p_header_size < 2) {
6237 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6242 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6245 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6249 if (p_header_size < 7) {
6250 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6254 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6257 l_mcc_record = l_tcp->m_mcc_records;
6259 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6260 if (l_mcc_record->m_index == l_indix) {
6267 if (i == l_tcp->m_nb_mcc_records) {
6268 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6269 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6271 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6272 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6273 if (! l_tcp->m_mcc_records) {
6274 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6277 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6278 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));
6280 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6282 l_mcc_record->m_index = l_indix;
6284 /* only one marker atm */
6285 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6288 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6292 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6295 if (l_nb_collections > 1) {
6296 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6302 for (i=0;i<l_nb_collections;++i) {
6303 if (p_header_size < 3) {
6304 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6308 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6312 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6316 opj_read_bytes(p_header_data,&l_nb_comps,2);
6321 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6322 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6324 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6325 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6329 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6331 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6332 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6333 p_header_data+=l_nb_bytes_by_comp;
6336 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6341 opj_read_bytes(p_header_data,&l_nb_comps,2);
6344 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6345 l_nb_comps &= 0x7fff;
6347 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6348 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6352 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6353 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6357 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6359 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6360 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6361 p_header_data+=l_nb_bytes_by_comp;
6364 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6369 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6372 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6373 l_mcc_record->m_decorrelation_array = 00;
6374 l_mcc_record->m_offset_array = 00;
6376 l_indix = l_tmp & 0xff;
6378 l_mct_data = l_tcp->m_mct_records;
6379 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6380 if (l_mct_data->m_index == l_indix) {
6381 l_mcc_record->m_decorrelation_array = l_mct_data;
6387 if (l_mcc_record->m_decorrelation_array == 00) {
6388 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6393 l_indix = (l_tmp >> 8) & 0xff;
6395 l_mct_data = l_tcp->m_mct_records;
6396 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6397 if (l_mct_data->m_index == l_indix) {
6398 l_mcc_record->m_offset_array = l_mct_data;
6404 if (l_mcc_record->m_offset_array == 00) {
6405 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6411 if (p_header_size != 0) {
6412 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6416 ++l_tcp->m_nb_mcc_records;
6423 * Writes the MCO marker (Multiple component transformation ordering)
6425 * @param p_stream the stream to write data to.
6426 * @param p_j2k J2K codec.
6427 * @param p_manager the user event manager.
6429 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6430 struct opj_stream_private *p_stream,
6431 struct opj_event_mgr * p_manager
6434 OPJ_BYTE * l_current_data = 00;
6435 OPJ_UINT32 l_mco_size;
6436 opj_tcp_v2_t * l_tcp = 00;
6437 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6441 assert(p_j2k != 00);
6442 assert(p_manager != 00);
6443 assert(p_stream != 00);
6445 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6446 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6448 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6449 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6451 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6452 = (OPJ_BYTE*)opj_realloc(
6453 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6455 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6460 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6463 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6464 l_current_data += 2;
6466 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6467 l_current_data += 2;
6469 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6472 l_mcc_record = l_tcp->m_mcc_records;
6473 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6474 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6480 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) {
6488 * Reads a MCO marker (Multiple Component Transform Ordering)
6490 * @param p_header_data the data contained in the MCO box.
6491 * @param p_j2k the jpeg2000 codec.
6492 * @param p_header_size the size of the data contained in the MCO marker.
6493 * @param p_manager the user event manager.
6495 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6496 OPJ_BYTE * p_header_data,
6497 OPJ_UINT32 p_header_size,
6498 struct opj_event_mgr * p_manager )
6500 OPJ_UINT32 l_tmp, i;
6501 OPJ_UINT32 l_nb_stages;
6502 opj_tcp_v2_t * l_tcp;
6503 opj_tccp_t * l_tccp;
6504 opj_image_t * l_image;
6505 opj_image_comp_t * l_img_comp;
6508 assert(p_header_data != 00);
6509 assert(p_j2k != 00);
6510 assert(p_manager != 00);
6512 l_image = p_j2k->m_private_image;
6513 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6514 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6515 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6517 if (p_header_size < 1) {
6518 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6522 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6525 if (l_nb_stages > 1) {
6526 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6530 if (p_header_size != l_nb_stages + 1) {
6531 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6535 l_tccp = l_tcp->tccps;
6536 l_img_comp = l_image->comps;
6538 for (i=0;i<l_image->numcomps;++i) {
6539 l_tccp->m_dc_level_shift = 0;
6543 if (l_tcp->m_mct_decoding_matrix) {
6544 opj_free(l_tcp->m_mct_decoding_matrix);
6545 l_tcp->m_mct_decoding_matrix = 00;
6548 for (i=0;i<l_nb_stages;++i) {
6549 opj_read_bytes(p_header_data,&l_tmp,1);
6552 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6560 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6563 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6564 opj_mct_data_t * l_deco_array, * l_offset_array;
6565 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6566 OPJ_UINT32 l_nb_elem;
6567 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6568 opj_tccp_t * l_tccp;
6571 assert(p_tcp != 00);
6573 l_mcc_record = p_tcp->m_mcc_records;
6575 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6576 if (l_mcc_record->m_index == p_index) {
6581 if (i==p_tcp->m_nb_mcc_records) {
6582 /** element discarded **/
6586 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6587 /** do not support number of comps != image */
6591 l_deco_array = l_mcc_record->m_decorrelation_array;
6594 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6595 if (l_deco_array->m_data_size != l_data_size) {
6599 l_nb_elem = p_image->numcomps * p_image->numcomps;
6600 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6601 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6603 if (! p_tcp->m_mct_decoding_matrix ) {
6607 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);
6610 l_offset_array = l_mcc_record->m_offset_array;
6612 if (l_offset_array) {
6613 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6614 if (l_offset_array->m_data_size != l_data_size) {
6618 l_nb_elem = p_image->numcomps;
6619 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6620 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6622 if (! l_offset_data ) {
6626 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6628 l_tccp = p_tcp->tccps;
6629 l_current_offset_data = l_offset_data;
6631 for (i=0;i<p_image->numcomps;++i) {
6632 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6636 opj_free(l_offset_data);
6643 * Writes the CBD marker (Component bit depth definition)
6645 * @param p_stream the stream to write data to.
6646 * @param p_j2k J2K codec.
6647 * @param p_manager the user event manager.
6649 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6650 struct opj_stream_private *p_stream,
6651 struct opj_event_mgr * p_manager )
6654 OPJ_UINT32 l_cbd_size;
6655 OPJ_BYTE * l_current_data = 00;
6656 opj_image_t *l_image = 00;
6657 opj_image_comp_t * l_comp = 00;
6660 assert(p_j2k != 00);
6661 assert(p_manager != 00);
6662 assert(p_stream != 00);
6664 l_image = p_j2k->m_private_image;
6665 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6667 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6668 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6669 = (OPJ_BYTE*)opj_realloc(
6670 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6673 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6677 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6680 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6682 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6683 l_current_data += 2;
6685 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6686 l_current_data += 2;
6688 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6691 l_comp = l_image->comps;
6693 for (i=0;i<l_image->numcomps;++i) {
6694 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6700 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) {
6708 * Reads a CBD marker (Component bit depth definition)
6709 * @param p_header_data the data contained in the CBD box.
6710 * @param p_j2k the jpeg2000 codec.
6711 * @param p_header_size the size of the data contained in the CBD marker.
6712 * @param p_manager the user event manager.
6714 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6715 OPJ_BYTE * p_header_data,
6716 OPJ_UINT32 p_header_size,
6717 struct opj_event_mgr * p_manager)
6719 OPJ_UINT32 l_nb_comp,l_num_comp;
6720 OPJ_UINT32 l_comp_def;
6722 opj_image_comp_t * l_comp = 00;
6725 assert(p_header_data != 00);
6726 assert(p_j2k != 00);
6727 assert(p_manager != 00);
6729 l_num_comp = p_j2k->m_private_image->numcomps;
6731 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6732 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6736 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6739 if (l_nb_comp != l_num_comp) {
6740 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6744 l_comp = p_j2k->m_private_image->comps;
6745 for (i=0;i<l_num_comp;++i) {
6746 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6748 l_comp->sgnd = (l_comp_def>>7) & 1;
6749 l_comp->prec = (l_comp_def&0x7f) + 1;
6757 /* ----------------------------------------------------------------------- */
6758 /* J2K / JPT decoder interface */
6759 /* ----------------------------------------------------------------------- */
6763 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6766 if(j2k->tile_len != NULL) {
6767 opj_free(j2k->tile_len);
6769 if(j2k->tile_data != NULL) {
6770 opj_free(j2k->tile_data);
6772 if(j2k->default_tcp != NULL) {
6773 opj_tcp_t *default_tcp = j2k->default_tcp;
6774 if(default_tcp->ppt_data_first != NULL) {
6775 opj_free(default_tcp->ppt_data_first);
6777 if(j2k->default_tcp->tccps != NULL) {
6778 opj_free(j2k->default_tcp->tccps);
6780 opj_free(j2k->default_tcp);
6782 if(j2k->cp != NULL) {
6783 opj_cp_t *cp = j2k->cp;
6784 if(cp->tcps != NULL) {
6785 for(i = 0; i < cp->tw * cp->th; i++) {
6786 if(cp->tcps[i].ppt_data_first != NULL) {
6787 opj_free(cp->tcps[i].ppt_data_first);
6789 if(cp->tcps[i].tccps != NULL) {
6790 opj_free(cp->tcps[i].tccps);
6795 if(cp->ppm_data_first != NULL) {
6796 opj_free(cp->ppm_data_first);
6798 if(cp->tileno != NULL) {
6799 opj_free(cp->tileno);
6801 if(cp->comment != NULL) {
6802 opj_free(cp->comment);
6811 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6813 if(j2k && parameters) {
6814 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6815 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6818 j2k->m_cp.correct = parameters->jpwl_correct;
6819 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6820 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6821 #endif /* USE_JPWL */
6828 * Read a JPT-stream and decode file
6831 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6832 opj_image_t *image = NULL;
6833 opj_jpt_msg_header_t header;
6835 opj_common_ptr cinfo = j2k->cinfo;
6837 OPJ_ARG_NOT_USED(cstr_info);
6841 /* create an empty image */
6842 image = opj_image_create0();
6845 j2k->state = J2K_STATE_MHSOC;
6847 /* Initialize the header */
6848 jpt_init_msg_header(&header);
6849 /* Read the first header of the message */
6850 jpt_read_msg_header(cinfo, cio, &header);
6852 position = cio_tell(cio);
6853 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6854 opj_image_destroy(image);
6855 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6860 opj_dec_mstabent_t *e = NULL;
6863 if (!cio_numbytesleft(cio)) {
6867 /* data-bin read -> need to read a new header */
6868 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6869 jpt_read_msg_header(cinfo, cio, &header);
6870 position = cio_tell(cio);
6871 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6872 opj_image_destroy(image);
6873 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6878 id = cio_read(cio, 2);
6879 if (id >> 8 != 0xff) {
6880 opj_image_destroy(image);
6881 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6884 e = j2k_dec_mstab_lookup(id);
6885 if (!(j2k->state & e->states)) {
6886 opj_image_destroy(image);
6887 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6893 if (j2k->state == J2K_STATE_MT) {
6896 if (j2k->state == J2K_STATE_NEOC) {
6900 if (j2k->state == J2K_STATE_NEOC) {
6904 if (j2k->state != J2K_STATE_MT) {
6905 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6911 /* ----------------------------------------------------------------------- */
6912 /* J2K encoder interface */
6913 /* ----------------------------------------------------------------------- */
6915 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6916 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6923 opj_j2k_v2_t* j2k_create_compress_v2(void)
6925 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6930 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6932 l_j2k->m_is_decoder = 0;
6933 l_j2k->m_cp.m_is_decoder = 0;
6935 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6936 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6941 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6943 /* validation list creation*/
6944 l_j2k->m_validation_list = opj_procedure_list_create();
6945 if (! l_j2k->m_validation_list) {
6950 /* execution list creation*/
6951 l_j2k->m_procedure_list = opj_procedure_list_create();
6952 if (! l_j2k->m_procedure_list) {
6960 void j2k_destroy_compress(opj_j2k_t *j2k) {
6964 if(j2k->cp != NULL) {
6965 opj_cp_t *cp = j2k->cp;
6968 opj_free(cp->comment);
6971 opj_free(cp->matrice);
6973 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6974 opj_free(cp->tcps[tileno].tccps);
6983 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6985 int tileno, numpocs_tile;
6986 opj_cp_t *cp = NULL;
6988 if(!j2k || !parameters || ! image) {
6992 /* create and initialize the coding parameters structure */
6993 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6995 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6998 /* set default values for cp */
7003 copy user encoding parameters
7005 cp->cinema = parameters->cp_cinema;
7006 cp->max_comp_size = parameters->max_comp_size;
7007 cp->rsiz = parameters->cp_rsiz;
7008 cp->disto_alloc = parameters->cp_disto_alloc;
7009 cp->fixed_alloc = parameters->cp_fixed_alloc;
7010 cp->fixed_quality = parameters->cp_fixed_quality;
7012 /* mod fixed_quality */
7013 if(parameters->cp_matrice) {
7014 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7015 cp->matrice = (int *) opj_malloc(array_size);
7016 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7020 cp->tdx = parameters->cp_tdx;
7021 cp->tdy = parameters->cp_tdy;
7024 cp->tx0 = parameters->cp_tx0;
7025 cp->ty0 = parameters->cp_ty0;
7027 /* comment string */
7028 if(parameters->cp_comment) {
7029 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7031 strcpy(cp->comment, parameters->cp_comment);
7036 calculate other encoding parameters
7039 if (parameters->tile_size_on) {
7040 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7041 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7043 cp->tdx = image->x1 - cp->tx0;
7044 cp->tdy = image->y1 - cp->ty0;
7047 if(parameters->tp_on){
7048 cp->tp_flag = parameters->tp_flag;
7053 for(i=0;i<image->numcomps ;i++){
7054 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7060 calculate JPWL encoding parameters
7063 if (parameters->jpwl_epc_on) {
7067 cp->epc_on = OPJ_TRUE;
7068 cp->info_on = OPJ_FALSE; /* no informative technique */
7071 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7072 cp->epb_on = OPJ_TRUE;
7074 cp->hprot_MH = parameters->jpwl_hprot_MH;
7075 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7076 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7077 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7079 /* if tile specs are not specified, copy MH specs */
7080 if (cp->hprot_TPH[0] == -1) {
7081 cp->hprot_TPH_tileno[0] = 0;
7082 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7084 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7085 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7086 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7087 cp->pprot[i] = parameters->jpwl_pprot[i];
7091 /* set ESD writing */
7092 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7093 cp->esd_on = OPJ_TRUE;
7095 cp->sens_size = parameters->jpwl_sens_size;
7096 cp->sens_addr = parameters->jpwl_sens_addr;
7097 cp->sens_range = parameters->jpwl_sens_range;
7099 cp->sens_MH = parameters->jpwl_sens_MH;
7100 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7101 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7102 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7106 /* always set RED writing to false: we are at the encoder */
7107 cp->red_on = OPJ_FALSE;
7110 cp->epc_on = OPJ_FALSE;
7112 #endif /* USE_JPWL */
7115 /* initialize the mutiple tiles */
7116 /* ---------------------------- */
7117 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7119 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7120 opj_tcp_t *tcp = &cp->tcps[tileno];
7121 tcp->numlayers = parameters->tcp_numlayers;
7122 assert ( tcp->numlayers >= 0 );
7123 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7125 if (cp->fixed_quality) {
7126 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7128 tcp->rates[j] = parameters->tcp_rates[j];
7130 if (cp->fixed_quality) { /* add fixed_quality */
7131 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7133 tcp->rates[j] = parameters->tcp_rates[j];
7137 tcp->csty = parameters->csty;
7138 tcp->prg = parameters->prog_order;
7139 tcp->mct = parameters->tcp_mct;
7143 if (parameters->numpocs) {
7144 /* initialisation of POC */
7146 assert( parameters->numpocs >= 0 );
7147 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7148 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7149 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7150 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7151 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7152 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7153 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7154 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7155 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7156 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7160 tcp->numpocs = numpocs_tile -1 ;
7165 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7167 for (i = 0; i < image->numcomps; i++) {
7168 opj_tccp_t *tccp = &tcp->tccps[i];
7169 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7170 tccp->numresolutions = parameters->numresolution;
7171 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7172 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7173 tccp->cblksty = parameters->mode;
7174 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7175 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7177 assert(parameters->roi_compno >= 0);
7178 if (i == (OPJ_UINT32)parameters->roi_compno) {
7179 tccp->roishift = parameters->roi_shift;
7184 if(parameters->cp_cinema)
7186 /*Precinct size for lowest frequency subband=128*/
7189 /*Precinct size at all other resolutions = 256*/
7190 for (j = 1; j < tccp->numresolutions; j++) {
7195 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7197 assert(tccp->numresolutions > 0);
7198 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7199 if (p < parameters->res_spec) {
7201 if (parameters->prcw_init[p] < 1) {
7204 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7207 if (parameters->prch_init[p] < 1) {
7210 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7214 int res_spec = parameters->res_spec;
7215 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7216 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7218 if (size_prcw < 1) {
7221 tccp->prcw[j] = int_floorlog2(size_prcw);
7224 if (size_prch < 1) {
7227 tccp->prch[j] = int_floorlog2(size_prch);
7231 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7234 for (j = 0; j < tccp->numresolutions; j++) {
7241 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7246 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7247 opj_cparameters_t *parameters,
7249 struct opj_event_mgr * p_manager)
7251 OPJ_UINT32 i, j, tileno, numpocs_tile;
7252 opj_cp_v2_t *cp = 00;
7255 if(!p_j2k || !parameters || ! image) {
7259 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7260 cp = &(p_j2k->m_cp);
7262 /* set default values for cp */
7267 copy user encoding parameters
7269 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7270 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7271 cp->rsiz = parameters->cp_rsiz;
7272 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7273 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7274 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7276 /* mod fixed_quality */
7277 if (parameters->cp_matrice) {
7278 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7279 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7280 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7284 cp->tdx = parameters->cp_tdx;
7285 cp->tdy = parameters->cp_tdy;
7288 cp->tx0 = parameters->cp_tx0;
7289 cp->ty0 = parameters->cp_ty0;
7291 /* comment string */
7292 if(parameters->cp_comment) {
7293 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7295 strcpy(cp->comment, parameters->cp_comment);
7300 calculate other encoding parameters
7303 if (parameters->tile_size_on) {
7304 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7305 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7307 cp->tdx = image->x1 - cp->tx0;
7308 cp->tdy = image->y1 - cp->ty0;
7311 if (parameters->tp_on) {
7312 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7313 cp->m_specific_param.m_enc.m_tp_on = 1;
7318 calculate JPWL encoding parameters
7321 if (parameters->jpwl_epc_on) {
7325 cp->epc_on = OPJ_TRUE;
7326 cp->info_on = OPJ_FALSE; /* no informative technique */
7329 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7330 cp->epb_on = OPJ_TRUE;
7332 cp->hprot_MH = parameters->jpwl_hprot_MH;
7333 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7334 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7335 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7337 /* if tile specs are not specified, copy MH specs */
7338 if (cp->hprot_TPH[0] == -1) {
7339 cp->hprot_TPH_tileno[0] = 0;
7340 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7342 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7343 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7344 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7345 cp->pprot[i] = parameters->jpwl_pprot[i];
7349 /* set ESD writing */
7350 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7351 cp->esd_on = OPJ_TRUE;
7353 cp->sens_size = parameters->jpwl_sens_size;
7354 cp->sens_addr = parameters->jpwl_sens_addr;
7355 cp->sens_range = parameters->jpwl_sens_range;
7357 cp->sens_MH = parameters->jpwl_sens_MH;
7358 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7359 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7360 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7364 /* always set RED writing to false: we are at the encoder */
7365 cp->red_on = OPJ_FALSE;
7368 cp->epc_on = OPJ_FALSE;
7370 #endif /* USE_JPWL */
7373 /* initialize the mutiple tiles */
7374 /* ---------------------------- */
7375 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7376 if (parameters->numpocs) {
7377 /* initialisation of POC */
7378 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7382 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7383 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7384 tcp->numlayers = parameters->tcp_numlayers;
7386 for (j = 0; j < tcp->numlayers; j++) {
7387 if(cp->m_specific_param.m_enc.m_cinema){
7388 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7389 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7391 tcp->rates[j] = parameters->tcp_rates[j];
7393 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7394 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7396 tcp->rates[j] = parameters->tcp_rates[j];
7401 tcp->csty = parameters->csty;
7402 tcp->prg = parameters->prog_order;
7403 tcp->mct = parameters->tcp_mct;
7408 if (parameters->numpocs) {
7409 /* initialisation of POC */
7412 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7413 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7414 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7416 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7417 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7418 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7419 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7420 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7421 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7422 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7428 tcp->numpocs = numpocs_tile -1 ;
7433 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7435 if (parameters->mct_data) {
7437 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7440 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7441 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7442 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7443 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7446 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7447 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7448 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7450 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7451 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7453 tcp->mct_norms = (OPJ_FLOAT64*)
7454 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7456 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7459 for (i = 0; i < image->numcomps; i++) {
7460 opj_tccp_t *tccp = &tcp->tccps[i];
7461 tccp->m_dc_level_shift = l_dc_shift[i];
7464 j2k_setup_mct_encoding(tcp,image);
7468 for (i = 0; i < image->numcomps; i++) {
7469 opj_tccp_t *tccp = &tcp->tccps[i];
7470 opj_image_comp_t * l_comp = &(image->comps[i]);
7472 if (! l_comp->sgnd) {
7473 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7478 for (i = 0; i < image->numcomps; i++) {
7479 opj_tccp_t *tccp = &tcp->tccps[i];
7481 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7482 tccp->numresolutions = parameters->numresolution;
7483 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7484 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7485 tccp->cblksty = parameters->mode;
7486 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7487 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7490 if (i == parameters->roi_compno) {
7491 tccp->roishift = parameters->roi_shift;
7496 if(parameters->cp_cinema) {
7497 //Precinct size for lowest frequency subband=128
7500 //Precinct size at all other resolutions = 256
7501 for (j = 1; j < tccp->numresolutions; j++) {
7506 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7507 OPJ_INT32 p = 0, it_res;
7508 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7509 if (p < parameters->res_spec) {
7511 if (parameters->prcw_init[p] < 1) {
7512 tccp->prcw[it_res] = 1;
7514 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7517 if (parameters->prch_init[p] < 1) {
7518 tccp->prch[it_res] = 1;
7520 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7524 int res_spec = parameters->res_spec;
7525 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7526 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7528 if (size_prcw < 1) {
7529 tccp->prcw[it_res] = 1;
7531 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7534 if (size_prch < 1) {
7535 tccp->prch[it_res] = 1;
7537 tccp->prch[it_res] = int_floorlog2(size_prch);
7541 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7544 for (j = 0; j < tccp->numresolutions; j++) {
7551 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7555 if (parameters->mct_data) {
7556 opj_free(parameters->mct_data);
7557 parameters->mct_data = 00;
7562 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7565 opj_cp_t *cp = NULL;
7567 opj_tcd_t *tcd = NULL; /* TCD component */
7575 j2k->cstr_info = cstr_info;
7578 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7579 cstr_info->image_w = image->x1 - image->x0;
7580 cstr_info->image_h = image->y1 - image->y0;
7581 cstr_info->prog = (&cp->tcps[0])->prg;
7582 cstr_info->tw = cp->tw;
7583 cstr_info->th = cp->th;
7584 cstr_info->tile_x = cp->tdx; /* new version parser */
7585 cstr_info->tile_y = cp->tdy; /* new version parser */
7586 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7587 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7588 cstr_info->numcomps = image->numcomps;
7589 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7590 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7591 for (compno=0; compno < image->numcomps; compno++) {
7592 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7594 cstr_info->D_max = 0.0; /* ADD Marcela */
7595 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7596 cstr_info->maxmarknum = 100;
7597 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7598 cstr_info->marknum = 0;
7608 for (compno = 1; compno < image->numcomps; compno++) {
7609 j2k_write_coc(j2k, compno);
7610 j2k_write_qcc(j2k, compno);
7614 for (compno = 0; compno < image->numcomps; compno++) {
7615 opj_tcp_t *tcp = &cp->tcps[0];
7616 if (tcp->tccps[compno].roishift)
7617 j2k_write_rgn(j2k, compno, 0);
7619 if (cp->comment != NULL) {
7623 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7627 if (cp->cinema == CINEMA4K_24) {
7632 /* uncomment only for testing JPSEC marker writing */
7633 /* j2k_write_sec(j2k); */
7637 cstr_info->main_head_end = cio_tell(cio) - 1;
7640 /**** Main Header ENDS here ***/
7642 /* create the tile encoder */
7643 tcd = tcd_create(j2k->cinfo);
7645 /* encode each tile */
7646 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7650 int acc_pack_num = 0;
7654 opj_tcp_t *tcp = &cp->tcps[tileno];
7655 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7657 j2k->curtileno = tileno;
7658 j2k->cur_tp_num = 0;
7659 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7660 /* initialisation before tile encoding */
7662 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7664 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7669 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7670 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7671 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7672 cstr_info->tile[j2k->curtileno].marknum = 0;
7676 for(pino = 0; pino <= tcp->numpocs; pino++) {
7680 /*Get number of tile parts*/
7681 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7682 tcd->tp_pos = cp->tp_pos;
7684 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7685 j2k->tp_num = tilepartno;
7688 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7689 cio_tell(cio) + j2k->pos_correction;
7693 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7694 for (compno = 1; compno < image->numcomps; compno++) {
7695 j2k_write_coc(j2k, compno);
7696 j2k_write_qcc(j2k, compno);
7698 if (cp->tcps[tileno].numpocs) {
7705 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7706 cio_tell(cio) + j2k->pos_correction + 1;
7709 j2k_write_sod(j2k, tcd);
7713 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7714 cio_tell(cio) + j2k->pos_correction - 1;
7715 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7717 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7718 cstr_info->packno - acc_pack_num;
7719 acc_pack_num = cstr_info->packno;
7727 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7732 if (tile->PPT) { // BAD PPT !!!
7735 PPT_file=fopen("PPT","rb");
7736 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7737 for (i=0;i<tile->len_ppt;i++) {
7739 fread(&elmt, 1, 1, PPT_file);
7740 fwrite(&elmt,1,1,f);
7749 /* destroy the tile encoder */
7750 tcd_free_encode(tcd);
7753 opj_free(j2k->cur_totnum_tp);
7758 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7760 /* The following adjustment is done to adjust the codestream size */
7761 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7762 /* the first bunch of bytes is not in the codestream */
7763 cstr_info->codestream_size -= cstr_info->main_head_start;
7769 preparation of JPWL marker segments
7773 /* encode according to JPWL */
7774 jpwl_encode(j2k, cio, image);
7777 #endif /* USE_JPWL */
7782 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7784 assert(cstr_info != 00);
7786 /* expand the list? */
7787 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7788 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7789 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7792 /* add the marker */
7793 cstr_info->marker[cstr_info->marknum].type = type;
7794 cstr_info->marker[cstr_info->marknum].pos = pos;
7795 cstr_info->marker[cstr_info->marknum].len = len;
7796 cstr_info->marknum++;
7800 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7802 assert(cstr_index != 00);
7804 /* expand the list? */
7805 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7806 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7807 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7810 /* add the marker */
7811 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7812 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7813 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7814 cstr_index->marknum++;
7818 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7820 opj_marker_info_t *marker;
7822 assert(cstr_info != 00);
7824 /* expand the list? */
7825 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7826 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7827 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7830 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7832 /* add the marker */
7833 marker->type = type;
7836 cstr_info->tile[tileno].marknum++;
7839 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)
7841 assert(cstr_index != 00);
7842 assert(cstr_index->tile_index != 00);
7844 /* expand the list? */
7845 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7846 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7847 cstr_index->tile_index[tileno].marker =
7848 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7849 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7852 /* add the marker */
7853 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7854 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7855 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7856 cstr_index->tile_index[tileno].marknum++;
7858 if (type == J2K_MS_SOT) {
7859 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7861 if (cstr_index->tile_index[tileno].tp_index)
7862 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7869 * -----------------------------------------------------------------------
7870 * -----------------------------------------------------------------------
7871 * -----------------------------------------------------------------------
7875 * Ends the decompression procedures and possibiliy add data to be read after the
7878 opj_bool j2k_end_decompress(
7879 opj_j2k_v2_t *p_j2k,
7880 opj_stream_private_t *p_stream,
7881 opj_event_mgr_t * p_manager)
7890 * Reads a jpeg2000 codestream header structure.
7893 * @param p_stream the stream to read data from.
7894 * @param p_j2k the jpeg2000 codec.
7895 * @param p_manager the user event manager.
7897 * @return true if the box is valid.
7899 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7900 opj_j2k_v2_t* p_j2k,
7901 opj_image_t** p_image,
7902 struct opj_event_mgr* p_manager )
7905 assert(p_j2k != 00);
7906 assert(p_stream != 00);
7907 assert(p_manager != 00);
7909 /* create an empty image header */
7910 p_j2k->m_private_image = opj_image_create0();
7911 if (! p_j2k->m_private_image) {
7915 /* customization of the validation */
7916 j2k_setup_decoding_validation(p_j2k);
7918 /* validation of the parameters codec */
7919 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7920 opj_image_destroy(p_j2k->m_private_image);
7921 p_j2k->m_private_image = NULL;
7925 /* customization of the encoding */
7926 j2k_setup_header_reading(p_j2k);
7929 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7930 opj_image_destroy(p_j2k->m_private_image);
7931 p_j2k->m_private_image = NULL;
7935 *p_image = opj_image_create0();
7940 /* Copy codestream image information to the output image */
7941 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7943 /*Allocate and initialize some elements of codestrem index*/
7944 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7952 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7954 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7957 assert(p_j2k != 00);
7959 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7961 /* DEVELOPER CORNER, add your custom procedures */
7962 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7967 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7968 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7970 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7973 assert(p_j2k != 00);
7975 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7976 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7977 /* DEVELOPER CORNER, add your custom validation procedure */
7983 * The mct encoding validation procedure.
7985 * @param p_j2k the jpeg2000 codec to validate.
7986 * @param p_stream the input stream to validate.
7987 * @param p_manager the user event manager.
7989 * @return true if the parameters are correct.
7991 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7992 opj_stream_private_t *p_stream,
7993 opj_event_mgr_t * p_manager )
7995 opj_bool l_is_valid = OPJ_TRUE;
7999 assert(p_j2k != 00);
8000 assert(p_stream != 00);
8001 assert(p_manager != 00);
8003 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8004 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8005 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8007 for (i=0;i<l_nb_tiles;++i) {
8008 if (l_tcp->mct == 2) {
8009 opj_tccp_t * l_tccp = l_tcp->tccps;
8010 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8012 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8013 l_is_valid &= ! (l_tccp->qmfbid & 1);
8024 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8027 OPJ_UINT32 l_indix = 1;
8028 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8029 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8030 OPJ_UINT32 l_mct_size,l_nb_elem;
8031 OPJ_FLOAT32 * l_data, * l_current_data;
8032 opj_tccp_t * l_tccp;
8035 assert(p_tcp != 00);
8037 if (p_tcp->mct != 2) {
8041 if (p_tcp->m_mct_decoding_matrix) {
8042 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8043 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8045 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));
8046 if (! p_tcp->m_mct_records) {
8049 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8051 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8053 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8055 if (l_mct_deco_data->m_data) {
8056 opj_free(l_mct_deco_data->m_data);
8057 l_mct_deco_data->m_data = 00;
8060 l_mct_deco_data->m_index = l_indix++;
8061 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8062 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8063 l_nb_elem = p_image->numcomps * p_image->numcomps;
8064 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8065 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8067 if (! l_mct_deco_data->m_data) {
8071 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);
8073 l_mct_deco_data->m_data_size = l_mct_size;
8074 ++p_tcp->m_nb_mct_records;
8077 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8078 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8079 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));
8081 if (! p_tcp->m_mct_records) {
8085 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8086 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8088 if (l_mct_deco_data) {
8089 l_mct_deco_data = l_mct_offset_data - 1;
8093 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8095 if (l_mct_offset_data->m_data) {
8096 opj_free(l_mct_offset_data->m_data);
8097 l_mct_offset_data->m_data = 00;
8100 l_mct_offset_data->m_index = l_indix++;
8101 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8102 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8103 l_nb_elem = p_image->numcomps;
8104 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8105 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8107 if (! l_mct_offset_data->m_data) {
8111 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8113 opj_free(l_mct_offset_data->m_data);
8114 l_mct_offset_data->m_data = 00;
8118 l_tccp = p_tcp->tccps;
8119 l_current_data = l_data;
8121 for (i=0;i<l_nb_elem;++i) {
8122 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8126 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8130 l_mct_offset_data->m_data_size = l_mct_size;
8132 ++p_tcp->m_nb_mct_records;
8134 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8135 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8136 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8137 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8139 if (! p_tcp->m_mcc_records) {
8142 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8143 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));
8147 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8148 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8149 l_mcc_data->m_is_irreversible = 1;
8150 l_mcc_data->m_nb_comps = p_image->numcomps;
8151 l_mcc_data->m_index = l_indix++;
8152 l_mcc_data->m_offset_array = l_mct_offset_data;
8153 ++p_tcp->m_nb_mcc_records;
8159 * Builds the cp decoder parameters to use to decode tile.
8161 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8162 opj_stream_private_t *p_stream,
8163 opj_event_mgr_t * p_manager )
8165 /* add here initialization of cp
8166 copy paste of setup_decoder */
8174 * Builds the cp encoder parameters to use to encode tile.
8176 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8177 opj_stream_private_t *p_stream,
8178 opj_event_mgr_t * p_manager )
8180 /* add here initialization of cp
8181 copy paste of setup_encoder */
8189 * The default encoding validation procedure without any extension.
8191 * @param p_j2k the jpeg2000 codec to validate.
8192 * @param p_stream the input stream to validate.
8193 * @param p_manager the user event manager.
8195 * @return true if the parameters are correct.
8197 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8198 opj_stream_private_t *p_stream,
8199 opj_event_mgr_t * p_manager )
8201 opj_bool l_is_valid = OPJ_TRUE;
8204 assert(p_j2k != 00);
8205 assert(p_stream != 00);
8206 assert(p_manager != 00);
8208 /* STATE checking */
8209 /* make sure the state is at 0 */
8210 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8212 /* POINTER validation */
8213 /* make sure a p_j2k codec is present */
8214 l_is_valid &= (p_j2k->m_procedure_list != 00);
8215 /* make sure a validation list is present */
8216 l_is_valid &= (p_j2k->m_validation_list != 00);
8218 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8219 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8223 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8224 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8228 /* PARAMETER VALIDATION */
8233 * The default decoding validation procedure without any extension.
8235 * @param p_j2k the jpeg2000 codec to validate.
8236 * @param p_stream the input stream to validate.
8237 * @param p_manager the user event manager.
8239 * @return true if the parameters are correct.
8241 opj_bool j2k_decoding_validation (
8242 opj_j2k_v2_t *p_j2k,
8243 opj_stream_private_t *p_stream,
8244 opj_event_mgr_t * p_manager
8247 opj_bool l_is_valid = OPJ_TRUE;
8250 assert(p_j2k != 00);
8251 assert(p_stream != 00);
8252 assert(p_manager != 00);
8255 /* STATE checking */
8256 /* make sure the state is at 0 */
8258 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8260 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8262 /* POINTER validation */
8263 /* make sure a p_j2k codec is present */
8264 /* make sure a procedure list is present */
8265 l_is_valid &= (p_j2k->m_procedure_list != 00);
8266 /* make sure a validation list is present */
8267 l_is_valid &= (p_j2k->m_validation_list != 00);
8269 /* PARAMETER VALIDATION */
8273 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8274 struct opj_stream_private *p_stream,
8275 struct opj_event_mgr * p_manager)
8277 OPJ_UINT32 l_current_marker;
8278 OPJ_UINT32 l_marker_size;
8279 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8282 assert(p_stream != 00);
8283 assert(p_j2k != 00);
8284 assert(p_manager != 00);
8286 /* We enter in the main header */
8287 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8289 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8290 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8291 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8295 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8296 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8297 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8301 /* Read 2 bytes as the new marker ID */
8302 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8304 /* Try to read until the SOT is detected */
8305 while (l_current_marker != J2K_MS_SOT) {
8307 /* Check if the current marker ID is valid */
8308 if (l_current_marker < 0xff00) {
8309 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8313 /* Get the marker handler from the marker ID */
8314 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8316 /* Manage case where marker is unknown */
8317 if (l_marker_handler->id == J2K_MS_UNK) {
8318 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8319 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8323 if (l_current_marker == J2K_MS_SOT)
8324 break; /* SOT marker is detected main header is completely read */
8325 else /* Get the marker handler from the marker ID */
8326 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8329 /* Check if the marker is known and if it is the right place to find it */
8330 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8331 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8335 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8336 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8337 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8341 /* read 2 bytes as the marker size */
8342 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8343 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8345 /* Check if the marker size is compatible with the header data size */
8346 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8347 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8348 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8349 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8352 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8355 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8356 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) {
8357 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8361 /* Read the marker segment with the correct marker handler */
8362 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8363 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8367 /* Add the marker to the codestream index*/
8368 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8369 l_marker_handler->id,
8370 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8371 l_marker_size + 4 );
8373 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8374 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8375 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8379 /* read 2 bytes as the new marker ID */
8380 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8383 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8385 /* Position of the last element if the main header */
8386 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8388 /* Next step: read a tile-part header */
8389 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8395 * Excutes the given procedures on the given codec.
8397 * @param p_procedure_list the list of procedures to execute
8398 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8399 * @param p_stream the stream to execute the procedures on.
8400 * @param p_manager the user manager.
8402 * @return true if all the procedures were successfully executed.
8404 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8405 opj_procedure_list_t * p_procedure_list,
8406 opj_stream_private_t *p_stream,
8407 opj_event_mgr_t * p_manager )
8409 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8410 opj_bool l_result = OPJ_TRUE;
8411 OPJ_UINT32 l_nb_proc, i;
8414 assert(p_procedure_list != 00);
8415 assert(p_j2k != 00);
8416 assert(p_stream != 00);
8417 assert(p_manager != 00);
8420 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8421 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);
8423 for (i=0;i<l_nb_proc;++i) {
8424 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8428 /* and clear the procedure list at the end.*/
8429 opj_procedure_list_clear(p_procedure_list);
8434 opj_bool j2k_copy_default_tcp_and_create_tcd
8436 opj_j2k_v2_t * p_j2k,
8437 opj_stream_private_t *p_stream,
8438 opj_event_mgr_t * p_manager
8441 opj_tcp_v2_t * l_tcp = 00;
8442 opj_tcp_v2_t * l_default_tcp = 00;
8443 OPJ_UINT32 l_nb_tiles;
8445 opj_tccp_t *l_current_tccp = 00;
8446 OPJ_UINT32 l_tccp_size;
8447 OPJ_UINT32 l_mct_size;
8448 opj_image_t * l_image;
8449 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8450 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8451 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8452 OPJ_UINT32 l_offset;
8455 assert(p_j2k != 00);
8456 assert(p_stream != 00);
8457 assert(p_manager != 00);
8459 l_image = p_j2k->m_private_image;
8460 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8461 l_tcp = p_j2k->m_cp.tcps;
8462 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8463 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8464 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8467 for (i=0; i<l_nb_tiles; ++i) {
8468 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8469 l_current_tccp = l_tcp->tccps;
8470 /*Copy default coding parameters into the current tile coding parameters*/
8471 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8472 /* Initialize some values of the current tile coding parameters*/
8474 l_tcp->ppt_data = 00;
8475 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8476 l_tcp->tccps = l_current_tccp;
8478 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8479 if (l_default_tcp->m_mct_decoding_matrix) {
8480 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8481 if (! l_tcp->m_mct_decoding_matrix ) {
8484 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8487 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8488 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8489 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8490 if (! l_tcp->m_mct_records) {
8493 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8495 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8496 l_src_mct_rec = l_default_tcp->m_mct_records;
8497 l_dest_mct_rec = l_tcp->m_mct_records;
8499 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8501 if (l_src_mct_rec->m_data) {
8503 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8504 if(! l_dest_mct_rec->m_data) {
8507 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8514 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8515 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8516 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8517 if (! l_tcp->m_mcc_records) {
8520 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8522 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8523 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8524 l_dest_mcc_rec = l_tcp->m_mcc_records;
8526 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8528 if (l_src_mcc_rec->m_decorrelation_array) {
8529 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8530 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8533 if (l_src_mcc_rec->m_offset_array) {
8534 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8535 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8542 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8543 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8545 /* Move to next tile cp*/
8549 /* Create the current tile decoder*/
8550 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8551 if (! p_j2k->m_tcd ) {
8555 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8556 tcd_destroy_v2(p_j2k->m_tcd);
8558 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8566 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8567 * with the marker value.
8568 * @param p_id Marker value to look up
8570 * @return the handler associated with the id.
8572 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8574 const opj_dec_memory_marker_handler_t *e;
8575 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8576 if (e->id == p_id) {
8577 break; /* we find a handler corresponding to the marker ID*/
8585 * Destroys a jpeg2000 codec.
8587 * @param p_j2k the jpeg20000 structure to destroy.
8589 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8595 if (p_j2k->m_is_decoder) {
8597 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8598 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8599 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8600 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8603 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8604 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8605 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8606 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8611 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8612 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8613 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8616 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8617 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8618 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8619 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8622 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8623 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8624 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8625 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8629 tcd_destroy_v2(p_j2k->m_tcd);
8631 j2k_cp_destroy(&(p_j2k->m_cp));
8632 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8634 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8635 p_j2k->m_procedure_list = 00;
8637 opj_procedure_list_destroy(p_j2k->m_validation_list);
8638 p_j2k->m_procedure_list = 00;
8640 j2k_destroy_cstr_index(p_j2k->cstr_index);
8641 p_j2k->cstr_index = NULL;
8643 opj_image_destroy(p_j2k->m_private_image);
8644 p_j2k->m_private_image = NULL;
8646 opj_image_destroy(p_j2k->m_output_image);
8647 p_j2k->m_output_image = NULL;
8652 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8656 if (p_cstr_ind->marker) {
8657 opj_free(p_cstr_ind->marker);
8658 p_cstr_ind->marker = NULL;
8661 if (p_cstr_ind->tile_index) {
8662 OPJ_UINT32 it_tile = 0;
8664 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8666 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8667 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8668 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8671 if(p_cstr_ind->tile_index[it_tile].tp_index){
8672 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8673 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8676 if(p_cstr_ind->tile_index[it_tile].marker){
8677 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8678 p_cstr_ind->tile_index[it_tile].marker = NULL;
8683 opj_free( p_cstr_ind->tile_index);
8684 p_cstr_ind->tile_index = NULL;
8687 opj_free(p_cstr_ind);
8694 * Destroys a tile coding parameter structure.
8696 * @param p_tcp the tile coding parameter to destroy.
8698 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8704 if (p_tcp->ppt_buffer != 00) {
8705 opj_free(p_tcp->ppt_buffer);
8706 p_tcp->ppt_buffer = 00;
8709 if (p_tcp->tccps != 00) {
8710 opj_free(p_tcp->tccps);
8714 if (p_tcp->m_mct_coding_matrix != 00) {
8715 opj_free(p_tcp->m_mct_coding_matrix);
8716 p_tcp->m_mct_coding_matrix = 00;
8719 if (p_tcp->m_mct_decoding_matrix != 00) {
8720 opj_free(p_tcp->m_mct_decoding_matrix);
8721 p_tcp->m_mct_decoding_matrix = 00;
8724 if (p_tcp->m_mcc_records) {
8725 opj_free(p_tcp->m_mcc_records);
8726 p_tcp->m_mcc_records = 00;
8727 p_tcp->m_nb_max_mcc_records = 0;
8728 p_tcp->m_nb_mcc_records = 0;
8731 if (p_tcp->m_mct_records) {
8732 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8735 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8736 if (l_mct_data->m_data) {
8737 opj_free(l_mct_data->m_data);
8738 l_mct_data->m_data = 00;
8744 opj_free(p_tcp->m_mct_records);
8745 p_tcp->m_mct_records = 00;
8748 if (p_tcp->mct_norms != 00) {
8749 opj_free(p_tcp->mct_norms);
8750 p_tcp->mct_norms = 00;
8753 j2k_tcp_data_destroy(p_tcp);
8758 * Destroys the data inside a tile coding parameter structure.
8760 * @param p_tcp the tile coding parameter which contain data to destroy.
8762 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8764 if (p_tcp->m_data) {
8765 opj_free(p_tcp->m_data);
8766 p_tcp->m_data = NULL;
8767 p_tcp->m_data_size = 0;
8772 * Destroys a coding parameter structure.
8774 * @param p_cp the coding parameter to destroy.
8776 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8778 OPJ_UINT32 l_nb_tiles;
8779 opj_tcp_v2_t * l_current_tile = 00;
8790 l_current_tile = p_cp->tcps;
8791 l_nb_tiles = p_cp->th * p_cp->tw;
8794 (i = 0; i < l_nb_tiles; ++i)
8796 j2k_tcp_destroy(l_current_tile);
8799 opj_free(p_cp->tcps);
8803 (p_cp->ppm_buffer != 00)
8805 opj_free(p_cp->ppm_buffer);
8806 p_cp->ppm_buffer = 00;
8809 (p_cp->comment != 00)
8811 opj_free(p_cp->comment);
8815 (! p_cp->m_is_decoder)
8818 (p_cp->m_specific_param.m_enc.m_matrice)
8820 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8821 p_cp->m_specific_param.m_enc.m_matrice = 00;
8829 * Reads a tile header.
8830 * @param p_j2k the jpeg2000 codec.
8831 * @param p_stream the stream to write data to.
8832 * @param p_manager the user event manager.
8834 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8835 OPJ_UINT32 * p_tile_index,
8836 OPJ_UINT32 * p_data_size,
8837 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8838 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8839 OPJ_UINT32 * p_nb_comps,
8841 opj_stream_private_t *p_stream,
8842 opj_event_mgr_t * p_manager )
8844 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8845 OPJ_UINT32 l_marker_size;
8846 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8847 opj_tcp_v2_t * l_tcp = NULL;
8848 OPJ_UINT32 l_nb_tiles;
8851 assert(p_stream != 00);
8852 assert(p_j2k != 00);
8853 assert(p_manager != 00);
8855 /* Reach the End Of Codestream ?*/
8856 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8857 l_current_marker = J2K_MS_EOC;
8859 /* We need to encounter a SOT marker (a new tile-part header) */
8860 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8864 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8865 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8867 /* Try to read until the Start Of Data is detected */
8868 while (l_current_marker != J2K_MS_SOD) {
8870 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8871 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8872 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8876 /* Read 2 bytes from the buffer as the marker size */
8877 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8879 /* Why this condition? FIXME */
8880 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8881 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8883 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8885 /* Get the marker handler from the marker ID */
8886 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8888 /* Check if the marker is known and if it is the right place to find it */
8889 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8890 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8893 /* FIXME manage case of unknown marker as in the main header ? */
8895 /* Check if the marker size is compatible with the header data size */
8896 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8897 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8898 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8899 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8902 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8905 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8906 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) {
8907 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8911 /* Read the marker segment with the correct marker handler */
8912 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8913 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8917 /* Add the marker to the codestream index*/
8918 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8920 l_marker_handler->id,
8921 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8922 l_marker_size + 4 );
8924 /* Keep the position of the last SOT marker read */
8925 if ( l_marker_handler->id == J2K_MS_SOT ) {
8926 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8927 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8929 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8934 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8935 /* Skip the rest of the tile part header*/
8936 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) {
8937 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8940 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8943 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8944 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8945 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8948 /* Read 2 bytes from the buffer as the new marker ID */
8949 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8953 /* If we didn't skip data before, we need to read the SOD marker*/
8954 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8955 /* Try to read the SOD marker and skip data ? FIXME */
8956 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8962 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8963 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8964 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8965 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8969 /* Read 2 bytes from buffer as the new marker ID */
8970 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8974 /* Indicate we will try to read a new tile-part header*/
8975 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8976 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8977 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8979 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8980 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8981 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8985 /* Read 2 bytes from buffer as the new marker ID */
8986 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8990 /* Current marker is the EOC marker ?*/
8991 if (l_current_marker == J2K_MS_EOC) {
8992 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8993 p_j2k->m_current_tile_number = 0;
8994 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8999 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9000 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9001 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9003 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9004 ++p_j2k->m_current_tile_number;
9008 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9009 *p_go_on = OPJ_FALSE;
9015 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9016 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9020 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9021 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9023 *p_tile_index = p_j2k->m_current_tile_number;
9024 *p_go_on = OPJ_TRUE;
9025 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9026 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9027 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9028 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9029 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9030 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9032 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9038 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9039 OPJ_UINT32 p_tile_index,
9041 OPJ_UINT32 p_data_size,
9042 opj_stream_private_t *p_stream,
9043 opj_event_mgr_t * p_manager )
9045 OPJ_UINT32 l_current_marker;
9046 OPJ_BYTE l_data [2];
9047 opj_tcp_v2_t * l_tcp;
9050 assert(p_stream != 00);
9051 assert(p_j2k != 00);
9052 assert(p_manager != 00);
9054 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9055 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9059 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9060 if (! l_tcp->m_data) {
9061 j2k_tcp_destroy(l_tcp);
9065 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9069 p_j2k->cstr_index) ) {
9070 j2k_tcp_destroy(l_tcp);
9071 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9075 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9079 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9080 * we destroy just the data which will be re-read in read_tile_header*/
9081 /*j2k_tcp_destroy(l_tcp);
9082 p_j2k->m_tcd->tcp = 0;*/
9083 j2k_tcp_data_destroy(l_tcp);
9085 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9086 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9088 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9089 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9090 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9094 opj_read_bytes(l_data,&l_current_marker,2);
9096 if (l_current_marker == J2K_MS_EOC) {
9097 p_j2k->m_current_tile_number = 0;
9098 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9100 else if (l_current_marker != J2K_MS_SOT)
9102 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9111 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9113 OPJ_UINT32 i,j,k = 0;
9114 OPJ_UINT32 l_width_src,l_height_src;
9115 OPJ_UINT32 l_width_dest,l_height_dest;
9116 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9117 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9118 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9119 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9120 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9122 opj_image_comp_t * l_img_comp_src = 00;
9123 opj_image_comp_t * l_img_comp_dest = 00;
9125 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9126 opj_image_t * l_image_src = 00;
9127 OPJ_UINT32 l_size_comp, l_remaining;
9128 OPJ_INT32 * l_dest_ptr;
9129 opj_tcd_resolution_v2_t* l_res= 00;
9131 l_tilec = p_tcd->tcd_image->tiles->comps;
9132 l_image_src = p_tcd->image;
9133 l_img_comp_src = l_image_src->comps;
9135 l_img_comp_dest = p_output_image->comps;
9137 for (i=0; i<l_image_src->numcomps; i++) {
9139 /* Allocate output component buffer if necessary */
9140 if (!l_img_comp_dest->data) {
9142 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9143 if (! l_img_comp_dest->data) {
9148 /* Copy info from decoded comp image to output image */
9149 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9152 /* Compute the precision of the output buffer */
9153 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9154 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9155 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9161 if (l_size_comp == 3) {
9166 /* Current tile component size*/
9168 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9169 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9172 l_width_src = (l_res->x1 - l_res->x0);
9173 l_height_src = (l_res->y1 - l_res->y0);
9175 /* Border of the current output component*/
9176 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9177 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9178 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9179 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9182 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9183 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9187 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9188 * of the input buffer (decoded tile component) which will be move
9189 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9190 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9191 * by this input area.
9193 assert( l_res->x0 >= 0);
9194 assert( l_res->x1 >= 0);
9195 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9196 l_start_x_dest = l_res->x0 - l_x0_dest;
9197 l_offset_x0_src = 0;
9199 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9200 l_width_dest = l_width_src;
9201 l_offset_x1_src = 0;
9204 l_width_dest = l_x1_dest - l_res->x0 ;
9205 l_offset_x1_src = l_width_src - l_width_dest;
9209 l_start_x_dest = 0 ;
9210 l_offset_x0_src = l_x0_dest - l_res->x0;
9212 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9213 l_width_dest = l_width_src - l_offset_x0_src;
9214 l_offset_x1_src = 0;
9217 l_width_dest = l_img_comp_dest->w ;
9218 l_offset_x1_src = l_res->x1 - l_x1_dest;
9222 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9223 l_start_y_dest = l_res->y0 - l_y0_dest;
9224 l_offset_y0_src = 0;
9226 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9227 l_height_dest = l_height_src;
9228 l_offset_y1_src = 0;
9231 l_height_dest = l_y1_dest - l_res->y0 ;
9232 l_offset_y1_src = l_height_src - l_height_dest;
9236 l_start_y_dest = 0 ;
9237 l_offset_y0_src = l_y0_dest - l_res->y0;
9239 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9240 l_height_dest = l_height_src - l_offset_y0_src;
9241 l_offset_y1_src = 0;
9244 l_height_dest = l_img_comp_dest->h ;
9245 l_offset_y1_src = l_res->y1 - l_y1_dest;
9249 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9254 /* Compute the input buffer offset */
9255 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9256 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9257 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9259 /* Compute the output buffer offset */
9260 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9261 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9263 /* Move the output buffer to the first place where we will write*/
9264 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9267 fprintf(stdout, "COMPO[%d]:\n",i);
9268 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9269 "\t tile offset:%d, %d, %d, %d\n"
9270 "\t buffer offset: %d; %d, %d\n",
9271 l_res->x0, l_res->y0, l_width_src, l_height_src,
9272 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9273 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9275 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9276 "\t start offset: %d, line offset= %d\n",
9277 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9281 switch (l_size_comp) {
9284 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9285 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9287 if (l_img_comp_src->sgnd) {
9288 for (j = 0 ; j < l_height_dest ; ++j) {
9289 for ( k = 0 ; k < l_width_dest ; ++k) {
9290 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9293 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9294 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9298 for ( j = 0 ; j < l_height_dest ; ++j ) {
9299 for ( k = 0 ; k < l_width_dest ; ++k) {
9300 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9303 l_dest_ptr+= l_line_offset_dest;
9304 l_src_ptr += l_line_offset_src;
9308 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9309 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9314 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9315 l_src_ptr += l_start_offset_src;
9317 if (l_img_comp_src->sgnd) {
9318 for (j=0;j<l_height_dest;++j) {
9319 for (k=0;k<l_width_dest;++k) {
9320 *(l_dest_ptr++) = *(l_src_ptr++);
9323 l_dest_ptr+= l_line_offset_dest;
9324 l_src_ptr += l_line_offset_src ;
9328 for (j=0;j<l_height_dest;++j) {
9329 for (k=0;k<l_width_dest;++k) {
9330 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9333 l_dest_ptr+= l_line_offset_dest;
9334 l_src_ptr += l_line_offset_src ;
9338 l_src_ptr += l_end_offset_src;
9339 p_data = (OPJ_BYTE*) l_src_ptr;
9344 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9345 l_src_ptr += l_start_offset_src;
9347 for (j=0;j<l_height_dest;++j) {
9348 for (k=0;k<l_width_dest;++k) {
9349 *(l_dest_ptr++) = (*(l_src_ptr++));
9352 l_dest_ptr+= l_line_offset_dest;
9353 l_src_ptr += l_line_offset_src ;
9356 l_src_ptr += l_end_offset_src;
9357 p_data = (OPJ_BYTE*) l_src_ptr;
9371 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9373 * @param p_j2k the jpeg2000 codec.
9374 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9375 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9376 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9377 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9378 * @param p_manager the user event manager
9380 * @return true if the area could be set.
9382 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9383 opj_image_t* p_image,
9384 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9385 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9386 struct opj_event_mgr * p_manager )
9388 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9389 opj_image_t * l_image = p_j2k->m_private_image;
9392 OPJ_INT32 l_comp_x1, l_comp_y1;
9393 opj_image_comp_t* l_img_comp = NULL;
9395 /* Check if we are read the main header */
9396 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9397 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9401 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9402 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9404 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9405 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9406 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9407 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9413 /* Check if the positions provided by the user are correct */
9416 assert(p_start_x >= 0 );
9417 assert(p_start_y >= 0 );
9419 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9420 opj_event_msg_v2(p_manager, EVT_ERROR,
9421 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9422 p_start_x, l_image->x1);
9425 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9426 opj_event_msg_v2(p_manager, EVT_WARNING,
9427 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9428 p_start_x, l_image->x0);
9429 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9430 p_image->x0 = l_image->x0;
9433 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9434 p_image->x0 = p_start_x;
9438 if ((OPJ_UINT32)p_start_y > l_image->y1){
9439 opj_event_msg_v2(p_manager, EVT_ERROR,
9440 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9441 p_start_y, l_image->y1);
9444 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9445 opj_event_msg_v2(p_manager, EVT_WARNING,
9446 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9447 p_start_y, l_image->y0);
9448 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9449 p_image->y0 = l_image->y0;
9452 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9453 p_image->y0 = p_start_y;
9457 assert((OPJ_UINT32)p_end_x > 0);
9458 assert((OPJ_UINT32)p_end_y > 0);
9459 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9460 opj_event_msg_v2(p_manager, EVT_ERROR,
9461 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9462 p_end_x, l_image->x0);
9465 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9466 opj_event_msg_v2(p_manager, EVT_WARNING,
9467 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9468 p_end_x, l_image->x1);
9469 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9470 p_image->x1 = l_image->x1;
9473 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9474 p_image->x1 = p_end_x;
9478 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9479 opj_event_msg_v2(p_manager, EVT_ERROR,
9480 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9481 p_end_y, l_image->y0);
9484 if ((OPJ_UINT32)p_end_y > l_image->y1){
9485 opj_event_msg_v2(p_manager, EVT_WARNING,
9486 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9487 p_end_y, l_image->y1);
9488 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9489 p_image->y1 = l_image->y1;
9492 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9493 p_image->y1 = p_end_y;
9497 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9499 l_img_comp = p_image->comps;
9500 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9504 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9505 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9506 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9507 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9509 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9510 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9512 opj_event_msg_v2(p_manager, EVT_ERROR,
9513 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9517 l_img_comp->w = l_w;
9519 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9520 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9522 opj_event_msg_v2(p_manager, EVT_ERROR,
9523 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9527 l_img_comp->h = l_h;
9532 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9533 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9540 /* ----------------------------------------------------------------------- */
9541 /* J2K / JPT decoder interface */
9542 /* ----------------------------------------------------------------------- */
9544 * Creates a J2K decompression structure.
9546 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9548 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9550 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9554 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9556 l_j2k->m_is_decoder = 1;
9557 l_j2k->m_cp.m_is_decoder = 1;
9559 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9560 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9564 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9566 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9567 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9572 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9574 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9576 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9578 /* codestream index creation */
9579 l_j2k->cstr_index = j2k_create_cstr_index();
9581 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9582 if (!l_j2k->cstr_index){
9587 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9590 /* validation list creation */
9591 l_j2k->m_validation_list = opj_procedure_list_create();
9592 if (! l_j2k->m_validation_list) {
9597 /* execution list creation */
9598 l_j2k->m_procedure_list = opj_procedure_list_create();
9599 if (! l_j2k->m_procedure_list) {
9608 opj_codestream_index_t* j2k_create_cstr_index(void)
9610 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9611 opj_calloc(1,sizeof(opj_codestream_index_t));
9615 cstr_index->maxmarknum = 100;
9616 cstr_index->marknum = 0;
9617 cstr_index->marker = (opj_marker_info_t*)
9618 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9619 if (!cstr_index-> marker)
9622 cstr_index->tile_index = NULL;
9629 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9631 * @param p_tile_no the tile index.
9632 * @param p_comp_no the component being outputted.
9633 * @param p_j2k the J2K codec.
9635 * @return the number of bytes taken by the SPCod element.
9637 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9638 OPJ_UINT32 p_tile_no,
9639 OPJ_UINT32 p_comp_no )
9641 opj_cp_v2_t *l_cp = 00;
9642 opj_tcp_v2_t *l_tcp = 00;
9643 opj_tccp_t *l_tccp = 00;
9646 assert(p_j2k != 00);
9648 l_cp = &(p_j2k->m_cp);
9649 l_tcp = &l_cp->tcps[p_tile_no];
9650 l_tccp = &l_tcp->tccps[p_comp_no];
9652 /* preconditions again */
9653 assert(p_tile_no < (l_cp->tw * l_cp->th));
9654 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9656 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9657 return 5 + l_tccp->numresolutions;
9665 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9667 * @param p_comp_no the component number to output.
9668 * @param p_stream the stream to write data to.
9669 * @param p_j2k J2K codec.
9670 * @param p_manager the user event manager.
9673 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9674 OPJ_UINT32 p_tile_no,
9675 OPJ_UINT32 p_comp_no,
9677 OPJ_UINT32 * p_header_size,
9678 struct opj_event_mgr * p_manager )
9681 opj_cp_v2_t *l_cp = 00;
9682 opj_tcp_v2_t *l_tcp = 00;
9683 opj_tccp_t *l_tccp = 00;
9686 assert(p_j2k != 00);
9687 assert(p_header_size != 00);
9688 assert(p_manager != 00);
9689 assert(p_data != 00);
9691 l_cp = &(p_j2k->m_cp);
9692 l_tcp = &l_cp->tcps[p_tile_no];
9693 l_tccp = &l_tcp->tccps[p_comp_no];
9695 /* preconditions again */
9696 assert(p_tile_no < (l_cp->tw * l_cp->th));
9697 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9699 if (*p_header_size < 5) {
9700 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9704 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9707 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9710 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9713 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9716 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9719 *p_header_size = *p_header_size - 5;
9721 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9723 if (*p_header_size < l_tccp->numresolutions) {
9724 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9728 for (i = 0; i < l_tccp->numresolutions; ++i) {
9729 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9733 *p_header_size = *p_header_size - l_tccp->numresolutions;
9740 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9741 * @param p_header_data the data contained in the COM box.
9742 * @param p_j2k the jpeg2000 codec.
9743 * @param p_header_size the size of the data contained in the COM marker.
9744 * @param p_manager the user event manager.
9746 opj_bool j2k_read_SPCod_SPCoc(
9747 opj_j2k_v2_t *p_j2k,
9749 OPJ_BYTE * p_header_data,
9750 OPJ_UINT32 * p_header_size,
9751 struct opj_event_mgr * p_manager
9754 OPJ_UINT32 i, l_tmp;
9755 opj_cp_v2_t *l_cp = NULL;
9756 opj_tcp_v2_t *l_tcp = NULL;
9757 opj_tccp_t *l_tccp = NULL;
9758 OPJ_BYTE * l_current_ptr = NULL;
9761 assert(p_j2k != 00);
9762 assert(p_manager != 00);
9763 assert(p_header_data != 00);
9765 l_cp = &(p_j2k->m_cp);
9766 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9767 &l_cp->tcps[p_j2k->m_current_tile_number] :
9768 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9770 /* precondition again */
9771 assert(compno < p_j2k->m_private_image->numcomps);
9773 l_tccp = &l_tcp->tccps[compno];
9774 l_current_ptr = p_header_data;
9776 /* make sure room is sufficient */
9777 if (*p_header_size < 5) {
9778 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9782 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9783 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9786 /* If user wants to remove more resolutions than the codestream contains, return error */
9787 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9788 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9789 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9790 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9794 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9798 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9802 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9805 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9808 *p_header_size = *p_header_size - 5;
9810 /* use custom precinct size ? */
9811 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9812 if (*p_header_size < l_tccp->numresolutions) {
9813 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9817 for (i = 0; i < l_tccp->numresolutions; ++i) {
9818 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9820 l_tccp->prcw[i] = l_tmp & 0xf;
9821 l_tccp->prch[i] = l_tmp >> 4;
9824 *p_header_size = *p_header_size - l_tccp->numresolutions;
9827 /* set default size for the precinct width and height */
9828 for (i = 0; i < l_tccp->numresolutions; ++i) {
9829 l_tccp->prcw[i] = 15;
9830 l_tccp->prch[i] = 15;
9834 #ifdef WIP_REMOVE_MSD
9836 if (p_j2k->cstr_info && compno == 0) {
9837 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9839 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9840 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9841 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9842 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9843 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9846 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9847 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9856 * Copies the tile component parameters of all the component from the first tile component.
9858 * @param p_j2k the J2k codec.
9860 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9864 opj_cp_v2_t *l_cp = NULL;
9865 opj_tcp_v2_t *l_tcp = NULL;
9866 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9867 OPJ_UINT32 l_prc_size;
9870 assert(p_j2k != 00);
9872 l_cp = &(p_j2k->m_cp);
9873 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9874 &l_cp->tcps[p_j2k->m_current_tile_number] :
9875 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9877 l_ref_tccp = &l_tcp->tccps[0];
9878 l_copied_tccp = l_ref_tccp + 1;
9879 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9881 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9882 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9883 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9884 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9885 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9886 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9887 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9888 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9894 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9896 * @param p_tile_no the tile index.
9897 * @param p_comp_no the component being outputted.
9898 * @param p_j2k the J2K codec.
9900 * @return the number of bytes taken by the SPCod element.
9902 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9903 OPJ_UINT32 p_tile_no,
9904 OPJ_UINT32 p_comp_no )
9906 OPJ_UINT32 l_num_bands;
9908 opj_cp_v2_t *l_cp = 00;
9909 opj_tcp_v2_t *l_tcp = 00;
9910 opj_tccp_t *l_tccp = 00;
9913 assert(p_j2k != 00);
9915 l_cp = &(p_j2k->m_cp);
9916 l_tcp = &l_cp->tcps[p_tile_no];
9917 l_tccp = &l_tcp->tccps[p_comp_no];
9919 /* preconditions again */
9920 assert(p_tile_no < l_cp->tw * l_cp->th);
9921 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9923 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9925 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9926 return 1 + l_num_bands;
9929 return 1 + 2*l_num_bands;
9934 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9936 * @param p_tile_no the tile to output.
9937 * @param p_comp_no the component number to output.
9938 * @param p_data the data buffer.
9939 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9940 * @param p_j2k J2K codec.
9941 * @param p_manager the user event manager.
9944 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9945 OPJ_UINT32 p_tile_no,
9946 OPJ_UINT32 p_comp_no,
9948 OPJ_UINT32 * p_header_size,
9949 struct opj_event_mgr * p_manager )
9951 OPJ_UINT32 l_header_size;
9952 OPJ_UINT32 l_band_no, l_num_bands;
9953 OPJ_UINT32 l_expn,l_mant;
9955 opj_cp_v2_t *l_cp = 00;
9956 opj_tcp_v2_t *l_tcp = 00;
9957 opj_tccp_t *l_tccp = 00;
9960 assert(p_j2k != 00);
9961 assert(p_header_size != 00);
9962 assert(p_manager != 00);
9963 assert(p_data != 00);
9965 l_cp = &(p_j2k->m_cp);
9966 l_tcp = &l_cp->tcps[p_tile_no];
9967 l_tccp = &l_tcp->tccps[p_comp_no];
9969 /* preconditions again */
9970 assert(p_tile_no < l_cp->tw * l_cp->th);
9971 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9973 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9975 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9976 l_header_size = 1 + l_num_bands;
9978 if (*p_header_size < l_header_size) {
9979 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9983 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9986 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9987 l_expn = l_tccp->stepsizes[l_band_no].expn;
9988 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9993 l_header_size = 1 + 2*l_num_bands;
9995 if (*p_header_size < l_header_size) {
9996 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10000 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10003 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10004 l_expn = l_tccp->stepsizes[l_band_no].expn;
10005 l_mant = l_tccp->stepsizes[l_band_no].mant;
10007 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10012 *p_header_size = *p_header_size - l_header_size;
10018 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10020 * @param p_comp_no the component being targeted.
10021 * @param p_header_data the data contained in the COM box.
10022 * @param p_j2k the jpeg2000 codec.
10023 * @param p_header_size the size of the data contained in the COM marker.
10024 * @param p_manager the user event manager.
10026 opj_bool j2k_read_SQcd_SQcc(
10027 opj_j2k_v2_t *p_j2k,
10028 OPJ_UINT32 p_comp_no,
10029 OPJ_BYTE* p_header_data,
10030 OPJ_UINT32 * p_header_size,
10031 struct opj_event_mgr * p_manager
10035 OPJ_UINT32 l_band_no;
10036 opj_cp_v2_t *l_cp = 00;
10037 opj_tcp_v2_t *l_tcp = 00;
10038 opj_tccp_t *l_tccp = 00;
10039 OPJ_BYTE * l_current_ptr = 00;
10040 OPJ_UINT32 l_tmp, l_num_band;
10043 assert(p_j2k != 00);
10044 assert(p_manager != 00);
10045 assert(p_header_data != 00);
10047 l_cp = &(p_j2k->m_cp);
10048 /* come from tile part header or main header ?*/
10049 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10050 &l_cp->tcps[p_j2k->m_current_tile_number] :
10051 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10053 /* precondition again*/
10054 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10056 l_tccp = &l_tcp->tccps[p_comp_no];
10057 l_current_ptr = p_header_data;
10059 if (*p_header_size < 1) {
10060 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10063 *p_header_size -= 1;
10065 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10068 l_tccp->qntsty = l_tmp & 0x1f;
10069 l_tccp->numgbits = l_tmp >> 5;
10070 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10074 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10076 (*p_header_size) / 2;
10078 if( l_num_band > J2K_MAXBANDS ) {
10079 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10080 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10081 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10082 /*return OPJ_FALSE;*/
10087 if (l_cp->correct) {
10089 /* if JPWL is on, we check whether there are too many subbands */
10090 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10091 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10092 "JPWL: bad number of subbands in Sqcx (%d)\n",
10094 if (!JPWL_ASSUME) {
10095 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10098 /* we try to correct */
10100 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10101 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10106 #endif /* USE_JPWL */
10108 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10109 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10110 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10112 if (l_band_no < J2K_MAXBANDS){
10113 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10114 l_tccp->stepsizes[l_band_no].mant = 0;
10117 *p_header_size = *p_header_size - l_num_band;
10120 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10121 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10123 if (l_band_no < J2K_MAXBANDS){
10124 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10125 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10128 *p_header_size = *p_header_size - 2*l_num_band;
10131 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10132 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10133 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10134 l_tccp->stepsizes[l_band_no].expn =
10135 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10136 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10137 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10145 * Copies the tile component parameters of all the component from the first tile component.
10147 * @param p_j2k the J2k codec.
10149 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10152 opj_cp_v2_t *l_cp = NULL;
10153 opj_tcp_v2_t *l_tcp = NULL;
10154 opj_tccp_t *l_ref_tccp = NULL;
10155 opj_tccp_t *l_copied_tccp = NULL;
10158 /* preconditions */
10159 assert(p_j2k != 00);
10161 l_cp = &(p_j2k->m_cp);
10162 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10163 &l_cp->tcps[p_j2k->m_current_tile_number] :
10164 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10166 l_ref_tccp = &l_tcp->tccps[0];
10167 l_copied_tccp = l_ref_tccp + 1;
10168 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10170 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10171 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10172 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10173 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10179 * Dump some elements from the J2K decompression structure .
10181 *@param p_j2k the jpeg2000 codec.
10182 *@param flag flag to describe what elments are dump.
10183 *@param out_stream output stream where dump the elements.
10186 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10188 /* Check if the flag is compatible with j2k file*/
10189 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10190 fprintf(out_stream, "Wrong flag\n");
10194 /* Dump the image_header */
10195 if (flag & OPJ_IMG_INFO){
10196 if (p_j2k->m_private_image)
10197 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10200 /* Dump the codestream info from main header */
10201 if (flag & OPJ_J2K_MH_INFO){
10202 j2k_dump_MH_info(p_j2k, out_stream);
10206 /* Dump the codestream info of the current tile */
10207 if (flag & OPJ_J2K_TH_INFO){
10211 /* Dump the codestream index from main header */
10212 if (flag & OPJ_J2K_MH_IND){
10213 j2k_dump_MH_index(p_j2k, out_stream);
10216 /* Dump the codestream index of the current tile */
10217 if (flag & OPJ_J2K_TH_IND){
10224 * Dump index elements of the codestream extract from the main header.
10226 *@param p_j2k the jpeg2000 codec.
10227 *@param out_stream output stream where dump the elements.
10230 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10232 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10233 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10235 fprintf(out_stream, "Codestream index from main header: {\n");
10237 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10238 "\t Main header end position=%" PRIi64 "\n",
10239 cstr_index->main_head_start, cstr_index->main_head_end);
10241 fprintf(out_stream, "\t Marker list: {\n");
10243 if (cstr_index->marker){
10244 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10245 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10246 cstr_index->marker[it_marker].type,
10247 cstr_index->marker[it_marker].pos,
10248 cstr_index->marker[it_marker].len );
10252 fprintf(out_stream, "\t }\n");
10255 if (cstr_index->tile_index){
10257 /* Simple test to avoid to write empty information*/
10258 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10259 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10260 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10263 if (l_acc_nb_of_tile_part)
10265 fprintf(out_stream, "\t Tile index: {\n");
10267 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10268 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10270 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10272 if (cstr_index->tile_index[it_tile].tp_index){
10273 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10274 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10276 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10277 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10278 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10282 if (cstr_index->tile_index[it_tile].marker){
10283 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10284 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10285 cstr_index->tile_index[it_tile].marker[it_marker].type,
10286 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10287 cstr_index->tile_index[it_tile].marker[it_marker].len );
10291 fprintf(out_stream,"\t }\n");
10295 fprintf(out_stream,"}\n");
10300 * Dump info elements of the codestream extract from the main header.
10302 *@param p_j2k the jpeg2000 codec.
10303 *@param out_stream output stream where dump the elements.
10306 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10308 opj_tcp_v2_t * l_default_tile=NULL;
10310 fprintf(out_stream, "Codestream info from main header: {\n");
10312 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10313 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10314 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10316 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10317 if (l_default_tile)
10320 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10322 fprintf(out_stream, "\t default tile {\n");
10323 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10324 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10325 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10326 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10328 for (compno = 0; compno < numcomps; compno++) {
10329 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10331 OPJ_INT32 bandno, numbands;
10334 fprintf(out_stream, "\t\t comp %d {\n", compno);
10335 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10336 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10337 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10338 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10339 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10340 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10342 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10343 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10344 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10346 fprintf(out_stream, "\n");
10348 /* quantization style*/
10349 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10350 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10351 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10352 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10353 for (bandno = 0; bandno < numbands; bandno++) {
10354 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10355 l_tccp->stepsizes[bandno].expn);
10357 fprintf(out_stream, "\n");
10360 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10362 fprintf(out_stream, "\t\t }\n");
10363 } /*end of component of default tile*/
10364 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10368 fprintf(out_stream, "}\n");
10373 * Dump an image header structure.
10375 *@param img_header the image header to dump.
10376 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10377 *@param out_stream output stream where dump the elements.
10379 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10383 if (dev_dump_flag){
10384 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10388 fprintf(out_stream, "Image info {\n");
10389 tab[0] = '\t';tab[1] = '\0';
10392 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10393 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10394 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10396 if (img_header->comps){
10398 for (compno = 0; compno < img_header->numcomps; compno++) {
10399 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10400 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10401 fprintf(out_stream,"%s}\n",tab);
10405 fprintf(out_stream, "}\n");
10409 * Dump a component image header structure.
10411 *@param comp_header the component image header to dump.
10412 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10413 *@param out_stream output stream where dump the elements.
10415 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10419 if (dev_dump_flag){
10420 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10423 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10426 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10427 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10428 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10431 fprintf(out_stream, "}\n");
10436 * Get the codestream info from a JPEG2000 codec.
10438 *@param p_j2k the component image header to dump.
10440 *@return the codestream information extract from the jpg2000 codec
10442 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10445 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10446 opj_tcp_v2_t *l_default_tile;
10447 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10449 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10451 cstr_info->tx0 = p_j2k->m_cp.tx0;
10452 cstr_info->ty0 = p_j2k->m_cp.ty0;
10453 cstr_info->tdx = p_j2k->m_cp.tdx;
10454 cstr_info->tdy = p_j2k->m_cp.tdy;
10455 cstr_info->tw = p_j2k->m_cp.tw;
10456 cstr_info->th = p_j2k->m_cp.th;
10458 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10460 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10462 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10463 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10464 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10465 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10467 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10469 for (compno = 0; compno < numcomps; compno++) {
10470 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10471 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10472 OPJ_INT32 bandno, numbands;
10475 l_tccp_info->csty = l_tccp->csty;
10476 l_tccp_info->numresolutions = l_tccp->numresolutions;
10477 l_tccp_info->cblkw = l_tccp->cblkw;
10478 l_tccp_info->cblkh = l_tccp->cblkh;
10479 l_tccp_info->cblksty = l_tccp->cblksty;
10480 l_tccp_info->qmfbid = l_tccp->qmfbid;
10481 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10483 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10484 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10487 /* quantization style*/
10488 l_tccp_info->qntsty = l_tccp->qntsty;
10489 l_tccp_info->numgbits = l_tccp->numgbits;
10491 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10492 if (numbands < J2K_MAXBANDS) {
10493 for (bandno = 0; bandno < numbands; bandno++) {
10494 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10495 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10500 l_tccp_info->roishift = l_tccp->roishift;
10508 * Get the codestream index from a JPEG2000 codec.
10510 *@param p_j2k the component image header to dump.
10512 *@return the codestream index extract from the jpg2000 codec
10514 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10516 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10517 opj_calloc(1,sizeof(opj_codestream_index_t));
10521 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10522 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10523 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10525 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10526 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10527 if (!l_cstr_index->marker){
10528 opj_free( l_cstr_index);
10532 if (p_j2k->cstr_index->marker)
10533 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10535 opj_free(l_cstr_index->marker);
10536 l_cstr_index->marker = NULL;
10539 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10540 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10541 if (!l_cstr_index->tile_index){
10542 opj_free( l_cstr_index->marker);
10543 opj_free( l_cstr_index);
10547 if (!p_j2k->cstr_index->tile_index){
10548 opj_free(l_cstr_index->tile_index);
10549 l_cstr_index->tile_index = NULL;
10552 OPJ_UINT32 it_tile = 0;
10553 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10556 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10558 l_cstr_index->tile_index[it_tile].marker =
10559 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10561 if (!l_cstr_index->tile_index[it_tile].marker) {
10562 OPJ_UINT32 it_tile_free;
10564 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10565 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10568 opj_free( l_cstr_index->tile_index);
10569 opj_free( l_cstr_index->marker);
10570 opj_free( l_cstr_index);
10574 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10575 memcpy( l_cstr_index->tile_index[it_tile].marker,
10576 p_j2k->cstr_index->tile_index[it_tile].marker,
10577 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10579 opj_free(l_cstr_index->tile_index[it_tile].marker);
10580 l_cstr_index->tile_index[it_tile].marker = NULL;
10583 /* Tile part index*/
10584 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10586 l_cstr_index->tile_index[it_tile].tp_index =
10587 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10589 if(!l_cstr_index->tile_index[it_tile].tp_index){
10590 OPJ_UINT32 it_tile_free;
10592 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10593 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10594 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10597 opj_free( l_cstr_index->tile_index);
10598 opj_free( l_cstr_index->marker);
10599 opj_free( l_cstr_index);
10603 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10604 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10605 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10606 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10609 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10610 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10613 /* Packet index (NOT USED)*/
10614 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10615 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10620 return l_cstr_index;
10623 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10625 OPJ_UINT32 it_tile=0;
10627 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10628 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10629 if (!p_j2k->cstr_index->tile_index)
10632 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10633 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10634 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10635 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10636 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10637 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10647 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10648 opj_stream_private_t *p_stream,
10649 opj_event_mgr_t * p_manager)
10651 opj_bool l_go_on = OPJ_TRUE;
10652 OPJ_UINT32 l_current_tile_no;
10653 OPJ_UINT32 l_data_size,l_max_data_size;
10654 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10655 OPJ_UINT32 l_nb_comps;
10656 OPJ_BYTE * l_current_data;
10658 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10659 if (! l_current_data) {
10662 l_max_data_size = 1000;
10667 if (! j2k_read_tile_header( p_j2k,
10668 &l_current_tile_no,
10670 &l_tile_x0, &l_tile_y0,
10671 &l_tile_x1, &l_tile_y1,
10676 opj_free(l_current_data);
10684 if (l_data_size > l_max_data_size) {
10685 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10686 if (! l_current_data) {
10687 opj_free(l_current_data);
10691 l_max_data_size = l_data_size;
10694 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10695 opj_free(l_current_data);
10698 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);
10700 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10701 opj_free(l_current_data);
10704 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10708 opj_free(l_current_data);
10714 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10716 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10719 assert(p_j2k != 00);
10721 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10722 /* DEVELOPER CORNER, add your custom procedures */
10727 * Read and decode one tile.
10729 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10730 opj_stream_private_t *p_stream,
10731 opj_event_mgr_t * p_manager)
10733 opj_bool l_go_on = OPJ_TRUE;
10734 OPJ_UINT32 l_current_tile_no;
10735 OPJ_UINT32 l_tile_no_to_dec;
10736 OPJ_UINT32 l_data_size,l_max_data_size;
10737 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10738 OPJ_UINT32 l_nb_comps;
10739 OPJ_BYTE * l_current_data;
10741 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10742 if (! l_current_data) {
10745 l_max_data_size = 1000;
10747 /*Allocate and initialize some elements of codestrem index if not already done*/
10748 if( !p_j2k->cstr_index->tile_index)
10750 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10751 opj_free(l_current_data);
10755 /* Move into the codestream to the first SOT used to decode the desired tile */
10756 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10757 if (p_j2k->cstr_index->tile_index)
10758 if(p_j2k->cstr_index->tile_index->tp_index)
10760 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10761 /* the index for this tile has not been built,
10762 * so move to the last SOT read */
10763 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10764 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10769 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)) {
10770 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10774 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10775 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10776 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10780 if (! j2k_read_tile_header( p_j2k,
10781 &l_current_tile_no,
10783 &l_tile_x0, &l_tile_y0,
10784 &l_tile_x1, &l_tile_y1,
10789 opj_free(l_current_data);
10798 if (l_data_size > l_max_data_size) {
10799 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10800 if (! l_current_data) {
10801 opj_free(l_current_data);
10805 l_max_data_size = l_data_size;
10810 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10811 opj_free(l_current_data);
10814 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);
10816 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10817 opj_free(l_current_data);
10820 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10822 if(l_current_tile_no == l_tile_no_to_dec)
10824 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10825 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10826 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10832 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);
10837 opj_free(l_current_data);
10844 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10846 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10849 assert(p_j2k != 00);
10851 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10852 /* DEVELOPER CORNER, add your custom procedures */
10858 * Decodes the tiles of the stream.
10860 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10861 opj_stream_private_t * p_stream,
10862 opj_image_t * p_image,
10863 opj_event_mgr_t * p_manager)
10870 p_j2k->m_output_image = opj_image_create0();
10871 if (! (p_j2k->m_output_image)) {
10874 opj_copy_image_header(p_image, p_j2k->m_output_image);
10876 /* customization of the decoding */
10877 j2k_setup_decoding(p_j2k);
10879 /* Decode the codestream */
10880 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10881 opj_image_destroy(p_j2k->m_private_image);
10882 p_j2k->m_private_image = NULL;
10886 /* Move data and copy one information from codec to output image*/
10887 for (compno = 0; compno < p_image->numcomps; compno++) {
10888 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10889 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10890 p_j2k->m_output_image->comps[compno].data = NULL;
10898 * Get the decoded tile.
10900 * @param p_j2k the jpeg2000 codestream codec.
10901 * @param p_stream input_stream
10902 * @param p_image output image. .
10903 * @param p_manager the user event manager
10904 * @param tile_index index of the tile we want decode
10906 * @return true if succeed.
10908 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10909 opj_stream_private_t *p_stream,
10910 opj_image_t* p_image,
10911 struct opj_event_mgr * p_manager,
10912 OPJ_UINT32 tile_index )
10915 OPJ_UINT32 l_tile_x, l_tile_y;
10916 opj_image_comp_t* l_img_comp;
10919 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10923 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10924 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);
10928 /* Compute the dimension of the desired tile*/
10929 l_tile_x = tile_index % p_j2k->m_cp.tw;
10930 l_tile_y = tile_index / p_j2k->m_cp.tw;
10932 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10933 if (p_image->x0 < p_j2k->m_private_image->x0)
10934 p_image->x0 = p_j2k->m_private_image->x0;
10935 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10936 if (p_image->x1 > p_j2k->m_private_image->x1)
10937 p_image->x1 = p_j2k->m_private_image->x1;
10939 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10940 if (p_image->y0 < p_j2k->m_private_image->y0)
10941 p_image->y0 = p_j2k->m_private_image->y0;
10942 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10943 if (p_image->y1 > p_j2k->m_private_image->y1)
10944 p_image->y1 = p_j2k->m_private_image->y1;
10946 l_img_comp = p_image->comps;
10947 for (compno=0; compno < p_image->numcomps; ++compno)
10949 OPJ_INT32 l_comp_x1, l_comp_y1;
10951 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10953 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10954 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10955 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10956 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10958 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10959 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10964 /* Destroy the previous output image*/
10965 if (p_j2k->m_output_image)
10966 opj_image_destroy(p_j2k->m_output_image);
10968 /* Create the ouput image from the information previously computed*/
10969 p_j2k->m_output_image = opj_image_create0();
10970 if (! (p_j2k->m_output_image)) {
10973 opj_copy_image_header(p_image, p_j2k->m_output_image);
10975 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10977 /* customization of the decoding */
10978 j2k_setup_decoding_tile(p_j2k);
10980 /* Decode the codestream */
10981 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10982 opj_image_destroy(p_j2k->m_private_image);
10983 p_j2k->m_private_image = NULL;
10987 /* Move data and copy one information from codec to output image*/
10988 for (compno = 0; compno < p_image->numcomps; compno++) {
10989 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10991 if (p_image->comps[compno].data)
10992 opj_free(p_image->comps[compno].data);
10994 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10996 p_j2k->m_output_image->comps[compno].data = NULL;
11002 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11004 OPJ_UINT32 it_comp;
11006 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11008 if (p_j2k->m_private_image) {
11009 if (p_j2k->m_private_image->comps) {
11010 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11011 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11012 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11013 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11014 if ( res_factor >= max_res){
11015 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11018 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11031 * Encodes all the tiles in a row.
11033 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11034 opj_stream_private_t *p_stream,
11035 opj_event_mgr_t * p_manager )
11038 OPJ_UINT32 l_nb_tiles;
11039 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11040 OPJ_BYTE * l_current_data;
11042 /* preconditions */
11043 assert(p_j2k != 00);
11044 assert(p_stream != 00);
11045 assert(p_manager != 00);
11047 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11048 if (! l_current_data) {
11051 l_max_tile_size = 1000;
11053 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11054 for (i=0;i<l_nb_tiles;++i) {
11055 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11056 opj_free(l_current_data);
11060 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11061 if (l_current_tile_size > l_max_tile_size) {
11062 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11063 if (! l_current_data) {
11066 l_max_tile_size = l_current_tile_size;
11069 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11071 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11076 opj_free(l_current_data);
11081 * Ends the compression procedures and possibility add data to be read after the
11084 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11085 opj_stream_private_t *p_stream,
11086 struct opj_event_mgr * p_manager)
11088 /* customization of the encoding */
11089 j2k_setup_end_compress(p_j2k);
11091 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11101 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11103 * @param p_j2k the jpeg2000 codec.
11104 * @param p_stream the stream object.
11105 * @param p_manager the user event manager.
11107 * @return true if the codec is valid.
11109 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11110 opj_stream_private_t *p_stream,
11111 opj_image_t * p_image,
11112 opj_event_mgr_t * p_manager)
11114 /* preconditions */
11115 assert(p_j2k != 00);
11116 assert(p_stream != 00);
11117 assert(p_manager != 00);
11119 p_j2k->m_private_image = opj_image_create0();
11120 opj_copy_image_header(p_image, p_j2k->m_private_image);
11122 // TODO_MSD: Find a better way
11123 if (p_image->comps) {
11124 OPJ_UINT32 it_comp;
11125 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11126 if (p_image->comps[it_comp].data) {
11127 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11128 p_image->comps[it_comp].data = NULL;
11134 /* customization of the validation */
11135 j2k_setup_encoding_validation (p_j2k);
11137 /* validation of the parameters codec */
11138 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11142 /* customization of the encoding */
11143 j2k_setup_header_writting(p_j2k);
11146 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11156 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11157 OPJ_UINT32 p_tile_index,
11158 opj_stream_private_t *p_stream,
11159 opj_event_mgr_t * p_manager )
11162 if (p_tile_index != p_j2k->m_current_tile_number) {
11163 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11167 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);
11169 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11170 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11171 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11173 /* initialisation before tile encoding */
11174 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11184 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11186 OPJ_UINT32 i,j,k = 0;
11187 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11188 opj_image_comp_t * l_img_comp = 00;
11189 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11190 opj_image_t * l_image = 00;
11191 OPJ_UINT32 l_size_comp, l_remaining;
11192 OPJ_INT32 * l_src_ptr;
11193 l_tilec = p_tcd->tcd_image->tiles->comps;
11194 l_image = p_tcd->image;
11195 l_img_comp = l_image->comps;
11197 for (i=0;i<p_tcd->image->numcomps;++i) {
11198 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11199 l_remaining = l_img_comp->prec & 7; /* (%8) */
11204 if (l_size_comp == 3) {
11208 l_width = (l_tilec->x1 - l_tilec->x0);
11209 l_height = (l_tilec->y1 - l_tilec->y0);
11210 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11211 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11212 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11213 l_stride = l_image_width - l_width;
11214 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11216 switch (l_size_comp) {
11219 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11220 if (l_img_comp->sgnd) {
11221 for (j=0;j<l_height;++j) {
11222 for (k=0;k<l_width;++k) {
11223 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11227 l_src_ptr += l_stride;
11231 for (j=0;j<l_height;++j) {
11232 for (k=0;k<l_width;++k) {
11233 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11237 l_src_ptr += l_stride;
11241 p_data = (OPJ_BYTE*) l_dest_ptr;
11246 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11247 if (l_img_comp->sgnd) {
11248 for (j=0;j<l_height;++j) {
11249 for (k=0;k<l_width;++k) {
11250 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11252 l_src_ptr += l_stride;
11256 for (j=0;j<l_height;++j) {
11257 for (k=0;k<l_width;++k) {
11258 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11260 l_src_ptr += l_stride;
11264 p_data = (OPJ_BYTE*) l_dest_ptr;
11269 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11270 for (j=0;j<l_height;++j) {
11271 for (k=0;k<l_width;++k) {
11272 *(l_dest_ptr++) = *(l_src_ptr++);
11274 l_src_ptr += l_stride;
11277 p_data = (OPJ_BYTE*) l_dest_ptr;
11290 * @param p_j2k the jpeg2000 codec.
11291 * @param p_stream the stream to write data to.
11292 * @param p_manager the user event manager.
11294 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11296 OPJ_UINT32 p_data_size,
11297 opj_stream_private_t *p_stream,
11298 opj_event_mgr_t * p_manager )
11300 opj_tcd_v2_t * l_tcd = 00;
11301 opj_cp_v2_t * l_cp = 00;
11302 opj_tcp_v2_t * l_tcp = 00;
11303 OPJ_UINT32 l_nb_bytes_written;
11304 OPJ_BYTE * l_current_data = 00;
11305 OPJ_UINT32 l_tile_size = 0;
11306 OPJ_UINT32 l_available_data;
11308 /* preconditions */
11309 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11311 l_tcd = p_j2k->m_tcd;
11312 l_cp = &(p_j2k->m_cp);
11313 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11315 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11316 l_available_data = l_tile_size;
11317 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11319 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11320 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11324 l_nb_bytes_written = 0;
11325 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11328 l_current_data += l_nb_bytes_written;
11329 l_available_data -= l_nb_bytes_written;
11331 l_nb_bytes_written = 0;
11332 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11336 l_available_data -= l_nb_bytes_written;
11337 l_nb_bytes_written = l_tile_size - l_available_data;
11339 if ( opj_stream_write_data( p_stream,
11340 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11341 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11345 ++p_j2k->m_current_tile_number;
11352 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11353 * are valid. Developers wanting to extend the library can add their own validation procedures.
11355 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11357 /* preconditions */
11358 assert(p_j2k != 00);
11360 /* DEVELOPER CORNER, insert your custom procedures */
11361 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11363 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11364 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11367 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11368 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11369 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11373 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11374 * are valid. Developers wanting to extend the library can add their own validation procedures.
11376 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11378 /* preconditions */
11379 assert(p_j2k != 00);
11381 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11382 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11384 /* DEVELOPER CORNER, add your custom validation procedure */
11385 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11390 * Sets up the procedures to do on writing header.
11391 * Developers wanting to extend the library can add their own writing procedures.
11393 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11395 /* preconditions */
11396 assert(p_j2k != 00);
11398 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11399 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11400 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11401 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11402 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11405 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11406 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11407 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11409 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11410 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11414 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11416 if (p_j2k->m_cp.comment != 00) {
11417 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11420 /* DEVELOPER CORNER, insert your custom procedures */
11421 if (p_j2k->m_cp.rsiz & MCT) {
11422 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11424 /* End of Developer Corner */
11426 if (p_j2k->cstr_index) {
11427 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11430 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11431 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11435 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11437 OPJ_UINT32 * p_data_written,
11438 OPJ_UINT32 p_total_data_size,
11439 opj_stream_private_t *p_stream,
11440 struct opj_event_mgr * p_manager )
11443 OPJ_UINT32 l_nb_bytes_written = 0;
11444 OPJ_UINT32 l_current_nb_bytes_written;
11445 OPJ_BYTE * l_begin_data = 00;
11447 opj_tcp_v2_t *l_tcp = 00;
11448 opj_tcd_v2_t * l_tcd = 00;
11449 opj_cp_v2_t * l_cp = 00;
11451 l_tcd = p_j2k->m_tcd;
11452 l_cp = &(p_j2k->m_cp);
11453 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11455 l_tcd->cur_pino = 0;
11457 /*Get number of tile parts*/
11458 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11463 l_current_nb_bytes_written = 0;
11464 l_begin_data = p_data;
11465 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11470 l_nb_bytes_written += l_current_nb_bytes_written;
11471 p_data += l_current_nb_bytes_written;
11472 p_total_data_size -= l_current_nb_bytes_written;
11474 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11475 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11476 l_current_nb_bytes_written = 0;
11477 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11478 l_nb_bytes_written += l_current_nb_bytes_written;
11479 p_data += l_current_nb_bytes_written;
11480 p_total_data_size -= l_current_nb_bytes_written;
11482 l_current_nb_bytes_written = 0;
11483 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11484 l_nb_bytes_written += l_current_nb_bytes_written;
11485 p_data += l_current_nb_bytes_written;
11486 p_total_data_size -= l_current_nb_bytes_written;
11489 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11490 l_current_nb_bytes_written = 0;
11491 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11492 l_nb_bytes_written += l_current_nb_bytes_written;
11493 p_data += l_current_nb_bytes_written;
11494 p_total_data_size -= l_current_nb_bytes_written;
11498 l_current_nb_bytes_written = 0;
11499 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11503 l_nb_bytes_written += l_current_nb_bytes_written;
11504 * p_data_written = l_nb_bytes_written;
11506 /* Writing Psot in SOT marker */
11507 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11509 if (l_cp->m_specific_param.m_enc.m_cinema){
11510 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11516 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11518 OPJ_UINT32 * p_data_written,
11519 OPJ_UINT32 p_total_data_size,
11520 opj_stream_private_t *p_stream,
11521 struct opj_event_mgr * p_manager
11524 OPJ_UINT32 tilepartno=0;
11525 OPJ_UINT32 l_nb_bytes_written = 0;
11526 OPJ_UINT32 l_current_nb_bytes_written;
11527 OPJ_UINT32 l_part_tile_size;
11528 OPJ_UINT32 tot_num_tp;
11531 OPJ_BYTE * l_begin_data;
11532 opj_tcp_v2_t *l_tcp = 00;
11533 opj_tcd_v2_t * l_tcd = 00;
11534 opj_cp_v2_t * l_cp = 00;
11537 l_tcd = p_j2k->m_tcd;
11538 l_cp = &(p_j2k->m_cp);
11539 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11541 /*Get number of tile parts*/
11542 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11544 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11545 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11546 l_current_nb_bytes_written = 0;
11547 l_part_tile_size = 0;
11548 l_begin_data = p_data;
11550 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11554 l_nb_bytes_written += l_current_nb_bytes_written;
11555 p_data += l_current_nb_bytes_written;
11556 p_total_data_size -= l_current_nb_bytes_written;
11557 l_part_tile_size += l_nb_bytes_written;
11559 l_current_nb_bytes_written = 0;
11560 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11564 p_data += l_current_nb_bytes_written;
11565 l_nb_bytes_written += l_current_nb_bytes_written;
11566 p_total_data_size -= l_current_nb_bytes_written;
11567 l_part_tile_size += l_nb_bytes_written;
11569 /* Writing Psot in SOT marker */
11570 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11572 if (l_cp->m_specific_param.m_enc.m_cinema) {
11573 j2k_update_tlm(p_j2k,l_part_tile_size);
11576 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11579 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11580 l_tcd->cur_pino = pino;
11582 /*Get number of tile parts*/
11583 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11584 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11585 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11586 l_current_nb_bytes_written = 0;
11587 l_part_tile_size = 0;
11588 l_begin_data = p_data;
11590 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11594 l_nb_bytes_written += l_current_nb_bytes_written;
11595 p_data += l_current_nb_bytes_written;
11596 p_total_data_size -= l_current_nb_bytes_written;
11597 l_part_tile_size += l_current_nb_bytes_written;
11599 l_current_nb_bytes_written = 0;
11601 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11605 l_nb_bytes_written += l_current_nb_bytes_written;
11606 p_data += l_current_nb_bytes_written;
11607 p_total_data_size -= l_current_nb_bytes_written;
11608 l_part_tile_size += l_current_nb_bytes_written;
11610 /* Writing Psot in SOT marker */
11611 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11613 if (l_cp->m_specific_param.m_enc.m_cinema) {
11614 j2k_update_tlm(p_j2k,l_part_tile_size);
11617 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11621 *p_data_written = l_nb_bytes_written;
11627 * Writes the updated tlm.
11629 * @param p_stream the stream to write data to.
11630 * @param p_j2k J2K codec.
11631 * @param p_manager the user event manager.
11633 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11634 struct opj_stream_private *p_stream,
11635 struct opj_event_mgr * p_manager )
11637 OPJ_UINT32 l_tlm_size;
11638 OPJ_SIZE_T l_tlm_position, l_current_position;
11640 /* preconditions */
11641 assert(p_j2k != 00);
11642 assert(p_manager != 00);
11643 assert(p_stream != 00);
11645 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11646 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11647 l_current_position = opj_stream_tell(p_stream);
11649 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11653 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) {
11657 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11666 * Ends the encoding, i.e. frees memory.
11668 * @param p_stream the stream to write data to.
11669 * @param p_j2k J2K codec.
11670 * @param p_manager the user event manager.
11672 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11673 struct opj_stream_private *p_stream,
11674 struct opj_event_mgr * p_manager )
11676 /* preconditions */
11677 assert(p_j2k != 00);
11678 assert(p_manager != 00);
11679 assert(p_stream != 00);
11681 tcd_destroy_v2(p_j2k->m_tcd);
11684 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11685 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11686 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11687 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11690 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11691 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11692 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11695 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11701 * Destroys the memory associated with the decoding of headers.
11703 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11704 opj_stream_private_t *p_stream,
11705 opj_event_mgr_t * p_manager )
11707 /* preconditions */
11708 assert(p_j2k != 00);
11709 assert(p_stream != 00);
11710 assert(p_manager != 00);
11712 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11713 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11714 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11717 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11726 * @param p_stream the stream to write data to.
11727 * @param p_j2k J2K codec.
11728 * @param p_manager the user event manager.
11730 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11731 struct opj_stream_private *p_stream,
11732 struct opj_event_mgr * p_manager )
11734 opj_codestream_info_t * l_cstr_info = 00;
11736 /* preconditions */
11737 assert(p_j2k != 00);
11738 assert(p_manager != 00);
11739 assert(p_stream != 00);
11742 /* TODO mergeV2: check this part which use cstr_info */
11743 /*l_cstr_info = p_j2k->cstr_info;
11747 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));
11749 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11750 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11752 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11754 l_cstr_info->tw = p_j2k->m_cp.tw;
11755 l_cstr_info->th = p_j2k->m_cp.th;
11757 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11758 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11759 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11760 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11762 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11764 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11766 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11768 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11769 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11772 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11774 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11776 /*l_cstr_info->maxmarknum = 100;
11777 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11778 l_cstr_info->marknum = 0;
11781 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);
11785 * Creates a tile-coder decoder.
11787 * @param p_stream the stream to write data to.
11788 * @param p_j2k J2K codec.
11789 * @param p_manager the user event manager.
11791 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11792 struct opj_stream_private *p_stream,
11793 struct opj_event_mgr * p_manager )
11795 /* preconditions */
11796 assert(p_j2k != 00);
11797 assert(p_manager != 00);
11798 assert(p_stream != 00);
11800 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11802 if (! p_j2k->m_tcd) {
11803 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11807 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11808 tcd_destroy_v2(p_j2k->m_tcd);
11819 * @param p_j2k the jpeg2000 codec.
11820 * @param p_stream the stream to write data to.
11821 * @param p_manager the user event manager.
11823 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11824 OPJ_UINT32 p_tile_index,
11826 OPJ_UINT32 p_data_size,
11827 opj_stream_private_t *p_stream,
11828 opj_event_mgr_t * p_manager )
11830 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11831 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11835 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11836 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);