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);
482 Write the COD marker (coding style default)
483 @param j2k J2K handle
485 static void j2k_write_cod(opj_j2k_t *j2k);
488 * Writes the COD marker (Coding style default)
490 * @param p_stream the stream to write data to.
491 * @param p_j2k J2K codec.
492 * @param p_manager the user event manager.
494 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
495 struct opj_stream_private *p_stream,
496 struct opj_event_mgr * p_manager );
499 * Reads a COD marker (Coding Styke defaults)
500 * @param p_header_data the data contained in the COD box.
501 * @param p_j2k the jpeg2000 codec.
502 * @param p_header_size the size of the data contained in the COD marker.
503 * @param p_manager the user event manager.
505 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
506 OPJ_BYTE * p_header_data,
507 OPJ_UINT32 p_header_size,
508 opj_event_mgr_t * p_manager);
511 Write the COC marker (coding style component)
512 @param j2k J2K handle
513 @param compno Number of the component concerned by the information written
515 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
518 * Writes the COC marker (Coding style component)
520 * @param p_comp_number the index of the component to output.
521 * @param p_stream the stream to write data to.
522 * @param p_j2k J2K codec.
523 * @param p_manager the user event manager.
525 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
526 OPJ_UINT32 p_comp_number,
527 struct opj_stream_private *p_stream,
528 struct opj_event_mgr * p_manager
531 * Writes the COC marker (Coding style component)
533 * @param p_comp_no the index of the component to output.
534 * @param p_stream the stream to write data to.
535 * @param p_j2k J2K codec.
536 * @param p_manager the user event manager.
538 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
539 OPJ_UINT32 p_comp_no,
541 OPJ_UINT32 * p_data_written,
542 struct opj_event_mgr * p_manager );
545 * Gets the maximum size taken by a coc.
547 * @param p_j2k the jpeg2000 codec to use.
549 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
553 * Reads a COC marker (Coding Style Component)
554 * @param p_header_data the data contained in the COC box.
555 * @param p_j2k the jpeg2000 codec.
556 * @param p_header_size the size of the data contained in the COC marker.
557 * @param p_manager the user event manager.
559 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
560 OPJ_BYTE * p_header_data,
561 OPJ_UINT32 p_header_size,
562 opj_event_mgr_t * p_manager );
565 Write the value concerning the specified component in the marker QCD and QCC
566 @param j2k J2K handle
567 @param compno Number of the component concerned by the information written
569 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
572 Write the QCD marker (quantization default)
573 @param j2k J2K handle
575 static void j2k_write_qcd(opj_j2k_t *j2k);
578 * Writes the QCD marker (quantization default)
580 * @param p_comp_number the index of the component to output.
581 * @param p_stream the stream to write data to.
582 * @param p_j2k J2K codec.
583 * @param p_manager the user event manager.
585 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
586 struct opj_stream_private *p_stream,
587 struct opj_event_mgr * p_manager );
590 * Reads a QCD marker (Quantization defaults)
591 * @param p_header_data the data contained in the QCD box.
592 * @param p_j2k the jpeg2000 codec.
593 * @param p_header_size the size of the data contained in the QCD marker.
594 * @param p_manager the user event manager.
596 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
597 OPJ_BYTE * p_header_data,
598 OPJ_UINT32 p_header_size,
599 opj_event_mgr_t * p_manager );
602 Write the QCC marker (quantization component)
603 @param j2k J2K handle
604 @param compno Number of the component concerned by the information written
606 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
609 * Writes the QCC marker (quantization component)
611 * @param p_comp_no the index of the component to output.
612 * @param p_stream the stream to write data to.
613 * @param p_j2k J2K codec.
614 * @param p_manager the user event manager.
616 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
617 OPJ_UINT32 p_comp_no,
618 struct opj_stream_private *p_stream,
619 struct opj_event_mgr * p_manager );
622 * Writes the QCC marker (quantization component)
624 * @param p_comp_no the index of the component to output.
625 * @param p_stream the stream to write data to.
626 * @param p_j2k J2K codec.
627 * @param p_manager the user event manager.
629 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
630 OPJ_UINT32 p_comp_no,
632 OPJ_UINT32 * p_data_written,
633 struct opj_event_mgr * p_manager );
636 * Gets the maximum size taken by a qcc.
638 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
641 * Reads a QCC marker (Quantization component)
642 * @param p_header_data the data contained in the QCC box.
643 * @param p_j2k the jpeg2000 codec.
644 * @param p_header_size the size of the data contained in the QCC marker.
645 * @param p_manager the user event manager.
647 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
648 OPJ_BYTE * p_header_data,
649 OPJ_UINT32 p_header_size,
650 opj_event_mgr_t * p_manager);
653 Write the POC marker (progression order change)
654 @param j2k J2K handle
656 static void j2k_write_poc(opj_j2k_t *j2k);
659 * Writes the POC marker (Progression Order Change)
661 * @param p_stream the stream to write data to.
662 * @param p_j2k J2K codec.
663 * @param p_manager the user event manager.
665 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
666 struct opj_stream_private *p_stream,
667 struct opj_event_mgr * p_manager );
670 * Writes the POC marker (Progression Order Change)
672 * @param p_stream the stream to write data to.
673 * @param p_j2k J2K codec.
674 * @param p_manager the user event manager.
676 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
678 OPJ_UINT32 * p_data_written,
679 struct opj_event_mgr * p_manager );
682 * Gets the maximum size taken by the writting of a POC.
684 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
687 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
689 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
692 * Gets the maximum size taken by the headers of the SOT.
694 * @param p_j2k the jpeg2000 codec to use.
696 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
699 * Reads a POC marker (Progression Order Change)
701 * @param p_header_data the data contained in the POC box.
702 * @param p_j2k the jpeg2000 codec.
703 * @param p_header_size the size of the data contained in the POC marker.
704 * @param p_manager the user event manager.
706 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
707 OPJ_BYTE * p_header_data,
708 OPJ_UINT32 p_header_size,
709 opj_event_mgr_t * p_manager );
712 * Reads a CRG marker (Component registration)
714 * @param p_header_data the data contained in the TLM box.
715 * @param p_j2k the jpeg2000 codec.
716 * @param p_header_size the size of the data contained in the TLM marker.
717 * @param p_manager the user event manager.
719 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
720 OPJ_BYTE * p_header_data,
721 OPJ_UINT32 p_header_size,
722 opj_event_mgr_t * p_manager );
724 * Reads a TLM marker (Tile Length Marker)
726 * @param p_header_data the data contained in the TLM box.
727 * @param p_j2k the jpeg2000 codec.
728 * @param p_header_size the size of the data contained in the TLM marker.
729 * @param p_manager the user event manager.
731 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
732 OPJ_BYTE * p_header_data,
733 OPJ_UINT32 p_header_size,
734 opj_event_mgr_t * p_manager);
737 * Writes the updated tlm.
739 * @param p_stream the stream to write data to.
740 * @param p_j2k J2K codec.
741 * @param p_manager the user event manager.
743 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
744 struct opj_stream_private *p_stream,
745 struct opj_event_mgr * p_manager );
748 * Reads a PLM marker (Packet length, main header marker)
750 * @param p_header_data the data contained in the TLM box.
751 * @param p_j2k the jpeg2000 codec.
752 * @param p_header_size the size of the data contained in the TLM marker.
753 * @param p_manager the user event manager.
755 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
756 OPJ_BYTE * p_header_data,
757 OPJ_UINT32 p_header_size,
758 opj_event_mgr_t * p_manager);
760 * Reads a PLT marker (Packet length, tile-part header)
762 * @param p_header_data the data contained in the PLT box.
763 * @param p_j2k the jpeg2000 codec.
764 * @param p_header_size the size of the data contained in the PLT marker.
765 * @param p_manager the user event manager.
767 static opj_bool opj_j2k_read_plt ( 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 );
774 * Reads a PPM marker (Packed packet headers, main header)
776 * @param p_header_data the data contained in the POC box.
777 * @param p_j2k the jpeg2000 codec.
778 * @param p_header_size the size of the data contained in the POC marker.
779 * @param p_manager the user event manager.
782 static opj_bool j2k_read_ppm_v2 (
784 OPJ_BYTE * p_header_data,
785 OPJ_UINT32 p_header_size,
786 struct opj_event_mgr * p_manager
790 static opj_bool j2k_read_ppm_v3 (
792 OPJ_BYTE * p_header_data,
793 OPJ_UINT32 p_header_size,
794 struct opj_event_mgr * p_manager
799 * Reads a PPT marker (Packed packet headers, tile-part header)
801 * @param p_header_data the data contained in the PPT box.
802 * @param p_j2k the jpeg2000 codec.
803 * @param p_header_size the size of the data contained in the PPT marker.
804 * @param p_manager the user event manager.
806 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
807 OPJ_BYTE * p_header_data,
808 OPJ_UINT32 p_header_size,
809 opj_event_mgr_t * p_manager );
811 Write the TLM marker (Mainheader)
812 @param j2k J2K handle
814 static void j2k_write_tlm(opj_j2k_t *j2k);
817 * Writes the TLM marker (Tile Length Marker)
819 * @param p_stream the stream to write data to.
820 * @param p_j2k J2K codec.
821 * @param p_manager the user event manager.
823 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
824 struct opj_stream_private *p_stream,
825 struct opj_event_mgr * p_manager );
828 Write the SOT marker (start of tile-part)
829 @param j2k J2K handle
831 static void j2k_write_sot(opj_j2k_t *j2k);
834 * Writes the SOT marker (Start of tile-part)
836 * @param p_stream the stream to write data to.
837 * @param p_j2k J2K codec.
838 * @param p_manager the user event manager.
840 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
842 OPJ_UINT32 * p_data_written,
843 const struct opj_stream_private *p_stream,
844 struct opj_event_mgr * p_manager );
847 * Reads a PPT marker (Packed packet headers, tile-part header)
849 * @param p_header_data the data contained in the PPT box.
850 * @param p_j2k the jpeg2000 codec.
851 * @param p_header_size the size of the data contained in the PPT marker.
852 * @param p_manager the user event manager.
854 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
855 OPJ_BYTE * p_header_data,
856 OPJ_UINT32 p_header_size,
857 opj_event_mgr_t * p_manager );
860 Write the SOD marker (start of data)
861 @param j2k J2K handle
862 @param tile_coder Pointer to a TCD handle
864 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
867 * Writes the SOD marker (Start of data)
869 * @param p_stream the stream to write data to.
870 * @param p_j2k J2K codec.
871 * @param p_manager the user event manager.
873 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
874 struct opj_tcd_v2 * p_tile_coder,
876 OPJ_UINT32 * p_data_written,
877 OPJ_UINT32 p_total_data_size,
878 const struct opj_stream_private *p_stream,
879 struct opj_event_mgr * p_manager );
882 * Reads a SOD marker (Start Of Data)
884 * @param p_header_data the data contained in the SOD box.
885 * @param p_j2k the jpeg2000 codec.
886 * @param p_header_size the size of the data contained in the SOD marker.
887 * @param p_manager the user event manager.
889 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
890 opj_stream_private_t *p_stream,
891 opj_event_mgr_t * p_manager );
894 * Updates the Tile Length Marker.
896 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
898 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
899 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
901 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
902 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
906 Write the RGN marker (region-of-interest)
907 @param j2k J2K handle
908 @param compno Number of the component concerned by the information written
909 @param tileno Number of the tile concerned by the information written
911 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
914 * Writes the RGN marker (Region Of Interest)
916 * @param p_tile_no the tile to output
917 * @param p_comp_no the component to output
918 * @param p_stream the stream to write data to.
919 * @param p_j2k J2K codec.
920 * @param p_manager the user event manager.
922 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
923 OPJ_UINT32 p_tile_no,
924 OPJ_UINT32 p_comp_no,
925 struct opj_stream_private *p_stream,
926 struct opj_event_mgr * p_manager );
929 * Reads a RGN marker (Region Of Interest)
931 * @param p_header_data the data contained in the POC box.
932 * @param p_j2k the jpeg2000 codec.
933 * @param p_header_size the size of the data contained in the POC marker.
934 * @param p_manager the user event manager.
936 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
937 OPJ_BYTE * p_header_data,
938 OPJ_UINT32 p_header_size,
939 opj_event_mgr_t * p_manager );
942 * Writes the EOC marker (End of Codestream)
944 * @param p_stream the stream to write data to.
945 * @param p_j2k J2K codec.
946 * @param p_manager the user event manager.
948 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
949 struct opj_stream_private *p_stream,
950 struct opj_event_mgr * p_manager );
953 Write the EOC marker (end of codestream)
954 @param j2k J2K handle
956 static void j2k_write_eoc(opj_j2k_t *j2k);
958 Read the EOC marker (end of codestream)
959 @param j2k J2K handle
961 static void j2k_read_eoc(opj_j2k_t *j2k);
964 * Reads a EOC marker (End Of Codestream)
966 * @param p_header_data the data contained in the SOD box.
967 * @param p_j2k the jpeg2000 codec.
968 * @param p_header_size the size of the data contained in the SOD marker.
969 * @param p_manager the user event manager.
972 static opj_bool j2k_read_eoc_v2 (
974 struct opj_stream_private *p_stream,
975 struct opj_event_mgr * p_manager
982 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
984 * @param p_stream the stream to write data to.
985 * @param p_j2k J2K codec.
986 * @param p_manager the user event manager.
988 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
989 struct opj_stream_private *p_stream,
990 struct opj_event_mgr * p_manager );
995 * @param p_stream the stream to write data to.
996 * @param p_j2k J2K codec.
997 * @param p_manager the user event manager.
999 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1000 struct opj_stream_private *p_stream,
1001 struct opj_event_mgr * p_manager );
1004 Read an unknown marker
1005 @param j2k J2K handle
1007 static void j2k_read_unk(opj_j2k_t *j2k);
1009 Add main header marker information
1010 @param cstr_info Codestream information structure
1011 @param type marker type
1012 @param pos byte offset of marker segment
1013 @param len length of marker segment
1015 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1017 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1019 Add tile header marker information
1020 @param tileno tile index number
1021 @param cstr_info Codestream information structure
1022 @param type marker type
1023 @param pos byte offset of marker segment
1024 @param len length of marker segment
1026 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1028 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);
1031 * Reads an unknown marker
1033 * @param p_stream the stream object to read from.
1034 * @param p_j2k the jpeg2000 codec.
1035 * @param p_manager the user event manager.
1037 * @return true if the marker could be deduced.
1039 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1040 struct opj_stream_private *p_stream,
1041 OPJ_UINT32 *output_marker,
1042 struct opj_event_mgr * p_manager );
1045 * Writes the MCT marker (Multiple Component Transform)
1047 * @param p_stream the stream to write data to.
1048 * @param p_j2k J2K codec.
1049 * @param p_manager the user event manager.
1051 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1052 opj_mct_data_t * p_mct_record,
1053 struct opj_stream_private *p_stream,
1054 struct opj_event_mgr * p_manager );
1057 * Reads a MCT marker (Multiple Component Transform)
1059 * @param p_header_data the data contained in the MCT box.
1060 * @param p_j2k the jpeg2000 codec.
1061 * @param p_header_size the size of the data contained in the MCT marker.
1062 * @param p_manager the user event manager.
1064 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1065 OPJ_BYTE * p_header_data,
1066 OPJ_UINT32 p_header_size,
1067 struct opj_event_mgr * p_manager );
1070 * Writes the MCC marker (Multiple Component Collection)
1072 * @param p_stream the stream to write data to.
1073 * @param p_j2k J2K codec.
1074 * @param p_manager the user event manager.
1076 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1077 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1078 struct opj_stream_private *p_stream,
1079 struct opj_event_mgr * p_manager );
1083 * Reads a MCC marker (Multiple Component Collection)
1085 * @param p_header_data the data contained in the MCC box.
1086 * @param p_j2k the jpeg2000 codec.
1087 * @param p_header_size the size of the data contained in the MCC marker.
1088 * @param p_manager the user event manager.
1090 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1091 OPJ_BYTE * p_header_data,
1092 OPJ_UINT32 p_header_size,
1093 struct opj_event_mgr * p_manager );
1096 * Writes the MCO marker (Multiple component transformation ordering)
1098 * @param p_stream the stream to write data to.
1099 * @param p_j2k J2K codec.
1100 * @param p_manager the user event manager.
1102 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1103 struct opj_stream_private *p_stream,
1104 struct opj_event_mgr * p_manager );
1107 * Reads a MCO marker (Multiple Component Transform Ordering)
1109 * @param p_header_data the data contained in the MCO box.
1110 * @param p_j2k the jpeg2000 codec.
1111 * @param p_header_size the size of the data contained in the MCO marker.
1112 * @param p_manager the user event manager.
1114 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1115 OPJ_BYTE * p_header_data,
1116 OPJ_UINT32 p_header_size,
1117 struct opj_event_mgr * p_manager );
1119 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1121 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1123 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1124 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1127 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1128 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1129 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1131 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1132 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1133 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1134 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1138 * Ends the encoding, i.e. frees memory.
1140 * @param p_stream the stream to write data to.
1141 * @param p_j2k J2K codec.
1142 * @param p_manager the user event manager.
1144 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1145 struct opj_stream_private *p_stream,
1146 struct opj_event_mgr * p_manager );
1149 * Writes the CBD marker (Component bit depth definition)
1151 * @param p_stream the stream to write data to.
1152 * @param p_j2k J2K codec.
1153 * @param p_manager the user event manager.
1155 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1156 struct opj_stream_private *p_stream,
1157 struct opj_event_mgr * p_manager );
1160 * Reads a CBD marker (Component bit depth definition)
1161 * @param p_header_data the data contained in the CBD box.
1162 * @param p_j2k the jpeg2000 codec.
1163 * @param p_header_size the size of the data contained in the CBD marker.
1164 * @param p_manager the user event manager.
1166 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1167 OPJ_BYTE * p_header_data,
1168 OPJ_UINT32 p_header_size,
1169 struct opj_event_mgr * p_manager);
1172 * Writes the image components.
1174 * @param p_stream the stream to write data to.
1175 * @param p_j2k J2K codec.
1176 * @param p_manager the user event manager.
1178 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1179 struct opj_stream_private *p_stream,
1180 struct opj_event_mgr * p_manager );
1183 * Writes regions of interests.
1185 * @param p_stream the stream to write data to.
1186 * @param p_j2k J2K codec.
1187 * @param p_manager the user event manager.
1189 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1190 struct opj_stream_private *p_stream,
1191 struct opj_event_mgr * p_manager );
1196 * @param p_stream the stream to write data to.
1197 * @param p_j2k J2K codec.
1198 * @param p_manager the user event manager.
1200 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1201 struct opj_stream_private *p_stream,
1202 struct opj_event_mgr * p_manager );
1205 * Checks the progression order changes values. Tells of the poc given as input are valid.
1206 * A nice message is outputted at errors.
1208 * @param p_pocs the progression order changes.
1209 * @param p_nb_pocs the number of progression order changes.
1210 * @param p_nb_resolutions the number of resolutions.
1211 * @param numcomps the number of components
1212 * @param numlayers the number of layers.
1214 * @return true if the pocs are valid.
1216 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1217 OPJ_UINT32 p_nb_pocs,
1218 OPJ_UINT32 p_nb_resolutions,
1219 OPJ_UINT32 numcomps,
1220 OPJ_UINT32 numlayers,
1221 opj_event_mgr_t * p_manager);
1224 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1226 * @param cp the coding parameters.
1227 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1228 * @param tileno the given tile.
1230 * @return the number of tile parts.
1232 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1234 /** mem allocation for TLM marker*/
1235 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1238 * Calculates the total number of tile parts needed by the encoder to
1239 * encode such an image. If not enough memory is available, then the function return false.
1241 * @param p_nb_tiles pointer that will hold the number of tile parts.
1242 * @param cp the coding parameters for the image.
1243 * @param image the image to encode.
1244 * @param p_j2k the p_j2k encoder.
1245 * @param p_manager the user event manager.
1247 * @return true if the function was successful, false else.
1249 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1251 OPJ_UINT32 * p_nb_tiles,
1253 opj_event_mgr_t * p_manager);
1255 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1257 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1259 static opj_codestream_index_t* j2k_create_cstr_index(void);
1265 /* ----------------------------------------------------------------------- */
1266 typedef struct j2k_prog_order{
1267 OPJ_PROG_ORDER enum_prog;
1271 j2k_prog_order_t j2k_prog_order_list[] = {
1277 {(OPJ_PROG_ORDER)-1, ""}
1285 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1293 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1295 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1297 j2k_read_int16_to_float,
1298 j2k_read_int32_to_float,
1299 j2k_read_float32_to_float,
1300 j2k_read_float64_to_float
1303 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1305 j2k_read_int16_to_int32,
1306 j2k_read_int32_to_int32,
1307 j2k_read_float32_to_int32,
1308 j2k_read_float64_to_int32
1311 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1313 j2k_write_float_to_int16,
1314 j2k_write_float_to_int32,
1315 j2k_write_float_to_float,
1316 j2k_write_float_to_float64
1319 typedef struct opj_dec_memory_marker_handler
1323 /** value of the state when the marker can appear */
1325 /** action linked to the marker */
1326 opj_bool (*handler) (
1327 opj_j2k_v2_t *p_j2k,
1328 OPJ_BYTE * p_header_data,
1329 OPJ_UINT32 p_header_size,
1330 struct opj_event_mgr * p_manager
1333 opj_dec_memory_marker_handler_t;
1335 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1338 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1339 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1340 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1341 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1342 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1343 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1344 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1345 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1346 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1347 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1348 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1349 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1350 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1352 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1353 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1354 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1355 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1356 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1357 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1359 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1360 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1361 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1362 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1363 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1364 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1365 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1366 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1367 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1368 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1369 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1370 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1371 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1373 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1374 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1375 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1376 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1377 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1378 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1380 #ifdef TODO_MS /* FIXME */
1381 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1382 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1383 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1384 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1386 #endif /* USE_JPWL */
1388 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1389 {J2K_MS_INSEC, 0, j2k_read_insec}
1390 #endif /* USE_JPSEC */
1391 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1396 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1398 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1399 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1403 for (i=0;i<p_nb_elem;++i) {
1404 opj_read_bytes(l_src_data,&l_temp,2);
1406 l_src_data+=sizeof(OPJ_INT16);
1408 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1412 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1414 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1415 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1419 for (i=0;i<p_nb_elem;++i) {
1420 opj_read_bytes(l_src_data,&l_temp,4);
1422 l_src_data+=sizeof(OPJ_INT32);
1424 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1428 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1430 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1431 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1435 for (i=0;i<p_nb_elem;++i) {
1436 opj_read_float(l_src_data,&l_temp);
1438 l_src_data+=sizeof(OPJ_FLOAT32);
1440 *(l_dest_data++) = l_temp;
1444 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1446 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1447 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1451 for (i=0;i<p_nb_elem;++i) {
1452 opj_read_double(l_src_data,&l_temp);
1454 l_src_data+=sizeof(OPJ_FLOAT64);
1456 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1460 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1462 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1463 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1467 for (i=0;i<p_nb_elem;++i) {
1468 opj_read_bytes(l_src_data,&l_temp,2);
1470 l_src_data+=sizeof(OPJ_INT16);
1472 *(l_dest_data++) = (OPJ_INT32) l_temp;
1476 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1478 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1479 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1483 for (i=0;i<p_nb_elem;++i) {
1484 opj_read_bytes(l_src_data,&l_temp,4);
1486 l_src_data+=sizeof(OPJ_INT32);
1488 *(l_dest_data++) = (OPJ_INT32) l_temp;
1492 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1494 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1495 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1499 for (i=0;i<p_nb_elem;++i) {
1500 opj_read_float(l_src_data,&l_temp);
1502 l_src_data+=sizeof(OPJ_FLOAT32);
1504 *(l_dest_data++) = (OPJ_INT32) l_temp;
1508 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1510 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1511 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1515 for (i=0;i<p_nb_elem;++i) {
1516 opj_read_double(l_src_data,&l_temp);
1518 l_src_data+=sizeof(OPJ_FLOAT64);
1520 *(l_dest_data++) = (OPJ_INT32) l_temp;
1524 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1526 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1527 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1531 for (i=0;i<p_nb_elem;++i) {
1532 l_temp = (OPJ_UINT32) *(l_src_data++);
1534 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1536 l_dest_data+=sizeof(OPJ_INT16);
1540 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1542 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1543 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1547 for (i=0;i<p_nb_elem;++i) {
1548 l_temp = (OPJ_UINT32) *(l_src_data++);
1550 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1552 l_dest_data+=sizeof(OPJ_INT32);
1556 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1558 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1559 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1563 for (i=0;i<p_nb_elem;++i) {
1564 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1566 opj_write_float(l_dest_data,l_temp);
1568 l_dest_data+=sizeof(OPJ_FLOAT32);
1572 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1574 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1575 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1579 for (i=0;i<p_nb_elem;++i) {
1580 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1582 opj_write_double(l_dest_data,l_temp);
1584 l_dest_data+=sizeof(OPJ_FLOAT64);
1590 * Converts an enum type progression order to string type.
1592 * @param prg_order the progression order to get.
1594 * @return the string representation of the given progression order.
1596 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1597 j2k_prog_order_t *po;
1598 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1599 if(po->enum_prog == prg_order){
1600 return po->str_prog;
1603 return po->str_prog;
1607 * Checks the progression order changes values. Tells if the poc given as input are valid.
1609 * @param p_pocs the progression order changes.
1610 * @param p_nb_pocs the number of progression order changes.
1611 * @param p_nb_resolutions the number of resolutions.
1612 * @param numcomps the number of components
1613 * @param numlayers the number of layers.
1614 * @param p_manager the user event manager.
1616 * @return true if the pocs are valid.
1618 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1619 OPJ_UINT32 p_nb_pocs,
1620 OPJ_UINT32 p_nb_resolutions,
1621 OPJ_UINT32 p_num_comps,
1622 OPJ_UINT32 p_num_layers,
1623 opj_event_mgr_t * p_manager)
1625 OPJ_UINT32* packet_array;
1626 OPJ_UINT32 index , resno, compno, layno;
1628 OPJ_UINT32 step_c = 1;
1629 OPJ_UINT32 step_r = p_num_comps * step_c;
1630 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1631 opj_bool loss = OPJ_FALSE;
1632 OPJ_UINT32 layno0 = 0;
1634 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1635 if (packet_array == 00) {
1636 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1639 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1641 if (p_nb_pocs == 0) {
1645 index = step_r * p_pocs->resno0;
1646 // take each resolution for each poc
1647 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1649 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1651 // take each comp of each resolution for each poc
1652 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1653 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1655 // and finally take each layer of each res of ...
1656 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1657 //index = step_r * resno + step_c * compno + step_l * layno;
1658 packet_array[comp_index] = 1;
1659 comp_index += step_l;
1662 res_index += step_c;
1669 // iterate through all the pocs
1670 for (i = 1; i < p_nb_pocs ; ++i) {
1671 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1673 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1674 index = step_r * p_pocs->resno0;
1676 // take each resolution for each poc
1677 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1678 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1680 // take each comp of each resolution for each poc
1681 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1682 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1684 // and finally take each layer of each res of ...
1685 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1686 //index = step_r * resno + step_c * compno + step_l * layno;
1687 packet_array[comp_index] = 1;
1688 comp_index += step_l;
1691 res_index += step_c;
1701 for (layno = 0; layno < p_num_layers ; ++layno) {
1702 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1703 for (compno = 0; compno < p_num_comps; ++compno) {
1704 loss |= (packet_array[index]!=1);
1705 //index = step_r * resno + step_c * compno + step_l * layno;
1712 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1715 opj_free(packet_array);
1720 /* ----------------------------------------------------------------------- */
1721 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1724 int tpnum=1,tpend=0;
1725 opj_tcp_t *tcp = &cp->tcps[tileno];
1726 prog = j2k_convert_progression_order(tcp->prg);
1731 if( cp->tp_flag == prog[i] ){
1732 tpend=1;cp->tp_pos=i;
1736 tpnum= tpnum * tcp->pocs[pino].compE;
1739 tpnum= tpnum * tcp->pocs[pino].resE;
1742 tpnum= tpnum * tcp->pocs[pino].prcE;
1745 tpnum= tpnum * tcp->pocs[pino].layE;
1757 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1759 * @param cp the coding parameters.
1760 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1761 * @param tileno the given tile.
1763 * @return the number of tile parts.
1765 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1767 const OPJ_CHAR *prog = 00;
1769 OPJ_UINT32 tpnum = 1;
1770 opj_tcp_v2_t *tcp = 00;
1771 opj_poc_t * l_current_poc = 00;
1774 assert(tileno < (cp->tw * cp->th));
1775 assert(pino < (cp->tcps[tileno].numpocs + 1));
1777 /* get the given tile coding parameter */
1778 tcp = &cp->tcps[tileno];
1781 l_current_poc = &(tcp->pocs[pino]);
1782 assert(l_current_poc != 0);
1784 /* get the progression order as a character string */
1785 prog = j2k_convert_progression_order(tcp->prg);
1786 assert(strlen(prog) > 0);
1788 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1792 /* component wise */
1794 tpnum *= l_current_poc->compE;
1796 /* resolution wise */
1798 tpnum *= l_current_poc->resE;
1802 tpnum *= l_current_poc->prcE;
1806 tpnum *= l_current_poc->layE;
1809 /* whould we split here ? */
1810 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1811 cp->m_specific_param.m_enc.m_tp_pos=i;
1823 /** mem allocation for TLM marker*/
1824 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1825 int pino,tileno,totnum_tp=0;
1827 OPJ_ARG_NOT_USED(img_numcomp);
1829 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1830 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1831 int cur_totnum_tp = 0;
1832 opj_tcp_t *tcp = &cp->tcps[tileno];
1833 for(pino = 0; pino <= tcp->numpocs; pino++) {
1835 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1836 if(!pi) { return -1;}
1837 tp_num = j2k_get_num_tp(cp,pino,tileno);
1838 totnum_tp = totnum_tp + tp_num;
1839 cur_totnum_tp = cur_totnum_tp + tp_num;
1840 pi_destroy(pi, cp, tileno);
1842 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1844 if (j2k->cstr_info) {
1845 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1846 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1854 * Calculates the total number of tile parts needed by the encoder to
1855 * encode such an image. If not enough memory is available, then the function return false.
1857 * @param p_nb_tiles pointer that will hold the number of tile parts.
1858 * @param cp the coding parameters for the image.
1859 * @param image the image to encode.
1860 * @param p_j2k the p_j2k encoder.
1861 * @param p_manager the user event manager.
1863 * @return true if the function was successful, false else.
1865 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1867 OPJ_UINT32 * p_nb_tiles,
1869 opj_event_mgr_t * p_manager)
1871 OPJ_UINT32 pino,tileno;
1872 OPJ_UINT32 l_nb_tiles;
1876 assert(p_nb_tiles != 00);
1878 assert(image != 00);
1879 assert(p_j2k != 00);
1880 assert(p_manager != 00);
1882 l_nb_tiles = cp->tw * cp->th;
1887 /* TODO mergeV2: check this part which use cstr_info */
1888 /*if (p_j2k->cstr_info) {
1889 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1891 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1892 OPJ_UINT32 cur_totnum_tp = 0;
1894 pi_update_encoding_parameters(image,cp,tileno);
1896 for (pino = 0; pino <= tcp->numpocs; ++pino)
1898 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1900 *p_nb_tiles = *p_nb_tiles + tp_num;
1902 cur_totnum_tp += tp_num;
1905 tcp->m_nb_tile_parts = cur_totnum_tp;
1907 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1908 if (l_info_tile_ptr->tp == 00) {
1912 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1914 l_info_tile_ptr->num_tps = cur_totnum_tp;
1921 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1922 OPJ_UINT32 cur_totnum_tp = 0;
1924 pi_update_encoding_parameters(image,cp,tileno);
1926 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1927 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1929 *p_nb_tiles = *p_nb_tiles + tp_num;
1931 cur_totnum_tp += tp_num;
1933 tcp->m_nb_tile_parts = cur_totnum_tp;
1942 static void j2k_write_soc(opj_j2k_t *j2k) {
1943 opj_cio_t *cio = j2k->cio;
1944 cio_write(cio, J2K_MS_SOC, 2);
1947 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1952 /* update markers struct */
1953 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1954 #endif /* USE_JPWL */
1959 * Writes the SOC marker (Start Of Codestream)
1961 * @param p_stream the stream to write data to.
1962 * @param p_j2k J2K codec.
1963 * @param p_manager the user event manager.
1965 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1966 struct opj_stream_private *p_stream,
1967 struct opj_event_mgr * p_manager )
1969 /* 2 bytes will be written */
1970 OPJ_BYTE * l_start_stream = 00;
1973 assert(p_stream != 00);
1974 assert(p_j2k != 00);
1975 assert(p_manager != 00);
1977 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1979 /* write SOC identifier */
1980 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1982 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1988 /* update markers struct */
1990 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1992 assert( 0 && "TODO" );
1993 #endif /* USE_JPWL */
2002 * Reads a SOC marker (Start of Codestream)
2003 * @param p_header_data the data contained in the SOC box.
2004 * @param jp2 the jpeg2000 file codec.
2005 * @param p_header_size the size of the data contained in the SOC marker.
2006 * @param p_manager the user event manager.
2008 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2009 struct opj_stream_private *p_stream,
2010 struct opj_event_mgr * p_manager )
2012 OPJ_BYTE l_data [2];
2013 OPJ_UINT32 l_marker;
2016 assert(p_j2k != 00);
2017 assert(p_manager != 00);
2018 assert(p_stream != 00);
2020 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2024 opj_read_bytes(l_data,&l_marker,2);
2025 if (l_marker != J2K_MS_SOC) {
2029 /* Next marker should be a SIZ marker in the main header */
2030 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2032 /* FIXME move it in a index structure included in p_j2k*/
2033 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2035 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2037 /* Add the marker to the codestream index*/
2038 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2043 static void j2k_write_siz(opj_j2k_t *j2k) {
2047 opj_cio_t *cio = j2k->cio;
2048 opj_image_t *image = j2k->image;
2049 opj_cp_t *cp = j2k->cp;
2051 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2052 lenp = cio_tell(cio);
2054 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2055 cio_write(cio, image->x1, 4); /* Xsiz */
2056 cio_write(cio, image->y1, 4); /* Ysiz */
2057 cio_write(cio, image->x0, 4); /* X0siz */
2058 cio_write(cio, image->y0, 4); /* Y0siz */
2059 cio_write(cio, cp->tdx, 4); /* XTsiz */
2060 cio_write(cio, cp->tdy, 4); /* YTsiz */
2061 cio_write(cio, cp->tx0, 4); /* XT0siz */
2062 cio_write(cio, cp->ty0, 4); /* YT0siz */
2063 cio_write(cio, image->numcomps, 2); /* Csiz */
2064 for (i = 0; i < image->numcomps; i++) {
2065 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2066 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2067 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2069 len = cio_tell(cio) - lenp;
2070 cio_seek(cio, lenp);
2071 cio_write(cio, len, 2); /* Lsiz */
2072 cio_seek(cio, lenp + len);
2075 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2079 * Writes the SIZ marker (image and tile size)
2081 * @param p_stream the stream to write data to.
2082 * @param p_j2k J2K codec.
2083 * @param p_manager the user event manager.
2085 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2086 struct opj_stream_private *p_stream,
2087 struct opj_event_mgr * p_manager )
2090 OPJ_UINT32 l_size_len;
2091 OPJ_BYTE * l_current_ptr;
2092 opj_image_t * l_image = 00;
2093 opj_cp_v2_t *cp = 00;
2094 opj_image_comp_t * l_img_comp = 00;
2097 assert(p_stream != 00);
2098 assert(p_j2k != 00);
2099 assert(p_manager != 00);
2101 l_image = p_j2k->m_private_image;
2102 cp = &(p_j2k->m_cp);
2103 l_size_len = 40 + 3 * l_image->numcomps;
2104 l_img_comp = l_image->comps;
2106 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2108 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2109 = (OPJ_BYTE*)opj_realloc(
2110 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2112 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2116 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2119 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2121 /* write SOC identifier */
2122 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2125 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2128 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2131 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2134 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2137 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2140 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2143 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2146 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2149 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2152 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2155 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2158 for (i = 0; i < l_image->numcomps; ++i) {
2159 /* TODO here with MCT ? */
2160 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2163 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2166 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2172 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) {
2182 * Reads a SIZ marker (image and tile size)
2183 * @param p_header_data the data contained in the SIZ box.
2184 * @param jp2 the jpeg2000 file codec.
2185 * @param p_header_size the size of the data contained in the SIZ marker.
2186 * @param p_manager the user event manager.
2188 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2189 OPJ_BYTE * p_header_data,
2190 OPJ_UINT32 p_header_size,
2191 opj_event_mgr_t * p_manager
2194 OPJ_UINT32 l_size, i;
2195 OPJ_UINT32 l_nb_comp;
2196 OPJ_UINT32 l_nb_comp_remain;
2197 OPJ_UINT32 l_remaining_size;
2198 OPJ_UINT32 l_nb_tiles;
2200 opj_image_t *l_image = 00;
2201 opj_cp_v2_t *l_cp = 00;
2202 opj_image_comp_t * l_img_comp = 00;
2203 opj_tcp_v2_t * l_current_tile_param = 00;
2206 assert(p_j2k != 00);
2207 assert(p_manager != 00);
2208 assert(p_header_data != 00);
2210 l_image = p_j2k->m_private_image;
2211 l_cp = &(p_j2k->m_cp);
2213 /* minimum size == 39 - 3 (= minimum component parameter) */
2214 if (p_header_size < 36) {
2215 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2219 l_remaining_size = p_header_size - 36;
2220 l_nb_comp = l_remaining_size / 3;
2221 l_nb_comp_remain = l_remaining_size % 3;
2222 if (l_nb_comp_remain != 0){
2223 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2227 l_size = p_header_size + 2; /* Lsiz */
2229 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2231 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2232 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2234 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2236 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2238 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2240 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2242 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2244 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2246 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2248 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2251 l_image->numcomps = (OPJ_UINT16) l_tmp;
2253 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2257 if (l_image->numcomps != l_nb_comp) {
2258 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);
2263 if (l_cp->correct) {
2264 /* if JPWL is on, we check whether TX errors have damaged
2265 too much the SIZ parameters */
2266 if (!(l_image->x1 * l_image->y1)) {
2267 opj_event_msg_v2(p_manager, EVT_ERROR,
2268 "JPWL: bad image size (%d x %d)\n",
2269 l_image->x1, l_image->y1);
2270 if (!JPWL_ASSUME || JPWL_ASSUME) {
2271 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2276 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2277 if (l_image->numcomps != ((len - 38) / 3)) {
2278 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2279 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2280 l_image->numcomps, ((len - 38) / 3));
2282 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2285 */ /* we try to correct */
2286 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2287 if (l_image->numcomps < ((len - 38) / 3)) {
2288 len = 38 + 3 * l_image->numcomps;
2289 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2292 l_image->numcomps = ((len - 38) / 3);
2293 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2299 /* update components number in the jpwl_exp_comps filed */
2300 l_cp->exp_comps = l_image->numcomps;
2302 #endif /* USE_JPWL */
2304 /* Allocate the resulting image components */
2305 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2306 if (l_image->comps == 00){
2307 l_image->numcomps = 0;
2308 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2312 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2313 l_img_comp = l_image->comps;
2315 /* Read the component information */
2316 for (i = 0; i < l_image->numcomps; ++i){
2318 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2320 l_img_comp->prec = (tmp & 0x7f) + 1;
2321 l_img_comp->sgnd = tmp >> 7;
2322 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2324 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2325 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2327 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2330 if (l_cp->correct) {
2331 /* if JPWL is on, we check whether TX errors have damaged
2332 too much the SIZ parameters, again */
2333 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2334 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2335 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2336 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2338 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2341 /* we try to correct */
2342 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2343 if (!l_image->comps[i].dx) {
2344 l_image->comps[i].dx = 1;
2345 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2346 i, l_image->comps[i].dx);
2348 if (!l_image->comps[i].dy) {
2349 l_image->comps[i].dy = 1;
2350 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2351 i, l_image->comps[i].dy);
2355 #endif /* USE_JPWL */
2356 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2357 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2361 /* Compute the number of tiles */
2362 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2363 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2364 l_nb_tiles = l_cp->tw * l_cp->th;
2366 /* Define the tiles which will be decoded */
2367 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2368 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;
2369 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;
2370 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);
2371 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);
2374 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2375 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2376 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2377 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2381 if (l_cp->correct) {
2382 /* if JPWL is on, we check whether TX errors have damaged
2383 too much the SIZ parameters */
2384 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2385 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2386 "JPWL: bad number of tiles (%d x %d)\n",
2387 l_cp->tw, l_cp->th);
2389 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2392 /* we try to correct */
2393 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2396 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2399 if (l_cp->tw > l_cp->max_tiles) {
2401 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2402 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2403 l_cp->max_tiles, l_cp->tw);
2407 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2410 if (l_cp->th > l_cp->max_tiles) {
2412 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2413 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2414 l_cp->max_tiles, l_cp->th);
2418 #endif /* USE_JPWL */
2420 /* memory allocations */
2421 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2422 if (l_cp->tcps == 00) {
2423 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2426 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2429 if (l_cp->correct) {
2431 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2432 "JPWL: could not alloc tcps field of cp\n");
2433 if (!JPWL_ASSUME || JPWL_ASSUME) {
2434 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2439 #endif /* USE_JPWL */
2441 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2442 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2443 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2444 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2447 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2449 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2450 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2452 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2453 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2456 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));
2457 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2459 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2460 (opj_simple_mcc_decorrelation_data_t*)
2461 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2463 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2464 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2467 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));
2468 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2470 /* set up default dc level shift */
2471 for (i=0;i<l_image->numcomps;++i) {
2472 if (! l_image->comps[i].sgnd) {
2473 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2477 l_current_tile_param = l_cp->tcps;
2478 for (i = 0; i < l_nb_tiles; ++i) {
2479 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2480 if (l_current_tile_param->tccps == 00) {
2481 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2484 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2486 ++l_current_tile_param;
2489 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2490 opj_image_comp_header_update(l_image,l_cp);
2497 static void j2k_write_com(opj_j2k_t *j2k) {
2501 if(j2k->cp->comment) {
2502 opj_cio_t *cio = j2k->cio;
2503 char *comment = j2k->cp->comment;
2505 cio_write(cio, J2K_MS_COM, 2);
2506 lenp = cio_tell(cio);
2508 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2509 for (i = 0; i < strlen(comment); i++) {
2510 cio_write(cio, comment[i], 1);
2512 len = cio_tell(cio) - lenp;
2513 cio_seek(cio, lenp);
2514 cio_write(cio, len, 2);
2515 cio_seek(cio, lenp + len);
2519 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2525 * Writes the COM marker (comment)
2527 * @param p_stream the stream to write data to.
2528 * @param p_j2k J2K codec.
2529 * @param p_manager the user event manager.
2531 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2532 struct opj_stream_private *p_stream,
2533 struct opj_event_mgr * p_manager )
2535 OPJ_UINT32 l_comment_size;
2536 OPJ_UINT32 l_total_com_size;
2537 const OPJ_CHAR *l_comment;
2538 OPJ_BYTE * l_current_ptr = 00;
2541 assert(p_j2k != 00);
2542 assert(p_stream != 00);
2543 assert(p_manager != 00);
2545 l_comment = p_j2k->m_cp.comment;
2546 l_comment_size = strlen(l_comment);
2547 l_total_com_size = l_comment_size + 6;
2549 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2550 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2551 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2554 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2558 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2561 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2563 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2566 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2569 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2572 memcpy( l_current_ptr,l_comment,l_comment_size);
2574 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) {
2581 static void j2k_read_com(opj_j2k_t *j2k) {
2584 opj_cio_t *cio = j2k->cio;
2586 len = cio_read(cio, 2);
2587 cio_skip(cio, len - 2);
2590 * Reads a COM marker (comments)
2591 * @param p_header_data the data contained in the COM box.
2592 * @param jp2 the jpeg2000 file codec.
2593 * @param p_header_size the size of the data contained in the COM marker.
2594 * @param p_manager the user event manager.
2596 opj_bool j2k_read_com_v2 (
2597 opj_j2k_v2_t *p_j2k,
2598 OPJ_BYTE * p_header_data,
2599 OPJ_UINT32 p_header_size,
2600 struct opj_event_mgr * p_manager
2604 assert(p_j2k != 00);
2605 assert(p_manager != 00);
2606 assert(p_header_data != 00);
2607 (void)p_header_size;
2612 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2615 opj_cp_t *cp = j2k->cp;
2616 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2617 opj_tccp_t *tccp = &tcp->tccps[compno];
2618 opj_cio_t *cio = j2k->cio;
2620 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2621 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2622 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2623 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2624 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2626 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2627 for (i = 0; i < tccp->numresolutions; i++) {
2628 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2634 static void j2k_write_cod(opj_j2k_t *j2k) {
2635 opj_cp_t *cp = NULL;
2636 opj_tcp_t *tcp = NULL;
2639 opj_cio_t *cio = j2k->cio;
2641 cio_write(cio, J2K_MS_COD, 2); /* COD */
2643 lenp = cio_tell(cio);
2647 tcp = &cp->tcps[j2k->curtileno];
2649 cio_write(cio, tcp->csty, 1); /* Scod */
2650 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2651 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2652 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2654 j2k_write_cox(j2k, 0);
2655 len = cio_tell(cio) - lenp;
2656 cio_seek(cio, lenp);
2657 cio_write(cio, len, 2); /* Lcod */
2658 cio_seek(cio, lenp + len);
2661 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2666 * Writes the COD marker (Coding style default)
2668 * @param p_stream the stream to write data to.
2669 * @param p_j2k J2K codec.
2670 * @param p_manager the user event manager.
2672 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2673 struct opj_stream_private *p_stream,
2674 struct opj_event_mgr * p_manager )
2676 opj_cp_v2_t *l_cp = 00;
2677 opj_tcp_v2_t *l_tcp = 00;
2678 OPJ_UINT32 l_code_size,l_remaining_size;
2679 OPJ_BYTE * l_current_data = 00;
2682 assert(p_j2k != 00);
2683 assert(p_manager != 00);
2684 assert(p_stream != 00);
2686 l_cp = &(p_j2k->m_cp);
2687 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2688 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2689 l_remaining_size = l_code_size;
2691 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2692 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2693 = (OPJ_BYTE*)opj_realloc(
2694 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2697 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2701 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2704 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2706 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2707 l_current_data += 2;
2709 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2710 l_current_data += 2;
2712 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2715 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2718 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2721 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2724 l_remaining_size -= 9;
2726 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2727 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2731 if (l_remaining_size != 0) {
2732 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2736 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) {
2745 * Reads a COD marker (Coding Styke defaults)
2746 * @param p_header_data the data contained in the COD box.
2747 * @param p_j2k the jpeg2000 codec.
2748 * @param p_header_size the size of the data contained in the COD marker.
2749 * @param p_manager the user event manager.
2751 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2752 OPJ_BYTE * p_header_data,
2753 OPJ_UINT32 p_header_size,
2754 opj_event_mgr_t * p_manager
2760 opj_cp_v2_t *l_cp = 00;
2761 opj_tcp_v2_t *l_tcp = 00;
2762 opj_image_t *l_image = 00;
2765 assert(p_header_data != 00);
2766 assert(p_j2k != 00);
2767 assert(p_manager != 00);
2769 l_image = p_j2k->m_private_image;
2770 l_cp = &(p_j2k->m_cp);
2772 /* If we are in the first tile-part header of the current tile */
2773 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2774 &l_cp->tcps[p_j2k->m_current_tile_number] :
2775 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2777 /* Make sure room is sufficient */
2778 if (p_header_size < 5) {
2779 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2783 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2785 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2787 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2788 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2791 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2792 if (l_cp->m_specific_param.m_dec.m_layer) {
2793 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2796 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2799 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2803 for (i = 0; i < l_image->numcomps; ++i) {
2804 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2807 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2808 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2812 if (p_header_size != 0) {
2813 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2817 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2818 j2k_copy_tile_component_parameters(p_j2k);
2821 #ifdef WIP_REMOVE_MSD
2822 if (p_j2k->cstr_info) {
2823 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2824 p_j2k->cstr_info->prog = l_tcp->prg;
2825 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2826 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2827 for (i = 0; i < l_image->numcomps; ++i) {
2828 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2836 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2839 opj_cp_t *cp = j2k->cp;
2840 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2841 opj_image_t *image = j2k->image;
2842 opj_cio_t *cio = j2k->cio;
2844 cio_write(cio, J2K_MS_COC, 2); /* COC */
2845 lenp = cio_tell(cio);
2847 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2848 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2849 j2k_write_cox(j2k, compno);
2850 len = cio_tell(cio) - lenp;
2851 cio_seek(cio, lenp);
2852 cio_write(cio, len, 2); /* Lcoc */
2853 cio_seek(cio, lenp + len);
2857 * Writes the COC marker (Coding style component)
2859 * @param p_comp_no the index of the component to output.
2860 * @param p_stream the stream to write data to.
2861 * @param p_j2k J2K codec.
2862 * @param p_manager the user event manager.
2864 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2865 OPJ_UINT32 p_comp_no,
2866 struct opj_stream_private *p_stream,
2867 struct opj_event_mgr * p_manager )
2869 OPJ_UINT32 l_coc_size,l_remaining_size;
2870 OPJ_UINT32 l_comp_room;
2873 assert(p_j2k != 00);
2874 assert(p_manager != 00);
2875 assert(p_stream != 00);
2877 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2879 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2881 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2882 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2883 = (OPJ_BYTE*)opj_realloc(
2884 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2886 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2890 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2893 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);
2895 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) {
2903 * Writes the COC marker (Coding style component)
2905 * @param p_comp_no the index of the component to output.
2906 * @param p_stream the stream to write data to.
2907 * @param p_j2k J2K codec.
2908 * @param p_manager the user event manager.
2910 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2911 OPJ_UINT32 p_comp_no,
2913 OPJ_UINT32 * p_data_written,
2914 struct opj_event_mgr * p_manager )
2916 opj_cp_v2_t *l_cp = 00;
2917 opj_tcp_v2_t *l_tcp = 00;
2918 OPJ_UINT32 l_coc_size,l_remaining_size;
2919 OPJ_BYTE * l_current_data = 00;
2920 opj_image_t *l_image = 00;
2921 OPJ_UINT32 l_comp_room;
2924 assert(p_j2k != 00);
2925 assert(p_manager != 00);
2927 l_cp = &(p_j2k->m_cp);
2928 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2929 l_image = p_j2k->m_private_image;
2930 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2932 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2933 l_remaining_size = l_coc_size;
2935 l_current_data = p_data;
2937 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2938 l_current_data += 2;
2940 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2941 l_current_data += 2;
2943 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2944 l_current_data+=l_comp_room;
2946 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2949 l_remaining_size -= (5 + l_comp_room);
2950 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2951 * p_data_written = l_coc_size;
2955 * Gets the maximum size taken by a coc.
2957 * @param p_j2k the jpeg2000 codec to use.
2959 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2962 OPJ_UINT32 l_nb_comp;
2963 OPJ_UINT32 l_nb_tiles;
2964 OPJ_UINT32 l_max = 0;
2968 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2969 l_nb_comp = p_j2k->m_private_image->numcomps;
2971 for (i=0;i<l_nb_tiles;++i) {
2972 for (j=0;j<l_nb_comp;++j) {
2973 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2982 * Reads a COC marker (Coding Style Component)
2983 * @param p_header_data the data contained in the COC box.
2984 * @param p_j2k the jpeg2000 codec.
2985 * @param p_header_size the size of the data contained in the COC marker.
2986 * @param p_manager the user event manager.
2988 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2989 OPJ_BYTE * p_header_data,
2990 OPJ_UINT32 p_header_size,
2991 opj_event_mgr_t * p_manager
2994 opj_cp_v2_t *l_cp = NULL;
2995 opj_tcp_v2_t *l_tcp = NULL;
2996 opj_image_t *l_image = NULL;
2997 OPJ_UINT32 l_comp_room;
2998 OPJ_UINT32 l_comp_no;
3001 assert(p_header_data != 00);
3002 assert(p_j2k != 00);
3003 assert(p_manager != 00);
3005 l_cp = &(p_j2k->m_cp);
3006 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3007 &l_cp->tcps[p_j2k->m_current_tile_number] :
3008 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3009 l_image = p_j2k->m_private_image;
3011 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3013 /* make sure room is sufficient*/
3014 if (p_header_size < l_comp_room + 1) {
3015 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3018 p_header_size -= l_comp_room + 1;
3020 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3021 p_header_data += l_comp_room;
3022 if (l_comp_no >= l_image->numcomps) {
3023 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3027 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3030 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3031 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3035 if (p_header_size != 0) {
3036 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3042 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3043 int bandno, numbands;
3046 opj_cp_t *cp = j2k->cp;
3047 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3048 opj_tccp_t *tccp = &tcp->tccps[compno];
3049 opj_cio_t *cio = j2k->cio;
3051 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3052 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3054 for (bandno = 0; bandno < numbands; bandno++) {
3055 expn = tccp->stepsizes[bandno].expn;
3056 mant = tccp->stepsizes[bandno].mant;
3058 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3059 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3061 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3067 static void j2k_write_qcd(opj_j2k_t *j2k) {
3070 opj_cio_t *cio = j2k->cio;
3072 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3073 lenp = cio_tell(cio);
3075 j2k_write_qcx(j2k, 0);
3076 len = cio_tell(cio) - lenp;
3077 cio_seek(cio, lenp);
3078 cio_write(cio, len, 2); /* Lqcd */
3079 cio_seek(cio, lenp + len);
3082 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3086 * Writes the QCD marker (quantization default)
3088 * @param p_comp_number the index of the component to output.
3089 * @param p_stream the stream to write data to.
3090 * @param p_j2k J2K codec.
3091 * @param p_manager the user event manager.
3093 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3094 struct opj_stream_private *p_stream,
3095 struct opj_event_mgr * p_manager )
3097 opj_cp_v2_t *l_cp = 00;
3098 opj_tcp_v2_t *l_tcp = 00;
3099 OPJ_UINT32 l_qcd_size,l_remaining_size;
3100 OPJ_BYTE * l_current_data = 00;
3103 assert(p_j2k != 00);
3104 assert(p_manager != 00);
3105 assert(p_stream != 00);
3107 l_cp = &(p_j2k->m_cp);
3108 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3109 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3110 l_remaining_size = l_qcd_size;
3112 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3113 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3114 = (OPJ_BYTE*)opj_realloc(
3115 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3118 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3122 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3125 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3127 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3128 l_current_data += 2;
3130 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3131 l_current_data += 2;
3133 l_remaining_size -= 4;
3135 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3136 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3140 if (l_remaining_size != 0) {
3141 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3145 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) {
3153 * Reads a QCD marker (Quantization defaults)
3154 * @param p_header_data the data contained in the QCD box.
3155 * @param p_j2k the jpeg2000 codec.
3156 * @param p_header_size the size of the data contained in the QCD marker.
3157 * @param p_manager the user event manager.
3159 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3160 OPJ_BYTE * p_header_data,
3161 OPJ_UINT32 p_header_size,
3162 opj_event_mgr_t * p_manager
3166 assert(p_header_data != 00);
3167 assert(p_j2k != 00);
3168 assert(p_manager != 00);
3170 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3171 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3175 if (p_header_size != 0) {
3176 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3180 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3181 j2k_copy_tile_quantization_parameters(p_j2k);
3186 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3189 opj_cio_t *cio = j2k->cio;
3191 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3192 lenp = cio_tell(cio);
3194 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3195 j2k_write_qcx(j2k, compno);
3196 len = cio_tell(cio) - lenp;
3197 cio_seek(cio, lenp);
3198 cio_write(cio, len, 2); /* Lqcc */
3199 cio_seek(cio, lenp + len);
3203 * Writes the QCC marker (quantization component)
3205 * @param p_comp_no the index of the component to output.
3206 * @param p_stream the stream to write data to.
3207 * @param p_j2k J2K codec.
3208 * @param p_manager the user event manager.
3210 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3211 OPJ_UINT32 p_comp_no,
3212 struct opj_stream_private *p_stream,
3213 struct opj_event_mgr * p_manager )
3215 OPJ_UINT32 l_qcc_size,l_remaining_size;
3218 assert(p_j2k != 00);
3219 assert(p_manager != 00);
3220 assert(p_stream != 00);
3222 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3223 l_remaining_size = l_qcc_size;
3225 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3226 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3227 = (OPJ_BYTE*)opj_realloc(
3228 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3230 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3234 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3237 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);
3239 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) {
3247 * Writes the QCC marker (quantization component)
3249 * @param p_comp_no the index of the component to output.
3250 * @param p_stream the stream to write data to.
3251 * @param p_j2k J2K codec.
3252 * @param p_manager the user event manager.
3254 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3255 OPJ_UINT32 p_comp_no,
3257 OPJ_UINT32 * p_data_written,
3258 struct opj_event_mgr * p_manager )
3260 OPJ_UINT32 l_qcc_size,l_remaining_size;
3261 OPJ_BYTE * l_current_data = 00;
3264 assert(p_j2k != 00);
3265 assert(p_manager != 00);
3267 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3268 l_remaining_size = l_qcc_size;
3270 l_current_data = p_data;
3272 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3273 l_current_data += 2;
3275 if (p_j2k->m_private_image->numcomps <= 256) {
3278 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3279 l_current_data += 2;
3281 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3284 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3285 l_remaining_size -= 6;
3288 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3289 l_current_data += 2;
3291 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3294 l_remaining_size -= 6;
3297 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3299 *p_data_written = l_qcc_size;
3303 * Gets the maximum size taken by a qcc.
3305 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3307 return j2k_get_max_coc_size(p_j2k);
3311 * Reads a QCC marker (Quantization component)
3312 * @param p_header_data the data contained in the QCC box.
3313 * @param p_j2k the jpeg2000 codec.
3314 * @param p_header_size the size of the data contained in the QCC marker.
3315 * @param p_manager the user event manager.
3317 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3318 OPJ_BYTE * p_header_data,
3319 OPJ_UINT32 p_header_size,
3320 opj_event_mgr_t * p_manager
3323 OPJ_UINT32 l_num_comp,l_comp_no;
3326 assert(p_header_data != 00);
3327 assert(p_j2k != 00);
3328 assert(p_manager != 00);
3330 l_num_comp = p_j2k->m_private_image->numcomps;
3332 if (l_num_comp <= 256) {
3333 if (p_header_size < 1) {
3334 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3337 opj_read_bytes(p_header_data,&l_comp_no,1);
3342 if (p_header_size < 2) {
3343 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3346 opj_read_bytes(p_header_data,&l_comp_no,2);
3352 if (p_j2k->m_cp.correct) {
3354 static OPJ_UINT32 backup_compno = 0;
3356 /* compno is negative or larger than the number of components!!! */
3357 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3358 opj_event_msg_v2(p_manager, EVT_ERROR,
3359 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3360 l_comp_no, l_num_comp);
3362 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3365 /* we try to correct */
3366 l_comp_no = backup_compno % l_num_comp;
3367 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3368 "- setting component number to %d\n",
3372 /* keep your private count of tiles */
3375 #endif /* USE_JPWL */
3377 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3378 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3382 if (p_header_size != 0) {
3383 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3391 static void j2k_write_poc(opj_j2k_t *j2k) {
3392 int len, numpchgs, i;
3394 int numcomps = j2k->image->numcomps;
3396 opj_cp_t *cp = j2k->cp;
3397 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3398 opj_tccp_t *tccp = &tcp->tccps[0];
3399 opj_cio_t *cio = j2k->cio;
3401 numpchgs = 1 + tcp->numpocs;
3402 cio_write(cio, J2K_MS_POC, 2); /* POC */
3403 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3404 cio_write(cio, len, 2); /* Lpoc */
3405 for (i = 0; i < numpchgs; i++) {
3406 opj_poc_t *poc = &tcp->pocs[i];
3407 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3408 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3409 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3410 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3411 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3412 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3413 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3414 poc->compno1 = int_min(poc->compno1, numcomps);
3415 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3420 * Writes the POC marker (Progression Order Change)
3422 * @param p_stream the stream to write data to.
3423 * @param p_j2k J2K codec.
3424 * @param p_manager the user event manager.
3426 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3427 struct opj_stream_private *p_stream,
3428 struct opj_event_mgr * p_manager )
3430 OPJ_UINT32 l_nb_comp;
3431 OPJ_UINT32 l_nb_poc;
3432 OPJ_UINT32 l_poc_size;
3433 OPJ_UINT32 l_written_size = 0;
3434 opj_tcp_v2_t *l_tcp = 00;
3435 opj_tccp_t *l_tccp = 00;
3436 OPJ_UINT32 l_poc_room;
3439 assert(p_j2k != 00);
3440 assert(p_manager != 00);
3441 assert(p_stream != 00);
3443 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3444 l_tccp = &l_tcp->tccps[0];
3445 l_nb_comp = p_j2k->m_private_image->numcomps;
3446 l_nb_poc = 1 + l_tcp->numpocs;
3448 if (l_nb_comp <= 256) {
3454 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3456 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3457 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3458 = (OPJ_BYTE*)opj_realloc(
3459 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3462 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3466 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3469 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3471 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) {
3480 * Writes the POC marker (Progression Order Change)
3482 * @param p_stream the stream to write data to.
3483 * @param p_j2k J2K codec.
3484 * @param p_manager the user event manager.
3486 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3488 OPJ_UINT32 * p_data_written,
3489 struct opj_event_mgr * p_manager )
3492 OPJ_BYTE * l_current_data = 00;
3493 OPJ_UINT32 l_nb_comp;
3494 OPJ_UINT32 l_nb_poc;
3495 OPJ_UINT32 l_poc_size;
3496 opj_image_t *l_image = 00;
3497 opj_tcp_v2_t *l_tcp = 00;
3498 opj_tccp_t *l_tccp = 00;
3499 opj_poc_t *l_current_poc = 00;
3500 OPJ_UINT32 l_poc_room;
3503 assert(p_j2k != 00);
3504 assert(p_manager != 00);
3506 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3507 l_tccp = &l_tcp->tccps[0];
3508 l_image = p_j2k->m_private_image;
3509 l_nb_comp = l_image->numcomps;
3510 l_nb_poc = 1 + l_tcp->numpocs;
3512 if (l_nb_comp <= 256) {
3519 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3521 l_current_data = p_data;
3523 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3524 l_current_data += 2;
3526 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3527 l_current_data += 2;
3529 l_current_poc = l_tcp->pocs;
3530 for (i = 0; i < l_nb_poc; ++i) {
3531 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3534 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3535 l_current_data+=l_poc_room;
3537 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3540 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3543 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3544 l_current_data+=l_poc_room;
3546 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3549 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3550 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3551 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3552 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3557 *p_data_written = l_poc_size;
3561 * Gets the maximum size taken by the writing of a POC.
3563 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3565 opj_tcp_v2_t * l_tcp = 00;
3566 OPJ_UINT32 l_nb_tiles = 0;
3567 OPJ_UINT32 l_max_poc = 0;
3570 l_tcp = p_j2k->m_cp.tcps;
3571 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3573 for (i=0;i<l_nb_tiles;++i) {
3574 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3580 return 4 + 9 * l_max_poc;
3584 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3586 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3589 OPJ_UINT32 l_nb_tiles;
3590 OPJ_UINT32 l_max = 0;
3591 opj_tcp_v2_t * l_tcp = 00;
3593 l_tcp = p_j2k->m_cp.tcps;
3594 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3596 for (i=0;i<l_nb_tiles;++i) {
3597 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3607 * Gets the maximum size taken by the headers of the SOT.
3609 * @param p_j2k the jpeg2000 codec to use.
3611 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3613 OPJ_UINT32 l_nb_bytes = 0;
3614 OPJ_UINT32 l_nb_comps;
3615 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3617 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3618 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3620 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3621 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3622 l_nb_bytes += l_nb_comps * l_coc_bytes;
3624 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3625 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3628 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3630 /*** DEVELOPER CORNER, Add room for your headers ***/
3637 * Reads a POC marker (Progression Order Change)
3639 * @param p_header_data the data contained in the POC box.
3640 * @param p_j2k the jpeg2000 codec.
3641 * @param p_header_size the size of the data contained in the POC marker.
3642 * @param p_manager the user event manager.
3644 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3645 OPJ_BYTE * p_header_data,
3646 OPJ_UINT32 p_header_size,
3647 opj_event_mgr_t * p_manager
3650 OPJ_UINT32 i, l_nb_comp, l_tmp;
3651 opj_image_t * l_image = 00;
3652 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3653 OPJ_UINT32 l_chunk_size, l_comp_room;
3655 opj_cp_v2_t *l_cp = 00;
3656 opj_tcp_v2_t *l_tcp = 00;
3657 opj_poc_t *l_current_poc = 00;
3660 assert(p_header_data != 00);
3661 assert(p_j2k != 00);
3662 assert(p_manager != 00);
3664 l_image = p_j2k->m_private_image;
3665 l_nb_comp = l_image->numcomps;
3666 if (l_nb_comp <= 256) {
3672 l_chunk_size = 5 + 2 * l_comp_room;
3673 l_current_poc_nb = p_header_size / l_chunk_size;
3674 l_current_poc_remaining = p_header_size % l_chunk_size;
3676 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3677 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3681 l_cp = &(p_j2k->m_cp);
3682 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3683 &l_cp->tcps[p_j2k->m_current_tile_number] :
3684 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3685 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3686 l_current_poc_nb += l_old_poc_nb;
3688 assert(l_current_poc_nb < 32);
3690 /* now poc is in use.*/
3693 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3694 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3695 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3697 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3698 p_header_data+=l_comp_room;
3699 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3701 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3703 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3704 p_header_data+=l_comp_room;
3705 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3707 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3708 /* make sure comp is in acceptable bounds */
3709 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3713 l_tcp->numpocs = l_current_poc_nb - 1;
3718 * Reads a CRG marker (Component registration)
3720 * @param p_header_data the data contained in the TLM box.
3721 * @param p_j2k the jpeg2000 codec.
3722 * @param p_header_size the size of the data contained in the TLM marker.
3723 * @param p_manager the user event manager.
3725 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3726 OPJ_BYTE * p_header_data,
3727 OPJ_UINT32 p_header_size,
3728 opj_event_mgr_t * p_manager
3731 OPJ_UINT32 l_nb_comp;
3733 assert(p_header_data != 00);
3734 assert(p_j2k != 00);
3735 assert(p_manager != 00);
3737 l_nb_comp = p_j2k->m_private_image->numcomps;
3739 if (p_header_size != l_nb_comp *4) {
3740 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3743 /* Do not care of this at the moment since only local variables are set here */
3746 (i = 0; i < l_nb_comp; ++i)
3748 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3750 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3758 * Reads a TLM marker (Tile Length Marker)
3760 * @param p_header_data the data contained in the TLM box.
3761 * @param p_j2k the jpeg2000 codec.
3762 * @param p_header_size the size of the data contained in the TLM marker.
3763 * @param p_manager the user event manager.
3765 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3766 OPJ_BYTE * p_header_data,
3767 OPJ_UINT32 p_header_size,
3768 opj_event_mgr_t * p_manager
3771 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3773 assert(p_header_data != 00);
3774 assert(p_j2k != 00);
3775 assert(p_manager != 00);
3777 if (p_header_size < 2) {
3778 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3783 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3785 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3788 l_ST = ((l_Stlm >> 4) & 0x3);
3789 l_SP = (l_Stlm >> 6) & 0x1;
3791 l_Ptlm_size = (l_SP + 1) * 2;
3792 l_quotient = l_Ptlm_size + l_ST;
3794 l_tot_num_tp = p_header_size / l_quotient;
3795 l_tot_num_tp_remaining = p_header_size % l_quotient;
3797 if (l_tot_num_tp_remaining != 0) {
3798 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3801 /* FIXME Do not care of this at the moment since only local variables are set here */
3804 (i = 0; i < l_tot_num_tp; ++i)
3806 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3807 p_header_data += l_ST;
3808 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3809 p_header_data += l_Ptlm_size;
3816 * Reads a PLM marker (Packet length, main header marker)
3818 * @param p_header_data the data contained in the TLM box.
3819 * @param p_j2k the jpeg2000 codec.
3820 * @param p_header_size the size of the data contained in the TLM marker.
3821 * @param p_manager the user event manager.
3823 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3824 OPJ_BYTE * p_header_data,
3825 OPJ_UINT32 p_header_size,
3826 opj_event_mgr_t * p_manager
3830 assert(p_header_data != 00);
3831 assert(p_j2k != 00);
3832 assert(p_manager != 00);
3834 if (p_header_size < 1) {
3835 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3838 /* Do not care of this at the moment since only local variables are set here */
3840 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3847 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3849 p_header_size -= (1+l_Nplm);
3853 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3857 (i = 0; i < l_Nplm; ++i)
3859 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3861 // take only the last seven bytes
3862 l_packet_len |= (l_tmp & 0x7f);
3870 // store packet length and proceed to next packet
3877 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3886 * Reads a PLT marker (Packet length, tile-part header)
3888 * @param p_header_data the data contained in the PLT box.
3889 * @param p_j2k the jpeg2000 codec.
3890 * @param p_header_size the size of the data contained in the PLT marker.
3891 * @param p_manager the user event manager.
3893 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3894 OPJ_BYTE * p_header_data,
3895 OPJ_UINT32 p_header_size,
3896 opj_event_mgr_t * p_manager
3899 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3902 assert(p_header_data != 00);
3903 assert(p_j2k != 00);
3904 assert(p_manager != 00);
3906 if (p_header_size < 1) {
3907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3911 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3915 for (i = 0; i < p_header_size; ++i) {
3916 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3918 /* take only the last seven bytes */
3919 l_packet_len |= (l_tmp & 0x7f);
3924 /* store packet length and proceed to next packet */
3929 if (l_packet_len != 0) {
3930 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3939 * Reads a PPM marker (Packed packet headers, main header)
3941 * @param p_header_data the data contained in the POC box.
3942 * @param p_j2k the jpeg2000 codec.
3943 * @param p_header_size the size of the data contained in the POC marker.
3944 * @param p_manager the user event manager.
3947 opj_bool j2k_read_ppm_v2 (
3948 opj_j2k_v2_t *p_j2k,
3949 OPJ_BYTE * p_header_data,
3950 OPJ_UINT32 p_header_size,
3951 struct opj_event_mgr * p_manager
3955 opj_cp_v2_t *l_cp = 00;
3956 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3959 assert(p_header_data != 00);
3960 assert(p_j2k != 00);
3961 assert(p_manager != 00);
3963 if (p_header_size < 1) {
3964 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3968 l_cp = &(p_j2k->m_cp);
3971 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3975 /* First PPM marker */
3977 if (p_header_size < 4) {
3978 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3982 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3986 /* First PPM marker: Initialization */
3987 l_cp->ppm_len = l_N_ppm;
3988 l_cp->ppm_data_size = 0;
3990 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3991 if (l_cp->ppm_buffer == 00) {
3992 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3995 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3997 l_cp->ppm_data = l_cp->ppm_buffer;
4001 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4002 if (p_header_size >= 4) {
4004 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4007 l_cp->ppm_len += l_N_ppm ;
4009 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4010 if (l_cp->ppm_buffer == 00) {
4011 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4014 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4016 l_cp->ppm_data = l_cp->ppm_buffer;
4023 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4025 if (l_remaining_data <= p_header_size) {
4026 /* we must store less information than available in the packet */
4027 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4028 l_cp->ppm_data_size = l_cp->ppm_len;
4029 p_header_size -= l_remaining_data;
4030 p_header_data += l_remaining_data;
4033 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4034 l_cp->ppm_data_size += p_header_size;
4035 p_header_data += p_header_size;
4048 * Reads a PPM marker (Packed packet headers, main header)
4050 * @param p_header_data the data contained in the POC box.
4051 * @param p_j2k the jpeg2000 codec.
4052 * @param p_header_size the size of the data contained in the POC marker.
4053 * @param p_manager the user event manager.
4055 opj_bool j2k_read_ppm_v3 (
4056 opj_j2k_v2_t *p_j2k,
4057 OPJ_BYTE * p_header_data,
4058 OPJ_UINT32 p_header_size,
4059 struct opj_event_mgr * p_manager
4062 opj_cp_v2_t *l_cp = 00;
4063 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4066 assert(p_header_data != 00);
4067 assert(p_j2k != 00);
4068 assert(p_manager != 00);
4070 /* Minimum size of PPM marker is equal to the size of Zppm element */
4071 if (p_header_size < 1) {
4072 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4076 l_cp = &(p_j2k->m_cp);
4079 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4083 /* First PPM marker */
4085 /* We need now at least the Nppm^0 element */
4086 if (p_header_size < 4) {
4087 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4091 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4095 /* First PPM marker: Initialization */
4096 l_cp->ppm_len = l_N_ppm;
4097 l_cp->ppm_data_read = 0;
4099 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4100 if (l_cp->ppm_data == 00) {
4101 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4104 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4106 l_cp->ppm_data_current = l_cp->ppm_data;
4108 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4111 if (p_header_size < 4) {
4112 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4116 /* Uncompleted Ippm series in the previous PPM marker?*/
4117 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4118 /* Get the place where add the remaining Ippm series*/
4119 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4120 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4123 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4127 /* Increase the size of ppm_data to add the new Ippm series*/
4128 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4130 /* Keep the position of the place where concatenate the new series*/
4131 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4132 l_cp->ppm_len += l_N_ppm;
4137 l_remaining_data = p_header_size;
4139 while (l_remaining_data >= l_N_ppm) {
4140 /* read a complete Ippm series*/
4141 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4142 p_header_size -= l_N_ppm;
4143 p_header_data += l_N_ppm;
4145 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4149 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4154 l_remaining_data = p_header_size;
4158 l_remaining_data = p_header_size;
4160 /* Next Ippm series is a complete series ?*/
4161 if (l_remaining_data > l_N_ppm) {
4162 /* Increase the size of ppm_data to add the new Ippm series*/
4163 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4165 /* Keep the position of the place where concatenate the new series */
4166 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4167 l_cp->ppm_len += l_N_ppm;
4172 /* Need to read an incomplete Ippm series*/
4173 if (l_remaining_data) {
4174 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4176 /* Keep the position of the place where concatenate the new series*/
4177 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4178 l_cp->ppm_len += l_N_ppm;
4180 /* Read incomplete Ippm series*/
4181 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4182 p_header_size -= l_remaining_data;
4183 p_header_data += l_remaining_data;
4185 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4190 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4191 if (p_header_size >= 4) {
4193 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4196 l_cp->ppm_len += l_N_ppm ;
4198 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4199 if (l_cp->ppm_buffer == 00) {
4200 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4203 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4205 l_cp->ppm_data = l_cp->ppm_buffer;
4212 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4214 if (l_remaining_data <= p_header_size) {
4215 /* we must store less information than available in the packet */
4216 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4217 l_cp->ppm_data_size = l_cp->ppm_len;
4218 p_header_size -= l_remaining_data;
4219 p_header_data += l_remaining_data;
4222 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4223 l_cp->ppm_data_size += p_header_size;
4224 p_header_data += p_header_size;
4234 * Reads a PPT marker (Packed packet headers, tile-part header)
4236 * @param p_header_data the data contained in the PPT box.
4237 * @param p_j2k the jpeg2000 codec.
4238 * @param p_header_size the size of the data contained in the PPT marker.
4239 * @param p_manager the user event manager.
4241 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4242 OPJ_BYTE * p_header_data,
4243 OPJ_UINT32 p_header_size,
4244 opj_event_mgr_t * p_manager
4247 opj_cp_v2_t *l_cp = 00;
4248 opj_tcp_v2_t *l_tcp = 00;
4252 assert(p_header_data != 00);
4253 assert(p_j2k != 00);
4254 assert(p_manager != 00);
4256 /* We need to have the Z_ppt element at minimum */
4257 if (p_header_size < 1) {
4258 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4262 l_cp = &(p_j2k->m_cp);
4264 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");
4268 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4271 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4275 /* Allocate buffer to read the packet header */
4277 /* First PPT marker */
4278 l_tcp->ppt_data_size = 0;
4279 l_tcp->ppt_len = p_header_size;
4281 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4282 if (l_tcp->ppt_buffer == 00) {
4283 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4286 l_tcp->ppt_data = l_tcp->ppt_buffer;
4288 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4291 l_tcp->ppt_len += p_header_size;
4293 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4294 if (l_tcp->ppt_buffer == 00) {
4295 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4298 l_tcp->ppt_data = l_tcp->ppt_buffer;
4300 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4303 /* Read packet header from buffer */
4304 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4306 l_tcp->ppt_data_size += p_header_size;
4312 * Writes the TLM marker (Tile Length Marker)
4314 * @param p_stream the stream to write data to.
4315 * @param p_j2k J2K codec.
4316 * @param p_manager the user event manager.
4318 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4319 struct opj_stream_private *p_stream,
4320 struct opj_event_mgr * p_manager )
4322 OPJ_BYTE * l_current_data = 00;
4323 OPJ_UINT32 l_tlm_size;
4326 assert(p_j2k != 00);
4327 assert(p_manager != 00);
4328 assert(p_stream != 00);
4330 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4332 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4333 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4334 = (OPJ_BYTE*)opj_realloc(
4335 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4338 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4342 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4345 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4347 /* change the way data is written to avoid seeking if possible */
4349 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4351 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4352 l_current_data += 2;
4354 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4355 l_current_data += 2;
4357 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4360 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4363 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4364 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) {
4371 static void j2k_write_tlm(opj_j2k_t *j2k){
4373 opj_cio_t *cio = j2k->cio;
4374 j2k->tlm_start = cio_tell(cio);
4375 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4376 lenp = 4 + (5*j2k->totnum_tp);
4377 cio_write(cio,lenp,2); /* Ltlm */
4378 cio_write(cio, 0,1); /* Ztlm=0*/
4379 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4380 cio_skip(cio,5*j2k->totnum_tp);
4383 static void j2k_write_sot(opj_j2k_t *j2k) {
4386 opj_cio_t *cio = j2k->cio;
4388 j2k->sot_start = cio_tell(cio);
4389 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4390 lenp = cio_tell(cio);
4391 cio_skip(cio, 2); /* Lsot (further) */
4392 cio_write(cio, j2k->curtileno, 2); /* Isot */
4393 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4394 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4395 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4396 len = cio_tell(cio) - lenp;
4397 cio_seek(cio, lenp);
4398 cio_write(cio, len, 2); /* Lsot */
4399 cio_seek(cio, lenp + len);
4403 /* update markers struct */
4404 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4405 #endif /* USE_JPWL */
4408 if( j2k->cstr_info && j2k->cur_tp_num==0){
4409 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4414 * Writes the SOT marker (Start of tile-part)
4416 * @param p_stream the stream to write data to.
4417 * @param p_j2k J2K codec.
4418 * @param p_manager the user event manager.
4420 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4422 OPJ_UINT32 * p_data_written,
4423 const struct opj_stream_private *p_stream,
4424 struct opj_event_mgr * p_manager )
4427 assert(p_j2k != 00);
4428 assert(p_manager != 00);
4429 assert(p_stream != 00);
4431 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4434 opj_write_bytes(p_data,10,2); /* Lsot */
4437 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4443 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4446 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4451 /* update markers struct */
4453 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4455 assert( 0 && "TODO" );
4456 #endif /* USE_JPWL */
4458 * p_data_written = 12;
4467 * Reads a PPT marker (Packed packet headers, tile-part header)
4469 * @param p_header_data the data contained in the PPT box.
4470 * @param p_j2k the jpeg2000 codec.
4471 * @param p_header_size the size of the data contained in the PPT marker.
4472 * @param p_manager the user event manager.
4474 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4475 OPJ_BYTE * p_header_data,
4476 OPJ_UINT32 p_header_size,
4477 opj_event_mgr_t * p_manager )
4479 opj_cp_v2_t *l_cp = 00;
4480 opj_tcp_v2_t *l_tcp = 00;
4481 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4482 OPJ_UINT32 l_current_part;
4483 OPJ_UINT32 l_tile_x,l_tile_y;
4486 assert(p_header_data != 00);
4487 assert(p_j2k != 00);
4488 assert(p_manager != 00);
4490 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4491 if (p_header_size != 8) {
4492 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4496 l_cp = &(p_j2k->m_cp);
4497 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4500 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4501 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4502 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4505 if (l_cp->correct) {
4507 int tileno = p_j2k->m_current_tile_number;
4508 static int backup_tileno = 0;
4510 /* tileno is negative or larger than the number of tiles!!! */
4511 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4512 opj_event_msg_v2(p_manager, EVT_ERROR,
4513 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4514 tileno, (l_cp->tw * l_cp->th));
4516 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4519 /* we try to correct */
4520 tileno = backup_tileno;
4521 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4522 "- setting tile number to %d\n",
4526 /* keep your private count of tiles */
4529 #endif /* USE_JPWL */
4531 /* look for the tile in the list of already processed tile (in parts). */
4532 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4533 /* since the time taken by this function can only grow at the time */
4535 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4538 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4539 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4541 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4547 if (l_cp->correct) {
4549 /* totlen is negative or larger than the bytes left!!! */
4550 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))) { */
4551 opj_event_msg_v2(p_manager, EVT_ERROR,
4552 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4553 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4555 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4558 /* we try to correct */
4560 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4561 "- setting Psot to %d => assuming it is the last tile\n",
4565 #endif /* USE_JPWL */
4567 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4569 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4570 "we assuming it is the last tile-part of the codestream.\n");
4571 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4574 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4577 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4580 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4581 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4582 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4583 if (l_tcp->m_nb_tile_parts) {
4584 if (l_current_part >= l_tcp->m_nb_tile_parts){
4585 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4586 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4587 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4591 l_tcp->m_nb_tile_parts = l_num_parts;
4594 /* If know the number of tile part header we will check if we didn't read the last*/
4595 if (l_tcp->m_nb_tile_parts) {
4596 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4597 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4601 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4602 /* Keep the size of data to skip after this marker */
4603 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4606 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4607 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4610 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4612 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4613 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4614 p_j2k->m_specific_param.m_decoder.m_skip_data =
4615 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4616 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4617 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4618 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4621 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4622 p_j2k->m_specific_param.m_decoder.m_skip_data =
4623 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4627 if (p_j2k->cstr_index)
4629 assert(p_j2k->cstr_index->tile_index != 00);
4630 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4631 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4633 if (l_num_parts != 0){
4634 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4635 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4638 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4639 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4640 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4642 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4643 (opj_tp_index_t*)opj_realloc(
4644 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4645 l_num_parts* sizeof(opj_tp_index_t));
4648 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4650 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4651 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4652 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4653 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4654 sizeof(opj_tp_index_t));
4657 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4658 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4659 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4660 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4661 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4662 * sizeof(opj_tp_index_t));
4671 /* 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 */
4672 /* if (p_j2k->cstr_info) {
4675 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4678 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4679 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4680 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4681 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4684 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4687 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4691 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4694 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4695 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4696 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4701 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4704 opj_tcp_t *tcp = NULL;
4705 opj_codestream_info_t *cstr_info = NULL;
4707 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4708 opj_cp_t *cp = j2k->cp;
4709 opj_cio_t *cio = j2k->cio;
4711 tcd->tp_num = j2k->tp_num ;
4712 tcd->cur_tp_num = j2k->cur_tp_num;
4714 cio_write(cio, J2K_MS_SOD, 2);
4716 if( j2k->cstr_info && j2k->cur_tp_num==0){
4717 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4720 if (j2k->curtileno == 0) {
4721 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4725 cstr_info = j2k->cstr_info;
4727 if (!j2k->cur_tp_num ) {
4728 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4729 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4732 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4733 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4737 /* update markers struct */
4738 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4739 #endif /* USE_JPWL */
4744 tcp = &cp->tcps[j2k->curtileno];
4745 for (layno = 0; layno < tcp->numlayers; layno++) {
4746 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4747 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4748 } else if (tcp->rates[layno]) {
4749 tcp->rates[layno]=1;
4752 if(j2k->cur_tp_num == 0){
4753 tcd->tcd_image->tiles->packno = 0;
4755 cstr_info->packno = 0;
4758 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4760 /* Writing Psot in SOT marker */
4761 totlen = cio_tell(cio) + l - j2k->sot_start;
4762 cio_seek(cio, j2k->sot_start + 6);
4763 cio_write(cio, totlen, 4);
4764 cio_seek(cio, j2k->sot_start + totlen);
4765 /* Writing Ttlm and Ptlm in TLM marker */
4767 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4768 cio_write(cio, j2k->curtileno, 1);
4769 cio_write(cio, totlen, 4);
4771 cio_seek(cio, j2k->sot_start + totlen);
4775 * Writes the SOD marker (Start of data)
4777 * @param p_stream the stream to write data to.
4778 * @param p_j2k J2K codec.
4779 * @param p_manager the user event manager.
4781 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4782 struct opj_tcd_v2 * p_tile_coder,
4784 OPJ_UINT32 * p_data_written,
4785 OPJ_UINT32 p_total_data_size,
4786 const struct opj_stream_private *p_stream,
4787 struct opj_event_mgr * p_manager )
4789 opj_tcp_v2_t *l_tcp = 00;
4790 opj_codestream_info_t *l_cstr_info = 00;
4791 opj_cp_v2_t *l_cp = 00;
4793 OPJ_UINT32 l_size_tile;
4794 OPJ_UINT32 l_remaining_data;
4797 assert(p_j2k != 00);
4798 assert(p_manager != 00);
4799 assert(p_stream != 00);
4801 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4804 /* make room for the EOF marker */
4805 l_remaining_data = p_total_data_size - 4;
4807 l_cp = &(p_j2k->m_cp);
4808 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4811 /* update tile coder */
4812 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4813 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4815 l_size_tile = l_cp->th * l_cp->tw;
4818 /* TODO mergeV2: check this part which use cstr_info */
4819 /*l_cstr_info = p_j2k->cstr_info;
4821 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4822 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4823 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4829 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4831 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4836 /* update markers struct */
4837 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4839 assert( 0 && "TODO" );
4840 #endif /* USE_JPWL */
4845 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4846 p_tile_coder->tcd_image->tiles->packno = 0;
4848 l_cstr_info->packno = 0;
4852 *p_data_written = 0;
4854 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)) {
4855 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4859 *p_data_written += 2;
4868 * Reads a SOD marker (Start Of Data)
4870 * @param p_header_data the data contained in the SOD box.
4871 * @param p_j2k the jpeg2000 codec.
4872 * @param p_header_size the size of the data contained in the SOD marker.
4873 * @param p_manager the user event manager.
4875 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4876 opj_stream_private_t *p_stream,
4877 opj_event_mgr_t * p_manager
4880 OPJ_UINT32 l_current_read_size;
4881 opj_codestream_index_t * l_cstr_index = 00;
4882 OPJ_BYTE ** l_current_data = 00;
4883 opj_tcp_v2_t * l_tcp = 00;
4884 OPJ_UINT32 * l_tile_len = 00;
4887 assert(p_j2k != 00);
4888 assert(p_manager != 00);
4889 assert(p_stream != 00);
4891 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4893 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4894 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4895 // but we are in the last tile part,
4896 // so its result will fit on OPJ_UINT32 unless we find
4897 // a file with a single tile part of more than 4 GB...*/
4898 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4901 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4903 l_current_data = &(l_tcp->m_data);
4904 l_tile_len = &l_tcp->m_data_size;
4906 if (! *l_current_data) {
4907 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4910 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4913 if (*l_current_data == 00) {
4914 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4920 l_cstr_index = p_j2k->cstr_index;
4922 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4924 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4925 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4927 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4928 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4930 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4934 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4936 /*l_cstr_index->packno = 0;*/
4939 l_current_read_size = opj_stream_read_data( p_stream,
4940 *l_current_data + *l_tile_len,
4941 p_j2k->m_specific_param.m_decoder.m_sot_length,
4944 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4945 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4948 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4951 *l_tile_len += l_current_read_size;
4957 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4958 opj_cp_t *cp = j2k->cp;
4959 opj_tcp_t *tcp = &cp->tcps[tileno];
4960 opj_cio_t *cio = j2k->cio;
4961 int numcomps = j2k->image->numcomps;
4963 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4964 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4965 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4966 cio_write(cio, 0, 1); /* Srgn */
4967 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4971 * Writes the RGN marker (Region Of Interest)
4973 * @param p_tile_no the tile to output
4974 * @param p_comp_no the component to output
4975 * @param p_stream the stream to write data to.
4976 * @param p_j2k J2K codec.
4977 * @param p_manager the user event manager.
4979 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4980 OPJ_UINT32 p_tile_no,
4981 OPJ_UINT32 p_comp_no,
4982 struct opj_stream_private *p_stream,
4983 struct opj_event_mgr * p_manager )
4985 OPJ_BYTE * l_current_data = 00;
4986 OPJ_UINT32 l_nb_comp;
4987 OPJ_UINT32 l_rgn_size;
4988 opj_image_t *l_image = 00;
4989 opj_cp_v2_t *l_cp = 00;
4990 opj_tcp_v2_t *l_tcp = 00;
4991 opj_tccp_t *l_tccp = 00;
4992 OPJ_UINT32 l_comp_room;
4995 assert(p_j2k != 00);
4996 assert(p_manager != 00);
4997 assert(p_stream != 00);
4999 l_cp = &(p_j2k->m_cp);
5000 l_tcp = &l_cp->tcps[p_tile_no];
5001 l_tccp = &l_tcp->tccps[p_comp_no];
5003 l_nb_comp = l_image->numcomps;
5005 if (l_nb_comp <= 256) {
5012 l_rgn_size = 6 + l_comp_room;
5014 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5016 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5017 l_current_data += 2;
5019 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5020 l_current_data += 2;
5022 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5023 l_current_data+=l_comp_room;
5025 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5028 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5031 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) {
5039 static void j2k_write_eoc(opj_j2k_t *j2k) {
5040 opj_cio_t *cio = j2k->cio;
5041 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5042 cio_write(cio, J2K_MS_EOC, 2);
5046 /* update markers struct */
5047 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5048 #endif /* USE_JPWL */
5053 * Writes the EOC marker (End of Codestream)
5055 * @param p_stream the stream to write data to.
5056 * @param p_j2k J2K codec.
5057 * @param p_manager the user event manager.
5059 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5060 struct opj_stream_private *p_stream,
5061 struct opj_event_mgr * p_manager )
5064 assert(p_j2k != 00);
5065 assert(p_manager != 00);
5066 assert(p_stream != 00);
5068 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5073 /* update markers struct */
5075 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5077 #endif /* USE_JPWL */
5079 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5083 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5092 * Reads a RGN marker (Region Of Interest)
5094 * @param p_header_data the data contained in the POC box.
5095 * @param p_j2k the jpeg2000 codec.
5096 * @param p_header_size the size of the data contained in the POC marker.
5097 * @param p_manager the user event manager.
5099 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5100 OPJ_BYTE * p_header_data,
5101 OPJ_UINT32 p_header_size,
5102 opj_event_mgr_t * p_manager
5105 OPJ_UINT32 l_nb_comp;
5106 opj_image_t * l_image = 00;
5108 opj_cp_v2_t *l_cp = 00;
5109 opj_tcp_v2_t *l_tcp = 00;
5110 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5113 assert(p_header_data != 00);
5114 assert(p_j2k != 00);
5115 assert(p_manager != 00);
5117 l_image = p_j2k->m_private_image;
5118 l_nb_comp = l_image->numcomps;
5120 if (l_nb_comp <= 256) {
5125 if (p_header_size != 2 + l_comp_room) {
5126 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5130 l_cp = &(p_j2k->m_cp);
5131 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5132 &l_cp->tcps[p_j2k->m_current_tile_number] :
5133 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5135 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5136 p_header_data+=l_comp_room;
5137 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5141 if (l_cp->correct) {
5142 /* totlen is negative or larger than the bytes left!!! */
5143 if (l_comp_room >= l_nb_comp) {
5144 opj_event_msg_v2(p_manager, EVT_ERROR,
5145 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5146 l_comp_room, l_nb_comp);
5147 if (!JPWL_ASSUME || JPWL_ASSUME) {
5148 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5153 #endif /* USE_JPWL */
5155 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5162 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5164 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5167 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5174 * Updates the rates of the tcp.
5176 * @param p_stream the stream to write data to.
5177 * @param p_j2k J2K codec.
5178 * @param p_manager the user event manager.
5180 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5181 struct opj_stream_private *p_stream,
5182 struct opj_event_mgr * p_manager )
5184 opj_cp_v2_t * l_cp = 00;
5185 opj_image_t * l_image = 00;
5186 opj_tcp_v2_t * l_tcp = 00;
5187 opj_image_comp_t * l_img_comp = 00;
5190 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5191 OPJ_FLOAT32 * l_rates = 0;
5192 OPJ_FLOAT32 l_sot_remove;
5193 OPJ_UINT32 l_bits_empty, l_size_pixel;
5194 OPJ_UINT32 l_tile_size = 0;
5195 OPJ_UINT32 l_last_res;
5196 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5199 assert(p_j2k != 00);
5200 assert(p_manager != 00);
5201 assert(p_stream != 00);
5204 l_cp = &(p_j2k->m_cp);
5205 l_image = p_j2k->m_private_image;
5208 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5209 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5210 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5212 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5213 l_tp_stride_func = get_tp_stride;
5216 l_tp_stride_func = get_default_stride;
5219 for (i=0;i<l_cp->th;++i) {
5220 for (j=0;j<l_cp->tw;++j) {
5221 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5223 /* 4 borders of the tile rescale on the image if necessary */
5224 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5225 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5226 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5227 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5229 l_rates = l_tcp->rates;
5231 /* Modification of the RATE >> */
5233 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5235 ((*l_rates) * l_bits_empty)
5243 for (k = 1; k < l_tcp->numlayers; ++k) {
5245 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5247 ((*l_rates) * l_bits_empty)
5263 for (i=0;i<l_cp->th;++i) {
5264 for (j=0;j<l_cp->tw;++j) {
5265 l_rates = l_tcp->rates;
5268 *l_rates -= l_sot_remove;
5270 if (*l_rates < 30) {
5277 l_last_res = l_tcp->numlayers - 1;
5279 for (k = 1; k < l_last_res; ++k) {
5282 *l_rates -= l_sot_remove;
5284 if (*l_rates < *(l_rates - 1) + 10) {
5285 *l_rates = (*(l_rates - 1)) + 20;
5293 *l_rates -= (l_sot_remove + 2.f);
5295 if (*l_rates < *(l_rates - 1) + 10) {
5296 *l_rates = (*(l_rates - 1)) + 20;
5304 l_img_comp = l_image->comps;
5307 for (i=0;i<l_image->numcomps;++i) {
5308 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5310 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5318 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5320 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5322 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5323 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5324 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5325 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5329 if (l_cp->m_specific_param.m_enc.m_cinema) {
5330 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5331 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5332 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5336 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5337 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5343 static void j2k_read_eoc(opj_j2k_t *j2k) {
5345 opj_bool success = OPJ_FALSE;
5347 /* if packets should be decoded */
5348 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5349 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5350 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5351 for (i = 0; i < j2k->cp->tileno_size; i++) {
5352 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5353 if (j2k->cp->tileno[i] != -1)
5355 tileno = j2k->cp->tileno[i];
5356 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5357 opj_free(j2k->tile_data[tileno]);
5358 j2k->tile_data[tileno] = NULL;
5359 tcd_free_decode_tile(tcd, i);
5362 success = OPJ_FALSE;
5363 if (success == OPJ_FALSE) {
5364 j2k->state |= J2K_STATE_ERR;
5368 tcd_free_decode(tcd);
5371 /* if packets should not be decoded */
5373 for (i = 0; i < j2k->cp->tileno_size; i++) {
5374 tileno = j2k->cp->tileno[i];
5375 opj_free(j2k->tile_data[tileno]);
5376 j2k->tile_data[tileno] = NULL;
5379 if (j2k->state & J2K_STATE_ERR)
5380 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5382 j2k->state = J2K_STATE_MT;
5386 * Reads a EOC marker (End Of Codestream)
5388 * @param p_header_data the data contained in the SOD box.
5389 * @param p_j2k the jpeg2000 codec.
5390 * @param p_header_size the size of the data contained in the SOD marker.
5391 * @param p_manager the user event manager.
5394 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5395 struct opj_stream_private *p_stream,
5396 struct opj_event_mgr * p_manager )
5399 opj_tcd_v2_t * l_tcd = 00;
5400 OPJ_UINT32 l_nb_tiles;
5401 opj_tcp_v2_t * l_tcp = 00;
5405 assert(p_j2k != 00);
5406 assert(p_manager != 00);
5407 assert(p_stream != 00);
5409 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5410 l_tcp = p_j2k->m_cp.tcps;
5412 l_tcd = tcd_create_v2(OPJ_TRUE);
5414 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5418 for (i = 0; i < l_nb_tiles; ++i) {
5419 if (l_tcp->m_data) {
5420 if (! tcd_init_decode_tile(l_tcd, i)) {
5421 tcd_destroy_v2(l_tcd);
5422 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5426 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5430 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5435 j2k_tcp_destroy(l_tcp);
5439 tcd_destroy_v2(l_tcd);
5445 * Gets the offset of the header.
5447 * @param p_stream the stream to write data to.
5448 * @param p_j2k J2K codec.
5449 * @param p_manager the user event manager.
5451 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5452 struct opj_stream_private *p_stream,
5453 struct opj_event_mgr * p_manager )
5456 assert(p_j2k != 00);
5457 assert(p_manager != 00);
5458 assert(p_stream != 00);
5460 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5466 * Writes the MCT marker (Multiple Component Transform)
5468 * @param p_stream the stream to write data to.
5469 * @param p_j2k J2K codec.
5470 * @param p_manager the user event manager.
5472 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5473 struct opj_stream_private *p_stream,
5474 struct opj_event_mgr * p_manager )
5477 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5478 opj_mct_data_t * l_mct_record;
5479 opj_tcp_v2_t * l_tcp;
5482 assert(p_j2k != 00);
5483 assert(p_stream != 00);
5484 assert(p_manager != 00);
5486 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5490 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5491 l_mct_record = l_tcp->m_mct_records;
5493 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5495 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5502 l_mcc_record = l_tcp->m_mcc_records;
5504 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5506 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5513 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5521 * Writes the image components.
5523 * @param p_stream the stream to write data to.
5524 * @param p_j2k J2K codec.
5525 * @param p_manager the user event manager.
5527 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5528 struct opj_stream_private *p_stream,
5529 struct opj_event_mgr * p_manager )
5534 assert(p_j2k != 00);
5535 assert(p_manager != 00);
5536 assert(p_stream != 00);
5538 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5540 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5544 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5553 * Writes regions of interests.
5555 * @param p_stream the stream to write data to.
5556 * @param p_j2k J2K codec.
5557 * @param p_manager the user event manager.
5559 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5560 struct opj_stream_private *p_stream,
5561 struct opj_event_mgr * p_manager )
5564 const opj_tccp_t *l_tccp = 00;
5567 assert(p_j2k != 00);
5568 assert(p_manager != 00);
5569 assert(p_stream != 00);
5571 l_tccp = p_j2k->m_cp.tcps->tccps;
5573 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5574 if (l_tccp->roishift) {
5576 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5590 * @param p_stream the stream to write data to.
5591 * @param p_j2k J2K codec.
5592 * @param p_manager the user event manager.
5594 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5595 struct opj_stream_private *p_stream,
5596 struct opj_event_mgr * p_manager )
5598 opj_codestream_index_t * l_cstr_index = 00;
5601 assert(p_j2k != 00);
5602 assert(p_manager != 00);
5603 assert(p_stream != 00);
5605 l_cstr_index = p_j2k->cstr_index;
5607 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5609 /* The following adjustment is done to adjust the codestream size */
5610 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5611 /* the first bunch of bytes is not in the codestream */
5612 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5617 /* preparation of JPWL marker segments */
5621 /* encode according to JPWL */
5622 jpwl_encode(p_j2k, p_stream, image);
5626 assert( 0 && "TODO" );
5627 #endif /* USE_JPWL */
5632 typedef struct opj_dec_mstabent {
5635 /** value of the state when the marker can appear */
5637 /** action linked to the marker */
5638 void (*handler) (opj_j2k_t *j2k);
5639 } opj_dec_mstabent_t;
5641 opj_dec_mstabent_t j2k_dec_mstab[] = {
5642 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5643 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5644 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5645 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5646 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5647 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5648 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5649 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5650 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5651 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5652 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5653 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5654 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5655 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5656 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5657 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5659 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5660 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
5663 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5664 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5665 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5666 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5667 #endif /* USE_JPWL */
5669 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5670 {J2K_MS_INSEC, 0, j2k_read_insec},
5671 #endif /* USE_JPSEC */
5673 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5676 static void j2k_read_unk(opj_j2k_t *j2k) {
5677 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5680 if (j2k->cp->correct) {
5682 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5683 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5684 id = cio_read(j2k->cio, 2);
5685 opj_event_msg(j2k->cinfo, EVT_ERROR,
5686 "JPWL: really don't know this marker %x\n",
5689 opj_event_msg(j2k->cinfo, EVT_ERROR,
5690 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5693 /* OK, activate this at your own risk!!! */
5694 /* we look for the marker at the minimum hamming distance from this */
5695 while (j2k_dec_mstab[m].id) {
5697 /* 1's where they differ */
5698 tmp_id = j2k_dec_mstab[m].id ^ id;
5700 /* compute the hamming distance between our id and the current */
5702 for (i = 0; i < 16; i++) {
5703 if ((tmp_id >> i) & 0x0001) {
5708 /* if current distance is smaller, set the minimum */
5709 if (cur_dist < min_dist) {
5710 min_dist = cur_dist;
5711 min_id = j2k_dec_mstab[m].id;
5714 /* jump to the next marker */
5718 /* do we substitute the marker? */
5719 if (min_dist < JPWL_MAXIMUM_HAMMING) {
5720 opj_event_msg(j2k->cinfo, EVT_ERROR,
5721 "- marker %x is at distance %d from the read %x\n",
5722 min_id, min_dist, id);
5723 opj_event_msg(j2k->cinfo, EVT_ERROR,
5724 "- trying to substitute in place and crossing fingers!\n");
5725 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5726 cio_write(j2k->cio, min_id, 2);
5729 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5734 #endif /* USE_JPWL */
5739 * Reads an unknown marker
5741 * @param p_stream the stream object to read from.
5742 * @param p_j2k the jpeg2000 codec.
5743 * @param p_manager the user event manager.
5745 * @return true if the marker could be deduced.
5747 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
5748 struct opj_stream_private *p_stream,
5749 OPJ_UINT32 *output_marker,
5750 struct opj_event_mgr * p_manager
5753 OPJ_UINT32 l_unknown_marker;
5754 const opj_dec_memory_marker_handler_t * l_marker_handler;
5755 OPJ_UINT32 l_size_unk = 2;
5758 assert(p_j2k != 00);
5759 assert(p_manager != 00);
5760 assert(p_stream != 00);
5762 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5765 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5766 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5767 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5771 /* read 2 bytes as the new marker ID*/
5772 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5774 if (!(l_unknown_marker < 0xff00)) {
5776 /* Get the marker handler from the marker ID*/
5777 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5779 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5780 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5784 if (l_marker_handler->id != J2K_MS_UNK) {
5785 /* Add the marker to the codestream index*/
5786 if (l_marker_handler->id != J2K_MS_SOT)
5787 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5788 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5790 break; /* next marker is known and well located */
5798 *output_marker = l_marker_handler->id ;
5804 Read the lookup table containing all the marker, status and action
5805 @param id Marker value
5807 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5808 opj_dec_mstabent_t *e;
5809 for (e = j2k_dec_mstab; e->id != 0; e++) {
5818 * Writes the MCT marker (Multiple Component Transform)
5820 * @param p_stream the stream to write data to.
5821 * @param p_j2k J2K codec.
5822 * @param p_manager the user event manager.
5824 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5825 opj_mct_data_t * p_mct_record,
5826 struct opj_stream_private *p_stream,
5827 struct opj_event_mgr * p_manager )
5829 OPJ_UINT32 l_mct_size;
5830 OPJ_BYTE * l_current_data = 00;
5834 assert(p_j2k != 00);
5835 assert(p_manager != 00);
5836 assert(p_stream != 00);
5838 l_mct_size = 10 + p_mct_record->m_data_size;
5840 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5841 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5842 = (OPJ_BYTE*)opj_realloc(
5843 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5846 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5850 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5853 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5855 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5856 l_current_data += 2;
5858 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5859 l_current_data += 2;
5861 opj_write_bytes(l_current_data,0,2); /* Zmct */
5862 l_current_data += 2;
5864 /* only one marker atm */
5865 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5867 opj_write_bytes(l_current_data,l_tmp,2);
5868 l_current_data += 2;
5870 opj_write_bytes(l_current_data,0,2); /* Ymct */
5873 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5875 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) {
5883 * Reads a MCT marker (Multiple Component Transform)
5885 * @param p_header_data the data contained in the MCT box.
5886 * @param p_j2k the jpeg2000 codec.
5887 * @param p_header_size the size of the data contained in the MCT marker.
5888 * @param p_manager the user event manager.
5890 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5891 OPJ_BYTE * p_header_data,
5892 OPJ_UINT32 p_header_size,
5893 struct opj_event_mgr * p_manager )
5896 opj_tcp_v2_t *l_tcp = 00;
5899 opj_mct_data_t * l_mct_data;
5902 assert(p_header_data != 00);
5903 assert(p_j2k != 00);
5905 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5906 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5907 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5909 if (p_header_size < 2) {
5910 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5915 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5918 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5922 if(p_header_size <= 6) {
5923 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5927 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5928 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5931 l_indix = l_tmp & 0xff;
5932 l_mct_data = l_tcp->m_mct_records;
5934 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5935 if (l_mct_data->m_index == l_indix) {
5942 if (i == l_tcp->m_nb_mct_records) {
5943 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5944 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5946 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));
5947 if(! l_tcp->m_mct_records) {
5948 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5952 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5953 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5956 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5959 if (l_mct_data->m_data) {
5960 opj_free(l_mct_data->m_data);
5961 l_mct_data->m_data = 00;
5964 l_mct_data->m_index = l_indix;
5965 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5966 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5968 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5971 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5977 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5978 if (! l_mct_data->m_data) {
5979 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5982 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5984 l_mct_data->m_data_size = p_header_size;
5985 ++l_tcp->m_nb_mct_records;
5991 * Writes the MCC marker (Multiple Component Collection)
5993 * @param p_stream the stream to write data to.
5994 * @param p_j2k J2K codec.
5995 * @param p_manager the user event manager.
5997 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5998 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5999 struct opj_stream_private *p_stream,
6000 struct opj_event_mgr * p_manager )
6003 OPJ_UINT32 l_mcc_size;
6004 OPJ_BYTE * l_current_data = 00;
6005 OPJ_UINT32 l_nb_bytes_for_comp;
6010 assert(p_j2k != 00);
6011 assert(p_manager != 00);
6012 assert(p_stream != 00);
6014 if (p_mcc_record->m_nb_comps > 255 ) {
6015 l_nb_bytes_for_comp = 2;
6019 l_nb_bytes_for_comp = 1;
6023 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6024 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6026 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6027 = (OPJ_BYTE*)opj_realloc(
6028 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6030 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6034 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6037 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6039 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6040 l_current_data += 2;
6042 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6043 l_current_data += 2;
6046 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6047 l_current_data += 2;
6049 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6052 /* only one marker atm */
6053 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6056 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6059 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6062 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 */
6065 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6066 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6067 l_current_data+=l_nb_bytes_for_comp;
6070 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 */
6073 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6075 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6076 l_current_data+=l_nb_bytes_for_comp;
6079 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6081 if (p_mcc_record->m_decorrelation_array) {
6082 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6085 if (p_mcc_record->m_offset_array) {
6086 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6089 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6092 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) {
6100 * Reads a MCC marker (Multiple Component Collection)
6102 * @param p_header_data the data contained in the MCC box.
6103 * @param p_j2k the jpeg2000 codec.
6104 * @param p_header_size the size of the data contained in the MCC marker.
6105 * @param p_manager the user event manager.
6107 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6108 OPJ_BYTE * p_header_data,
6109 OPJ_UINT32 p_header_size,
6110 struct opj_event_mgr * p_manager )
6115 opj_tcp_v2_t * l_tcp;
6116 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6117 opj_mct_data_t * l_mct_data;
6118 OPJ_UINT32 l_nb_collections;
6119 OPJ_UINT32 l_nb_comps;
6120 OPJ_UINT32 l_nb_bytes_by_comp;
6124 assert(p_header_data != 00);
6125 assert(p_j2k != 00);
6126 assert(p_manager != 00);
6128 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6129 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6130 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6132 if (p_header_size < 2) {
6133 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6138 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6141 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6145 if (p_header_size < 7) {
6146 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6150 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6153 l_mcc_record = l_tcp->m_mcc_records;
6155 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6156 if (l_mcc_record->m_index == l_indix) {
6163 if (i == l_tcp->m_nb_mcc_records) {
6164 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6165 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6167 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6168 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6169 if (! l_tcp->m_mcc_records) {
6170 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6173 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6174 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));
6176 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6178 l_mcc_record->m_index = l_indix;
6180 /* only one marker atm */
6181 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6184 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6188 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6191 if (l_nb_collections > 1) {
6192 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6198 for (i=0;i<l_nb_collections;++i) {
6199 if (p_header_size < 3) {
6200 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6204 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6208 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6212 opj_read_bytes(p_header_data,&l_nb_comps,2);
6217 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6218 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6220 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6221 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6225 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6227 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6228 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6229 p_header_data+=l_nb_bytes_by_comp;
6232 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6237 opj_read_bytes(p_header_data,&l_nb_comps,2);
6240 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6241 l_nb_comps &= 0x7fff;
6243 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6244 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6248 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6249 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6253 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6255 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6256 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6257 p_header_data+=l_nb_bytes_by_comp;
6260 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6265 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6268 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6269 l_mcc_record->m_decorrelation_array = 00;
6270 l_mcc_record->m_offset_array = 00;
6272 l_indix = l_tmp & 0xff;
6274 l_mct_data = l_tcp->m_mct_records;
6275 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6276 if (l_mct_data->m_index == l_indix) {
6277 l_mcc_record->m_decorrelation_array = l_mct_data;
6283 if (l_mcc_record->m_decorrelation_array == 00) {
6284 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6289 l_indix = (l_tmp >> 8) & 0xff;
6291 l_mct_data = l_tcp->m_mct_records;
6292 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6293 if (l_mct_data->m_index == l_indix) {
6294 l_mcc_record->m_offset_array = l_mct_data;
6300 if (l_mcc_record->m_offset_array == 00) {
6301 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6307 if (p_header_size != 0) {
6308 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6312 ++l_tcp->m_nb_mcc_records;
6319 * Writes the MCO marker (Multiple component transformation ordering)
6321 * @param p_stream the stream to write data to.
6322 * @param p_j2k J2K codec.
6323 * @param p_manager the user event manager.
6325 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6326 struct opj_stream_private *p_stream,
6327 struct opj_event_mgr * p_manager
6330 OPJ_BYTE * l_current_data = 00;
6331 OPJ_UINT32 l_mco_size;
6332 opj_tcp_v2_t * l_tcp = 00;
6333 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6337 assert(p_j2k != 00);
6338 assert(p_manager != 00);
6339 assert(p_stream != 00);
6341 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6342 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6344 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6345 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6347 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6348 = (OPJ_BYTE*)opj_realloc(
6349 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6351 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6356 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6359 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6360 l_current_data += 2;
6362 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6363 l_current_data += 2;
6365 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6368 l_mcc_record = l_tcp->m_mcc_records;
6369 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6370 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6376 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) {
6384 * Reads a MCO marker (Multiple Component Transform Ordering)
6386 * @param p_header_data the data contained in the MCO box.
6387 * @param p_j2k the jpeg2000 codec.
6388 * @param p_header_size the size of the data contained in the MCO marker.
6389 * @param p_manager the user event manager.
6391 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6392 OPJ_BYTE * p_header_data,
6393 OPJ_UINT32 p_header_size,
6394 struct opj_event_mgr * p_manager )
6396 OPJ_UINT32 l_tmp, i;
6397 OPJ_UINT32 l_nb_stages;
6398 opj_tcp_v2_t * l_tcp;
6399 opj_tccp_t * l_tccp;
6400 opj_image_t * l_image;
6401 opj_image_comp_t * l_img_comp;
6404 assert(p_header_data != 00);
6405 assert(p_j2k != 00);
6406 assert(p_manager != 00);
6408 l_image = p_j2k->m_private_image;
6409 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6410 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6411 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6413 if (p_header_size < 1) {
6414 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6418 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6421 if (l_nb_stages > 1) {
6422 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6426 if (p_header_size != l_nb_stages + 1) {
6427 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6431 l_tccp = l_tcp->tccps;
6432 l_img_comp = l_image->comps;
6434 for (i=0;i<l_image->numcomps;++i) {
6435 l_tccp->m_dc_level_shift = 0;
6439 if (l_tcp->m_mct_decoding_matrix) {
6440 opj_free(l_tcp->m_mct_decoding_matrix);
6441 l_tcp->m_mct_decoding_matrix = 00;
6444 for (i=0;i<l_nb_stages;++i) {
6445 opj_read_bytes(p_header_data,&l_tmp,1);
6448 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6456 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6459 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6460 opj_mct_data_t * l_deco_array, * l_offset_array;
6461 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6462 OPJ_UINT32 l_nb_elem;
6463 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6464 opj_tccp_t * l_tccp;
6467 assert(p_tcp != 00);
6469 l_mcc_record = p_tcp->m_mcc_records;
6471 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6472 if (l_mcc_record->m_index == p_index) {
6477 if (i==p_tcp->m_nb_mcc_records) {
6478 /** element discarded **/
6482 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6483 /** do not support number of comps != image */
6487 l_deco_array = l_mcc_record->m_decorrelation_array;
6490 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6491 if (l_deco_array->m_data_size != l_data_size) {
6495 l_nb_elem = p_image->numcomps * p_image->numcomps;
6496 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6497 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6499 if (! p_tcp->m_mct_decoding_matrix ) {
6503 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);
6506 l_offset_array = l_mcc_record->m_offset_array;
6508 if (l_offset_array) {
6509 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6510 if (l_offset_array->m_data_size != l_data_size) {
6514 l_nb_elem = p_image->numcomps;
6515 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6516 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6518 if (! l_offset_data ) {
6522 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6524 l_tccp = p_tcp->tccps;
6525 l_current_offset_data = l_offset_data;
6527 for (i=0;i<p_image->numcomps;++i) {
6528 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6532 opj_free(l_offset_data);
6539 * Writes the CBD marker (Component bit depth definition)
6541 * @param p_stream the stream to write data to.
6542 * @param p_j2k J2K codec.
6543 * @param p_manager the user event manager.
6545 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6546 struct opj_stream_private *p_stream,
6547 struct opj_event_mgr * p_manager )
6550 OPJ_UINT32 l_cbd_size;
6551 OPJ_BYTE * l_current_data = 00;
6552 opj_image_t *l_image = 00;
6553 opj_image_comp_t * l_comp = 00;
6556 assert(p_j2k != 00);
6557 assert(p_manager != 00);
6558 assert(p_stream != 00);
6560 l_image = p_j2k->m_private_image;
6561 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6563 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6564 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6565 = (OPJ_BYTE*)opj_realloc(
6566 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6569 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6573 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6576 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6578 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6579 l_current_data += 2;
6581 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6582 l_current_data += 2;
6584 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6587 l_comp = l_image->comps;
6589 for (i=0;i<l_image->numcomps;++i) {
6590 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6596 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) {
6604 * Reads a CBD marker (Component bit depth definition)
6605 * @param p_header_data the data contained in the CBD box.
6606 * @param p_j2k the jpeg2000 codec.
6607 * @param p_header_size the size of the data contained in the CBD marker.
6608 * @param p_manager the user event manager.
6610 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6611 OPJ_BYTE * p_header_data,
6612 OPJ_UINT32 p_header_size,
6613 struct opj_event_mgr * p_manager)
6615 OPJ_UINT32 l_nb_comp,l_num_comp;
6616 OPJ_UINT32 l_comp_def;
6618 opj_image_comp_t * l_comp = 00;
6621 assert(p_header_data != 00);
6622 assert(p_j2k != 00);
6623 assert(p_manager != 00);
6625 l_num_comp = p_j2k->m_private_image->numcomps;
6627 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6628 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6632 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6635 if (l_nb_comp != l_num_comp) {
6636 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6640 l_comp = p_j2k->m_private_image->comps;
6641 for (i=0;i<l_num_comp;++i) {
6642 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6644 l_comp->sgnd = (l_comp_def>>7) & 1;
6645 l_comp->prec = (l_comp_def&0x7f) + 1;
6653 /* ----------------------------------------------------------------------- */
6654 /* J2K / JPT decoder interface */
6655 /* ----------------------------------------------------------------------- */
6659 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6662 if(j2k->tile_len != NULL) {
6663 opj_free(j2k->tile_len);
6665 if(j2k->tile_data != NULL) {
6666 opj_free(j2k->tile_data);
6668 if(j2k->default_tcp != NULL) {
6669 opj_tcp_t *default_tcp = j2k->default_tcp;
6670 if(default_tcp->ppt_data_first != NULL) {
6671 opj_free(default_tcp->ppt_data_first);
6673 if(j2k->default_tcp->tccps != NULL) {
6674 opj_free(j2k->default_tcp->tccps);
6676 opj_free(j2k->default_tcp);
6678 if(j2k->cp != NULL) {
6679 opj_cp_t *cp = j2k->cp;
6680 if(cp->tcps != NULL) {
6681 for(i = 0; i < cp->tw * cp->th; i++) {
6682 if(cp->tcps[i].ppt_data_first != NULL) {
6683 opj_free(cp->tcps[i].ppt_data_first);
6685 if(cp->tcps[i].tccps != NULL) {
6686 opj_free(cp->tcps[i].tccps);
6691 if(cp->ppm_data_first != NULL) {
6692 opj_free(cp->ppm_data_first);
6694 if(cp->tileno != NULL) {
6695 opj_free(cp->tileno);
6697 if(cp->comment != NULL) {
6698 opj_free(cp->comment);
6707 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6709 if(j2k && parameters) {
6710 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6711 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6714 j2k->m_cp.correct = parameters->jpwl_correct;
6715 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6716 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6717 #endif /* USE_JPWL */
6724 * Read a JPT-stream and decode file
6727 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6728 opj_image_t *image = NULL;
6729 opj_jpt_msg_header_t header;
6731 opj_common_ptr cinfo = j2k->cinfo;
6733 OPJ_ARG_NOT_USED(cstr_info);
6737 /* create an empty image */
6738 image = opj_image_create0();
6741 j2k->state = J2K_STATE_MHSOC;
6743 /* Initialize the header */
6744 jpt_init_msg_header(&header);
6745 /* Read the first header of the message */
6746 jpt_read_msg_header(cinfo, cio, &header);
6748 position = cio_tell(cio);
6749 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6750 opj_image_destroy(image);
6751 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6756 opj_dec_mstabent_t *e = NULL;
6759 if (!cio_numbytesleft(cio)) {
6763 /* data-bin read -> need to read a new header */
6764 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6765 jpt_read_msg_header(cinfo, cio, &header);
6766 position = cio_tell(cio);
6767 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6768 opj_image_destroy(image);
6769 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6774 id = cio_read(cio, 2);
6775 if (id >> 8 != 0xff) {
6776 opj_image_destroy(image);
6777 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6780 e = j2k_dec_mstab_lookup(id);
6781 if (!(j2k->state & e->states)) {
6782 opj_image_destroy(image);
6783 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6789 if (j2k->state == J2K_STATE_MT) {
6792 if (j2k->state == J2K_STATE_NEOC) {
6796 if (j2k->state == J2K_STATE_NEOC) {
6800 if (j2k->state != J2K_STATE_MT) {
6801 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6807 /* ----------------------------------------------------------------------- */
6808 /* J2K encoder interface */
6809 /* ----------------------------------------------------------------------- */
6811 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6812 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6819 opj_j2k_v2_t* j2k_create_compress_v2(void)
6821 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6826 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6828 l_j2k->m_is_decoder = 0;
6829 l_j2k->m_cp.m_is_decoder = 0;
6831 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6832 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6837 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6839 /* validation list creation*/
6840 l_j2k->m_validation_list = opj_procedure_list_create();
6841 if (! l_j2k->m_validation_list) {
6846 /* execution list creation*/
6847 l_j2k->m_procedure_list = opj_procedure_list_create();
6848 if (! l_j2k->m_procedure_list) {
6856 void j2k_destroy_compress(opj_j2k_t *j2k) {
6860 if(j2k->cp != NULL) {
6861 opj_cp_t *cp = j2k->cp;
6864 opj_free(cp->comment);
6867 opj_free(cp->matrice);
6869 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6870 opj_free(cp->tcps[tileno].tccps);
6879 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6881 int tileno, numpocs_tile;
6882 opj_cp_t *cp = NULL;
6884 if(!j2k || !parameters || ! image) {
6888 /* create and initialize the coding parameters structure */
6889 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6891 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6894 /* set default values for cp */
6899 copy user encoding parameters
6901 cp->cinema = parameters->cp_cinema;
6902 cp->max_comp_size = parameters->max_comp_size;
6903 cp->rsiz = parameters->cp_rsiz;
6904 cp->disto_alloc = parameters->cp_disto_alloc;
6905 cp->fixed_alloc = parameters->cp_fixed_alloc;
6906 cp->fixed_quality = parameters->cp_fixed_quality;
6908 /* mod fixed_quality */
6909 if(parameters->cp_matrice) {
6910 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6911 cp->matrice = (int *) opj_malloc(array_size);
6912 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6916 cp->tdx = parameters->cp_tdx;
6917 cp->tdy = parameters->cp_tdy;
6920 cp->tx0 = parameters->cp_tx0;
6921 cp->ty0 = parameters->cp_ty0;
6923 /* comment string */
6924 if(parameters->cp_comment) {
6925 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6927 strcpy(cp->comment, parameters->cp_comment);
6932 calculate other encoding parameters
6935 if (parameters->tile_size_on) {
6936 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6937 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6939 cp->tdx = image->x1 - cp->tx0;
6940 cp->tdy = image->y1 - cp->ty0;
6943 if(parameters->tp_on){
6944 cp->tp_flag = parameters->tp_flag;
6949 for(i=0;i<image->numcomps ;i++){
6950 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6956 calculate JPWL encoding parameters
6959 if (parameters->jpwl_epc_on) {
6963 cp->epc_on = OPJ_TRUE;
6964 cp->info_on = OPJ_FALSE; /* no informative technique */
6967 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6968 cp->epb_on = OPJ_TRUE;
6970 cp->hprot_MH = parameters->jpwl_hprot_MH;
6971 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6972 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6973 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6975 /* if tile specs are not specified, copy MH specs */
6976 if (cp->hprot_TPH[0] == -1) {
6977 cp->hprot_TPH_tileno[0] = 0;
6978 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6980 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6981 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6982 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6983 cp->pprot[i] = parameters->jpwl_pprot[i];
6987 /* set ESD writing */
6988 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6989 cp->esd_on = OPJ_TRUE;
6991 cp->sens_size = parameters->jpwl_sens_size;
6992 cp->sens_addr = parameters->jpwl_sens_addr;
6993 cp->sens_range = parameters->jpwl_sens_range;
6995 cp->sens_MH = parameters->jpwl_sens_MH;
6996 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6997 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6998 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7002 /* always set RED writing to false: we are at the encoder */
7003 cp->red_on = OPJ_FALSE;
7006 cp->epc_on = OPJ_FALSE;
7008 #endif /* USE_JPWL */
7011 /* initialize the mutiple tiles */
7012 /* ---------------------------- */
7013 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7015 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7016 opj_tcp_t *tcp = &cp->tcps[tileno];
7017 tcp->numlayers = parameters->tcp_numlayers;
7018 assert ( tcp->numlayers >= 0 );
7019 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7021 if (cp->fixed_quality) {
7022 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7024 tcp->rates[j] = parameters->tcp_rates[j];
7026 if (cp->fixed_quality) { /* add fixed_quality */
7027 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7029 tcp->rates[j] = parameters->tcp_rates[j];
7033 tcp->csty = parameters->csty;
7034 tcp->prg = parameters->prog_order;
7035 tcp->mct = parameters->tcp_mct;
7039 if (parameters->numpocs) {
7040 /* initialisation of POC */
7042 assert( parameters->numpocs >= 0 );
7043 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7044 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7045 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7046 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7047 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7048 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7049 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7050 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7051 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7052 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7056 tcp->numpocs = numpocs_tile -1 ;
7061 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7063 for (i = 0; i < image->numcomps; i++) {
7064 opj_tccp_t *tccp = &tcp->tccps[i];
7065 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7066 tccp->numresolutions = parameters->numresolution;
7067 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7068 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7069 tccp->cblksty = parameters->mode;
7070 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7071 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7073 assert(parameters->roi_compno >= 0);
7074 if (i == (OPJ_UINT32)parameters->roi_compno) {
7075 tccp->roishift = parameters->roi_shift;
7080 if(parameters->cp_cinema)
7082 /*Precinct size for lowest frequency subband=128*/
7085 /*Precinct size at all other resolutions = 256*/
7086 for (j = 1; j < tccp->numresolutions; j++) {
7091 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7093 assert(tccp->numresolutions > 0);
7094 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7095 if (p < parameters->res_spec) {
7097 if (parameters->prcw_init[p] < 1) {
7100 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7103 if (parameters->prch_init[p] < 1) {
7106 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7110 int res_spec = parameters->res_spec;
7111 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7112 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7114 if (size_prcw < 1) {
7117 tccp->prcw[j] = int_floorlog2(size_prcw);
7120 if (size_prch < 1) {
7123 tccp->prch[j] = int_floorlog2(size_prch);
7127 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7130 for (j = 0; j < tccp->numresolutions; j++) {
7137 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7142 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7143 opj_cparameters_t *parameters,
7145 struct opj_event_mgr * p_manager)
7147 OPJ_UINT32 i, j, tileno, numpocs_tile;
7148 opj_cp_v2_t *cp = 00;
7151 if(!p_j2k || !parameters || ! image) {
7155 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7156 cp = &(p_j2k->m_cp);
7158 /* set default values for cp */
7163 copy user encoding parameters
7165 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7166 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7167 cp->rsiz = parameters->cp_rsiz;
7168 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7169 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7170 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7172 /* mod fixed_quality */
7173 if (parameters->cp_matrice) {
7174 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7175 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7176 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7180 cp->tdx = parameters->cp_tdx;
7181 cp->tdy = parameters->cp_tdy;
7184 cp->tx0 = parameters->cp_tx0;
7185 cp->ty0 = parameters->cp_ty0;
7187 /* comment string */
7188 if(parameters->cp_comment) {
7189 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7191 strcpy(cp->comment, parameters->cp_comment);
7196 calculate other encoding parameters
7199 if (parameters->tile_size_on) {
7200 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7201 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7203 cp->tdx = image->x1 - cp->tx0;
7204 cp->tdy = image->y1 - cp->ty0;
7207 if (parameters->tp_on) {
7208 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7209 cp->m_specific_param.m_enc.m_tp_on = 1;
7214 calculate JPWL encoding parameters
7217 if (parameters->jpwl_epc_on) {
7221 cp->epc_on = OPJ_TRUE;
7222 cp->info_on = OPJ_FALSE; /* no informative technique */
7225 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7226 cp->epb_on = OPJ_TRUE;
7228 cp->hprot_MH = parameters->jpwl_hprot_MH;
7229 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7230 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7231 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7233 /* if tile specs are not specified, copy MH specs */
7234 if (cp->hprot_TPH[0] == -1) {
7235 cp->hprot_TPH_tileno[0] = 0;
7236 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7238 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7239 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7240 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7241 cp->pprot[i] = parameters->jpwl_pprot[i];
7245 /* set ESD writing */
7246 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7247 cp->esd_on = OPJ_TRUE;
7249 cp->sens_size = parameters->jpwl_sens_size;
7250 cp->sens_addr = parameters->jpwl_sens_addr;
7251 cp->sens_range = parameters->jpwl_sens_range;
7253 cp->sens_MH = parameters->jpwl_sens_MH;
7254 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7255 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7256 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7260 /* always set RED writing to false: we are at the encoder */
7261 cp->red_on = OPJ_FALSE;
7264 cp->epc_on = OPJ_FALSE;
7266 #endif /* USE_JPWL */
7269 /* initialize the mutiple tiles */
7270 /* ---------------------------- */
7271 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7272 if (parameters->numpocs) {
7273 /* initialisation of POC */
7274 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7278 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7279 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7280 tcp->numlayers = parameters->tcp_numlayers;
7282 for (j = 0; j < tcp->numlayers; j++) {
7283 if(cp->m_specific_param.m_enc.m_cinema){
7284 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7285 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7287 tcp->rates[j] = parameters->tcp_rates[j];
7289 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7290 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7292 tcp->rates[j] = parameters->tcp_rates[j];
7297 tcp->csty = parameters->csty;
7298 tcp->prg = parameters->prog_order;
7299 tcp->mct = parameters->tcp_mct;
7304 if (parameters->numpocs) {
7305 /* initialisation of POC */
7308 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7309 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7310 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7312 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7313 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7314 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7315 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7316 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7317 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7318 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7324 tcp->numpocs = numpocs_tile -1 ;
7329 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7331 if (parameters->mct_data) {
7333 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7336 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7337 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7338 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7339 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7342 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7343 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7344 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7346 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7347 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7349 tcp->mct_norms = (OPJ_FLOAT64*)
7350 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7352 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7355 for (i = 0; i < image->numcomps; i++) {
7356 opj_tccp_t *tccp = &tcp->tccps[i];
7357 tccp->m_dc_level_shift = l_dc_shift[i];
7360 j2k_setup_mct_encoding(tcp,image);
7364 for (i = 0; i < image->numcomps; i++) {
7365 opj_tccp_t *tccp = &tcp->tccps[i];
7366 opj_image_comp_t * l_comp = &(image->comps[i]);
7368 if (! l_comp->sgnd) {
7369 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7374 for (i = 0; i < image->numcomps; i++) {
7375 opj_tccp_t *tccp = &tcp->tccps[i];
7377 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7378 tccp->numresolutions = parameters->numresolution;
7379 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7380 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7381 tccp->cblksty = parameters->mode;
7382 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7383 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7386 if (i == parameters->roi_compno) {
7387 tccp->roishift = parameters->roi_shift;
7392 if(parameters->cp_cinema) {
7393 //Precinct size for lowest frequency subband=128
7396 //Precinct size at all other resolutions = 256
7397 for (j = 1; j < tccp->numresolutions; j++) {
7402 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7403 OPJ_INT32 p = 0, it_res;
7404 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7405 if (p < parameters->res_spec) {
7407 if (parameters->prcw_init[p] < 1) {
7408 tccp->prcw[it_res] = 1;
7410 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7413 if (parameters->prch_init[p] < 1) {
7414 tccp->prch[it_res] = 1;
7416 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7420 int res_spec = parameters->res_spec;
7421 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7422 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7424 if (size_prcw < 1) {
7425 tccp->prcw[it_res] = 1;
7427 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7430 if (size_prch < 1) {
7431 tccp->prch[it_res] = 1;
7433 tccp->prch[it_res] = int_floorlog2(size_prch);
7437 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7440 for (j = 0; j < tccp->numresolutions; j++) {
7447 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7451 if (parameters->mct_data) {
7452 opj_free(parameters->mct_data);
7453 parameters->mct_data = 00;
7458 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7461 opj_cp_t *cp = NULL;
7463 opj_tcd_t *tcd = NULL; /* TCD component */
7471 j2k->cstr_info = cstr_info;
7474 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7475 cstr_info->image_w = image->x1 - image->x0;
7476 cstr_info->image_h = image->y1 - image->y0;
7477 cstr_info->prog = (&cp->tcps[0])->prg;
7478 cstr_info->tw = cp->tw;
7479 cstr_info->th = cp->th;
7480 cstr_info->tile_x = cp->tdx; /* new version parser */
7481 cstr_info->tile_y = cp->tdy; /* new version parser */
7482 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7483 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7484 cstr_info->numcomps = image->numcomps;
7485 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7486 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7487 for (compno=0; compno < image->numcomps; compno++) {
7488 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7490 cstr_info->D_max = 0.0; /* ADD Marcela */
7491 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7492 cstr_info->maxmarknum = 100;
7493 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7494 cstr_info->marknum = 0;
7504 for (compno = 1; compno < image->numcomps; compno++) {
7505 j2k_write_coc(j2k, compno);
7506 j2k_write_qcc(j2k, compno);
7510 for (compno = 0; compno < image->numcomps; compno++) {
7511 opj_tcp_t *tcp = &cp->tcps[0];
7512 if (tcp->tccps[compno].roishift)
7513 j2k_write_rgn(j2k, compno, 0);
7515 if (cp->comment != NULL) {
7519 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7523 if (cp->cinema == CINEMA4K_24) {
7528 /* uncomment only for testing JPSEC marker writing */
7529 /* j2k_write_sec(j2k); */
7533 cstr_info->main_head_end = cio_tell(cio) - 1;
7536 /**** Main Header ENDS here ***/
7538 /* create the tile encoder */
7539 tcd = tcd_create(j2k->cinfo);
7541 /* encode each tile */
7542 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7546 int acc_pack_num = 0;
7550 opj_tcp_t *tcp = &cp->tcps[tileno];
7551 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7553 j2k->curtileno = tileno;
7554 j2k->cur_tp_num = 0;
7555 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7556 /* initialisation before tile encoding */
7558 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7560 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7565 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7566 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7567 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7568 cstr_info->tile[j2k->curtileno].marknum = 0;
7572 for(pino = 0; pino <= tcp->numpocs; pino++) {
7576 /*Get number of tile parts*/
7577 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7578 tcd->tp_pos = cp->tp_pos;
7580 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7581 j2k->tp_num = tilepartno;
7584 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7585 cio_tell(cio) + j2k->pos_correction;
7589 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7590 for (compno = 1; compno < image->numcomps; compno++) {
7591 j2k_write_coc(j2k, compno);
7592 j2k_write_qcc(j2k, compno);
7594 if (cp->tcps[tileno].numpocs) {
7601 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7602 cio_tell(cio) + j2k->pos_correction + 1;
7605 j2k_write_sod(j2k, tcd);
7609 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7610 cio_tell(cio) + j2k->pos_correction - 1;
7611 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7613 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7614 cstr_info->packno - acc_pack_num;
7615 acc_pack_num = cstr_info->packno;
7623 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7628 if (tile->PPT) { // BAD PPT !!!
7631 PPT_file=fopen("PPT","rb");
7632 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7633 for (i=0;i<tile->len_ppt;i++) {
7635 fread(&elmt, 1, 1, PPT_file);
7636 fwrite(&elmt,1,1,f);
7645 /* destroy the tile encoder */
7646 tcd_free_encode(tcd);
7649 opj_free(j2k->cur_totnum_tp);
7654 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7656 /* The following adjustment is done to adjust the codestream size */
7657 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7658 /* the first bunch of bytes is not in the codestream */
7659 cstr_info->codestream_size -= cstr_info->main_head_start;
7665 preparation of JPWL marker segments
7669 /* encode according to JPWL */
7670 jpwl_encode(j2k, cio, image);
7673 #endif /* USE_JPWL */
7678 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7680 assert(cstr_info != 00);
7682 /* expand the list? */
7683 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7684 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7685 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7688 /* add the marker */
7689 cstr_info->marker[cstr_info->marknum].type = type;
7690 cstr_info->marker[cstr_info->marknum].pos = pos;
7691 cstr_info->marker[cstr_info->marknum].len = len;
7692 cstr_info->marknum++;
7696 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7698 assert(cstr_index != 00);
7700 /* expand the list? */
7701 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7702 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7703 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7706 /* add the marker */
7707 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7708 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7709 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7710 cstr_index->marknum++;
7714 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7716 opj_marker_info_t *marker;
7718 assert(cstr_info != 00);
7720 /* expand the list? */
7721 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7722 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7723 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7726 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7728 /* add the marker */
7729 marker->type = type;
7732 cstr_info->tile[tileno].marknum++;
7735 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)
7737 assert(cstr_index != 00);
7738 assert(cstr_index->tile_index != 00);
7740 /* expand the list? */
7741 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7742 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7743 cstr_index->tile_index[tileno].marker =
7744 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7745 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7748 /* add the marker */
7749 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7750 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7751 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7752 cstr_index->tile_index[tileno].marknum++;
7754 if (type == J2K_MS_SOT) {
7755 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7757 if (cstr_index->tile_index[tileno].tp_index)
7758 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7765 * -----------------------------------------------------------------------
7766 * -----------------------------------------------------------------------
7767 * -----------------------------------------------------------------------
7771 * Ends the decompression procedures and possibiliy add data to be read after the
7774 opj_bool j2k_end_decompress(
7775 opj_j2k_v2_t *p_j2k,
7776 opj_stream_private_t *p_stream,
7777 opj_event_mgr_t * p_manager)
7786 * Reads a jpeg2000 codestream header structure.
7789 * @param p_stream the stream to read data from.
7790 * @param p_j2k the jpeg2000 codec.
7791 * @param p_manager the user event manager.
7793 * @return true if the box is valid.
7795 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7796 opj_j2k_v2_t* p_j2k,
7797 opj_image_t** p_image,
7798 struct opj_event_mgr* p_manager )
7801 assert(p_j2k != 00);
7802 assert(p_stream != 00);
7803 assert(p_manager != 00);
7805 /* create an empty image header */
7806 p_j2k->m_private_image = opj_image_create0();
7807 if (! p_j2k->m_private_image) {
7811 /* customization of the validation */
7812 j2k_setup_decoding_validation(p_j2k);
7814 /* validation of the parameters codec */
7815 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7816 opj_image_destroy(p_j2k->m_private_image);
7817 p_j2k->m_private_image = NULL;
7821 /* customization of the encoding */
7822 j2k_setup_header_reading(p_j2k);
7825 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7826 opj_image_destroy(p_j2k->m_private_image);
7827 p_j2k->m_private_image = NULL;
7831 *p_image = opj_image_create0();
7836 /* Copy codestream image information to the output image */
7837 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7839 /*Allocate and initialize some elements of codestrem index*/
7840 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7848 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7850 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7853 assert(p_j2k != 00);
7855 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7857 /* DEVELOPER CORNER, add your custom procedures */
7858 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7863 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7864 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7866 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7869 assert(p_j2k != 00);
7871 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7872 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7873 /* DEVELOPER CORNER, add your custom validation procedure */
7879 * The mct encoding validation procedure.
7881 * @param p_j2k the jpeg2000 codec to validate.
7882 * @param p_stream the input stream to validate.
7883 * @param p_manager the user event manager.
7885 * @return true if the parameters are correct.
7887 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7888 opj_stream_private_t *p_stream,
7889 opj_event_mgr_t * p_manager )
7891 opj_bool l_is_valid = OPJ_TRUE;
7895 assert(p_j2k != 00);
7896 assert(p_stream != 00);
7897 assert(p_manager != 00);
7899 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7900 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7901 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7903 for (i=0;i<l_nb_tiles;++i) {
7904 if (l_tcp->mct == 2) {
7905 opj_tccp_t * l_tccp = l_tcp->tccps;
7906 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7908 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7909 l_is_valid &= ! (l_tccp->qmfbid & 1);
7920 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7923 OPJ_UINT32 l_indix = 1;
7924 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7925 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7926 OPJ_UINT32 l_mct_size,l_nb_elem;
7927 OPJ_FLOAT32 * l_data, * l_current_data;
7928 opj_tccp_t * l_tccp;
7931 assert(p_tcp != 00);
7933 if (p_tcp->mct != 2) {
7937 if (p_tcp->m_mct_decoding_matrix) {
7938 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7939 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7941 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));
7942 if (! p_tcp->m_mct_records) {
7945 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7947 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7949 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7951 if (l_mct_deco_data->m_data) {
7952 opj_free(l_mct_deco_data->m_data);
7953 l_mct_deco_data->m_data = 00;
7956 l_mct_deco_data->m_index = l_indix++;
7957 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7958 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7959 l_nb_elem = p_image->numcomps * p_image->numcomps;
7960 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7961 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7963 if (! l_mct_deco_data->m_data) {
7967 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);
7969 l_mct_deco_data->m_data_size = l_mct_size;
7970 ++p_tcp->m_nb_mct_records;
7973 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7974 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7975 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));
7977 if (! p_tcp->m_mct_records) {
7981 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7982 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7984 if (l_mct_deco_data) {
7985 l_mct_deco_data = l_mct_offset_data - 1;
7989 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7991 if (l_mct_offset_data->m_data) {
7992 opj_free(l_mct_offset_data->m_data);
7993 l_mct_offset_data->m_data = 00;
7996 l_mct_offset_data->m_index = l_indix++;
7997 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7998 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7999 l_nb_elem = p_image->numcomps;
8000 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8001 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8003 if (! l_mct_offset_data->m_data) {
8007 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8009 opj_free(l_mct_offset_data->m_data);
8010 l_mct_offset_data->m_data = 00;
8014 l_tccp = p_tcp->tccps;
8015 l_current_data = l_data;
8017 for (i=0;i<l_nb_elem;++i) {
8018 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8022 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8026 l_mct_offset_data->m_data_size = l_mct_size;
8028 ++p_tcp->m_nb_mct_records;
8030 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8031 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8032 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8033 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8035 if (! p_tcp->m_mcc_records) {
8038 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8039 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));
8043 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8044 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8045 l_mcc_data->m_is_irreversible = 1;
8046 l_mcc_data->m_nb_comps = p_image->numcomps;
8047 l_mcc_data->m_index = l_indix++;
8048 l_mcc_data->m_offset_array = l_mct_offset_data;
8049 ++p_tcp->m_nb_mcc_records;
8055 * Builds the cp decoder parameters to use to decode tile.
8057 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8058 opj_stream_private_t *p_stream,
8059 opj_event_mgr_t * p_manager )
8061 /* add here initialization of cp
8062 copy paste of setup_decoder */
8070 * Builds the cp encoder parameters to use to encode tile.
8072 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8073 opj_stream_private_t *p_stream,
8074 opj_event_mgr_t * p_manager )
8076 /* add here initialization of cp
8077 copy paste of setup_encoder */
8085 * The default encoding validation procedure without any extension.
8087 * @param p_j2k the jpeg2000 codec to validate.
8088 * @param p_stream the input stream to validate.
8089 * @param p_manager the user event manager.
8091 * @return true if the parameters are correct.
8093 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8094 opj_stream_private_t *p_stream,
8095 opj_event_mgr_t * p_manager )
8097 opj_bool l_is_valid = OPJ_TRUE;
8100 assert(p_j2k != 00);
8101 assert(p_stream != 00);
8102 assert(p_manager != 00);
8104 /* STATE checking */
8105 /* make sure the state is at 0 */
8106 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8108 /* POINTER validation */
8109 /* make sure a p_j2k codec is present */
8110 l_is_valid &= (p_j2k->m_procedure_list != 00);
8111 /* make sure a validation list is present */
8112 l_is_valid &= (p_j2k->m_validation_list != 00);
8114 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8115 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8119 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8120 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8124 /* PARAMETER VALIDATION */
8129 * The default decoding validation procedure without any extension.
8131 * @param p_j2k the jpeg2000 codec to validate.
8132 * @param p_stream the input stream to validate.
8133 * @param p_manager the user event manager.
8135 * @return true if the parameters are correct.
8137 opj_bool j2k_decoding_validation (
8138 opj_j2k_v2_t *p_j2k,
8139 opj_stream_private_t *p_stream,
8140 opj_event_mgr_t * p_manager
8143 opj_bool l_is_valid = OPJ_TRUE;
8146 assert(p_j2k != 00);
8147 assert(p_stream != 00);
8148 assert(p_manager != 00);
8151 /* STATE checking */
8152 /* make sure the state is at 0 */
8154 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8156 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8158 /* POINTER validation */
8159 /* make sure a p_j2k codec is present */
8160 /* make sure a procedure list is present */
8161 l_is_valid &= (p_j2k->m_procedure_list != 00);
8162 /* make sure a validation list is present */
8163 l_is_valid &= (p_j2k->m_validation_list != 00);
8165 /* PARAMETER VALIDATION */
8169 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8170 struct opj_stream_private *p_stream,
8171 struct opj_event_mgr * p_manager)
8173 OPJ_UINT32 l_current_marker;
8174 OPJ_UINT32 l_marker_size;
8175 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8178 assert(p_stream != 00);
8179 assert(p_j2k != 00);
8180 assert(p_manager != 00);
8182 /* We enter in the main header */
8183 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8185 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8186 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8187 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8191 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8192 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8193 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8197 /* Read 2 bytes as the new marker ID */
8198 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8200 /* Try to read until the SOT is detected */
8201 while (l_current_marker != J2K_MS_SOT) {
8203 /* Check if the current marker ID is valid */
8204 if (l_current_marker < 0xff00) {
8205 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8209 /* Get the marker handler from the marker ID */
8210 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8212 /* Manage case where marker is unknown */
8213 if (l_marker_handler->id == J2K_MS_UNK) {
8214 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8215 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8219 if (l_current_marker == J2K_MS_SOT)
8220 break; /* SOT marker is detected main header is completely read */
8221 else /* Get the marker handler from the marker ID */
8222 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8225 /* Check if the marker is known and if it is the right place to find it */
8226 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8227 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8231 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8232 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8233 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8237 /* read 2 bytes as the marker size */
8238 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8239 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8241 /* Check if the marker size is compatible with the header data size */
8242 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8243 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8244 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8245 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8248 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8251 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8252 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) {
8253 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8257 /* Read the marker segment with the correct marker handler */
8258 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8259 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8263 /* Add the marker to the codestream index*/
8264 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8265 l_marker_handler->id,
8266 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8267 l_marker_size + 4 );
8269 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8270 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8271 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8275 /* read 2 bytes as the new marker ID */
8276 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8279 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8281 /* Position of the last element if the main header */
8282 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8284 /* Next step: read a tile-part header */
8285 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8291 * Excutes the given procedures on the given codec.
8293 * @param p_procedure_list the list of procedures to execute
8294 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8295 * @param p_stream the stream to execute the procedures on.
8296 * @param p_manager the user manager.
8298 * @return true if all the procedures were successfully executed.
8300 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8301 opj_procedure_list_t * p_procedure_list,
8302 opj_stream_private_t *p_stream,
8303 opj_event_mgr_t * p_manager )
8305 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8306 opj_bool l_result = OPJ_TRUE;
8307 OPJ_UINT32 l_nb_proc, i;
8310 assert(p_procedure_list != 00);
8311 assert(p_j2k != 00);
8312 assert(p_stream != 00);
8313 assert(p_manager != 00);
8316 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8317 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);
8319 for (i=0;i<l_nb_proc;++i) {
8320 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8324 /* and clear the procedure list at the end.*/
8325 opj_procedure_list_clear(p_procedure_list);
8330 opj_bool j2k_copy_default_tcp_and_create_tcd
8332 opj_j2k_v2_t * p_j2k,
8333 opj_stream_private_t *p_stream,
8334 opj_event_mgr_t * p_manager
8337 opj_tcp_v2_t * l_tcp = 00;
8338 opj_tcp_v2_t * l_default_tcp = 00;
8339 OPJ_UINT32 l_nb_tiles;
8341 opj_tccp_t *l_current_tccp = 00;
8342 OPJ_UINT32 l_tccp_size;
8343 OPJ_UINT32 l_mct_size;
8344 opj_image_t * l_image;
8345 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8346 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8347 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8348 OPJ_UINT32 l_offset;
8351 assert(p_j2k != 00);
8352 assert(p_stream != 00);
8353 assert(p_manager != 00);
8355 l_image = p_j2k->m_private_image;
8356 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8357 l_tcp = p_j2k->m_cp.tcps;
8358 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8359 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8360 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8363 for (i=0; i<l_nb_tiles; ++i) {
8364 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8365 l_current_tccp = l_tcp->tccps;
8366 /*Copy default coding parameters into the current tile coding parameters*/
8367 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8368 /* Initialize some values of the current tile coding parameters*/
8370 l_tcp->ppt_data = 00;
8371 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8372 l_tcp->tccps = l_current_tccp;
8374 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8375 if (l_default_tcp->m_mct_decoding_matrix) {
8376 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8377 if (! l_tcp->m_mct_decoding_matrix ) {
8380 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8383 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8384 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8385 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8386 if (! l_tcp->m_mct_records) {
8389 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8391 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8392 l_src_mct_rec = l_default_tcp->m_mct_records;
8393 l_dest_mct_rec = l_tcp->m_mct_records;
8395 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8397 if (l_src_mct_rec->m_data) {
8399 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8400 if(! l_dest_mct_rec->m_data) {
8403 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8410 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8411 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8412 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8413 if (! l_tcp->m_mcc_records) {
8416 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8418 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8419 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8420 l_dest_mcc_rec = l_tcp->m_mcc_records;
8422 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8424 if (l_src_mcc_rec->m_decorrelation_array) {
8425 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8426 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8429 if (l_src_mcc_rec->m_offset_array) {
8430 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8431 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8438 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8439 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8441 /* Move to next tile cp*/
8445 /* Create the current tile decoder*/
8446 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8447 if (! p_j2k->m_tcd ) {
8451 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8452 tcd_destroy_v2(p_j2k->m_tcd);
8454 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8462 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8463 * with the marker value.
8464 * @param p_id Marker value to look up
8466 * @return the handler associated with the id.
8468 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8470 const opj_dec_memory_marker_handler_t *e;
8471 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8472 if (e->id == p_id) {
8473 break; /* we find a handler corresponding to the marker ID*/
8481 * Destroys a jpeg2000 codec.
8483 * @param p_j2k the jpeg20000 structure to destroy.
8485 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8491 if (p_j2k->m_is_decoder) {
8493 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8494 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8495 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8496 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8499 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8500 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8501 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8502 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8507 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8508 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8509 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8512 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8513 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8514 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8515 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8518 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8519 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8520 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8521 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8525 tcd_destroy_v2(p_j2k->m_tcd);
8527 j2k_cp_destroy(&(p_j2k->m_cp));
8528 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8530 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8531 p_j2k->m_procedure_list = 00;
8533 opj_procedure_list_destroy(p_j2k->m_validation_list);
8534 p_j2k->m_procedure_list = 00;
8536 j2k_destroy_cstr_index(p_j2k->cstr_index);
8537 p_j2k->cstr_index = NULL;
8539 opj_image_destroy(p_j2k->m_private_image);
8540 p_j2k->m_private_image = NULL;
8542 opj_image_destroy(p_j2k->m_output_image);
8543 p_j2k->m_output_image = NULL;
8548 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8552 if (p_cstr_ind->marker) {
8553 opj_free(p_cstr_ind->marker);
8554 p_cstr_ind->marker = NULL;
8557 if (p_cstr_ind->tile_index) {
8558 OPJ_UINT32 it_tile = 0;
8560 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8562 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8563 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8564 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8567 if(p_cstr_ind->tile_index[it_tile].tp_index){
8568 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8569 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8572 if(p_cstr_ind->tile_index[it_tile].marker){
8573 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8574 p_cstr_ind->tile_index[it_tile].marker = NULL;
8579 opj_free( p_cstr_ind->tile_index);
8580 p_cstr_ind->tile_index = NULL;
8583 opj_free(p_cstr_ind);
8590 * Destroys a tile coding parameter structure.
8592 * @param p_tcp the tile coding parameter to destroy.
8594 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8600 if (p_tcp->ppt_buffer != 00) {
8601 opj_free(p_tcp->ppt_buffer);
8602 p_tcp->ppt_buffer = 00;
8605 if (p_tcp->tccps != 00) {
8606 opj_free(p_tcp->tccps);
8610 if (p_tcp->m_mct_coding_matrix != 00) {
8611 opj_free(p_tcp->m_mct_coding_matrix);
8612 p_tcp->m_mct_coding_matrix = 00;
8615 if (p_tcp->m_mct_decoding_matrix != 00) {
8616 opj_free(p_tcp->m_mct_decoding_matrix);
8617 p_tcp->m_mct_decoding_matrix = 00;
8620 if (p_tcp->m_mcc_records) {
8621 opj_free(p_tcp->m_mcc_records);
8622 p_tcp->m_mcc_records = 00;
8623 p_tcp->m_nb_max_mcc_records = 0;
8624 p_tcp->m_nb_mcc_records = 0;
8627 if (p_tcp->m_mct_records) {
8628 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8631 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8632 if (l_mct_data->m_data) {
8633 opj_free(l_mct_data->m_data);
8634 l_mct_data->m_data = 00;
8640 opj_free(p_tcp->m_mct_records);
8641 p_tcp->m_mct_records = 00;
8644 if (p_tcp->mct_norms != 00) {
8645 opj_free(p_tcp->mct_norms);
8646 p_tcp->mct_norms = 00;
8649 j2k_tcp_data_destroy(p_tcp);
8654 * Destroys the data inside a tile coding parameter structure.
8656 * @param p_tcp the tile coding parameter which contain data to destroy.
8658 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8660 if (p_tcp->m_data) {
8661 opj_free(p_tcp->m_data);
8662 p_tcp->m_data = NULL;
8663 p_tcp->m_data_size = 0;
8668 * Destroys a coding parameter structure.
8670 * @param p_cp the coding parameter to destroy.
8672 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8674 OPJ_UINT32 l_nb_tiles;
8675 opj_tcp_v2_t * l_current_tile = 00;
8686 l_current_tile = p_cp->tcps;
8687 l_nb_tiles = p_cp->th * p_cp->tw;
8690 (i = 0; i < l_nb_tiles; ++i)
8692 j2k_tcp_destroy(l_current_tile);
8695 opj_free(p_cp->tcps);
8699 (p_cp->ppm_buffer != 00)
8701 opj_free(p_cp->ppm_buffer);
8702 p_cp->ppm_buffer = 00;
8705 (p_cp->comment != 00)
8707 opj_free(p_cp->comment);
8711 (! p_cp->m_is_decoder)
8714 (p_cp->m_specific_param.m_enc.m_matrice)
8716 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8717 p_cp->m_specific_param.m_enc.m_matrice = 00;
8725 * Reads a tile header.
8726 * @param p_j2k the jpeg2000 codec.
8727 * @param p_stream the stream to write data to.
8728 * @param p_manager the user event manager.
8730 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8731 OPJ_UINT32 * p_tile_index,
8732 OPJ_UINT32 * p_data_size,
8733 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8734 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8735 OPJ_UINT32 * p_nb_comps,
8737 opj_stream_private_t *p_stream,
8738 opj_event_mgr_t * p_manager )
8740 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8741 OPJ_UINT32 l_marker_size;
8742 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8743 opj_tcp_v2_t * l_tcp = NULL;
8744 OPJ_UINT32 l_nb_tiles;
8747 assert(p_stream != 00);
8748 assert(p_j2k != 00);
8749 assert(p_manager != 00);
8751 /* Reach the End Of Codestream ?*/
8752 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8753 l_current_marker = J2K_MS_EOC;
8755 /* We need to encounter a SOT marker (a new tile-part header) */
8756 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8760 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8761 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8763 /* Try to read until the Start Of Data is detected */
8764 while (l_current_marker != J2K_MS_SOD) {
8766 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8767 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8768 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8772 /* Read 2 bytes from the buffer as the marker size */
8773 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8775 /* Why this condition? FIXME */
8776 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8777 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8779 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8781 /* Get the marker handler from the marker ID */
8782 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8784 /* Check if the marker is known and if it is the right place to find it */
8785 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8786 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8789 /* FIXME manage case of unknown marker as in the main header ? */
8791 /* Check if the marker size is compatible with the header data size */
8792 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8793 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8794 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8795 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8798 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8801 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8802 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) {
8803 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8807 /* Read the marker segment with the correct marker handler */
8808 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8809 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8813 /* Add the marker to the codestream index*/
8814 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8816 l_marker_handler->id,
8817 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8818 l_marker_size + 4 );
8820 /* Keep the position of the last SOT marker read */
8821 if ( l_marker_handler->id == J2K_MS_SOT ) {
8822 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8823 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8825 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8830 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8831 /* Skip the rest of the tile part header*/
8832 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) {
8833 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8836 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8839 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8840 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8841 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8844 /* Read 2 bytes from the buffer as the new marker ID */
8845 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8849 /* If we didn't skip data before, we need to read the SOD marker*/
8850 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8851 /* Try to read the SOD marker and skip data ? FIXME */
8852 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8858 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8859 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8860 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8861 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8865 /* Read 2 bytes from buffer as the new marker ID */
8866 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8870 /* Indicate we will try to read a new tile-part header*/
8871 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8872 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8873 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8875 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8876 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8877 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8881 /* Read 2 bytes from buffer as the new marker ID */
8882 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8886 /* Current marker is the EOC marker ?*/
8887 if (l_current_marker == J2K_MS_EOC) {
8888 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8889 p_j2k->m_current_tile_number = 0;
8890 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8895 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8896 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8897 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8899 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8900 ++p_j2k->m_current_tile_number;
8904 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8905 *p_go_on = OPJ_FALSE;
8911 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8912 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8916 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8917 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8919 *p_tile_index = p_j2k->m_current_tile_number;
8920 *p_go_on = OPJ_TRUE;
8921 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8922 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8923 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8924 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8925 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8926 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8928 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8934 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8935 OPJ_UINT32 p_tile_index,
8937 OPJ_UINT32 p_data_size,
8938 opj_stream_private_t *p_stream,
8939 opj_event_mgr_t * p_manager )
8941 OPJ_UINT32 l_current_marker;
8942 OPJ_BYTE l_data [2];
8943 opj_tcp_v2_t * l_tcp;
8946 assert(p_stream != 00);
8947 assert(p_j2k != 00);
8948 assert(p_manager != 00);
8950 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8951 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8955 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8956 if (! l_tcp->m_data) {
8957 j2k_tcp_destroy(l_tcp);
8961 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8965 p_j2k->cstr_index) ) {
8966 j2k_tcp_destroy(l_tcp);
8967 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8971 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8975 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8976 * we destroy just the data which will be re-read in read_tile_header*/
8977 /*j2k_tcp_destroy(l_tcp);
8978 p_j2k->m_tcd->tcp = 0;*/
8979 j2k_tcp_data_destroy(l_tcp);
8981 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8982 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8984 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8985 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8986 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8990 opj_read_bytes(l_data,&l_current_marker,2);
8992 if (l_current_marker == J2K_MS_EOC) {
8993 p_j2k->m_current_tile_number = 0;
8994 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8996 else if (l_current_marker != J2K_MS_SOT)
8998 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9007 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9009 OPJ_UINT32 i,j,k = 0;
9010 OPJ_UINT32 l_width_src,l_height_src;
9011 OPJ_UINT32 l_width_dest,l_height_dest;
9012 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9013 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9014 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9015 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9016 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9018 opj_image_comp_t * l_img_comp_src = 00;
9019 opj_image_comp_t * l_img_comp_dest = 00;
9021 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9022 opj_image_t * l_image_src = 00;
9023 OPJ_UINT32 l_size_comp, l_remaining;
9024 OPJ_INT32 * l_dest_ptr;
9025 opj_tcd_resolution_v2_t* l_res= 00;
9027 l_tilec = p_tcd->tcd_image->tiles->comps;
9028 l_image_src = p_tcd->image;
9029 l_img_comp_src = l_image_src->comps;
9031 l_img_comp_dest = p_output_image->comps;
9033 for (i=0; i<l_image_src->numcomps; i++) {
9035 /* Allocate output component buffer if necessary */
9036 if (!l_img_comp_dest->data) {
9038 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9039 if (! l_img_comp_dest->data) {
9044 /* Copy info from decoded comp image to output image */
9045 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9048 /* Compute the precision of the output buffer */
9049 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9050 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9051 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9057 if (l_size_comp == 3) {
9062 /* Current tile component size*/
9064 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9065 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9068 l_width_src = (l_res->x1 - l_res->x0);
9069 l_height_src = (l_res->y1 - l_res->y0);
9071 /* Border of the current output component*/
9072 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9073 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9074 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9075 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9078 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9079 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9083 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9084 * of the input buffer (decoded tile component) which will be move
9085 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9086 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9087 * by this input area.
9089 assert( l_res->x0 >= 0);
9090 assert( l_res->x1 >= 0);
9091 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9092 l_start_x_dest = l_res->x0 - l_x0_dest;
9093 l_offset_x0_src = 0;
9095 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9096 l_width_dest = l_width_src;
9097 l_offset_x1_src = 0;
9100 l_width_dest = l_x1_dest - l_res->x0 ;
9101 l_offset_x1_src = l_width_src - l_width_dest;
9105 l_start_x_dest = 0 ;
9106 l_offset_x0_src = l_x0_dest - l_res->x0;
9108 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9109 l_width_dest = l_width_src - l_offset_x0_src;
9110 l_offset_x1_src = 0;
9113 l_width_dest = l_img_comp_dest->w ;
9114 l_offset_x1_src = l_res->x1 - l_x1_dest;
9118 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9119 l_start_y_dest = l_res->y0 - l_y0_dest;
9120 l_offset_y0_src = 0;
9122 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9123 l_height_dest = l_height_src;
9124 l_offset_y1_src = 0;
9127 l_height_dest = l_y1_dest - l_res->y0 ;
9128 l_offset_y1_src = l_height_src - l_height_dest;
9132 l_start_y_dest = 0 ;
9133 l_offset_y0_src = l_y0_dest - l_res->y0;
9135 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9136 l_height_dest = l_height_src - l_offset_y0_src;
9137 l_offset_y1_src = 0;
9140 l_height_dest = l_img_comp_dest->h ;
9141 l_offset_y1_src = l_res->y1 - l_y1_dest;
9145 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9150 /* Compute the input buffer offset */
9151 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9152 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9153 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9155 /* Compute the output buffer offset */
9156 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9157 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9159 /* Move the output buffer to the first place where we will write*/
9160 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9163 fprintf(stdout, "COMPO[%d]:\n",i);
9164 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9165 "\t tile offset:%d, %d, %d, %d\n"
9166 "\t buffer offset: %d; %d, %d\n",
9167 l_res->x0, l_res->y0, l_width_src, l_height_src,
9168 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9169 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9171 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9172 "\t start offset: %d, line offset= %d\n",
9173 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9177 switch (l_size_comp) {
9180 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9181 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9183 if (l_img_comp_src->sgnd) {
9184 for (j = 0 ; j < l_height_dest ; ++j) {
9185 for ( k = 0 ; k < l_width_dest ; ++k) {
9186 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9189 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9190 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9194 for ( j = 0 ; j < l_height_dest ; ++j ) {
9195 for ( k = 0 ; k < l_width_dest ; ++k) {
9196 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9199 l_dest_ptr+= l_line_offset_dest;
9200 l_src_ptr += l_line_offset_src;
9204 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9205 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9210 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9211 l_src_ptr += l_start_offset_src;
9213 if (l_img_comp_src->sgnd) {
9214 for (j=0;j<l_height_dest;++j) {
9215 for (k=0;k<l_width_dest;++k) {
9216 *(l_dest_ptr++) = *(l_src_ptr++);
9219 l_dest_ptr+= l_line_offset_dest;
9220 l_src_ptr += l_line_offset_src ;
9224 for (j=0;j<l_height_dest;++j) {
9225 for (k=0;k<l_width_dest;++k) {
9226 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9229 l_dest_ptr+= l_line_offset_dest;
9230 l_src_ptr += l_line_offset_src ;
9234 l_src_ptr += l_end_offset_src;
9235 p_data = (OPJ_BYTE*) l_src_ptr;
9240 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9241 l_src_ptr += l_start_offset_src;
9243 for (j=0;j<l_height_dest;++j) {
9244 for (k=0;k<l_width_dest;++k) {
9245 *(l_dest_ptr++) = (*(l_src_ptr++));
9248 l_dest_ptr+= l_line_offset_dest;
9249 l_src_ptr += l_line_offset_src ;
9252 l_src_ptr += l_end_offset_src;
9253 p_data = (OPJ_BYTE*) l_src_ptr;
9267 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9269 * @param p_j2k the jpeg2000 codec.
9270 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9271 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9272 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9273 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9274 * @param p_manager the user event manager
9276 * @return true if the area could be set.
9278 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9279 opj_image_t* p_image,
9280 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9281 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9282 struct opj_event_mgr * p_manager )
9284 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9285 opj_image_t * l_image = p_j2k->m_private_image;
9288 OPJ_INT32 l_comp_x1, l_comp_y1;
9289 opj_image_comp_t* l_img_comp = NULL;
9291 /* Check if we are read the main header */
9292 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9293 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9297 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9298 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9300 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9301 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9302 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9303 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9309 /* Check if the positions provided by the user are correct */
9312 assert(p_start_x >= 0 );
9313 assert(p_start_y >= 0 );
9315 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9316 opj_event_msg_v2(p_manager, EVT_ERROR,
9317 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9318 p_start_x, l_image->x1);
9321 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9322 opj_event_msg_v2(p_manager, EVT_WARNING,
9323 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9324 p_start_x, l_image->x0);
9325 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9326 p_image->x0 = l_image->x0;
9329 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9330 p_image->x0 = p_start_x;
9334 if ((OPJ_UINT32)p_start_y > l_image->y1){
9335 opj_event_msg_v2(p_manager, EVT_ERROR,
9336 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9337 p_start_y, l_image->y1);
9340 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9341 opj_event_msg_v2(p_manager, EVT_WARNING,
9342 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9343 p_start_y, l_image->y0);
9344 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9345 p_image->y0 = l_image->y0;
9348 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9349 p_image->y0 = p_start_y;
9353 assert((OPJ_UINT32)p_end_x > 0);
9354 assert((OPJ_UINT32)p_end_y > 0);
9355 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9356 opj_event_msg_v2(p_manager, EVT_ERROR,
9357 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9358 p_end_x, l_image->x0);
9361 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9362 opj_event_msg_v2(p_manager, EVT_WARNING,
9363 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9364 p_end_x, l_image->x1);
9365 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9366 p_image->x1 = l_image->x1;
9369 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9370 p_image->x1 = p_end_x;
9374 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9375 opj_event_msg_v2(p_manager, EVT_ERROR,
9376 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9377 p_end_y, l_image->y0);
9380 if ((OPJ_UINT32)p_end_y > l_image->y1){
9381 opj_event_msg_v2(p_manager, EVT_WARNING,
9382 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9383 p_end_y, l_image->y1);
9384 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9385 p_image->y1 = l_image->y1;
9388 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9389 p_image->y1 = p_end_y;
9393 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9395 l_img_comp = p_image->comps;
9396 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9400 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9401 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9402 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9403 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9405 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9406 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9408 opj_event_msg_v2(p_manager, EVT_ERROR,
9409 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9413 l_img_comp->w = l_w;
9415 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9416 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9418 opj_event_msg_v2(p_manager, EVT_ERROR,
9419 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9423 l_img_comp->h = l_h;
9428 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9429 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9436 /* ----------------------------------------------------------------------- */
9437 /* J2K / JPT decoder interface */
9438 /* ----------------------------------------------------------------------- */
9440 * Creates a J2K decompression structure.
9442 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9444 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9446 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9450 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9452 l_j2k->m_is_decoder = 1;
9453 l_j2k->m_cp.m_is_decoder = 1;
9455 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9456 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9460 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9462 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9463 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9468 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9470 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9472 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9474 /* codestream index creation */
9475 l_j2k->cstr_index = j2k_create_cstr_index();
9477 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9478 if (!l_j2k->cstr_index){
9483 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9486 /* validation list creation */
9487 l_j2k->m_validation_list = opj_procedure_list_create();
9488 if (! l_j2k->m_validation_list) {
9493 /* execution list creation */
9494 l_j2k->m_procedure_list = opj_procedure_list_create();
9495 if (! l_j2k->m_procedure_list) {
9504 opj_codestream_index_t* j2k_create_cstr_index(void)
9506 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9507 opj_calloc(1,sizeof(opj_codestream_index_t));
9511 cstr_index->maxmarknum = 100;
9512 cstr_index->marknum = 0;
9513 cstr_index->marker = (opj_marker_info_t*)
9514 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9515 if (!cstr_index-> marker)
9518 cstr_index->tile_index = NULL;
9525 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9527 * @param p_tile_no the tile index.
9528 * @param p_comp_no the component being outputted.
9529 * @param p_j2k the J2K codec.
9531 * @return the number of bytes taken by the SPCod element.
9533 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9534 OPJ_UINT32 p_tile_no,
9535 OPJ_UINT32 p_comp_no )
9537 opj_cp_v2_t *l_cp = 00;
9538 opj_tcp_v2_t *l_tcp = 00;
9539 opj_tccp_t *l_tccp = 00;
9542 assert(p_j2k != 00);
9544 l_cp = &(p_j2k->m_cp);
9545 l_tcp = &l_cp->tcps[p_tile_no];
9546 l_tccp = &l_tcp->tccps[p_comp_no];
9548 /* preconditions again */
9549 assert(p_tile_no < (l_cp->tw * l_cp->th));
9550 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9552 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9553 return 5 + l_tccp->numresolutions;
9561 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9563 * @param p_comp_no the component number to output.
9564 * @param p_stream the stream to write data to.
9565 * @param p_j2k J2K codec.
9566 * @param p_manager the user event manager.
9569 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9570 OPJ_UINT32 p_tile_no,
9571 OPJ_UINT32 p_comp_no,
9573 OPJ_UINT32 * p_header_size,
9574 struct opj_event_mgr * p_manager )
9577 opj_cp_v2_t *l_cp = 00;
9578 opj_tcp_v2_t *l_tcp = 00;
9579 opj_tccp_t *l_tccp = 00;
9582 assert(p_j2k != 00);
9583 assert(p_header_size != 00);
9584 assert(p_manager != 00);
9585 assert(p_data != 00);
9587 l_cp = &(p_j2k->m_cp);
9588 l_tcp = &l_cp->tcps[p_tile_no];
9589 l_tccp = &l_tcp->tccps[p_comp_no];
9591 /* preconditions again */
9592 assert(p_tile_no < (l_cp->tw * l_cp->th));
9593 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9595 if (*p_header_size < 5) {
9596 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9600 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9603 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9606 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9609 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9612 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9615 *p_header_size = *p_header_size - 5;
9617 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9619 if (*p_header_size < l_tccp->numresolutions) {
9620 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9624 for (i = 0; i < l_tccp->numresolutions; ++i) {
9625 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9629 *p_header_size = *p_header_size - l_tccp->numresolutions;
9636 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9637 * @param p_header_data the data contained in the COM box.
9638 * @param p_j2k the jpeg2000 codec.
9639 * @param p_header_size the size of the data contained in the COM marker.
9640 * @param p_manager the user event manager.
9642 opj_bool j2k_read_SPCod_SPCoc(
9643 opj_j2k_v2_t *p_j2k,
9645 OPJ_BYTE * p_header_data,
9646 OPJ_UINT32 * p_header_size,
9647 struct opj_event_mgr * p_manager
9650 OPJ_UINT32 i, l_tmp;
9651 opj_cp_v2_t *l_cp = NULL;
9652 opj_tcp_v2_t *l_tcp = NULL;
9653 opj_tccp_t *l_tccp = NULL;
9654 OPJ_BYTE * l_current_ptr = NULL;
9657 assert(p_j2k != 00);
9658 assert(p_manager != 00);
9659 assert(p_header_data != 00);
9661 l_cp = &(p_j2k->m_cp);
9662 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9663 &l_cp->tcps[p_j2k->m_current_tile_number] :
9664 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9666 /* precondition again */
9667 assert(compno < p_j2k->m_private_image->numcomps);
9669 l_tccp = &l_tcp->tccps[compno];
9670 l_current_ptr = p_header_data;
9672 /* make sure room is sufficient */
9673 if (*p_header_size < 5) {
9674 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9678 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9679 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9682 /* If user wants to remove more resolutions than the codestream contains, return error */
9683 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9684 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9685 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9686 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9690 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9694 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9698 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9701 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9704 *p_header_size = *p_header_size - 5;
9706 /* use custom precinct size ? */
9707 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9708 if (*p_header_size < l_tccp->numresolutions) {
9709 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9713 for (i = 0; i < l_tccp->numresolutions; ++i) {
9714 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9716 l_tccp->prcw[i] = l_tmp & 0xf;
9717 l_tccp->prch[i] = l_tmp >> 4;
9720 *p_header_size = *p_header_size - l_tccp->numresolutions;
9723 /* set default size for the precinct width and height */
9724 for (i = 0; i < l_tccp->numresolutions; ++i) {
9725 l_tccp->prcw[i] = 15;
9726 l_tccp->prch[i] = 15;
9730 #ifdef WIP_REMOVE_MSD
9732 if (p_j2k->cstr_info && compno == 0) {
9733 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9735 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9736 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9737 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9738 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9739 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9742 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9743 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9752 * Copies the tile component parameters of all the component from the first tile component.
9754 * @param p_j2k the J2k codec.
9756 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9760 opj_cp_v2_t *l_cp = NULL;
9761 opj_tcp_v2_t *l_tcp = NULL;
9762 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9763 OPJ_UINT32 l_prc_size;
9766 assert(p_j2k != 00);
9768 l_cp = &(p_j2k->m_cp);
9769 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9770 &l_cp->tcps[p_j2k->m_current_tile_number] :
9771 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9773 l_ref_tccp = &l_tcp->tccps[0];
9774 l_copied_tccp = l_ref_tccp + 1;
9775 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9777 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9778 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9779 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9780 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9781 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9782 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9783 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9784 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9790 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9792 * @param p_tile_no the tile index.
9793 * @param p_comp_no the component being outputted.
9794 * @param p_j2k the J2K codec.
9796 * @return the number of bytes taken by the SPCod element.
9798 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9799 OPJ_UINT32 p_tile_no,
9800 OPJ_UINT32 p_comp_no )
9802 OPJ_UINT32 l_num_bands;
9804 opj_cp_v2_t *l_cp = 00;
9805 opj_tcp_v2_t *l_tcp = 00;
9806 opj_tccp_t *l_tccp = 00;
9809 assert(p_j2k != 00);
9811 l_cp = &(p_j2k->m_cp);
9812 l_tcp = &l_cp->tcps[p_tile_no];
9813 l_tccp = &l_tcp->tccps[p_comp_no];
9815 /* preconditions again */
9816 assert(p_tile_no < l_cp->tw * l_cp->th);
9817 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9819 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9821 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9822 return 1 + l_num_bands;
9825 return 1 + 2*l_num_bands;
9830 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9832 * @param p_tile_no the tile to output.
9833 * @param p_comp_no the component number to output.
9834 * @param p_data the data buffer.
9835 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9836 * @param p_j2k J2K codec.
9837 * @param p_manager the user event manager.
9840 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9841 OPJ_UINT32 p_tile_no,
9842 OPJ_UINT32 p_comp_no,
9844 OPJ_UINT32 * p_header_size,
9845 struct opj_event_mgr * p_manager )
9847 OPJ_UINT32 l_header_size;
9848 OPJ_UINT32 l_band_no, l_num_bands;
9849 OPJ_UINT32 l_expn,l_mant;
9851 opj_cp_v2_t *l_cp = 00;
9852 opj_tcp_v2_t *l_tcp = 00;
9853 opj_tccp_t *l_tccp = 00;
9856 assert(p_j2k != 00);
9857 assert(p_header_size != 00);
9858 assert(p_manager != 00);
9859 assert(p_data != 00);
9861 l_cp = &(p_j2k->m_cp);
9862 l_tcp = &l_cp->tcps[p_tile_no];
9863 l_tccp = &l_tcp->tccps[p_comp_no];
9865 /* preconditions again */
9866 assert(p_tile_no < l_cp->tw * l_cp->th);
9867 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9869 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9871 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9872 l_header_size = 1 + l_num_bands;
9874 if (*p_header_size < l_header_size) {
9875 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9879 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9882 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9883 l_expn = l_tccp->stepsizes[l_band_no].expn;
9884 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9889 l_header_size = 1 + 2*l_num_bands;
9891 if (*p_header_size < l_header_size) {
9892 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9896 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9899 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9900 l_expn = l_tccp->stepsizes[l_band_no].expn;
9901 l_mant = l_tccp->stepsizes[l_band_no].mant;
9903 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9908 *p_header_size = *p_header_size - l_header_size;
9914 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9916 * @param p_comp_no the component being targeted.
9917 * @param p_header_data the data contained in the COM box.
9918 * @param p_j2k the jpeg2000 codec.
9919 * @param p_header_size the size of the data contained in the COM marker.
9920 * @param p_manager the user event manager.
9922 opj_bool j2k_read_SQcd_SQcc(
9923 opj_j2k_v2_t *p_j2k,
9924 OPJ_UINT32 p_comp_no,
9925 OPJ_BYTE* p_header_data,
9926 OPJ_UINT32 * p_header_size,
9927 struct opj_event_mgr * p_manager
9931 OPJ_UINT32 l_band_no;
9932 opj_cp_v2_t *l_cp = 00;
9933 opj_tcp_v2_t *l_tcp = 00;
9934 opj_tccp_t *l_tccp = 00;
9935 OPJ_BYTE * l_current_ptr = 00;
9936 OPJ_UINT32 l_tmp, l_num_band;
9939 assert(p_j2k != 00);
9940 assert(p_manager != 00);
9941 assert(p_header_data != 00);
9943 l_cp = &(p_j2k->m_cp);
9944 /* come from tile part header or main header ?*/
9945 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9946 &l_cp->tcps[p_j2k->m_current_tile_number] :
9947 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9949 /* precondition again*/
9950 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9952 l_tccp = &l_tcp->tccps[p_comp_no];
9953 l_current_ptr = p_header_data;
9955 if (*p_header_size < 1) {
9956 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9959 *p_header_size -= 1;
9961 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9964 l_tccp->qntsty = l_tmp & 0x1f;
9965 l_tccp->numgbits = l_tmp >> 5;
9966 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9970 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9972 (*p_header_size) / 2;
9974 if( l_num_band > J2K_MAXBANDS ) {
9975 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9976 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9977 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9978 /*return OPJ_FALSE;*/
9983 if (l_cp->correct) {
9985 /* if JPWL is on, we check whether there are too many subbands */
9986 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9987 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9988 "JPWL: bad number of subbands in Sqcx (%d)\n",
9991 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9994 /* we try to correct */
9996 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9997 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10002 #endif /* USE_JPWL */
10004 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10005 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10006 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10008 if (l_band_no < J2K_MAXBANDS){
10009 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10010 l_tccp->stepsizes[l_band_no].mant = 0;
10013 *p_header_size = *p_header_size - l_num_band;
10016 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10017 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10019 if (l_band_no < J2K_MAXBANDS){
10020 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10021 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10024 *p_header_size = *p_header_size - 2*l_num_band;
10027 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10028 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10029 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10030 l_tccp->stepsizes[l_band_no].expn =
10031 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10032 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10033 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10041 * Copies the tile component parameters of all the component from the first tile component.
10043 * @param p_j2k the J2k codec.
10045 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10048 opj_cp_v2_t *l_cp = NULL;
10049 opj_tcp_v2_t *l_tcp = NULL;
10050 opj_tccp_t *l_ref_tccp = NULL;
10051 opj_tccp_t *l_copied_tccp = NULL;
10054 /* preconditions */
10055 assert(p_j2k != 00);
10057 l_cp = &(p_j2k->m_cp);
10058 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10059 &l_cp->tcps[p_j2k->m_current_tile_number] :
10060 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10062 l_ref_tccp = &l_tcp->tccps[0];
10063 l_copied_tccp = l_ref_tccp + 1;
10064 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10066 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10067 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10068 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10069 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10075 * Dump some elements from the J2K decompression structure .
10077 *@param p_j2k the jpeg2000 codec.
10078 *@param flag flag to describe what elments are dump.
10079 *@param out_stream output stream where dump the elements.
10082 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10084 /* Check if the flag is compatible with j2k file*/
10085 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10086 fprintf(out_stream, "Wrong flag\n");
10090 /* Dump the image_header */
10091 if (flag & OPJ_IMG_INFO){
10092 if (p_j2k->m_private_image)
10093 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10096 /* Dump the codestream info from main header */
10097 if (flag & OPJ_J2K_MH_INFO){
10098 j2k_dump_MH_info(p_j2k, out_stream);
10102 /* Dump the codestream info of the current tile */
10103 if (flag & OPJ_J2K_TH_INFO){
10107 /* Dump the codestream index from main header */
10108 if (flag & OPJ_J2K_MH_IND){
10109 j2k_dump_MH_index(p_j2k, out_stream);
10112 /* Dump the codestream index of the current tile */
10113 if (flag & OPJ_J2K_TH_IND){
10120 * Dump index elements of the codestream extract from the main header.
10122 *@param p_j2k the jpeg2000 codec.
10123 *@param out_stream output stream where dump the elements.
10126 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10128 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10129 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10131 fprintf(out_stream, "Codestream index from main header: {\n");
10133 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10134 "\t Main header end position=%" PRIi64 "\n",
10135 cstr_index->main_head_start, cstr_index->main_head_end);
10137 fprintf(out_stream, "\t Marker list: {\n");
10139 if (cstr_index->marker){
10140 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10141 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10142 cstr_index->marker[it_marker].type,
10143 cstr_index->marker[it_marker].pos,
10144 cstr_index->marker[it_marker].len );
10148 fprintf(out_stream, "\t }\n");
10151 if (cstr_index->tile_index){
10153 /* Simple test to avoid to write empty information*/
10154 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10155 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10156 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10159 if (l_acc_nb_of_tile_part)
10161 fprintf(out_stream, "\t Tile index: {\n");
10163 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10164 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10166 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10168 if (cstr_index->tile_index[it_tile].tp_index){
10169 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10170 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10172 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10173 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10174 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10178 if (cstr_index->tile_index[it_tile].marker){
10179 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10180 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10181 cstr_index->tile_index[it_tile].marker[it_marker].type,
10182 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10183 cstr_index->tile_index[it_tile].marker[it_marker].len );
10187 fprintf(out_stream,"\t }\n");
10191 fprintf(out_stream,"}\n");
10196 * Dump info elements of the codestream extract from the main header.
10198 *@param p_j2k the jpeg2000 codec.
10199 *@param out_stream output stream where dump the elements.
10202 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10204 opj_tcp_v2_t * l_default_tile=NULL;
10206 fprintf(out_stream, "Codestream info from main header: {\n");
10208 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10209 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10210 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10212 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10213 if (l_default_tile)
10216 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10218 fprintf(out_stream, "\t default tile {\n");
10219 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10220 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10221 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10222 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10224 for (compno = 0; compno < numcomps; compno++) {
10225 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10227 OPJ_INT32 bandno, numbands;
10230 fprintf(out_stream, "\t\t comp %d {\n", compno);
10231 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10232 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10233 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10234 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10235 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10236 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10238 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10239 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10240 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10242 fprintf(out_stream, "\n");
10244 /* quantization style*/
10245 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10246 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10247 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10248 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10249 for (bandno = 0; bandno < numbands; bandno++) {
10250 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10251 l_tccp->stepsizes[bandno].expn);
10253 fprintf(out_stream, "\n");
10256 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10258 fprintf(out_stream, "\t\t }\n");
10259 } /*end of component of default tile*/
10260 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10264 fprintf(out_stream, "}\n");
10269 * Dump an image header structure.
10271 *@param img_header the image header to dump.
10272 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10273 *@param out_stream output stream where dump the elements.
10275 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10279 if (dev_dump_flag){
10280 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10284 fprintf(out_stream, "Image info {\n");
10285 tab[0] = '\t';tab[1] = '\0';
10288 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10289 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10290 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10292 if (img_header->comps){
10294 for (compno = 0; compno < img_header->numcomps; compno++) {
10295 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10296 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10297 fprintf(out_stream,"%s}\n",tab);
10301 fprintf(out_stream, "}\n");
10305 * Dump a component image header structure.
10307 *@param comp_header the component image header to dump.
10308 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10309 *@param out_stream output stream where dump the elements.
10311 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10315 if (dev_dump_flag){
10316 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10319 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10322 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10323 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10324 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10327 fprintf(out_stream, "}\n");
10332 * Get the codestream info from a JPEG2000 codec.
10334 *@param p_j2k the component image header to dump.
10336 *@return the codestream information extract from the jpg2000 codec
10338 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10341 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10342 opj_tcp_v2_t *l_default_tile;
10343 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10345 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10347 cstr_info->tx0 = p_j2k->m_cp.tx0;
10348 cstr_info->ty0 = p_j2k->m_cp.ty0;
10349 cstr_info->tdx = p_j2k->m_cp.tdx;
10350 cstr_info->tdy = p_j2k->m_cp.tdy;
10351 cstr_info->tw = p_j2k->m_cp.tw;
10352 cstr_info->th = p_j2k->m_cp.th;
10354 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10356 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10358 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10359 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10360 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10361 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10363 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10365 for (compno = 0; compno < numcomps; compno++) {
10366 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10367 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10368 OPJ_INT32 bandno, numbands;
10371 l_tccp_info->csty = l_tccp->csty;
10372 l_tccp_info->numresolutions = l_tccp->numresolutions;
10373 l_tccp_info->cblkw = l_tccp->cblkw;
10374 l_tccp_info->cblkh = l_tccp->cblkh;
10375 l_tccp_info->cblksty = l_tccp->cblksty;
10376 l_tccp_info->qmfbid = l_tccp->qmfbid;
10377 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10379 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10380 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10383 /* quantization style*/
10384 l_tccp_info->qntsty = l_tccp->qntsty;
10385 l_tccp_info->numgbits = l_tccp->numgbits;
10387 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10388 if (numbands < J2K_MAXBANDS) {
10389 for (bandno = 0; bandno < numbands; bandno++) {
10390 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10391 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10396 l_tccp_info->roishift = l_tccp->roishift;
10404 * Get the codestream index from a JPEG2000 codec.
10406 *@param p_j2k the component image header to dump.
10408 *@return the codestream index extract from the jpg2000 codec
10410 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10412 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10413 opj_calloc(1,sizeof(opj_codestream_index_t));
10417 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10418 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10419 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10421 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10422 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10423 if (!l_cstr_index->marker){
10424 opj_free( l_cstr_index);
10428 if (p_j2k->cstr_index->marker)
10429 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10431 opj_free(l_cstr_index->marker);
10432 l_cstr_index->marker = NULL;
10435 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10436 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10437 if (!l_cstr_index->tile_index){
10438 opj_free( l_cstr_index->marker);
10439 opj_free( l_cstr_index);
10443 if (!p_j2k->cstr_index->tile_index){
10444 opj_free(l_cstr_index->tile_index);
10445 l_cstr_index->tile_index = NULL;
10448 OPJ_UINT32 it_tile = 0;
10449 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10452 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10454 l_cstr_index->tile_index[it_tile].marker =
10455 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10457 if (!l_cstr_index->tile_index[it_tile].marker) {
10458 OPJ_UINT32 it_tile_free;
10460 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10461 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10464 opj_free( l_cstr_index->tile_index);
10465 opj_free( l_cstr_index->marker);
10466 opj_free( l_cstr_index);
10470 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10471 memcpy( l_cstr_index->tile_index[it_tile].marker,
10472 p_j2k->cstr_index->tile_index[it_tile].marker,
10473 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10475 opj_free(l_cstr_index->tile_index[it_tile].marker);
10476 l_cstr_index->tile_index[it_tile].marker = NULL;
10479 /* Tile part index*/
10480 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10482 l_cstr_index->tile_index[it_tile].tp_index =
10483 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10485 if(!l_cstr_index->tile_index[it_tile].tp_index){
10486 OPJ_UINT32 it_tile_free;
10488 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10489 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10490 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10493 opj_free( l_cstr_index->tile_index);
10494 opj_free( l_cstr_index->marker);
10495 opj_free( l_cstr_index);
10499 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10500 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10501 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10502 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10505 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10506 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10509 /* Packet index (NOT USED)*/
10510 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10511 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10516 return l_cstr_index;
10519 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10521 OPJ_UINT32 it_tile=0;
10523 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10524 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10525 if (!p_j2k->cstr_index->tile_index)
10528 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10529 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10530 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10531 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10532 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10533 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10543 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10544 opj_stream_private_t *p_stream,
10545 opj_event_mgr_t * p_manager)
10547 opj_bool l_go_on = OPJ_TRUE;
10548 OPJ_UINT32 l_current_tile_no;
10549 OPJ_UINT32 l_data_size,l_max_data_size;
10550 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10551 OPJ_UINT32 l_nb_comps;
10552 OPJ_BYTE * l_current_data;
10554 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10555 if (! l_current_data) {
10558 l_max_data_size = 1000;
10563 if (! j2k_read_tile_header( p_j2k,
10564 &l_current_tile_no,
10566 &l_tile_x0, &l_tile_y0,
10567 &l_tile_x1, &l_tile_y1,
10572 opj_free(l_current_data);
10580 if (l_data_size > l_max_data_size) {
10581 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10582 if (! l_current_data) {
10583 opj_free(l_current_data);
10587 l_max_data_size = l_data_size;
10590 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10591 opj_free(l_current_data);
10594 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);
10596 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10597 opj_free(l_current_data);
10600 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10604 opj_free(l_current_data);
10610 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10612 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10615 assert(p_j2k != 00);
10617 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10618 /* DEVELOPER CORNER, add your custom procedures */
10623 * Read and decode one tile.
10625 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10626 opj_stream_private_t *p_stream,
10627 opj_event_mgr_t * p_manager)
10629 opj_bool l_go_on = OPJ_TRUE;
10630 OPJ_UINT32 l_current_tile_no;
10631 OPJ_UINT32 l_tile_no_to_dec;
10632 OPJ_UINT32 l_data_size,l_max_data_size;
10633 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10634 OPJ_UINT32 l_nb_comps;
10635 OPJ_BYTE * l_current_data;
10637 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10638 if (! l_current_data) {
10641 l_max_data_size = 1000;
10643 /*Allocate and initialize some elements of codestrem index if not already done*/
10644 if( !p_j2k->cstr_index->tile_index)
10646 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10647 opj_free(l_current_data);
10651 /* Move into the codestream to the first SOT used to decode the desired tile */
10652 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10653 if (p_j2k->cstr_index->tile_index)
10654 if(p_j2k->cstr_index->tile_index->tp_index)
10656 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10657 /* the index for this tile has not been built,
10658 * so move to the last SOT read */
10659 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10660 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10665 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)) {
10666 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10670 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10671 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10672 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10676 if (! j2k_read_tile_header( p_j2k,
10677 &l_current_tile_no,
10679 &l_tile_x0, &l_tile_y0,
10680 &l_tile_x1, &l_tile_y1,
10685 opj_free(l_current_data);
10694 if (l_data_size > l_max_data_size) {
10695 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10696 if (! l_current_data) {
10697 opj_free(l_current_data);
10701 l_max_data_size = l_data_size;
10706 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10707 opj_free(l_current_data);
10710 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);
10712 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10713 opj_free(l_current_data);
10716 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10718 if(l_current_tile_no == l_tile_no_to_dec)
10720 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10721 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10722 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10728 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);
10733 opj_free(l_current_data);
10740 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10742 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10745 assert(p_j2k != 00);
10747 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10748 /* DEVELOPER CORNER, add your custom procedures */
10754 * Decodes the tiles of the stream.
10756 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10757 opj_stream_private_t * p_stream,
10758 opj_image_t * p_image,
10759 opj_event_mgr_t * p_manager)
10766 p_j2k->m_output_image = opj_image_create0();
10767 if (! (p_j2k->m_output_image)) {
10770 opj_copy_image_header(p_image, p_j2k->m_output_image);
10772 /* customization of the decoding */
10773 j2k_setup_decoding(p_j2k);
10775 /* Decode the codestream */
10776 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10777 opj_image_destroy(p_j2k->m_private_image);
10778 p_j2k->m_private_image = NULL;
10782 /* Move data and copy one information from codec to output image*/
10783 for (compno = 0; compno < p_image->numcomps; compno++) {
10784 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10785 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10786 p_j2k->m_output_image->comps[compno].data = NULL;
10794 * Get the decoded tile.
10796 * @param p_j2k the jpeg2000 codestream codec.
10797 * @param p_stream input_stream
10798 * @param p_image output image. .
10799 * @param p_manager the user event manager
10800 * @param tile_index index of the tile we want decode
10802 * @return true if succeed.
10804 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10805 opj_stream_private_t *p_stream,
10806 opj_image_t* p_image,
10807 struct opj_event_mgr * p_manager,
10808 OPJ_UINT32 tile_index )
10811 OPJ_UINT32 l_tile_x, l_tile_y;
10812 opj_image_comp_t* l_img_comp;
10815 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10819 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10820 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);
10824 /* Compute the dimension of the desired tile*/
10825 l_tile_x = tile_index % p_j2k->m_cp.tw;
10826 l_tile_y = tile_index / p_j2k->m_cp.tw;
10828 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10829 if (p_image->x0 < p_j2k->m_private_image->x0)
10830 p_image->x0 = p_j2k->m_private_image->x0;
10831 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10832 if (p_image->x1 > p_j2k->m_private_image->x1)
10833 p_image->x1 = p_j2k->m_private_image->x1;
10835 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10836 if (p_image->y0 < p_j2k->m_private_image->y0)
10837 p_image->y0 = p_j2k->m_private_image->y0;
10838 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10839 if (p_image->y1 > p_j2k->m_private_image->y1)
10840 p_image->y1 = p_j2k->m_private_image->y1;
10842 l_img_comp = p_image->comps;
10843 for (compno=0; compno < p_image->numcomps; ++compno)
10845 OPJ_INT32 l_comp_x1, l_comp_y1;
10847 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10849 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10850 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10851 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10852 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10854 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10855 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10860 /* Destroy the previous output image*/
10861 if (p_j2k->m_output_image)
10862 opj_image_destroy(p_j2k->m_output_image);
10864 /* Create the ouput image from the information previously computed*/
10865 p_j2k->m_output_image = opj_image_create0();
10866 if (! (p_j2k->m_output_image)) {
10869 opj_copy_image_header(p_image, p_j2k->m_output_image);
10871 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10873 /* customization of the decoding */
10874 j2k_setup_decoding_tile(p_j2k);
10876 /* Decode the codestream */
10877 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10878 opj_image_destroy(p_j2k->m_private_image);
10879 p_j2k->m_private_image = NULL;
10883 /* Move data and copy one information from codec to output image*/
10884 for (compno = 0; compno < p_image->numcomps; compno++) {
10885 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10887 if (p_image->comps[compno].data)
10888 opj_free(p_image->comps[compno].data);
10890 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10892 p_j2k->m_output_image->comps[compno].data = NULL;
10898 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10900 OPJ_UINT32 it_comp;
10902 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10904 if (p_j2k->m_private_image) {
10905 if (p_j2k->m_private_image->comps) {
10906 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10907 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10908 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10909 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10910 if ( res_factor >= max_res){
10911 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10914 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10927 * Encodes all the tiles in a row.
10929 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10930 opj_stream_private_t *p_stream,
10931 opj_event_mgr_t * p_manager )
10934 OPJ_UINT32 l_nb_tiles;
10935 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10936 OPJ_BYTE * l_current_data;
10938 /* preconditions */
10939 assert(p_j2k != 00);
10940 assert(p_stream != 00);
10941 assert(p_manager != 00);
10943 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10944 if (! l_current_data) {
10947 l_max_tile_size = 1000;
10949 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10950 for (i=0;i<l_nb_tiles;++i) {
10951 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10952 opj_free(l_current_data);
10956 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10957 if (l_current_tile_size > l_max_tile_size) {
10958 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10959 if (! l_current_data) {
10962 l_max_tile_size = l_current_tile_size;
10965 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10967 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10972 opj_free(l_current_data);
10977 * Ends the compression procedures and possibility add data to be read after the
10980 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
10981 opj_stream_private_t *p_stream,
10982 struct opj_event_mgr * p_manager)
10984 /* customization of the encoding */
10985 j2k_setup_end_compress(p_j2k);
10987 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10997 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10999 * @param p_j2k the jpeg2000 codec.
11000 * @param p_stream the stream object.
11001 * @param p_manager the user event manager.
11003 * @return true if the codec is valid.
11005 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11006 opj_stream_private_t *p_stream,
11007 opj_image_t * p_image,
11008 opj_event_mgr_t * p_manager)
11010 /* preconditions */
11011 assert(p_j2k != 00);
11012 assert(p_stream != 00);
11013 assert(p_manager != 00);
11015 p_j2k->m_private_image = opj_image_create0();
11016 opj_copy_image_header(p_image, p_j2k->m_private_image);
11018 // TODO_MSD: Find a better way
11019 if (p_image->comps) {
11020 OPJ_UINT32 it_comp;
11021 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11022 if (p_image->comps[it_comp].data) {
11023 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11024 p_image->comps[it_comp].data = NULL;
11030 /* customization of the validation */
11031 j2k_setup_encoding_validation (p_j2k);
11033 /* validation of the parameters codec */
11034 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11038 /* customization of the encoding */
11039 j2k_setup_header_writting(p_j2k);
11042 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11052 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11053 OPJ_UINT32 p_tile_index,
11054 opj_stream_private_t *p_stream,
11055 opj_event_mgr_t * p_manager )
11058 if (p_tile_index != p_j2k->m_current_tile_number) {
11059 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11063 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);
11065 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11066 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11067 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11069 /* initialisation before tile encoding */
11070 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11080 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11082 OPJ_UINT32 i,j,k = 0;
11083 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11084 opj_image_comp_t * l_img_comp = 00;
11085 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11086 opj_image_t * l_image = 00;
11087 OPJ_UINT32 l_size_comp, l_remaining;
11088 OPJ_INT32 * l_src_ptr;
11089 l_tilec = p_tcd->tcd_image->tiles->comps;
11090 l_image = p_tcd->image;
11091 l_img_comp = l_image->comps;
11093 for (i=0;i<p_tcd->image->numcomps;++i) {
11094 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11095 l_remaining = l_img_comp->prec & 7; /* (%8) */
11100 if (l_size_comp == 3) {
11104 l_width = (l_tilec->x1 - l_tilec->x0);
11105 l_height = (l_tilec->y1 - l_tilec->y0);
11106 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11107 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11108 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11109 l_stride = l_image_width - l_width;
11110 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11112 switch (l_size_comp) {
11115 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11116 if (l_img_comp->sgnd) {
11117 for (j=0;j<l_height;++j) {
11118 for (k=0;k<l_width;++k) {
11119 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11123 l_src_ptr += l_stride;
11127 for (j=0;j<l_height;++j) {
11128 for (k=0;k<l_width;++k) {
11129 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11133 l_src_ptr += l_stride;
11137 p_data = (OPJ_BYTE*) l_dest_ptr;
11142 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11143 if (l_img_comp->sgnd) {
11144 for (j=0;j<l_height;++j) {
11145 for (k=0;k<l_width;++k) {
11146 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11148 l_src_ptr += l_stride;
11152 for (j=0;j<l_height;++j) {
11153 for (k=0;k<l_width;++k) {
11154 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11156 l_src_ptr += l_stride;
11160 p_data = (OPJ_BYTE*) l_dest_ptr;
11165 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11166 for (j=0;j<l_height;++j) {
11167 for (k=0;k<l_width;++k) {
11168 *(l_dest_ptr++) = *(l_src_ptr++);
11170 l_src_ptr += l_stride;
11173 p_data = (OPJ_BYTE*) l_dest_ptr;
11186 * @param p_j2k the jpeg2000 codec.
11187 * @param p_stream the stream to write data to.
11188 * @param p_manager the user event manager.
11190 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11192 OPJ_UINT32 p_data_size,
11193 opj_stream_private_t *p_stream,
11194 opj_event_mgr_t * p_manager )
11196 opj_tcd_v2_t * l_tcd = 00;
11197 opj_cp_v2_t * l_cp = 00;
11198 opj_tcp_v2_t * l_tcp = 00;
11199 OPJ_UINT32 l_nb_bytes_written;
11200 OPJ_BYTE * l_current_data = 00;
11201 OPJ_UINT32 l_tile_size = 0;
11202 OPJ_UINT32 l_available_data;
11204 /* preconditions */
11205 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11207 l_tcd = p_j2k->m_tcd;
11208 l_cp = &(p_j2k->m_cp);
11209 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11211 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11212 l_available_data = l_tile_size;
11213 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11215 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11216 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11220 l_nb_bytes_written = 0;
11221 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11224 l_current_data += l_nb_bytes_written;
11225 l_available_data -= l_nb_bytes_written;
11227 l_nb_bytes_written = 0;
11228 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11232 l_available_data -= l_nb_bytes_written;
11233 l_nb_bytes_written = l_tile_size - l_available_data;
11235 if ( opj_stream_write_data( p_stream,
11236 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11237 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11241 ++p_j2k->m_current_tile_number;
11248 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11249 * are valid. Developers wanting to extend the library can add their own validation procedures.
11251 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11253 /* preconditions */
11254 assert(p_j2k != 00);
11256 /* DEVELOPER CORNER, insert your custom procedures */
11257 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11259 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11260 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11263 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11264 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11265 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11269 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11270 * are valid. Developers wanting to extend the library can add their own validation procedures.
11272 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11274 /* preconditions */
11275 assert(p_j2k != 00);
11277 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11278 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11280 /* DEVELOPER CORNER, add your custom validation procedure */
11281 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11286 * Sets up the procedures to do on writing header.
11287 * Developers wanting to extend the library can add their own writing procedures.
11289 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11291 /* preconditions */
11292 assert(p_j2k != 00);
11294 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11295 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11296 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11297 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11298 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11301 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11302 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11303 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11305 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11306 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11310 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11312 if (p_j2k->m_cp.comment != 00) {
11313 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11316 /* DEVELOPER CORNER, insert your custom procedures */
11317 if (p_j2k->m_cp.rsiz & MCT) {
11318 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11320 /* End of Developer Corner */
11322 if (p_j2k->cstr_index) {
11323 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11326 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11327 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11331 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11333 OPJ_UINT32 * p_data_written,
11334 OPJ_UINT32 p_total_data_size,
11335 opj_stream_private_t *p_stream,
11336 struct opj_event_mgr * p_manager )
11339 OPJ_UINT32 l_nb_bytes_written = 0;
11340 OPJ_UINT32 l_current_nb_bytes_written;
11341 OPJ_BYTE * l_begin_data = 00;
11343 opj_tcp_v2_t *l_tcp = 00;
11344 opj_tcd_v2_t * l_tcd = 00;
11345 opj_cp_v2_t * l_cp = 00;
11347 l_tcd = p_j2k->m_tcd;
11348 l_cp = &(p_j2k->m_cp);
11349 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11351 l_tcd->cur_pino = 0;
11353 /*Get number of tile parts*/
11354 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11359 l_current_nb_bytes_written = 0;
11360 l_begin_data = p_data;
11361 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11366 l_nb_bytes_written += l_current_nb_bytes_written;
11367 p_data += l_current_nb_bytes_written;
11368 p_total_data_size -= l_current_nb_bytes_written;
11370 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11371 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11372 l_current_nb_bytes_written = 0;
11373 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11374 l_nb_bytes_written += l_current_nb_bytes_written;
11375 p_data += l_current_nb_bytes_written;
11376 p_total_data_size -= l_current_nb_bytes_written;
11378 l_current_nb_bytes_written = 0;
11379 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11380 l_nb_bytes_written += l_current_nb_bytes_written;
11381 p_data += l_current_nb_bytes_written;
11382 p_total_data_size -= l_current_nb_bytes_written;
11385 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11386 l_current_nb_bytes_written = 0;
11387 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11388 l_nb_bytes_written += l_current_nb_bytes_written;
11389 p_data += l_current_nb_bytes_written;
11390 p_total_data_size -= l_current_nb_bytes_written;
11394 l_current_nb_bytes_written = 0;
11395 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11399 l_nb_bytes_written += l_current_nb_bytes_written;
11400 * p_data_written = l_nb_bytes_written;
11402 /* Writing Psot in SOT marker */
11403 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11405 if (l_cp->m_specific_param.m_enc.m_cinema){
11406 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11412 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11414 OPJ_UINT32 * p_data_written,
11415 OPJ_UINT32 p_total_data_size,
11416 opj_stream_private_t *p_stream,
11417 struct opj_event_mgr * p_manager
11420 OPJ_UINT32 tilepartno=0;
11421 OPJ_UINT32 l_nb_bytes_written = 0;
11422 OPJ_UINT32 l_current_nb_bytes_written;
11423 OPJ_UINT32 l_part_tile_size;
11424 OPJ_UINT32 tot_num_tp;
11427 OPJ_BYTE * l_begin_data;
11428 opj_tcp_v2_t *l_tcp = 00;
11429 opj_tcd_v2_t * l_tcd = 00;
11430 opj_cp_v2_t * l_cp = 00;
11433 l_tcd = p_j2k->m_tcd;
11434 l_cp = &(p_j2k->m_cp);
11435 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11437 /*Get number of tile parts*/
11438 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11440 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11441 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11442 l_current_nb_bytes_written = 0;
11443 l_part_tile_size = 0;
11444 l_begin_data = p_data;
11446 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11450 l_nb_bytes_written += l_current_nb_bytes_written;
11451 p_data += l_current_nb_bytes_written;
11452 p_total_data_size -= l_current_nb_bytes_written;
11453 l_part_tile_size += l_nb_bytes_written;
11455 l_current_nb_bytes_written = 0;
11456 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11460 p_data += l_current_nb_bytes_written;
11461 l_nb_bytes_written += l_current_nb_bytes_written;
11462 p_total_data_size -= l_current_nb_bytes_written;
11463 l_part_tile_size += l_nb_bytes_written;
11465 /* Writing Psot in SOT marker */
11466 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11468 if (l_cp->m_specific_param.m_enc.m_cinema) {
11469 j2k_update_tlm(p_j2k,l_part_tile_size);
11472 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11475 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11476 l_tcd->cur_pino = pino;
11478 /*Get number of tile parts*/
11479 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11480 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11481 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11482 l_current_nb_bytes_written = 0;
11483 l_part_tile_size = 0;
11484 l_begin_data = p_data;
11486 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11490 l_nb_bytes_written += l_current_nb_bytes_written;
11491 p_data += l_current_nb_bytes_written;
11492 p_total_data_size -= l_current_nb_bytes_written;
11493 l_part_tile_size += l_current_nb_bytes_written;
11495 l_current_nb_bytes_written = 0;
11497 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11501 l_nb_bytes_written += l_current_nb_bytes_written;
11502 p_data += l_current_nb_bytes_written;
11503 p_total_data_size -= l_current_nb_bytes_written;
11504 l_part_tile_size += l_current_nb_bytes_written;
11506 /* Writing Psot in SOT marker */
11507 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11509 if (l_cp->m_specific_param.m_enc.m_cinema) {
11510 j2k_update_tlm(p_j2k,l_part_tile_size);
11513 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11517 *p_data_written = l_nb_bytes_written;
11523 * Writes the updated tlm.
11525 * @param p_stream the stream to write data to.
11526 * @param p_j2k J2K codec.
11527 * @param p_manager the user event manager.
11529 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11530 struct opj_stream_private *p_stream,
11531 struct opj_event_mgr * p_manager )
11533 OPJ_UINT32 l_tlm_size;
11534 OPJ_SIZE_T l_tlm_position, l_current_position;
11536 /* preconditions */
11537 assert(p_j2k != 00);
11538 assert(p_manager != 00);
11539 assert(p_stream != 00);
11541 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11542 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11543 l_current_position = opj_stream_tell(p_stream);
11545 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11549 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) {
11553 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11562 * Ends the encoding, i.e. frees memory.
11564 * @param p_stream the stream to write data to.
11565 * @param p_j2k J2K codec.
11566 * @param p_manager the user event manager.
11568 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11569 struct opj_stream_private *p_stream,
11570 struct opj_event_mgr * p_manager )
11572 /* preconditions */
11573 assert(p_j2k != 00);
11574 assert(p_manager != 00);
11575 assert(p_stream != 00);
11577 tcd_destroy_v2(p_j2k->m_tcd);
11580 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11581 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11582 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11583 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11586 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11587 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11588 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11591 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11597 * Destroys the memory associated with the decoding of headers.
11599 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11600 opj_stream_private_t *p_stream,
11601 opj_event_mgr_t * p_manager )
11603 /* preconditions */
11604 assert(p_j2k != 00);
11605 assert(p_stream != 00);
11606 assert(p_manager != 00);
11608 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11609 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11610 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11613 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11622 * @param p_stream the stream to write data to.
11623 * @param p_j2k J2K codec.
11624 * @param p_manager the user event manager.
11626 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11627 struct opj_stream_private *p_stream,
11628 struct opj_event_mgr * p_manager )
11630 opj_codestream_info_t * l_cstr_info = 00;
11632 /* preconditions */
11633 assert(p_j2k != 00);
11634 assert(p_manager != 00);
11635 assert(p_stream != 00);
11638 /* TODO mergeV2: check this part which use cstr_info */
11639 /*l_cstr_info = p_j2k->cstr_info;
11643 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));
11645 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11646 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11648 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11650 l_cstr_info->tw = p_j2k->m_cp.tw;
11651 l_cstr_info->th = p_j2k->m_cp.th;
11653 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11654 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11655 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11656 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11658 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11660 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11662 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11664 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11665 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11668 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11670 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11672 /*l_cstr_info->maxmarknum = 100;
11673 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11674 l_cstr_info->marknum = 0;
11677 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);
11681 * Creates a tile-coder decoder.
11683 * @param p_stream the stream to write data to.
11684 * @param p_j2k J2K codec.
11685 * @param p_manager the user event manager.
11687 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11688 struct opj_stream_private *p_stream,
11689 struct opj_event_mgr * p_manager )
11691 /* preconditions */
11692 assert(p_j2k != 00);
11693 assert(p_manager != 00);
11694 assert(p_stream != 00);
11696 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11698 if (! p_j2k->m_tcd) {
11699 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11703 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11704 tcd_destroy_v2(p_j2k->m_tcd);
11715 * @param p_j2k the jpeg2000 codec.
11716 * @param p_stream the stream to write data to.
11717 * @param p_manager the user event manager.
11719 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11720 OPJ_UINT32 p_tile_index,
11722 OPJ_UINT32 p_data_size,
11723 opj_stream_private_t *p_stream,
11724 opj_event_mgr_t * p_manager )
11726 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11727 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11731 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11732 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);