2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
372 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 Write the SOC marker (Start Of Codestream)
382 @param j2k J2K handle
384 static void j2k_write_soc(opj_j2k_t *j2k);
387 * Writes the SOC marker (Start Of Codestream)
389 * @param p_stream the stream to write data to.
390 * @param p_j2k J2K codec.
391 * @param p_manager the user event manager.
393 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
394 struct opj_stream_private *p_stream,
395 struct opj_event_mgr * p_manager );
398 * Reads a SOC marker (Start of Codestream)
399 * @param p_header_data the data contained in the SOC box.
400 * @param jp2 the jpeg2000 file codec.
401 * @param p_header_size the size of the data contained in the SOC marker.
402 * @param p_manager the user event manager.
404 static opj_bool opj_j2k_read_soc(
406 struct opj_stream_private *p_stream,
407 struct opj_event_mgr * p_manager
411 Write the SIZ marker (image and tile size)
412 @param j2k J2K handle
414 static void j2k_write_siz(opj_j2k_t *j2k);
417 * Writes the SIZ marker (image and tile size)
419 * @param p_stream the stream to write data to.
420 * @param p_j2k J2K codec.
421 * @param p_manager the user event manager.
423 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
424 struct opj_stream_private *p_stream,
425 struct opj_event_mgr * p_manager );
428 * Reads a SIZ marker (image and tile size)
429 * @param p_header_data the data contained in the SIZ box.
430 * @param jp2 the jpeg2000 file codec.
431 * @param p_header_size the size of the data contained in the SIZ marker.
432 * @param p_manager the user event manager.
434 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
435 OPJ_BYTE * p_header_data,
436 OPJ_UINT32 p_header_size,
437 opj_event_mgr_t * p_manager);
440 Write the COM marker (comment)
441 @param j2k J2K handle
443 static void j2k_write_com(opj_j2k_t *j2k);
446 * Writes the COM marker (comment)
448 * @param p_stream the stream to write data to.
449 * @param p_j2k J2K codec.
450 * @param p_manager the user event manager.
452 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
453 struct opj_stream_private *p_stream,
454 struct opj_event_mgr * p_manager );
457 * Reads a COM marker (comments)
458 * @param p_header_data the data contained in the COM box.
459 * @param jp2 the jpeg2000 file codec.
460 * @param p_header_size the size of the data contained in the COM marker.
461 * @param p_manager the user event manager.
463 static opj_bool j2k_read_com_v2 (
465 OPJ_BYTE * p_header_data,
466 OPJ_UINT32 p_header_size,
467 struct opj_event_mgr * p_manager
470 Write the value concerning the specified component in the marker COD and COC
471 @param j2k J2K handle
472 @param compno Number of the component concerned by the information written
474 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
477 Write the COD marker (coding style default)
478 @param j2k J2K handle
480 static void j2k_write_cod(opj_j2k_t *j2k);
483 * Writes the COD marker (Coding style default)
485 * @param p_stream the stream to write data to.
486 * @param p_j2k J2K codec.
487 * @param p_manager the user event manager.
489 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
490 struct opj_stream_private *p_stream,
491 struct opj_event_mgr * p_manager );
494 * Reads a COD marker (Coding Styke defaults)
495 * @param p_header_data the data contained in the COD box.
496 * @param p_j2k the jpeg2000 codec.
497 * @param p_header_size the size of the data contained in the COD marker.
498 * @param p_manager the user event manager.
500 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
501 OPJ_BYTE * p_header_data,
502 OPJ_UINT32 p_header_size,
503 opj_event_mgr_t * p_manager);
506 Write the COC marker (coding style component)
507 @param j2k J2K handle
508 @param compno Number of the component concerned by the information written
510 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
513 * Writes the COC marker (Coding style component)
515 * @param p_comp_number the index of the component to output.
516 * @param p_stream the stream to write data to.
517 * @param p_j2k J2K codec.
518 * @param p_manager the user event manager.
520 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
521 OPJ_UINT32 p_comp_number,
522 struct opj_stream_private *p_stream,
523 struct opj_event_mgr * p_manager
526 * Writes the COC marker (Coding style component)
528 * @param p_comp_no the index of the component to output.
529 * @param p_stream the stream to write data to.
530 * @param p_j2k J2K codec.
531 * @param p_manager the user event manager.
533 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
534 OPJ_UINT32 p_comp_no,
536 OPJ_UINT32 * p_data_written,
537 struct opj_event_mgr * p_manager );
540 * Gets the maximum size taken by a coc.
542 * @param p_j2k the jpeg2000 codec to use.
544 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
548 * Reads a COC marker (Coding Style Component)
549 * @param p_header_data the data contained in the COC box.
550 * @param p_j2k the jpeg2000 codec.
551 * @param p_header_size the size of the data contained in the COC marker.
552 * @param p_manager the user event manager.
554 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
555 OPJ_BYTE * p_header_data,
556 OPJ_UINT32 p_header_size,
557 opj_event_mgr_t * p_manager );
560 Write the value concerning the specified component in the marker QCD and QCC
561 @param j2k J2K handle
562 @param compno Number of the component concerned by the information written
564 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
567 Write the QCD marker (quantization default)
568 @param j2k J2K handle
570 static void j2k_write_qcd(opj_j2k_t *j2k);
573 * Writes the QCD marker (quantization default)
575 * @param p_comp_number the index of the component to output.
576 * @param p_stream the stream to write data to.
577 * @param p_j2k J2K codec.
578 * @param p_manager the user event manager.
580 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
581 struct opj_stream_private *p_stream,
582 struct opj_event_mgr * p_manager );
585 * Reads a QCD marker (Quantization defaults)
586 * @param p_header_data the data contained in the QCD box.
587 * @param p_j2k the jpeg2000 codec.
588 * @param p_header_size the size of the data contained in the QCD marker.
589 * @param p_manager the user event manager.
591 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
592 OPJ_BYTE * p_header_data,
593 OPJ_UINT32 p_header_size,
594 opj_event_mgr_t * p_manager );
597 Write the QCC marker (quantization component)
598 @param j2k J2K handle
599 @param compno Number of the component concerned by the information written
601 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
604 * Writes the QCC marker (quantization component)
606 * @param p_comp_no the index of the component to output.
607 * @param p_stream the stream to write data to.
608 * @param p_j2k J2K codec.
609 * @param p_manager the user event manager.
611 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
612 OPJ_UINT32 p_comp_no,
613 struct opj_stream_private *p_stream,
614 struct opj_event_mgr * p_manager );
617 * Writes the QCC marker (quantization component)
619 * @param p_comp_no the index of the component to output.
620 * @param p_stream the stream to write data to.
621 * @param p_j2k J2K codec.
622 * @param p_manager the user event manager.
624 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
625 OPJ_UINT32 p_comp_no,
627 OPJ_UINT32 * p_data_written,
628 struct opj_event_mgr * p_manager );
631 * Gets the maximum size taken by a qcc.
633 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
636 * Reads a QCC marker (Quantization component)
637 * @param p_header_data the data contained in the QCC box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the QCC marker.
640 * @param p_manager the user event manager.
642 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
643 OPJ_BYTE * p_header_data,
644 OPJ_UINT32 p_header_size,
645 opj_event_mgr_t * p_manager);
648 Write the POC marker (progression order change)
649 @param j2k J2K handle
651 static void j2k_write_poc(opj_j2k_t *j2k);
654 * Writes the POC marker (Progression Order Change)
656 * @param p_stream the stream to write data to.
657 * @param p_j2k J2K codec.
658 * @param p_manager the user event manager.
660 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
661 struct opj_stream_private *p_stream,
662 struct opj_event_mgr * p_manager );
665 * Writes the POC marker (Progression Order Change)
667 * @param p_stream the stream to write data to.
668 * @param p_j2k J2K codec.
669 * @param p_manager the user event manager.
671 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
673 OPJ_UINT32 * p_data_written,
674 struct opj_event_mgr * p_manager );
677 * Gets the maximum size taken by the writting of a POC.
679 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
682 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
684 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
687 * Gets the maximum size taken by the headers of the SOT.
689 * @param p_j2k the jpeg2000 codec to use.
691 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
694 * Reads a POC marker (Progression Order Change)
696 * @param p_header_data the data contained in the POC box.
697 * @param p_j2k the jpeg2000 codec.
698 * @param p_header_size the size of the data contained in the POC marker.
699 * @param p_manager the user event manager.
701 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
702 OPJ_BYTE * p_header_data,
703 OPJ_UINT32 p_header_size,
704 opj_event_mgr_t * p_manager );
707 * Reads a CRG marker (Component registration)
709 * @param p_header_data the data contained in the TLM box.
710 * @param p_j2k the jpeg2000 codec.
711 * @param p_header_size the size of the data contained in the TLM marker.
712 * @param p_manager the user event manager.
714 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 opj_event_mgr_t * p_manager );
719 * Reads a TLM marker (Tile Length Marker)
721 * @param p_header_data the data contained in the TLM box.
722 * @param p_j2k the jpeg2000 codec.
723 * @param p_header_size the size of the data contained in the TLM marker.
724 * @param p_manager the user event manager.
726 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
727 OPJ_BYTE * p_header_data,
728 OPJ_UINT32 p_header_size,
729 opj_event_mgr_t * p_manager);
732 * Writes the updated tlm.
734 * @param p_stream the stream to write data to.
735 * @param p_j2k J2K codec.
736 * @param p_manager the user event manager.
738 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
739 struct opj_stream_private *p_stream,
740 struct opj_event_mgr * p_manager );
743 * Reads a PLM marker (Packet length, main header marker)
745 * @param p_header_data the data contained in the TLM box.
746 * @param p_j2k the jpeg2000 codec.
747 * @param p_header_size the size of the data contained in the TLM marker.
748 * @param p_manager the user event manager.
750 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
751 OPJ_BYTE * p_header_data,
752 OPJ_UINT32 p_header_size,
753 opj_event_mgr_t * p_manager);
755 * Reads a PLT marker (Packet length, tile-part header)
757 * @param p_header_data the data contained in the PLT box.
758 * @param p_j2k the jpeg2000 codec.
759 * @param p_header_size the size of the data contained in the PLT marker.
760 * @param p_manager the user event manager.
762 static opj_bool opj_j2k_read_plt ( 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 );
769 * Reads a PPM marker (Packed packet headers, main header)
771 * @param p_header_data the data contained in the POC box.
772 * @param p_j2k the jpeg2000 codec.
773 * @param p_header_size the size of the data contained in the POC marker.
774 * @param p_manager the user event manager.
777 static opj_bool j2k_read_ppm_v2 (
779 OPJ_BYTE * p_header_data,
780 OPJ_UINT32 p_header_size,
781 struct opj_event_mgr * p_manager
785 static opj_bool j2k_read_ppm_v3 (
787 OPJ_BYTE * p_header_data,
788 OPJ_UINT32 p_header_size,
789 struct opj_event_mgr * p_manager
794 * Reads a PPT marker (Packed packet headers, tile-part header)
796 * @param p_header_data the data contained in the PPT box.
797 * @param p_j2k the jpeg2000 codec.
798 * @param p_header_size the size of the data contained in the PPT marker.
799 * @param p_manager the user event manager.
801 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
802 OPJ_BYTE * p_header_data,
803 OPJ_UINT32 p_header_size,
804 opj_event_mgr_t * p_manager );
806 Write the TLM marker (Mainheader)
807 @param j2k J2K handle
809 static void j2k_write_tlm(opj_j2k_t *j2k);
812 * Writes the TLM marker (Tile Length Marker)
814 * @param p_stream the stream to write data to.
815 * @param p_j2k J2K codec.
816 * @param p_manager the user event manager.
818 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
819 struct opj_stream_private *p_stream,
820 struct opj_event_mgr * p_manager );
823 Write the SOT marker (start of tile-part)
824 @param j2k J2K handle
826 static void j2k_write_sot(opj_j2k_t *j2k);
829 * Writes the SOT marker (Start of tile-part)
831 * @param p_stream the stream to write data to.
832 * @param p_j2k J2K codec.
833 * @param p_manager the user event manager.
835 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
837 OPJ_UINT32 * p_data_written,
838 const struct opj_stream_private *p_stream,
839 struct opj_event_mgr * p_manager );
842 * Reads a PPT marker (Packed packet headers, tile-part header)
844 * @param p_header_data the data contained in the PPT box.
845 * @param p_j2k the jpeg2000 codec.
846 * @param p_header_size the size of the data contained in the PPT marker.
847 * @param p_manager the user event manager.
849 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
850 OPJ_BYTE * p_header_data,
851 OPJ_UINT32 p_header_size,
852 opj_event_mgr_t * p_manager );
855 Write the SOD marker (start of data)
856 @param j2k J2K handle
857 @param tile_coder Pointer to a TCD handle
859 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
862 * Writes the SOD marker (Start of data)
864 * @param p_stream the stream to write data to.
865 * @param p_j2k J2K codec.
866 * @param p_manager the user event manager.
868 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
869 struct opj_tcd_v2 * p_tile_coder,
871 OPJ_UINT32 * p_data_written,
872 OPJ_UINT32 p_total_data_size,
873 const struct opj_stream_private *p_stream,
874 struct opj_event_mgr * p_manager );
877 * Reads a SOD marker (Start Of Data)
879 * @param p_header_data the data contained in the SOD box.
880 * @param p_j2k the jpeg2000 codec.
881 * @param p_header_size the size of the data contained in the SOD marker.
882 * @param p_manager the user event manager.
884 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
885 opj_stream_private_t *p_stream,
886 opj_event_mgr_t * p_manager );
889 * Updates the Tile Length Marker.
891 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
893 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
894 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
896 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
897 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
901 Write the RGN marker (region-of-interest)
902 @param j2k J2K handle
903 @param compno Number of the component concerned by the information written
904 @param tileno Number of the tile concerned by the information written
906 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
909 * Writes the RGN marker (Region Of Interest)
911 * @param p_tile_no the tile to output
912 * @param p_comp_no the component to output
913 * @param p_stream the stream to write data to.
914 * @param p_j2k J2K codec.
915 * @param p_manager the user event manager.
917 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
918 OPJ_UINT32 p_tile_no,
919 OPJ_UINT32 p_comp_no,
920 struct opj_stream_private *p_stream,
921 struct opj_event_mgr * p_manager );
924 * Reads a RGN marker (Region Of Interest)
926 * @param p_header_data the data contained in the POC box.
927 * @param p_j2k the jpeg2000 codec.
928 * @param p_header_size the size of the data contained in the POC marker.
929 * @param p_manager the user event manager.
931 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
932 OPJ_BYTE * p_header_data,
933 OPJ_UINT32 p_header_size,
934 opj_event_mgr_t * p_manager );
937 * Writes the EOC marker (End of Codestream)
939 * @param p_stream the stream to write data to.
940 * @param p_j2k J2K codec.
941 * @param p_manager the user event manager.
943 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
944 struct opj_stream_private *p_stream,
945 struct opj_event_mgr * p_manager );
948 Write the EOC marker (end of codestream)
949 @param j2k J2K handle
951 static void j2k_write_eoc(opj_j2k_t *j2k);
953 Read the EOC marker (end of codestream)
954 @param j2k J2K handle
956 static void j2k_read_eoc(opj_j2k_t *j2k);
959 * Reads a EOC marker (End Of Codestream)
961 * @param p_header_data the data contained in the SOD box.
962 * @param p_j2k the jpeg2000 codec.
963 * @param p_header_size the size of the data contained in the SOD marker.
964 * @param p_manager the user event manager.
967 static opj_bool j2k_read_eoc_v2 (
969 struct opj_stream_private *p_stream,
970 struct opj_event_mgr * p_manager
977 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
979 * @param p_stream the stream to write data to.
980 * @param p_j2k J2K codec.
981 * @param p_manager the user event manager.
983 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
984 struct opj_stream_private *p_stream,
985 struct opj_event_mgr * p_manager );
990 * @param p_stream the stream to write data to.
991 * @param p_j2k J2K codec.
992 * @param p_manager the user event manager.
994 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
995 struct opj_stream_private *p_stream,
996 struct opj_event_mgr * p_manager );
999 Read an unknown marker
1000 @param j2k J2K handle
1002 static void j2k_read_unk(opj_j2k_t *j2k);
1004 Add main header marker information
1005 @param cstr_info Codestream information structure
1006 @param type marker type
1007 @param pos byte offset of marker segment
1008 @param len length of marker segment
1010 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1012 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1014 Add tile header marker information
1015 @param tileno tile index number
1016 @param cstr_info Codestream information structure
1017 @param type marker type
1018 @param pos byte offset of marker segment
1019 @param len length of marker segment
1021 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1023 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);
1026 * Reads an unknown marker
1028 * @param p_stream the stream object to read from.
1029 * @param p_j2k the jpeg2000 codec.
1030 * @param p_manager the user event manager.
1032 * @return true if the marker could be deduced.
1034 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1035 struct opj_stream_private *p_stream,
1036 OPJ_UINT32 *output_marker,
1037 struct opj_event_mgr * p_manager );
1040 * Writes the MCT marker (Multiple Component Transform)
1042 * @param p_stream the stream to write data to.
1043 * @param p_j2k J2K codec.
1044 * @param p_manager the user event manager.
1046 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1047 opj_mct_data_t * p_mct_record,
1048 struct opj_stream_private *p_stream,
1049 struct opj_event_mgr * p_manager );
1052 * Reads a MCT marker (Multiple Component Transform)
1054 * @param p_header_data the data contained in the MCT box.
1055 * @param p_j2k the jpeg2000 codec.
1056 * @param p_header_size the size of the data contained in the MCT marker.
1057 * @param p_manager the user event manager.
1059 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1060 OPJ_BYTE * p_header_data,
1061 OPJ_UINT32 p_header_size,
1062 struct opj_event_mgr * p_manager );
1065 * Writes the MCC marker (Multiple Component Collection)
1067 * @param p_stream the stream to write data to.
1068 * @param p_j2k J2K codec.
1069 * @param p_manager the user event manager.
1071 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1072 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1073 struct opj_stream_private *p_stream,
1074 struct opj_event_mgr * p_manager );
1078 * Reads a MCC marker (Multiple Component Collection)
1080 * @param p_header_data the data contained in the MCC box.
1081 * @param p_j2k the jpeg2000 codec.
1082 * @param p_header_size the size of the data contained in the MCC marker.
1083 * @param p_manager the user event manager.
1085 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1086 OPJ_BYTE * p_header_data,
1087 OPJ_UINT32 p_header_size,
1088 struct opj_event_mgr * p_manager );
1091 * Writes the MCO marker (Multiple component transformation ordering)
1093 * @param p_stream the stream to write data to.
1094 * @param p_j2k J2K codec.
1095 * @param p_manager the user event manager.
1097 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1098 struct opj_stream_private *p_stream,
1099 struct opj_event_mgr * p_manager );
1102 * Reads a MCO marker (Multiple Component Transform Ordering)
1104 * @param p_header_data the data contained in the MCO box.
1105 * @param p_j2k the jpeg2000 codec.
1106 * @param p_header_size the size of the data contained in the MCO marker.
1107 * @param p_manager the user event manager.
1109 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1110 OPJ_BYTE * p_header_data,
1111 OPJ_UINT32 p_header_size,
1112 struct opj_event_mgr * p_manager );
1114 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1116 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1117 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1118 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1119 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1121 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1123 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1124 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1127 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1128 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1129 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1133 * Ends the encoding, i.e. frees memory.
1135 * @param p_stream the stream to write data to.
1136 * @param p_j2k J2K codec.
1137 * @param p_manager the user event manager.
1139 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1140 struct opj_stream_private *p_stream,
1141 struct opj_event_mgr * p_manager );
1144 * Writes the CBD marker (Component bit depth definition)
1146 * @param p_stream the stream to write data to.
1147 * @param p_j2k J2K codec.
1148 * @param p_manager the user event manager.
1150 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1151 struct opj_stream_private *p_stream,
1152 struct opj_event_mgr * p_manager );
1155 * Reads a CBD marker (Component bit depth definition)
1156 * @param p_header_data the data contained in the CBD box.
1157 * @param p_j2k the jpeg2000 codec.
1158 * @param p_header_size the size of the data contained in the CBD marker.
1159 * @param p_manager the user event manager.
1161 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1162 OPJ_BYTE * p_header_data,
1163 OPJ_UINT32 p_header_size,
1164 struct opj_event_mgr * p_manager);
1167 * Writes the image components.
1169 * @param p_stream the stream to write data to.
1170 * @param p_j2k J2K codec.
1171 * @param p_manager the user event manager.
1173 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1174 struct opj_stream_private *p_stream,
1175 struct opj_event_mgr * p_manager );
1178 * Writes regions of interests.
1180 * @param p_stream the stream to write data to.
1181 * @param p_j2k J2K codec.
1182 * @param p_manager the user event manager.
1184 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1185 struct opj_stream_private *p_stream,
1186 struct opj_event_mgr * p_manager );
1191 * @param p_stream the stream to write data to.
1192 * @param p_j2k J2K codec.
1193 * @param p_manager the user event manager.
1195 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1196 struct opj_stream_private *p_stream,
1197 struct opj_event_mgr * p_manager );
1200 * Checks the progression order changes values. Tells of the poc given as input are valid.
1201 * A nice message is outputted at errors.
1203 * @param p_pocs the progression order changes.
1204 * @param p_nb_pocs the number of progression order changes.
1205 * @param p_nb_resolutions the number of resolutions.
1206 * @param numcomps the number of components
1207 * @param numlayers the number of layers.
1209 * @return true if the pocs are valid.
1211 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1212 OPJ_UINT32 p_nb_pocs,
1213 OPJ_UINT32 p_nb_resolutions,
1214 OPJ_UINT32 numcomps,
1215 OPJ_UINT32 numlayers,
1216 opj_event_mgr_t * p_manager);
1219 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1221 * @param cp the coding parameters.
1222 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1223 * @param tileno the given tile.
1225 * @return the number of tile parts.
1227 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1229 /** mem allocation for TLM marker*/
1230 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1233 * Calculates the total number of tile parts needed by the encoder to
1234 * encode such an image. If not enough memory is available, then the function return false.
1236 * @param p_nb_tiles pointer that will hold the number of tile parts.
1237 * @param cp the coding parameters for the image.
1238 * @param image the image to encode.
1239 * @param p_j2k the p_j2k encoder.
1240 * @param p_manager the user event manager.
1242 * @return true if the function was successful, false else.
1244 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1246 OPJ_UINT32 * p_nb_tiles,
1248 opj_event_mgr_t * p_manager);
1250 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1252 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1254 static opj_codestream_index_t* j2k_create_cstr_index(void);
1260 /* ----------------------------------------------------------------------- */
1261 typedef struct j2k_prog_order{
1262 OPJ_PROG_ORDER enum_prog;
1266 j2k_prog_order_t j2k_prog_order_list[] = {
1272 {(OPJ_PROG_ORDER)-1, ""}
1280 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1288 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1290 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1292 j2k_read_int16_to_float,
1293 j2k_read_int32_to_float,
1294 j2k_read_float32_to_float,
1295 j2k_read_float64_to_float
1298 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1300 j2k_read_int16_to_int32,
1301 j2k_read_int32_to_int32,
1302 j2k_read_float32_to_int32,
1303 j2k_read_float64_to_int32
1306 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1308 j2k_write_float_to_int16,
1309 j2k_write_float_to_int32,
1310 j2k_write_float_to_float,
1311 j2k_write_float_to_float64
1314 typedef struct opj_dec_memory_marker_handler
1318 /** value of the state when the marker can appear */
1320 /** action linked to the marker */
1321 opj_bool (*handler) (
1322 opj_j2k_v2_t *p_j2k,
1323 OPJ_BYTE * p_header_data,
1324 OPJ_UINT32 p_header_size,
1325 struct opj_event_mgr * p_manager
1328 opj_dec_memory_marker_handler_t;
1330 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1333 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1334 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1335 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1336 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1337 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1338 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1339 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1340 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1341 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1342 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1343 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1344 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1345 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1347 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1348 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1349 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1350 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1351 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1352 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1354 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1355 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1356 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1357 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1358 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1359 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1360 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1361 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1362 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1363 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1364 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1365 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1366 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1368 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1369 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1370 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1371 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1372 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1373 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1375 #ifdef TODO_MS /* FIXME */
1376 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1377 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1378 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1379 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1381 #endif /* USE_JPWL */
1383 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1384 {J2K_MS_INSEC, 0, j2k_read_insec}
1385 #endif /* USE_JPSEC */
1386 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1391 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1393 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1394 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1398 for (i=0;i<p_nb_elem;++i) {
1399 opj_read_bytes(l_src_data,&l_temp,2);
1401 l_src_data+=sizeof(OPJ_INT16);
1403 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1407 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1409 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1410 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1414 for (i=0;i<p_nb_elem;++i) {
1415 opj_read_bytes(l_src_data,&l_temp,4);
1417 l_src_data+=sizeof(OPJ_INT32);
1419 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1423 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1425 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1426 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1430 for (i=0;i<p_nb_elem;++i) {
1431 opj_read_float(l_src_data,&l_temp);
1433 l_src_data+=sizeof(OPJ_FLOAT32);
1435 *(l_dest_data++) = l_temp;
1439 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1441 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1442 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1446 for (i=0;i<p_nb_elem;++i) {
1447 opj_read_double(l_src_data,&l_temp);
1449 l_src_data+=sizeof(OPJ_FLOAT64);
1451 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1455 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1457 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1458 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1462 for (i=0;i<p_nb_elem;++i) {
1463 opj_read_bytes(l_src_data,&l_temp,2);
1465 l_src_data+=sizeof(OPJ_INT16);
1467 *(l_dest_data++) = (OPJ_INT32) l_temp;
1471 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1473 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1474 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1478 for (i=0;i<p_nb_elem;++i) {
1479 opj_read_bytes(l_src_data,&l_temp,4);
1481 l_src_data+=sizeof(OPJ_INT32);
1483 *(l_dest_data++) = (OPJ_INT32) l_temp;
1487 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1489 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1490 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1494 for (i=0;i<p_nb_elem;++i) {
1495 opj_read_float(l_src_data,&l_temp);
1497 l_src_data+=sizeof(OPJ_FLOAT32);
1499 *(l_dest_data++) = (OPJ_INT32) l_temp;
1503 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1505 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1506 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1510 for (i=0;i<p_nb_elem;++i) {
1511 opj_read_double(l_src_data,&l_temp);
1513 l_src_data+=sizeof(OPJ_FLOAT64);
1515 *(l_dest_data++) = (OPJ_INT32) l_temp;
1519 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1521 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1522 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1526 for (i=0;i<p_nb_elem;++i) {
1527 l_temp = (OPJ_UINT32) *(l_src_data++);
1529 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1531 l_dest_data+=sizeof(OPJ_INT16);
1535 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1537 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1538 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1542 for (i=0;i<p_nb_elem;++i) {
1543 l_temp = (OPJ_UINT32) *(l_src_data++);
1545 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1547 l_dest_data+=sizeof(OPJ_INT32);
1551 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1553 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1554 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1558 for (i=0;i<p_nb_elem;++i) {
1559 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1561 opj_write_float(l_dest_data,l_temp);
1563 l_dest_data+=sizeof(OPJ_FLOAT32);
1567 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1569 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1570 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1574 for (i=0;i<p_nb_elem;++i) {
1575 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1577 opj_write_double(l_dest_data,l_temp);
1579 l_dest_data+=sizeof(OPJ_FLOAT64);
1585 * Converts an enum type progression order to string type.
1587 * @param prg_order the progression order to get.
1589 * @return the string representation of the given progression order.
1591 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1592 j2k_prog_order_t *po;
1593 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1594 if(po->enum_prog == prg_order){
1595 return po->str_prog;
1598 return po->str_prog;
1602 * Checks the progression order changes values. Tells if the poc given as input are valid.
1604 * @param p_pocs the progression order changes.
1605 * @param p_nb_pocs the number of progression order changes.
1606 * @param p_nb_resolutions the number of resolutions.
1607 * @param numcomps the number of components
1608 * @param numlayers the number of layers.
1609 * @param p_manager the user event manager.
1611 * @return true if the pocs are valid.
1613 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1614 OPJ_UINT32 p_nb_pocs,
1615 OPJ_UINT32 p_nb_resolutions,
1616 OPJ_UINT32 p_num_comps,
1617 OPJ_UINT32 p_num_layers,
1618 opj_event_mgr_t * p_manager)
1620 OPJ_UINT32* packet_array;
1621 OPJ_UINT32 index , resno, compno, layno;
1623 OPJ_UINT32 step_c = 1;
1624 OPJ_UINT32 step_r = p_num_comps * step_c;
1625 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1626 opj_bool loss = OPJ_FALSE;
1627 OPJ_UINT32 layno0 = 0;
1629 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1630 if (packet_array == 00) {
1631 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1634 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1636 if (p_nb_pocs == 0) {
1640 index = step_r * p_pocs->resno0;
1641 // take each resolution for each poc
1642 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1644 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1646 // take each comp of each resolution for each poc
1647 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1648 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1650 // and finally take each layer of each res of ...
1651 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1652 //index = step_r * resno + step_c * compno + step_l * layno;
1653 packet_array[comp_index] = 1;
1654 comp_index += step_l;
1657 res_index += step_c;
1664 // iterate through all the pocs
1665 for (i = 1; i < p_nb_pocs ; ++i) {
1666 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1668 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1669 index = step_r * p_pocs->resno0;
1671 // take each resolution for each poc
1672 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1673 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1675 // take each comp of each resolution for each poc
1676 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1677 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1679 // and finally take each layer of each res of ...
1680 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1681 //index = step_r * resno + step_c * compno + step_l * layno;
1682 packet_array[comp_index] = 1;
1683 comp_index += step_l;
1686 res_index += step_c;
1696 for (layno = 0; layno < p_num_layers ; ++layno) {
1697 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1698 for (compno = 0; compno < p_num_comps; ++compno) {
1699 loss |= (packet_array[index]!=1);
1700 //index = step_r * resno + step_c * compno + step_l * layno;
1707 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1710 opj_free(packet_array);
1715 /* ----------------------------------------------------------------------- */
1716 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1719 int tpnum=1,tpend=0;
1720 opj_tcp_t *tcp = &cp->tcps[tileno];
1721 prog = j2k_convert_progression_order(tcp->prg);
1726 if( cp->tp_flag == prog[i] ){
1727 tpend=1;cp->tp_pos=i;
1731 tpnum= tpnum * tcp->pocs[pino].compE;
1734 tpnum= tpnum * tcp->pocs[pino].resE;
1737 tpnum= tpnum * tcp->pocs[pino].prcE;
1740 tpnum= tpnum * tcp->pocs[pino].layE;
1752 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1754 * @param cp the coding parameters.
1755 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1756 * @param tileno the given tile.
1758 * @return the number of tile parts.
1760 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1762 const OPJ_CHAR *prog = 00;
1764 OPJ_UINT32 tpnum = 1;
1765 opj_tcp_v2_t *tcp = 00;
1766 opj_poc_t * l_current_poc = 00;
1769 assert(tileno < (cp->tw * cp->th));
1770 assert(pino < (cp->tcps[tileno].numpocs + 1));
1772 /* get the given tile coding parameter */
1773 tcp = &cp->tcps[tileno];
1776 l_current_poc = &(tcp->pocs[pino]);
1777 assert(l_current_poc != 0);
1779 /* get the progression order as a character string */
1780 prog = j2k_convert_progression_order(tcp->prg);
1781 assert(strlen(prog) > 0);
1783 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1787 /* component wise */
1789 tpnum *= l_current_poc->compE;
1791 /* resolution wise */
1793 tpnum *= l_current_poc->resE;
1797 tpnum *= l_current_poc->prcE;
1801 tpnum *= l_current_poc->layE;
1804 /* whould we split here ? */
1805 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1806 cp->m_specific_param.m_enc.m_tp_pos=i;
1818 /** mem allocation for TLM marker*/
1819 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1820 int pino,tileno,totnum_tp=0;
1822 OPJ_ARG_NOT_USED(img_numcomp);
1824 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1825 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1826 int cur_totnum_tp = 0;
1827 opj_tcp_t *tcp = &cp->tcps[tileno];
1828 for(pino = 0; pino <= tcp->numpocs; pino++) {
1830 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1831 if(!pi) { return -1;}
1832 tp_num = j2k_get_num_tp(cp,pino,tileno);
1833 totnum_tp = totnum_tp + tp_num;
1834 cur_totnum_tp = cur_totnum_tp + tp_num;
1835 pi_destroy(pi, cp, tileno);
1837 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1839 if (j2k->cstr_info) {
1840 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1841 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1849 * Calculates the total number of tile parts needed by the encoder to
1850 * encode such an image. If not enough memory is available, then the function return false.
1852 * @param p_nb_tiles pointer that will hold the number of tile parts.
1853 * @param cp the coding parameters for the image.
1854 * @param image the image to encode.
1855 * @param p_j2k the p_j2k encoder.
1856 * @param p_manager the user event manager.
1858 * @return true if the function was successful, false else.
1860 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1862 OPJ_UINT32 * p_nb_tiles,
1864 opj_event_mgr_t * p_manager)
1866 OPJ_UINT32 pino,tileno;
1867 OPJ_UINT32 l_nb_tiles;
1871 assert(p_nb_tiles != 00);
1873 assert(image != 00);
1874 assert(p_j2k != 00);
1875 assert(p_manager != 00);
1877 l_nb_tiles = cp->tw * cp->th;
1882 /* TODO mergeV2: check this part which use cstr_info */
1883 /*if (p_j2k->cstr_info) {
1884 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1886 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1887 OPJ_UINT32 cur_totnum_tp = 0;
1889 pi_update_encoding_parameters(image,cp,tileno);
1891 for (pino = 0; pino <= tcp->numpocs; ++pino)
1893 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1895 *p_nb_tiles = *p_nb_tiles + tp_num;
1897 cur_totnum_tp += tp_num;
1900 tcp->m_nb_tile_parts = cur_totnum_tp;
1902 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1903 if (l_info_tile_ptr->tp == 00) {
1907 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1909 l_info_tile_ptr->num_tps = cur_totnum_tp;
1916 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1917 OPJ_UINT32 cur_totnum_tp = 0;
1919 pi_update_encoding_parameters(image,cp,tileno);
1921 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1922 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1924 *p_nb_tiles = *p_nb_tiles + tp_num;
1926 cur_totnum_tp += tp_num;
1928 tcp->m_nb_tile_parts = cur_totnum_tp;
1937 static void j2k_write_soc(opj_j2k_t *j2k) {
1938 opj_cio_t *cio = j2k->cio;
1939 cio_write(cio, J2K_MS_SOC, 2);
1942 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1947 /* update markers struct */
1948 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1949 #endif /* USE_JPWL */
1954 * Writes the SOC marker (Start Of Codestream)
1956 * @param p_stream the stream to write data to.
1957 * @param p_j2k J2K codec.
1958 * @param p_manager the user event manager.
1960 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1961 struct opj_stream_private *p_stream,
1962 struct opj_event_mgr * p_manager )
1964 /* 2 bytes will be written */
1965 OPJ_BYTE * l_start_stream = 00;
1968 assert(p_stream != 00);
1969 assert(p_j2k != 00);
1970 assert(p_manager != 00);
1972 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1974 /* write SOC identifier */
1975 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1977 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1983 /* update markers struct */
1985 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1987 assert( 0 && "TODO" );
1988 #endif /* USE_JPWL */
1997 * Reads a SOC marker (Start of Codestream)
1998 * @param p_header_data the data contained in the SOC box.
1999 * @param jp2 the jpeg2000 file codec.
2000 * @param p_header_size the size of the data contained in the SOC marker.
2001 * @param p_manager the user event manager.
2003 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2004 struct opj_stream_private *p_stream,
2005 struct opj_event_mgr * p_manager )
2007 OPJ_BYTE l_data [2];
2008 OPJ_UINT32 l_marker;
2011 assert(p_j2k != 00);
2012 assert(p_manager != 00);
2013 assert(p_stream != 00);
2015 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2019 opj_read_bytes(l_data,&l_marker,2);
2020 if (l_marker != J2K_MS_SOC) {
2024 /* Next marker should be a SIZ marker in the main header */
2025 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2027 /* FIXME move it in a index structure included in p_j2k*/
2028 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2030 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2032 /* Add the marker to the codestream index*/
2033 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2038 static void j2k_write_siz(opj_j2k_t *j2k) {
2042 opj_cio_t *cio = j2k->cio;
2043 opj_image_t *image = j2k->image;
2044 opj_cp_t *cp = j2k->cp;
2046 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2047 lenp = cio_tell(cio);
2049 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2050 cio_write(cio, image->x1, 4); /* Xsiz */
2051 cio_write(cio, image->y1, 4); /* Ysiz */
2052 cio_write(cio, image->x0, 4); /* X0siz */
2053 cio_write(cio, image->y0, 4); /* Y0siz */
2054 cio_write(cio, cp->tdx, 4); /* XTsiz */
2055 cio_write(cio, cp->tdy, 4); /* YTsiz */
2056 cio_write(cio, cp->tx0, 4); /* XT0siz */
2057 cio_write(cio, cp->ty0, 4); /* YT0siz */
2058 cio_write(cio, image->numcomps, 2); /* Csiz */
2059 for (i = 0; i < image->numcomps; i++) {
2060 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2061 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2062 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2064 len = cio_tell(cio) - lenp;
2065 cio_seek(cio, lenp);
2066 cio_write(cio, len, 2); /* Lsiz */
2067 cio_seek(cio, lenp + len);
2070 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2074 * Writes the SIZ marker (image and tile size)
2076 * @param p_stream the stream to write data to.
2077 * @param p_j2k J2K codec.
2078 * @param p_manager the user event manager.
2080 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2081 struct opj_stream_private *p_stream,
2082 struct opj_event_mgr * p_manager )
2085 OPJ_UINT32 l_size_len;
2086 OPJ_BYTE * l_current_ptr;
2087 opj_image_t * l_image = 00;
2088 opj_cp_v2_t *cp = 00;
2089 opj_image_comp_t * l_img_comp = 00;
2092 assert(p_stream != 00);
2093 assert(p_j2k != 00);
2094 assert(p_manager != 00);
2096 l_image = p_j2k->m_private_image;
2097 cp = &(p_j2k->m_cp);
2098 l_size_len = 40 + 3 * l_image->numcomps;
2099 l_img_comp = l_image->comps;
2101 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2103 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2104 = (OPJ_BYTE*)opj_realloc(
2105 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2107 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2111 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2114 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2116 /* write SOC identifier */
2117 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2120 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2123 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2126 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2129 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2132 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2135 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2138 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2141 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2144 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2147 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2150 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2153 for (i = 0; i < l_image->numcomps; ++i) {
2154 /* TODO here with MCT ? */
2155 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2158 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2161 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2167 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) {
2177 * Reads a SIZ marker (image and tile size)
2178 * @param p_header_data the data contained in the SIZ box.
2179 * @param jp2 the jpeg2000 file codec.
2180 * @param p_header_size the size of the data contained in the SIZ marker.
2181 * @param p_manager the user event manager.
2183 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2184 OPJ_BYTE * p_header_data,
2185 OPJ_UINT32 p_header_size,
2186 opj_event_mgr_t * p_manager
2189 OPJ_UINT32 l_size, i;
2190 OPJ_UINT32 l_nb_comp;
2191 OPJ_UINT32 l_nb_comp_remain;
2192 OPJ_UINT32 l_remaining_size;
2193 OPJ_UINT32 l_nb_tiles;
2195 opj_image_t *l_image = 00;
2196 opj_cp_v2_t *l_cp = 00;
2197 opj_image_comp_t * l_img_comp = 00;
2198 opj_tcp_v2_t * l_current_tile_param = 00;
2201 assert(p_j2k != 00);
2202 assert(p_manager != 00);
2203 assert(p_header_data != 00);
2205 l_image = p_j2k->m_private_image;
2206 l_cp = &(p_j2k->m_cp);
2208 /* minimum size == 39 - 3 (= minimum component parameter) */
2209 if (p_header_size < 36) {
2210 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2214 l_remaining_size = p_header_size - 36;
2215 l_nb_comp = l_remaining_size / 3;
2216 l_nb_comp_remain = l_remaining_size % 3;
2217 if (l_nb_comp_remain != 0){
2218 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2222 l_size = p_header_size + 2; /* Lsiz */
2224 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2226 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2227 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2229 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2231 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2233 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2235 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2237 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2239 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2241 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2243 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2246 l_image->numcomps = (OPJ_UINT16) l_tmp;
2248 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2252 if (l_image->numcomps != l_nb_comp) {
2253 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);
2258 if (l_cp->correct) {
2259 /* if JPWL is on, we check whether TX errors have damaged
2260 too much the SIZ parameters */
2261 if (!(l_image->x1 * l_image->y1)) {
2262 opj_event_msg_v2(p_manager, EVT_ERROR,
2263 "JPWL: bad image size (%d x %d)\n",
2264 l_image->x1, l_image->y1);
2265 if (!JPWL_ASSUME || JPWL_ASSUME) {
2266 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2271 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2272 if (l_image->numcomps != ((len - 38) / 3)) {
2273 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2274 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2275 l_image->numcomps, ((len - 38) / 3));
2277 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2280 */ /* we try to correct */
2281 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2282 if (l_image->numcomps < ((len - 38) / 3)) {
2283 len = 38 + 3 * l_image->numcomps;
2284 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2287 l_image->numcomps = ((len - 38) / 3);
2288 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2294 /* update components number in the jpwl_exp_comps filed */
2295 l_cp->exp_comps = l_image->numcomps;
2297 #endif /* USE_JPWL */
2299 /* Allocate the resulting image components */
2300 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2301 if (l_image->comps == 00){
2302 l_image->numcomps = 0;
2303 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2307 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2308 l_img_comp = l_image->comps;
2310 /* Read the component information */
2311 for (i = 0; i < l_image->numcomps; ++i){
2313 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2315 l_img_comp->prec = (tmp & 0x7f) + 1;
2316 l_img_comp->sgnd = tmp >> 7;
2317 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2319 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2320 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2322 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2325 if (l_cp->correct) {
2326 /* if JPWL is on, we check whether TX errors have damaged
2327 too much the SIZ parameters, again */
2328 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2329 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2330 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2331 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2333 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2336 /* we try to correct */
2337 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2338 if (!l_image->comps[i].dx) {
2339 l_image->comps[i].dx = 1;
2340 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2341 i, l_image->comps[i].dx);
2343 if (!l_image->comps[i].dy) {
2344 l_image->comps[i].dy = 1;
2345 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2346 i, l_image->comps[i].dy);
2350 #endif /* USE_JPWL */
2351 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2352 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2356 /* Compute the number of tiles */
2357 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2358 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2359 l_nb_tiles = l_cp->tw * l_cp->th;
2361 /* Define the tiles which will be decoded */
2362 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2363 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;
2364 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;
2365 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);
2366 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);
2369 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2370 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2371 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2372 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2376 if (l_cp->correct) {
2377 /* if JPWL is on, we check whether TX errors have damaged
2378 too much the SIZ parameters */
2379 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2380 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2381 "JPWL: bad number of tiles (%d x %d)\n",
2382 l_cp->tw, l_cp->th);
2384 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2387 /* we try to correct */
2388 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2391 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2394 if (l_cp->tw > l_cp->max_tiles) {
2396 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2397 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2398 l_cp->max_tiles, l_cp->tw);
2402 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2405 if (l_cp->th > l_cp->max_tiles) {
2407 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2408 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2409 l_cp->max_tiles, l_cp->th);
2413 #endif /* USE_JPWL */
2415 /* memory allocations */
2416 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2417 if (l_cp->tcps == 00) {
2418 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2421 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2424 if (l_cp->correct) {
2426 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2427 "JPWL: could not alloc tcps field of cp\n");
2428 if (!JPWL_ASSUME || JPWL_ASSUME) {
2429 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2434 #endif /* USE_JPWL */
2436 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2437 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2438 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2439 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2442 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2444 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2445 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2447 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2448 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2451 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));
2452 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2454 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2455 (opj_simple_mcc_decorrelation_data_t*)
2456 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2458 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2459 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2462 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));
2463 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2465 /* set up default dc level shift */
2466 for (i=0;i<l_image->numcomps;++i) {
2467 if (! l_image->comps[i].sgnd) {
2468 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2472 l_current_tile_param = l_cp->tcps;
2473 for (i = 0; i < l_nb_tiles; ++i) {
2474 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2475 if (l_current_tile_param->tccps == 00) {
2476 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2479 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2481 ++l_current_tile_param;
2484 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2485 opj_image_comp_header_update(l_image,l_cp);
2492 static void j2k_write_com(opj_j2k_t *j2k) {
2496 if(j2k->cp->comment) {
2497 opj_cio_t *cio = j2k->cio;
2498 char *comment = j2k->cp->comment;
2500 cio_write(cio, J2K_MS_COM, 2);
2501 lenp = cio_tell(cio);
2503 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2504 for (i = 0; i < strlen(comment); i++) {
2505 cio_write(cio, comment[i], 1);
2507 len = cio_tell(cio) - lenp;
2508 cio_seek(cio, lenp);
2509 cio_write(cio, len, 2);
2510 cio_seek(cio, lenp + len);
2514 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2520 * Writes the COM marker (comment)
2522 * @param p_stream the stream to write data to.
2523 * @param p_j2k J2K codec.
2524 * @param p_manager the user event manager.
2526 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2527 struct opj_stream_private *p_stream,
2528 struct opj_event_mgr * p_manager )
2530 OPJ_UINT32 l_comment_size;
2531 OPJ_UINT32 l_total_com_size;
2532 const OPJ_CHAR *l_comment;
2533 OPJ_BYTE * l_current_ptr = 00;
2536 assert(p_j2k != 00);
2537 assert(p_stream != 00);
2538 assert(p_manager != 00);
2540 l_comment = p_j2k->m_cp.comment;
2541 l_comment_size = strlen(l_comment);
2542 l_total_com_size = l_comment_size + 6;
2544 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2545 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2546 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2549 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2553 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2556 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2558 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2561 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2564 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2567 memcpy( l_current_ptr,l_comment,l_comment_size);
2569 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) {
2577 * Reads a COM marker (comments)
2578 * @param p_header_data the data contained in the COM box.
2579 * @param jp2 the jpeg2000 file codec.
2580 * @param p_header_size the size of the data contained in the COM marker.
2581 * @param p_manager the user event manager.
2583 opj_bool j2k_read_com_v2 (
2584 opj_j2k_v2_t *p_j2k,
2585 OPJ_BYTE * p_header_data,
2586 OPJ_UINT32 p_header_size,
2587 struct opj_event_mgr * p_manager
2591 assert(p_j2k != 00);
2592 assert(p_manager != 00);
2593 assert(p_header_data != 00);
2594 (void)p_header_size;
2599 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2602 opj_cp_t *cp = j2k->cp;
2603 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2604 opj_tccp_t *tccp = &tcp->tccps[compno];
2605 opj_cio_t *cio = j2k->cio;
2607 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2608 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2609 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2610 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2611 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2613 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2614 for (i = 0; i < tccp->numresolutions; i++) {
2615 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2621 static void j2k_write_cod(opj_j2k_t *j2k) {
2622 opj_cp_t *cp = NULL;
2623 opj_tcp_t *tcp = NULL;
2626 opj_cio_t *cio = j2k->cio;
2628 cio_write(cio, J2K_MS_COD, 2); /* COD */
2630 lenp = cio_tell(cio);
2634 tcp = &cp->tcps[j2k->curtileno];
2636 cio_write(cio, tcp->csty, 1); /* Scod */
2637 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2638 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2639 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2641 j2k_write_cox(j2k, 0);
2642 len = cio_tell(cio) - lenp;
2643 cio_seek(cio, lenp);
2644 cio_write(cio, len, 2); /* Lcod */
2645 cio_seek(cio, lenp + len);
2648 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2653 * Writes the COD marker (Coding style default)
2655 * @param p_stream the stream to write data to.
2656 * @param p_j2k J2K codec.
2657 * @param p_manager the user event manager.
2659 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2660 struct opj_stream_private *p_stream,
2661 struct opj_event_mgr * p_manager )
2663 opj_cp_v2_t *l_cp = 00;
2664 opj_tcp_v2_t *l_tcp = 00;
2665 OPJ_UINT32 l_code_size,l_remaining_size;
2666 OPJ_BYTE * l_current_data = 00;
2669 assert(p_j2k != 00);
2670 assert(p_manager != 00);
2671 assert(p_stream != 00);
2673 l_cp = &(p_j2k->m_cp);
2674 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2675 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2676 l_remaining_size = l_code_size;
2678 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2679 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2680 = (OPJ_BYTE*)opj_realloc(
2681 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2684 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2688 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2691 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2693 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2694 l_current_data += 2;
2696 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2697 l_current_data += 2;
2699 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2702 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2705 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2708 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2711 l_remaining_size -= 9;
2713 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2714 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2718 if (l_remaining_size != 0) {
2719 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2723 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) {
2732 * Reads a COD marker (Coding Styke defaults)
2733 * @param p_header_data the data contained in the COD box.
2734 * @param p_j2k the jpeg2000 codec.
2735 * @param p_header_size the size of the data contained in the COD marker.
2736 * @param p_manager the user event manager.
2738 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2739 OPJ_BYTE * p_header_data,
2740 OPJ_UINT32 p_header_size,
2741 opj_event_mgr_t * p_manager
2747 opj_cp_v2_t *l_cp = 00;
2748 opj_tcp_v2_t *l_tcp = 00;
2749 opj_image_t *l_image = 00;
2752 assert(p_header_data != 00);
2753 assert(p_j2k != 00);
2754 assert(p_manager != 00);
2756 l_image = p_j2k->m_private_image;
2757 l_cp = &(p_j2k->m_cp);
2759 /* If we are in the first tile-part header of the current tile */
2760 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2761 &l_cp->tcps[p_j2k->m_current_tile_number] :
2762 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2764 /* Make sure room is sufficient */
2765 if (p_header_size < 5) {
2766 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2770 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2772 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2774 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2775 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2778 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2779 if (l_cp->m_specific_param.m_dec.m_layer) {
2780 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2783 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2786 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2790 for (i = 0; i < l_image->numcomps; ++i) {
2791 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2794 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2795 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2799 if (p_header_size != 0) {
2800 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2804 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2805 j2k_copy_tile_component_parameters(p_j2k);
2808 #ifdef WIP_REMOVE_MSD
2809 if (p_j2k->cstr_info) {
2810 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2811 p_j2k->cstr_info->prog = l_tcp->prg;
2812 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2813 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2814 for (i = 0; i < l_image->numcomps; ++i) {
2815 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2823 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2826 opj_cp_t *cp = j2k->cp;
2827 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2828 opj_image_t *image = j2k->image;
2829 opj_cio_t *cio = j2k->cio;
2831 cio_write(cio, J2K_MS_COC, 2); /* COC */
2832 lenp = cio_tell(cio);
2834 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2835 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2836 j2k_write_cox(j2k, compno);
2837 len = cio_tell(cio) - lenp;
2838 cio_seek(cio, lenp);
2839 cio_write(cio, len, 2); /* Lcoc */
2840 cio_seek(cio, lenp + len);
2844 * Writes the COC marker (Coding style component)
2846 * @param p_comp_no the index of the component to output.
2847 * @param p_stream the stream to write data to.
2848 * @param p_j2k J2K codec.
2849 * @param p_manager the user event manager.
2851 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2852 OPJ_UINT32 p_comp_no,
2853 struct opj_stream_private *p_stream,
2854 struct opj_event_mgr * p_manager )
2856 OPJ_UINT32 l_coc_size,l_remaining_size;
2857 OPJ_UINT32 l_comp_room;
2860 assert(p_j2k != 00);
2861 assert(p_manager != 00);
2862 assert(p_stream != 00);
2864 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2866 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2868 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2869 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2870 = (OPJ_BYTE*)opj_realloc(
2871 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2873 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2877 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2880 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);
2882 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) {
2890 * Writes the COC marker (Coding style component)
2892 * @param p_comp_no the index of the component to output.
2893 * @param p_stream the stream to write data to.
2894 * @param p_j2k J2K codec.
2895 * @param p_manager the user event manager.
2897 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2898 OPJ_UINT32 p_comp_no,
2900 OPJ_UINT32 * p_data_written,
2901 struct opj_event_mgr * p_manager )
2903 opj_cp_v2_t *l_cp = 00;
2904 opj_tcp_v2_t *l_tcp = 00;
2905 OPJ_UINT32 l_coc_size,l_remaining_size;
2906 OPJ_BYTE * l_current_data = 00;
2907 opj_image_t *l_image = 00;
2908 OPJ_UINT32 l_comp_room;
2911 assert(p_j2k != 00);
2912 assert(p_manager != 00);
2914 l_cp = &(p_j2k->m_cp);
2915 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2916 l_image = p_j2k->m_private_image;
2917 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2919 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2920 l_remaining_size = l_coc_size;
2922 l_current_data = p_data;
2924 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2925 l_current_data += 2;
2927 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2928 l_current_data += 2;
2930 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2931 l_current_data+=l_comp_room;
2933 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2936 l_remaining_size -= (5 + l_comp_room);
2937 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2938 * p_data_written = l_coc_size;
2942 * Gets the maximum size taken by a coc.
2944 * @param p_j2k the jpeg2000 codec to use.
2946 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2949 OPJ_UINT32 l_nb_comp;
2950 OPJ_UINT32 l_nb_tiles;
2951 OPJ_UINT32 l_max = 0;
2955 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2956 l_nb_comp = p_j2k->m_private_image->numcomps;
2958 for (i=0;i<l_nb_tiles;++i) {
2959 for (j=0;j<l_nb_comp;++j) {
2960 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2969 * Reads a COC marker (Coding Style Component)
2970 * @param p_header_data the data contained in the COC box.
2971 * @param p_j2k the jpeg2000 codec.
2972 * @param p_header_size the size of the data contained in the COC marker.
2973 * @param p_manager the user event manager.
2975 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2976 OPJ_BYTE * p_header_data,
2977 OPJ_UINT32 p_header_size,
2978 opj_event_mgr_t * p_manager
2981 opj_cp_v2_t *l_cp = NULL;
2982 opj_tcp_v2_t *l_tcp = NULL;
2983 opj_image_t *l_image = NULL;
2984 OPJ_UINT32 l_comp_room;
2985 OPJ_UINT32 l_comp_no;
2988 assert(p_header_data != 00);
2989 assert(p_j2k != 00);
2990 assert(p_manager != 00);
2992 l_cp = &(p_j2k->m_cp);
2993 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2994 &l_cp->tcps[p_j2k->m_current_tile_number] :
2995 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2996 l_image = p_j2k->m_private_image;
2998 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3000 /* make sure room is sufficient*/
3001 if (p_header_size < l_comp_room + 1) {
3002 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3005 p_header_size -= l_comp_room + 1;
3007 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3008 p_header_data += l_comp_room;
3009 if (l_comp_no >= l_image->numcomps) {
3010 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3014 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3017 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3018 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3022 if (p_header_size != 0) {
3023 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3029 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3030 int bandno, numbands;
3033 opj_cp_t *cp = j2k->cp;
3034 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3035 opj_tccp_t *tccp = &tcp->tccps[compno];
3036 opj_cio_t *cio = j2k->cio;
3038 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3039 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3041 for (bandno = 0; bandno < numbands; bandno++) {
3042 expn = tccp->stepsizes[bandno].expn;
3043 mant = tccp->stepsizes[bandno].mant;
3045 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3046 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3048 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3054 static void j2k_write_qcd(opj_j2k_t *j2k) {
3057 opj_cio_t *cio = j2k->cio;
3059 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3060 lenp = cio_tell(cio);
3062 j2k_write_qcx(j2k, 0);
3063 len = cio_tell(cio) - lenp;
3064 cio_seek(cio, lenp);
3065 cio_write(cio, len, 2); /* Lqcd */
3066 cio_seek(cio, lenp + len);
3069 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3073 * Writes the QCD marker (quantization default)
3075 * @param p_comp_number the index of the component to output.
3076 * @param p_stream the stream to write data to.
3077 * @param p_j2k J2K codec.
3078 * @param p_manager the user event manager.
3080 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3081 struct opj_stream_private *p_stream,
3082 struct opj_event_mgr * p_manager )
3084 opj_cp_v2_t *l_cp = 00;
3085 opj_tcp_v2_t *l_tcp = 00;
3086 OPJ_UINT32 l_qcd_size,l_remaining_size;
3087 OPJ_BYTE * l_current_data = 00;
3090 assert(p_j2k != 00);
3091 assert(p_manager != 00);
3092 assert(p_stream != 00);
3094 l_cp = &(p_j2k->m_cp);
3095 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3096 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3097 l_remaining_size = l_qcd_size;
3099 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3100 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3101 = (OPJ_BYTE*)opj_realloc(
3102 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3105 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3109 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3112 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3114 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3115 l_current_data += 2;
3117 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3118 l_current_data += 2;
3120 l_remaining_size -= 4;
3122 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3123 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3127 if (l_remaining_size != 0) {
3128 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3132 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) {
3140 * Reads a QCD marker (Quantization defaults)
3141 * @param p_header_data the data contained in the QCD box.
3142 * @param p_j2k the jpeg2000 codec.
3143 * @param p_header_size the size of the data contained in the QCD marker.
3144 * @param p_manager the user event manager.
3146 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3147 OPJ_BYTE * p_header_data,
3148 OPJ_UINT32 p_header_size,
3149 opj_event_mgr_t * p_manager
3153 assert(p_header_data != 00);
3154 assert(p_j2k != 00);
3155 assert(p_manager != 00);
3157 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3158 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3162 if (p_header_size != 0) {
3163 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3167 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3168 j2k_copy_tile_quantization_parameters(p_j2k);
3173 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3176 opj_cio_t *cio = j2k->cio;
3178 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3179 lenp = cio_tell(cio);
3181 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3182 j2k_write_qcx(j2k, compno);
3183 len = cio_tell(cio) - lenp;
3184 cio_seek(cio, lenp);
3185 cio_write(cio, len, 2); /* Lqcc */
3186 cio_seek(cio, lenp + len);
3190 * Writes the QCC marker (quantization component)
3192 * @param p_comp_no the index of the component to output.
3193 * @param p_stream the stream to write data to.
3194 * @param p_j2k J2K codec.
3195 * @param p_manager the user event manager.
3197 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3198 OPJ_UINT32 p_comp_no,
3199 struct opj_stream_private *p_stream,
3200 struct opj_event_mgr * p_manager )
3202 OPJ_UINT32 l_qcc_size,l_remaining_size;
3205 assert(p_j2k != 00);
3206 assert(p_manager != 00);
3207 assert(p_stream != 00);
3209 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3210 l_remaining_size = l_qcc_size;
3212 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3213 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3214 = (OPJ_BYTE*)opj_realloc(
3215 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3217 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3221 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3224 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);
3226 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) {
3234 * Writes the QCC marker (quantization component)
3236 * @param p_comp_no the index of the component to output.
3237 * @param p_stream the stream to write data to.
3238 * @param p_j2k J2K codec.
3239 * @param p_manager the user event manager.
3241 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3242 OPJ_UINT32 p_comp_no,
3244 OPJ_UINT32 * p_data_written,
3245 struct opj_event_mgr * p_manager )
3247 OPJ_UINT32 l_qcc_size,l_remaining_size;
3248 OPJ_BYTE * l_current_data = 00;
3251 assert(p_j2k != 00);
3252 assert(p_manager != 00);
3254 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3255 l_remaining_size = l_qcc_size;
3257 l_current_data = p_data;
3259 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3260 l_current_data += 2;
3262 if (p_j2k->m_private_image->numcomps <= 256) {
3265 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3266 l_current_data += 2;
3268 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3271 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3272 l_remaining_size -= 6;
3275 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3276 l_current_data += 2;
3278 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3281 l_remaining_size -= 6;
3284 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3286 *p_data_written = l_qcc_size;
3290 * Gets the maximum size taken by a qcc.
3292 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3294 return j2k_get_max_coc_size(p_j2k);
3298 * Reads a QCC marker (Quantization component)
3299 * @param p_header_data the data contained in the QCC box.
3300 * @param p_j2k the jpeg2000 codec.
3301 * @param p_header_size the size of the data contained in the QCC marker.
3302 * @param p_manager the user event manager.
3304 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3305 OPJ_BYTE * p_header_data,
3306 OPJ_UINT32 p_header_size,
3307 opj_event_mgr_t * p_manager
3310 OPJ_UINT32 l_num_comp,l_comp_no;
3313 assert(p_header_data != 00);
3314 assert(p_j2k != 00);
3315 assert(p_manager != 00);
3317 l_num_comp = p_j2k->m_private_image->numcomps;
3319 if (l_num_comp <= 256) {
3320 if (p_header_size < 1) {
3321 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3324 opj_read_bytes(p_header_data,&l_comp_no,1);
3329 if (p_header_size < 2) {
3330 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3333 opj_read_bytes(p_header_data,&l_comp_no,2);
3339 if (p_j2k->m_cp.correct) {
3341 static OPJ_UINT32 backup_compno = 0;
3343 /* compno is negative or larger than the number of components!!! */
3344 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3345 opj_event_msg_v2(p_manager, EVT_ERROR,
3346 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3347 l_comp_no, l_num_comp);
3349 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3352 /* we try to correct */
3353 l_comp_no = backup_compno % l_num_comp;
3354 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3355 "- setting component number to %d\n",
3359 /* keep your private count of tiles */
3362 #endif /* USE_JPWL */
3364 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3365 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3369 if (p_header_size != 0) {
3370 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3378 static void j2k_write_poc(opj_j2k_t *j2k) {
3379 int len, numpchgs, i;
3381 int numcomps = j2k->image->numcomps;
3383 opj_cp_t *cp = j2k->cp;
3384 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3385 opj_tccp_t *tccp = &tcp->tccps[0];
3386 opj_cio_t *cio = j2k->cio;
3388 numpchgs = 1 + tcp->numpocs;
3389 cio_write(cio, J2K_MS_POC, 2); /* POC */
3390 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3391 cio_write(cio, len, 2); /* Lpoc */
3392 for (i = 0; i < numpchgs; i++) {
3393 opj_poc_t *poc = &tcp->pocs[i];
3394 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3395 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3396 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3397 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3398 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3399 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3400 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3401 poc->compno1 = int_min(poc->compno1, numcomps);
3402 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3407 * Writes the POC marker (Progression Order Change)
3409 * @param p_stream the stream to write data to.
3410 * @param p_j2k J2K codec.
3411 * @param p_manager the user event manager.
3413 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3414 struct opj_stream_private *p_stream,
3415 struct opj_event_mgr * p_manager )
3417 OPJ_UINT32 l_nb_comp;
3418 OPJ_UINT32 l_nb_poc;
3419 OPJ_UINT32 l_poc_size;
3420 OPJ_UINT32 l_written_size = 0;
3421 opj_tcp_v2_t *l_tcp = 00;
3422 opj_tccp_t *l_tccp = 00;
3423 OPJ_UINT32 l_poc_room;
3426 assert(p_j2k != 00);
3427 assert(p_manager != 00);
3428 assert(p_stream != 00);
3430 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3431 l_tccp = &l_tcp->tccps[0];
3432 l_nb_comp = p_j2k->m_private_image->numcomps;
3433 l_nb_poc = 1 + l_tcp->numpocs;
3435 if (l_nb_comp <= 256) {
3441 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3443 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3444 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3445 = (OPJ_BYTE*)opj_realloc(
3446 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3449 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3453 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3456 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3458 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) {
3467 * Writes the POC marker (Progression Order Change)
3469 * @param p_stream the stream to write data to.
3470 * @param p_j2k J2K codec.
3471 * @param p_manager the user event manager.
3473 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3475 OPJ_UINT32 * p_data_written,
3476 struct opj_event_mgr * p_manager )
3479 OPJ_BYTE * l_current_data = 00;
3480 OPJ_UINT32 l_nb_comp;
3481 OPJ_UINT32 l_nb_poc;
3482 OPJ_UINT32 l_poc_size;
3483 opj_image_t *l_image = 00;
3484 opj_tcp_v2_t *l_tcp = 00;
3485 opj_tccp_t *l_tccp = 00;
3486 opj_poc_t *l_current_poc = 00;
3487 OPJ_UINT32 l_poc_room;
3490 assert(p_j2k != 00);
3491 assert(p_manager != 00);
3493 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3494 l_tccp = &l_tcp->tccps[0];
3495 l_image = p_j2k->m_private_image;
3496 l_nb_comp = l_image->numcomps;
3497 l_nb_poc = 1 + l_tcp->numpocs;
3499 if (l_nb_comp <= 256) {
3506 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3508 l_current_data = p_data;
3510 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3511 l_current_data += 2;
3513 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3514 l_current_data += 2;
3516 l_current_poc = l_tcp->pocs;
3517 for (i = 0; i < l_nb_poc; ++i) {
3518 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3521 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3522 l_current_data+=l_poc_room;
3524 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3527 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3530 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3531 l_current_data+=l_poc_room;
3533 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3536 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3537 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3538 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3539 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3544 *p_data_written = l_poc_size;
3548 * Gets the maximum size taken by the writing of a POC.
3550 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3552 opj_tcp_v2_t * l_tcp = 00;
3553 OPJ_UINT32 l_nb_tiles = 0;
3554 OPJ_UINT32 l_max_poc = 0;
3557 l_tcp = p_j2k->m_cp.tcps;
3558 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3560 for (i=0;i<l_nb_tiles;++i) {
3561 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3567 return 4 + 9 * l_max_poc;
3571 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3573 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3576 OPJ_UINT32 l_nb_tiles;
3577 OPJ_UINT32 l_max = 0;
3578 opj_tcp_v2_t * l_tcp = 00;
3580 l_tcp = p_j2k->m_cp.tcps;
3581 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3583 for (i=0;i<l_nb_tiles;++i) {
3584 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3594 * Gets the maximum size taken by the headers of the SOT.
3596 * @param p_j2k the jpeg2000 codec to use.
3598 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3600 OPJ_UINT32 l_nb_bytes = 0;
3601 OPJ_UINT32 l_nb_comps;
3602 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3604 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3605 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3607 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3608 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3609 l_nb_bytes += l_nb_comps * l_coc_bytes;
3611 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3612 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3615 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3617 /*** DEVELOPER CORNER, Add room for your headers ***/
3624 * Reads a POC marker (Progression Order Change)
3626 * @param p_header_data the data contained in the POC box.
3627 * @param p_j2k the jpeg2000 codec.
3628 * @param p_header_size the size of the data contained in the POC marker.
3629 * @param p_manager the user event manager.
3631 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3632 OPJ_BYTE * p_header_data,
3633 OPJ_UINT32 p_header_size,
3634 opj_event_mgr_t * p_manager
3637 OPJ_UINT32 i, l_nb_comp, l_tmp;
3638 opj_image_t * l_image = 00;
3639 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3640 OPJ_UINT32 l_chunk_size, l_comp_room;
3642 opj_cp_v2_t *l_cp = 00;
3643 opj_tcp_v2_t *l_tcp = 00;
3644 opj_poc_t *l_current_poc = 00;
3647 assert(p_header_data != 00);
3648 assert(p_j2k != 00);
3649 assert(p_manager != 00);
3651 l_image = p_j2k->m_private_image;
3652 l_nb_comp = l_image->numcomps;
3653 if (l_nb_comp <= 256) {
3659 l_chunk_size = 5 + 2 * l_comp_room;
3660 l_current_poc_nb = p_header_size / l_chunk_size;
3661 l_current_poc_remaining = p_header_size % l_chunk_size;
3663 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3664 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3668 l_cp = &(p_j2k->m_cp);
3669 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3670 &l_cp->tcps[p_j2k->m_current_tile_number] :
3671 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3672 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3673 l_current_poc_nb += l_old_poc_nb;
3675 assert(l_current_poc_nb < 32);
3677 /* now poc is in use.*/
3680 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3681 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3682 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3684 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3685 p_header_data+=l_comp_room;
3686 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3688 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3690 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3691 p_header_data+=l_comp_room;
3692 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3694 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3695 /* make sure comp is in acceptable bounds */
3696 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3700 l_tcp->numpocs = l_current_poc_nb - 1;
3705 * Reads a CRG marker (Component registration)
3707 * @param p_header_data the data contained in the TLM box.
3708 * @param p_j2k the jpeg2000 codec.
3709 * @param p_header_size the size of the data contained in the TLM marker.
3710 * @param p_manager the user event manager.
3712 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3713 OPJ_BYTE * p_header_data,
3714 OPJ_UINT32 p_header_size,
3715 opj_event_mgr_t * p_manager
3718 OPJ_UINT32 l_nb_comp;
3720 assert(p_header_data != 00);
3721 assert(p_j2k != 00);
3722 assert(p_manager != 00);
3724 l_nb_comp = p_j2k->m_private_image->numcomps;
3726 if (p_header_size != l_nb_comp *4) {
3727 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3730 /* Do not care of this at the moment since only local variables are set here */
3733 (i = 0; i < l_nb_comp; ++i)
3735 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3737 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3745 * Reads a TLM marker (Tile Length Marker)
3747 * @param p_header_data the data contained in the TLM box.
3748 * @param p_j2k the jpeg2000 codec.
3749 * @param p_header_size the size of the data contained in the TLM marker.
3750 * @param p_manager the user event manager.
3752 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3753 OPJ_BYTE * p_header_data,
3754 OPJ_UINT32 p_header_size,
3755 opj_event_mgr_t * p_manager
3758 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3760 assert(p_header_data != 00);
3761 assert(p_j2k != 00);
3762 assert(p_manager != 00);
3764 if (p_header_size < 2) {
3765 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3770 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3772 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3775 l_ST = ((l_Stlm >> 4) & 0x3);
3776 l_SP = (l_Stlm >> 6) & 0x1;
3778 l_Ptlm_size = (l_SP + 1) * 2;
3779 l_quotient = l_Ptlm_size + l_ST;
3781 l_tot_num_tp = p_header_size / l_quotient;
3782 l_tot_num_tp_remaining = p_header_size % l_quotient;
3784 if (l_tot_num_tp_remaining != 0) {
3785 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3788 /* FIXME Do not care of this at the moment since only local variables are set here */
3791 (i = 0; i < l_tot_num_tp; ++i)
3793 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3794 p_header_data += l_ST;
3795 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3796 p_header_data += l_Ptlm_size;
3803 * Reads a PLM marker (Packet length, main header marker)
3805 * @param p_header_data the data contained in the TLM box.
3806 * @param p_j2k the jpeg2000 codec.
3807 * @param p_header_size the size of the data contained in the TLM marker.
3808 * @param p_manager the user event manager.
3810 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3811 OPJ_BYTE * p_header_data,
3812 OPJ_UINT32 p_header_size,
3813 opj_event_mgr_t * p_manager
3817 assert(p_header_data != 00);
3818 assert(p_j2k != 00);
3819 assert(p_manager != 00);
3821 if (p_header_size < 1) {
3822 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3825 /* Do not care of this at the moment since only local variables are set here */
3827 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3834 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3836 p_header_size -= (1+l_Nplm);
3840 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3844 (i = 0; i < l_Nplm; ++i)
3846 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3848 // take only the last seven bytes
3849 l_packet_len |= (l_tmp & 0x7f);
3857 // store packet length and proceed to next packet
3864 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3873 * Reads a PLT marker (Packet length, tile-part header)
3875 * @param p_header_data the data contained in the PLT box.
3876 * @param p_j2k the jpeg2000 codec.
3877 * @param p_header_size the size of the data contained in the PLT marker.
3878 * @param p_manager the user event manager.
3880 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3881 OPJ_BYTE * p_header_data,
3882 OPJ_UINT32 p_header_size,
3883 opj_event_mgr_t * p_manager
3886 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3889 assert(p_header_data != 00);
3890 assert(p_j2k != 00);
3891 assert(p_manager != 00);
3893 if (p_header_size < 1) {
3894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3898 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3902 for (i = 0; i < p_header_size; ++i) {
3903 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3905 /* take only the last seven bytes */
3906 l_packet_len |= (l_tmp & 0x7f);
3911 /* store packet length and proceed to next packet */
3916 if (l_packet_len != 0) {
3917 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3926 * Reads a PPM marker (Packed packet headers, main header)
3928 * @param p_header_data the data contained in the POC box.
3929 * @param p_j2k the jpeg2000 codec.
3930 * @param p_header_size the size of the data contained in the POC marker.
3931 * @param p_manager the user event manager.
3934 opj_bool j2k_read_ppm_v2 (
3935 opj_j2k_v2_t *p_j2k,
3936 OPJ_BYTE * p_header_data,
3937 OPJ_UINT32 p_header_size,
3938 struct opj_event_mgr * p_manager
3942 opj_cp_v2_t *l_cp = 00;
3943 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3946 assert(p_header_data != 00);
3947 assert(p_j2k != 00);
3948 assert(p_manager != 00);
3950 if (p_header_size < 1) {
3951 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3955 l_cp = &(p_j2k->m_cp);
3958 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3962 /* First PPM marker */
3964 if (p_header_size < 4) {
3965 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3969 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3973 /* First PPM marker: Initialization */
3974 l_cp->ppm_len = l_N_ppm;
3975 l_cp->ppm_data_size = 0;
3977 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3978 if (l_cp->ppm_buffer == 00) {
3979 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3982 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3984 l_cp->ppm_data = l_cp->ppm_buffer;
3988 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3989 if (p_header_size >= 4) {
3991 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3994 l_cp->ppm_len += l_N_ppm ;
3996 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3997 if (l_cp->ppm_buffer == 00) {
3998 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4001 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4003 l_cp->ppm_data = l_cp->ppm_buffer;
4010 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4012 if (l_remaining_data <= p_header_size) {
4013 /* we must store less information than available in the packet */
4014 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4015 l_cp->ppm_data_size = l_cp->ppm_len;
4016 p_header_size -= l_remaining_data;
4017 p_header_data += l_remaining_data;
4020 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4021 l_cp->ppm_data_size += p_header_size;
4022 p_header_data += p_header_size;
4035 * Reads a PPM marker (Packed packet headers, main header)
4037 * @param p_header_data the data contained in the POC box.
4038 * @param p_j2k the jpeg2000 codec.
4039 * @param p_header_size the size of the data contained in the POC marker.
4040 * @param p_manager the user event manager.
4042 opj_bool j2k_read_ppm_v3 (
4043 opj_j2k_v2_t *p_j2k,
4044 OPJ_BYTE * p_header_data,
4045 OPJ_UINT32 p_header_size,
4046 struct opj_event_mgr * p_manager
4049 opj_cp_v2_t *l_cp = 00;
4050 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4053 assert(p_header_data != 00);
4054 assert(p_j2k != 00);
4055 assert(p_manager != 00);
4057 /* Minimum size of PPM marker is equal to the size of Zppm element */
4058 if (p_header_size < 1) {
4059 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4063 l_cp = &(p_j2k->m_cp);
4066 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4070 /* First PPM marker */
4072 /* We need now at least the Nppm^0 element */
4073 if (p_header_size < 4) {
4074 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4078 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4082 /* First PPM marker: Initialization */
4083 l_cp->ppm_len = l_N_ppm;
4084 l_cp->ppm_data_read = 0;
4086 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4087 if (l_cp->ppm_data == 00) {
4088 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4091 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4093 l_cp->ppm_data_current = l_cp->ppm_data;
4095 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4098 if (p_header_size < 4) {
4099 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4103 /* Uncompleted Ippm series in the previous PPM marker?*/
4104 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4105 /* Get the place where add the remaining Ippm series*/
4106 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4107 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4110 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4114 /* Increase the size of ppm_data to add the new Ippm series*/
4115 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4117 /* Keep the position of the place where concatenate the new series*/
4118 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4119 l_cp->ppm_len += l_N_ppm;
4124 l_remaining_data = p_header_size;
4126 while (l_remaining_data >= l_N_ppm) {
4127 /* read a complete Ippm series*/
4128 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4129 p_header_size -= l_N_ppm;
4130 p_header_data += l_N_ppm;
4132 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4136 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4141 l_remaining_data = p_header_size;
4145 l_remaining_data = p_header_size;
4147 /* Next Ippm series is a complete series ?*/
4148 if (l_remaining_data > l_N_ppm) {
4149 /* Increase the size of ppm_data to add the new Ippm series*/
4150 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4152 /* Keep the position of the place where concatenate the new series */
4153 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4154 l_cp->ppm_len += l_N_ppm;
4159 /* Need to read an incomplete Ippm series*/
4160 if (l_remaining_data) {
4161 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4163 /* Keep the position of the place where concatenate the new series*/
4164 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4165 l_cp->ppm_len += l_N_ppm;
4167 /* Read incomplete Ippm series*/
4168 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4169 p_header_size -= l_remaining_data;
4170 p_header_data += l_remaining_data;
4172 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4177 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4178 if (p_header_size >= 4) {
4180 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4183 l_cp->ppm_len += l_N_ppm ;
4185 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4186 if (l_cp->ppm_buffer == 00) {
4187 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4190 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4192 l_cp->ppm_data = l_cp->ppm_buffer;
4199 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4201 if (l_remaining_data <= p_header_size) {
4202 /* we must store less information than available in the packet */
4203 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4204 l_cp->ppm_data_size = l_cp->ppm_len;
4205 p_header_size -= l_remaining_data;
4206 p_header_data += l_remaining_data;
4209 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4210 l_cp->ppm_data_size += p_header_size;
4211 p_header_data += p_header_size;
4221 * Reads a PPT marker (Packed packet headers, tile-part header)
4223 * @param p_header_data the data contained in the PPT box.
4224 * @param p_j2k the jpeg2000 codec.
4225 * @param p_header_size the size of the data contained in the PPT marker.
4226 * @param p_manager the user event manager.
4228 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4229 OPJ_BYTE * p_header_data,
4230 OPJ_UINT32 p_header_size,
4231 opj_event_mgr_t * p_manager
4234 opj_cp_v2_t *l_cp = 00;
4235 opj_tcp_v2_t *l_tcp = 00;
4239 assert(p_header_data != 00);
4240 assert(p_j2k != 00);
4241 assert(p_manager != 00);
4243 /* We need to have the Z_ppt element at minimum */
4244 if (p_header_size < 1) {
4245 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4249 l_cp = &(p_j2k->m_cp);
4251 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");
4255 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4258 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4262 /* Allocate buffer to read the packet header */
4264 /* First PPT marker */
4265 l_tcp->ppt_data_size = 0;
4266 l_tcp->ppt_len = p_header_size;
4268 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4269 if (l_tcp->ppt_buffer == 00) {
4270 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4273 l_tcp->ppt_data = l_tcp->ppt_buffer;
4275 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4278 l_tcp->ppt_len += p_header_size;
4280 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4281 if (l_tcp->ppt_buffer == 00) {
4282 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4285 l_tcp->ppt_data = l_tcp->ppt_buffer;
4287 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4290 /* Read packet header from buffer */
4291 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4293 l_tcp->ppt_data_size += p_header_size;
4299 * Writes the TLM marker (Tile Length Marker)
4301 * @param p_stream the stream to write data to.
4302 * @param p_j2k J2K codec.
4303 * @param p_manager the user event manager.
4305 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4306 struct opj_stream_private *p_stream,
4307 struct opj_event_mgr * p_manager )
4309 OPJ_BYTE * l_current_data = 00;
4310 OPJ_UINT32 l_tlm_size;
4313 assert(p_j2k != 00);
4314 assert(p_manager != 00);
4315 assert(p_stream != 00);
4317 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4319 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4320 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4321 = (OPJ_BYTE*)opj_realloc(
4322 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4325 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4329 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4332 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4334 /* change the way data is written to avoid seeking if possible */
4336 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4338 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4339 l_current_data += 2;
4341 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4342 l_current_data += 2;
4344 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4347 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4350 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4351 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) {
4358 static void j2k_write_tlm(opj_j2k_t *j2k){
4360 opj_cio_t *cio = j2k->cio;
4361 j2k->tlm_start = cio_tell(cio);
4362 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4363 lenp = 4 + (5*j2k->totnum_tp);
4364 cio_write(cio,lenp,2); /* Ltlm */
4365 cio_write(cio, 0,1); /* Ztlm=0*/
4366 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4367 cio_skip(cio,5*j2k->totnum_tp);
4370 static void j2k_write_sot(opj_j2k_t *j2k) {
4373 opj_cio_t *cio = j2k->cio;
4375 j2k->sot_start = cio_tell(cio);
4376 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4377 lenp = cio_tell(cio);
4378 cio_skip(cio, 2); /* Lsot (further) */
4379 cio_write(cio, j2k->curtileno, 2); /* Isot */
4380 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4381 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4382 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4383 len = cio_tell(cio) - lenp;
4384 cio_seek(cio, lenp);
4385 cio_write(cio, len, 2); /* Lsot */
4386 cio_seek(cio, lenp + len);
4390 /* update markers struct */
4391 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4392 #endif /* USE_JPWL */
4395 if( j2k->cstr_info && j2k->cur_tp_num==0){
4396 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4401 * Writes the SOT marker (Start of tile-part)
4403 * @param p_stream the stream to write data to.
4404 * @param p_j2k J2K codec.
4405 * @param p_manager the user event manager.
4407 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4409 OPJ_UINT32 * p_data_written,
4410 const struct opj_stream_private *p_stream,
4411 struct opj_event_mgr * p_manager )
4414 assert(p_j2k != 00);
4415 assert(p_manager != 00);
4416 assert(p_stream != 00);
4418 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4421 opj_write_bytes(p_data,10,2); /* Lsot */
4424 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4430 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4433 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4438 /* update markers struct */
4440 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4442 assert( 0 && "TODO" );
4443 #endif /* USE_JPWL */
4445 * p_data_written = 12;
4454 * Reads a PPT marker (Packed packet headers, tile-part header)
4456 * @param p_header_data the data contained in the PPT box.
4457 * @param p_j2k the jpeg2000 codec.
4458 * @param p_header_size the size of the data contained in the PPT marker.
4459 * @param p_manager the user event manager.
4461 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4462 OPJ_BYTE * p_header_data,
4463 OPJ_UINT32 p_header_size,
4464 opj_event_mgr_t * p_manager )
4466 opj_cp_v2_t *l_cp = 00;
4467 opj_tcp_v2_t *l_tcp = 00;
4468 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4469 OPJ_UINT32 l_current_part;
4470 OPJ_UINT32 l_tile_x,l_tile_y;
4473 assert(p_header_data != 00);
4474 assert(p_j2k != 00);
4475 assert(p_manager != 00);
4477 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4478 if (p_header_size != 8) {
4479 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4483 l_cp = &(p_j2k->m_cp);
4484 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4487 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4488 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4489 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4492 if (l_cp->correct) {
4494 int tileno = p_j2k->m_current_tile_number;
4495 static int backup_tileno = 0;
4497 /* tileno is negative or larger than the number of tiles!!! */
4498 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4499 opj_event_msg_v2(p_manager, EVT_ERROR,
4500 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4501 tileno, (l_cp->tw * l_cp->th));
4503 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4506 /* we try to correct */
4507 tileno = backup_tileno;
4508 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4509 "- setting tile number to %d\n",
4513 /* keep your private count of tiles */
4516 #endif /* USE_JPWL */
4518 /* look for the tile in the list of already processed tile (in parts). */
4519 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4520 /* since the time taken by this function can only grow at the time */
4522 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4525 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4526 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4528 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4534 if (l_cp->correct) {
4536 /* totlen is negative or larger than the bytes left!!! */
4537 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))) { */
4538 opj_event_msg_v2(p_manager, EVT_ERROR,
4539 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4540 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4542 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4545 /* we try to correct */
4547 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4548 "- setting Psot to %d => assuming it is the last tile\n",
4552 #endif /* USE_JPWL */
4554 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4556 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4557 "we assuming it is the last tile-part of the codestream.\n");
4558 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4561 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4564 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4567 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4568 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4569 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4570 if (l_tcp->m_nb_tile_parts) {
4571 if (l_current_part >= l_tcp->m_nb_tile_parts){
4572 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4573 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4574 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4578 l_tcp->m_nb_tile_parts = l_num_parts;
4581 /* If know the number of tile part header we will check if we didn't read the last*/
4582 if (l_tcp->m_nb_tile_parts) {
4583 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4584 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4588 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4589 /* Keep the size of data to skip after this marker */
4590 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4593 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4594 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4597 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4599 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4600 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4601 p_j2k->m_specific_param.m_decoder.m_skip_data =
4602 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4603 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4604 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4605 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4608 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4609 p_j2k->m_specific_param.m_decoder.m_skip_data =
4610 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4614 if (p_j2k->cstr_index)
4616 assert(p_j2k->cstr_index->tile_index != 00);
4617 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4618 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4620 if (l_num_parts != 0){
4621 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4622 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4625 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4626 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4627 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4629 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4630 (opj_tp_index_t*)opj_realloc(
4631 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4632 l_num_parts* sizeof(opj_tp_index_t));
4635 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4637 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4638 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4639 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4640 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4641 sizeof(opj_tp_index_t));
4644 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4645 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4646 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4647 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4648 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4649 * sizeof(opj_tp_index_t));
4658 /* 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 */
4659 /* if (p_j2k->cstr_info) {
4662 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4665 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4666 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4667 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4668 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4671 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4674 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4678 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4681 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4682 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4683 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4688 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4691 opj_tcp_t *tcp = NULL;
4692 opj_codestream_info_t *cstr_info = NULL;
4694 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4695 opj_cp_t *cp = j2k->cp;
4696 opj_cio_t *cio = j2k->cio;
4698 tcd->tp_num = j2k->tp_num ;
4699 tcd->cur_tp_num = j2k->cur_tp_num;
4701 cio_write(cio, J2K_MS_SOD, 2);
4703 if( j2k->cstr_info && j2k->cur_tp_num==0){
4704 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4707 if (j2k->curtileno == 0) {
4708 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4712 cstr_info = j2k->cstr_info;
4714 if (!j2k->cur_tp_num ) {
4715 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4716 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4719 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4720 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4724 /* update markers struct */
4725 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4726 #endif /* USE_JPWL */
4731 tcp = &cp->tcps[j2k->curtileno];
4732 for (layno = 0; layno < tcp->numlayers; layno++) {
4733 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4734 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4735 } else if (tcp->rates[layno]) {
4736 tcp->rates[layno]=1;
4739 if(j2k->cur_tp_num == 0){
4740 tcd->tcd_image->tiles->packno = 0;
4742 cstr_info->packno = 0;
4745 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4747 /* Writing Psot in SOT marker */
4748 totlen = cio_tell(cio) + l - j2k->sot_start;
4749 cio_seek(cio, j2k->sot_start + 6);
4750 cio_write(cio, totlen, 4);
4751 cio_seek(cio, j2k->sot_start + totlen);
4752 /* Writing Ttlm and Ptlm in TLM marker */
4754 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4755 cio_write(cio, j2k->curtileno, 1);
4756 cio_write(cio, totlen, 4);
4758 cio_seek(cio, j2k->sot_start + totlen);
4762 * Writes the SOD marker (Start of data)
4764 * @param p_stream the stream to write data to.
4765 * @param p_j2k J2K codec.
4766 * @param p_manager the user event manager.
4768 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4769 struct opj_tcd_v2 * p_tile_coder,
4771 OPJ_UINT32 * p_data_written,
4772 OPJ_UINT32 p_total_data_size,
4773 const struct opj_stream_private *p_stream,
4774 struct opj_event_mgr * p_manager )
4776 opj_tcp_v2_t *l_tcp = 00;
4777 opj_codestream_info_t *l_cstr_info = 00;
4778 opj_cp_v2_t *l_cp = 00;
4780 OPJ_UINT32 l_size_tile;
4781 OPJ_UINT32 l_remaining_data;
4784 assert(p_j2k != 00);
4785 assert(p_manager != 00);
4786 assert(p_stream != 00);
4788 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4791 /* make room for the EOF marker */
4792 l_remaining_data = p_total_data_size - 4;
4794 l_cp = &(p_j2k->m_cp);
4795 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4798 /* update tile coder */
4799 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4800 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4802 l_size_tile = l_cp->th * l_cp->tw;
4805 /* TODO mergeV2: check this part which use cstr_info */
4806 /*l_cstr_info = p_j2k->cstr_info;
4808 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4809 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4810 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4816 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4818 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4823 /* update markers struct */
4824 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4826 assert( 0 && "TODO" );
4827 #endif /* USE_JPWL */
4832 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4833 p_tile_coder->tcd_image->tiles->packno = 0;
4835 l_cstr_info->packno = 0;
4839 *p_data_written = 0;
4841 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)) {
4842 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4846 *p_data_written += 2;
4855 * Reads a SOD marker (Start Of Data)
4857 * @param p_header_data the data contained in the SOD box.
4858 * @param p_j2k the jpeg2000 codec.
4859 * @param p_header_size the size of the data contained in the SOD marker.
4860 * @param p_manager the user event manager.
4862 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4863 opj_stream_private_t *p_stream,
4864 opj_event_mgr_t * p_manager
4867 OPJ_UINT32 l_current_read_size;
4868 opj_codestream_index_t * l_cstr_index = 00;
4869 OPJ_BYTE ** l_current_data = 00;
4870 opj_tcp_v2_t * l_tcp = 00;
4871 OPJ_UINT32 * l_tile_len = 00;
4874 assert(p_j2k != 00);
4875 assert(p_manager != 00);
4876 assert(p_stream != 00);
4878 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4880 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4881 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4882 // but we are in the last tile part,
4883 // so its result will fit on OPJ_UINT32 unless we find
4884 // a file with a single tile part of more than 4 GB...*/
4885 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4888 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4890 l_current_data = &(l_tcp->m_data);
4891 l_tile_len = &l_tcp->m_data_size;
4893 if (! *l_current_data) {
4894 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4897 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4900 if (*l_current_data == 00) {
4901 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4907 l_cstr_index = p_j2k->cstr_index;
4909 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4911 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4912 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4914 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4915 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4917 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4921 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4923 /*l_cstr_index->packno = 0;*/
4926 l_current_read_size = opj_stream_read_data( p_stream,
4927 *l_current_data + *l_tile_len,
4928 p_j2k->m_specific_param.m_decoder.m_sot_length,
4931 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4932 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4935 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4938 *l_tile_len += l_current_read_size;
4944 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4945 opj_cp_t *cp = j2k->cp;
4946 opj_tcp_t *tcp = &cp->tcps[tileno];
4947 opj_cio_t *cio = j2k->cio;
4948 int numcomps = j2k->image->numcomps;
4950 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4951 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4952 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4953 cio_write(cio, 0, 1); /* Srgn */
4954 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4958 * Writes the RGN marker (Region Of Interest)
4960 * @param p_tile_no the tile to output
4961 * @param p_comp_no the component to output
4962 * @param p_stream the stream to write data to.
4963 * @param p_j2k J2K codec.
4964 * @param p_manager the user event manager.
4966 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4967 OPJ_UINT32 p_tile_no,
4968 OPJ_UINT32 p_comp_no,
4969 struct opj_stream_private *p_stream,
4970 struct opj_event_mgr * p_manager )
4972 OPJ_BYTE * l_current_data = 00;
4973 OPJ_UINT32 l_nb_comp;
4974 OPJ_UINT32 l_rgn_size;
4975 opj_image_t *l_image = 00;
4976 opj_cp_v2_t *l_cp = 00;
4977 opj_tcp_v2_t *l_tcp = 00;
4978 opj_tccp_t *l_tccp = 00;
4979 OPJ_UINT32 l_comp_room;
4982 assert(p_j2k != 00);
4983 assert(p_manager != 00);
4984 assert(p_stream != 00);
4986 l_cp = &(p_j2k->m_cp);
4987 l_tcp = &l_cp->tcps[p_tile_no];
4988 l_tccp = &l_tcp->tccps[p_comp_no];
4990 l_nb_comp = l_image->numcomps;
4992 if (l_nb_comp <= 256) {
4999 l_rgn_size = 6 + l_comp_room;
5001 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5003 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5004 l_current_data += 2;
5006 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5007 l_current_data += 2;
5009 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5010 l_current_data+=l_comp_room;
5012 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5015 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5018 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) {
5026 static void j2k_write_eoc(opj_j2k_t *j2k) {
5027 opj_cio_t *cio = j2k->cio;
5028 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5029 cio_write(cio, J2K_MS_EOC, 2);
5033 /* update markers struct */
5034 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5035 #endif /* USE_JPWL */
5040 * Writes the EOC marker (End of Codestream)
5042 * @param p_stream the stream to write data to.
5043 * @param p_j2k J2K codec.
5044 * @param p_manager the user event manager.
5046 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5047 struct opj_stream_private *p_stream,
5048 struct opj_event_mgr * p_manager )
5051 assert(p_j2k != 00);
5052 assert(p_manager != 00);
5053 assert(p_stream != 00);
5055 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5060 /* update markers struct */
5062 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5064 #endif /* USE_JPWL */
5066 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5070 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5079 * Reads a RGN marker (Region Of Interest)
5081 * @param p_header_data the data contained in the POC box.
5082 * @param p_j2k the jpeg2000 codec.
5083 * @param p_header_size the size of the data contained in the POC marker.
5084 * @param p_manager the user event manager.
5086 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5087 OPJ_BYTE * p_header_data,
5088 OPJ_UINT32 p_header_size,
5089 opj_event_mgr_t * p_manager
5092 OPJ_UINT32 l_nb_comp;
5093 opj_image_t * l_image = 00;
5095 opj_cp_v2_t *l_cp = 00;
5096 opj_tcp_v2_t *l_tcp = 00;
5097 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5100 assert(p_header_data != 00);
5101 assert(p_j2k != 00);
5102 assert(p_manager != 00);
5104 l_image = p_j2k->m_private_image;
5105 l_nb_comp = l_image->numcomps;
5107 if (l_nb_comp <= 256) {
5112 if (p_header_size != 2 + l_comp_room) {
5113 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5117 l_cp = &(p_j2k->m_cp);
5118 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5119 &l_cp->tcps[p_j2k->m_current_tile_number] :
5120 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5122 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5123 p_header_data+=l_comp_room;
5124 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5128 if (l_cp->correct) {
5129 /* totlen is negative or larger than the bytes left!!! */
5130 if (l_comp_room >= l_nb_comp) {
5131 opj_event_msg_v2(p_manager, EVT_ERROR,
5132 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5133 l_comp_room, l_nb_comp);
5134 if (!JPWL_ASSUME || JPWL_ASSUME) {
5135 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5140 #endif /* USE_JPWL */
5142 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5149 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5151 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5154 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5161 * Updates the rates of the tcp.
5163 * @param p_stream the stream to write data to.
5164 * @param p_j2k J2K codec.
5165 * @param p_manager the user event manager.
5167 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5168 struct opj_stream_private *p_stream,
5169 struct opj_event_mgr * p_manager )
5171 opj_cp_v2_t * l_cp = 00;
5172 opj_image_t * l_image = 00;
5173 opj_tcp_v2_t * l_tcp = 00;
5174 opj_image_comp_t * l_img_comp = 00;
5177 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5178 OPJ_FLOAT32 * l_rates = 0;
5179 OPJ_FLOAT32 l_sot_remove;
5180 OPJ_UINT32 l_bits_empty, l_size_pixel;
5181 OPJ_UINT32 l_tile_size = 0;
5182 OPJ_UINT32 l_last_res;
5183 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5186 assert(p_j2k != 00);
5187 assert(p_manager != 00);
5188 assert(p_stream != 00);
5191 l_cp = &(p_j2k->m_cp);
5192 l_image = p_j2k->m_private_image;
5195 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5196 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5197 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5199 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5200 l_tp_stride_func = get_tp_stride;
5203 l_tp_stride_func = get_default_stride;
5206 for (i=0;i<l_cp->th;++i) {
5207 for (j=0;j<l_cp->tw;++j) {
5208 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5210 /* 4 borders of the tile rescale on the image if necessary */
5211 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5212 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5213 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5214 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5216 l_rates = l_tcp->rates;
5218 /* Modification of the RATE >> */
5220 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5222 ((*l_rates) * l_bits_empty)
5230 for (k = 1; k < l_tcp->numlayers; ++k) {
5232 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5234 ((*l_rates) * l_bits_empty)
5250 for (i=0;i<l_cp->th;++i) {
5251 for (j=0;j<l_cp->tw;++j) {
5252 l_rates = l_tcp->rates;
5255 *l_rates -= l_sot_remove;
5257 if (*l_rates < 30) {
5264 l_last_res = l_tcp->numlayers - 1;
5266 for (k = 1; k < l_last_res; ++k) {
5269 *l_rates -= l_sot_remove;
5271 if (*l_rates < *(l_rates - 1) + 10) {
5272 *l_rates = (*(l_rates - 1)) + 20;
5280 *l_rates -= (l_sot_remove + 2.f);
5282 if (*l_rates < *(l_rates - 1) + 10) {
5283 *l_rates = (*(l_rates - 1)) + 20;
5291 l_img_comp = l_image->comps;
5294 for (i=0;i<l_image->numcomps;++i) {
5295 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5297 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5305 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5307 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5309 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5310 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5311 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5312 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5316 if (l_cp->m_specific_param.m_enc.m_cinema) {
5317 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5318 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5319 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5323 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5324 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5330 static void j2k_read_eoc(opj_j2k_t *j2k) {
5332 opj_bool success = OPJ_FALSE;
5334 /* if packets should be decoded */
5335 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5336 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5337 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5338 for (i = 0; i < j2k->cp->tileno_size; i++) {
5339 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5340 if (j2k->cp->tileno[i] != -1)
5342 tileno = j2k->cp->tileno[i];
5343 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5344 opj_free(j2k->tile_data[tileno]);
5345 j2k->tile_data[tileno] = NULL;
5346 tcd_free_decode_tile(tcd, i);
5349 success = OPJ_FALSE;
5350 if (success == OPJ_FALSE) {
5351 j2k->state |= J2K_STATE_ERR;
5355 tcd_free_decode(tcd);
5358 /* if packets should not be decoded */
5360 for (i = 0; i < j2k->cp->tileno_size; i++) {
5361 tileno = j2k->cp->tileno[i];
5362 opj_free(j2k->tile_data[tileno]);
5363 j2k->tile_data[tileno] = NULL;
5366 if (j2k->state & J2K_STATE_ERR)
5367 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5369 j2k->state = J2K_STATE_MT;
5373 * Reads a EOC marker (End Of Codestream)
5375 * @param p_header_data the data contained in the SOD box.
5376 * @param p_j2k the jpeg2000 codec.
5377 * @param p_header_size the size of the data contained in the SOD marker.
5378 * @param p_manager the user event manager.
5381 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5382 struct opj_stream_private *p_stream,
5383 struct opj_event_mgr * p_manager )
5386 opj_tcd_v2_t * l_tcd = 00;
5387 OPJ_UINT32 l_nb_tiles;
5388 opj_tcp_v2_t * l_tcp = 00;
5392 assert(p_j2k != 00);
5393 assert(p_manager != 00);
5394 assert(p_stream != 00);
5396 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5397 l_tcp = p_j2k->m_cp.tcps;
5399 l_tcd = tcd_create_v2(OPJ_TRUE);
5401 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5405 for (i = 0; i < l_nb_tiles; ++i) {
5406 if (l_tcp->m_data) {
5407 if (! tcd_init_decode_tile(l_tcd, i)) {
5408 tcd_destroy_v2(l_tcd);
5409 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5413 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5417 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5422 j2k_tcp_destroy(l_tcp);
5426 tcd_destroy_v2(l_tcd);
5432 * Gets the offset of the header.
5434 * @param p_stream the stream to write data to.
5435 * @param p_j2k J2K codec.
5436 * @param p_manager the user event manager.
5438 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5439 struct opj_stream_private *p_stream,
5440 struct opj_event_mgr * p_manager )
5443 assert(p_j2k != 00);
5444 assert(p_manager != 00);
5445 assert(p_stream != 00);
5447 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5453 * Writes the MCT marker (Multiple Component Transform)
5455 * @param p_stream the stream to write data to.
5456 * @param p_j2k J2K codec.
5457 * @param p_manager the user event manager.
5459 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5460 struct opj_stream_private *p_stream,
5461 struct opj_event_mgr * p_manager )
5464 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5465 opj_mct_data_t * l_mct_record;
5466 opj_tcp_v2_t * l_tcp;
5469 assert(p_j2k != 00);
5470 assert(p_stream != 00);
5471 assert(p_manager != 00);
5473 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5477 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5478 l_mct_record = l_tcp->m_mct_records;
5480 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5482 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5489 l_mcc_record = l_tcp->m_mcc_records;
5491 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5493 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5500 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5508 * Writes the image components.
5510 * @param p_stream the stream to write data to.
5511 * @param p_j2k J2K codec.
5512 * @param p_manager the user event manager.
5514 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5515 struct opj_stream_private *p_stream,
5516 struct opj_event_mgr * p_manager )
5521 assert(p_j2k != 00);
5522 assert(p_manager != 00);
5523 assert(p_stream != 00);
5525 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5527 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5531 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5540 * Writes regions of interests.
5542 * @param p_stream the stream to write data to.
5543 * @param p_j2k J2K codec.
5544 * @param p_manager the user event manager.
5546 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5547 struct opj_stream_private *p_stream,
5548 struct opj_event_mgr * p_manager )
5551 const opj_tccp_t *l_tccp = 00;
5554 assert(p_j2k != 00);
5555 assert(p_manager != 00);
5556 assert(p_stream != 00);
5558 l_tccp = p_j2k->m_cp.tcps->tccps;
5560 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5561 if (l_tccp->roishift) {
5563 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
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_epc( opj_j2k_v2_t *p_j2k,
5582 struct opj_stream_private *p_stream,
5583 struct opj_event_mgr * p_manager )
5585 opj_codestream_index_t * l_cstr_index = 00;
5588 assert(p_j2k != 00);
5589 assert(p_manager != 00);
5590 assert(p_stream != 00);
5592 l_cstr_index = p_j2k->cstr_index;
5594 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5596 /* The following adjustment is done to adjust the codestream size */
5597 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5598 /* the first bunch of bytes is not in the codestream */
5599 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5604 /* preparation of JPWL marker segments */
5608 /* encode according to JPWL */
5609 jpwl_encode(p_j2k, p_stream, image);
5613 assert( 0 && "TODO" );
5614 #endif /* USE_JPWL */
5619 typedef struct opj_dec_mstabent {
5622 /** value of the state when the marker can appear */
5624 /** action linked to the marker */
5625 void (*handler) (opj_j2k_t *j2k);
5626 } opj_dec_mstabent_t;
5628 opj_dec_mstabent_t j2k_dec_mstab[] = {
5629 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5630 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5631 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5632 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5633 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5634 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5635 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5636 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5637 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5638 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5639 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5640 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5641 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5642 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5643 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5644 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5646 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5647 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5650 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5651 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5652 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5653 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5654 #endif /* USE_JPWL */
5656 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5657 {J2K_MS_INSEC, 0, j2k_read_insec},
5658 #endif /* USE_JPSEC */
5660 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5663 static void j2k_read_unk(opj_j2k_t *j2k) {
5664 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5667 if (j2k->cp->correct) {
5669 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5670 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5671 id = cio_read(j2k->cio, 2);
5672 opj_event_msg(j2k->cinfo, EVT_ERROR,
5673 "JPWL: really don't know this marker %x\n",
5676 opj_event_msg(j2k->cinfo, EVT_ERROR,
5677 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5680 /* OK, activate this at your own risk!!! */
5681 /* we look for the marker at the minimum hamming distance from this */
5682 while (j2k_dec_mstab[m].id) {
5684 /* 1's where they differ */
5685 tmp_id = j2k_dec_mstab[m].id ^ id;
5687 /* compute the hamming distance between our id and the current */
5689 for (i = 0; i < 16; i++) {
5690 if ((tmp_id >> i) & 0x0001) {
5695 /* if current distance is smaller, set the minimum */
5696 if (cur_dist < min_dist) {
5697 min_dist = cur_dist;
5698 min_id = j2k_dec_mstab[m].id;
5701 /* jump to the next marker */
5705 /* do we substitute the marker? */
5706 if (min_dist < JPWL_MAXIMUM_HAMMING) {
5707 opj_event_msg(j2k->cinfo, EVT_ERROR,
5708 "- marker %x is at distance %d from the read %x\n",
5709 min_id, min_dist, id);
5710 opj_event_msg(j2k->cinfo, EVT_ERROR,
5711 "- trying to substitute in place and crossing fingers!\n");
5712 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5713 cio_write(j2k->cio, min_id, 2);
5716 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5721 #endif /* USE_JPWL */
5726 * Reads an unknown marker
5728 * @param p_stream the stream object to read from.
5729 * @param p_j2k the jpeg2000 codec.
5730 * @param p_manager the user event manager.
5732 * @return true if the marker could be deduced.
5734 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
5735 struct opj_stream_private *p_stream,
5736 OPJ_UINT32 *output_marker,
5737 struct opj_event_mgr * p_manager
5740 OPJ_UINT32 l_unknown_marker;
5741 const opj_dec_memory_marker_handler_t * l_marker_handler;
5742 OPJ_UINT32 l_size_unk = 2;
5745 assert(p_j2k != 00);
5746 assert(p_manager != 00);
5747 assert(p_stream != 00);
5749 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5752 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5753 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5754 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5758 /* read 2 bytes as the new marker ID*/
5759 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5761 if (!(l_unknown_marker < 0xff00)) {
5763 /* Get the marker handler from the marker ID*/
5764 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
5766 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5767 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5771 if (l_marker_handler->id != J2K_MS_UNK) {
5772 /* Add the marker to the codestream index*/
5773 if (l_marker_handler->id != J2K_MS_SOT)
5774 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5775 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5777 break; /* next marker is known and well located */
5785 *output_marker = l_marker_handler->id ;
5791 Read the lookup table containing all the marker, status and action
5792 @param id Marker value
5794 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5795 opj_dec_mstabent_t *e;
5796 for (e = j2k_dec_mstab; e->id != 0; e++) {
5805 * Writes the MCT marker (Multiple Component Transform)
5807 * @param p_stream the stream to write data to.
5808 * @param p_j2k J2K codec.
5809 * @param p_manager the user event manager.
5811 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5812 opj_mct_data_t * p_mct_record,
5813 struct opj_stream_private *p_stream,
5814 struct opj_event_mgr * p_manager )
5816 OPJ_UINT32 l_mct_size;
5817 OPJ_BYTE * l_current_data = 00;
5821 assert(p_j2k != 00);
5822 assert(p_manager != 00);
5823 assert(p_stream != 00);
5825 l_mct_size = 10 + p_mct_record->m_data_size;
5827 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5828 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5829 = (OPJ_BYTE*)opj_realloc(
5830 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5833 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5837 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5840 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5842 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5843 l_current_data += 2;
5845 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5846 l_current_data += 2;
5848 opj_write_bytes(l_current_data,0,2); /* Zmct */
5849 l_current_data += 2;
5851 /* only one marker atm */
5852 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5854 opj_write_bytes(l_current_data,l_tmp,2);
5855 l_current_data += 2;
5857 opj_write_bytes(l_current_data,0,2); /* Ymct */
5860 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5862 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) {
5870 * Reads a MCT marker (Multiple Component Transform)
5872 * @param p_header_data the data contained in the MCT box.
5873 * @param p_j2k the jpeg2000 codec.
5874 * @param p_header_size the size of the data contained in the MCT marker.
5875 * @param p_manager the user event manager.
5877 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5878 OPJ_BYTE * p_header_data,
5879 OPJ_UINT32 p_header_size,
5880 struct opj_event_mgr * p_manager )
5883 opj_tcp_v2_t *l_tcp = 00;
5886 opj_mct_data_t * l_mct_data;
5889 assert(p_header_data != 00);
5890 assert(p_j2k != 00);
5892 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5893 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5894 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5896 if (p_header_size < 2) {
5897 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5902 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5905 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5909 if(p_header_size <= 6) {
5910 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5914 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5915 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5918 l_indix = l_tmp & 0xff;
5919 l_mct_data = l_tcp->m_mct_records;
5921 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5922 if (l_mct_data->m_index == l_indix) {
5929 if (i == l_tcp->m_nb_mct_records) {
5930 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5931 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5933 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));
5934 if(! l_tcp->m_mct_records) {
5935 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5939 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5940 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5943 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5946 if (l_mct_data->m_data) {
5947 opj_free(l_mct_data->m_data);
5948 l_mct_data->m_data = 00;
5951 l_mct_data->m_index = l_indix;
5952 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5953 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5955 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5958 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5964 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5965 if (! l_mct_data->m_data) {
5966 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5969 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5971 l_mct_data->m_data_size = p_header_size;
5972 ++l_tcp->m_nb_mct_records;
5978 * Writes the MCC marker (Multiple Component Collection)
5980 * @param p_stream the stream to write data to.
5981 * @param p_j2k J2K codec.
5982 * @param p_manager the user event manager.
5984 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5985 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5986 struct opj_stream_private *p_stream,
5987 struct opj_event_mgr * p_manager )
5990 OPJ_UINT32 l_mcc_size;
5991 OPJ_BYTE * l_current_data = 00;
5992 OPJ_UINT32 l_nb_bytes_for_comp;
5997 assert(p_j2k != 00);
5998 assert(p_manager != 00);
5999 assert(p_stream != 00);
6001 if (p_mcc_record->m_nb_comps > 255 ) {
6002 l_nb_bytes_for_comp = 2;
6006 l_nb_bytes_for_comp = 1;
6010 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6011 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6013 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6014 = (OPJ_BYTE*)opj_realloc(
6015 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6017 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6021 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6024 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6026 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6027 l_current_data += 2;
6029 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6030 l_current_data += 2;
6033 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6034 l_current_data += 2;
6036 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6039 /* only one marker atm */
6040 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6043 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6046 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6049 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 */
6052 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6053 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6054 l_current_data+=l_nb_bytes_for_comp;
6057 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 */
6060 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6062 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6063 l_current_data+=l_nb_bytes_for_comp;
6066 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6068 if (p_mcc_record->m_decorrelation_array) {
6069 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6072 if (p_mcc_record->m_offset_array) {
6073 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6076 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6079 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) {
6087 * Reads a MCC marker (Multiple Component Collection)
6089 * @param p_header_data the data contained in the MCC box.
6090 * @param p_j2k the jpeg2000 codec.
6091 * @param p_header_size the size of the data contained in the MCC marker.
6092 * @param p_manager the user event manager.
6094 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6095 OPJ_BYTE * p_header_data,
6096 OPJ_UINT32 p_header_size,
6097 struct opj_event_mgr * p_manager )
6102 opj_tcp_v2_t * l_tcp;
6103 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6104 opj_mct_data_t * l_mct_data;
6105 OPJ_UINT32 l_nb_collections;
6106 OPJ_UINT32 l_nb_comps;
6107 OPJ_UINT32 l_nb_bytes_by_comp;
6111 assert(p_header_data != 00);
6112 assert(p_j2k != 00);
6113 assert(p_manager != 00);
6115 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6116 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6117 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6119 if (p_header_size < 2) {
6120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6125 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6128 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6132 if (p_header_size < 7) {
6133 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6137 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6140 l_mcc_record = l_tcp->m_mcc_records;
6142 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6143 if (l_mcc_record->m_index == l_indix) {
6150 if (i == l_tcp->m_nb_mcc_records) {
6151 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6152 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6154 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6155 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6156 if (! l_tcp->m_mcc_records) {
6157 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6160 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6161 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));
6163 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6165 l_mcc_record->m_index = l_indix;
6167 /* only one marker atm */
6168 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6171 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6175 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6178 if (l_nb_collections > 1) {
6179 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6185 for (i=0;i<l_nb_collections;++i) {
6186 if (p_header_size < 3) {
6187 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6191 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6195 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6199 opj_read_bytes(p_header_data,&l_nb_comps,2);
6204 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6205 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6207 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6208 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6212 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6214 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6215 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6216 p_header_data+=l_nb_bytes_by_comp;
6219 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6224 opj_read_bytes(p_header_data,&l_nb_comps,2);
6227 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6228 l_nb_comps &= 0x7fff;
6230 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6231 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6235 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6236 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6240 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6242 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6243 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6244 p_header_data+=l_nb_bytes_by_comp;
6247 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6252 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6255 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6256 l_mcc_record->m_decorrelation_array = 00;
6257 l_mcc_record->m_offset_array = 00;
6259 l_indix = l_tmp & 0xff;
6261 l_mct_data = l_tcp->m_mct_records;
6262 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6263 if (l_mct_data->m_index == l_indix) {
6264 l_mcc_record->m_decorrelation_array = l_mct_data;
6270 if (l_mcc_record->m_decorrelation_array == 00) {
6271 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6276 l_indix = (l_tmp >> 8) & 0xff;
6278 l_mct_data = l_tcp->m_mct_records;
6279 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6280 if (l_mct_data->m_index == l_indix) {
6281 l_mcc_record->m_offset_array = l_mct_data;
6287 if (l_mcc_record->m_offset_array == 00) {
6288 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6294 if (p_header_size != 0) {
6295 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6299 ++l_tcp->m_nb_mcc_records;
6306 * Writes the MCO marker (Multiple component transformation ordering)
6308 * @param p_stream the stream to write data to.
6309 * @param p_j2k J2K codec.
6310 * @param p_manager the user event manager.
6312 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6313 struct opj_stream_private *p_stream,
6314 struct opj_event_mgr * p_manager
6317 OPJ_BYTE * l_current_data = 00;
6318 OPJ_UINT32 l_mco_size;
6319 opj_tcp_v2_t * l_tcp = 00;
6320 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6324 assert(p_j2k != 00);
6325 assert(p_manager != 00);
6326 assert(p_stream != 00);
6328 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6329 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6331 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6332 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6334 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6335 = (OPJ_BYTE*)opj_realloc(
6336 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6338 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6343 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6346 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6347 l_current_data += 2;
6349 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6350 l_current_data += 2;
6352 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6355 l_mcc_record = l_tcp->m_mcc_records;
6356 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6357 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6363 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) {
6371 * Reads a MCO marker (Multiple Component Transform Ordering)
6373 * @param p_header_data the data contained in the MCO box.
6374 * @param p_j2k the jpeg2000 codec.
6375 * @param p_header_size the size of the data contained in the MCO marker.
6376 * @param p_manager the user event manager.
6378 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6379 OPJ_BYTE * p_header_data,
6380 OPJ_UINT32 p_header_size,
6381 struct opj_event_mgr * p_manager )
6383 OPJ_UINT32 l_tmp, i;
6384 OPJ_UINT32 l_nb_stages;
6385 opj_tcp_v2_t * l_tcp;
6386 opj_tccp_t * l_tccp;
6387 opj_image_t * l_image;
6388 opj_image_comp_t * l_img_comp;
6391 assert(p_header_data != 00);
6392 assert(p_j2k != 00);
6393 assert(p_manager != 00);
6395 l_image = p_j2k->m_private_image;
6396 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6397 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6398 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6400 if (p_header_size < 1) {
6401 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6405 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6408 if (l_nb_stages > 1) {
6409 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6413 if (p_header_size != l_nb_stages + 1) {
6414 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6418 l_tccp = l_tcp->tccps;
6419 l_img_comp = l_image->comps;
6421 for (i=0;i<l_image->numcomps;++i) {
6422 l_tccp->m_dc_level_shift = 0;
6426 if (l_tcp->m_mct_decoding_matrix) {
6427 opj_free(l_tcp->m_mct_decoding_matrix);
6428 l_tcp->m_mct_decoding_matrix = 00;
6431 for (i=0;i<l_nb_stages;++i) {
6432 opj_read_bytes(p_header_data,&l_tmp,1);
6435 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6443 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6446 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6447 opj_mct_data_t * l_deco_array, * l_offset_array;
6448 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6449 OPJ_UINT32 l_nb_elem;
6450 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6451 opj_tccp_t * l_tccp;
6454 assert(p_tcp != 00);
6456 l_mcc_record = p_tcp->m_mcc_records;
6458 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6459 if (l_mcc_record->m_index == p_index) {
6464 if (i==p_tcp->m_nb_mcc_records) {
6465 /** element discarded **/
6469 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6470 /** do not support number of comps != image */
6474 l_deco_array = l_mcc_record->m_decorrelation_array;
6477 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6478 if (l_deco_array->m_data_size != l_data_size) {
6482 l_nb_elem = p_image->numcomps * p_image->numcomps;
6483 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6484 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6486 if (! p_tcp->m_mct_decoding_matrix ) {
6490 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);
6493 l_offset_array = l_mcc_record->m_offset_array;
6495 if (l_offset_array) {
6496 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6497 if (l_offset_array->m_data_size != l_data_size) {
6501 l_nb_elem = p_image->numcomps;
6502 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6503 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6505 if (! l_offset_data ) {
6509 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6511 l_tccp = p_tcp->tccps;
6512 l_current_offset_data = l_offset_data;
6514 for (i=0;i<p_image->numcomps;++i) {
6515 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6519 opj_free(l_offset_data);
6526 * Writes the CBD marker (Component bit depth definition)
6528 * @param p_stream the stream to write data to.
6529 * @param p_j2k J2K codec.
6530 * @param p_manager the user event manager.
6532 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6533 struct opj_stream_private *p_stream,
6534 struct opj_event_mgr * p_manager )
6537 OPJ_UINT32 l_cbd_size;
6538 OPJ_BYTE * l_current_data = 00;
6539 opj_image_t *l_image = 00;
6540 opj_image_comp_t * l_comp = 00;
6543 assert(p_j2k != 00);
6544 assert(p_manager != 00);
6545 assert(p_stream != 00);
6547 l_image = p_j2k->m_private_image;
6548 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6550 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6551 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6552 = (OPJ_BYTE*)opj_realloc(
6553 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6556 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6560 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6563 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6565 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6566 l_current_data += 2;
6568 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6569 l_current_data += 2;
6571 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6574 l_comp = l_image->comps;
6576 for (i=0;i<l_image->numcomps;++i) {
6577 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6583 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) {
6591 * Reads a CBD marker (Component bit depth definition)
6592 * @param p_header_data the data contained in the CBD box.
6593 * @param p_j2k the jpeg2000 codec.
6594 * @param p_header_size the size of the data contained in the CBD marker.
6595 * @param p_manager the user event manager.
6597 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6598 OPJ_BYTE * p_header_data,
6599 OPJ_UINT32 p_header_size,
6600 struct opj_event_mgr * p_manager)
6602 OPJ_UINT32 l_nb_comp,l_num_comp;
6603 OPJ_UINT32 l_comp_def;
6605 opj_image_comp_t * l_comp = 00;
6608 assert(p_header_data != 00);
6609 assert(p_j2k != 00);
6610 assert(p_manager != 00);
6612 l_num_comp = p_j2k->m_private_image->numcomps;
6614 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6615 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6619 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6622 if (l_nb_comp != l_num_comp) {
6623 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6627 l_comp = p_j2k->m_private_image->comps;
6628 for (i=0;i<l_num_comp;++i) {
6629 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6631 l_comp->sgnd = (l_comp_def>>7) & 1;
6632 l_comp->prec = (l_comp_def&0x7f) + 1;
6640 /* ----------------------------------------------------------------------- */
6641 /* J2K / JPT decoder interface */
6642 /* ----------------------------------------------------------------------- */
6646 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6649 if(j2k->tile_len != NULL) {
6650 opj_free(j2k->tile_len);
6652 if(j2k->tile_data != NULL) {
6653 opj_free(j2k->tile_data);
6655 if(j2k->default_tcp != NULL) {
6656 opj_tcp_t *default_tcp = j2k->default_tcp;
6657 if(default_tcp->ppt_data_first != NULL) {
6658 opj_free(default_tcp->ppt_data_first);
6660 if(j2k->default_tcp->tccps != NULL) {
6661 opj_free(j2k->default_tcp->tccps);
6663 opj_free(j2k->default_tcp);
6665 if(j2k->cp != NULL) {
6666 opj_cp_t *cp = j2k->cp;
6667 if(cp->tcps != NULL) {
6668 for(i = 0; i < cp->tw * cp->th; i++) {
6669 if(cp->tcps[i].ppt_data_first != NULL) {
6670 opj_free(cp->tcps[i].ppt_data_first);
6672 if(cp->tcps[i].tccps != NULL) {
6673 opj_free(cp->tcps[i].tccps);
6678 if(cp->ppm_data_first != NULL) {
6679 opj_free(cp->ppm_data_first);
6681 if(cp->tileno != NULL) {
6682 opj_free(cp->tileno);
6684 if(cp->comment != NULL) {
6685 opj_free(cp->comment);
6694 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6696 if(j2k && parameters) {
6697 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6698 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6701 j2k->m_cp.correct = parameters->jpwl_correct;
6702 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6703 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6704 #endif /* USE_JPWL */
6711 * Read a JPT-stream and decode file
6714 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
6715 opj_image_t *image = NULL;
6716 opj_jpt_msg_header_t header;
6718 opj_common_ptr cinfo = j2k->cinfo;
6720 OPJ_ARG_NOT_USED(cstr_info);
6724 /* create an empty image */
6725 image = opj_image_create0();
6728 j2k->state = J2K_STATE_MHSOC;
6730 /* Initialize the header */
6731 jpt_init_msg_header(&header);
6732 /* Read the first header of the message */
6733 jpt_read_msg_header(cinfo, cio, &header);
6735 position = cio_tell(cio);
6736 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
6737 opj_image_destroy(image);
6738 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
6743 opj_dec_mstabent_t *e = NULL;
6746 if (!cio_numbytesleft(cio)) {
6750 /* data-bin read -> need to read a new header */
6751 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
6752 jpt_read_msg_header(cinfo, cio, &header);
6753 position = cio_tell(cio);
6754 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
6755 opj_image_destroy(image);
6756 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
6761 id = cio_read(cio, 2);
6762 if (id >> 8 != 0xff) {
6763 opj_image_destroy(image);
6764 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
6767 e = j2k_dec_mstab_lookup(id);
6768 if (!(j2k->state & e->states)) {
6769 opj_image_destroy(image);
6770 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
6776 if (j2k->state == J2K_STATE_MT) {
6779 if (j2k->state == J2K_STATE_NEOC) {
6783 if (j2k->state == J2K_STATE_NEOC) {
6787 if (j2k->state != J2K_STATE_MT) {
6788 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
6794 /* ----------------------------------------------------------------------- */
6795 /* J2K encoder interface */
6796 /* ----------------------------------------------------------------------- */
6798 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6799 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6806 opj_j2k_v2_t* j2k_create_compress_v2(void)
6808 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6813 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6815 l_j2k->m_is_decoder = 0;
6816 l_j2k->m_cp.m_is_decoder = 0;
6818 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6819 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6824 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6826 /* validation list creation*/
6827 l_j2k->m_validation_list = opj_procedure_list_create();
6828 if (! l_j2k->m_validation_list) {
6833 /* execution list creation*/
6834 l_j2k->m_procedure_list = opj_procedure_list_create();
6835 if (! l_j2k->m_procedure_list) {
6843 void j2k_destroy_compress(opj_j2k_t *j2k) {
6847 if(j2k->cp != NULL) {
6848 opj_cp_t *cp = j2k->cp;
6851 opj_free(cp->comment);
6854 opj_free(cp->matrice);
6856 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6857 opj_free(cp->tcps[tileno].tccps);
6866 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6868 int tileno, numpocs_tile;
6869 opj_cp_t *cp = NULL;
6871 if(!j2k || !parameters || ! image) {
6875 /* create and initialize the coding parameters structure */
6876 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6878 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6881 /* set default values for cp */
6886 copy user encoding parameters
6888 cp->cinema = parameters->cp_cinema;
6889 cp->max_comp_size = parameters->max_comp_size;
6890 cp->rsiz = parameters->cp_rsiz;
6891 cp->disto_alloc = parameters->cp_disto_alloc;
6892 cp->fixed_alloc = parameters->cp_fixed_alloc;
6893 cp->fixed_quality = parameters->cp_fixed_quality;
6895 /* mod fixed_quality */
6896 if(parameters->cp_matrice) {
6897 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6898 cp->matrice = (int *) opj_malloc(array_size);
6899 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6903 cp->tdx = parameters->cp_tdx;
6904 cp->tdy = parameters->cp_tdy;
6907 cp->tx0 = parameters->cp_tx0;
6908 cp->ty0 = parameters->cp_ty0;
6910 /* comment string */
6911 if(parameters->cp_comment) {
6912 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6914 strcpy(cp->comment, parameters->cp_comment);
6919 calculate other encoding parameters
6922 if (parameters->tile_size_on) {
6923 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6924 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6926 cp->tdx = image->x1 - cp->tx0;
6927 cp->tdy = image->y1 - cp->ty0;
6930 if(parameters->tp_on){
6931 cp->tp_flag = parameters->tp_flag;
6936 for(i=0;i<image->numcomps ;i++){
6937 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6943 calculate JPWL encoding parameters
6946 if (parameters->jpwl_epc_on) {
6950 cp->epc_on = OPJ_TRUE;
6951 cp->info_on = OPJ_FALSE; /* no informative technique */
6954 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6955 cp->epb_on = OPJ_TRUE;
6957 cp->hprot_MH = parameters->jpwl_hprot_MH;
6958 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6959 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6960 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6962 /* if tile specs are not specified, copy MH specs */
6963 if (cp->hprot_TPH[0] == -1) {
6964 cp->hprot_TPH_tileno[0] = 0;
6965 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6967 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6968 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6969 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6970 cp->pprot[i] = parameters->jpwl_pprot[i];
6974 /* set ESD writing */
6975 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6976 cp->esd_on = OPJ_TRUE;
6978 cp->sens_size = parameters->jpwl_sens_size;
6979 cp->sens_addr = parameters->jpwl_sens_addr;
6980 cp->sens_range = parameters->jpwl_sens_range;
6982 cp->sens_MH = parameters->jpwl_sens_MH;
6983 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6984 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6985 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6989 /* always set RED writing to false: we are at the encoder */
6990 cp->red_on = OPJ_FALSE;
6993 cp->epc_on = OPJ_FALSE;
6995 #endif /* USE_JPWL */
6998 /* initialize the mutiple tiles */
6999 /* ---------------------------- */
7000 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7002 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7003 opj_tcp_t *tcp = &cp->tcps[tileno];
7004 tcp->numlayers = parameters->tcp_numlayers;
7005 assert ( tcp->numlayers >= 0 );
7006 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7008 if (cp->fixed_quality) {
7009 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7011 tcp->rates[j] = parameters->tcp_rates[j];
7013 if (cp->fixed_quality) { /* add fixed_quality */
7014 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7016 tcp->rates[j] = parameters->tcp_rates[j];
7020 tcp->csty = parameters->csty;
7021 tcp->prg = parameters->prog_order;
7022 tcp->mct = parameters->tcp_mct;
7026 if (parameters->numpocs) {
7027 /* initialisation of POC */
7029 assert( parameters->numpocs >= 0 );
7030 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7031 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7032 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7033 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7034 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7035 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7036 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7037 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7038 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7039 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7043 tcp->numpocs = numpocs_tile -1 ;
7048 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7050 for (i = 0; i < image->numcomps; i++) {
7051 opj_tccp_t *tccp = &tcp->tccps[i];
7052 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7053 tccp->numresolutions = parameters->numresolution;
7054 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7055 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7056 tccp->cblksty = parameters->mode;
7057 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7058 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7060 assert(parameters->roi_compno >= 0);
7061 if (i == (OPJ_UINT32)parameters->roi_compno) {
7062 tccp->roishift = parameters->roi_shift;
7067 if(parameters->cp_cinema)
7069 /*Precinct size for lowest frequency subband=128*/
7072 /*Precinct size at all other resolutions = 256*/
7073 for (j = 1; j < tccp->numresolutions; j++) {
7078 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7080 assert(tccp->numresolutions > 0);
7081 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7082 if (p < parameters->res_spec) {
7084 if (parameters->prcw_init[p] < 1) {
7087 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7090 if (parameters->prch_init[p] < 1) {
7093 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7097 int res_spec = parameters->res_spec;
7098 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7099 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7101 if (size_prcw < 1) {
7104 tccp->prcw[j] = int_floorlog2(size_prcw);
7107 if (size_prch < 1) {
7110 tccp->prch[j] = int_floorlog2(size_prch);
7114 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7117 for (j = 0; j < tccp->numresolutions; j++) {
7124 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7129 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7130 opj_cparameters_t *parameters,
7132 struct opj_event_mgr * p_manager)
7134 OPJ_UINT32 i, j, tileno, numpocs_tile;
7135 opj_cp_v2_t *cp = 00;
7138 if(!p_j2k || !parameters || ! image) {
7142 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7143 cp = &(p_j2k->m_cp);
7145 /* set default values for cp */
7150 copy user encoding parameters
7152 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7153 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7154 cp->rsiz = parameters->cp_rsiz;
7155 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7156 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7157 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7159 /* mod fixed_quality */
7160 if (parameters->cp_matrice) {
7161 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7162 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7163 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7167 cp->tdx = parameters->cp_tdx;
7168 cp->tdy = parameters->cp_tdy;
7171 cp->tx0 = parameters->cp_tx0;
7172 cp->ty0 = parameters->cp_ty0;
7174 /* comment string */
7175 if(parameters->cp_comment) {
7176 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7178 strcpy(cp->comment, parameters->cp_comment);
7183 calculate other encoding parameters
7186 if (parameters->tile_size_on) {
7187 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7188 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7190 cp->tdx = image->x1 - cp->tx0;
7191 cp->tdy = image->y1 - cp->ty0;
7194 if (parameters->tp_on) {
7195 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7196 cp->m_specific_param.m_enc.m_tp_on = 1;
7201 calculate JPWL encoding parameters
7204 if (parameters->jpwl_epc_on) {
7208 cp->epc_on = OPJ_TRUE;
7209 cp->info_on = OPJ_FALSE; /* no informative technique */
7212 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7213 cp->epb_on = OPJ_TRUE;
7215 cp->hprot_MH = parameters->jpwl_hprot_MH;
7216 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7217 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7218 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7220 /* if tile specs are not specified, copy MH specs */
7221 if (cp->hprot_TPH[0] == -1) {
7222 cp->hprot_TPH_tileno[0] = 0;
7223 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7225 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7226 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7227 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7228 cp->pprot[i] = parameters->jpwl_pprot[i];
7232 /* set ESD writing */
7233 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7234 cp->esd_on = OPJ_TRUE;
7236 cp->sens_size = parameters->jpwl_sens_size;
7237 cp->sens_addr = parameters->jpwl_sens_addr;
7238 cp->sens_range = parameters->jpwl_sens_range;
7240 cp->sens_MH = parameters->jpwl_sens_MH;
7241 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7242 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7243 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7247 /* always set RED writing to false: we are at the encoder */
7248 cp->red_on = OPJ_FALSE;
7251 cp->epc_on = OPJ_FALSE;
7253 #endif /* USE_JPWL */
7256 /* initialize the mutiple tiles */
7257 /* ---------------------------- */
7258 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7259 if (parameters->numpocs) {
7260 /* initialisation of POC */
7261 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7265 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7266 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7267 tcp->numlayers = parameters->tcp_numlayers;
7269 for (j = 0; j < tcp->numlayers; j++) {
7270 if(cp->m_specific_param.m_enc.m_cinema){
7271 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7272 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7274 tcp->rates[j] = parameters->tcp_rates[j];
7276 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7277 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7279 tcp->rates[j] = parameters->tcp_rates[j];
7284 tcp->csty = parameters->csty;
7285 tcp->prg = parameters->prog_order;
7286 tcp->mct = parameters->tcp_mct;
7291 if (parameters->numpocs) {
7292 /* initialisation of POC */
7295 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7296 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7297 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7299 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7300 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7301 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7302 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7303 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7304 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7305 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7311 tcp->numpocs = numpocs_tile -1 ;
7316 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7318 if (parameters->mct_data) {
7320 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7323 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7324 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7325 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7326 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7329 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7330 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7331 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7333 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7334 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7336 tcp->mct_norms = (OPJ_FLOAT64*)
7337 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7339 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7342 for (i = 0; i < image->numcomps; i++) {
7343 opj_tccp_t *tccp = &tcp->tccps[i];
7344 tccp->m_dc_level_shift = l_dc_shift[i];
7347 j2k_setup_mct_encoding(tcp,image);
7351 for (i = 0; i < image->numcomps; i++) {
7352 opj_tccp_t *tccp = &tcp->tccps[i];
7353 opj_image_comp_t * l_comp = &(image->comps[i]);
7355 if (! l_comp->sgnd) {
7356 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7361 for (i = 0; i < image->numcomps; i++) {
7362 opj_tccp_t *tccp = &tcp->tccps[i];
7364 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7365 tccp->numresolutions = parameters->numresolution;
7366 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7367 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7368 tccp->cblksty = parameters->mode;
7369 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7370 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7373 if (i == parameters->roi_compno) {
7374 tccp->roishift = parameters->roi_shift;
7379 if(parameters->cp_cinema) {
7380 //Precinct size for lowest frequency subband=128
7383 //Precinct size at all other resolutions = 256
7384 for (j = 1; j < tccp->numresolutions; j++) {
7389 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7390 OPJ_INT32 p = 0, it_res;
7391 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7392 if (p < parameters->res_spec) {
7394 if (parameters->prcw_init[p] < 1) {
7395 tccp->prcw[it_res] = 1;
7397 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7400 if (parameters->prch_init[p] < 1) {
7401 tccp->prch[it_res] = 1;
7403 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7407 int res_spec = parameters->res_spec;
7408 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7409 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7411 if (size_prcw < 1) {
7412 tccp->prcw[it_res] = 1;
7414 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7417 if (size_prch < 1) {
7418 tccp->prch[it_res] = 1;
7420 tccp->prch[it_res] = int_floorlog2(size_prch);
7424 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7427 for (j = 0; j < tccp->numresolutions; j++) {
7434 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7438 if (parameters->mct_data) {
7439 opj_free(parameters->mct_data);
7440 parameters->mct_data = 00;
7445 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7448 opj_cp_t *cp = NULL;
7450 opj_tcd_t *tcd = NULL; /* TCD component */
7458 j2k->cstr_info = cstr_info;
7461 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7462 cstr_info->image_w = image->x1 - image->x0;
7463 cstr_info->image_h = image->y1 - image->y0;
7464 cstr_info->prog = (&cp->tcps[0])->prg;
7465 cstr_info->tw = cp->tw;
7466 cstr_info->th = cp->th;
7467 cstr_info->tile_x = cp->tdx; /* new version parser */
7468 cstr_info->tile_y = cp->tdy; /* new version parser */
7469 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7470 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7471 cstr_info->numcomps = image->numcomps;
7472 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7473 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7474 for (compno=0; compno < image->numcomps; compno++) {
7475 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7477 cstr_info->D_max = 0.0; /* ADD Marcela */
7478 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7479 cstr_info->maxmarknum = 100;
7480 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7481 cstr_info->marknum = 0;
7491 for (compno = 1; compno < image->numcomps; compno++) {
7492 j2k_write_coc(j2k, compno);
7493 j2k_write_qcc(j2k, compno);
7497 for (compno = 0; compno < image->numcomps; compno++) {
7498 opj_tcp_t *tcp = &cp->tcps[0];
7499 if (tcp->tccps[compno].roishift)
7500 j2k_write_rgn(j2k, compno, 0);
7502 if (cp->comment != NULL) {
7506 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7510 if (cp->cinema == CINEMA4K_24) {
7515 /* uncomment only for testing JPSEC marker writing */
7516 /* j2k_write_sec(j2k); */
7520 cstr_info->main_head_end = cio_tell(cio) - 1;
7523 /**** Main Header ENDS here ***/
7525 /* create the tile encoder */
7526 tcd = tcd_create(j2k->cinfo);
7528 /* encode each tile */
7529 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7533 int acc_pack_num = 0;
7537 opj_tcp_t *tcp = &cp->tcps[tileno];
7538 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7540 j2k->curtileno = tileno;
7541 j2k->cur_tp_num = 0;
7542 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7543 /* initialisation before tile encoding */
7545 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7547 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7552 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7553 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7554 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7555 cstr_info->tile[j2k->curtileno].marknum = 0;
7559 for(pino = 0; pino <= tcp->numpocs; pino++) {
7563 /*Get number of tile parts*/
7564 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7565 tcd->tp_pos = cp->tp_pos;
7567 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7568 j2k->tp_num = tilepartno;
7571 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7572 cio_tell(cio) + j2k->pos_correction;
7576 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7577 for (compno = 1; compno < image->numcomps; compno++) {
7578 j2k_write_coc(j2k, compno);
7579 j2k_write_qcc(j2k, compno);
7581 if (cp->tcps[tileno].numpocs) {
7588 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7589 cio_tell(cio) + j2k->pos_correction + 1;
7592 j2k_write_sod(j2k, tcd);
7596 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7597 cio_tell(cio) + j2k->pos_correction - 1;
7598 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7600 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7601 cstr_info->packno - acc_pack_num;
7602 acc_pack_num = cstr_info->packno;
7610 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7615 if (tile->PPT) { // BAD PPT !!!
7618 PPT_file=fopen("PPT","rb");
7619 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7620 for (i=0;i<tile->len_ppt;i++) {
7622 fread(&elmt, 1, 1, PPT_file);
7623 fwrite(&elmt,1,1,f);
7632 /* destroy the tile encoder */
7633 tcd_free_encode(tcd);
7636 opj_free(j2k->cur_totnum_tp);
7641 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7643 /* The following adjustment is done to adjust the codestream size */
7644 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7645 /* the first bunch of bytes is not in the codestream */
7646 cstr_info->codestream_size -= cstr_info->main_head_start;
7652 preparation of JPWL marker segments
7656 /* encode according to JPWL */
7657 jpwl_encode(j2k, cio, image);
7660 #endif /* USE_JPWL */
7665 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7667 assert(cstr_info != 00);
7669 /* expand the list? */
7670 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7671 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7672 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7675 /* add the marker */
7676 cstr_info->marker[cstr_info->marknum].type = type;
7677 cstr_info->marker[cstr_info->marknum].pos = pos;
7678 cstr_info->marker[cstr_info->marknum].len = len;
7679 cstr_info->marknum++;
7683 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7685 assert(cstr_index != 00);
7687 /* expand the list? */
7688 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7689 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7690 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7693 /* add the marker */
7694 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7695 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7696 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7697 cstr_index->marknum++;
7701 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7703 opj_marker_info_t *marker;
7705 assert(cstr_info != 00);
7707 /* expand the list? */
7708 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7709 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7710 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7713 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7715 /* add the marker */
7716 marker->type = type;
7719 cstr_info->tile[tileno].marknum++;
7722 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)
7724 assert(cstr_index != 00);
7725 assert(cstr_index->tile_index != 00);
7727 /* expand the list? */
7728 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7729 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7730 cstr_index->tile_index[tileno].marker =
7731 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7732 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7735 /* add the marker */
7736 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7737 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7738 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7739 cstr_index->tile_index[tileno].marknum++;
7741 if (type == J2K_MS_SOT) {
7742 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7744 if (cstr_index->tile_index[tileno].tp_index)
7745 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7752 * -----------------------------------------------------------------------
7753 * -----------------------------------------------------------------------
7754 * -----------------------------------------------------------------------
7758 * Ends the decompression procedures and possibiliy add data to be read after the
7761 opj_bool j2k_end_decompress(
7762 opj_j2k_v2_t *p_j2k,
7763 opj_stream_private_t *p_stream,
7764 opj_event_mgr_t * p_manager)
7773 * Reads a jpeg2000 codestream header structure.
7776 * @param p_stream the stream to read data from.
7777 * @param p_j2k the jpeg2000 codec.
7778 * @param p_manager the user event manager.
7780 * @return true if the box is valid.
7782 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
7783 opj_j2k_v2_t* p_j2k,
7784 opj_image_t** p_image,
7785 struct opj_event_mgr* p_manager )
7788 assert(p_j2k != 00);
7789 assert(p_stream != 00);
7790 assert(p_manager != 00);
7792 /* create an empty image header */
7793 p_j2k->m_private_image = opj_image_create0();
7794 if (! p_j2k->m_private_image) {
7798 /* customization of the validation */
7799 j2k_setup_decoding_validation(p_j2k);
7801 /* validation of the parameters codec */
7802 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7803 opj_image_destroy(p_j2k->m_private_image);
7804 p_j2k->m_private_image = NULL;
7808 /* customization of the encoding */
7809 j2k_setup_header_reading(p_j2k);
7812 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7813 opj_image_destroy(p_j2k->m_private_image);
7814 p_j2k->m_private_image = NULL;
7818 *p_image = opj_image_create0();
7823 /* Copy codestream image information to the output image */
7824 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7826 /*Allocate and initialize some elements of codestrem index*/
7827 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
7835 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7837 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7840 assert(p_j2k != 00);
7842 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
7844 /* DEVELOPER CORNER, add your custom procedures */
7845 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
7850 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7851 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7853 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7856 assert(p_j2k != 00);
7858 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
7859 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
7860 /* DEVELOPER CORNER, add your custom validation procedure */
7866 * The mct encoding validation procedure.
7868 * @param p_j2k the jpeg2000 codec to validate.
7869 * @param p_stream the input stream to validate.
7870 * @param p_manager the user event manager.
7872 * @return true if the parameters are correct.
7874 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7875 opj_stream_private_t *p_stream,
7876 opj_event_mgr_t * p_manager )
7878 opj_bool l_is_valid = OPJ_TRUE;
7882 assert(p_j2k != 00);
7883 assert(p_stream != 00);
7884 assert(p_manager != 00);
7886 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7887 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7888 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7890 for (i=0;i<l_nb_tiles;++i) {
7891 if (l_tcp->mct == 2) {
7892 opj_tccp_t * l_tccp = l_tcp->tccps;
7893 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7895 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7896 l_is_valid &= ! (l_tccp->qmfbid & 1);
7907 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7910 OPJ_UINT32 l_indix = 1;
7911 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7912 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7913 OPJ_UINT32 l_mct_size,l_nb_elem;
7914 OPJ_FLOAT32 * l_data, * l_current_data;
7915 opj_tccp_t * l_tccp;
7918 assert(p_tcp != 00);
7920 if (p_tcp->mct != 2) {
7924 if (p_tcp->m_mct_decoding_matrix) {
7925 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7926 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7928 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));
7929 if (! p_tcp->m_mct_records) {
7932 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7934 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7936 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7938 if (l_mct_deco_data->m_data) {
7939 opj_free(l_mct_deco_data->m_data);
7940 l_mct_deco_data->m_data = 00;
7943 l_mct_deco_data->m_index = l_indix++;
7944 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7945 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7946 l_nb_elem = p_image->numcomps * p_image->numcomps;
7947 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7948 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7950 if (! l_mct_deco_data->m_data) {
7954 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);
7956 l_mct_deco_data->m_data_size = l_mct_size;
7957 ++p_tcp->m_nb_mct_records;
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;
7962 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) {
7968 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7969 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7971 if (l_mct_deco_data) {
7972 l_mct_deco_data = l_mct_offset_data - 1;
7976 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7978 if (l_mct_offset_data->m_data) {
7979 opj_free(l_mct_offset_data->m_data);
7980 l_mct_offset_data->m_data = 00;
7983 l_mct_offset_data->m_index = l_indix++;
7984 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7985 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7986 l_nb_elem = p_image->numcomps;
7987 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7988 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7990 if (! l_mct_offset_data->m_data) {
7994 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7996 opj_free(l_mct_offset_data->m_data);
7997 l_mct_offset_data->m_data = 00;
8001 l_tccp = p_tcp->tccps;
8002 l_current_data = l_data;
8004 for (i=0;i<l_nb_elem;++i) {
8005 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8009 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8013 l_mct_offset_data->m_data_size = l_mct_size;
8015 ++p_tcp->m_nb_mct_records;
8017 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8018 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8019 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8020 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8022 if (! p_tcp->m_mcc_records) {
8025 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8026 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));
8030 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8031 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8032 l_mcc_data->m_is_irreversible = 1;
8033 l_mcc_data->m_nb_comps = p_image->numcomps;
8034 l_mcc_data->m_index = l_indix++;
8035 l_mcc_data->m_offset_array = l_mct_offset_data;
8036 ++p_tcp->m_nb_mcc_records;
8042 * Builds the cp decoder parameters to use to decode tile.
8044 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8045 opj_stream_private_t *p_stream,
8046 opj_event_mgr_t * p_manager )
8048 /* add here initialization of cp
8049 copy paste of setup_decoder */
8057 * Builds the cp encoder parameters to use to encode tile.
8059 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8060 opj_stream_private_t *p_stream,
8061 opj_event_mgr_t * p_manager )
8063 /* add here initialization of cp
8064 copy paste of setup_encoder */
8072 * The default encoding validation procedure without any extension.
8074 * @param p_j2k the jpeg2000 codec to validate.
8075 * @param p_stream the input stream to validate.
8076 * @param p_manager the user event manager.
8078 * @return true if the parameters are correct.
8080 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8081 opj_stream_private_t *p_stream,
8082 opj_event_mgr_t * p_manager )
8084 opj_bool l_is_valid = OPJ_TRUE;
8087 assert(p_j2k != 00);
8088 assert(p_stream != 00);
8089 assert(p_manager != 00);
8091 /* STATE checking */
8092 /* make sure the state is at 0 */
8093 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8095 /* POINTER validation */
8096 /* make sure a p_j2k codec is present */
8097 l_is_valid &= (p_j2k->m_procedure_list != 00);
8098 /* make sure a validation list is present */
8099 l_is_valid &= (p_j2k->m_validation_list != 00);
8101 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8102 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8106 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8107 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8111 /* PARAMETER VALIDATION */
8116 * The default decoding validation procedure without any extension.
8118 * @param p_j2k the jpeg2000 codec to validate.
8119 * @param p_stream the input stream to validate.
8120 * @param p_manager the user event manager.
8122 * @return true if the parameters are correct.
8124 opj_bool j2k_decoding_validation (
8125 opj_j2k_v2_t *p_j2k,
8126 opj_stream_private_t *p_stream,
8127 opj_event_mgr_t * p_manager
8130 opj_bool l_is_valid = OPJ_TRUE;
8133 assert(p_j2k != 00);
8134 assert(p_stream != 00);
8135 assert(p_manager != 00);
8138 /* STATE checking */
8139 /* make sure the state is at 0 */
8141 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8143 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8145 /* POINTER validation */
8146 /* make sure a p_j2k codec is present */
8147 /* make sure a procedure list is present */
8148 l_is_valid &= (p_j2k->m_procedure_list != 00);
8149 /* make sure a validation list is present */
8150 l_is_valid &= (p_j2k->m_validation_list != 00);
8152 /* PARAMETER VALIDATION */
8156 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8157 struct opj_stream_private *p_stream,
8158 struct opj_event_mgr * p_manager)
8160 OPJ_UINT32 l_current_marker;
8161 OPJ_UINT32 l_marker_size;
8162 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8165 assert(p_stream != 00);
8166 assert(p_j2k != 00);
8167 assert(p_manager != 00);
8169 /* We enter in the main header */
8170 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8172 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8173 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8174 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8178 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8179 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8180 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8184 /* Read 2 bytes as the new marker ID */
8185 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8187 /* Try to read until the SOT is detected */
8188 while (l_current_marker != J2K_MS_SOT) {
8190 /* Check if the current marker ID is valid */
8191 if (l_current_marker < 0xff00) {
8192 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8196 /* Get the marker handler from the marker ID */
8197 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8199 /* Manage case where marker is unknown */
8200 if (l_marker_handler->id == J2K_MS_UNK) {
8201 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8202 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8206 if (l_current_marker == J2K_MS_SOT)
8207 break; /* SOT marker is detected main header is completely read */
8208 else /* Get the marker handler from the marker ID */
8209 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8212 /* Check if the marker is known and if it is the right place to find it */
8213 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8214 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8218 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8219 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8220 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8224 /* read 2 bytes as the marker size */
8225 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8226 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8228 /* Check if the marker size is compatible with the header data size */
8229 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8230 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8231 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8232 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8235 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8238 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8239 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) {
8240 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8244 /* Read the marker segment with the correct marker handler */
8245 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8246 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8250 /* Add the marker to the codestream index*/
8251 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8252 l_marker_handler->id,
8253 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8254 l_marker_size + 4 );
8256 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8257 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8258 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8262 /* read 2 bytes as the new marker ID */
8263 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8266 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8268 /* Position of the last element if the main header */
8269 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8271 /* Next step: read a tile-part header */
8272 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8278 * Excutes the given procedures on the given codec.
8280 * @param p_procedure_list the list of procedures to execute
8281 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8282 * @param p_stream the stream to execute the procedures on.
8283 * @param p_manager the user manager.
8285 * @return true if all the procedures were successfully executed.
8287 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8288 opj_procedure_list_t * p_procedure_list,
8289 opj_stream_private_t *p_stream,
8290 opj_event_mgr_t * p_manager )
8292 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8293 opj_bool l_result = OPJ_TRUE;
8294 OPJ_UINT32 l_nb_proc, i;
8297 assert(p_procedure_list != 00);
8298 assert(p_j2k != 00);
8299 assert(p_stream != 00);
8300 assert(p_manager != 00);
8303 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8304 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);
8306 for (i=0;i<l_nb_proc;++i) {
8307 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8311 /* and clear the procedure list at the end.*/
8312 opj_procedure_list_clear(p_procedure_list);
8317 opj_bool j2k_copy_default_tcp_and_create_tcd
8319 opj_j2k_v2_t * p_j2k,
8320 opj_stream_private_t *p_stream,
8321 opj_event_mgr_t * p_manager
8324 opj_tcp_v2_t * l_tcp = 00;
8325 opj_tcp_v2_t * l_default_tcp = 00;
8326 OPJ_UINT32 l_nb_tiles;
8328 opj_tccp_t *l_current_tccp = 00;
8329 OPJ_UINT32 l_tccp_size;
8330 OPJ_UINT32 l_mct_size;
8331 opj_image_t * l_image;
8332 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8333 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8334 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8335 OPJ_UINT32 l_offset;
8338 assert(p_j2k != 00);
8339 assert(p_stream != 00);
8340 assert(p_manager != 00);
8342 l_image = p_j2k->m_private_image;
8343 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8344 l_tcp = p_j2k->m_cp.tcps;
8345 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8346 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8347 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8350 for (i=0; i<l_nb_tiles; ++i) {
8351 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8352 l_current_tccp = l_tcp->tccps;
8353 /*Copy default coding parameters into the current tile coding parameters*/
8354 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8355 /* Initialize some values of the current tile coding parameters*/
8357 l_tcp->ppt_data = 00;
8358 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8359 l_tcp->tccps = l_current_tccp;
8361 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8362 if (l_default_tcp->m_mct_decoding_matrix) {
8363 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8364 if (! l_tcp->m_mct_decoding_matrix ) {
8367 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8370 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8371 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8372 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8373 if (! l_tcp->m_mct_records) {
8376 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8378 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8379 l_src_mct_rec = l_default_tcp->m_mct_records;
8380 l_dest_mct_rec = l_tcp->m_mct_records;
8382 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8384 if (l_src_mct_rec->m_data) {
8386 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8387 if(! l_dest_mct_rec->m_data) {
8390 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8397 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8398 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8399 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8400 if (! l_tcp->m_mcc_records) {
8403 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8405 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8406 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8407 l_dest_mcc_rec = l_tcp->m_mcc_records;
8409 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8411 if (l_src_mcc_rec->m_decorrelation_array) {
8412 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8413 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8416 if (l_src_mcc_rec->m_offset_array) {
8417 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8418 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8425 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8426 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8428 /* Move to next tile cp*/
8432 /* Create the current tile decoder*/
8433 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8434 if (! p_j2k->m_tcd ) {
8438 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8439 tcd_destroy_v2(p_j2k->m_tcd);
8441 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8449 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8450 * with the marker value.
8451 * @param p_id Marker value to look up
8453 * @return the handler associated with the id.
8455 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8457 const opj_dec_memory_marker_handler_t *e;
8458 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8459 if (e->id == p_id) {
8460 break; /* we find a handler corresponding to the marker ID*/
8468 * Destroys a jpeg2000 codec.
8470 * @param p_j2k the jpeg20000 structure to destroy.
8472 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8478 if (p_j2k->m_is_decoder) {
8480 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8481 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8482 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8483 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8486 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8487 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8488 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8489 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8494 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8495 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8496 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8499 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8500 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8501 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8502 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8505 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8506 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8507 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8508 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8512 tcd_destroy_v2(p_j2k->m_tcd);
8514 j2k_cp_destroy(&(p_j2k->m_cp));
8515 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8517 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8518 p_j2k->m_procedure_list = 00;
8520 opj_procedure_list_destroy(p_j2k->m_validation_list);
8521 p_j2k->m_procedure_list = 00;
8523 j2k_destroy_cstr_index(p_j2k->cstr_index);
8524 p_j2k->cstr_index = NULL;
8526 opj_image_destroy(p_j2k->m_private_image);
8527 p_j2k->m_private_image = NULL;
8529 opj_image_destroy(p_j2k->m_output_image);
8530 p_j2k->m_output_image = NULL;
8535 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8539 if (p_cstr_ind->marker) {
8540 opj_free(p_cstr_ind->marker);
8541 p_cstr_ind->marker = NULL;
8544 if (p_cstr_ind->tile_index) {
8545 OPJ_UINT32 it_tile = 0;
8547 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8549 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8550 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8551 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8554 if(p_cstr_ind->tile_index[it_tile].tp_index){
8555 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8556 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8559 if(p_cstr_ind->tile_index[it_tile].marker){
8560 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8561 p_cstr_ind->tile_index[it_tile].marker = NULL;
8566 opj_free( p_cstr_ind->tile_index);
8567 p_cstr_ind->tile_index = NULL;
8570 opj_free(p_cstr_ind);
8577 * Destroys a tile coding parameter structure.
8579 * @param p_tcp the tile coding parameter to destroy.
8581 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8587 if (p_tcp->ppt_buffer != 00) {
8588 opj_free(p_tcp->ppt_buffer);
8589 p_tcp->ppt_buffer = 00;
8592 if (p_tcp->tccps != 00) {
8593 opj_free(p_tcp->tccps);
8597 if (p_tcp->m_mct_coding_matrix != 00) {
8598 opj_free(p_tcp->m_mct_coding_matrix);
8599 p_tcp->m_mct_coding_matrix = 00;
8602 if (p_tcp->m_mct_decoding_matrix != 00) {
8603 opj_free(p_tcp->m_mct_decoding_matrix);
8604 p_tcp->m_mct_decoding_matrix = 00;
8607 if (p_tcp->m_mcc_records) {
8608 opj_free(p_tcp->m_mcc_records);
8609 p_tcp->m_mcc_records = 00;
8610 p_tcp->m_nb_max_mcc_records = 0;
8611 p_tcp->m_nb_mcc_records = 0;
8614 if (p_tcp->m_mct_records) {
8615 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8618 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8619 if (l_mct_data->m_data) {
8620 opj_free(l_mct_data->m_data);
8621 l_mct_data->m_data = 00;
8627 opj_free(p_tcp->m_mct_records);
8628 p_tcp->m_mct_records = 00;
8631 if (p_tcp->mct_norms != 00) {
8632 opj_free(p_tcp->mct_norms);
8633 p_tcp->mct_norms = 00;
8636 j2k_tcp_data_destroy(p_tcp);
8641 * Destroys the data inside a tile coding parameter structure.
8643 * @param p_tcp the tile coding parameter which contain data to destroy.
8645 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8647 if (p_tcp->m_data) {
8648 opj_free(p_tcp->m_data);
8649 p_tcp->m_data = NULL;
8650 p_tcp->m_data_size = 0;
8655 * Destroys a coding parameter structure.
8657 * @param p_cp the coding parameter to destroy.
8659 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8661 OPJ_UINT32 l_nb_tiles;
8662 opj_tcp_v2_t * l_current_tile = 00;
8673 l_current_tile = p_cp->tcps;
8674 l_nb_tiles = p_cp->th * p_cp->tw;
8677 (i = 0; i < l_nb_tiles; ++i)
8679 j2k_tcp_destroy(l_current_tile);
8682 opj_free(p_cp->tcps);
8686 (p_cp->ppm_buffer != 00)
8688 opj_free(p_cp->ppm_buffer);
8689 p_cp->ppm_buffer = 00;
8692 (p_cp->comment != 00)
8694 opj_free(p_cp->comment);
8698 (! p_cp->m_is_decoder)
8701 (p_cp->m_specific_param.m_enc.m_matrice)
8703 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8704 p_cp->m_specific_param.m_enc.m_matrice = 00;
8712 * Reads a tile header.
8713 * @param p_j2k the jpeg2000 codec.
8714 * @param p_stream the stream to write data to.
8715 * @param p_manager the user event manager.
8717 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8718 OPJ_UINT32 * p_tile_index,
8719 OPJ_UINT32 * p_data_size,
8720 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8721 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8722 OPJ_UINT32 * p_nb_comps,
8724 opj_stream_private_t *p_stream,
8725 opj_event_mgr_t * p_manager )
8727 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8728 OPJ_UINT32 l_marker_size;
8729 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8730 opj_tcp_v2_t * l_tcp = NULL;
8731 OPJ_UINT32 l_nb_tiles;
8734 assert(p_stream != 00);
8735 assert(p_j2k != 00);
8736 assert(p_manager != 00);
8738 /* Reach the End Of Codestream ?*/
8739 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8740 l_current_marker = J2K_MS_EOC;
8742 /* We need to encounter a SOT marker (a new tile-part header) */
8743 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8747 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8748 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8750 /* Try to read until the Start Of Data is detected */
8751 while (l_current_marker != J2K_MS_SOD) {
8753 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8754 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8755 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8759 /* Read 2 bytes from the buffer as the marker size */
8760 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8762 /* Why this condition? FIXME */
8763 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8764 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8766 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8768 /* Get the marker handler from the marker ID */
8769 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8771 /* Check if the marker is known and if it is the right place to find it */
8772 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8773 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8776 /* FIXME manage case of unknown marker as in the main header ? */
8778 /* Check if the marker size is compatible with the header data size */
8779 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8780 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8781 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8782 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8785 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8788 /* Try to read the rest of the marker segment 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,l_marker_size,p_manager) != l_marker_size) {
8790 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8794 /* Read the marker segment with the correct marker handler */
8795 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8796 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8800 /* Add the marker to the codestream index*/
8801 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8803 l_marker_handler->id,
8804 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8805 l_marker_size + 4 );
8807 /* Keep the position of the last SOT marker read */
8808 if ( l_marker_handler->id == J2K_MS_SOT ) {
8809 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8810 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8812 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8817 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8818 /* Skip the rest of the tile part header*/
8819 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) {
8820 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8823 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8826 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8827 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8828 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8831 /* Read 2 bytes from the buffer as the new marker ID */
8832 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8836 /* If we didn't skip data before, we need to read the SOD marker*/
8837 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8838 /* Try to read the SOD marker and skip data ? FIXME */
8839 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8845 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8846 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8847 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8848 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8852 /* Read 2 bytes from buffer as the new marker ID */
8853 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8857 /* Indicate we will try to read a new tile-part header*/
8858 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8859 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8860 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8862 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8863 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8864 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8868 /* Read 2 bytes from buffer as the new marker ID */
8869 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8873 /* Current marker is the EOC marker ?*/
8874 if (l_current_marker == J2K_MS_EOC) {
8875 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8876 p_j2k->m_current_tile_number = 0;
8877 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8882 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8883 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8884 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8886 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8887 ++p_j2k->m_current_tile_number;
8891 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8892 *p_go_on = OPJ_FALSE;
8898 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8899 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8903 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8904 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8906 *p_tile_index = p_j2k->m_current_tile_number;
8907 *p_go_on = OPJ_TRUE;
8908 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8909 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8910 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8911 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8912 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8913 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8915 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8921 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8922 OPJ_UINT32 p_tile_index,
8924 OPJ_UINT32 p_data_size,
8925 opj_stream_private_t *p_stream,
8926 opj_event_mgr_t * p_manager )
8928 OPJ_UINT32 l_current_marker;
8929 OPJ_BYTE l_data [2];
8930 opj_tcp_v2_t * l_tcp;
8933 assert(p_stream != 00);
8934 assert(p_j2k != 00);
8935 assert(p_manager != 00);
8937 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8938 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8942 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8943 if (! l_tcp->m_data) {
8944 j2k_tcp_destroy(l_tcp);
8948 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8952 p_j2k->cstr_index) ) {
8953 j2k_tcp_destroy(l_tcp);
8954 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8958 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8962 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8963 * we destroy just the data which will be re-read in read_tile_header*/
8964 /*j2k_tcp_destroy(l_tcp);
8965 p_j2k->m_tcd->tcp = 0;*/
8966 j2k_tcp_data_destroy(l_tcp);
8968 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8969 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8971 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8972 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8973 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8977 opj_read_bytes(l_data,&l_current_marker,2);
8979 if (l_current_marker == J2K_MS_EOC) {
8980 p_j2k->m_current_tile_number = 0;
8981 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8983 else if (l_current_marker != J2K_MS_SOT)
8985 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8994 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8996 OPJ_UINT32 i,j,k = 0;
8997 OPJ_UINT32 l_width_src,l_height_src;
8998 OPJ_UINT32 l_width_dest,l_height_dest;
8999 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9000 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9001 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9002 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9003 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9005 opj_image_comp_t * l_img_comp_src = 00;
9006 opj_image_comp_t * l_img_comp_dest = 00;
9008 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9009 opj_image_t * l_image_src = 00;
9010 OPJ_UINT32 l_size_comp, l_remaining;
9011 OPJ_INT32 * l_dest_ptr;
9012 opj_tcd_resolution_v2_t* l_res= 00;
9014 l_tilec = p_tcd->tcd_image->tiles->comps;
9015 l_image_src = p_tcd->image;
9016 l_img_comp_src = l_image_src->comps;
9018 l_img_comp_dest = p_output_image->comps;
9020 for (i=0; i<l_image_src->numcomps; i++) {
9022 /* Allocate output component buffer if necessary */
9023 if (!l_img_comp_dest->data) {
9025 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9026 if (! l_img_comp_dest->data) {
9031 /* Copy info from decoded comp image to output image */
9032 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9035 /* Compute the precision of the output buffer */
9036 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9037 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9038 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9044 if (l_size_comp == 3) {
9049 /* Current tile component size*/
9051 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9052 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9055 l_width_src = (l_res->x1 - l_res->x0);
9056 l_height_src = (l_res->y1 - l_res->y0);
9058 /* Border of the current output component*/
9059 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9060 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9061 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9062 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9065 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9066 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9070 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9071 * of the input buffer (decoded tile component) which will be move
9072 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9073 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9074 * by this input area.
9076 assert( l_res->x0 >= 0);
9077 assert( l_res->x1 >= 0);
9078 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9079 l_start_x_dest = l_res->x0 - l_x0_dest;
9080 l_offset_x0_src = 0;
9082 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9083 l_width_dest = l_width_src;
9084 l_offset_x1_src = 0;
9087 l_width_dest = l_x1_dest - l_res->x0 ;
9088 l_offset_x1_src = l_width_src - l_width_dest;
9092 l_start_x_dest = 0 ;
9093 l_offset_x0_src = l_x0_dest - l_res->x0;
9095 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9096 l_width_dest = l_width_src - l_offset_x0_src;
9097 l_offset_x1_src = 0;
9100 l_width_dest = l_img_comp_dest->w ;
9101 l_offset_x1_src = l_res->x1 - l_x1_dest;
9105 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9106 l_start_y_dest = l_res->y0 - l_y0_dest;
9107 l_offset_y0_src = 0;
9109 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9110 l_height_dest = l_height_src;
9111 l_offset_y1_src = 0;
9114 l_height_dest = l_y1_dest - l_res->y0 ;
9115 l_offset_y1_src = l_height_src - l_height_dest;
9119 l_start_y_dest = 0 ;
9120 l_offset_y0_src = l_y0_dest - l_res->y0;
9122 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9123 l_height_dest = l_height_src - l_offset_y0_src;
9124 l_offset_y1_src = 0;
9127 l_height_dest = l_img_comp_dest->h ;
9128 l_offset_y1_src = l_res->y1 - l_y1_dest;
9132 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9137 /* Compute the input buffer offset */
9138 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9139 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9140 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9142 /* Compute the output buffer offset */
9143 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9144 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9146 /* Move the output buffer to the first place where we will write*/
9147 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9150 fprintf(stdout, "COMPO[%d]:\n",i);
9151 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9152 "\t tile offset:%d, %d, %d, %d\n"
9153 "\t buffer offset: %d; %d, %d\n",
9154 l_res->x0, l_res->y0, l_width_src, l_height_src,
9155 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9156 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9158 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9159 "\t start offset: %d, line offset= %d\n",
9160 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9164 switch (l_size_comp) {
9167 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9168 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9170 if (l_img_comp_src->sgnd) {
9171 for (j = 0 ; j < l_height_dest ; ++j) {
9172 for ( k = 0 ; k < l_width_dest ; ++k) {
9173 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9176 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9177 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9181 for ( j = 0 ; j < l_height_dest ; ++j ) {
9182 for ( k = 0 ; k < l_width_dest ; ++k) {
9183 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9186 l_dest_ptr+= l_line_offset_dest;
9187 l_src_ptr += l_line_offset_src;
9191 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9192 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9197 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9198 l_src_ptr += l_start_offset_src;
9200 if (l_img_comp_src->sgnd) {
9201 for (j=0;j<l_height_dest;++j) {
9202 for (k=0;k<l_width_dest;++k) {
9203 *(l_dest_ptr++) = *(l_src_ptr++);
9206 l_dest_ptr+= l_line_offset_dest;
9207 l_src_ptr += l_line_offset_src ;
9211 for (j=0;j<l_height_dest;++j) {
9212 for (k=0;k<l_width_dest;++k) {
9213 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9216 l_dest_ptr+= l_line_offset_dest;
9217 l_src_ptr += l_line_offset_src ;
9221 l_src_ptr += l_end_offset_src;
9222 p_data = (OPJ_BYTE*) l_src_ptr;
9227 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9228 l_src_ptr += l_start_offset_src;
9230 for (j=0;j<l_height_dest;++j) {
9231 for (k=0;k<l_width_dest;++k) {
9232 *(l_dest_ptr++) = (*(l_src_ptr++));
9235 l_dest_ptr+= l_line_offset_dest;
9236 l_src_ptr += l_line_offset_src ;
9239 l_src_ptr += l_end_offset_src;
9240 p_data = (OPJ_BYTE*) l_src_ptr;
9254 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9256 * @param p_j2k the jpeg2000 codec.
9257 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9258 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9259 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9260 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9261 * @param p_manager the user event manager
9263 * @return true if the area could be set.
9265 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9266 opj_image_t* p_image,
9267 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9268 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9269 struct opj_event_mgr * p_manager )
9271 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9272 opj_image_t * l_image = p_j2k->m_private_image;
9275 OPJ_INT32 l_comp_x1, l_comp_y1;
9276 opj_image_comp_t* l_img_comp = NULL;
9278 /* Check if we are read the main header */
9279 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9280 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9284 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9285 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9287 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9288 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9289 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9290 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9296 /* Check if the positions provided by the user are correct */
9299 assert(p_start_x >= 0 );
9300 assert(p_start_y >= 0 );
9302 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9303 opj_event_msg_v2(p_manager, EVT_ERROR,
9304 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9305 p_start_x, l_image->x1);
9308 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9309 opj_event_msg_v2(p_manager, EVT_WARNING,
9310 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9311 p_start_x, l_image->x0);
9312 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9313 p_image->x0 = l_image->x0;
9316 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9317 p_image->x0 = p_start_x;
9321 if ((OPJ_UINT32)p_start_y > l_image->y1){
9322 opj_event_msg_v2(p_manager, EVT_ERROR,
9323 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9324 p_start_y, l_image->y1);
9327 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9328 opj_event_msg_v2(p_manager, EVT_WARNING,
9329 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9330 p_start_y, l_image->y0);
9331 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9332 p_image->y0 = l_image->y0;
9335 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9336 p_image->y0 = p_start_y;
9340 assert((OPJ_UINT32)p_end_x > 0);
9341 assert((OPJ_UINT32)p_end_y > 0);
9342 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9343 opj_event_msg_v2(p_manager, EVT_ERROR,
9344 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9345 p_end_x, l_image->x0);
9348 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9349 opj_event_msg_v2(p_manager, EVT_WARNING,
9350 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9351 p_end_x, l_image->x1);
9352 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9353 p_image->x1 = l_image->x1;
9356 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9357 p_image->x1 = p_end_x;
9361 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9362 opj_event_msg_v2(p_manager, EVT_ERROR,
9363 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9364 p_end_y, l_image->y0);
9367 if ((OPJ_UINT32)p_end_y > l_image->y1){
9368 opj_event_msg_v2(p_manager, EVT_WARNING,
9369 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9370 p_end_y, l_image->y1);
9371 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9372 p_image->y1 = l_image->y1;
9375 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9376 p_image->y1 = p_end_y;
9380 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9382 l_img_comp = p_image->comps;
9383 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9387 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9388 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9389 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9390 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9392 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9393 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9395 opj_event_msg_v2(p_manager, EVT_ERROR,
9396 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9400 l_img_comp->w = l_w;
9402 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9403 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9405 opj_event_msg_v2(p_manager, EVT_ERROR,
9406 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9410 l_img_comp->h = l_h;
9415 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9416 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9423 /* ----------------------------------------------------------------------- */
9424 /* J2K / JPT decoder interface */
9425 /* ----------------------------------------------------------------------- */
9427 * Creates a J2K decompression structure.
9429 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9431 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9433 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9437 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9439 l_j2k->m_is_decoder = 1;
9440 l_j2k->m_cp.m_is_decoder = 1;
9442 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9443 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9447 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9449 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9450 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9455 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9457 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9459 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9461 /* codestream index creation */
9462 l_j2k->cstr_index = j2k_create_cstr_index();
9464 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9465 if (!l_j2k->cstr_index){
9470 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9473 /* validation list creation */
9474 l_j2k->m_validation_list = opj_procedure_list_create();
9475 if (! l_j2k->m_validation_list) {
9480 /* execution list creation */
9481 l_j2k->m_procedure_list = opj_procedure_list_create();
9482 if (! l_j2k->m_procedure_list) {
9491 opj_codestream_index_t* j2k_create_cstr_index(void)
9493 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9494 opj_calloc(1,sizeof(opj_codestream_index_t));
9498 cstr_index->maxmarknum = 100;
9499 cstr_index->marknum = 0;
9500 cstr_index->marker = (opj_marker_info_t*)
9501 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9502 if (!cstr_index-> marker)
9505 cstr_index->tile_index = NULL;
9512 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9514 * @param p_tile_no the tile index.
9515 * @param p_comp_no the component being outputted.
9516 * @param p_j2k the J2K codec.
9518 * @return the number of bytes taken by the SPCod element.
9520 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9521 OPJ_UINT32 p_tile_no,
9522 OPJ_UINT32 p_comp_no )
9524 opj_cp_v2_t *l_cp = 00;
9525 opj_tcp_v2_t *l_tcp = 00;
9526 opj_tccp_t *l_tccp = 00;
9529 assert(p_j2k != 00);
9531 l_cp = &(p_j2k->m_cp);
9532 l_tcp = &l_cp->tcps[p_tile_no];
9533 l_tccp = &l_tcp->tccps[p_comp_no];
9535 /* preconditions again */
9536 assert(p_tile_no < (l_cp->tw * l_cp->th));
9537 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9539 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9540 return 5 + l_tccp->numresolutions;
9548 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9550 * @param p_comp_no the component number to output.
9551 * @param p_stream the stream to write data to.
9552 * @param p_j2k J2K codec.
9553 * @param p_manager the user event manager.
9556 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9557 OPJ_UINT32 p_tile_no,
9558 OPJ_UINT32 p_comp_no,
9560 OPJ_UINT32 * p_header_size,
9561 struct opj_event_mgr * p_manager )
9564 opj_cp_v2_t *l_cp = 00;
9565 opj_tcp_v2_t *l_tcp = 00;
9566 opj_tccp_t *l_tccp = 00;
9569 assert(p_j2k != 00);
9570 assert(p_header_size != 00);
9571 assert(p_manager != 00);
9572 assert(p_data != 00);
9574 l_cp = &(p_j2k->m_cp);
9575 l_tcp = &l_cp->tcps[p_tile_no];
9576 l_tccp = &l_tcp->tccps[p_comp_no];
9578 /* preconditions again */
9579 assert(p_tile_no < (l_cp->tw * l_cp->th));
9580 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9582 if (*p_header_size < 5) {
9583 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9587 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9590 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9593 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9596 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9599 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9602 *p_header_size = *p_header_size - 5;
9604 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9606 if (*p_header_size < l_tccp->numresolutions) {
9607 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9611 for (i = 0; i < l_tccp->numresolutions; ++i) {
9612 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9616 *p_header_size = *p_header_size - l_tccp->numresolutions;
9623 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9624 * @param p_header_data the data contained in the COM box.
9625 * @param p_j2k the jpeg2000 codec.
9626 * @param p_header_size the size of the data contained in the COM marker.
9627 * @param p_manager the user event manager.
9629 opj_bool j2k_read_SPCod_SPCoc(
9630 opj_j2k_v2_t *p_j2k,
9632 OPJ_BYTE * p_header_data,
9633 OPJ_UINT32 * p_header_size,
9634 struct opj_event_mgr * p_manager
9637 OPJ_UINT32 i, l_tmp;
9638 opj_cp_v2_t *l_cp = NULL;
9639 opj_tcp_v2_t *l_tcp = NULL;
9640 opj_tccp_t *l_tccp = NULL;
9641 OPJ_BYTE * l_current_ptr = NULL;
9644 assert(p_j2k != 00);
9645 assert(p_manager != 00);
9646 assert(p_header_data != 00);
9648 l_cp = &(p_j2k->m_cp);
9649 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9650 &l_cp->tcps[p_j2k->m_current_tile_number] :
9651 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9653 /* precondition again */
9654 assert(compno < p_j2k->m_private_image->numcomps);
9656 l_tccp = &l_tcp->tccps[compno];
9657 l_current_ptr = p_header_data;
9659 /* make sure room is sufficient */
9660 if (*p_header_size < 5) {
9661 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9665 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9666 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9669 /* If user wants to remove more resolutions than the codestream contains, return error */
9670 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9671 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9672 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9673 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9677 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9681 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9685 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9688 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9691 *p_header_size = *p_header_size - 5;
9693 /* use custom precinct size ? */
9694 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9695 if (*p_header_size < l_tccp->numresolutions) {
9696 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9700 for (i = 0; i < l_tccp->numresolutions; ++i) {
9701 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9703 l_tccp->prcw[i] = l_tmp & 0xf;
9704 l_tccp->prch[i] = l_tmp >> 4;
9707 *p_header_size = *p_header_size - l_tccp->numresolutions;
9710 /* set default size for the precinct width and height */
9711 for (i = 0; i < l_tccp->numresolutions; ++i) {
9712 l_tccp->prcw[i] = 15;
9713 l_tccp->prch[i] = 15;
9717 #ifdef WIP_REMOVE_MSD
9719 if (p_j2k->cstr_info && compno == 0) {
9720 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9722 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9723 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9724 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9725 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9726 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9729 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9730 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9739 * Copies the tile component parameters of all the component from the first tile component.
9741 * @param p_j2k the J2k codec.
9743 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9747 opj_cp_v2_t *l_cp = NULL;
9748 opj_tcp_v2_t *l_tcp = NULL;
9749 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9750 OPJ_UINT32 l_prc_size;
9753 assert(p_j2k != 00);
9755 l_cp = &(p_j2k->m_cp);
9756 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9757 &l_cp->tcps[p_j2k->m_current_tile_number] :
9758 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9760 l_ref_tccp = &l_tcp->tccps[0];
9761 l_copied_tccp = l_ref_tccp + 1;
9762 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9764 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9765 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9766 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9767 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9768 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9769 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9770 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9771 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9777 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9779 * @param p_tile_no the tile index.
9780 * @param p_comp_no the component being outputted.
9781 * @param p_j2k the J2K codec.
9783 * @return the number of bytes taken by the SPCod element.
9785 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9786 OPJ_UINT32 p_tile_no,
9787 OPJ_UINT32 p_comp_no )
9789 OPJ_UINT32 l_num_bands;
9791 opj_cp_v2_t *l_cp = 00;
9792 opj_tcp_v2_t *l_tcp = 00;
9793 opj_tccp_t *l_tccp = 00;
9796 assert(p_j2k != 00);
9798 l_cp = &(p_j2k->m_cp);
9799 l_tcp = &l_cp->tcps[p_tile_no];
9800 l_tccp = &l_tcp->tccps[p_comp_no];
9802 /* preconditions again */
9803 assert(p_tile_no < l_cp->tw * l_cp->th);
9804 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9806 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9808 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9809 return 1 + l_num_bands;
9812 return 1 + 2*l_num_bands;
9817 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9819 * @param p_tile_no the tile to output.
9820 * @param p_comp_no the component number to output.
9821 * @param p_data the data buffer.
9822 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9823 * @param p_j2k J2K codec.
9824 * @param p_manager the user event manager.
9827 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9828 OPJ_UINT32 p_tile_no,
9829 OPJ_UINT32 p_comp_no,
9831 OPJ_UINT32 * p_header_size,
9832 struct opj_event_mgr * p_manager )
9834 OPJ_UINT32 l_header_size;
9835 OPJ_UINT32 l_band_no, l_num_bands;
9836 OPJ_UINT32 l_expn,l_mant;
9838 opj_cp_v2_t *l_cp = 00;
9839 opj_tcp_v2_t *l_tcp = 00;
9840 opj_tccp_t *l_tccp = 00;
9843 assert(p_j2k != 00);
9844 assert(p_header_size != 00);
9845 assert(p_manager != 00);
9846 assert(p_data != 00);
9848 l_cp = &(p_j2k->m_cp);
9849 l_tcp = &l_cp->tcps[p_tile_no];
9850 l_tccp = &l_tcp->tccps[p_comp_no];
9852 /* preconditions again */
9853 assert(p_tile_no < l_cp->tw * l_cp->th);
9854 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9856 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9858 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9859 l_header_size = 1 + l_num_bands;
9861 if (*p_header_size < l_header_size) {
9862 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9866 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9869 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9870 l_expn = l_tccp->stepsizes[l_band_no].expn;
9871 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9876 l_header_size = 1 + 2*l_num_bands;
9878 if (*p_header_size < l_header_size) {
9879 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9883 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9886 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9887 l_expn = l_tccp->stepsizes[l_band_no].expn;
9888 l_mant = l_tccp->stepsizes[l_band_no].mant;
9890 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9895 *p_header_size = *p_header_size - l_header_size;
9901 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9903 * @param p_comp_no the component being targeted.
9904 * @param p_header_data the data contained in the COM box.
9905 * @param p_j2k the jpeg2000 codec.
9906 * @param p_header_size the size of the data contained in the COM marker.
9907 * @param p_manager the user event manager.
9909 opj_bool j2k_read_SQcd_SQcc(
9910 opj_j2k_v2_t *p_j2k,
9911 OPJ_UINT32 p_comp_no,
9912 OPJ_BYTE* p_header_data,
9913 OPJ_UINT32 * p_header_size,
9914 struct opj_event_mgr * p_manager
9918 OPJ_UINT32 l_band_no;
9919 opj_cp_v2_t *l_cp = 00;
9920 opj_tcp_v2_t *l_tcp = 00;
9921 opj_tccp_t *l_tccp = 00;
9922 OPJ_BYTE * l_current_ptr = 00;
9923 OPJ_UINT32 l_tmp, l_num_band;
9926 assert(p_j2k != 00);
9927 assert(p_manager != 00);
9928 assert(p_header_data != 00);
9930 l_cp = &(p_j2k->m_cp);
9931 /* come from tile part header or main header ?*/
9932 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9933 &l_cp->tcps[p_j2k->m_current_tile_number] :
9934 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9936 /* precondition again*/
9937 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9939 l_tccp = &l_tcp->tccps[p_comp_no];
9940 l_current_ptr = p_header_data;
9942 if (*p_header_size < 1) {
9943 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9946 *p_header_size -= 1;
9948 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9951 l_tccp->qntsty = l_tmp & 0x1f;
9952 l_tccp->numgbits = l_tmp >> 5;
9953 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9957 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9959 (*p_header_size) / 2;
9961 if( l_num_band > J2K_MAXBANDS ) {
9962 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9963 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9964 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9965 /*return OPJ_FALSE;*/
9970 if (l_cp->correct) {
9972 /* if JPWL is on, we check whether there are too many subbands */
9973 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9974 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9975 "JPWL: bad number of subbands in Sqcx (%d)\n",
9978 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9981 /* we try to correct */
9983 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9984 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9989 #endif /* USE_JPWL */
9991 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9992 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9993 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9995 if (l_band_no < J2K_MAXBANDS){
9996 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9997 l_tccp->stepsizes[l_band_no].mant = 0;
10000 *p_header_size = *p_header_size - l_num_band;
10003 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10004 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10006 if (l_band_no < J2K_MAXBANDS){
10007 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10008 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10011 *p_header_size = *p_header_size - 2*l_num_band;
10014 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10015 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10016 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10017 l_tccp->stepsizes[l_band_no].expn =
10018 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10019 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10020 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10028 * Copies the tile component parameters of all the component from the first tile component.
10030 * @param p_j2k the J2k codec.
10032 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10035 opj_cp_v2_t *l_cp = NULL;
10036 opj_tcp_v2_t *l_tcp = NULL;
10037 opj_tccp_t *l_ref_tccp = NULL;
10038 opj_tccp_t *l_copied_tccp = NULL;
10041 /* preconditions */
10042 assert(p_j2k != 00);
10044 l_cp = &(p_j2k->m_cp);
10045 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10046 &l_cp->tcps[p_j2k->m_current_tile_number] :
10047 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10049 l_ref_tccp = &l_tcp->tccps[0];
10050 l_copied_tccp = l_ref_tccp + 1;
10051 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10053 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10054 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10055 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10056 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10062 * Dump some elements from the J2K decompression structure .
10064 *@param p_j2k the jpeg2000 codec.
10065 *@param flag flag to describe what elments are dump.
10066 *@param out_stream output stream where dump the elements.
10069 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10071 /* Check if the flag is compatible with j2k file*/
10072 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10073 fprintf(out_stream, "Wrong flag\n");
10077 /* Dump the image_header */
10078 if (flag & OPJ_IMG_INFO){
10079 if (p_j2k->m_private_image)
10080 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10083 /* Dump the codestream info from main header */
10084 if (flag & OPJ_J2K_MH_INFO){
10085 j2k_dump_MH_info(p_j2k, out_stream);
10089 /* Dump the codestream info of the current tile */
10090 if (flag & OPJ_J2K_TH_INFO){
10094 /* Dump the codestream index from main header */
10095 if (flag & OPJ_J2K_MH_IND){
10096 j2k_dump_MH_index(p_j2k, out_stream);
10099 /* Dump the codestream index of the current tile */
10100 if (flag & OPJ_J2K_TH_IND){
10107 * Dump index elements of the codestream extract from the main header.
10109 *@param p_j2k the jpeg2000 codec.
10110 *@param out_stream output stream where dump the elements.
10113 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10115 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10116 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10118 fprintf(out_stream, "Codestream index from main header: {\n");
10120 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10121 "\t Main header end position=%" PRIi64 "\n",
10122 cstr_index->main_head_start, cstr_index->main_head_end);
10124 fprintf(out_stream, "\t Marker list: {\n");
10126 if (cstr_index->marker){
10127 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10128 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10129 cstr_index->marker[it_marker].type,
10130 cstr_index->marker[it_marker].pos,
10131 cstr_index->marker[it_marker].len );
10135 fprintf(out_stream, "\t }\n");
10138 if (cstr_index->tile_index){
10140 /* Simple test to avoid to write empty information*/
10141 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10142 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10143 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10146 if (l_acc_nb_of_tile_part)
10148 fprintf(out_stream, "\t Tile index: {\n");
10150 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10151 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10153 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10155 if (cstr_index->tile_index[it_tile].tp_index){
10156 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10157 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10159 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10160 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10161 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10165 if (cstr_index->tile_index[it_tile].marker){
10166 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10167 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10168 cstr_index->tile_index[it_tile].marker[it_marker].type,
10169 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10170 cstr_index->tile_index[it_tile].marker[it_marker].len );
10174 fprintf(out_stream,"\t }\n");
10178 fprintf(out_stream,"}\n");
10183 * Dump info elements of the codestream extract from the main header.
10185 *@param p_j2k the jpeg2000 codec.
10186 *@param out_stream output stream where dump the elements.
10189 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10191 opj_tcp_v2_t * l_default_tile=NULL;
10193 fprintf(out_stream, "Codestream info from main header: {\n");
10195 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10196 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10197 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10199 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10200 if (l_default_tile)
10203 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10205 fprintf(out_stream, "\t default tile {\n");
10206 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10207 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10208 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10209 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10211 for (compno = 0; compno < numcomps; compno++) {
10212 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10214 OPJ_INT32 bandno, numbands;
10217 fprintf(out_stream, "\t\t comp %d {\n", compno);
10218 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10219 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10220 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10221 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10222 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10223 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10225 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10226 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10227 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10229 fprintf(out_stream, "\n");
10231 /* quantization style*/
10232 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10233 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10234 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10235 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10236 for (bandno = 0; bandno < numbands; bandno++) {
10237 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10238 l_tccp->stepsizes[bandno].expn);
10240 fprintf(out_stream, "\n");
10243 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10245 fprintf(out_stream, "\t\t }\n");
10246 } /*end of component of default tile*/
10247 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10251 fprintf(out_stream, "}\n");
10256 * Dump an image header structure.
10258 *@param img_header the image header to dump.
10259 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10260 *@param out_stream output stream where dump the elements.
10262 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10266 if (dev_dump_flag){
10267 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10271 fprintf(out_stream, "Image info {\n");
10272 tab[0] = '\t';tab[1] = '\0';
10275 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10276 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10277 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10279 if (img_header->comps){
10281 for (compno = 0; compno < img_header->numcomps; compno++) {
10282 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10283 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10284 fprintf(out_stream,"%s}\n",tab);
10288 fprintf(out_stream, "}\n");
10292 * Dump a component image header structure.
10294 *@param comp_header the component image header to dump.
10295 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10296 *@param out_stream output stream where dump the elements.
10298 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10302 if (dev_dump_flag){
10303 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10306 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10309 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10310 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10311 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10314 fprintf(out_stream, "}\n");
10319 * Get the codestream info from a JPEG2000 codec.
10321 *@param p_j2k the component image header to dump.
10323 *@return the codestream information extract from the jpg2000 codec
10325 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10328 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10329 opj_tcp_v2_t *l_default_tile;
10330 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10332 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10334 cstr_info->tx0 = p_j2k->m_cp.tx0;
10335 cstr_info->ty0 = p_j2k->m_cp.ty0;
10336 cstr_info->tdx = p_j2k->m_cp.tdx;
10337 cstr_info->tdy = p_j2k->m_cp.tdy;
10338 cstr_info->tw = p_j2k->m_cp.tw;
10339 cstr_info->th = p_j2k->m_cp.th;
10341 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10343 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10345 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10346 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10347 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10348 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10350 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10352 for (compno = 0; compno < numcomps; compno++) {
10353 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10354 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10355 OPJ_INT32 bandno, numbands;
10358 l_tccp_info->csty = l_tccp->csty;
10359 l_tccp_info->numresolutions = l_tccp->numresolutions;
10360 l_tccp_info->cblkw = l_tccp->cblkw;
10361 l_tccp_info->cblkh = l_tccp->cblkh;
10362 l_tccp_info->cblksty = l_tccp->cblksty;
10363 l_tccp_info->qmfbid = l_tccp->qmfbid;
10364 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10366 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10367 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10370 /* quantization style*/
10371 l_tccp_info->qntsty = l_tccp->qntsty;
10372 l_tccp_info->numgbits = l_tccp->numgbits;
10374 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10375 if (numbands < J2K_MAXBANDS) {
10376 for (bandno = 0; bandno < numbands; bandno++) {
10377 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10378 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10383 l_tccp_info->roishift = l_tccp->roishift;
10391 * Get the codestream index from a JPEG2000 codec.
10393 *@param p_j2k the component image header to dump.
10395 *@return the codestream index extract from the jpg2000 codec
10397 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10399 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10400 opj_calloc(1,sizeof(opj_codestream_index_t));
10404 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10405 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10406 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10408 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10409 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10410 if (!l_cstr_index->marker){
10411 opj_free( l_cstr_index);
10415 if (p_j2k->cstr_index->marker)
10416 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10418 opj_free(l_cstr_index->marker);
10419 l_cstr_index->marker = NULL;
10422 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10423 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10424 if (!l_cstr_index->tile_index){
10425 opj_free( l_cstr_index->marker);
10426 opj_free( l_cstr_index);
10430 if (!p_j2k->cstr_index->tile_index){
10431 opj_free(l_cstr_index->tile_index);
10432 l_cstr_index->tile_index = NULL;
10435 OPJ_UINT32 it_tile = 0;
10436 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10439 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10441 l_cstr_index->tile_index[it_tile].marker =
10442 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10444 if (!l_cstr_index->tile_index[it_tile].marker) {
10445 OPJ_UINT32 it_tile_free;
10447 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10448 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10451 opj_free( l_cstr_index->tile_index);
10452 opj_free( l_cstr_index->marker);
10453 opj_free( l_cstr_index);
10457 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10458 memcpy( l_cstr_index->tile_index[it_tile].marker,
10459 p_j2k->cstr_index->tile_index[it_tile].marker,
10460 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10462 opj_free(l_cstr_index->tile_index[it_tile].marker);
10463 l_cstr_index->tile_index[it_tile].marker = NULL;
10466 /* Tile part index*/
10467 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10469 l_cstr_index->tile_index[it_tile].tp_index =
10470 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10472 if(!l_cstr_index->tile_index[it_tile].tp_index){
10473 OPJ_UINT32 it_tile_free;
10475 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10476 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10477 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10480 opj_free( l_cstr_index->tile_index);
10481 opj_free( l_cstr_index->marker);
10482 opj_free( l_cstr_index);
10486 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10487 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10488 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10489 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10492 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10493 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10496 /* Packet index (NOT USED)*/
10497 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10498 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10503 return l_cstr_index;
10506 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10508 OPJ_UINT32 it_tile=0;
10510 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10511 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10512 if (!p_j2k->cstr_index->tile_index)
10515 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10516 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10517 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10518 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10519 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10520 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10530 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10531 opj_stream_private_t *p_stream,
10532 opj_event_mgr_t * p_manager)
10534 opj_bool l_go_on = OPJ_TRUE;
10535 OPJ_UINT32 l_current_tile_no;
10536 OPJ_UINT32 l_data_size,l_max_data_size;
10537 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10538 OPJ_UINT32 l_nb_comps;
10539 OPJ_BYTE * l_current_data;
10541 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10542 if (! l_current_data) {
10545 l_max_data_size = 1000;
10550 if (! j2k_read_tile_header( p_j2k,
10551 &l_current_tile_no,
10553 &l_tile_x0, &l_tile_y0,
10554 &l_tile_x1, &l_tile_y1,
10559 opj_free(l_current_data);
10567 if (l_data_size > l_max_data_size) {
10568 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10569 if (! l_current_data) {
10570 opj_free(l_current_data);
10574 l_max_data_size = l_data_size;
10577 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10578 opj_free(l_current_data);
10581 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);
10583 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10584 opj_free(l_current_data);
10587 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10591 opj_free(l_current_data);
10597 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10599 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10602 assert(p_j2k != 00);
10604 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10605 /* DEVELOPER CORNER, add your custom procedures */
10610 * Read and decode one tile.
10612 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10613 opj_stream_private_t *p_stream,
10614 opj_event_mgr_t * p_manager)
10616 opj_bool l_go_on = OPJ_TRUE;
10617 OPJ_UINT32 l_current_tile_no;
10618 OPJ_UINT32 l_tile_no_to_dec;
10619 OPJ_UINT32 l_data_size,l_max_data_size;
10620 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10621 OPJ_UINT32 l_nb_comps;
10622 OPJ_BYTE * l_current_data;
10624 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10625 if (! l_current_data) {
10628 l_max_data_size = 1000;
10630 /*Allocate and initialize some elements of codestrem index if not already done*/
10631 if( !p_j2k->cstr_index->tile_index)
10633 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10634 opj_free(l_current_data);
10638 /* Move into the codestream to the first SOT used to decode the desired tile */
10639 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10640 if (p_j2k->cstr_index->tile_index)
10641 if(p_j2k->cstr_index->tile_index->tp_index)
10643 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10644 /* the index for this tile has not been built,
10645 * so move to the last SOT read */
10646 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10647 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10652 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)) {
10653 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10657 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10658 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10659 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10663 if (! j2k_read_tile_header( p_j2k,
10664 &l_current_tile_no,
10666 &l_tile_x0, &l_tile_y0,
10667 &l_tile_x1, &l_tile_y1,
10672 opj_free(l_current_data);
10681 if (l_data_size > l_max_data_size) {
10682 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10683 if (! l_current_data) {
10684 opj_free(l_current_data);
10688 l_max_data_size = l_data_size;
10693 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10694 opj_free(l_current_data);
10697 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);
10699 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10700 opj_free(l_current_data);
10703 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10705 if(l_current_tile_no == l_tile_no_to_dec)
10707 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10708 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10709 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10715 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);
10720 opj_free(l_current_data);
10727 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10729 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10732 assert(p_j2k != 00);
10734 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10735 /* DEVELOPER CORNER, add your custom procedures */
10741 * Decodes the tiles of the stream.
10743 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10744 opj_stream_private_t * p_stream,
10745 opj_image_t * p_image,
10746 opj_event_mgr_t * p_manager)
10753 p_j2k->m_output_image = opj_image_create0();
10754 if (! (p_j2k->m_output_image)) {
10757 opj_copy_image_header(p_image, p_j2k->m_output_image);
10759 /* customization of the decoding */
10760 j2k_setup_decoding(p_j2k);
10762 /* Decode the codestream */
10763 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10764 opj_image_destroy(p_j2k->m_private_image);
10765 p_j2k->m_private_image = NULL;
10769 /* Move data and copy one information from codec to output image*/
10770 for (compno = 0; compno < p_image->numcomps; compno++) {
10771 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10772 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10773 p_j2k->m_output_image->comps[compno].data = NULL;
10781 * Get the decoded tile.
10783 * @param p_j2k the jpeg2000 codestream codec.
10784 * @param p_stream input_stream
10785 * @param p_image output image. .
10786 * @param p_manager the user event manager
10787 * @param tile_index index of the tile we want decode
10789 * @return true if succeed.
10791 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10792 opj_stream_private_t *p_stream,
10793 opj_image_t* p_image,
10794 struct opj_event_mgr * p_manager,
10795 OPJ_UINT32 tile_index )
10798 OPJ_UINT32 l_tile_x, l_tile_y;
10799 opj_image_comp_t* l_img_comp;
10802 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10806 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10807 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);
10811 /* Compute the dimension of the desired tile*/
10812 l_tile_x = tile_index % p_j2k->m_cp.tw;
10813 l_tile_y = tile_index / p_j2k->m_cp.tw;
10815 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10816 if (p_image->x0 < p_j2k->m_private_image->x0)
10817 p_image->x0 = p_j2k->m_private_image->x0;
10818 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10819 if (p_image->x1 > p_j2k->m_private_image->x1)
10820 p_image->x1 = p_j2k->m_private_image->x1;
10822 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10823 if (p_image->y0 < p_j2k->m_private_image->y0)
10824 p_image->y0 = p_j2k->m_private_image->y0;
10825 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10826 if (p_image->y1 > p_j2k->m_private_image->y1)
10827 p_image->y1 = p_j2k->m_private_image->y1;
10829 l_img_comp = p_image->comps;
10830 for (compno=0; compno < p_image->numcomps; ++compno)
10832 OPJ_INT32 l_comp_x1, l_comp_y1;
10834 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10836 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10837 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10838 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10839 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10841 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10842 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10847 /* Destroy the previous output image*/
10848 if (p_j2k->m_output_image)
10849 opj_image_destroy(p_j2k->m_output_image);
10851 /* Create the ouput image from the information previously computed*/
10852 p_j2k->m_output_image = opj_image_create0();
10853 if (! (p_j2k->m_output_image)) {
10856 opj_copy_image_header(p_image, p_j2k->m_output_image);
10858 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10860 /* customization of the decoding */
10861 j2k_setup_decoding_tile(p_j2k);
10863 /* Decode the codestream */
10864 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10865 opj_image_destroy(p_j2k->m_private_image);
10866 p_j2k->m_private_image = NULL;
10870 /* Move data and copy one information from codec to output image*/
10871 for (compno = 0; compno < p_image->numcomps; compno++) {
10872 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10874 if (p_image->comps[compno].data)
10875 opj_free(p_image->comps[compno].data);
10877 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10879 p_j2k->m_output_image->comps[compno].data = NULL;
10885 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
10887 OPJ_UINT32 it_comp;
10889 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10891 if (p_j2k->m_private_image) {
10892 if (p_j2k->m_private_image->comps) {
10893 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10894 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10895 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10896 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10897 if ( res_factor >= max_res){
10898 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10901 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10914 * Encodes all the tiles in a row.
10916 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10917 opj_stream_private_t *p_stream,
10918 opj_event_mgr_t * p_manager )
10921 OPJ_UINT32 l_nb_tiles;
10922 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10923 OPJ_BYTE * l_current_data;
10925 /* preconditions */
10926 assert(p_j2k != 00);
10927 assert(p_stream != 00);
10928 assert(p_manager != 00);
10930 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10931 if (! l_current_data) {
10934 l_max_tile_size = 1000;
10936 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10937 for (i=0;i<l_nb_tiles;++i) {
10938 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10939 opj_free(l_current_data);
10943 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10944 if (l_current_tile_size > l_max_tile_size) {
10945 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10946 if (! l_current_data) {
10949 l_max_tile_size = l_current_tile_size;
10952 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10954 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10959 opj_free(l_current_data);
10964 * Ends the compression procedures and possibility add data to be read after the
10967 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
10968 opj_stream_private_t *p_stream,
10969 struct opj_event_mgr * p_manager)
10971 /* customization of the encoding */
10972 j2k_setup_end_compress(p_j2k);
10974 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10984 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10986 * @param p_j2k the jpeg2000 codec.
10987 * @param p_stream the stream object.
10988 * @param p_manager the user event manager.
10990 * @return true if the codec is valid.
10992 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
10993 opj_stream_private_t *p_stream,
10994 opj_image_t * p_image,
10995 opj_event_mgr_t * p_manager)
10997 /* preconditions */
10998 assert(p_j2k != 00);
10999 assert(p_stream != 00);
11000 assert(p_manager != 00);
11002 p_j2k->m_private_image = opj_image_create0();
11003 opj_copy_image_header(p_image, p_j2k->m_private_image);
11005 // TODO_MSD: Find a better way
11006 if (p_image->comps) {
11007 OPJ_UINT32 it_comp;
11008 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11009 if (p_image->comps[it_comp].data) {
11010 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11011 p_image->comps[it_comp].data = NULL;
11017 /* customization of the validation */
11018 j2k_setup_encoding_validation (p_j2k);
11020 /* validation of the parameters codec */
11021 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11025 /* customization of the encoding */
11026 j2k_setup_header_writting(p_j2k);
11029 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11039 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11040 OPJ_UINT32 p_tile_index,
11041 opj_stream_private_t *p_stream,
11042 opj_event_mgr_t * p_manager )
11045 if (p_tile_index != p_j2k->m_current_tile_number) {
11046 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11050 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);
11052 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11053 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11054 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11056 /* initialisation before tile encoding */
11057 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11067 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11069 OPJ_UINT32 i,j,k = 0;
11070 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11071 opj_image_comp_t * l_img_comp = 00;
11072 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11073 opj_image_t * l_image = 00;
11074 OPJ_UINT32 l_size_comp, l_remaining;
11075 OPJ_INT32 * l_src_ptr;
11076 l_tilec = p_tcd->tcd_image->tiles->comps;
11077 l_image = p_tcd->image;
11078 l_img_comp = l_image->comps;
11080 for (i=0;i<p_tcd->image->numcomps;++i) {
11081 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11082 l_remaining = l_img_comp->prec & 7; /* (%8) */
11087 if (l_size_comp == 3) {
11091 l_width = (l_tilec->x1 - l_tilec->x0);
11092 l_height = (l_tilec->y1 - l_tilec->y0);
11093 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11094 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11095 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11096 l_stride = l_image_width - l_width;
11097 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11099 switch (l_size_comp) {
11102 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11103 if (l_img_comp->sgnd) {
11104 for (j=0;j<l_height;++j) {
11105 for (k=0;k<l_width;++k) {
11106 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11110 l_src_ptr += l_stride;
11114 for (j=0;j<l_height;++j) {
11115 for (k=0;k<l_width;++k) {
11116 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11120 l_src_ptr += l_stride;
11124 p_data = (OPJ_BYTE*) l_dest_ptr;
11129 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11130 if (l_img_comp->sgnd) {
11131 for (j=0;j<l_height;++j) {
11132 for (k=0;k<l_width;++k) {
11133 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11135 l_src_ptr += l_stride;
11139 for (j=0;j<l_height;++j) {
11140 for (k=0;k<l_width;++k) {
11141 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11143 l_src_ptr += l_stride;
11147 p_data = (OPJ_BYTE*) l_dest_ptr;
11152 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11153 for (j=0;j<l_height;++j) {
11154 for (k=0;k<l_width;++k) {
11155 *(l_dest_ptr++) = *(l_src_ptr++);
11157 l_src_ptr += l_stride;
11160 p_data = (OPJ_BYTE*) l_dest_ptr;
11173 * @param p_j2k the jpeg2000 codec.
11174 * @param p_stream the stream to write data to.
11175 * @param p_manager the user event manager.
11177 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11179 OPJ_UINT32 p_data_size,
11180 opj_stream_private_t *p_stream,
11181 opj_event_mgr_t * p_manager )
11183 opj_tcd_v2_t * l_tcd = 00;
11184 opj_cp_v2_t * l_cp = 00;
11185 opj_tcp_v2_t * l_tcp = 00;
11186 OPJ_UINT32 l_nb_bytes_written;
11187 OPJ_BYTE * l_current_data = 00;
11188 OPJ_UINT32 l_tile_size = 0;
11189 OPJ_UINT32 l_available_data;
11191 /* preconditions */
11192 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11194 l_tcd = p_j2k->m_tcd;
11195 l_cp = &(p_j2k->m_cp);
11196 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11198 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11199 l_available_data = l_tile_size;
11200 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11202 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11203 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11207 l_nb_bytes_written = 0;
11208 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11211 l_current_data += l_nb_bytes_written;
11212 l_available_data -= l_nb_bytes_written;
11214 l_nb_bytes_written = 0;
11215 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11219 l_available_data -= l_nb_bytes_written;
11220 l_nb_bytes_written = l_tile_size - l_available_data;
11222 if ( opj_stream_write_data( p_stream,
11223 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11224 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11228 ++p_j2k->m_current_tile_number;
11235 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11236 * are valid. Developers wanting to extend the library can add their own validation procedures.
11238 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11240 /* preconditions */
11241 assert(p_j2k != 00);
11243 /* DEVELOPER CORNER, insert your custom procedures */
11244 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11246 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11247 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11250 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11251 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11252 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11256 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11257 * are valid. Developers wanting to extend the library can add their own validation procedures.
11259 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11261 /* preconditions */
11262 assert(p_j2k != 00);
11264 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11265 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11267 /* DEVELOPER CORNER, add your custom validation procedure */
11268 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11273 * Sets up the procedures to do on writing header.
11274 * Developers wanting to extend the library can add their own writing procedures.
11276 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11278 /* preconditions */
11279 assert(p_j2k != 00);
11281 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11282 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11283 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11284 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11285 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11288 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11289 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11290 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11292 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11293 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11297 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11299 if (p_j2k->m_cp.comment != 00) {
11300 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11303 /* DEVELOPER CORNER, insert your custom procedures */
11304 if (p_j2k->m_cp.rsiz & MCT) {
11305 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11307 /* End of Developer Corner */
11309 if (p_j2k->cstr_index) {
11310 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11313 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11314 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11318 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11320 OPJ_UINT32 * p_data_written,
11321 OPJ_UINT32 p_total_data_size,
11322 opj_stream_private_t *p_stream,
11323 struct opj_event_mgr * p_manager )
11326 OPJ_UINT32 l_nb_bytes_written = 0;
11327 OPJ_UINT32 l_current_nb_bytes_written;
11328 OPJ_BYTE * l_begin_data = 00;
11330 opj_tcp_v2_t *l_tcp = 00;
11331 opj_tcd_v2_t * l_tcd = 00;
11332 opj_cp_v2_t * l_cp = 00;
11334 l_tcd = p_j2k->m_tcd;
11335 l_cp = &(p_j2k->m_cp);
11336 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11338 l_tcd->cur_pino = 0;
11340 /*Get number of tile parts*/
11341 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11346 l_current_nb_bytes_written = 0;
11347 l_begin_data = p_data;
11348 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11353 l_nb_bytes_written += l_current_nb_bytes_written;
11354 p_data += l_current_nb_bytes_written;
11355 p_total_data_size -= l_current_nb_bytes_written;
11357 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11358 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11359 l_current_nb_bytes_written = 0;
11360 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11361 l_nb_bytes_written += l_current_nb_bytes_written;
11362 p_data += l_current_nb_bytes_written;
11363 p_total_data_size -= l_current_nb_bytes_written;
11365 l_current_nb_bytes_written = 0;
11366 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11367 l_nb_bytes_written += l_current_nb_bytes_written;
11368 p_data += l_current_nb_bytes_written;
11369 p_total_data_size -= l_current_nb_bytes_written;
11372 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11373 l_current_nb_bytes_written = 0;
11374 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11375 l_nb_bytes_written += l_current_nb_bytes_written;
11376 p_data += l_current_nb_bytes_written;
11377 p_total_data_size -= l_current_nb_bytes_written;
11381 l_current_nb_bytes_written = 0;
11382 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11386 l_nb_bytes_written += l_current_nb_bytes_written;
11387 * p_data_written = l_nb_bytes_written;
11389 /* Writing Psot in SOT marker */
11390 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11392 if (l_cp->m_specific_param.m_enc.m_cinema){
11393 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11399 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11401 OPJ_UINT32 * p_data_written,
11402 OPJ_UINT32 p_total_data_size,
11403 opj_stream_private_t *p_stream,
11404 struct opj_event_mgr * p_manager
11407 OPJ_UINT32 tilepartno=0;
11408 OPJ_UINT32 l_nb_bytes_written = 0;
11409 OPJ_UINT32 l_current_nb_bytes_written;
11410 OPJ_UINT32 l_part_tile_size;
11411 OPJ_UINT32 tot_num_tp;
11414 OPJ_BYTE * l_begin_data;
11415 opj_tcp_v2_t *l_tcp = 00;
11416 opj_tcd_v2_t * l_tcd = 00;
11417 opj_cp_v2_t * l_cp = 00;
11420 l_tcd = p_j2k->m_tcd;
11421 l_cp = &(p_j2k->m_cp);
11422 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11424 /*Get number of tile parts*/
11425 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11427 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11428 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11429 l_current_nb_bytes_written = 0;
11430 l_part_tile_size = 0;
11431 l_begin_data = p_data;
11433 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11437 l_nb_bytes_written += l_current_nb_bytes_written;
11438 p_data += l_current_nb_bytes_written;
11439 p_total_data_size -= l_current_nb_bytes_written;
11440 l_part_tile_size += l_nb_bytes_written;
11442 l_current_nb_bytes_written = 0;
11443 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11447 p_data += l_current_nb_bytes_written;
11448 l_nb_bytes_written += l_current_nb_bytes_written;
11449 p_total_data_size -= l_current_nb_bytes_written;
11450 l_part_tile_size += l_nb_bytes_written;
11452 /* Writing Psot in SOT marker */
11453 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11455 if (l_cp->m_specific_param.m_enc.m_cinema) {
11456 j2k_update_tlm(p_j2k,l_part_tile_size);
11459 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11462 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11463 l_tcd->cur_pino = pino;
11465 /*Get number of tile parts*/
11466 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11467 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11468 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11469 l_current_nb_bytes_written = 0;
11470 l_part_tile_size = 0;
11471 l_begin_data = p_data;
11473 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11477 l_nb_bytes_written += l_current_nb_bytes_written;
11478 p_data += l_current_nb_bytes_written;
11479 p_total_data_size -= l_current_nb_bytes_written;
11480 l_part_tile_size += l_current_nb_bytes_written;
11482 l_current_nb_bytes_written = 0;
11484 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11488 l_nb_bytes_written += l_current_nb_bytes_written;
11489 p_data += l_current_nb_bytes_written;
11490 p_total_data_size -= l_current_nb_bytes_written;
11491 l_part_tile_size += l_current_nb_bytes_written;
11493 /* Writing Psot in SOT marker */
11494 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11496 if (l_cp->m_specific_param.m_enc.m_cinema) {
11497 j2k_update_tlm(p_j2k,l_part_tile_size);
11500 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11504 *p_data_written = l_nb_bytes_written;
11510 * Writes the updated tlm.
11512 * @param p_stream the stream to write data to.
11513 * @param p_j2k J2K codec.
11514 * @param p_manager the user event manager.
11516 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11517 struct opj_stream_private *p_stream,
11518 struct opj_event_mgr * p_manager )
11520 OPJ_UINT32 l_tlm_size;
11521 OPJ_SIZE_T l_tlm_position, l_current_position;
11523 /* preconditions */
11524 assert(p_j2k != 00);
11525 assert(p_manager != 00);
11526 assert(p_stream != 00);
11528 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11529 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11530 l_current_position = opj_stream_tell(p_stream);
11532 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11536 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) {
11540 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11549 * Ends the encoding, i.e. frees memory.
11551 * @param p_stream the stream to write data to.
11552 * @param p_j2k J2K codec.
11553 * @param p_manager the user event manager.
11555 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11556 struct opj_stream_private *p_stream,
11557 struct opj_event_mgr * p_manager )
11559 /* preconditions */
11560 assert(p_j2k != 00);
11561 assert(p_manager != 00);
11562 assert(p_stream != 00);
11564 tcd_destroy_v2(p_j2k->m_tcd);
11567 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11568 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11569 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11570 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11573 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11574 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11575 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11578 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11584 * Destroys the memory associated with the decoding of headers.
11586 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11587 opj_stream_private_t *p_stream,
11588 opj_event_mgr_t * p_manager )
11590 /* preconditions */
11591 assert(p_j2k != 00);
11592 assert(p_stream != 00);
11593 assert(p_manager != 00);
11595 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11596 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11597 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11600 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11609 * @param p_stream the stream to write data to.
11610 * @param p_j2k J2K codec.
11611 * @param p_manager the user event manager.
11613 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11614 struct opj_stream_private *p_stream,
11615 struct opj_event_mgr * p_manager )
11617 opj_codestream_info_t * l_cstr_info = 00;
11619 /* preconditions */
11620 assert(p_j2k != 00);
11621 assert(p_manager != 00);
11622 assert(p_stream != 00);
11625 /* TODO mergeV2: check this part which use cstr_info */
11626 /*l_cstr_info = p_j2k->cstr_info;
11630 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));
11632 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11633 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11635 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11637 l_cstr_info->tw = p_j2k->m_cp.tw;
11638 l_cstr_info->th = p_j2k->m_cp.th;
11640 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11641 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11642 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11643 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11645 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11647 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11649 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11651 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11652 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11655 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11657 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11659 /*l_cstr_info->maxmarknum = 100;
11660 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11661 l_cstr_info->marknum = 0;
11664 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);
11668 * Creates a tile-coder decoder.
11670 * @param p_stream the stream to write data to.
11671 * @param p_j2k J2K codec.
11672 * @param p_manager the user event manager.
11674 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11675 struct opj_stream_private *p_stream,
11676 struct opj_event_mgr * p_manager )
11678 /* preconditions */
11679 assert(p_j2k != 00);
11680 assert(p_manager != 00);
11681 assert(p_stream != 00);
11683 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11685 if (! p_j2k->m_tcd) {
11686 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11690 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11691 tcd_destroy_v2(p_j2k->m_tcd);
11702 * @param p_j2k the jpeg2000 codec.
11703 * @param p_stream the stream to write data to.
11704 * @param p_manager the user event manager.
11706 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11707 OPJ_UINT32 p_tile_index,
11709 OPJ_UINT32 p_data_size,
11710 opj_stream_private_t *p_stream,
11711 opj_event_mgr_t * p_manager )
11713 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11714 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11718 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11719 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);