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 Read the CRG marker (component registration)
713 @param j2k J2K handle
715 static void j2k_read_crg(opj_j2k_t *j2k);
717 * Reads a CRG marker (Component registration)
719 * @param p_header_data the data contained in the TLM box.
720 * @param p_j2k the jpeg2000 codec.
721 * @param p_header_size the size of the data contained in the TLM marker.
722 * @param p_manager the user event manager.
724 static opj_bool j2k_read_crg_v2 (
726 OPJ_BYTE * p_header_data,
727 OPJ_UINT32 p_header_size,
728 struct opj_event_mgr * p_manager
731 * Reads a TLM marker (Tile Length Marker)
733 * @param p_header_data the data contained in the TLM box.
734 * @param p_j2k the jpeg2000 codec.
735 * @param p_header_size the size of the data contained in the TLM marker.
736 * @param p_manager the user event manager.
738 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
739 OPJ_BYTE * p_header_data,
740 OPJ_UINT32 p_header_size,
741 opj_event_mgr_t * p_manager);
744 * Writes the updated tlm.
746 * @param p_stream the stream to write data to.
747 * @param p_j2k J2K codec.
748 * @param p_manager the user event manager.
750 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
751 struct opj_stream_private *p_stream,
752 struct opj_event_mgr * p_manager );
755 * Reads a PLM marker (Packet length, main header marker)
757 * @param p_header_data the data contained in the TLM box.
758 * @param p_j2k the jpeg2000 codec.
759 * @param p_header_size the size of the data contained in the TLM marker.
760 * @param p_manager the user event manager.
762 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
763 OPJ_BYTE * p_header_data,
764 OPJ_UINT32 p_header_size,
765 opj_event_mgr_t * p_manager);
767 * Reads a PLT marker (Packet length, tile-part header)
769 * @param p_header_data the data contained in the PLT box.
770 * @param p_j2k the jpeg2000 codec.
771 * @param p_header_size the size of the data contained in the PLT marker.
772 * @param p_manager the user event manager.
774 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
775 OPJ_BYTE * p_header_data,
776 OPJ_UINT32 p_header_size,
777 opj_event_mgr_t * p_manager );
781 * Reads a PPM marker (Packed packet headers, main header)
783 * @param p_header_data the data contained in the POC box.
784 * @param p_j2k the jpeg2000 codec.
785 * @param p_header_size the size of the data contained in the POC marker.
786 * @param p_manager the user event manager.
789 static opj_bool j2k_read_ppm_v2 (
791 OPJ_BYTE * p_header_data,
792 OPJ_UINT32 p_header_size,
793 struct opj_event_mgr * p_manager
797 static opj_bool j2k_read_ppm_v3 (
799 OPJ_BYTE * p_header_data,
800 OPJ_UINT32 p_header_size,
801 struct opj_event_mgr * p_manager
806 * Reads a PPT marker (Packed packet headers, tile-part header)
808 * @param p_header_data the data contained in the PPT box.
809 * @param p_j2k the jpeg2000 codec.
810 * @param p_header_size the size of the data contained in the PPT marker.
811 * @param p_manager the user event manager.
813 static opj_bool j2k_read_ppt_v2 (
815 OPJ_BYTE * p_header_data,
816 OPJ_UINT32 p_header_size,
817 struct opj_event_mgr * p_manager
820 Write the TLM marker (Mainheader)
821 @param j2k J2K handle
823 static void j2k_write_tlm(opj_j2k_t *j2k);
826 * Writes the TLM marker (Tile Length Marker)
828 * @param p_stream the stream to write data to.
829 * @param p_j2k J2K codec.
830 * @param p_manager the user event manager.
832 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
833 struct opj_stream_private *p_stream,
834 struct opj_event_mgr * p_manager );
837 Write the SOT marker (start of tile-part)
838 @param j2k J2K handle
840 static void j2k_write_sot(opj_j2k_t *j2k);
843 * Writes the SOT marker (Start of tile-part)
845 * @param p_stream the stream to write data to.
846 * @param p_j2k J2K codec.
847 * @param p_manager the user event manager.
849 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
851 OPJ_UINT32 * p_data_written,
852 const struct opj_stream_private *p_stream,
853 struct opj_event_mgr * p_manager );
856 * Reads a PPT marker (Packed packet headers, tile-part header)
858 * @param p_header_data the data contained in the PPT box.
859 * @param p_j2k the jpeg2000 codec.
860 * @param p_header_size the size of the data contained in the PPT marker.
861 * @param p_manager the user event manager.
863 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
864 OPJ_BYTE * p_header_data,
865 OPJ_UINT32 p_header_size,
866 opj_event_mgr_t * p_manager );
869 Write the SOD marker (start of data)
870 @param j2k J2K handle
871 @param tile_coder Pointer to a TCD handle
873 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
876 * Writes the SOD marker (Start of data)
878 * @param p_stream the stream to write data to.
879 * @param p_j2k J2K codec.
880 * @param p_manager the user event manager.
882 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
883 struct opj_tcd_v2 * p_tile_coder,
885 OPJ_UINT32 * p_data_written,
886 OPJ_UINT32 p_total_data_size,
887 const struct opj_stream_private *p_stream,
888 struct opj_event_mgr * p_manager );
891 * Reads a SOD marker (Start Of Data)
893 * @param p_header_data the data contained in the SOD box.
894 * @param p_j2k the jpeg2000 codec.
895 * @param p_header_size the size of the data contained in the SOD marker.
896 * @param p_manager the user event manager.
898 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
899 opj_stream_private_t *p_stream,
900 opj_event_mgr_t * p_manager );
903 * Updates the Tile Length Marker.
905 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
907 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
908 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
910 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
911 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
915 Write the RGN marker (region-of-interest)
916 @param j2k J2K handle
917 @param compno Number of the component concerned by the information written
918 @param tileno Number of the tile concerned by the information written
920 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
923 * Writes the RGN marker (Region Of Interest)
925 * @param p_tile_no the tile to output
926 * @param p_comp_no the component to output
927 * @param p_stream the stream to write data to.
928 * @param p_j2k J2K codec.
929 * @param p_manager the user event manager.
931 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
932 OPJ_UINT32 p_tile_no,
933 OPJ_UINT32 p_comp_no,
934 struct opj_stream_private *p_stream,
935 struct opj_event_mgr * p_manager );
938 * Reads a RGN marker (Region Of Interest)
940 * @param p_header_data the data contained in the POC box.
941 * @param p_j2k the jpeg2000 codec.
942 * @param p_header_size the size of the data contained in the POC marker.
943 * @param p_manager the user event manager.
945 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
946 OPJ_BYTE * p_header_data,
947 OPJ_UINT32 p_header_size,
948 opj_event_mgr_t * p_manager );
951 * Writes the EOC marker (End of Codestream)
953 * @param p_stream the stream to write data to.
954 * @param p_j2k J2K codec.
955 * @param p_manager the user event manager.
957 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
958 struct opj_stream_private *p_stream,
959 struct opj_event_mgr * p_manager );
962 Write the EOC marker (end of codestream)
963 @param j2k J2K handle
965 static void j2k_write_eoc(opj_j2k_t *j2k);
967 Read the EOC marker (end of codestream)
968 @param j2k J2K handle
970 static void j2k_read_eoc(opj_j2k_t *j2k);
973 * Reads a EOC marker (End Of Codestream)
975 * @param p_header_data the data contained in the SOD box.
976 * @param p_j2k the jpeg2000 codec.
977 * @param p_header_size the size of the data contained in the SOD marker.
978 * @param p_manager the user event manager.
981 static opj_bool j2k_read_eoc_v2 (
983 struct opj_stream_private *p_stream,
984 struct opj_event_mgr * p_manager
991 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
993 * @param p_stream the stream to write data to.
994 * @param p_j2k J2K codec.
995 * @param p_manager the user event manager.
997 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
998 struct opj_stream_private *p_stream,
999 struct opj_event_mgr * p_manager );
1004 * @param p_stream the stream to write data to.
1005 * @param p_j2k J2K codec.
1006 * @param p_manager the user event manager.
1008 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1009 struct opj_stream_private *p_stream,
1010 struct opj_event_mgr * p_manager );
1013 Read an unknown marker
1014 @param j2k J2K handle
1016 static void j2k_read_unk(opj_j2k_t *j2k);
1018 Add main header marker information
1019 @param cstr_info Codestream information structure
1020 @param type marker type
1021 @param pos byte offset of marker segment
1022 @param len length of marker segment
1024 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1026 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1028 Add tile header marker information
1029 @param tileno tile index number
1030 @param cstr_info Codestream information structure
1031 @param type marker type
1032 @param pos byte offset of marker segment
1033 @param len length of marker segment
1035 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1037 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);
1040 * Reads an unknown marker
1042 * @param p_stream the stream object to read from.
1043 * @param p_j2k the jpeg2000 codec.
1044 * @param p_manager the user event manager.
1046 * @return true if the marker could be deduced.
1048 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1049 struct opj_stream_private *p_stream,
1050 OPJ_UINT32 *output_marker,
1051 struct opj_event_mgr * p_manager );
1054 * Writes the MCT marker (Multiple Component Transform)
1056 * @param p_stream the stream to write data to.
1057 * @param p_j2k J2K codec.
1058 * @param p_manager the user event manager.
1060 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1061 opj_mct_data_t * p_mct_record,
1062 struct opj_stream_private *p_stream,
1063 struct opj_event_mgr * p_manager );
1066 * Reads a MCT marker (Multiple Component Transform)
1068 * @param p_header_data the data contained in the MCT box.
1069 * @param p_j2k the jpeg2000 codec.
1070 * @param p_header_size the size of the data contained in the MCT marker.
1071 * @param p_manager the user event manager.
1073 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1074 OPJ_BYTE * p_header_data,
1075 OPJ_UINT32 p_header_size,
1076 struct opj_event_mgr * p_manager );
1079 * Writes the MCC marker (Multiple Component Collection)
1081 * @param p_stream the stream to write data to.
1082 * @param p_j2k J2K codec.
1083 * @param p_manager the user event manager.
1085 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1086 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1087 struct opj_stream_private *p_stream,
1088 struct opj_event_mgr * p_manager );
1092 * Reads a MCC marker (Multiple Component Collection)
1094 * @param p_header_data the data contained in the MCC box.
1095 * @param p_j2k the jpeg2000 codec.
1096 * @param p_header_size the size of the data contained in the MCC marker.
1097 * @param p_manager the user event manager.
1099 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1100 OPJ_BYTE * p_header_data,
1101 OPJ_UINT32 p_header_size,
1102 struct opj_event_mgr * p_manager );
1105 * Writes the MCO marker (Multiple component transformation ordering)
1107 * @param p_stream the stream to write data to.
1108 * @param p_j2k J2K codec.
1109 * @param p_manager the user event manager.
1111 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1112 struct opj_stream_private *p_stream,
1113 struct opj_event_mgr * p_manager );
1116 * Reads a MCO marker (Multiple Component Transform Ordering)
1118 * @param p_header_data the data contained in the MCO box.
1119 * @param p_j2k the jpeg2000 codec.
1120 * @param p_header_size the size of the data contained in the MCO marker.
1121 * @param p_manager the user event manager.
1123 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1124 OPJ_BYTE * p_header_data,
1125 OPJ_UINT32 p_header_size,
1126 struct opj_event_mgr * p_manager );
1128 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1130 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1131 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1132 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1133 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1135 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1136 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1137 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1138 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1140 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1141 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1142 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1143 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1147 * Ends the encoding, i.e. frees memory.
1149 * @param p_stream the stream to write data to.
1150 * @param p_j2k J2K codec.
1151 * @param p_manager the user event manager.
1153 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1154 struct opj_stream_private *p_stream,
1155 struct opj_event_mgr * p_manager );
1158 * Writes the CBD marker (Component bit depth definition)
1160 * @param p_stream the stream to write data to.
1161 * @param p_j2k J2K codec.
1162 * @param p_manager the user event manager.
1164 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1165 struct opj_stream_private *p_stream,
1166 struct opj_event_mgr * p_manager );
1169 * Reads a CBD marker (Component bit depth definition)
1170 * @param p_header_data the data contained in the CBD box.
1171 * @param p_j2k the jpeg2000 codec.
1172 * @param p_header_size the size of the data contained in the CBD marker.
1173 * @param p_manager the user event manager.
1175 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1176 OPJ_BYTE * p_header_data,
1177 OPJ_UINT32 p_header_size,
1178 struct opj_event_mgr * p_manager);
1181 * Writes the image components.
1183 * @param p_stream the stream to write data to.
1184 * @param p_j2k J2K codec.
1185 * @param p_manager the user event manager.
1187 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1188 struct opj_stream_private *p_stream,
1189 struct opj_event_mgr * p_manager );
1192 * Writes regions of interests.
1194 * @param p_stream the stream to write data to.
1195 * @param p_j2k J2K codec.
1196 * @param p_manager the user event manager.
1198 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1199 struct opj_stream_private *p_stream,
1200 struct opj_event_mgr * p_manager );
1205 * @param p_stream the stream to write data to.
1206 * @param p_j2k J2K codec.
1207 * @param p_manager the user event manager.
1209 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1210 struct opj_stream_private *p_stream,
1211 struct opj_event_mgr * p_manager );
1214 * Checks the progression order changes values. Tells of the poc given as input are valid.
1215 * A nice message is outputted at errors.
1217 * @param p_pocs the progression order changes.
1218 * @param p_nb_pocs the number of progression order changes.
1219 * @param p_nb_resolutions the number of resolutions.
1220 * @param numcomps the number of components
1221 * @param numlayers the number of layers.
1223 * @return true if the pocs are valid.
1225 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1226 OPJ_UINT32 p_nb_pocs,
1227 OPJ_UINT32 p_nb_resolutions,
1228 OPJ_UINT32 numcomps,
1229 OPJ_UINT32 numlayers,
1230 opj_event_mgr_t * p_manager);
1233 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1235 * @param cp the coding parameters.
1236 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1237 * @param tileno the given tile.
1239 * @return the number of tile parts.
1241 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1243 /** mem allocation for TLM marker*/
1244 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1247 * Calculates the total number of tile parts needed by the encoder to
1248 * encode such an image. If not enough memory is available, then the function return false.
1250 * @param p_nb_tiles pointer that will hold the number of tile parts.
1251 * @param cp the coding parameters for the image.
1252 * @param image the image to encode.
1253 * @param p_j2k the p_j2k encoder.
1254 * @param p_manager the user event manager.
1256 * @return true if the function was successful, false else.
1258 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1260 OPJ_UINT32 * p_nb_tiles,
1262 opj_event_mgr_t * p_manager);
1264 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1266 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1268 static opj_codestream_index_t* j2k_create_cstr_index(void);
1274 /* ----------------------------------------------------------------------- */
1275 typedef struct j2k_prog_order{
1276 OPJ_PROG_ORDER enum_prog;
1280 j2k_prog_order_t j2k_prog_order_list[] = {
1286 {(OPJ_PROG_ORDER)-1, ""}
1294 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1302 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1304 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1306 j2k_read_int16_to_float,
1307 j2k_read_int32_to_float,
1308 j2k_read_float32_to_float,
1309 j2k_read_float64_to_float
1312 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1314 j2k_read_int16_to_int32,
1315 j2k_read_int32_to_int32,
1316 j2k_read_float32_to_int32,
1317 j2k_read_float64_to_int32
1320 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1322 j2k_write_float_to_int16,
1323 j2k_write_float_to_int32,
1324 j2k_write_float_to_float,
1325 j2k_write_float_to_float64
1328 typedef struct opj_dec_memory_marker_handler
1332 /** value of the state when the marker can appear */
1334 /** action linked to the marker */
1335 opj_bool (*handler) (
1336 opj_j2k_v2_t *p_j2k,
1337 OPJ_BYTE * p_header_data,
1338 OPJ_UINT32 p_header_size,
1339 struct opj_event_mgr * p_manager
1342 opj_dec_memory_marker_handler_t;
1344 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1347 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1348 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1349 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1350 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1351 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1352 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1353 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1354 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1355 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1356 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1357 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1358 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1359 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1361 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1362 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1363 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1364 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1365 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1366 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1368 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1369 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1370 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1371 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1372 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1373 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1374 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1375 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1376 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1377 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1378 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1379 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1380 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1382 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1383 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1384 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1385 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1386 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1387 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1389 #ifdef TODO_MS /* FIXME */
1390 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1391 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1392 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1393 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1395 #endif /* USE_JPWL */
1397 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1398 {J2K_MS_INSEC, 0, j2k_read_insec}
1399 #endif /* USE_JPSEC */
1400 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1405 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1407 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1408 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1412 for (i=0;i<p_nb_elem;++i) {
1413 opj_read_bytes(l_src_data,&l_temp,2);
1415 l_src_data+=sizeof(OPJ_INT16);
1417 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1421 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1423 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1424 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1428 for (i=0;i<p_nb_elem;++i) {
1429 opj_read_bytes(l_src_data,&l_temp,4);
1431 l_src_data+=sizeof(OPJ_INT32);
1433 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1437 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1439 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1440 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1444 for (i=0;i<p_nb_elem;++i) {
1445 opj_read_float(l_src_data,&l_temp);
1447 l_src_data+=sizeof(OPJ_FLOAT32);
1449 *(l_dest_data++) = l_temp;
1453 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1455 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1456 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1460 for (i=0;i<p_nb_elem;++i) {
1461 opj_read_double(l_src_data,&l_temp);
1463 l_src_data+=sizeof(OPJ_FLOAT64);
1465 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1469 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1471 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1472 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1476 for (i=0;i<p_nb_elem;++i) {
1477 opj_read_bytes(l_src_data,&l_temp,2);
1479 l_src_data+=sizeof(OPJ_INT16);
1481 *(l_dest_data++) = (OPJ_INT32) l_temp;
1485 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1487 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1488 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1492 for (i=0;i<p_nb_elem;++i) {
1493 opj_read_bytes(l_src_data,&l_temp,4);
1495 l_src_data+=sizeof(OPJ_INT32);
1497 *(l_dest_data++) = (OPJ_INT32) l_temp;
1501 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1503 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1504 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1508 for (i=0;i<p_nb_elem;++i) {
1509 opj_read_float(l_src_data,&l_temp);
1511 l_src_data+=sizeof(OPJ_FLOAT32);
1513 *(l_dest_data++) = (OPJ_INT32) l_temp;
1517 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1519 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1520 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1524 for (i=0;i<p_nb_elem;++i) {
1525 opj_read_double(l_src_data,&l_temp);
1527 l_src_data+=sizeof(OPJ_FLOAT64);
1529 *(l_dest_data++) = (OPJ_INT32) l_temp;
1533 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1535 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1536 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1540 for (i=0;i<p_nb_elem;++i) {
1541 l_temp = (OPJ_UINT32) *(l_src_data++);
1543 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1545 l_dest_data+=sizeof(OPJ_INT16);
1549 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1551 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1552 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1556 for (i=0;i<p_nb_elem;++i) {
1557 l_temp = (OPJ_UINT32) *(l_src_data++);
1559 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1561 l_dest_data+=sizeof(OPJ_INT32);
1565 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1567 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1568 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1572 for (i=0;i<p_nb_elem;++i) {
1573 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1575 opj_write_float(l_dest_data,l_temp);
1577 l_dest_data+=sizeof(OPJ_FLOAT32);
1581 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1583 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1584 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1588 for (i=0;i<p_nb_elem;++i) {
1589 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1591 opj_write_double(l_dest_data,l_temp);
1593 l_dest_data+=sizeof(OPJ_FLOAT64);
1599 * Converts an enum type progression order to string type.
1601 * @param prg_order the progression order to get.
1603 * @return the string representation of the given progression order.
1605 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1606 j2k_prog_order_t *po;
1607 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1608 if(po->enum_prog == prg_order){
1609 return po->str_prog;
1612 return po->str_prog;
1616 * Checks the progression order changes values. Tells if the poc given as input are valid.
1618 * @param p_pocs the progression order changes.
1619 * @param p_nb_pocs the number of progression order changes.
1620 * @param p_nb_resolutions the number of resolutions.
1621 * @param numcomps the number of components
1622 * @param numlayers the number of layers.
1623 * @param p_manager the user event manager.
1625 * @return true if the pocs are valid.
1627 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1628 OPJ_UINT32 p_nb_pocs,
1629 OPJ_UINT32 p_nb_resolutions,
1630 OPJ_UINT32 p_num_comps,
1631 OPJ_UINT32 p_num_layers,
1632 opj_event_mgr_t * p_manager)
1634 OPJ_UINT32* packet_array;
1635 OPJ_UINT32 index , resno, compno, layno;
1637 OPJ_UINT32 step_c = 1;
1638 OPJ_UINT32 step_r = p_num_comps * step_c;
1639 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1640 opj_bool loss = OPJ_FALSE;
1641 OPJ_UINT32 layno0 = 0;
1643 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1644 if (packet_array == 00) {
1645 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1648 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1650 if (p_nb_pocs == 0) {
1654 index = step_r * p_pocs->resno0;
1655 // take each resolution for each poc
1656 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1658 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1660 // take each comp of each resolution for each poc
1661 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1662 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1664 // and finally take each layer of each res of ...
1665 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1666 //index = step_r * resno + step_c * compno + step_l * layno;
1667 packet_array[comp_index] = 1;
1668 comp_index += step_l;
1671 res_index += step_c;
1678 // iterate through all the pocs
1679 for (i = 1; i < p_nb_pocs ; ++i) {
1680 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1682 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1683 index = step_r * p_pocs->resno0;
1685 // take each resolution for each poc
1686 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1687 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1689 // take each comp of each resolution for each poc
1690 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1691 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1693 // and finally take each layer of each res of ...
1694 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1695 //index = step_r * resno + step_c * compno + step_l * layno;
1696 packet_array[comp_index] = 1;
1697 comp_index += step_l;
1700 res_index += step_c;
1710 for (layno = 0; layno < p_num_layers ; ++layno) {
1711 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1712 for (compno = 0; compno < p_num_comps; ++compno) {
1713 loss |= (packet_array[index]!=1);
1714 //index = step_r * resno + step_c * compno + step_l * layno;
1721 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1724 opj_free(packet_array);
1729 /* ----------------------------------------------------------------------- */
1730 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1733 int tpnum=1,tpend=0;
1734 opj_tcp_t *tcp = &cp->tcps[tileno];
1735 prog = j2k_convert_progression_order(tcp->prg);
1740 if( cp->tp_flag == prog[i] ){
1741 tpend=1;cp->tp_pos=i;
1745 tpnum= tpnum * tcp->pocs[pino].compE;
1748 tpnum= tpnum * tcp->pocs[pino].resE;
1751 tpnum= tpnum * tcp->pocs[pino].prcE;
1754 tpnum= tpnum * tcp->pocs[pino].layE;
1766 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1768 * @param cp the coding parameters.
1769 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1770 * @param tileno the given tile.
1772 * @return the number of tile parts.
1774 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1776 const OPJ_CHAR *prog = 00;
1778 OPJ_UINT32 tpnum = 1;
1779 opj_tcp_v2_t *tcp = 00;
1780 opj_poc_t * l_current_poc = 00;
1783 assert(tileno < (cp->tw * cp->th));
1784 assert(pino < (cp->tcps[tileno].numpocs + 1));
1786 /* get the given tile coding parameter */
1787 tcp = &cp->tcps[tileno];
1790 l_current_poc = &(tcp->pocs[pino]);
1791 assert(l_current_poc != 0);
1793 /* get the progression order as a character string */
1794 prog = j2k_convert_progression_order(tcp->prg);
1795 assert(strlen(prog) > 0);
1797 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1801 /* component wise */
1803 tpnum *= l_current_poc->compE;
1805 /* resolution wise */
1807 tpnum *= l_current_poc->resE;
1811 tpnum *= l_current_poc->prcE;
1815 tpnum *= l_current_poc->layE;
1818 /* whould we split here ? */
1819 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1820 cp->m_specific_param.m_enc.m_tp_pos=i;
1832 /** mem allocation for TLM marker*/
1833 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1834 int pino,tileno,totnum_tp=0;
1836 OPJ_ARG_NOT_USED(img_numcomp);
1838 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1839 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1840 int cur_totnum_tp = 0;
1841 opj_tcp_t *tcp = &cp->tcps[tileno];
1842 for(pino = 0; pino <= tcp->numpocs; pino++) {
1844 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1845 if(!pi) { return -1;}
1846 tp_num = j2k_get_num_tp(cp,pino,tileno);
1847 totnum_tp = totnum_tp + tp_num;
1848 cur_totnum_tp = cur_totnum_tp + tp_num;
1849 pi_destroy(pi, cp, tileno);
1851 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1853 if (j2k->cstr_info) {
1854 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1855 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1863 * Calculates the total number of tile parts needed by the encoder to
1864 * encode such an image. If not enough memory is available, then the function return false.
1866 * @param p_nb_tiles pointer that will hold the number of tile parts.
1867 * @param cp the coding parameters for the image.
1868 * @param image the image to encode.
1869 * @param p_j2k the p_j2k encoder.
1870 * @param p_manager the user event manager.
1872 * @return true if the function was successful, false else.
1874 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1876 OPJ_UINT32 * p_nb_tiles,
1878 opj_event_mgr_t * p_manager)
1880 OPJ_UINT32 pino,tileno;
1881 OPJ_UINT32 l_nb_tiles;
1885 assert(p_nb_tiles != 00);
1887 assert(image != 00);
1888 assert(p_j2k != 00);
1889 assert(p_manager != 00);
1891 l_nb_tiles = cp->tw * cp->th;
1896 /* TODO mergeV2: check this part which use cstr_info */
1897 /*if (p_j2k->cstr_info) {
1898 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1900 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1901 OPJ_UINT32 cur_totnum_tp = 0;
1903 pi_update_encoding_parameters(image,cp,tileno);
1905 for (pino = 0; pino <= tcp->numpocs; ++pino)
1907 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1909 *p_nb_tiles = *p_nb_tiles + tp_num;
1911 cur_totnum_tp += tp_num;
1914 tcp->m_nb_tile_parts = cur_totnum_tp;
1916 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1917 if (l_info_tile_ptr->tp == 00) {
1921 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1923 l_info_tile_ptr->num_tps = cur_totnum_tp;
1930 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1931 OPJ_UINT32 cur_totnum_tp = 0;
1933 pi_update_encoding_parameters(image,cp,tileno);
1935 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1936 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1938 *p_nb_tiles = *p_nb_tiles + tp_num;
1940 cur_totnum_tp += tp_num;
1942 tcp->m_nb_tile_parts = cur_totnum_tp;
1951 static void j2k_write_soc(opj_j2k_t *j2k) {
1952 opj_cio_t *cio = j2k->cio;
1953 cio_write(cio, J2K_MS_SOC, 2);
1956 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1961 /* update markers struct */
1962 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1963 #endif /* USE_JPWL */
1968 * Writes the SOC marker (Start Of Codestream)
1970 * @param p_stream the stream to write data to.
1971 * @param p_j2k J2K codec.
1972 * @param p_manager the user event manager.
1974 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1975 struct opj_stream_private *p_stream,
1976 struct opj_event_mgr * p_manager )
1978 /* 2 bytes will be written */
1979 OPJ_BYTE * l_start_stream = 00;
1982 assert(p_stream != 00);
1983 assert(p_j2k != 00);
1984 assert(p_manager != 00);
1986 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1988 /* write SOC identifier */
1989 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1991 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1997 /* update markers struct */
1999 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2001 assert( 0 && "TODO" );
2002 #endif /* USE_JPWL */
2011 * Reads a SOC marker (Start of Codestream)
2012 * @param p_header_data the data contained in the SOC box.
2013 * @param jp2 the jpeg2000 file codec.
2014 * @param p_header_size the size of the data contained in the SOC marker.
2015 * @param p_manager the user event manager.
2017 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2018 struct opj_stream_private *p_stream,
2019 struct opj_event_mgr * p_manager )
2021 OPJ_BYTE l_data [2];
2022 OPJ_UINT32 l_marker;
2025 assert(p_j2k != 00);
2026 assert(p_manager != 00);
2027 assert(p_stream != 00);
2029 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2033 opj_read_bytes(l_data,&l_marker,2);
2034 if (l_marker != J2K_MS_SOC) {
2038 /* Next marker should be a SIZ marker in the main header */
2039 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2041 /* FIXME move it in a index structure included in p_j2k*/
2042 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2044 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2046 /* Add the marker to the codestream index*/
2047 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2052 static void j2k_write_siz(opj_j2k_t *j2k) {
2056 opj_cio_t *cio = j2k->cio;
2057 opj_image_t *image = j2k->image;
2058 opj_cp_t *cp = j2k->cp;
2060 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2061 lenp = cio_tell(cio);
2063 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2064 cio_write(cio, image->x1, 4); /* Xsiz */
2065 cio_write(cio, image->y1, 4); /* Ysiz */
2066 cio_write(cio, image->x0, 4); /* X0siz */
2067 cio_write(cio, image->y0, 4); /* Y0siz */
2068 cio_write(cio, cp->tdx, 4); /* XTsiz */
2069 cio_write(cio, cp->tdy, 4); /* YTsiz */
2070 cio_write(cio, cp->tx0, 4); /* XT0siz */
2071 cio_write(cio, cp->ty0, 4); /* YT0siz */
2072 cio_write(cio, image->numcomps, 2); /* Csiz */
2073 for (i = 0; i < image->numcomps; i++) {
2074 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2075 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2076 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2078 len = cio_tell(cio) - lenp;
2079 cio_seek(cio, lenp);
2080 cio_write(cio, len, 2); /* Lsiz */
2081 cio_seek(cio, lenp + len);
2084 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2088 * Writes the SIZ marker (image and tile size)
2090 * @param p_stream the stream to write data to.
2091 * @param p_j2k J2K codec.
2092 * @param p_manager the user event manager.
2094 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2095 struct opj_stream_private *p_stream,
2096 struct opj_event_mgr * p_manager )
2099 OPJ_UINT32 l_size_len;
2100 OPJ_BYTE * l_current_ptr;
2101 opj_image_t * l_image = 00;
2102 opj_cp_v2_t *cp = 00;
2103 opj_image_comp_t * l_img_comp = 00;
2106 assert(p_stream != 00);
2107 assert(p_j2k != 00);
2108 assert(p_manager != 00);
2110 l_image = p_j2k->m_private_image;
2111 cp = &(p_j2k->m_cp);
2112 l_size_len = 40 + 3 * l_image->numcomps;
2113 l_img_comp = l_image->comps;
2115 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2117 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2118 = (OPJ_BYTE*)opj_realloc(
2119 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2121 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2125 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2128 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2130 /* write SOC identifier */
2131 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2134 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2137 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2140 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2143 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2146 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2149 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2152 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2155 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2158 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2161 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2164 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2167 for (i = 0; i < l_image->numcomps; ++i) {
2168 /* TODO here with MCT ? */
2169 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2172 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2175 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2181 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) {
2191 * Reads a SIZ marker (image and tile size)
2192 * @param p_header_data the data contained in the SIZ box.
2193 * @param jp2 the jpeg2000 file codec.
2194 * @param p_header_size the size of the data contained in the SIZ marker.
2195 * @param p_manager the user event manager.
2197 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2198 OPJ_BYTE * p_header_data,
2199 OPJ_UINT32 p_header_size,
2200 opj_event_mgr_t * p_manager
2203 OPJ_UINT32 l_size, i;
2204 OPJ_UINT32 l_nb_comp;
2205 OPJ_UINT32 l_nb_comp_remain;
2206 OPJ_UINT32 l_remaining_size;
2207 OPJ_UINT32 l_nb_tiles;
2209 opj_image_t *l_image = 00;
2210 opj_cp_v2_t *l_cp = 00;
2211 opj_image_comp_t * l_img_comp = 00;
2212 opj_tcp_v2_t * l_current_tile_param = 00;
2215 assert(p_j2k != 00);
2216 assert(p_manager != 00);
2217 assert(p_header_data != 00);
2219 l_image = p_j2k->m_private_image;
2220 l_cp = &(p_j2k->m_cp);
2222 /* minimum size == 39 - 3 (= minimum component parameter) */
2223 if (p_header_size < 36) {
2224 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2228 l_remaining_size = p_header_size - 36;
2229 l_nb_comp = l_remaining_size / 3;
2230 l_nb_comp_remain = l_remaining_size % 3;
2231 if (l_nb_comp_remain != 0){
2232 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2236 l_size = p_header_size + 2; /* Lsiz */
2238 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2240 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2241 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2243 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2245 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2247 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2249 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2251 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2253 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2255 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2257 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2260 l_image->numcomps = (OPJ_UINT16) l_tmp;
2262 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2266 if (l_image->numcomps != l_nb_comp) {
2267 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);
2272 if (l_cp->correct) {
2273 /* if JPWL is on, we check whether TX errors have damaged
2274 too much the SIZ parameters */
2275 if (!(l_image->x1 * l_image->y1)) {
2276 opj_event_msg_v2(p_manager, EVT_ERROR,
2277 "JPWL: bad image size (%d x %d)\n",
2278 l_image->x1, l_image->y1);
2279 if (!JPWL_ASSUME || JPWL_ASSUME) {
2280 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2285 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2286 if (l_image->numcomps != ((len - 38) / 3)) {
2287 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2288 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2289 l_image->numcomps, ((len - 38) / 3));
2291 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2294 */ /* we try to correct */
2295 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2296 if (l_image->numcomps < ((len - 38) / 3)) {
2297 len = 38 + 3 * l_image->numcomps;
2298 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2301 l_image->numcomps = ((len - 38) / 3);
2302 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2308 /* update components number in the jpwl_exp_comps filed */
2309 l_cp->exp_comps = l_image->numcomps;
2311 #endif /* USE_JPWL */
2313 /* Allocate the resulting image components */
2314 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2315 if (l_image->comps == 00){
2316 l_image->numcomps = 0;
2317 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2321 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2322 l_img_comp = l_image->comps;
2324 /* Read the component information */
2325 for (i = 0; i < l_image->numcomps; ++i){
2327 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2329 l_img_comp->prec = (tmp & 0x7f) + 1;
2330 l_img_comp->sgnd = tmp >> 7;
2331 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2333 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2334 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2336 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2339 if (l_cp->correct) {
2340 /* if JPWL is on, we check whether TX errors have damaged
2341 too much the SIZ parameters, again */
2342 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2343 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2344 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2345 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2347 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2350 /* we try to correct */
2351 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2352 if (!l_image->comps[i].dx) {
2353 l_image->comps[i].dx = 1;
2354 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2355 i, l_image->comps[i].dx);
2357 if (!l_image->comps[i].dy) {
2358 l_image->comps[i].dy = 1;
2359 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2360 i, l_image->comps[i].dy);
2364 #endif /* USE_JPWL */
2365 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2366 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2370 /* Compute the number of tiles */
2371 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2372 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2373 l_nb_tiles = l_cp->tw * l_cp->th;
2375 /* Define the tiles which will be decoded */
2376 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2377 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;
2378 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;
2379 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);
2380 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);
2383 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2384 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2385 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2386 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2390 if (l_cp->correct) {
2391 /* if JPWL is on, we check whether TX errors have damaged
2392 too much the SIZ parameters */
2393 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2394 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2395 "JPWL: bad number of tiles (%d x %d)\n",
2396 l_cp->tw, l_cp->th);
2398 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2401 /* we try to correct */
2402 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2405 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2408 if (l_cp->tw > l_cp->max_tiles) {
2410 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2411 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2412 l_cp->max_tiles, l_cp->tw);
2416 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2419 if (l_cp->th > l_cp->max_tiles) {
2421 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2422 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2423 l_cp->max_tiles, l_cp->th);
2427 #endif /* USE_JPWL */
2429 /* memory allocations */
2430 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2431 if (l_cp->tcps == 00) {
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2435 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2438 if (l_cp->correct) {
2440 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2441 "JPWL: could not alloc tcps field of cp\n");
2442 if (!JPWL_ASSUME || JPWL_ASSUME) {
2443 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2448 #endif /* USE_JPWL */
2450 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2451 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2452 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
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->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2458 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2459 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2461 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2462 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2465 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2466 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2468 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2469 (opj_simple_mcc_decorrelation_data_t*)
2470 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2472 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2473 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2476 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));
2477 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2479 /* set up default dc level shift */
2480 for (i=0;i<l_image->numcomps;++i) {
2481 if (! l_image->comps[i].sgnd) {
2482 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2486 l_current_tile_param = l_cp->tcps;
2487 for (i = 0; i < l_nb_tiles; ++i) {
2488 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2489 if (l_current_tile_param->tccps == 00) {
2490 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2493 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2495 ++l_current_tile_param;
2498 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2499 opj_image_comp_header_update(l_image,l_cp);
2506 static void j2k_write_com(opj_j2k_t *j2k) {
2510 if(j2k->cp->comment) {
2511 opj_cio_t *cio = j2k->cio;
2512 char *comment = j2k->cp->comment;
2514 cio_write(cio, J2K_MS_COM, 2);
2515 lenp = cio_tell(cio);
2517 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2518 for (i = 0; i < strlen(comment); i++) {
2519 cio_write(cio, comment[i], 1);
2521 len = cio_tell(cio) - lenp;
2522 cio_seek(cio, lenp);
2523 cio_write(cio, len, 2);
2524 cio_seek(cio, lenp + len);
2528 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2534 * Writes the COM marker (comment)
2536 * @param p_stream the stream to write data to.
2537 * @param p_j2k J2K codec.
2538 * @param p_manager the user event manager.
2540 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2541 struct opj_stream_private *p_stream,
2542 struct opj_event_mgr * p_manager )
2544 OPJ_UINT32 l_comment_size;
2545 OPJ_UINT32 l_total_com_size;
2546 const OPJ_CHAR *l_comment;
2547 OPJ_BYTE * l_current_ptr = 00;
2550 assert(p_j2k != 00);
2551 assert(p_stream != 00);
2552 assert(p_manager != 00);
2554 l_comment = p_j2k->m_cp.comment;
2555 l_comment_size = strlen(l_comment);
2556 l_total_com_size = l_comment_size + 6;
2558 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2559 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2560 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2563 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2567 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2570 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2572 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2575 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2578 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2581 memcpy( l_current_ptr,l_comment,l_comment_size);
2583 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) {
2590 static void j2k_read_com(opj_j2k_t *j2k) {
2593 opj_cio_t *cio = j2k->cio;
2595 len = cio_read(cio, 2);
2596 cio_skip(cio, len - 2);
2599 * Reads a COM marker (comments)
2600 * @param p_header_data the data contained in the COM box.
2601 * @param jp2 the jpeg2000 file codec.
2602 * @param p_header_size the size of the data contained in the COM marker.
2603 * @param p_manager the user event manager.
2605 opj_bool j2k_read_com_v2 (
2606 opj_j2k_v2_t *p_j2k,
2607 OPJ_BYTE * p_header_data,
2608 OPJ_UINT32 p_header_size,
2609 struct opj_event_mgr * p_manager
2613 assert(p_j2k != 00);
2614 assert(p_manager != 00);
2615 assert(p_header_data != 00);
2616 (void)p_header_size;
2621 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2624 opj_cp_t *cp = j2k->cp;
2625 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2626 opj_tccp_t *tccp = &tcp->tccps[compno];
2627 opj_cio_t *cio = j2k->cio;
2629 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2630 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2631 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2632 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2633 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2635 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2636 for (i = 0; i < tccp->numresolutions; i++) {
2637 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2643 static void j2k_write_cod(opj_j2k_t *j2k) {
2644 opj_cp_t *cp = NULL;
2645 opj_tcp_t *tcp = NULL;
2648 opj_cio_t *cio = j2k->cio;
2650 cio_write(cio, J2K_MS_COD, 2); /* COD */
2652 lenp = cio_tell(cio);
2656 tcp = &cp->tcps[j2k->curtileno];
2658 cio_write(cio, tcp->csty, 1); /* Scod */
2659 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2660 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2661 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2663 j2k_write_cox(j2k, 0);
2664 len = cio_tell(cio) - lenp;
2665 cio_seek(cio, lenp);
2666 cio_write(cio, len, 2); /* Lcod */
2667 cio_seek(cio, lenp + len);
2670 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2675 * Writes the COD marker (Coding style default)
2677 * @param p_stream the stream to write data to.
2678 * @param p_j2k J2K codec.
2679 * @param p_manager the user event manager.
2681 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2682 struct opj_stream_private *p_stream,
2683 struct opj_event_mgr * p_manager )
2685 opj_cp_v2_t *l_cp = 00;
2686 opj_tcp_v2_t *l_tcp = 00;
2687 OPJ_UINT32 l_code_size,l_remaining_size;
2688 OPJ_BYTE * l_current_data = 00;
2691 assert(p_j2k != 00);
2692 assert(p_manager != 00);
2693 assert(p_stream != 00);
2695 l_cp = &(p_j2k->m_cp);
2696 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2697 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2698 l_remaining_size = l_code_size;
2700 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2701 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2702 = (OPJ_BYTE*)opj_realloc(
2703 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2706 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2710 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2713 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2715 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2716 l_current_data += 2;
2718 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2719 l_current_data += 2;
2721 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2724 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2727 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2730 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2733 l_remaining_size -= 9;
2735 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2736 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2740 if (l_remaining_size != 0) {
2741 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2745 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) {
2754 * Reads a COD marker (Coding Styke defaults)
2755 * @param p_header_data the data contained in the COD box.
2756 * @param p_j2k the jpeg2000 codec.
2757 * @param p_header_size the size of the data contained in the COD marker.
2758 * @param p_manager the user event manager.
2760 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2761 OPJ_BYTE * p_header_data,
2762 OPJ_UINT32 p_header_size,
2763 opj_event_mgr_t * p_manager
2769 opj_cp_v2_t *l_cp = 00;
2770 opj_tcp_v2_t *l_tcp = 00;
2771 opj_image_t *l_image = 00;
2774 assert(p_header_data != 00);
2775 assert(p_j2k != 00);
2776 assert(p_manager != 00);
2778 l_image = p_j2k->m_private_image;
2779 l_cp = &(p_j2k->m_cp);
2781 /* If we are in the first tile-part header of the current tile */
2782 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2783 &l_cp->tcps[p_j2k->m_current_tile_number] :
2784 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2786 /* Make sure room is sufficient */
2787 if (p_header_size < 5) {
2788 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2792 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2794 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2796 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2797 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2800 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2801 if (l_cp->m_specific_param.m_dec.m_layer) {
2802 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2805 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2808 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2812 for (i = 0; i < l_image->numcomps; ++i) {
2813 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2816 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2817 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2821 if (p_header_size != 0) {
2822 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2826 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2827 j2k_copy_tile_component_parameters(p_j2k);
2830 #ifdef WIP_REMOVE_MSD
2831 if (p_j2k->cstr_info) {
2832 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2833 p_j2k->cstr_info->prog = l_tcp->prg;
2834 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2835 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2836 for (i = 0; i < l_image->numcomps; ++i) {
2837 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2845 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2848 opj_cp_t *cp = j2k->cp;
2849 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2850 opj_image_t *image = j2k->image;
2851 opj_cio_t *cio = j2k->cio;
2853 cio_write(cio, J2K_MS_COC, 2); /* COC */
2854 lenp = cio_tell(cio);
2856 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2857 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2858 j2k_write_cox(j2k, compno);
2859 len = cio_tell(cio) - lenp;
2860 cio_seek(cio, lenp);
2861 cio_write(cio, len, 2); /* Lcoc */
2862 cio_seek(cio, lenp + len);
2866 * Writes the COC marker (Coding style component)
2868 * @param p_comp_no the index of the component to output.
2869 * @param p_stream the stream to write data to.
2870 * @param p_j2k J2K codec.
2871 * @param p_manager the user event manager.
2873 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2874 OPJ_UINT32 p_comp_no,
2875 struct opj_stream_private *p_stream,
2876 struct opj_event_mgr * p_manager )
2878 OPJ_UINT32 l_coc_size,l_remaining_size;
2879 OPJ_UINT32 l_comp_room;
2882 assert(p_j2k != 00);
2883 assert(p_manager != 00);
2884 assert(p_stream != 00);
2886 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2888 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2890 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2891 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2892 = (OPJ_BYTE*)opj_realloc(
2893 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2895 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2899 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2902 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);
2904 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) {
2912 * Writes the COC marker (Coding style component)
2914 * @param p_comp_no the index of the component to output.
2915 * @param p_stream the stream to write data to.
2916 * @param p_j2k J2K codec.
2917 * @param p_manager the user event manager.
2919 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2920 OPJ_UINT32 p_comp_no,
2922 OPJ_UINT32 * p_data_written,
2923 struct opj_event_mgr * p_manager )
2925 opj_cp_v2_t *l_cp = 00;
2926 opj_tcp_v2_t *l_tcp = 00;
2927 OPJ_UINT32 l_coc_size,l_remaining_size;
2928 OPJ_BYTE * l_current_data = 00;
2929 opj_image_t *l_image = 00;
2930 OPJ_UINT32 l_comp_room;
2933 assert(p_j2k != 00);
2934 assert(p_manager != 00);
2936 l_cp = &(p_j2k->m_cp);
2937 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2938 l_image = p_j2k->m_private_image;
2939 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2941 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2942 l_remaining_size = l_coc_size;
2944 l_current_data = p_data;
2946 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2947 l_current_data += 2;
2949 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2950 l_current_data += 2;
2952 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2953 l_current_data+=l_comp_room;
2955 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2958 l_remaining_size -= (5 + l_comp_room);
2959 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2960 * p_data_written = l_coc_size;
2964 * Gets the maximum size taken by a coc.
2966 * @param p_j2k the jpeg2000 codec to use.
2968 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2971 OPJ_UINT32 l_nb_comp;
2972 OPJ_UINT32 l_nb_tiles;
2973 OPJ_UINT32 l_max = 0;
2977 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2978 l_nb_comp = p_j2k->m_private_image->numcomps;
2980 for (i=0;i<l_nb_tiles;++i) {
2981 for (j=0;j<l_nb_comp;++j) {
2982 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2991 * Reads a COC marker (Coding Style Component)
2992 * @param p_header_data the data contained in the COC box.
2993 * @param p_j2k the jpeg2000 codec.
2994 * @param p_header_size the size of the data contained in the COC marker.
2995 * @param p_manager the user event manager.
2997 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2998 OPJ_BYTE * p_header_data,
2999 OPJ_UINT32 p_header_size,
3000 opj_event_mgr_t * p_manager
3003 opj_cp_v2_t *l_cp = NULL;
3004 opj_tcp_v2_t *l_tcp = NULL;
3005 opj_image_t *l_image = NULL;
3006 OPJ_UINT32 l_comp_room;
3007 OPJ_UINT32 l_comp_no;
3010 assert(p_header_data != 00);
3011 assert(p_j2k != 00);
3012 assert(p_manager != 00);
3014 l_cp = &(p_j2k->m_cp);
3015 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3016 &l_cp->tcps[p_j2k->m_current_tile_number] :
3017 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3018 l_image = p_j2k->m_private_image;
3020 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3022 /* make sure room is sufficient*/
3023 if (p_header_size < l_comp_room + 1) {
3024 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3027 p_header_size -= l_comp_room + 1;
3029 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3030 p_header_data += l_comp_room;
3031 if (l_comp_no >= l_image->numcomps) {
3032 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3036 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3039 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3040 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3044 if (p_header_size != 0) {
3045 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3051 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3052 int bandno, numbands;
3055 opj_cp_t *cp = j2k->cp;
3056 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3057 opj_tccp_t *tccp = &tcp->tccps[compno];
3058 opj_cio_t *cio = j2k->cio;
3060 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3061 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3063 for (bandno = 0; bandno < numbands; bandno++) {
3064 expn = tccp->stepsizes[bandno].expn;
3065 mant = tccp->stepsizes[bandno].mant;
3067 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3068 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3070 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3076 static void j2k_write_qcd(opj_j2k_t *j2k) {
3079 opj_cio_t *cio = j2k->cio;
3081 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3082 lenp = cio_tell(cio);
3084 j2k_write_qcx(j2k, 0);
3085 len = cio_tell(cio) - lenp;
3086 cio_seek(cio, lenp);
3087 cio_write(cio, len, 2); /* Lqcd */
3088 cio_seek(cio, lenp + len);
3091 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3095 * Writes the QCD marker (quantization default)
3097 * @param p_comp_number the index of the component to output.
3098 * @param p_stream the stream to write data to.
3099 * @param p_j2k J2K codec.
3100 * @param p_manager the user event manager.
3102 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3103 struct opj_stream_private *p_stream,
3104 struct opj_event_mgr * p_manager )
3106 opj_cp_v2_t *l_cp = 00;
3107 opj_tcp_v2_t *l_tcp = 00;
3108 OPJ_UINT32 l_qcd_size,l_remaining_size;
3109 OPJ_BYTE * l_current_data = 00;
3112 assert(p_j2k != 00);
3113 assert(p_manager != 00);
3114 assert(p_stream != 00);
3116 l_cp = &(p_j2k->m_cp);
3117 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3118 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3119 l_remaining_size = l_qcd_size;
3121 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3122 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3123 = (OPJ_BYTE*)opj_realloc(
3124 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3127 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3131 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3134 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3136 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3137 l_current_data += 2;
3139 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3140 l_current_data += 2;
3142 l_remaining_size -= 4;
3144 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3145 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3149 if (l_remaining_size != 0) {
3150 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3154 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) {
3162 * Reads a QCD marker (Quantization defaults)
3163 * @param p_header_data the data contained in the QCD box.
3164 * @param p_j2k the jpeg2000 codec.
3165 * @param p_header_size the size of the data contained in the QCD marker.
3166 * @param p_manager the user event manager.
3168 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3169 OPJ_BYTE * p_header_data,
3170 OPJ_UINT32 p_header_size,
3171 opj_event_mgr_t * p_manager
3175 assert(p_header_data != 00);
3176 assert(p_j2k != 00);
3177 assert(p_manager != 00);
3179 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3180 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3184 if (p_header_size != 0) {
3185 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3189 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3190 j2k_copy_tile_quantization_parameters(p_j2k);
3195 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3198 opj_cio_t *cio = j2k->cio;
3200 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3201 lenp = cio_tell(cio);
3203 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3204 j2k_write_qcx(j2k, compno);
3205 len = cio_tell(cio) - lenp;
3206 cio_seek(cio, lenp);
3207 cio_write(cio, len, 2); /* Lqcc */
3208 cio_seek(cio, lenp + len);
3212 * Writes the QCC marker (quantization component)
3214 * @param p_comp_no the index of the component to output.
3215 * @param p_stream the stream to write data to.
3216 * @param p_j2k J2K codec.
3217 * @param p_manager the user event manager.
3219 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3220 OPJ_UINT32 p_comp_no,
3221 struct opj_stream_private *p_stream,
3222 struct opj_event_mgr * p_manager )
3224 OPJ_UINT32 l_qcc_size,l_remaining_size;
3227 assert(p_j2k != 00);
3228 assert(p_manager != 00);
3229 assert(p_stream != 00);
3231 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3232 l_remaining_size = l_qcc_size;
3234 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3235 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3236 = (OPJ_BYTE*)opj_realloc(
3237 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3239 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3243 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3246 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);
3248 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) {
3256 * Writes the QCC marker (quantization component)
3258 * @param p_comp_no the index of the component to output.
3259 * @param p_stream the stream to write data to.
3260 * @param p_j2k J2K codec.
3261 * @param p_manager the user event manager.
3263 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3264 OPJ_UINT32 p_comp_no,
3266 OPJ_UINT32 * p_data_written,
3267 struct opj_event_mgr * p_manager )
3269 OPJ_UINT32 l_qcc_size,l_remaining_size;
3270 OPJ_BYTE * l_current_data = 00;
3273 assert(p_j2k != 00);
3274 assert(p_manager != 00);
3276 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3277 l_remaining_size = l_qcc_size;
3279 l_current_data = p_data;
3281 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3282 l_current_data += 2;
3284 if (p_j2k->m_private_image->numcomps <= 256) {
3287 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3288 l_current_data += 2;
3290 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3293 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3294 l_remaining_size -= 6;
3297 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3298 l_current_data += 2;
3300 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3303 l_remaining_size -= 6;
3306 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3308 *p_data_written = l_qcc_size;
3312 * Gets the maximum size taken by a qcc.
3314 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3316 return j2k_get_max_coc_size(p_j2k);
3320 * Reads a QCC marker (Quantization component)
3321 * @param p_header_data the data contained in the QCC box.
3322 * @param p_j2k the jpeg2000 codec.
3323 * @param p_header_size the size of the data contained in the QCC marker.
3324 * @param p_manager the user event manager.
3326 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3327 OPJ_BYTE * p_header_data,
3328 OPJ_UINT32 p_header_size,
3329 opj_event_mgr_t * p_manager
3332 OPJ_UINT32 l_num_comp,l_comp_no;
3335 assert(p_header_data != 00);
3336 assert(p_j2k != 00);
3337 assert(p_manager != 00);
3339 l_num_comp = p_j2k->m_private_image->numcomps;
3341 if (l_num_comp <= 256) {
3342 if (p_header_size < 1) {
3343 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3346 opj_read_bytes(p_header_data,&l_comp_no,1);
3351 if (p_header_size < 2) {
3352 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3355 opj_read_bytes(p_header_data,&l_comp_no,2);
3361 if (p_j2k->m_cp.correct) {
3363 static OPJ_UINT32 backup_compno = 0;
3365 /* compno is negative or larger than the number of components!!! */
3366 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3367 opj_event_msg_v2(p_manager, EVT_ERROR,
3368 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3369 l_comp_no, l_num_comp);
3371 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3374 /* we try to correct */
3375 l_comp_no = backup_compno % l_num_comp;
3376 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3377 "- setting component number to %d\n",
3381 /* keep your private count of tiles */
3384 #endif /* USE_JPWL */
3386 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3387 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3391 if (p_header_size != 0) {
3392 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3400 static void j2k_write_poc(opj_j2k_t *j2k) {
3401 int len, numpchgs, i;
3403 int numcomps = j2k->image->numcomps;
3405 opj_cp_t *cp = j2k->cp;
3406 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3407 opj_tccp_t *tccp = &tcp->tccps[0];
3408 opj_cio_t *cio = j2k->cio;
3410 numpchgs = 1 + tcp->numpocs;
3411 cio_write(cio, J2K_MS_POC, 2); /* POC */
3412 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3413 cio_write(cio, len, 2); /* Lpoc */
3414 for (i = 0; i < numpchgs; i++) {
3415 opj_poc_t *poc = &tcp->pocs[i];
3416 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3417 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3418 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3419 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3420 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3421 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3422 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3423 poc->compno1 = int_min(poc->compno1, numcomps);
3424 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3429 * Writes the POC marker (Progression Order Change)
3431 * @param p_stream the stream to write data to.
3432 * @param p_j2k J2K codec.
3433 * @param p_manager the user event manager.
3435 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3436 struct opj_stream_private *p_stream,
3437 struct opj_event_mgr * p_manager )
3439 OPJ_UINT32 l_nb_comp;
3440 OPJ_UINT32 l_nb_poc;
3441 OPJ_UINT32 l_poc_size;
3442 OPJ_UINT32 l_written_size = 0;
3443 opj_tcp_v2_t *l_tcp = 00;
3444 opj_tccp_t *l_tccp = 00;
3445 OPJ_UINT32 l_poc_room;
3448 assert(p_j2k != 00);
3449 assert(p_manager != 00);
3450 assert(p_stream != 00);
3452 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3453 l_tccp = &l_tcp->tccps[0];
3454 l_nb_comp = p_j2k->m_private_image->numcomps;
3455 l_nb_poc = 1 + l_tcp->numpocs;
3457 if (l_nb_comp <= 256) {
3463 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3465 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3466 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3467 = (OPJ_BYTE*)opj_realloc(
3468 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3471 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3475 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3478 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3480 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) {
3489 * Writes the POC marker (Progression Order Change)
3491 * @param p_stream the stream to write data to.
3492 * @param p_j2k J2K codec.
3493 * @param p_manager the user event manager.
3495 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3497 OPJ_UINT32 * p_data_written,
3498 struct opj_event_mgr * p_manager )
3501 OPJ_BYTE * l_current_data = 00;
3502 OPJ_UINT32 l_nb_comp;
3503 OPJ_UINT32 l_nb_poc;
3504 OPJ_UINT32 l_poc_size;
3505 opj_image_t *l_image = 00;
3506 opj_tcp_v2_t *l_tcp = 00;
3507 opj_tccp_t *l_tccp = 00;
3508 opj_poc_t *l_current_poc = 00;
3509 OPJ_UINT32 l_poc_room;
3512 assert(p_j2k != 00);
3513 assert(p_manager != 00);
3515 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3516 l_tccp = &l_tcp->tccps[0];
3517 l_image = p_j2k->m_private_image;
3518 l_nb_comp = l_image->numcomps;
3519 l_nb_poc = 1 + l_tcp->numpocs;
3521 if (l_nb_comp <= 256) {
3528 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3530 l_current_data = p_data;
3532 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3533 l_current_data += 2;
3535 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3536 l_current_data += 2;
3538 l_current_poc = l_tcp->pocs;
3539 for (i = 0; i < l_nb_poc; ++i) {
3540 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3543 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3544 l_current_data+=l_poc_room;
3546 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3549 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3552 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3553 l_current_data+=l_poc_room;
3555 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3558 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3559 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3560 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3561 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3566 *p_data_written = l_poc_size;
3570 * Gets the maximum size taken by the writing of a POC.
3572 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3574 opj_tcp_v2_t * l_tcp = 00;
3575 OPJ_UINT32 l_nb_tiles = 0;
3576 OPJ_UINT32 l_max_poc = 0;
3579 l_tcp = p_j2k->m_cp.tcps;
3580 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3582 for (i=0;i<l_nb_tiles;++i) {
3583 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3589 return 4 + 9 * l_max_poc;
3593 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3595 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3598 OPJ_UINT32 l_nb_tiles;
3599 OPJ_UINT32 l_max = 0;
3600 opj_tcp_v2_t * l_tcp = 00;
3602 l_tcp = p_j2k->m_cp.tcps;
3603 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3605 for (i=0;i<l_nb_tiles;++i) {
3606 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3616 * Gets the maximum size taken by the headers of the SOT.
3618 * @param p_j2k the jpeg2000 codec to use.
3620 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3622 OPJ_UINT32 l_nb_bytes = 0;
3623 OPJ_UINT32 l_nb_comps;
3624 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3626 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3627 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3629 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3630 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3631 l_nb_bytes += l_nb_comps * l_coc_bytes;
3633 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3634 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3637 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3639 /*** DEVELOPER CORNER, Add room for your headers ***/
3646 * Reads a POC marker (Progression Order Change)
3648 * @param p_header_data the data contained in the POC box.
3649 * @param p_j2k the jpeg2000 codec.
3650 * @param p_header_size the size of the data contained in the POC marker.
3651 * @param p_manager the user event manager.
3653 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3654 OPJ_BYTE * p_header_data,
3655 OPJ_UINT32 p_header_size,
3656 opj_event_mgr_t * p_manager
3659 OPJ_UINT32 i, l_nb_comp, l_tmp;
3660 opj_image_t * l_image = 00;
3661 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3662 OPJ_UINT32 l_chunk_size, l_comp_room;
3664 opj_cp_v2_t *l_cp = 00;
3665 opj_tcp_v2_t *l_tcp = 00;
3666 opj_poc_t *l_current_poc = 00;
3669 assert(p_header_data != 00);
3670 assert(p_j2k != 00);
3671 assert(p_manager != 00);
3673 l_image = p_j2k->m_private_image;
3674 l_nb_comp = l_image->numcomps;
3675 if (l_nb_comp <= 256) {
3681 l_chunk_size = 5 + 2 * l_comp_room;
3682 l_current_poc_nb = p_header_size / l_chunk_size;
3683 l_current_poc_remaining = p_header_size % l_chunk_size;
3685 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3686 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3690 l_cp = &(p_j2k->m_cp);
3691 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3692 &l_cp->tcps[p_j2k->m_current_tile_number] :
3693 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3694 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3695 l_current_poc_nb += l_old_poc_nb;
3697 assert(l_current_poc_nb < 32);
3699 /* now poc is in use.*/
3702 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3703 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3704 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3706 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3707 p_header_data+=l_comp_room;
3708 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3710 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3712 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3713 p_header_data+=l_comp_room;
3714 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3716 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3717 /* make sure comp is in acceptable bounds */
3718 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3722 l_tcp->numpocs = l_current_poc_nb - 1;
3726 static void j2k_read_crg(opj_j2k_t *j2k) {
3727 int len, i, Xcrg_i, Ycrg_i;
3729 opj_cio_t *cio = j2k->cio;
3730 int numcomps = j2k->image->numcomps;
3732 len = cio_read(cio, 2); /* Lcrg */
3733 for (i = 0; i < numcomps; i++) {
3734 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3735 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
3740 * Reads a CRG marker (Component registration)
3742 * @param p_header_data the data contained in the TLM box.
3743 * @param p_j2k the jpeg2000 codec.
3744 * @param p_header_size the size of the data contained in the TLM marker.
3745 * @param p_manager the user event manager.
3747 opj_bool j2k_read_crg_v2 (
3748 opj_j2k_v2_t *p_j2k,
3749 OPJ_BYTE * p_header_data,
3750 OPJ_UINT32 p_header_size,
3751 struct opj_event_mgr * p_manager
3754 OPJ_UINT32 l_nb_comp;
3756 assert(p_header_data != 00);
3757 assert(p_j2k != 00);
3758 assert(p_manager != 00);
3760 l_nb_comp = p_j2k->m_private_image->numcomps;
3762 if (p_header_size != l_nb_comp *4) {
3763 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3766 /* Do not care of this at the moment since only local variables are set here */
3769 (i = 0; i < l_nb_comp; ++i)
3771 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3773 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3781 * Reads a TLM marker (Tile Length Marker)
3783 * @param p_header_data the data contained in the TLM box.
3784 * @param p_j2k the jpeg2000 codec.
3785 * @param p_header_size the size of the data contained in the TLM marker.
3786 * @param p_manager the user event manager.
3788 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3789 OPJ_BYTE * p_header_data,
3790 OPJ_UINT32 p_header_size,
3791 opj_event_mgr_t * p_manager
3794 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3796 assert(p_header_data != 00);
3797 assert(p_j2k != 00);
3798 assert(p_manager != 00);
3800 if (p_header_size < 2) {
3801 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3806 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3808 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3811 l_ST = ((l_Stlm >> 4) & 0x3);
3812 l_SP = (l_Stlm >> 6) & 0x1;
3814 l_Ptlm_size = (l_SP + 1) * 2;
3815 l_quotient = l_Ptlm_size + l_ST;
3817 l_tot_num_tp = p_header_size / l_quotient;
3818 l_tot_num_tp_remaining = p_header_size % l_quotient;
3820 if (l_tot_num_tp_remaining != 0) {
3821 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3824 /* FIXME Do not care of this at the moment since only local variables are set here */
3827 (i = 0; i < l_tot_num_tp; ++i)
3829 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3830 p_header_data += l_ST;
3831 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3832 p_header_data += l_Ptlm_size;
3839 * Reads a PLM marker (Packet length, main header marker)
3841 * @param p_header_data the data contained in the TLM box.
3842 * @param p_j2k the jpeg2000 codec.
3843 * @param p_header_size the size of the data contained in the TLM marker.
3844 * @param p_manager the user event manager.
3846 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3847 OPJ_BYTE * p_header_data,
3848 OPJ_UINT32 p_header_size,
3849 opj_event_mgr_t * p_manager
3853 assert(p_header_data != 00);
3854 assert(p_j2k != 00);
3855 assert(p_manager != 00);
3857 if (p_header_size < 1) {
3858 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3861 /* Do not care of this at the moment since only local variables are set here */
3863 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3870 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3872 p_header_size -= (1+l_Nplm);
3876 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3880 (i = 0; i < l_Nplm; ++i)
3882 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3884 // take only the last seven bytes
3885 l_packet_len |= (l_tmp & 0x7f);
3893 // store packet length and proceed to next packet
3900 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3909 * Reads a PLT marker (Packet length, tile-part header)
3911 * @param p_header_data the data contained in the PLT box.
3912 * @param p_j2k the jpeg2000 codec.
3913 * @param p_header_size the size of the data contained in the PLT marker.
3914 * @param p_manager the user event manager.
3916 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3917 OPJ_BYTE * p_header_data,
3918 OPJ_UINT32 p_header_size,
3919 opj_event_mgr_t * p_manager
3922 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3925 assert(p_header_data != 00);
3926 assert(p_j2k != 00);
3927 assert(p_manager != 00);
3929 if (p_header_size < 1) {
3930 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3934 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3938 for (i = 0; i < p_header_size; ++i) {
3939 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3941 /* take only the last seven bytes */
3942 l_packet_len |= (l_tmp & 0x7f);
3947 /* store packet length and proceed to next packet */
3952 if (l_packet_len != 0) {
3953 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3962 * Reads a PPM marker (Packed packet headers, main header)
3964 * @param p_header_data the data contained in the POC box.
3965 * @param p_j2k the jpeg2000 codec.
3966 * @param p_header_size the size of the data contained in the POC marker.
3967 * @param p_manager the user event manager.
3970 opj_bool j2k_read_ppm_v2 (
3971 opj_j2k_v2_t *p_j2k,
3972 OPJ_BYTE * p_header_data,
3973 OPJ_UINT32 p_header_size,
3974 struct opj_event_mgr * p_manager
3978 opj_cp_v2_t *l_cp = 00;
3979 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3982 assert(p_header_data != 00);
3983 assert(p_j2k != 00);
3984 assert(p_manager != 00);
3986 if (p_header_size < 1) {
3987 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3991 l_cp = &(p_j2k->m_cp);
3994 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3998 /* First PPM marker */
4000 if (p_header_size < 4) {
4001 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4005 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4009 /* First PPM marker: Initialization */
4010 l_cp->ppm_len = l_N_ppm;
4011 l_cp->ppm_data_size = 0;
4013 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4014 if (l_cp->ppm_buffer == 00) {
4015 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4018 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4020 l_cp->ppm_data = l_cp->ppm_buffer;
4024 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4025 if (p_header_size >= 4) {
4027 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4030 l_cp->ppm_len += l_N_ppm ;
4032 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4033 if (l_cp->ppm_buffer == 00) {
4034 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4037 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4039 l_cp->ppm_data = l_cp->ppm_buffer;
4046 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4048 if (l_remaining_data <= p_header_size) {
4049 /* we must store less information than available in the packet */
4050 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4051 l_cp->ppm_data_size = l_cp->ppm_len;
4052 p_header_size -= l_remaining_data;
4053 p_header_data += l_remaining_data;
4056 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4057 l_cp->ppm_data_size += p_header_size;
4058 p_header_data += p_header_size;
4071 * Reads a PPM marker (Packed packet headers, main header)
4073 * @param p_header_data the data contained in the POC box.
4074 * @param p_j2k the jpeg2000 codec.
4075 * @param p_header_size the size of the data contained in the POC marker.
4076 * @param p_manager the user event manager.
4078 opj_bool j2k_read_ppm_v3 (
4079 opj_j2k_v2_t *p_j2k,
4080 OPJ_BYTE * p_header_data,
4081 OPJ_UINT32 p_header_size,
4082 struct opj_event_mgr * p_manager
4085 opj_cp_v2_t *l_cp = 00;
4086 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4089 assert(p_header_data != 00);
4090 assert(p_j2k != 00);
4091 assert(p_manager != 00);
4093 /* Minimum size of PPM marker is equal to the size of Zppm element */
4094 if (p_header_size < 1) {
4095 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4099 l_cp = &(p_j2k->m_cp);
4102 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4106 /* First PPM marker */
4108 /* We need now at least the Nppm^0 element */
4109 if (p_header_size < 4) {
4110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4114 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4118 /* First PPM marker: Initialization */
4119 l_cp->ppm_len = l_N_ppm;
4120 l_cp->ppm_data_read = 0;
4122 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4123 if (l_cp->ppm_data == 00) {
4124 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4127 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4129 l_cp->ppm_data_current = l_cp->ppm_data;
4131 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4134 if (p_header_size < 4) {
4135 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4139 /* Uncompleted Ippm series in the previous PPM marker?*/
4140 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4141 /* Get the place where add the remaining Ippm series*/
4142 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4143 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4146 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4150 /* Increase the size of ppm_data to add the new Ippm series*/
4151 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4153 /* Keep the position of the place where concatenate the new series*/
4154 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4155 l_cp->ppm_len += l_N_ppm;
4160 l_remaining_data = p_header_size;
4162 while (l_remaining_data >= l_N_ppm) {
4163 /* read a complete Ippm series*/
4164 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4165 p_header_size -= l_N_ppm;
4166 p_header_data += l_N_ppm;
4168 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4172 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4177 l_remaining_data = p_header_size;
4181 l_remaining_data = p_header_size;
4183 /* Next Ippm series is a complete series ?*/
4184 if (l_remaining_data > l_N_ppm) {
4185 /* Increase the size of ppm_data to add the new Ippm series*/
4186 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4188 /* Keep the position of the place where concatenate the new series */
4189 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4190 l_cp->ppm_len += l_N_ppm;
4195 /* Need to read an incomplete Ippm series*/
4196 if (l_remaining_data) {
4197 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4199 /* Keep the position of the place where concatenate the new series*/
4200 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4201 l_cp->ppm_len += l_N_ppm;
4203 /* Read incomplete Ippm series*/
4204 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4205 p_header_size -= l_remaining_data;
4206 p_header_data += l_remaining_data;
4208 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4213 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4214 if (p_header_size >= 4) {
4216 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4219 l_cp->ppm_len += l_N_ppm ;
4221 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4222 if (l_cp->ppm_buffer == 00) {
4223 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4226 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4228 l_cp->ppm_data = l_cp->ppm_buffer;
4235 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4237 if (l_remaining_data <= p_header_size) {
4238 /* we must store less information than available in the packet */
4239 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4240 l_cp->ppm_data_size = l_cp->ppm_len;
4241 p_header_size -= l_remaining_data;
4242 p_header_data += l_remaining_data;
4245 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4246 l_cp->ppm_data_size += p_header_size;
4247 p_header_data += p_header_size;
4257 * Reads a PPT marker (Packed packet headers, tile-part header)
4259 * @param p_header_data the data contained in the PPT box.
4260 * @param p_j2k the jpeg2000 codec.
4261 * @param p_header_size the size of the data contained in the PPT marker.
4262 * @param p_manager the user event manager.
4264 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4265 OPJ_BYTE * p_header_data,
4266 OPJ_UINT32 p_header_size,
4267 struct opj_event_mgr * p_manager )
4269 opj_cp_v2_t *l_cp = 00;
4270 opj_tcp_v2_t *l_tcp = 00;
4274 assert(p_header_data != 00);
4275 assert(p_j2k != 00);
4276 assert(p_manager != 00);
4278 /* We need to have the Z_ppt element at minimum */
4279 if (p_header_size < 1) {
4280 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4284 l_cp = &(p_j2k->m_cp);
4286 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");
4290 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4293 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4297 /* Allocate buffer to read the packet header */
4299 /* First PPT marker */
4300 l_tcp->ppt_data_size = 0;
4301 l_tcp->ppt_len = p_header_size;
4303 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4304 if (l_tcp->ppt_buffer == 00) {
4305 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4308 l_tcp->ppt_data = l_tcp->ppt_buffer;
4310 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4313 l_tcp->ppt_len += p_header_size;
4315 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4316 if (l_tcp->ppt_buffer == 00) {
4317 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4320 l_tcp->ppt_data = l_tcp->ppt_buffer;
4322 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4325 /* Read packet header from buffer */
4326 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4328 l_tcp->ppt_data_size += p_header_size;
4334 * Writes the TLM marker (Tile Length Marker)
4336 * @param p_stream the stream to write data to.
4337 * @param p_j2k J2K codec.
4338 * @param p_manager the user event manager.
4340 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4341 struct opj_stream_private *p_stream,
4342 struct opj_event_mgr * p_manager )
4344 OPJ_BYTE * l_current_data = 00;
4345 OPJ_UINT32 l_tlm_size;
4348 assert(p_j2k != 00);
4349 assert(p_manager != 00);
4350 assert(p_stream != 00);
4352 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4354 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4355 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4356 = (OPJ_BYTE*)opj_realloc(
4357 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4360 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4364 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4367 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4369 /* change the way data is written to avoid seeking if possible */
4371 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4373 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4374 l_current_data += 2;
4376 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4377 l_current_data += 2;
4379 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4382 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4385 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4386 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) {
4393 static void j2k_write_tlm(opj_j2k_t *j2k){
4395 opj_cio_t *cio = j2k->cio;
4396 j2k->tlm_start = cio_tell(cio);
4397 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4398 lenp = 4 + (5*j2k->totnum_tp);
4399 cio_write(cio,lenp,2); /* Ltlm */
4400 cio_write(cio, 0,1); /* Ztlm=0*/
4401 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4402 cio_skip(cio,5*j2k->totnum_tp);
4405 static void j2k_write_sot(opj_j2k_t *j2k) {
4408 opj_cio_t *cio = j2k->cio;
4410 j2k->sot_start = cio_tell(cio);
4411 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4412 lenp = cio_tell(cio);
4413 cio_skip(cio, 2); /* Lsot (further) */
4414 cio_write(cio, j2k->curtileno, 2); /* Isot */
4415 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4416 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4417 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4418 len = cio_tell(cio) - lenp;
4419 cio_seek(cio, lenp);
4420 cio_write(cio, len, 2); /* Lsot */
4421 cio_seek(cio, lenp + len);
4425 /* update markers struct */
4426 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4427 #endif /* USE_JPWL */
4430 if( j2k->cstr_info && j2k->cur_tp_num==0){
4431 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4436 * Writes the SOT marker (Start of tile-part)
4438 * @param p_stream the stream to write data to.
4439 * @param p_j2k J2K codec.
4440 * @param p_manager the user event manager.
4442 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4444 OPJ_UINT32 * p_data_written,
4445 const struct opj_stream_private *p_stream,
4446 struct opj_event_mgr * p_manager )
4449 assert(p_j2k != 00);
4450 assert(p_manager != 00);
4451 assert(p_stream != 00);
4453 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4456 opj_write_bytes(p_data,10,2); /* Lsot */
4459 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4465 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4468 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4473 /* update markers struct */
4475 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4477 assert( 0 && "TODO" );
4478 #endif /* USE_JPWL */
4480 * p_data_written = 12;
4489 * Reads a PPT marker (Packed packet headers, tile-part header)
4491 * @param p_header_data the data contained in the PPT box.
4492 * @param p_j2k the jpeg2000 codec.
4493 * @param p_header_size the size of the data contained in the PPT marker.
4494 * @param p_manager the user event manager.
4496 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4497 OPJ_BYTE * p_header_data,
4498 OPJ_UINT32 p_header_size,
4499 opj_event_mgr_t * p_manager )
4501 opj_cp_v2_t *l_cp = 00;
4502 opj_tcp_v2_t *l_tcp = 00;
4503 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4504 OPJ_UINT32 l_current_part;
4505 OPJ_UINT32 l_tile_x,l_tile_y;
4508 assert(p_header_data != 00);
4509 assert(p_j2k != 00);
4510 assert(p_manager != 00);
4512 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4513 if (p_header_size != 8) {
4514 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4518 l_cp = &(p_j2k->m_cp);
4519 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4522 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4523 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4524 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4527 if (l_cp->correct) {
4529 int tileno = p_j2k->m_current_tile_number;
4530 static int backup_tileno = 0;
4532 /* tileno is negative or larger than the number of tiles!!! */
4533 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4534 opj_event_msg_v2(p_manager, EVT_ERROR,
4535 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4536 tileno, (l_cp->tw * l_cp->th));
4538 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4541 /* we try to correct */
4542 tileno = backup_tileno;
4543 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4544 "- setting tile number to %d\n",
4548 /* keep your private count of tiles */
4551 #endif /* USE_JPWL */
4553 /* look for the tile in the list of already processed tile (in parts). */
4554 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4555 /* since the time taken by this function can only grow at the time */
4557 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4560 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4561 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4563 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4569 if (l_cp->correct) {
4571 /* totlen is negative or larger than the bytes left!!! */
4572 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))) { */
4573 opj_event_msg_v2(p_manager, EVT_ERROR,
4574 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4575 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4577 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4580 /* we try to correct */
4582 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4583 "- setting Psot to %d => assuming it is the last tile\n",
4587 #endif /* USE_JPWL */
4589 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4591 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4592 "we assuming it is the last tile-part of the codestream.\n");
4593 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4596 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4599 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4602 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4603 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4604 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4605 if (l_tcp->m_nb_tile_parts) {
4606 if (l_current_part >= l_tcp->m_nb_tile_parts){
4607 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4608 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4609 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4613 l_tcp->m_nb_tile_parts = l_num_parts;
4616 /* If know the number of tile part header we will check if we didn't read the last*/
4617 if (l_tcp->m_nb_tile_parts) {
4618 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4619 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4623 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4624 /* Keep the size of data to skip after this marker */
4625 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4628 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4629 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4632 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4634 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4635 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4636 p_j2k->m_specific_param.m_decoder.m_skip_data =
4637 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4638 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4639 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4640 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4643 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4644 p_j2k->m_specific_param.m_decoder.m_skip_data =
4645 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4649 if (p_j2k->cstr_index)
4651 assert(p_j2k->cstr_index->tile_index != 00);
4652 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4653 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4655 if (l_num_parts != 0){
4656 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4657 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4660 if (!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].tp_index =
4662 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4664 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4665 (opj_tp_index_t*)opj_realloc(
4666 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4667 l_num_parts* sizeof(opj_tp_index_t));
4670 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4672 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4673 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4674 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4675 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4676 sizeof(opj_tp_index_t));
4679 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4680 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4681 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4682 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4683 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4684 * sizeof(opj_tp_index_t));
4693 /* 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 */
4694 /* if (p_j2k->cstr_info) {
4697 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4700 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4701 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4702 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4703 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4706 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4709 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4713 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4716 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4717 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4718 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4723 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4726 opj_tcp_t *tcp = NULL;
4727 opj_codestream_info_t *cstr_info = NULL;
4729 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4730 opj_cp_t *cp = j2k->cp;
4731 opj_cio_t *cio = j2k->cio;
4733 tcd->tp_num = j2k->tp_num ;
4734 tcd->cur_tp_num = j2k->cur_tp_num;
4736 cio_write(cio, J2K_MS_SOD, 2);
4738 if( j2k->cstr_info && j2k->cur_tp_num==0){
4739 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4742 if (j2k->curtileno == 0) {
4743 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4747 cstr_info = j2k->cstr_info;
4749 if (!j2k->cur_tp_num ) {
4750 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4751 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4754 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4755 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4759 /* update markers struct */
4760 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4761 #endif /* USE_JPWL */
4766 tcp = &cp->tcps[j2k->curtileno];
4767 for (layno = 0; layno < tcp->numlayers; layno++) {
4768 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4769 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4770 } else if (tcp->rates[layno]) {
4771 tcp->rates[layno]=1;
4774 if(j2k->cur_tp_num == 0){
4775 tcd->tcd_image->tiles->packno = 0;
4777 cstr_info->packno = 0;
4780 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4782 /* Writing Psot in SOT marker */
4783 totlen = cio_tell(cio) + l - j2k->sot_start;
4784 cio_seek(cio, j2k->sot_start + 6);
4785 cio_write(cio, totlen, 4);
4786 cio_seek(cio, j2k->sot_start + totlen);
4787 /* Writing Ttlm and Ptlm in TLM marker */
4789 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4790 cio_write(cio, j2k->curtileno, 1);
4791 cio_write(cio, totlen, 4);
4793 cio_seek(cio, j2k->sot_start + totlen);
4797 * Writes the SOD marker (Start of data)
4799 * @param p_stream the stream to write data to.
4800 * @param p_j2k J2K codec.
4801 * @param p_manager the user event manager.
4803 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4804 struct opj_tcd_v2 * p_tile_coder,
4806 OPJ_UINT32 * p_data_written,
4807 OPJ_UINT32 p_total_data_size,
4808 const struct opj_stream_private *p_stream,
4809 struct opj_event_mgr * p_manager )
4811 opj_tcp_v2_t *l_tcp = 00;
4812 opj_codestream_info_t *l_cstr_info = 00;
4813 opj_cp_v2_t *l_cp = 00;
4815 OPJ_UINT32 l_size_tile;
4816 OPJ_UINT32 l_remaining_data;
4819 assert(p_j2k != 00);
4820 assert(p_manager != 00);
4821 assert(p_stream != 00);
4823 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4826 /* make room for the EOF marker */
4827 l_remaining_data = p_total_data_size - 4;
4829 l_cp = &(p_j2k->m_cp);
4830 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4833 /* update tile coder */
4834 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4835 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4837 l_size_tile = l_cp->th * l_cp->tw;
4840 /* TODO mergeV2: check this part which use cstr_info */
4841 /*l_cstr_info = p_j2k->cstr_info;
4843 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4844 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4845 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4851 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4853 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4858 /* update markers struct */
4859 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4861 assert( 0 && "TODO" );
4862 #endif /* USE_JPWL */
4867 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4868 p_tile_coder->tcd_image->tiles->packno = 0;
4870 l_cstr_info->packno = 0;
4874 *p_data_written = 0;
4876 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)) {
4877 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4881 *p_data_written += 2;
4890 * Reads a SOD marker (Start Of Data)
4892 * @param p_header_data the data contained in the SOD box.
4893 * @param p_j2k the jpeg2000 codec.
4894 * @param p_header_size the size of the data contained in the SOD marker.
4895 * @param p_manager the user event manager.
4897 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4898 opj_stream_private_t *p_stream,
4899 opj_event_mgr_t * p_manager
4902 OPJ_UINT32 l_current_read_size;
4903 opj_codestream_index_t * l_cstr_index = 00;
4904 OPJ_BYTE ** l_current_data = 00;
4905 opj_tcp_v2_t * l_tcp = 00;
4906 OPJ_UINT32 * l_tile_len = 00;
4909 assert(p_j2k != 00);
4910 assert(p_manager != 00);
4911 assert(p_stream != 00);
4913 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4915 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4916 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4917 // but we are in the last tile part,
4918 // so its result will fit on OPJ_UINT32 unless we find
4919 // a file with a single tile part of more than 4 GB...*/
4920 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4923 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4925 l_current_data = &(l_tcp->m_data);
4926 l_tile_len = &l_tcp->m_data_size;
4928 if (! *l_current_data) {
4929 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4932 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4935 if (*l_current_data == 00) {
4936 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4942 l_cstr_index = p_j2k->cstr_index;
4944 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4946 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4947 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4949 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4950 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4952 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4956 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4958 /*l_cstr_index->packno = 0;*/
4961 l_current_read_size = opj_stream_read_data( p_stream,
4962 *l_current_data + *l_tile_len,
4963 p_j2k->m_specific_param.m_decoder.m_sot_length,
4966 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4967 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4970 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4973 *l_tile_len += l_current_read_size;
4979 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4980 opj_cp_t *cp = j2k->cp;
4981 opj_tcp_t *tcp = &cp->tcps[tileno];
4982 opj_cio_t *cio = j2k->cio;
4983 int numcomps = j2k->image->numcomps;
4985 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4986 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4987 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4988 cio_write(cio, 0, 1); /* Srgn */
4989 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4993 * Writes the RGN marker (Region Of Interest)
4995 * @param p_tile_no the tile to output
4996 * @param p_comp_no the component to output
4997 * @param p_stream the stream to write data to.
4998 * @param p_j2k J2K codec.
4999 * @param p_manager the user event manager.
5001 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5002 OPJ_UINT32 p_tile_no,
5003 OPJ_UINT32 p_comp_no,
5004 struct opj_stream_private *p_stream,
5005 struct opj_event_mgr * p_manager )
5007 OPJ_BYTE * l_current_data = 00;
5008 OPJ_UINT32 l_nb_comp;
5009 OPJ_UINT32 l_rgn_size;
5010 opj_image_t *l_image = 00;
5011 opj_cp_v2_t *l_cp = 00;
5012 opj_tcp_v2_t *l_tcp = 00;
5013 opj_tccp_t *l_tccp = 00;
5014 OPJ_UINT32 l_comp_room;
5017 assert(p_j2k != 00);
5018 assert(p_manager != 00);
5019 assert(p_stream != 00);
5021 l_cp = &(p_j2k->m_cp);
5022 l_tcp = &l_cp->tcps[p_tile_no];
5023 l_tccp = &l_tcp->tccps[p_comp_no];
5025 l_nb_comp = l_image->numcomps;
5027 if (l_nb_comp <= 256) {
5034 l_rgn_size = 6 + l_comp_room;
5036 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5038 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5039 l_current_data += 2;
5041 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5042 l_current_data += 2;
5044 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5045 l_current_data+=l_comp_room;
5047 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5050 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5053 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) {
5061 static void j2k_write_eoc(opj_j2k_t *j2k) {
5062 opj_cio_t *cio = j2k->cio;
5063 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5064 cio_write(cio, J2K_MS_EOC, 2);
5068 /* update markers struct */
5069 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5070 #endif /* USE_JPWL */
5075 * Writes the EOC marker (End of Codestream)
5077 * @param p_stream the stream to write data to.
5078 * @param p_j2k J2K codec.
5079 * @param p_manager the user event manager.
5081 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5082 struct opj_stream_private *p_stream,
5083 struct opj_event_mgr * p_manager )
5086 assert(p_j2k != 00);
5087 assert(p_manager != 00);
5088 assert(p_stream != 00);
5090 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5095 /* update markers struct */
5097 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5099 #endif /* USE_JPWL */
5101 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5105 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5114 * Reads a RGN marker (Region Of Interest)
5116 * @param p_header_data the data contained in the POC box.
5117 * @param p_j2k the jpeg2000 codec.
5118 * @param p_header_size the size of the data contained in the POC marker.
5119 * @param p_manager the user event manager.
5121 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5122 OPJ_BYTE * p_header_data,
5123 OPJ_UINT32 p_header_size,
5124 opj_event_mgr_t * p_manager
5127 OPJ_UINT32 l_nb_comp;
5128 opj_image_t * l_image = 00;
5130 opj_cp_v2_t *l_cp = 00;
5131 opj_tcp_v2_t *l_tcp = 00;
5132 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5135 assert(p_header_data != 00);
5136 assert(p_j2k != 00);
5137 assert(p_manager != 00);
5139 l_image = p_j2k->m_private_image;
5140 l_nb_comp = l_image->numcomps;
5142 if (l_nb_comp <= 256) {
5147 if (p_header_size != 2 + l_comp_room) {
5148 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5152 l_cp = &(p_j2k->m_cp);
5153 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5154 &l_cp->tcps[p_j2k->m_current_tile_number] :
5155 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5157 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5158 p_header_data+=l_comp_room;
5159 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5163 if (l_cp->correct) {
5164 /* totlen is negative or larger than the bytes left!!! */
5165 if (l_comp_room >= l_nb_comp) {
5166 opj_event_msg_v2(p_manager, EVT_ERROR,
5167 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5168 l_comp_room, l_nb_comp);
5169 if (!JPWL_ASSUME || JPWL_ASSUME) {
5170 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5175 #endif /* USE_JPWL */
5177 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5184 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5186 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5189 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5196 * Updates the rates of the tcp.
5198 * @param p_stream the stream to write data to.
5199 * @param p_j2k J2K codec.
5200 * @param p_manager the user event manager.
5202 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5203 struct opj_stream_private *p_stream,
5204 struct opj_event_mgr * p_manager )
5206 opj_cp_v2_t * l_cp = 00;
5207 opj_image_t * l_image = 00;
5208 opj_tcp_v2_t * l_tcp = 00;
5209 opj_image_comp_t * l_img_comp = 00;
5212 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5213 OPJ_FLOAT32 * l_rates = 0;
5214 OPJ_FLOAT32 l_sot_remove;
5215 OPJ_UINT32 l_bits_empty, l_size_pixel;
5216 OPJ_UINT32 l_tile_size = 0;
5217 OPJ_UINT32 l_last_res;
5218 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5221 assert(p_j2k != 00);
5222 assert(p_manager != 00);
5223 assert(p_stream != 00);
5226 l_cp = &(p_j2k->m_cp);
5227 l_image = p_j2k->m_private_image;
5230 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5231 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5232 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5234 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5235 l_tp_stride_func = get_tp_stride;
5238 l_tp_stride_func = get_default_stride;
5241 for (i=0;i<l_cp->th;++i) {
5242 for (j=0;j<l_cp->tw;++j) {
5243 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5245 /* 4 borders of the tile rescale on the image if necessary */
5246 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5247 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5248 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5249 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5251 l_rates = l_tcp->rates;
5253 /* Modification of the RATE >> */
5255 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5257 ((*l_rates) * l_bits_empty)
5265 for (k = 1; k < l_tcp->numlayers; ++k) {
5267 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5269 ((*l_rates) * l_bits_empty)
5285 for (i=0;i<l_cp->th;++i) {
5286 for (j=0;j<l_cp->tw;++j) {
5287 l_rates = l_tcp->rates;
5290 *l_rates -= l_sot_remove;
5292 if (*l_rates < 30) {
5299 l_last_res = l_tcp->numlayers - 1;
5301 for (k = 1; k < l_last_res; ++k) {
5304 *l_rates -= l_sot_remove;
5306 if (*l_rates < *(l_rates - 1) + 10) {
5307 *l_rates = (*(l_rates - 1)) + 20;
5315 *l_rates -= (l_sot_remove + 2.f);
5317 if (*l_rates < *(l_rates - 1) + 10) {
5318 *l_rates = (*(l_rates - 1)) + 20;
5326 l_img_comp = l_image->comps;
5329 for (i=0;i<l_image->numcomps;++i) {
5330 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5332 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5340 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5342 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5344 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5345 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5346 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5347 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5351 if (l_cp->m_specific_param.m_enc.m_cinema) {
5352 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5353 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5354 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5358 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5359 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5365 static void j2k_read_eoc(opj_j2k_t *j2k) {
5367 opj_bool success = OPJ_FALSE;
5369 /* if packets should be decoded */
5370 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5371 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5372 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5373 for (i = 0; i < j2k->cp->tileno_size; i++) {
5374 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5375 if (j2k->cp->tileno[i] != -1)
5377 tileno = j2k->cp->tileno[i];
5378 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5379 opj_free(j2k->tile_data[tileno]);
5380 j2k->tile_data[tileno] = NULL;
5381 tcd_free_decode_tile(tcd, i);
5384 success = OPJ_FALSE;
5385 if (success == OPJ_FALSE) {
5386 j2k->state |= J2K_STATE_ERR;
5390 tcd_free_decode(tcd);
5393 /* if packets should not be decoded */
5395 for (i = 0; i < j2k->cp->tileno_size; i++) {
5396 tileno = j2k->cp->tileno[i];
5397 opj_free(j2k->tile_data[tileno]);
5398 j2k->tile_data[tileno] = NULL;
5401 if (j2k->state & J2K_STATE_ERR)
5402 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5404 j2k->state = J2K_STATE_MT;
5408 * Reads a EOC marker (End Of Codestream)
5410 * @param p_header_data the data contained in the SOD box.
5411 * @param p_j2k the jpeg2000 codec.
5412 * @param p_header_size the size of the data contained in the SOD marker.
5413 * @param p_manager the user event manager.
5416 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5417 struct opj_stream_private *p_stream,
5418 struct opj_event_mgr * p_manager )
5421 opj_tcd_v2_t * l_tcd = 00;
5422 OPJ_UINT32 l_nb_tiles;
5423 opj_tcp_v2_t * l_tcp = 00;
5427 assert(p_j2k != 00);
5428 assert(p_manager != 00);
5429 assert(p_stream != 00);
5431 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5432 l_tcp = p_j2k->m_cp.tcps;
5434 l_tcd = tcd_create_v2(OPJ_TRUE);
5436 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5440 for (i = 0; i < l_nb_tiles; ++i) {
5441 if (l_tcp->m_data) {
5442 if (! tcd_init_decode_tile(l_tcd, i)) {
5443 tcd_destroy_v2(l_tcd);
5444 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5448 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5452 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5457 j2k_tcp_destroy(l_tcp);
5461 tcd_destroy_v2(l_tcd);
5467 * Gets the offset of the header.
5469 * @param p_stream the stream to write data to.
5470 * @param p_j2k J2K codec.
5471 * @param p_manager the user event manager.
5473 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5474 struct opj_stream_private *p_stream,
5475 struct opj_event_mgr * p_manager )
5478 assert(p_j2k != 00);
5479 assert(p_manager != 00);
5480 assert(p_stream != 00);
5482 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5488 * Writes the MCT marker (Multiple Component Transform)
5490 * @param p_stream the stream to write data to.
5491 * @param p_j2k J2K codec.
5492 * @param p_manager the user event manager.
5494 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5495 struct opj_stream_private *p_stream,
5496 struct opj_event_mgr * p_manager )
5499 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5500 opj_mct_data_t * l_mct_record;
5501 opj_tcp_v2_t * l_tcp;
5504 assert(p_j2k != 00);
5505 assert(p_stream != 00);
5506 assert(p_manager != 00);
5508 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5512 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5513 l_mct_record = l_tcp->m_mct_records;
5515 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5517 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5524 l_mcc_record = l_tcp->m_mcc_records;
5526 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5528 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5535 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5543 * Writes the image components.
5545 * @param p_stream the stream to write data to.
5546 * @param p_j2k J2K codec.
5547 * @param p_manager the user event manager.
5549 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5550 struct opj_stream_private *p_stream,
5551 struct opj_event_mgr * p_manager )
5556 assert(p_j2k != 00);
5557 assert(p_manager != 00);
5558 assert(p_stream != 00);
5560 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5562 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5566 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5575 * Writes regions of interests.
5577 * @param p_stream the stream to write data to.
5578 * @param p_j2k J2K codec.
5579 * @param p_manager the user event manager.
5581 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5582 struct opj_stream_private *p_stream,
5583 struct opj_event_mgr * p_manager )
5586 const opj_tccp_t *l_tccp = 00;
5589 assert(p_j2k != 00);
5590 assert(p_manager != 00);
5591 assert(p_stream != 00);
5593 l_tccp = p_j2k->m_cp.tcps->tccps;
5595 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5596 if (l_tccp->roishift) {
5598 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5612 * @param p_stream the stream to write data to.
5613 * @param p_j2k J2K codec.
5614 * @param p_manager the user event manager.
5616 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5617 struct opj_stream_private *p_stream,
5618 struct opj_event_mgr * p_manager )
5620 opj_codestream_index_t * l_cstr_index = 00;
5623 assert(p_j2k != 00);
5624 assert(p_manager != 00);
5625 assert(p_stream != 00);
5627 l_cstr_index = p_j2k->cstr_index;
5629 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5631 /* The following adjustment is done to adjust the codestream size */
5632 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5633 /* the first bunch of bytes is not in the codestream */
5634 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5639 /* preparation of JPWL marker segments */
5643 /* encode according to JPWL */
5644 jpwl_encode(p_j2k, p_stream, image);
5648 assert( 0 && "TODO" );
5649 #endif /* USE_JPWL */
5654 typedef struct opj_dec_mstabent {
5657 /** value of the state when the marker can appear */
5659 /** action linked to the marker */
5660 void (*handler) (opj_j2k_t *j2k);
5661 } opj_dec_mstabent_t;
5663 opj_dec_mstabent_t j2k_dec_mstab[] = {
5664 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5665 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5666 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5667 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5668 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5669 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5670 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5671 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5672 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5673 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5674 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5675 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5676 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5677 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5678 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5679 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5681 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
5682 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
5685 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5686 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5687 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5688 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5689 #endif /* USE_JPWL */
5691 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5692 {J2K_MS_INSEC, 0, j2k_read_insec},
5693 #endif /* USE_JPSEC */
5695 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5698 static void j2k_read_unk(opj_j2k_t *j2k) {
5699 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5702 if (j2k->cp->correct) {
5704 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5705 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5706 id = cio_read(j2k->cio, 2);
5707 opj_event_msg(j2k->cinfo, EVT_ERROR,
5708 "JPWL: really don't know this marker %x\n",
5711 opj_event_msg(j2k->cinfo, EVT_ERROR,
5712 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5715 /* OK, activate this at your own risk!!! */
5716 /* we look for the marker at the minimum hamming distance from this */
5717 while (j2k_dec_mstab[m].id) {
5719 /* 1's where they differ */
5720 tmp_id = j2k_dec_mstab[m].id ^ id;
5722 /* compute the hamming distance between our id and the current */
5724 for (i = 0; i < 16; i++) {
5725 if ((tmp_id >> i) & 0x0001) {
5730 /* if current distance is smaller, set the minimum */
5731 if (cur_dist < min_dist) {
5732 min_dist = cur_dist;
5733 min_id = j2k_dec_mstab[m].id;
5736 /* jump to the next marker */
5740 /* do we substitute the marker? */
5741 if (min_dist < JPWL_MAXIMUM_HAMMING) {
5742 opj_event_msg(j2k->cinfo, EVT_ERROR,
5743 "- marker %x is at distance %d from the read %x\n",
5744 min_id, min_dist, id);
5745 opj_event_msg(j2k->cinfo, EVT_ERROR,
5746 "- trying to substitute in place and crossing fingers!\n");
5747 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5748 cio_write(j2k->cio, min_id, 2);
5751 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5756 #endif /* USE_JPWL */
5761 * Reads an unknown marker
5763 * @param p_stream the stream object to read from.
5764 * @param p_j2k the jpeg2000 codec.
5765 * @param p_manager the user event manager.
5767 * @return true if the marker could be deduced.
5769 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
5770 struct opj_stream_private *p_stream,
5771 OPJ_UINT32 *output_marker,
5772 struct opj_event_mgr * p_manager
5775 OPJ_UINT32 l_unknown_marker;
5776 const opj_dec_memory_marker_handler_t * l_marker_handler;
5777 OPJ_UINT32 l_size_unk = 2;
5780 assert(p_j2k != 00);
5781 assert(p_manager != 00);
5782 assert(p_stream != 00);
5784 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5787 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5788 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5789 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5793 /* read 2 bytes as the new marker ID*/
5794 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5796 if (!(l_unknown_marker < 0xff00)) {
5798 /* Get the marker handler from the marker ID*/
5799 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5801 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5802 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5806 if (l_marker_handler->id != J2K_MS_UNK) {
5807 /* Add the marker to the codestream index*/
5808 if (l_marker_handler->id != J2K_MS_SOT)
5809 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5810 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5812 break; /* next marker is known and well located */
5820 *output_marker = l_marker_handler->id ;
5826 Read the lookup table containing all the marker, status and action
5827 @param id Marker value
5829 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5830 opj_dec_mstabent_t *e;
5831 for (e = j2k_dec_mstab; e->id != 0; e++) {
5840 * Writes the MCT marker (Multiple Component Transform)
5842 * @param p_stream the stream to write data to.
5843 * @param p_j2k J2K codec.
5844 * @param p_manager the user event manager.
5846 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5847 opj_mct_data_t * p_mct_record,
5848 struct opj_stream_private *p_stream,
5849 struct opj_event_mgr * p_manager )
5851 OPJ_UINT32 l_mct_size;
5852 OPJ_BYTE * l_current_data = 00;
5856 assert(p_j2k != 00);
5857 assert(p_manager != 00);
5858 assert(p_stream != 00);
5860 l_mct_size = 10 + p_mct_record->m_data_size;
5862 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5863 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5864 = (OPJ_BYTE*)opj_realloc(
5865 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5868 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5872 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5875 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5877 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5878 l_current_data += 2;
5880 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5881 l_current_data += 2;
5883 opj_write_bytes(l_current_data,0,2); /* Zmct */
5884 l_current_data += 2;
5886 /* only one marker atm */
5887 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5889 opj_write_bytes(l_current_data,l_tmp,2);
5890 l_current_data += 2;
5892 opj_write_bytes(l_current_data,0,2); /* Ymct */
5895 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5897 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) {
5905 * Reads a MCT marker (Multiple Component Transform)
5907 * @param p_header_data the data contained in the MCT box.
5908 * @param p_j2k the jpeg2000 codec.
5909 * @param p_header_size the size of the data contained in the MCT marker.
5910 * @param p_manager the user event manager.
5912 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5913 OPJ_BYTE * p_header_data,
5914 OPJ_UINT32 p_header_size,
5915 struct opj_event_mgr * p_manager )
5918 opj_tcp_v2_t *l_tcp = 00;
5921 opj_mct_data_t * l_mct_data;
5924 assert(p_header_data != 00);
5925 assert(p_j2k != 00);
5927 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5928 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5929 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5931 if (p_header_size < 2) {
5932 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5937 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5940 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5944 if(p_header_size <= 6) {
5945 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5949 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5950 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5953 l_indix = l_tmp & 0xff;
5954 l_mct_data = l_tcp->m_mct_records;
5956 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5957 if (l_mct_data->m_index == l_indix) {
5964 if (i == l_tcp->m_nb_mct_records) {
5965 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5966 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5968 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));
5969 if(! l_tcp->m_mct_records) {
5970 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5974 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5975 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5978 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5981 if (l_mct_data->m_data) {
5982 opj_free(l_mct_data->m_data);
5983 l_mct_data->m_data = 00;
5986 l_mct_data->m_index = l_indix;
5987 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5988 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5990 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5993 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5999 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6000 if (! l_mct_data->m_data) {
6001 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6004 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6006 l_mct_data->m_data_size = p_header_size;
6007 ++l_tcp->m_nb_mct_records;
6013 * Writes the MCC marker (Multiple Component Collection)
6015 * @param p_stream the stream to write data to.
6016 * @param p_j2k J2K codec.
6017 * @param p_manager the user event manager.
6019 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6020 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6021 struct opj_stream_private *p_stream,
6022 struct opj_event_mgr * p_manager )
6025 OPJ_UINT32 l_mcc_size;
6026 OPJ_BYTE * l_current_data = 00;
6027 OPJ_UINT32 l_nb_bytes_for_comp;
6032 assert(p_j2k != 00);
6033 assert(p_manager != 00);
6034 assert(p_stream != 00);
6036 if (p_mcc_record->m_nb_comps > 255 ) {
6037 l_nb_bytes_for_comp = 2;
6041 l_nb_bytes_for_comp = 1;
6045 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6046 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6048 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6049 = (OPJ_BYTE*)opj_realloc(
6050 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6052 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6056 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6059 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6061 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6062 l_current_data += 2;
6064 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6065 l_current_data += 2;
6068 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6069 l_current_data += 2;
6071 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6074 /* only one marker atm */
6075 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6078 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6081 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6084 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 */
6087 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6088 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6089 l_current_data+=l_nb_bytes_for_comp;
6092 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 */
6095 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6097 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6098 l_current_data+=l_nb_bytes_for_comp;
6101 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6103 if (p_mcc_record->m_decorrelation_array) {
6104 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6107 if (p_mcc_record->m_offset_array) {
6108 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6111 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6114 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) {
6122 * Reads a MCC marker (Multiple Component Collection)
6124 * @param p_header_data the data contained in the MCC box.
6125 * @param p_j2k the jpeg2000 codec.
6126 * @param p_header_size the size of the data contained in the MCC marker.
6127 * @param p_manager the user event manager.
6129 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6130 OPJ_BYTE * p_header_data,
6131 OPJ_UINT32 p_header_size,
6132 struct opj_event_mgr * p_manager )
6137 opj_tcp_v2_t * l_tcp;
6138 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6139 opj_mct_data_t * l_mct_data;
6140 OPJ_UINT32 l_nb_collections;
6141 OPJ_UINT32 l_nb_comps;
6142 OPJ_UINT32 l_nb_bytes_by_comp;
6146 assert(p_header_data != 00);
6147 assert(p_j2k != 00);
6148 assert(p_manager != 00);
6150 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6151 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6152 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6154 if (p_header_size < 2) {
6155 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6160 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6163 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6167 if (p_header_size < 7) {
6168 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6172 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6175 l_mcc_record = l_tcp->m_mcc_records;
6177 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6178 if (l_mcc_record->m_index == l_indix) {
6185 if (i == l_tcp->m_nb_mcc_records) {
6186 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6187 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6189 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6190 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6191 if (! l_tcp->m_mcc_records) {
6192 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6195 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6196 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));
6198 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6200 l_mcc_record->m_index = l_indix;
6202 /* only one marker atm */
6203 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6206 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6210 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6213 if (l_nb_collections > 1) {
6214 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6220 for (i=0;i<l_nb_collections;++i) {
6221 if (p_header_size < 3) {
6222 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6226 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6230 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6234 opj_read_bytes(p_header_data,&l_nb_comps,2);
6239 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6240 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6242 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6243 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6247 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6249 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6250 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6251 p_header_data+=l_nb_bytes_by_comp;
6254 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6259 opj_read_bytes(p_header_data,&l_nb_comps,2);
6262 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6263 l_nb_comps &= 0x7fff;
6265 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6266 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6270 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6271 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6275 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6277 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6278 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6279 p_header_data+=l_nb_bytes_by_comp;
6282 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6287 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6290 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6291 l_mcc_record->m_decorrelation_array = 00;
6292 l_mcc_record->m_offset_array = 00;
6294 l_indix = l_tmp & 0xff;
6296 l_mct_data = l_tcp->m_mct_records;
6297 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6298 if (l_mct_data->m_index == l_indix) {
6299 l_mcc_record->m_decorrelation_array = l_mct_data;
6305 if (l_mcc_record->m_decorrelation_array == 00) {
6306 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6311 l_indix = (l_tmp >> 8) & 0xff;
6313 l_mct_data = l_tcp->m_mct_records;
6314 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6315 if (l_mct_data->m_index == l_indix) {
6316 l_mcc_record->m_offset_array = l_mct_data;
6322 if (l_mcc_record->m_offset_array == 00) {
6323 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6329 if (p_header_size != 0) {
6330 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6334 ++l_tcp->m_nb_mcc_records;
6341 * Writes the MCO marker (Multiple component transformation ordering)
6343 * @param p_stream the stream to write data to.
6344 * @param p_j2k J2K codec.
6345 * @param p_manager the user event manager.
6347 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6348 struct opj_stream_private *p_stream,
6349 struct opj_event_mgr * p_manager
6352 OPJ_BYTE * l_current_data = 00;
6353 OPJ_UINT32 l_mco_size;
6354 opj_tcp_v2_t * l_tcp = 00;
6355 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6359 assert(p_j2k != 00);
6360 assert(p_manager != 00);
6361 assert(p_stream != 00);
6363 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6364 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6366 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6367 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6369 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6370 = (OPJ_BYTE*)opj_realloc(
6371 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6373 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6378 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6381 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6382 l_current_data += 2;
6384 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6385 l_current_data += 2;
6387 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6390 l_mcc_record = l_tcp->m_mcc_records;
6391 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6392 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6398 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) {
6406 * Reads a MCO marker (Multiple Component Transform Ordering)
6408 * @param p_header_data the data contained in the MCO box.
6409 * @param p_j2k the jpeg2000 codec.
6410 * @param p_header_size the size of the data contained in the MCO marker.
6411 * @param p_manager the user event manager.
6413 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6414 OPJ_BYTE * p_header_data,
6415 OPJ_UINT32 p_header_size,
6416 struct opj_event_mgr * p_manager )
6418 OPJ_UINT32 l_tmp, i;
6419 OPJ_UINT32 l_nb_stages;
6420 opj_tcp_v2_t * l_tcp;
6421 opj_tccp_t * l_tccp;
6422 opj_image_t * l_image;
6423 opj_image_comp_t * l_img_comp;
6426 assert(p_header_data != 00);
6427 assert(p_j2k != 00);
6428 assert(p_manager != 00);
6430 l_image = p_j2k->m_private_image;
6431 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6432 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6433 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6435 if (p_header_size < 1) {
6436 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6440 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6443 if (l_nb_stages > 1) {
6444 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6448 if (p_header_size != l_nb_stages + 1) {
6449 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6453 l_tccp = l_tcp->tccps;
6454 l_img_comp = l_image->comps;
6456 for (i=0;i<l_image->numcomps;++i) {
6457 l_tccp->m_dc_level_shift = 0;
6461 if (l_tcp->m_mct_decoding_matrix) {
6462 opj_free(l_tcp->m_mct_decoding_matrix);
6463 l_tcp->m_mct_decoding_matrix = 00;
6466 for (i=0;i<l_nb_stages;++i) {
6467 opj_read_bytes(p_header_data,&l_tmp,1);
6470 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6478 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6481 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6482 opj_mct_data_t * l_deco_array, * l_offset_array;
6483 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6484 OPJ_UINT32 l_nb_elem;
6485 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6486 opj_tccp_t * l_tccp;
6489 assert(p_tcp != 00);
6491 l_mcc_record = p_tcp->m_mcc_records;
6493 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6494 if (l_mcc_record->m_index == p_index) {
6499 if (i==p_tcp->m_nb_mcc_records) {
6500 /** element discarded **/
6504 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6505 /** do not support number of comps != image */
6509 l_deco_array = l_mcc_record->m_decorrelation_array;
6512 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6513 if (l_deco_array->m_data_size != l_data_size) {
6517 l_nb_elem = p_image->numcomps * p_image->numcomps;
6518 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6519 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6521 if (! p_tcp->m_mct_decoding_matrix ) {
6525 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);
6528 l_offset_array = l_mcc_record->m_offset_array;
6530 if (l_offset_array) {
6531 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6532 if (l_offset_array->m_data_size != l_data_size) {
6536 l_nb_elem = p_image->numcomps;
6537 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6538 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6540 if (! l_offset_data ) {
6544 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6546 l_tccp = p_tcp->tccps;
6547 l_current_offset_data = l_offset_data;
6549 for (i=0;i<p_image->numcomps;++i) {
6550 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6554 opj_free(l_offset_data);
6561 * Writes the CBD marker (Component bit depth definition)
6563 * @param p_stream the stream to write data to.
6564 * @param p_j2k J2K codec.
6565 * @param p_manager the user event manager.
6567 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6568 struct opj_stream_private *p_stream,
6569 struct opj_event_mgr * p_manager )
6572 OPJ_UINT32 l_cbd_size;
6573 OPJ_BYTE * l_current_data = 00;
6574 opj_image_t *l_image = 00;
6575 opj_image_comp_t * l_comp = 00;
6578 assert(p_j2k != 00);
6579 assert(p_manager != 00);
6580 assert(p_stream != 00);
6582 l_image = p_j2k->m_private_image;
6583 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6585 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6586 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6587 = (OPJ_BYTE*)opj_realloc(
6588 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6591 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6595 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6598 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6600 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6601 l_current_data += 2;
6603 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6604 l_current_data += 2;
6606 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6609 l_comp = l_image->comps;
6611 for (i=0;i<l_image->numcomps;++i) {
6612 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6618 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) {
6626 * Reads a CBD marker (Component bit depth definition)
6627 * @param p_header_data the data contained in the CBD box.
6628 * @param p_j2k the jpeg2000 codec.
6629 * @param p_header_size the size of the data contained in the CBD marker.
6630 * @param p_manager the user event manager.
6632 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6633 OPJ_BYTE * p_header_data,
6634 OPJ_UINT32 p_header_size,
6635 struct opj_event_mgr * p_manager)
6637 OPJ_UINT32 l_nb_comp,l_num_comp;
6638 OPJ_UINT32 l_comp_def;
6640 opj_image_comp_t * l_comp = 00;
6643 assert(p_header_data != 00);
6644 assert(p_j2k != 00);
6645 assert(p_manager != 00);
6647 l_num_comp = p_j2k->m_private_image->numcomps;
6649 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6650 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6654 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6657 if (l_nb_comp != l_num_comp) {
6658 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6662 l_comp = p_j2k->m_private_image->comps;
6663 for (i=0;i<l_num_comp;++i) {
6664 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6666 l_comp->sgnd = (l_comp_def>>7) & 1;
6667 l_comp->prec = (l_comp_def&0x7f) + 1;
6675 /* ----------------------------------------------------------------------- */
6676 /* J2K / JPT decoder interface */
6677 /* ----------------------------------------------------------------------- */
6681 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6684 if(j2k->tile_len != NULL) {
6685 opj_free(j2k->tile_len);
6687 if(j2k->tile_data != NULL) {
6688 opj_free(j2k->tile_data);
6690 if(j2k->default_tcp != NULL) {
6691 opj_tcp_t *default_tcp = j2k->default_tcp;
6692 if(default_tcp->ppt_data_first != NULL) {
6693 opj_free(default_tcp->ppt_data_first);
6695 if(j2k->default_tcp->tccps != NULL) {
6696 opj_free(j2k->default_tcp->tccps);
6698 opj_free(j2k->default_tcp);
6700 if(j2k->cp != NULL) {
6701 opj_cp_t *cp = j2k->cp;
6702 if(cp->tcps != NULL) {
6703 for(i = 0; i < cp->tw * cp->th; i++) {
6704 if(cp->tcps[i].ppt_data_first != NULL) {
6705 opj_free(cp->tcps[i].ppt_data_first);
6707 if(cp->tcps[i].tccps != NULL) {
6708 opj_free(cp->tcps[i].tccps);
6713 if(cp->ppm_data_first != NULL) {
6714 opj_free(cp->ppm_data_first);
6716 if(cp->tileno != NULL) {
6717 opj_free(cp->tileno);
6719 if(cp->comment != NULL) {
6720 opj_free(cp->comment);
6729 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6731 if(j2k && parameters) {
6732 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6733 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6736 j2k->m_cp.correct = parameters->jpwl_correct;
6737 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6738 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6739 #endif /* USE_JPWL */
6746 * Read a JPT-stream and decode file
6749 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6750 opj_image_t *image = NULL;
6751 opj_jpt_msg_header_t header;
6753 opj_common_ptr cinfo = j2k->cinfo;
6755 OPJ_ARG_NOT_USED(cstr_info);
6759 /* create an empty image */
6760 image = opj_image_create0();
6763 j2k->state = J2K_STATE_MHSOC;
6765 /* Initialize the header */
6766 jpt_init_msg_header(&header);
6767 /* Read the first header of the message */
6768 jpt_read_msg_header(cinfo, cio, &header);
6770 position = cio_tell(cio);
6771 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6772 opj_image_destroy(image);
6773 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6778 opj_dec_mstabent_t *e = NULL;
6781 if (!cio_numbytesleft(cio)) {
6785 /* data-bin read -> need to read a new header */
6786 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6787 jpt_read_msg_header(cinfo, cio, &header);
6788 position = cio_tell(cio);
6789 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6790 opj_image_destroy(image);
6791 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6796 id = cio_read(cio, 2);
6797 if (id >> 8 != 0xff) {
6798 opj_image_destroy(image);
6799 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6802 e = j2k_dec_mstab_lookup(id);
6803 if (!(j2k->state & e->states)) {
6804 opj_image_destroy(image);
6805 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6811 if (j2k->state == J2K_STATE_MT) {
6814 if (j2k->state == J2K_STATE_NEOC) {
6818 if (j2k->state == J2K_STATE_NEOC) {
6822 if (j2k->state != J2K_STATE_MT) {
6823 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6829 /* ----------------------------------------------------------------------- */
6830 /* J2K encoder interface */
6831 /* ----------------------------------------------------------------------- */
6833 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6834 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6841 opj_j2k_v2_t* j2k_create_compress_v2(void)
6843 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6848 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6850 l_j2k->m_is_decoder = 0;
6851 l_j2k->m_cp.m_is_decoder = 0;
6853 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6854 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6859 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6861 /* validation list creation*/
6862 l_j2k->m_validation_list = opj_procedure_list_create();
6863 if (! l_j2k->m_validation_list) {
6868 /* execution list creation*/
6869 l_j2k->m_procedure_list = opj_procedure_list_create();
6870 if (! l_j2k->m_procedure_list) {
6878 void j2k_destroy_compress(opj_j2k_t *j2k) {
6882 if(j2k->cp != NULL) {
6883 opj_cp_t *cp = j2k->cp;
6886 opj_free(cp->comment);
6889 opj_free(cp->matrice);
6891 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6892 opj_free(cp->tcps[tileno].tccps);
6901 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6903 int tileno, numpocs_tile;
6904 opj_cp_t *cp = NULL;
6906 if(!j2k || !parameters || ! image) {
6910 /* create and initialize the coding parameters structure */
6911 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6913 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6916 /* set default values for cp */
6921 copy user encoding parameters
6923 cp->cinema = parameters->cp_cinema;
6924 cp->max_comp_size = parameters->max_comp_size;
6925 cp->rsiz = parameters->cp_rsiz;
6926 cp->disto_alloc = parameters->cp_disto_alloc;
6927 cp->fixed_alloc = parameters->cp_fixed_alloc;
6928 cp->fixed_quality = parameters->cp_fixed_quality;
6930 /* mod fixed_quality */
6931 if(parameters->cp_matrice) {
6932 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6933 cp->matrice = (int *) opj_malloc(array_size);
6934 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6938 cp->tdx = parameters->cp_tdx;
6939 cp->tdy = parameters->cp_tdy;
6942 cp->tx0 = parameters->cp_tx0;
6943 cp->ty0 = parameters->cp_ty0;
6945 /* comment string */
6946 if(parameters->cp_comment) {
6947 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6949 strcpy(cp->comment, parameters->cp_comment);
6954 calculate other encoding parameters
6957 if (parameters->tile_size_on) {
6958 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6959 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6961 cp->tdx = image->x1 - cp->tx0;
6962 cp->tdy = image->y1 - cp->ty0;
6965 if(parameters->tp_on){
6966 cp->tp_flag = parameters->tp_flag;
6971 for(i=0;i<image->numcomps ;i++){
6972 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6978 calculate JPWL encoding parameters
6981 if (parameters->jpwl_epc_on) {
6985 cp->epc_on = OPJ_TRUE;
6986 cp->info_on = OPJ_FALSE; /* no informative technique */
6989 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6990 cp->epb_on = OPJ_TRUE;
6992 cp->hprot_MH = parameters->jpwl_hprot_MH;
6993 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6994 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6995 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6997 /* if tile specs are not specified, copy MH specs */
6998 if (cp->hprot_TPH[0] == -1) {
6999 cp->hprot_TPH_tileno[0] = 0;
7000 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7002 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7003 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7004 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7005 cp->pprot[i] = parameters->jpwl_pprot[i];
7009 /* set ESD writing */
7010 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7011 cp->esd_on = OPJ_TRUE;
7013 cp->sens_size = parameters->jpwl_sens_size;
7014 cp->sens_addr = parameters->jpwl_sens_addr;
7015 cp->sens_range = parameters->jpwl_sens_range;
7017 cp->sens_MH = parameters->jpwl_sens_MH;
7018 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7019 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7020 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7024 /* always set RED writing to false: we are at the encoder */
7025 cp->red_on = OPJ_FALSE;
7028 cp->epc_on = OPJ_FALSE;
7030 #endif /* USE_JPWL */
7033 /* initialize the mutiple tiles */
7034 /* ---------------------------- */
7035 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7037 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7038 opj_tcp_t *tcp = &cp->tcps[tileno];
7039 tcp->numlayers = parameters->tcp_numlayers;
7040 assert ( tcp->numlayers >= 0 );
7041 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7043 if (cp->fixed_quality) {
7044 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7046 tcp->rates[j] = parameters->tcp_rates[j];
7048 if (cp->fixed_quality) { /* add fixed_quality */
7049 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7051 tcp->rates[j] = parameters->tcp_rates[j];
7055 tcp->csty = parameters->csty;
7056 tcp->prg = parameters->prog_order;
7057 tcp->mct = parameters->tcp_mct;
7061 if (parameters->numpocs) {
7062 /* initialisation of POC */
7064 assert( parameters->numpocs >= 0 );
7065 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7066 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7067 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7068 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7069 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7070 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7071 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7072 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7073 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7074 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7078 tcp->numpocs = numpocs_tile -1 ;
7083 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7085 for (i = 0; i < image->numcomps; i++) {
7086 opj_tccp_t *tccp = &tcp->tccps[i];
7087 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7088 tccp->numresolutions = parameters->numresolution;
7089 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7090 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7091 tccp->cblksty = parameters->mode;
7092 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7093 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7095 assert(parameters->roi_compno >= 0);
7096 if (i == (OPJ_UINT32)parameters->roi_compno) {
7097 tccp->roishift = parameters->roi_shift;
7102 if(parameters->cp_cinema)
7104 /*Precinct size for lowest frequency subband=128*/
7107 /*Precinct size at all other resolutions = 256*/
7108 for (j = 1; j < tccp->numresolutions; j++) {
7113 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7115 assert(tccp->numresolutions > 0);
7116 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7117 if (p < parameters->res_spec) {
7119 if (parameters->prcw_init[p] < 1) {
7122 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7125 if (parameters->prch_init[p] < 1) {
7128 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7132 int res_spec = parameters->res_spec;
7133 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7134 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7136 if (size_prcw < 1) {
7139 tccp->prcw[j] = int_floorlog2(size_prcw);
7142 if (size_prch < 1) {
7145 tccp->prch[j] = int_floorlog2(size_prch);
7149 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7152 for (j = 0; j < tccp->numresolutions; j++) {
7159 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7164 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7165 opj_cparameters_t *parameters,
7167 struct opj_event_mgr * p_manager)
7169 OPJ_UINT32 i, j, tileno, numpocs_tile;
7170 opj_cp_v2_t *cp = 00;
7173 if(!p_j2k || !parameters || ! image) {
7177 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7178 cp = &(p_j2k->m_cp);
7180 /* set default values for cp */
7185 copy user encoding parameters
7187 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7188 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7189 cp->rsiz = parameters->cp_rsiz;
7190 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7191 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7192 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7194 /* mod fixed_quality */
7195 if (parameters->cp_matrice) {
7196 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7197 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7198 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7202 cp->tdx = parameters->cp_tdx;
7203 cp->tdy = parameters->cp_tdy;
7206 cp->tx0 = parameters->cp_tx0;
7207 cp->ty0 = parameters->cp_ty0;
7209 /* comment string */
7210 if(parameters->cp_comment) {
7211 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7213 strcpy(cp->comment, parameters->cp_comment);
7218 calculate other encoding parameters
7221 if (parameters->tile_size_on) {
7222 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7223 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7225 cp->tdx = image->x1 - cp->tx0;
7226 cp->tdy = image->y1 - cp->ty0;
7229 if (parameters->tp_on) {
7230 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7231 cp->m_specific_param.m_enc.m_tp_on = 1;
7236 calculate JPWL encoding parameters
7239 if (parameters->jpwl_epc_on) {
7243 cp->epc_on = OPJ_TRUE;
7244 cp->info_on = OPJ_FALSE; /* no informative technique */
7247 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7248 cp->epb_on = OPJ_TRUE;
7250 cp->hprot_MH = parameters->jpwl_hprot_MH;
7251 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7252 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7253 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7255 /* if tile specs are not specified, copy MH specs */
7256 if (cp->hprot_TPH[0] == -1) {
7257 cp->hprot_TPH_tileno[0] = 0;
7258 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7260 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7261 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7262 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7263 cp->pprot[i] = parameters->jpwl_pprot[i];
7267 /* set ESD writing */
7268 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7269 cp->esd_on = OPJ_TRUE;
7271 cp->sens_size = parameters->jpwl_sens_size;
7272 cp->sens_addr = parameters->jpwl_sens_addr;
7273 cp->sens_range = parameters->jpwl_sens_range;
7275 cp->sens_MH = parameters->jpwl_sens_MH;
7276 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7277 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7278 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7282 /* always set RED writing to false: we are at the encoder */
7283 cp->red_on = OPJ_FALSE;
7286 cp->epc_on = OPJ_FALSE;
7288 #endif /* USE_JPWL */
7291 /* initialize the mutiple tiles */
7292 /* ---------------------------- */
7293 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7294 if (parameters->numpocs) {
7295 /* initialisation of POC */
7296 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7300 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7301 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7302 tcp->numlayers = parameters->tcp_numlayers;
7304 for (j = 0; j < tcp->numlayers; j++) {
7305 if(cp->m_specific_param.m_enc.m_cinema){
7306 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7307 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7309 tcp->rates[j] = parameters->tcp_rates[j];
7311 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7312 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7314 tcp->rates[j] = parameters->tcp_rates[j];
7319 tcp->csty = parameters->csty;
7320 tcp->prg = parameters->prog_order;
7321 tcp->mct = parameters->tcp_mct;
7326 if (parameters->numpocs) {
7327 /* initialisation of POC */
7330 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7331 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7332 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7334 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7335 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7336 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7337 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7338 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7339 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7340 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7346 tcp->numpocs = numpocs_tile -1 ;
7351 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7353 if (parameters->mct_data) {
7355 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7358 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7359 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7360 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7361 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7364 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7365 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7366 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7368 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7369 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7371 tcp->mct_norms = (OPJ_FLOAT64*)
7372 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7374 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7377 for (i = 0; i < image->numcomps; i++) {
7378 opj_tccp_t *tccp = &tcp->tccps[i];
7379 tccp->m_dc_level_shift = l_dc_shift[i];
7382 j2k_setup_mct_encoding(tcp,image);
7386 for (i = 0; i < image->numcomps; i++) {
7387 opj_tccp_t *tccp = &tcp->tccps[i];
7388 opj_image_comp_t * l_comp = &(image->comps[i]);
7390 if (! l_comp->sgnd) {
7391 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7396 for (i = 0; i < image->numcomps; i++) {
7397 opj_tccp_t *tccp = &tcp->tccps[i];
7399 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7400 tccp->numresolutions = parameters->numresolution;
7401 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7402 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7403 tccp->cblksty = parameters->mode;
7404 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7405 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7408 if (i == parameters->roi_compno) {
7409 tccp->roishift = parameters->roi_shift;
7414 if(parameters->cp_cinema) {
7415 //Precinct size for lowest frequency subband=128
7418 //Precinct size at all other resolutions = 256
7419 for (j = 1; j < tccp->numresolutions; j++) {
7424 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7425 OPJ_INT32 p = 0, it_res;
7426 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7427 if (p < parameters->res_spec) {
7429 if (parameters->prcw_init[p] < 1) {
7430 tccp->prcw[it_res] = 1;
7432 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7435 if (parameters->prch_init[p] < 1) {
7436 tccp->prch[it_res] = 1;
7438 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7442 int res_spec = parameters->res_spec;
7443 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7444 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7446 if (size_prcw < 1) {
7447 tccp->prcw[it_res] = 1;
7449 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7452 if (size_prch < 1) {
7453 tccp->prch[it_res] = 1;
7455 tccp->prch[it_res] = int_floorlog2(size_prch);
7459 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7462 for (j = 0; j < tccp->numresolutions; j++) {
7469 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7473 if (parameters->mct_data) {
7474 opj_free(parameters->mct_data);
7475 parameters->mct_data = 00;
7480 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7483 opj_cp_t *cp = NULL;
7485 opj_tcd_t *tcd = NULL; /* TCD component */
7493 j2k->cstr_info = cstr_info;
7496 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7497 cstr_info->image_w = image->x1 - image->x0;
7498 cstr_info->image_h = image->y1 - image->y0;
7499 cstr_info->prog = (&cp->tcps[0])->prg;
7500 cstr_info->tw = cp->tw;
7501 cstr_info->th = cp->th;
7502 cstr_info->tile_x = cp->tdx; /* new version parser */
7503 cstr_info->tile_y = cp->tdy; /* new version parser */
7504 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7505 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7506 cstr_info->numcomps = image->numcomps;
7507 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7508 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7509 for (compno=0; compno < image->numcomps; compno++) {
7510 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7512 cstr_info->D_max = 0.0; /* ADD Marcela */
7513 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7514 cstr_info->maxmarknum = 100;
7515 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7516 cstr_info->marknum = 0;
7526 for (compno = 1; compno < image->numcomps; compno++) {
7527 j2k_write_coc(j2k, compno);
7528 j2k_write_qcc(j2k, compno);
7532 for (compno = 0; compno < image->numcomps; compno++) {
7533 opj_tcp_t *tcp = &cp->tcps[0];
7534 if (tcp->tccps[compno].roishift)
7535 j2k_write_rgn(j2k, compno, 0);
7537 if (cp->comment != NULL) {
7541 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7545 if (cp->cinema == CINEMA4K_24) {
7550 /* uncomment only for testing JPSEC marker writing */
7551 /* j2k_write_sec(j2k); */
7555 cstr_info->main_head_end = cio_tell(cio) - 1;
7558 /**** Main Header ENDS here ***/
7560 /* create the tile encoder */
7561 tcd = tcd_create(j2k->cinfo);
7563 /* encode each tile */
7564 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7568 int acc_pack_num = 0;
7572 opj_tcp_t *tcp = &cp->tcps[tileno];
7573 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7575 j2k->curtileno = tileno;
7576 j2k->cur_tp_num = 0;
7577 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7578 /* initialisation before tile encoding */
7580 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7582 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7587 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7588 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7589 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7590 cstr_info->tile[j2k->curtileno].marknum = 0;
7594 for(pino = 0; pino <= tcp->numpocs; pino++) {
7598 /*Get number of tile parts*/
7599 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7600 tcd->tp_pos = cp->tp_pos;
7602 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7603 j2k->tp_num = tilepartno;
7606 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7607 cio_tell(cio) + j2k->pos_correction;
7611 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7612 for (compno = 1; compno < image->numcomps; compno++) {
7613 j2k_write_coc(j2k, compno);
7614 j2k_write_qcc(j2k, compno);
7616 if (cp->tcps[tileno].numpocs) {
7623 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7624 cio_tell(cio) + j2k->pos_correction + 1;
7627 j2k_write_sod(j2k, tcd);
7631 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7632 cio_tell(cio) + j2k->pos_correction - 1;
7633 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7635 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7636 cstr_info->packno - acc_pack_num;
7637 acc_pack_num = cstr_info->packno;
7645 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7650 if (tile->PPT) { // BAD PPT !!!
7653 PPT_file=fopen("PPT","rb");
7654 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7655 for (i=0;i<tile->len_ppt;i++) {
7657 fread(&elmt, 1, 1, PPT_file);
7658 fwrite(&elmt,1,1,f);
7667 /* destroy the tile encoder */
7668 tcd_free_encode(tcd);
7671 opj_free(j2k->cur_totnum_tp);
7676 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7678 /* The following adjustment is done to adjust the codestream size */
7679 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7680 /* the first bunch of bytes is not in the codestream */
7681 cstr_info->codestream_size -= cstr_info->main_head_start;
7687 preparation of JPWL marker segments
7691 /* encode according to JPWL */
7692 jpwl_encode(j2k, cio, image);
7695 #endif /* USE_JPWL */
7700 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7702 assert(cstr_info != 00);
7704 /* expand the list? */
7705 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7706 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7707 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7710 /* add the marker */
7711 cstr_info->marker[cstr_info->marknum].type = type;
7712 cstr_info->marker[cstr_info->marknum].pos = pos;
7713 cstr_info->marker[cstr_info->marknum].len = len;
7714 cstr_info->marknum++;
7718 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7720 assert(cstr_index != 00);
7722 /* expand the list? */
7723 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7724 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7725 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7728 /* add the marker */
7729 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7730 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7731 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7732 cstr_index->marknum++;
7736 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7738 opj_marker_info_t *marker;
7740 assert(cstr_info != 00);
7742 /* expand the list? */
7743 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7744 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7745 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7748 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7750 /* add the marker */
7751 marker->type = type;
7754 cstr_info->tile[tileno].marknum++;
7757 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)
7759 assert(cstr_index != 00);
7760 assert(cstr_index->tile_index != 00);
7762 /* expand the list? */
7763 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7764 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7765 cstr_index->tile_index[tileno].marker =
7766 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7767 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7770 /* add the marker */
7771 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7772 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7773 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7774 cstr_index->tile_index[tileno].marknum++;
7776 if (type == J2K_MS_SOT) {
7777 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7779 if (cstr_index->tile_index[tileno].tp_index)
7780 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7787 * -----------------------------------------------------------------------
7788 * -----------------------------------------------------------------------
7789 * -----------------------------------------------------------------------
7793 * Ends the decompression procedures and possibiliy add data to be read after the
7796 opj_bool j2k_end_decompress(
7797 opj_j2k_v2_t *p_j2k,
7798 opj_stream_private_t *p_stream,
7799 opj_event_mgr_t * p_manager)
7808 * Reads a jpeg2000 codestream header structure.
7811 * @param p_stream the stream to read data from.
7812 * @param p_j2k the jpeg2000 codec.
7813 * @param p_manager the user event manager.
7815 * @return true if the box is valid.
7817 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7818 opj_j2k_v2_t* p_j2k,
7819 opj_image_t** p_image,
7820 struct opj_event_mgr* p_manager )
7823 assert(p_j2k != 00);
7824 assert(p_stream != 00);
7825 assert(p_manager != 00);
7827 /* create an empty image header */
7828 p_j2k->m_private_image = opj_image_create0();
7829 if (! p_j2k->m_private_image) {
7833 /* customization of the validation */
7834 j2k_setup_decoding_validation(p_j2k);
7836 /* validation of the parameters codec */
7837 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7838 opj_image_destroy(p_j2k->m_private_image);
7839 p_j2k->m_private_image = NULL;
7843 /* customization of the encoding */
7844 j2k_setup_header_reading(p_j2k);
7847 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7848 opj_image_destroy(p_j2k->m_private_image);
7849 p_j2k->m_private_image = NULL;
7853 *p_image = opj_image_create0();
7858 /* Copy codestream image information to the output image */
7859 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7861 /*Allocate and initialize some elements of codestrem index*/
7862 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7870 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7872 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7875 assert(p_j2k != 00);
7877 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7879 /* DEVELOPER CORNER, add your custom procedures */
7880 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7885 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7886 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7888 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7891 assert(p_j2k != 00);
7893 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7894 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7895 /* DEVELOPER CORNER, add your custom validation procedure */
7901 * The mct encoding validation procedure.
7903 * @param p_j2k the jpeg2000 codec to validate.
7904 * @param p_stream the input stream to validate.
7905 * @param p_manager the user event manager.
7907 * @return true if the parameters are correct.
7909 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7910 opj_stream_private_t *p_stream,
7911 opj_event_mgr_t * p_manager )
7913 opj_bool l_is_valid = OPJ_TRUE;
7917 assert(p_j2k != 00);
7918 assert(p_stream != 00);
7919 assert(p_manager != 00);
7921 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7922 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7923 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7925 for (i=0;i<l_nb_tiles;++i) {
7926 if (l_tcp->mct == 2) {
7927 opj_tccp_t * l_tccp = l_tcp->tccps;
7928 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7930 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7931 l_is_valid &= ! (l_tccp->qmfbid & 1);
7942 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7945 OPJ_UINT32 l_indix = 1;
7946 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7947 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7948 OPJ_UINT32 l_mct_size,l_nb_elem;
7949 OPJ_FLOAT32 * l_data, * l_current_data;
7950 opj_tccp_t * l_tccp;
7953 assert(p_tcp != 00);
7955 if (p_tcp->mct != 2) {
7959 if (p_tcp->m_mct_decoding_matrix) {
7960 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7961 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7963 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));
7964 if (! p_tcp->m_mct_records) {
7967 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7969 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7971 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7973 if (l_mct_deco_data->m_data) {
7974 opj_free(l_mct_deco_data->m_data);
7975 l_mct_deco_data->m_data = 00;
7978 l_mct_deco_data->m_index = l_indix++;
7979 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7980 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7981 l_nb_elem = p_image->numcomps * p_image->numcomps;
7982 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7983 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7985 if (! l_mct_deco_data->m_data) {
7989 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);
7991 l_mct_deco_data->m_data_size = l_mct_size;
7992 ++p_tcp->m_nb_mct_records;
7995 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7996 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7997 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));
7999 if (! p_tcp->m_mct_records) {
8003 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8004 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8006 if (l_mct_deco_data) {
8007 l_mct_deco_data = l_mct_offset_data - 1;
8011 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8013 if (l_mct_offset_data->m_data) {
8014 opj_free(l_mct_offset_data->m_data);
8015 l_mct_offset_data->m_data = 00;
8018 l_mct_offset_data->m_index = l_indix++;
8019 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8020 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8021 l_nb_elem = p_image->numcomps;
8022 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8023 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8025 if (! l_mct_offset_data->m_data) {
8029 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8031 opj_free(l_mct_offset_data->m_data);
8032 l_mct_offset_data->m_data = 00;
8036 l_tccp = p_tcp->tccps;
8037 l_current_data = l_data;
8039 for (i=0;i<l_nb_elem;++i) {
8040 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8044 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8048 l_mct_offset_data->m_data_size = l_mct_size;
8050 ++p_tcp->m_nb_mct_records;
8052 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8053 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8054 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8055 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8057 if (! p_tcp->m_mcc_records) {
8060 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8061 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));
8065 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8066 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8067 l_mcc_data->m_is_irreversible = 1;
8068 l_mcc_data->m_nb_comps = p_image->numcomps;
8069 l_mcc_data->m_index = l_indix++;
8070 l_mcc_data->m_offset_array = l_mct_offset_data;
8071 ++p_tcp->m_nb_mcc_records;
8077 * Builds the cp decoder parameters to use to decode tile.
8079 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8080 opj_stream_private_t *p_stream,
8081 opj_event_mgr_t * p_manager )
8083 /* add here initialization of cp
8084 copy paste of setup_decoder */
8092 * Builds the cp encoder parameters to use to encode tile.
8094 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8095 opj_stream_private_t *p_stream,
8096 opj_event_mgr_t * p_manager )
8098 /* add here initialization of cp
8099 copy paste of setup_encoder */
8107 * The default encoding validation procedure without any extension.
8109 * @param p_j2k the jpeg2000 codec to validate.
8110 * @param p_stream the input stream to validate.
8111 * @param p_manager the user event manager.
8113 * @return true if the parameters are correct.
8115 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8116 opj_stream_private_t *p_stream,
8117 opj_event_mgr_t * p_manager )
8119 opj_bool l_is_valid = OPJ_TRUE;
8122 assert(p_j2k != 00);
8123 assert(p_stream != 00);
8124 assert(p_manager != 00);
8126 /* STATE checking */
8127 /* make sure the state is at 0 */
8128 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8130 /* POINTER validation */
8131 /* make sure a p_j2k codec is present */
8132 l_is_valid &= (p_j2k->m_procedure_list != 00);
8133 /* make sure a validation list is present */
8134 l_is_valid &= (p_j2k->m_validation_list != 00);
8136 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8137 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8141 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8142 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8146 /* PARAMETER VALIDATION */
8151 * The default decoding validation procedure without any extension.
8153 * @param p_j2k the jpeg2000 codec to validate.
8154 * @param p_stream the input stream to validate.
8155 * @param p_manager the user event manager.
8157 * @return true if the parameters are correct.
8159 opj_bool j2k_decoding_validation (
8160 opj_j2k_v2_t *p_j2k,
8161 opj_stream_private_t *p_stream,
8162 opj_event_mgr_t * p_manager
8165 opj_bool l_is_valid = OPJ_TRUE;
8168 assert(p_j2k != 00);
8169 assert(p_stream != 00);
8170 assert(p_manager != 00);
8173 /* STATE checking */
8174 /* make sure the state is at 0 */
8176 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8178 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8180 /* POINTER validation */
8181 /* make sure a p_j2k codec is present */
8182 /* make sure a procedure list is present */
8183 l_is_valid &= (p_j2k->m_procedure_list != 00);
8184 /* make sure a validation list is present */
8185 l_is_valid &= (p_j2k->m_validation_list != 00);
8187 /* PARAMETER VALIDATION */
8191 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8192 struct opj_stream_private *p_stream,
8193 struct opj_event_mgr * p_manager)
8195 OPJ_UINT32 l_current_marker;
8196 OPJ_UINT32 l_marker_size;
8197 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8200 assert(p_stream != 00);
8201 assert(p_j2k != 00);
8202 assert(p_manager != 00);
8204 /* We enter in the main header */
8205 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8207 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8208 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8209 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8213 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8214 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8215 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8219 /* Read 2 bytes as the new marker ID */
8220 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8222 /* Try to read until the SOT is detected */
8223 while (l_current_marker != J2K_MS_SOT) {
8225 /* Check if the current marker ID is valid */
8226 if (l_current_marker < 0xff00) {
8227 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8231 /* Get the marker handler from the marker ID */
8232 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8234 /* Manage case where marker is unknown */
8235 if (l_marker_handler->id == J2K_MS_UNK) {
8236 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8237 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8241 if (l_current_marker == J2K_MS_SOT)
8242 break; /* SOT marker is detected main header is completely read */
8243 else /* Get the marker handler from the marker ID */
8244 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8247 /* Check if the marker is known and if it is the right place to find it */
8248 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8249 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8253 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8254 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8255 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8259 /* read 2 bytes as the marker size */
8260 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8261 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8263 /* Check if the marker size is compatible with the header data size */
8264 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8265 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8266 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8267 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8270 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8273 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8274 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) {
8275 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8279 /* Read the marker segment with the correct marker handler */
8280 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8281 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8285 /* Add the marker to the codestream index*/
8286 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8287 l_marker_handler->id,
8288 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8289 l_marker_size + 4 );
8291 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8292 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8293 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8297 /* read 2 bytes as the new marker ID */
8298 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8301 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8303 /* Position of the last element if the main header */
8304 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8306 /* Next step: read a tile-part header */
8307 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8313 * Excutes the given procedures on the given codec.
8315 * @param p_procedure_list the list of procedures to execute
8316 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8317 * @param p_stream the stream to execute the procedures on.
8318 * @param p_manager the user manager.
8320 * @return true if all the procedures were successfully executed.
8322 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8323 opj_procedure_list_t * p_procedure_list,
8324 opj_stream_private_t *p_stream,
8325 opj_event_mgr_t * p_manager )
8327 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8328 opj_bool l_result = OPJ_TRUE;
8329 OPJ_UINT32 l_nb_proc, i;
8332 assert(p_procedure_list != 00);
8333 assert(p_j2k != 00);
8334 assert(p_stream != 00);
8335 assert(p_manager != 00);
8338 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8339 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);
8341 for (i=0;i<l_nb_proc;++i) {
8342 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8346 /* and clear the procedure list at the end.*/
8347 opj_procedure_list_clear(p_procedure_list);
8352 opj_bool j2k_copy_default_tcp_and_create_tcd
8354 opj_j2k_v2_t * p_j2k,
8355 opj_stream_private_t *p_stream,
8356 opj_event_mgr_t * p_manager
8359 opj_tcp_v2_t * l_tcp = 00;
8360 opj_tcp_v2_t * l_default_tcp = 00;
8361 OPJ_UINT32 l_nb_tiles;
8363 opj_tccp_t *l_current_tccp = 00;
8364 OPJ_UINT32 l_tccp_size;
8365 OPJ_UINT32 l_mct_size;
8366 opj_image_t * l_image;
8367 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8368 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8369 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8370 OPJ_UINT32 l_offset;
8373 assert(p_j2k != 00);
8374 assert(p_stream != 00);
8375 assert(p_manager != 00);
8377 l_image = p_j2k->m_private_image;
8378 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8379 l_tcp = p_j2k->m_cp.tcps;
8380 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8381 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8382 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8385 for (i=0; i<l_nb_tiles; ++i) {
8386 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8387 l_current_tccp = l_tcp->tccps;
8388 /*Copy default coding parameters into the current tile coding parameters*/
8389 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8390 /* Initialize some values of the current tile coding parameters*/
8392 l_tcp->ppt_data = 00;
8393 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8394 l_tcp->tccps = l_current_tccp;
8396 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8397 if (l_default_tcp->m_mct_decoding_matrix) {
8398 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8399 if (! l_tcp->m_mct_decoding_matrix ) {
8402 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8405 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8406 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8407 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8408 if (! l_tcp->m_mct_records) {
8411 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8413 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8414 l_src_mct_rec = l_default_tcp->m_mct_records;
8415 l_dest_mct_rec = l_tcp->m_mct_records;
8417 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8419 if (l_src_mct_rec->m_data) {
8421 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8422 if(! l_dest_mct_rec->m_data) {
8425 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8432 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8433 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8434 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8435 if (! l_tcp->m_mcc_records) {
8438 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8440 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8441 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8442 l_dest_mcc_rec = l_tcp->m_mcc_records;
8444 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8446 if (l_src_mcc_rec->m_decorrelation_array) {
8447 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8448 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8451 if (l_src_mcc_rec->m_offset_array) {
8452 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8453 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8460 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8461 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8463 /* Move to next tile cp*/
8467 /* Create the current tile decoder*/
8468 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8469 if (! p_j2k->m_tcd ) {
8473 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8474 tcd_destroy_v2(p_j2k->m_tcd);
8476 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8484 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8485 * with the marker value.
8486 * @param p_id Marker value to look up
8488 * @return the handler associated with the id.
8490 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8492 const opj_dec_memory_marker_handler_t *e;
8493 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8494 if (e->id == p_id) {
8495 break; /* we find a handler corresponding to the marker ID*/
8503 * Destroys a jpeg2000 codec.
8505 * @param p_j2k the jpeg20000 structure to destroy.
8507 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8513 if (p_j2k->m_is_decoder) {
8515 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8516 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8517 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8518 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8521 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8522 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8523 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8524 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8529 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8530 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8531 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8534 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8535 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8536 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8537 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8540 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8541 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8542 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8543 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8547 tcd_destroy_v2(p_j2k->m_tcd);
8549 j2k_cp_destroy(&(p_j2k->m_cp));
8550 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8552 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8553 p_j2k->m_procedure_list = 00;
8555 opj_procedure_list_destroy(p_j2k->m_validation_list);
8556 p_j2k->m_procedure_list = 00;
8558 j2k_destroy_cstr_index(p_j2k->cstr_index);
8559 p_j2k->cstr_index = NULL;
8561 opj_image_destroy(p_j2k->m_private_image);
8562 p_j2k->m_private_image = NULL;
8564 opj_image_destroy(p_j2k->m_output_image);
8565 p_j2k->m_output_image = NULL;
8570 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8574 if (p_cstr_ind->marker) {
8575 opj_free(p_cstr_ind->marker);
8576 p_cstr_ind->marker = NULL;
8579 if (p_cstr_ind->tile_index) {
8580 OPJ_UINT32 it_tile = 0;
8582 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8584 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8585 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8586 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8589 if(p_cstr_ind->tile_index[it_tile].tp_index){
8590 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8591 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8594 if(p_cstr_ind->tile_index[it_tile].marker){
8595 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8596 p_cstr_ind->tile_index[it_tile].marker = NULL;
8601 opj_free( p_cstr_ind->tile_index);
8602 p_cstr_ind->tile_index = NULL;
8605 opj_free(p_cstr_ind);
8612 * Destroys a tile coding parameter structure.
8614 * @param p_tcp the tile coding parameter to destroy.
8616 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8622 if (p_tcp->ppt_buffer != 00) {
8623 opj_free(p_tcp->ppt_buffer);
8624 p_tcp->ppt_buffer = 00;
8627 if (p_tcp->tccps != 00) {
8628 opj_free(p_tcp->tccps);
8632 if (p_tcp->m_mct_coding_matrix != 00) {
8633 opj_free(p_tcp->m_mct_coding_matrix);
8634 p_tcp->m_mct_coding_matrix = 00;
8637 if (p_tcp->m_mct_decoding_matrix != 00) {
8638 opj_free(p_tcp->m_mct_decoding_matrix);
8639 p_tcp->m_mct_decoding_matrix = 00;
8642 if (p_tcp->m_mcc_records) {
8643 opj_free(p_tcp->m_mcc_records);
8644 p_tcp->m_mcc_records = 00;
8645 p_tcp->m_nb_max_mcc_records = 0;
8646 p_tcp->m_nb_mcc_records = 0;
8649 if (p_tcp->m_mct_records) {
8650 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8653 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8654 if (l_mct_data->m_data) {
8655 opj_free(l_mct_data->m_data);
8656 l_mct_data->m_data = 00;
8662 opj_free(p_tcp->m_mct_records);
8663 p_tcp->m_mct_records = 00;
8666 if (p_tcp->mct_norms != 00) {
8667 opj_free(p_tcp->mct_norms);
8668 p_tcp->mct_norms = 00;
8671 j2k_tcp_data_destroy(p_tcp);
8676 * Destroys the data inside a tile coding parameter structure.
8678 * @param p_tcp the tile coding parameter which contain data to destroy.
8680 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8682 if (p_tcp->m_data) {
8683 opj_free(p_tcp->m_data);
8684 p_tcp->m_data = NULL;
8685 p_tcp->m_data_size = 0;
8690 * Destroys a coding parameter structure.
8692 * @param p_cp the coding parameter to destroy.
8694 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8696 OPJ_UINT32 l_nb_tiles;
8697 opj_tcp_v2_t * l_current_tile = 00;
8708 l_current_tile = p_cp->tcps;
8709 l_nb_tiles = p_cp->th * p_cp->tw;
8712 (i = 0; i < l_nb_tiles; ++i)
8714 j2k_tcp_destroy(l_current_tile);
8717 opj_free(p_cp->tcps);
8721 (p_cp->ppm_buffer != 00)
8723 opj_free(p_cp->ppm_buffer);
8724 p_cp->ppm_buffer = 00;
8727 (p_cp->comment != 00)
8729 opj_free(p_cp->comment);
8733 (! p_cp->m_is_decoder)
8736 (p_cp->m_specific_param.m_enc.m_matrice)
8738 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8739 p_cp->m_specific_param.m_enc.m_matrice = 00;
8747 * Reads a tile header.
8748 * @param p_j2k the jpeg2000 codec.
8749 * @param p_stream the stream to write data to.
8750 * @param p_manager the user event manager.
8752 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8753 OPJ_UINT32 * p_tile_index,
8754 OPJ_UINT32 * p_data_size,
8755 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8756 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8757 OPJ_UINT32 * p_nb_comps,
8759 opj_stream_private_t *p_stream,
8760 opj_event_mgr_t * p_manager )
8762 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8763 OPJ_UINT32 l_marker_size;
8764 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8765 opj_tcp_v2_t * l_tcp = NULL;
8766 OPJ_UINT32 l_nb_tiles;
8769 assert(p_stream != 00);
8770 assert(p_j2k != 00);
8771 assert(p_manager != 00);
8773 /* Reach the End Of Codestream ?*/
8774 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8775 l_current_marker = J2K_MS_EOC;
8777 /* We need to encounter a SOT marker (a new tile-part header) */
8778 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8782 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8783 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8785 /* Try to read until the Start Of Data is detected */
8786 while (l_current_marker != J2K_MS_SOD) {
8788 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8789 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8790 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8794 /* Read 2 bytes from the buffer as the marker size */
8795 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8797 /* Why this condition? FIXME */
8798 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8799 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8801 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8803 /* Get the marker handler from the marker ID */
8804 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8806 /* Check if the marker is known and if it is the right place to find it */
8807 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8808 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8811 /* FIXME manage case of unknown marker as in the main header ? */
8813 /* Check if the marker size is compatible with the header data size */
8814 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8815 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8816 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8817 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8820 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8823 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8824 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
8825 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8829 /* Read the marker segment with the correct marker handler */
8830 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8831 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8835 /* Add the marker to the codestream index*/
8836 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8838 l_marker_handler->id,
8839 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8840 l_marker_size + 4 );
8842 /* Keep the position of the last SOT marker read */
8843 if ( l_marker_handler->id == J2K_MS_SOT ) {
8844 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8845 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8847 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8852 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8853 /* Skip the rest of the tile part header*/
8854 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) {
8855 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8858 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8861 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8862 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8863 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8866 /* Read 2 bytes from the buffer as the new marker ID */
8867 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8871 /* If we didn't skip data before, we need to read the SOD marker*/
8872 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8873 /* Try to read the SOD marker and skip data ? FIXME */
8874 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8880 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8881 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8882 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8883 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8887 /* Read 2 bytes from buffer as the new marker ID */
8888 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8892 /* Indicate we will try to read a new tile-part header*/
8893 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8894 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8895 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8897 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8898 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8899 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8903 /* Read 2 bytes from buffer as the new marker ID */
8904 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8908 /* Current marker is the EOC marker ?*/
8909 if (l_current_marker == J2K_MS_EOC) {
8910 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8911 p_j2k->m_current_tile_number = 0;
8912 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8917 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8918 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8919 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8921 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8922 ++p_j2k->m_current_tile_number;
8926 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8927 *p_go_on = OPJ_FALSE;
8933 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8934 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8938 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8939 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8941 *p_tile_index = p_j2k->m_current_tile_number;
8942 *p_go_on = OPJ_TRUE;
8943 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8944 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8945 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8946 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8947 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8948 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8950 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8956 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8957 OPJ_UINT32 p_tile_index,
8959 OPJ_UINT32 p_data_size,
8960 opj_stream_private_t *p_stream,
8961 opj_event_mgr_t * p_manager )
8963 OPJ_UINT32 l_current_marker;
8964 OPJ_BYTE l_data [2];
8965 opj_tcp_v2_t * l_tcp;
8968 assert(p_stream != 00);
8969 assert(p_j2k != 00);
8970 assert(p_manager != 00);
8972 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8973 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8977 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8978 if (! l_tcp->m_data) {
8979 j2k_tcp_destroy(l_tcp);
8983 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8987 p_j2k->cstr_index) ) {
8988 j2k_tcp_destroy(l_tcp);
8989 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8993 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8997 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8998 * we destroy just the data which will be re-read in read_tile_header*/
8999 /*j2k_tcp_destroy(l_tcp);
9000 p_j2k->m_tcd->tcp = 0;*/
9001 j2k_tcp_data_destroy(l_tcp);
9003 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9004 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9006 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9007 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9008 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9012 opj_read_bytes(l_data,&l_current_marker,2);
9014 if (l_current_marker == J2K_MS_EOC) {
9015 p_j2k->m_current_tile_number = 0;
9016 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9018 else if (l_current_marker != J2K_MS_SOT)
9020 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9029 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9031 OPJ_UINT32 i,j,k = 0;
9032 OPJ_UINT32 l_width_src,l_height_src;
9033 OPJ_UINT32 l_width_dest,l_height_dest;
9034 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9035 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9036 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9037 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9038 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9040 opj_image_comp_t * l_img_comp_src = 00;
9041 opj_image_comp_t * l_img_comp_dest = 00;
9043 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9044 opj_image_t * l_image_src = 00;
9045 OPJ_UINT32 l_size_comp, l_remaining;
9046 OPJ_INT32 * l_dest_ptr;
9047 opj_tcd_resolution_v2_t* l_res= 00;
9049 l_tilec = p_tcd->tcd_image->tiles->comps;
9050 l_image_src = p_tcd->image;
9051 l_img_comp_src = l_image_src->comps;
9053 l_img_comp_dest = p_output_image->comps;
9055 for (i=0; i<l_image_src->numcomps; i++) {
9057 /* Allocate output component buffer if necessary */
9058 if (!l_img_comp_dest->data) {
9060 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9061 if (! l_img_comp_dest->data) {
9066 /* Copy info from decoded comp image to output image */
9067 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9070 /* Compute the precision of the output buffer */
9071 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9072 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9073 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9079 if (l_size_comp == 3) {
9084 /* Current tile component size*/
9086 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9087 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9090 l_width_src = (l_res->x1 - l_res->x0);
9091 l_height_src = (l_res->y1 - l_res->y0);
9093 /* Border of the current output component*/
9094 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9095 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9096 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9097 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9100 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9101 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9105 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9106 * of the input buffer (decoded tile component) which will be move
9107 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9108 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9109 * by this input area.
9111 assert( l_res->x0 >= 0);
9112 assert( l_res->x1 >= 0);
9113 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9114 l_start_x_dest = l_res->x0 - l_x0_dest;
9115 l_offset_x0_src = 0;
9117 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9118 l_width_dest = l_width_src;
9119 l_offset_x1_src = 0;
9122 l_width_dest = l_x1_dest - l_res->x0 ;
9123 l_offset_x1_src = l_width_src - l_width_dest;
9127 l_start_x_dest = 0 ;
9128 l_offset_x0_src = l_x0_dest - l_res->x0;
9130 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9131 l_width_dest = l_width_src - l_offset_x0_src;
9132 l_offset_x1_src = 0;
9135 l_width_dest = l_img_comp_dest->w ;
9136 l_offset_x1_src = l_res->x1 - l_x1_dest;
9140 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9141 l_start_y_dest = l_res->y0 - l_y0_dest;
9142 l_offset_y0_src = 0;
9144 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9145 l_height_dest = l_height_src;
9146 l_offset_y1_src = 0;
9149 l_height_dest = l_y1_dest - l_res->y0 ;
9150 l_offset_y1_src = l_height_src - l_height_dest;
9154 l_start_y_dest = 0 ;
9155 l_offset_y0_src = l_y0_dest - l_res->y0;
9157 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9158 l_height_dest = l_height_src - l_offset_y0_src;
9159 l_offset_y1_src = 0;
9162 l_height_dest = l_img_comp_dest->h ;
9163 l_offset_y1_src = l_res->y1 - l_y1_dest;
9167 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9172 /* Compute the input buffer offset */
9173 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9174 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9175 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9177 /* Compute the output buffer offset */
9178 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9179 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9181 /* Move the output buffer to the first place where we will write*/
9182 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9185 fprintf(stdout, "COMPO[%d]:\n",i);
9186 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9187 "\t tile offset:%d, %d, %d, %d\n"
9188 "\t buffer offset: %d; %d, %d\n",
9189 l_res->x0, l_res->y0, l_width_src, l_height_src,
9190 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9191 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9193 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9194 "\t start offset: %d, line offset= %d\n",
9195 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9199 switch (l_size_comp) {
9202 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9203 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9205 if (l_img_comp_src->sgnd) {
9206 for (j = 0 ; j < l_height_dest ; ++j) {
9207 for ( k = 0 ; k < l_width_dest ; ++k) {
9208 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9211 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9212 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9216 for ( j = 0 ; j < l_height_dest ; ++j ) {
9217 for ( k = 0 ; k < l_width_dest ; ++k) {
9218 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9221 l_dest_ptr+= l_line_offset_dest;
9222 l_src_ptr += l_line_offset_src;
9226 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9227 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9232 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9233 l_src_ptr += l_start_offset_src;
9235 if (l_img_comp_src->sgnd) {
9236 for (j=0;j<l_height_dest;++j) {
9237 for (k=0;k<l_width_dest;++k) {
9238 *(l_dest_ptr++) = *(l_src_ptr++);
9241 l_dest_ptr+= l_line_offset_dest;
9242 l_src_ptr += l_line_offset_src ;
9246 for (j=0;j<l_height_dest;++j) {
9247 for (k=0;k<l_width_dest;++k) {
9248 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9251 l_dest_ptr+= l_line_offset_dest;
9252 l_src_ptr += l_line_offset_src ;
9256 l_src_ptr += l_end_offset_src;
9257 p_data = (OPJ_BYTE*) l_src_ptr;
9262 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9263 l_src_ptr += l_start_offset_src;
9265 for (j=0;j<l_height_dest;++j) {
9266 for (k=0;k<l_width_dest;++k) {
9267 *(l_dest_ptr++) = (*(l_src_ptr++));
9270 l_dest_ptr+= l_line_offset_dest;
9271 l_src_ptr += l_line_offset_src ;
9274 l_src_ptr += l_end_offset_src;
9275 p_data = (OPJ_BYTE*) l_src_ptr;
9289 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9291 * @param p_j2k the jpeg2000 codec.
9292 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9293 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9294 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9295 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9296 * @param p_manager the user event manager
9298 * @return true if the area could be set.
9300 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9301 opj_image_t* p_image,
9302 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9303 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9304 struct opj_event_mgr * p_manager )
9306 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9307 opj_image_t * l_image = p_j2k->m_private_image;
9310 OPJ_INT32 l_comp_x1, l_comp_y1;
9311 opj_image_comp_t* l_img_comp = NULL;
9313 /* Check if we are read the main header */
9314 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9315 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9319 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9320 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9322 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9323 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9324 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9325 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9331 /* Check if the positions provided by the user are correct */
9334 assert(p_start_x >= 0 );
9335 assert(p_start_y >= 0 );
9337 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9338 opj_event_msg_v2(p_manager, EVT_ERROR,
9339 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9340 p_start_x, l_image->x1);
9343 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9344 opj_event_msg_v2(p_manager, EVT_WARNING,
9345 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9346 p_start_x, l_image->x0);
9347 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9348 p_image->x0 = l_image->x0;
9351 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9352 p_image->x0 = p_start_x;
9356 if ((OPJ_UINT32)p_start_y > l_image->y1){
9357 opj_event_msg_v2(p_manager, EVT_ERROR,
9358 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9359 p_start_y, l_image->y1);
9362 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9363 opj_event_msg_v2(p_manager, EVT_WARNING,
9364 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9365 p_start_y, l_image->y0);
9366 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9367 p_image->y0 = l_image->y0;
9370 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9371 p_image->y0 = p_start_y;
9375 assert((OPJ_UINT32)p_end_x > 0);
9376 assert((OPJ_UINT32)p_end_y > 0);
9377 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9378 opj_event_msg_v2(p_manager, EVT_ERROR,
9379 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9380 p_end_x, l_image->x0);
9383 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9384 opj_event_msg_v2(p_manager, EVT_WARNING,
9385 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9386 p_end_x, l_image->x1);
9387 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9388 p_image->x1 = l_image->x1;
9391 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9392 p_image->x1 = p_end_x;
9396 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9397 opj_event_msg_v2(p_manager, EVT_ERROR,
9398 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9399 p_end_y, l_image->y0);
9402 if ((OPJ_UINT32)p_end_y > l_image->y1){
9403 opj_event_msg_v2(p_manager, EVT_WARNING,
9404 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9405 p_end_y, l_image->y1);
9406 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9407 p_image->y1 = l_image->y1;
9410 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9411 p_image->y1 = p_end_y;
9415 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9417 l_img_comp = p_image->comps;
9418 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9422 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9423 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9424 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9425 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9427 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9428 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9430 opj_event_msg_v2(p_manager, EVT_ERROR,
9431 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9435 l_img_comp->w = l_w;
9437 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9438 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9440 opj_event_msg_v2(p_manager, EVT_ERROR,
9441 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9445 l_img_comp->h = l_h;
9450 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9451 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9458 /* ----------------------------------------------------------------------- */
9459 /* J2K / JPT decoder interface */
9460 /* ----------------------------------------------------------------------- */
9462 * Creates a J2K decompression structure.
9464 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9466 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9468 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9472 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9474 l_j2k->m_is_decoder = 1;
9475 l_j2k->m_cp.m_is_decoder = 1;
9477 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9478 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9482 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9484 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9485 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9490 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9492 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9494 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9496 /* codestream index creation */
9497 l_j2k->cstr_index = j2k_create_cstr_index();
9499 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9500 if (!l_j2k->cstr_index){
9505 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9508 /* validation list creation */
9509 l_j2k->m_validation_list = opj_procedure_list_create();
9510 if (! l_j2k->m_validation_list) {
9515 /* execution list creation */
9516 l_j2k->m_procedure_list = opj_procedure_list_create();
9517 if (! l_j2k->m_procedure_list) {
9526 opj_codestream_index_t* j2k_create_cstr_index(void)
9528 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9529 opj_calloc(1,sizeof(opj_codestream_index_t));
9533 cstr_index->maxmarknum = 100;
9534 cstr_index->marknum = 0;
9535 cstr_index->marker = (opj_marker_info_t*)
9536 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9537 if (!cstr_index-> marker)
9540 cstr_index->tile_index = NULL;
9547 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9549 * @param p_tile_no the tile index.
9550 * @param p_comp_no the component being outputted.
9551 * @param p_j2k the J2K codec.
9553 * @return the number of bytes taken by the SPCod element.
9555 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9556 OPJ_UINT32 p_tile_no,
9557 OPJ_UINT32 p_comp_no )
9559 opj_cp_v2_t *l_cp = 00;
9560 opj_tcp_v2_t *l_tcp = 00;
9561 opj_tccp_t *l_tccp = 00;
9564 assert(p_j2k != 00);
9566 l_cp = &(p_j2k->m_cp);
9567 l_tcp = &l_cp->tcps[p_tile_no];
9568 l_tccp = &l_tcp->tccps[p_comp_no];
9570 /* preconditions again */
9571 assert(p_tile_no < (l_cp->tw * l_cp->th));
9572 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9574 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9575 return 5 + l_tccp->numresolutions;
9583 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9585 * @param p_comp_no the component number to output.
9586 * @param p_stream the stream to write data to.
9587 * @param p_j2k J2K codec.
9588 * @param p_manager the user event manager.
9591 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9592 OPJ_UINT32 p_tile_no,
9593 OPJ_UINT32 p_comp_no,
9595 OPJ_UINT32 * p_header_size,
9596 struct opj_event_mgr * p_manager )
9599 opj_cp_v2_t *l_cp = 00;
9600 opj_tcp_v2_t *l_tcp = 00;
9601 opj_tccp_t *l_tccp = 00;
9604 assert(p_j2k != 00);
9605 assert(p_header_size != 00);
9606 assert(p_manager != 00);
9607 assert(p_data != 00);
9609 l_cp = &(p_j2k->m_cp);
9610 l_tcp = &l_cp->tcps[p_tile_no];
9611 l_tccp = &l_tcp->tccps[p_comp_no];
9613 /* preconditions again */
9614 assert(p_tile_no < (l_cp->tw * l_cp->th));
9615 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9617 if (*p_header_size < 5) {
9618 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9622 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9625 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9628 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9631 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9634 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9637 *p_header_size = *p_header_size - 5;
9639 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9641 if (*p_header_size < l_tccp->numresolutions) {
9642 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9646 for (i = 0; i < l_tccp->numresolutions; ++i) {
9647 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9651 *p_header_size = *p_header_size - l_tccp->numresolutions;
9658 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9659 * @param p_header_data the data contained in the COM box.
9660 * @param p_j2k the jpeg2000 codec.
9661 * @param p_header_size the size of the data contained in the COM marker.
9662 * @param p_manager the user event manager.
9664 opj_bool j2k_read_SPCod_SPCoc(
9665 opj_j2k_v2_t *p_j2k,
9667 OPJ_BYTE * p_header_data,
9668 OPJ_UINT32 * p_header_size,
9669 struct opj_event_mgr * p_manager
9672 OPJ_UINT32 i, l_tmp;
9673 opj_cp_v2_t *l_cp = NULL;
9674 opj_tcp_v2_t *l_tcp = NULL;
9675 opj_tccp_t *l_tccp = NULL;
9676 OPJ_BYTE * l_current_ptr = NULL;
9679 assert(p_j2k != 00);
9680 assert(p_manager != 00);
9681 assert(p_header_data != 00);
9683 l_cp = &(p_j2k->m_cp);
9684 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9685 &l_cp->tcps[p_j2k->m_current_tile_number] :
9686 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9688 /* precondition again */
9689 assert(compno < p_j2k->m_private_image->numcomps);
9691 l_tccp = &l_tcp->tccps[compno];
9692 l_current_ptr = p_header_data;
9694 /* make sure room is sufficient */
9695 if (*p_header_size < 5) {
9696 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9700 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9701 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9704 /* If user wants to remove more resolutions than the codestream contains, return error */
9705 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9707 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9708 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9712 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9716 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9720 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9723 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9726 *p_header_size = *p_header_size - 5;
9728 /* use custom precinct size ? */
9729 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9730 if (*p_header_size < l_tccp->numresolutions) {
9731 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9735 for (i = 0; i < l_tccp->numresolutions; ++i) {
9736 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9738 l_tccp->prcw[i] = l_tmp & 0xf;
9739 l_tccp->prch[i] = l_tmp >> 4;
9742 *p_header_size = *p_header_size - l_tccp->numresolutions;
9745 /* set default size for the precinct width and height */
9746 for (i = 0; i < l_tccp->numresolutions; ++i) {
9747 l_tccp->prcw[i] = 15;
9748 l_tccp->prch[i] = 15;
9752 #ifdef WIP_REMOVE_MSD
9754 if (p_j2k->cstr_info && compno == 0) {
9755 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9757 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9758 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9759 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9760 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9761 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9764 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9765 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9774 * Copies the tile component parameters of all the component from the first tile component.
9776 * @param p_j2k the J2k codec.
9778 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9782 opj_cp_v2_t *l_cp = NULL;
9783 opj_tcp_v2_t *l_tcp = NULL;
9784 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9785 OPJ_UINT32 l_prc_size;
9788 assert(p_j2k != 00);
9790 l_cp = &(p_j2k->m_cp);
9791 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9792 &l_cp->tcps[p_j2k->m_current_tile_number] :
9793 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9795 l_ref_tccp = &l_tcp->tccps[0];
9796 l_copied_tccp = l_ref_tccp + 1;
9797 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9799 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9800 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9801 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9802 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9803 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9804 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9805 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9806 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9812 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9814 * @param p_tile_no the tile index.
9815 * @param p_comp_no the component being outputted.
9816 * @param p_j2k the J2K codec.
9818 * @return the number of bytes taken by the SPCod element.
9820 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9821 OPJ_UINT32 p_tile_no,
9822 OPJ_UINT32 p_comp_no )
9824 OPJ_UINT32 l_num_bands;
9826 opj_cp_v2_t *l_cp = 00;
9827 opj_tcp_v2_t *l_tcp = 00;
9828 opj_tccp_t *l_tccp = 00;
9831 assert(p_j2k != 00);
9833 l_cp = &(p_j2k->m_cp);
9834 l_tcp = &l_cp->tcps[p_tile_no];
9835 l_tccp = &l_tcp->tccps[p_comp_no];
9837 /* preconditions again */
9838 assert(p_tile_no < l_cp->tw * l_cp->th);
9839 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9841 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9843 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9844 return 1 + l_num_bands;
9847 return 1 + 2*l_num_bands;
9852 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9854 * @param p_tile_no the tile to output.
9855 * @param p_comp_no the component number to output.
9856 * @param p_data the data buffer.
9857 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9858 * @param p_j2k J2K codec.
9859 * @param p_manager the user event manager.
9862 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9863 OPJ_UINT32 p_tile_no,
9864 OPJ_UINT32 p_comp_no,
9866 OPJ_UINT32 * p_header_size,
9867 struct opj_event_mgr * p_manager )
9869 OPJ_UINT32 l_header_size;
9870 OPJ_UINT32 l_band_no, l_num_bands;
9871 OPJ_UINT32 l_expn,l_mant;
9873 opj_cp_v2_t *l_cp = 00;
9874 opj_tcp_v2_t *l_tcp = 00;
9875 opj_tccp_t *l_tccp = 00;
9878 assert(p_j2k != 00);
9879 assert(p_header_size != 00);
9880 assert(p_manager != 00);
9881 assert(p_data != 00);
9883 l_cp = &(p_j2k->m_cp);
9884 l_tcp = &l_cp->tcps[p_tile_no];
9885 l_tccp = &l_tcp->tccps[p_comp_no];
9887 /* preconditions again */
9888 assert(p_tile_no < l_cp->tw * l_cp->th);
9889 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9891 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9893 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9894 l_header_size = 1 + l_num_bands;
9896 if (*p_header_size < l_header_size) {
9897 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9901 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9904 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9905 l_expn = l_tccp->stepsizes[l_band_no].expn;
9906 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9911 l_header_size = 1 + 2*l_num_bands;
9913 if (*p_header_size < l_header_size) {
9914 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9918 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9921 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9922 l_expn = l_tccp->stepsizes[l_band_no].expn;
9923 l_mant = l_tccp->stepsizes[l_band_no].mant;
9925 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9930 *p_header_size = *p_header_size - l_header_size;
9936 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9938 * @param p_comp_no the component being targeted.
9939 * @param p_header_data the data contained in the COM box.
9940 * @param p_j2k the jpeg2000 codec.
9941 * @param p_header_size the size of the data contained in the COM marker.
9942 * @param p_manager the user event manager.
9944 opj_bool j2k_read_SQcd_SQcc(
9945 opj_j2k_v2_t *p_j2k,
9946 OPJ_UINT32 p_comp_no,
9947 OPJ_BYTE* p_header_data,
9948 OPJ_UINT32 * p_header_size,
9949 struct opj_event_mgr * p_manager
9953 OPJ_UINT32 l_band_no;
9954 opj_cp_v2_t *l_cp = 00;
9955 opj_tcp_v2_t *l_tcp = 00;
9956 opj_tccp_t *l_tccp = 00;
9957 OPJ_BYTE * l_current_ptr = 00;
9958 OPJ_UINT32 l_tmp, l_num_band;
9961 assert(p_j2k != 00);
9962 assert(p_manager != 00);
9963 assert(p_header_data != 00);
9965 l_cp = &(p_j2k->m_cp);
9966 /* come from tile part header or main header ?*/
9967 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9968 &l_cp->tcps[p_j2k->m_current_tile_number] :
9969 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9971 /* precondition again*/
9972 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9974 l_tccp = &l_tcp->tccps[p_comp_no];
9975 l_current_ptr = p_header_data;
9977 if (*p_header_size < 1) {
9978 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9981 *p_header_size -= 1;
9983 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9986 l_tccp->qntsty = l_tmp & 0x1f;
9987 l_tccp->numgbits = l_tmp >> 5;
9988 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9992 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9994 (*p_header_size) / 2;
9996 if( l_num_band > J2K_MAXBANDS ) {
9997 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9998 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9999 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10000 /*return OPJ_FALSE;*/
10005 if (l_cp->correct) {
10007 /* if JPWL is on, we check whether there are too many subbands */
10008 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10009 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10010 "JPWL: bad number of subbands in Sqcx (%d)\n",
10012 if (!JPWL_ASSUME) {
10013 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10016 /* we try to correct */
10018 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10019 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10024 #endif /* USE_JPWL */
10026 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10027 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10028 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10030 if (l_band_no < J2K_MAXBANDS){
10031 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10032 l_tccp->stepsizes[l_band_no].mant = 0;
10035 *p_header_size = *p_header_size - l_num_band;
10038 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10039 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10041 if (l_band_no < J2K_MAXBANDS){
10042 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10043 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10046 *p_header_size = *p_header_size - 2*l_num_band;
10049 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10050 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10051 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10052 l_tccp->stepsizes[l_band_no].expn =
10053 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10054 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10055 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10063 * Copies the tile component parameters of all the component from the first tile component.
10065 * @param p_j2k the J2k codec.
10067 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10070 opj_cp_v2_t *l_cp = NULL;
10071 opj_tcp_v2_t *l_tcp = NULL;
10072 opj_tccp_t *l_ref_tccp = NULL;
10073 opj_tccp_t *l_copied_tccp = NULL;
10076 /* preconditions */
10077 assert(p_j2k != 00);
10079 l_cp = &(p_j2k->m_cp);
10080 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10081 &l_cp->tcps[p_j2k->m_current_tile_number] :
10082 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10084 l_ref_tccp = &l_tcp->tccps[0];
10085 l_copied_tccp = l_ref_tccp + 1;
10086 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10088 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10089 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10090 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10091 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10097 * Dump some elements from the J2K decompression structure .
10099 *@param p_j2k the jpeg2000 codec.
10100 *@param flag flag to describe what elments are dump.
10101 *@param out_stream output stream where dump the elements.
10104 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10106 /* Check if the flag is compatible with j2k file*/
10107 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10108 fprintf(out_stream, "Wrong flag\n");
10112 /* Dump the image_header */
10113 if (flag & OPJ_IMG_INFO){
10114 if (p_j2k->m_private_image)
10115 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10118 /* Dump the codestream info from main header */
10119 if (flag & OPJ_J2K_MH_INFO){
10120 j2k_dump_MH_info(p_j2k, out_stream);
10124 /* Dump the codestream info of the current tile */
10125 if (flag & OPJ_J2K_TH_INFO){
10129 /* Dump the codestream index from main header */
10130 if (flag & OPJ_J2K_MH_IND){
10131 j2k_dump_MH_index(p_j2k, out_stream);
10134 /* Dump the codestream index of the current tile */
10135 if (flag & OPJ_J2K_TH_IND){
10142 * Dump index elements of the codestream extract from the main header.
10144 *@param p_j2k the jpeg2000 codec.
10145 *@param out_stream output stream where dump the elements.
10148 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10150 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10151 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10153 fprintf(out_stream, "Codestream index from main header: {\n");
10155 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10156 "\t Main header end position=%" PRIi64 "\n",
10157 cstr_index->main_head_start, cstr_index->main_head_end);
10159 fprintf(out_stream, "\t Marker list: {\n");
10161 if (cstr_index->marker){
10162 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10163 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10164 cstr_index->marker[it_marker].type,
10165 cstr_index->marker[it_marker].pos,
10166 cstr_index->marker[it_marker].len );
10170 fprintf(out_stream, "\t }\n");
10173 if (cstr_index->tile_index){
10175 /* Simple test to avoid to write empty information*/
10176 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10177 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10178 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10181 if (l_acc_nb_of_tile_part)
10183 fprintf(out_stream, "\t Tile index: {\n");
10185 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10186 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10188 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10190 if (cstr_index->tile_index[it_tile].tp_index){
10191 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10192 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10194 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10195 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10196 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10200 if (cstr_index->tile_index[it_tile].marker){
10201 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10202 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10203 cstr_index->tile_index[it_tile].marker[it_marker].type,
10204 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10205 cstr_index->tile_index[it_tile].marker[it_marker].len );
10209 fprintf(out_stream,"\t }\n");
10213 fprintf(out_stream,"}\n");
10218 * Dump info elements of the codestream extract from the main header.
10220 *@param p_j2k the jpeg2000 codec.
10221 *@param out_stream output stream where dump the elements.
10224 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10226 opj_tcp_v2_t * l_default_tile=NULL;
10228 fprintf(out_stream, "Codestream info from main header: {\n");
10230 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10231 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10232 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10234 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10235 if (l_default_tile)
10238 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10240 fprintf(out_stream, "\t default tile {\n");
10241 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10242 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10243 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10244 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10246 for (compno = 0; compno < numcomps; compno++) {
10247 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10249 OPJ_INT32 bandno, numbands;
10252 fprintf(out_stream, "\t\t comp %d {\n", compno);
10253 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10254 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10255 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10256 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10257 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10258 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10260 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10261 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10262 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10264 fprintf(out_stream, "\n");
10266 /* quantization style*/
10267 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10268 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10269 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10270 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10271 for (bandno = 0; bandno < numbands; bandno++) {
10272 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10273 l_tccp->stepsizes[bandno].expn);
10275 fprintf(out_stream, "\n");
10278 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10280 fprintf(out_stream, "\t\t }\n");
10281 } /*end of component of default tile*/
10282 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10286 fprintf(out_stream, "}\n");
10291 * Dump an image header structure.
10293 *@param img_header the image header to dump.
10294 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10295 *@param out_stream output stream where dump the elements.
10297 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10301 if (dev_dump_flag){
10302 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10306 fprintf(out_stream, "Image info {\n");
10307 tab[0] = '\t';tab[1] = '\0';
10310 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10311 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10312 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10314 if (img_header->comps){
10316 for (compno = 0; compno < img_header->numcomps; compno++) {
10317 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10318 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10319 fprintf(out_stream,"%s}\n",tab);
10323 fprintf(out_stream, "}\n");
10327 * Dump a component image header structure.
10329 *@param comp_header the component image header to dump.
10330 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10331 *@param out_stream output stream where dump the elements.
10333 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10337 if (dev_dump_flag){
10338 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10341 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10344 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10345 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10346 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10349 fprintf(out_stream, "}\n");
10354 * Get the codestream info from a JPEG2000 codec.
10356 *@param p_j2k the component image header to dump.
10358 *@return the codestream information extract from the jpg2000 codec
10360 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10363 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10364 opj_tcp_v2_t *l_default_tile;
10365 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10367 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10369 cstr_info->tx0 = p_j2k->m_cp.tx0;
10370 cstr_info->ty0 = p_j2k->m_cp.ty0;
10371 cstr_info->tdx = p_j2k->m_cp.tdx;
10372 cstr_info->tdy = p_j2k->m_cp.tdy;
10373 cstr_info->tw = p_j2k->m_cp.tw;
10374 cstr_info->th = p_j2k->m_cp.th;
10376 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10378 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10380 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10381 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10382 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10383 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10385 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10387 for (compno = 0; compno < numcomps; compno++) {
10388 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10389 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10390 OPJ_INT32 bandno, numbands;
10393 l_tccp_info->csty = l_tccp->csty;
10394 l_tccp_info->numresolutions = l_tccp->numresolutions;
10395 l_tccp_info->cblkw = l_tccp->cblkw;
10396 l_tccp_info->cblkh = l_tccp->cblkh;
10397 l_tccp_info->cblksty = l_tccp->cblksty;
10398 l_tccp_info->qmfbid = l_tccp->qmfbid;
10399 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10401 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10402 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10405 /* quantization style*/
10406 l_tccp_info->qntsty = l_tccp->qntsty;
10407 l_tccp_info->numgbits = l_tccp->numgbits;
10409 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10410 if (numbands < J2K_MAXBANDS) {
10411 for (bandno = 0; bandno < numbands; bandno++) {
10412 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10413 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10418 l_tccp_info->roishift = l_tccp->roishift;
10426 * Get the codestream index from a JPEG2000 codec.
10428 *@param p_j2k the component image header to dump.
10430 *@return the codestream index extract from the jpg2000 codec
10432 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10434 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10435 opj_calloc(1,sizeof(opj_codestream_index_t));
10439 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10440 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10441 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10443 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10444 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10445 if (!l_cstr_index->marker){
10446 opj_free( l_cstr_index);
10450 if (p_j2k->cstr_index->marker)
10451 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10453 opj_free(l_cstr_index->marker);
10454 l_cstr_index->marker = NULL;
10457 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10458 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10459 if (!l_cstr_index->tile_index){
10460 opj_free( l_cstr_index->marker);
10461 opj_free( l_cstr_index);
10465 if (!p_j2k->cstr_index->tile_index){
10466 opj_free(l_cstr_index->tile_index);
10467 l_cstr_index->tile_index = NULL;
10470 OPJ_UINT32 it_tile = 0;
10471 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10474 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10476 l_cstr_index->tile_index[it_tile].marker =
10477 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10479 if (!l_cstr_index->tile_index[it_tile].marker) {
10480 OPJ_UINT32 it_tile_free;
10482 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10483 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10486 opj_free( l_cstr_index->tile_index);
10487 opj_free( l_cstr_index->marker);
10488 opj_free( l_cstr_index);
10492 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10493 memcpy( l_cstr_index->tile_index[it_tile].marker,
10494 p_j2k->cstr_index->tile_index[it_tile].marker,
10495 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10497 opj_free(l_cstr_index->tile_index[it_tile].marker);
10498 l_cstr_index->tile_index[it_tile].marker = NULL;
10501 /* Tile part index*/
10502 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10504 l_cstr_index->tile_index[it_tile].tp_index =
10505 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10507 if(!l_cstr_index->tile_index[it_tile].tp_index){
10508 OPJ_UINT32 it_tile_free;
10510 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10511 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10512 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10515 opj_free( l_cstr_index->tile_index);
10516 opj_free( l_cstr_index->marker);
10517 opj_free( l_cstr_index);
10521 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10522 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10523 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10524 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10527 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10528 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10531 /* Packet index (NOT USED)*/
10532 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10533 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10538 return l_cstr_index;
10541 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10543 OPJ_UINT32 it_tile=0;
10545 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10546 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10547 if (!p_j2k->cstr_index->tile_index)
10550 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10551 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10552 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10553 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10554 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10555 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10565 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10566 opj_stream_private_t *p_stream,
10567 opj_event_mgr_t * p_manager)
10569 opj_bool l_go_on = OPJ_TRUE;
10570 OPJ_UINT32 l_current_tile_no;
10571 OPJ_UINT32 l_data_size,l_max_data_size;
10572 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10573 OPJ_UINT32 l_nb_comps;
10574 OPJ_BYTE * l_current_data;
10576 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10577 if (! l_current_data) {
10580 l_max_data_size = 1000;
10585 if (! j2k_read_tile_header( p_j2k,
10586 &l_current_tile_no,
10588 &l_tile_x0, &l_tile_y0,
10589 &l_tile_x1, &l_tile_y1,
10594 opj_free(l_current_data);
10602 if (l_data_size > l_max_data_size) {
10603 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10604 if (! l_current_data) {
10605 opj_free(l_current_data);
10609 l_max_data_size = l_data_size;
10612 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10613 opj_free(l_current_data);
10616 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);
10618 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10619 opj_free(l_current_data);
10622 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10626 opj_free(l_current_data);
10632 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10634 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10637 assert(p_j2k != 00);
10639 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10640 /* DEVELOPER CORNER, add your custom procedures */
10645 * Read and decode one tile.
10647 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10648 opj_stream_private_t *p_stream,
10649 opj_event_mgr_t * p_manager)
10651 opj_bool l_go_on = OPJ_TRUE;
10652 OPJ_UINT32 l_current_tile_no;
10653 OPJ_UINT32 l_tile_no_to_dec;
10654 OPJ_UINT32 l_data_size,l_max_data_size;
10655 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10656 OPJ_UINT32 l_nb_comps;
10657 OPJ_BYTE * l_current_data;
10659 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10660 if (! l_current_data) {
10663 l_max_data_size = 1000;
10665 /*Allocate and initialize some elements of codestrem index if not already done*/
10666 if( !p_j2k->cstr_index->tile_index)
10668 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10669 opj_free(l_current_data);
10673 /* Move into the codestream to the first SOT used to decode the desired tile */
10674 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10675 if (p_j2k->cstr_index->tile_index)
10676 if(p_j2k->cstr_index->tile_index->tp_index)
10678 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10679 /* the index for this tile has not been built,
10680 * so move to the last SOT read */
10681 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10682 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10687 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)) {
10688 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10692 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10693 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10694 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10698 if (! j2k_read_tile_header( p_j2k,
10699 &l_current_tile_no,
10701 &l_tile_x0, &l_tile_y0,
10702 &l_tile_x1, &l_tile_y1,
10707 opj_free(l_current_data);
10716 if (l_data_size > l_max_data_size) {
10717 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10718 if (! l_current_data) {
10719 opj_free(l_current_data);
10723 l_max_data_size = l_data_size;
10728 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10729 opj_free(l_current_data);
10732 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);
10734 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10735 opj_free(l_current_data);
10738 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10740 if(l_current_tile_no == l_tile_no_to_dec)
10742 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10743 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10744 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10750 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);
10755 opj_free(l_current_data);
10762 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10764 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10767 assert(p_j2k != 00);
10769 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10770 /* DEVELOPER CORNER, add your custom procedures */
10776 * Decodes the tiles of the stream.
10778 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10779 opj_stream_private_t * p_stream,
10780 opj_image_t * p_image,
10781 opj_event_mgr_t * p_manager)
10788 p_j2k->m_output_image = opj_image_create0();
10789 if (! (p_j2k->m_output_image)) {
10792 opj_copy_image_header(p_image, p_j2k->m_output_image);
10794 /* customization of the decoding */
10795 j2k_setup_decoding(p_j2k);
10797 /* Decode the codestream */
10798 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10799 opj_image_destroy(p_j2k->m_private_image);
10800 p_j2k->m_private_image = NULL;
10804 /* Move data and copy one information from codec to output image*/
10805 for (compno = 0; compno < p_image->numcomps; compno++) {
10806 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10807 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10808 p_j2k->m_output_image->comps[compno].data = NULL;
10816 * Get the decoded tile.
10818 * @param p_j2k the jpeg2000 codestream codec.
10819 * @param p_stream input_stream
10820 * @param p_image output image. .
10821 * @param p_manager the user event manager
10822 * @param tile_index index of the tile we want decode
10824 * @return true if succeed.
10826 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10827 opj_stream_private_t *p_stream,
10828 opj_image_t* p_image,
10829 struct opj_event_mgr * p_manager,
10830 OPJ_UINT32 tile_index )
10833 OPJ_UINT32 l_tile_x, l_tile_y;
10834 opj_image_comp_t* l_img_comp;
10837 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10841 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10842 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);
10846 /* Compute the dimension of the desired tile*/
10847 l_tile_x = tile_index % p_j2k->m_cp.tw;
10848 l_tile_y = tile_index / p_j2k->m_cp.tw;
10850 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10851 if (p_image->x0 < p_j2k->m_private_image->x0)
10852 p_image->x0 = p_j2k->m_private_image->x0;
10853 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10854 if (p_image->x1 > p_j2k->m_private_image->x1)
10855 p_image->x1 = p_j2k->m_private_image->x1;
10857 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10858 if (p_image->y0 < p_j2k->m_private_image->y0)
10859 p_image->y0 = p_j2k->m_private_image->y0;
10860 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10861 if (p_image->y1 > p_j2k->m_private_image->y1)
10862 p_image->y1 = p_j2k->m_private_image->y1;
10864 l_img_comp = p_image->comps;
10865 for (compno=0; compno < p_image->numcomps; ++compno)
10867 OPJ_INT32 l_comp_x1, l_comp_y1;
10869 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10871 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10872 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10873 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10874 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10876 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10877 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10882 /* Destroy the previous output image*/
10883 if (p_j2k->m_output_image)
10884 opj_image_destroy(p_j2k->m_output_image);
10886 /* Create the ouput image from the information previously computed*/
10887 p_j2k->m_output_image = opj_image_create0();
10888 if (! (p_j2k->m_output_image)) {
10891 opj_copy_image_header(p_image, p_j2k->m_output_image);
10893 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10895 /* customization of the decoding */
10896 j2k_setup_decoding_tile(p_j2k);
10898 /* Decode the codestream */
10899 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10900 opj_image_destroy(p_j2k->m_private_image);
10901 p_j2k->m_private_image = NULL;
10905 /* Move data and copy one information from codec to output image*/
10906 for (compno = 0; compno < p_image->numcomps; compno++) {
10907 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10909 if (p_image->comps[compno].data)
10910 opj_free(p_image->comps[compno].data);
10912 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10914 p_j2k->m_output_image->comps[compno].data = NULL;
10920 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10922 OPJ_UINT32 it_comp;
10924 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10926 if (p_j2k->m_private_image) {
10927 if (p_j2k->m_private_image->comps) {
10928 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10929 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10930 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10931 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10932 if ( res_factor >= max_res){
10933 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10936 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10949 * Encodes all the tiles in a row.
10951 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10952 opj_stream_private_t *p_stream,
10953 opj_event_mgr_t * p_manager )
10956 OPJ_UINT32 l_nb_tiles;
10957 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10958 OPJ_BYTE * l_current_data;
10960 /* preconditions */
10961 assert(p_j2k != 00);
10962 assert(p_stream != 00);
10963 assert(p_manager != 00);
10965 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10966 if (! l_current_data) {
10969 l_max_tile_size = 1000;
10971 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10972 for (i=0;i<l_nb_tiles;++i) {
10973 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10974 opj_free(l_current_data);
10978 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10979 if (l_current_tile_size > l_max_tile_size) {
10980 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10981 if (! l_current_data) {
10984 l_max_tile_size = l_current_tile_size;
10987 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10989 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10994 opj_free(l_current_data);
10999 * Ends the compression procedures and possibility add data to be read after the
11002 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11003 opj_stream_private_t *p_stream,
11004 struct opj_event_mgr * p_manager)
11006 /* customization of the encoding */
11007 j2k_setup_end_compress(p_j2k);
11009 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11019 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11021 * @param p_j2k the jpeg2000 codec.
11022 * @param p_stream the stream object.
11023 * @param p_manager the user event manager.
11025 * @return true if the codec is valid.
11027 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11028 opj_stream_private_t *p_stream,
11029 opj_image_t * p_image,
11030 opj_event_mgr_t * p_manager)
11032 /* preconditions */
11033 assert(p_j2k != 00);
11034 assert(p_stream != 00);
11035 assert(p_manager != 00);
11037 p_j2k->m_private_image = opj_image_create0();
11038 opj_copy_image_header(p_image, p_j2k->m_private_image);
11040 // TODO_MSD: Find a better way
11041 if (p_image->comps) {
11042 OPJ_UINT32 it_comp;
11043 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11044 if (p_image->comps[it_comp].data) {
11045 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11046 p_image->comps[it_comp].data = NULL;
11052 /* customization of the validation */
11053 j2k_setup_encoding_validation (p_j2k);
11055 /* validation of the parameters codec */
11056 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11060 /* customization of the encoding */
11061 j2k_setup_header_writting(p_j2k);
11064 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11074 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11075 OPJ_UINT32 p_tile_index,
11076 opj_stream_private_t *p_stream,
11077 opj_event_mgr_t * p_manager )
11080 if (p_tile_index != p_j2k->m_current_tile_number) {
11081 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11085 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);
11087 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11088 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11089 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11091 /* initialisation before tile encoding */
11092 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11102 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11104 OPJ_UINT32 i,j,k = 0;
11105 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11106 opj_image_comp_t * l_img_comp = 00;
11107 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11108 opj_image_t * l_image = 00;
11109 OPJ_UINT32 l_size_comp, l_remaining;
11110 OPJ_INT32 * l_src_ptr;
11111 l_tilec = p_tcd->tcd_image->tiles->comps;
11112 l_image = p_tcd->image;
11113 l_img_comp = l_image->comps;
11115 for (i=0;i<p_tcd->image->numcomps;++i) {
11116 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11117 l_remaining = l_img_comp->prec & 7; /* (%8) */
11122 if (l_size_comp == 3) {
11126 l_width = (l_tilec->x1 - l_tilec->x0);
11127 l_height = (l_tilec->y1 - l_tilec->y0);
11128 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11129 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11130 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11131 l_stride = l_image_width - l_width;
11132 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11134 switch (l_size_comp) {
11137 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11138 if (l_img_comp->sgnd) {
11139 for (j=0;j<l_height;++j) {
11140 for (k=0;k<l_width;++k) {
11141 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11145 l_src_ptr += l_stride;
11149 for (j=0;j<l_height;++j) {
11150 for (k=0;k<l_width;++k) {
11151 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11155 l_src_ptr += l_stride;
11159 p_data = (OPJ_BYTE*) l_dest_ptr;
11164 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11165 if (l_img_comp->sgnd) {
11166 for (j=0;j<l_height;++j) {
11167 for (k=0;k<l_width;++k) {
11168 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11170 l_src_ptr += l_stride;
11174 for (j=0;j<l_height;++j) {
11175 for (k=0;k<l_width;++k) {
11176 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11178 l_src_ptr += l_stride;
11182 p_data = (OPJ_BYTE*) l_dest_ptr;
11187 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11188 for (j=0;j<l_height;++j) {
11189 for (k=0;k<l_width;++k) {
11190 *(l_dest_ptr++) = *(l_src_ptr++);
11192 l_src_ptr += l_stride;
11195 p_data = (OPJ_BYTE*) l_dest_ptr;
11208 * @param p_j2k the jpeg2000 codec.
11209 * @param p_stream the stream to write data to.
11210 * @param p_manager the user event manager.
11212 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11214 OPJ_UINT32 p_data_size,
11215 opj_stream_private_t *p_stream,
11216 opj_event_mgr_t * p_manager )
11218 opj_tcd_v2_t * l_tcd = 00;
11219 opj_cp_v2_t * l_cp = 00;
11220 opj_tcp_v2_t * l_tcp = 00;
11221 OPJ_UINT32 l_nb_bytes_written;
11222 OPJ_BYTE * l_current_data = 00;
11223 OPJ_UINT32 l_tile_size = 0;
11224 OPJ_UINT32 l_available_data;
11226 /* preconditions */
11227 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11229 l_tcd = p_j2k->m_tcd;
11230 l_cp = &(p_j2k->m_cp);
11231 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11233 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11234 l_available_data = l_tile_size;
11235 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11237 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11238 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11242 l_nb_bytes_written = 0;
11243 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11246 l_current_data += l_nb_bytes_written;
11247 l_available_data -= l_nb_bytes_written;
11249 l_nb_bytes_written = 0;
11250 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11254 l_available_data -= l_nb_bytes_written;
11255 l_nb_bytes_written = l_tile_size - l_available_data;
11257 if ( opj_stream_write_data( p_stream,
11258 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11259 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11263 ++p_j2k->m_current_tile_number;
11270 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11271 * are valid. Developers wanting to extend the library can add their own validation procedures.
11273 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11275 /* preconditions */
11276 assert(p_j2k != 00);
11278 /* DEVELOPER CORNER, insert your custom procedures */
11279 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11281 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11282 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11285 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11286 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11287 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11291 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11292 * are valid. Developers wanting to extend the library can add their own validation procedures.
11294 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11296 /* preconditions */
11297 assert(p_j2k != 00);
11299 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11300 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11302 /* DEVELOPER CORNER, add your custom validation procedure */
11303 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11308 * Sets up the procedures to do on writing header.
11309 * Developers wanting to extend the library can add their own writing procedures.
11311 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11313 /* preconditions */
11314 assert(p_j2k != 00);
11316 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11317 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11318 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11319 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11320 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11323 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11324 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11325 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11327 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11328 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11332 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11334 if (p_j2k->m_cp.comment != 00) {
11335 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11338 /* DEVELOPER CORNER, insert your custom procedures */
11339 if (p_j2k->m_cp.rsiz & MCT) {
11340 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11342 /* End of Developer Corner */
11344 if (p_j2k->cstr_index) {
11345 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11348 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11349 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11353 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11355 OPJ_UINT32 * p_data_written,
11356 OPJ_UINT32 p_total_data_size,
11357 opj_stream_private_t *p_stream,
11358 struct opj_event_mgr * p_manager )
11361 OPJ_UINT32 l_nb_bytes_written = 0;
11362 OPJ_UINT32 l_current_nb_bytes_written;
11363 OPJ_BYTE * l_begin_data = 00;
11365 opj_tcp_v2_t *l_tcp = 00;
11366 opj_tcd_v2_t * l_tcd = 00;
11367 opj_cp_v2_t * l_cp = 00;
11369 l_tcd = p_j2k->m_tcd;
11370 l_cp = &(p_j2k->m_cp);
11371 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11373 l_tcd->cur_pino = 0;
11375 /*Get number of tile parts*/
11376 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11381 l_current_nb_bytes_written = 0;
11382 l_begin_data = p_data;
11383 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,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;
11392 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11393 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11394 l_current_nb_bytes_written = 0;
11395 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11396 l_nb_bytes_written += l_current_nb_bytes_written;
11397 p_data += l_current_nb_bytes_written;
11398 p_total_data_size -= l_current_nb_bytes_written;
11400 l_current_nb_bytes_written = 0;
11401 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11402 l_nb_bytes_written += l_current_nb_bytes_written;
11403 p_data += l_current_nb_bytes_written;
11404 p_total_data_size -= l_current_nb_bytes_written;
11407 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11408 l_current_nb_bytes_written = 0;
11409 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11410 l_nb_bytes_written += l_current_nb_bytes_written;
11411 p_data += l_current_nb_bytes_written;
11412 p_total_data_size -= l_current_nb_bytes_written;
11416 l_current_nb_bytes_written = 0;
11417 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11421 l_nb_bytes_written += l_current_nb_bytes_written;
11422 * p_data_written = l_nb_bytes_written;
11424 /* Writing Psot in SOT marker */
11425 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11427 if (l_cp->m_specific_param.m_enc.m_cinema){
11428 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11434 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11436 OPJ_UINT32 * p_data_written,
11437 OPJ_UINT32 p_total_data_size,
11438 opj_stream_private_t *p_stream,
11439 struct opj_event_mgr * p_manager
11442 OPJ_UINT32 tilepartno=0;
11443 OPJ_UINT32 l_nb_bytes_written = 0;
11444 OPJ_UINT32 l_current_nb_bytes_written;
11445 OPJ_UINT32 l_part_tile_size;
11446 OPJ_UINT32 tot_num_tp;
11449 OPJ_BYTE * l_begin_data;
11450 opj_tcp_v2_t *l_tcp = 00;
11451 opj_tcd_v2_t * l_tcd = 00;
11452 opj_cp_v2_t * l_cp = 00;
11455 l_tcd = p_j2k->m_tcd;
11456 l_cp = &(p_j2k->m_cp);
11457 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11459 /*Get number of tile parts*/
11460 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11462 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11463 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11464 l_current_nb_bytes_written = 0;
11465 l_part_tile_size = 0;
11466 l_begin_data = p_data;
11468 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11472 l_nb_bytes_written += l_current_nb_bytes_written;
11473 p_data += l_current_nb_bytes_written;
11474 p_total_data_size -= l_current_nb_bytes_written;
11475 l_part_tile_size += l_nb_bytes_written;
11477 l_current_nb_bytes_written = 0;
11478 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11482 p_data += l_current_nb_bytes_written;
11483 l_nb_bytes_written += l_current_nb_bytes_written;
11484 p_total_data_size -= l_current_nb_bytes_written;
11485 l_part_tile_size += l_nb_bytes_written;
11487 /* Writing Psot in SOT marker */
11488 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11490 if (l_cp->m_specific_param.m_enc.m_cinema) {
11491 j2k_update_tlm(p_j2k,l_part_tile_size);
11494 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11497 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11498 l_tcd->cur_pino = pino;
11500 /*Get number of tile parts*/
11501 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11502 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11503 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11504 l_current_nb_bytes_written = 0;
11505 l_part_tile_size = 0;
11506 l_begin_data = p_data;
11508 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11512 l_nb_bytes_written += l_current_nb_bytes_written;
11513 p_data += l_current_nb_bytes_written;
11514 p_total_data_size -= l_current_nb_bytes_written;
11515 l_part_tile_size += l_current_nb_bytes_written;
11517 l_current_nb_bytes_written = 0;
11519 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11523 l_nb_bytes_written += l_current_nb_bytes_written;
11524 p_data += l_current_nb_bytes_written;
11525 p_total_data_size -= l_current_nb_bytes_written;
11526 l_part_tile_size += l_current_nb_bytes_written;
11528 /* Writing Psot in SOT marker */
11529 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11531 if (l_cp->m_specific_param.m_enc.m_cinema) {
11532 j2k_update_tlm(p_j2k,l_part_tile_size);
11535 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11539 *p_data_written = l_nb_bytes_written;
11545 * Writes the updated tlm.
11547 * @param p_stream the stream to write data to.
11548 * @param p_j2k J2K codec.
11549 * @param p_manager the user event manager.
11551 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11552 struct opj_stream_private *p_stream,
11553 struct opj_event_mgr * p_manager )
11555 OPJ_UINT32 l_tlm_size;
11556 OPJ_SIZE_T l_tlm_position, l_current_position;
11558 /* preconditions */
11559 assert(p_j2k != 00);
11560 assert(p_manager != 00);
11561 assert(p_stream != 00);
11563 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11564 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11565 l_current_position = opj_stream_tell(p_stream);
11567 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11571 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) {
11575 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11584 * Ends the encoding, i.e. frees memory.
11586 * @param p_stream the stream to write data to.
11587 * @param p_j2k J2K codec.
11588 * @param p_manager the user event manager.
11590 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11591 struct opj_stream_private *p_stream,
11592 struct opj_event_mgr * p_manager )
11594 /* preconditions */
11595 assert(p_j2k != 00);
11596 assert(p_manager != 00);
11597 assert(p_stream != 00);
11599 tcd_destroy_v2(p_j2k->m_tcd);
11602 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11603 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11604 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11605 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11608 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11609 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11610 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11613 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11619 * Destroys the memory associated with the decoding of headers.
11621 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11622 opj_stream_private_t *p_stream,
11623 opj_event_mgr_t * p_manager )
11625 /* preconditions */
11626 assert(p_j2k != 00);
11627 assert(p_stream != 00);
11628 assert(p_manager != 00);
11630 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11631 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11632 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11635 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11644 * @param p_stream the stream to write data to.
11645 * @param p_j2k J2K codec.
11646 * @param p_manager the user event manager.
11648 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11649 struct opj_stream_private *p_stream,
11650 struct opj_event_mgr * p_manager )
11652 opj_codestream_info_t * l_cstr_info = 00;
11654 /* preconditions */
11655 assert(p_j2k != 00);
11656 assert(p_manager != 00);
11657 assert(p_stream != 00);
11660 /* TODO mergeV2: check this part which use cstr_info */
11661 /*l_cstr_info = p_j2k->cstr_info;
11665 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));
11667 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11668 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11670 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11672 l_cstr_info->tw = p_j2k->m_cp.tw;
11673 l_cstr_info->th = p_j2k->m_cp.th;
11675 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11676 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11677 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11678 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11680 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11682 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11684 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11686 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11687 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11690 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11692 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11694 /*l_cstr_info->maxmarknum = 100;
11695 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11696 l_cstr_info->marknum = 0;
11699 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);
11703 * Creates a tile-coder decoder.
11705 * @param p_stream the stream to write data to.
11706 * @param p_j2k J2K codec.
11707 * @param p_manager the user event manager.
11709 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11710 struct opj_stream_private *p_stream,
11711 struct opj_event_mgr * p_manager )
11713 /* preconditions */
11714 assert(p_j2k != 00);
11715 assert(p_manager != 00);
11716 assert(p_stream != 00);
11718 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11720 if (! p_j2k->m_tcd) {
11721 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11725 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11726 tcd_destroy_v2(p_j2k->m_tcd);
11737 * @param p_j2k the jpeg2000 codec.
11738 * @param p_stream the stream to write data to.
11739 * @param p_manager the user event manager.
11741 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11742 OPJ_UINT32 p_tile_index,
11744 OPJ_UINT32 p_data_size,
11745 opj_stream_private_t *p_stream,
11746 opj_event_mgr_t * p_manager )
11748 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11749 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11753 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11754 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);