WIP: begin to test opj_read_tile_header with V2 style
[openjpeg.git] / libopenjpeg / j2k.c
1 /*
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) 2006-2007, Parvatha Elangovan
9  * Copyright (c) 2010-2011, Kaori Hagihara
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "opj_includes.h"
35
36 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
37 /*@{*/
38
39 /** @name Local static functions */
40 /*@{*/
41
42 /**
43  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
44  */
45 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
46
47 /**
48  * The read header procedure.
49  */
50 opj_bool j2k_read_header_procedure(
51                                                             opj_j2k_v2_t *p_j2k,
52                                                                 struct opj_stream_private *p_stream,
53                                                                 struct opj_event_mgr * p_manager);
54
55 /**
56  * The default decoding validation procedure without any extension.
57  *
58  * @param       p_j2k                   the jpeg2000 codec to validate.
59  * @param       p_stream                                the input stream to validate.
60  * @param       p_manager               the user event manager.
61  *
62  * @return true if the parameters are correct.
63  */
64 opj_bool j2k_decoding_validation (
65                                                                 opj_j2k_v2_t * p_j2k,
66                                                                 opj_stream_private_t *p_stream,
67                                                                 opj_event_mgr_t * p_manager
68                                                         );
69
70 /**
71  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
72  * are valid. Developpers wanting to extend the library can add their own validation procedures.
73  */
74 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
75
76 /**
77  * Builds the tcd decoder to use to decode tile.
78  */
79 opj_bool j2k_build_decoder (
80                                                 opj_j2k_v2_t * p_j2k,
81                                                 opj_stream_private_t *p_stream,
82                                                 opj_event_mgr_t * p_manager
83                                                 );
84
85 /**
86  * Excutes the given procedures on the given codec.
87  *
88  * @param       p_procedure_list        the list of procedures to execute
89  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
90  * @param       p_stream                                        the stream to execute the procedures on.
91  * @param       p_manager                       the user manager.
92  *
93  * @return      true                            if all the procedures were successfully executed.
94  */
95 static opj_bool j2k_exec (
96                                         opj_j2k_v2_t * p_j2k,
97                                         opj_procedure_list_t * p_procedure_list,
98                                         opj_stream_private_t *p_stream,
99                                         opj_event_mgr_t * p_manager
100                                   );
101
102 /**
103  * Copies the decoding tile parameters onto all the tile parameters.
104  * Creates also the tile decoder.
105  */
106 opj_bool j2k_copy_default_tcp_and_create_tcd(
107                                                 opj_j2k_v2_t * p_j2k,
108                                                 opj_stream_private_t *p_stream,
109                                                 opj_event_mgr_t * p_manager
110                                                 );
111
112 /**
113  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
114  * with the marker value.
115  * @param       p_id            Marker value to look up
116  *
117  * @return      the handler associated with the id.
118 */
119 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
120
121 /**
122  * Destroys a tile coding parameter structure.
123  *
124  * @param       p_tcp           the tile coding parameter to destroy.
125  */
126 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
127
128 /**
129  * Destroys a coding parameter structure.
130  *
131  * @param       p_cp            the coding parameter to destroy.
132  */
133 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
134
135
136 /**
137  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
138  * @param       p_header_data   the data contained in the COM box.
139  * @param       p_j2k                   the jpeg2000 codec.
140  * @param       p_header_size   the size of the data contained in the COM marker.
141  * @param       p_manager               the user event manager.
142 */
143 static opj_bool j2k_read_SPCod_SPCoc(
144                                                         opj_j2k_v2_t *p_j2k,
145                                                         OPJ_UINT32 compno,
146                                                         OPJ_BYTE * p_header_data,
147                                                         OPJ_UINT32 * p_header_size,
148                                                         struct opj_event_mgr * p_manager
149                                                         );
150
151 /**
152  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
153  *
154  * @param       p_tile_no               the tile to output.
155  * @param       p_comp_no               the component number to output.
156  * @param       p_data                  the data buffer.
157  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
158  * @param       p_j2k                           J2K codec.
159  * @param       p_manager               the user event manager.
160  *
161 */
162 static opj_bool j2k_read_SQcd_SQcc(
163                                                         opj_j2k_v2_t *p_j2k,
164                                                         OPJ_UINT32 compno,
165                                                         OPJ_BYTE * p_header_data,
166                                                         OPJ_UINT32 * p_header_size,
167                                                         struct opj_event_mgr * p_manager
168                                         );
169
170 /**
171  * Copies the tile component parameters of all the component from the first tile component.
172  *
173  * @param               p_j2k           the J2k codec.
174  */
175 static void j2k_copy_tile_component_parameters(
176                                                         opj_j2k_v2_t *p_j2k
177                                                         );
178
179 /**
180  * Copies the tile quantization parameters of all the component from the first tile component.
181  *
182  * @param               p_j2k           the J2k codec.
183  */
184 static void j2k_copy_tile_quantization_parameters(
185                                                         opj_j2k_v2_t *p_j2k
186                                                         );
187
188 /*
189  * -----------------------------------------------------------------------
190  * -----------------------------------------------------------------------
191  * -----------------------------------------------------------------------
192  */
193
194 /**
195 Write the SOC marker (Start Of Codestream)
196 @param j2k J2K handle
197 */
198 static void j2k_write_soc(opj_j2k_t *j2k);
199 /**
200 Read the SOC marker (Start of Codestream)
201 @param j2k J2K handle
202 */
203 static void j2k_read_soc(opj_j2k_t *j2k);
204
205 /**
206  * Reads a SOC marker (Start of Codestream)
207  * @param       p_header_data   the data contained in the SOC box.
208  * @param       jp2                             the jpeg2000 file codec.
209  * @param       p_header_size   the size of the data contained in the SOC marker.
210  * @param       p_manager               the user event manager.
211 */
212 static opj_bool j2k_read_soc_v2(
213                                         opj_j2k_v2_t *p_j2k,
214                                         struct opj_stream_private *p_stream,
215                                         struct opj_event_mgr * p_manager
216                                  );
217
218 /**
219 Write the SIZ marker (image and tile size)
220 @param j2k J2K handle
221 */
222 static void j2k_write_siz(opj_j2k_t *j2k);
223 /**
224 Read the SIZ marker (image and tile size)
225 @param j2k J2K handle
226 */
227 static void j2k_read_siz(opj_j2k_t *j2k);
228
229 /**
230  * Reads a SIZ marker (image and tile size)
231  * @param       p_header_data   the data contained in the SIZ box.
232  * @param       jp2                             the jpeg2000 file codec.
233  * @param       p_header_size   the size of the data contained in the SIZ marker.
234  * @param       p_manager               the user event manager.
235 */
236 static opj_bool j2k_read_siz_v2 (
237                                                   opj_j2k_v2_t *p_j2k,
238                                                   OPJ_BYTE * p_header_data,
239                                                   OPJ_UINT32 p_header_size,
240                                                   struct opj_event_mgr * p_manager
241                                         );
242
243 /**
244 Write the COM marker (comment)
245 @param j2k J2K handle
246 */
247 static void j2k_write_com(opj_j2k_t *j2k);
248 /**
249 Read the COM marker (comment)
250 @param j2k J2K handle
251 */
252 static void j2k_read_com(opj_j2k_t *j2k);
253 /**
254  * Reads a COM marker (comments)
255  * @param       p_header_data   the data contained in the COM box.
256  * @param       jp2                             the jpeg2000 file codec.
257  * @param       p_header_size   the size of the data contained in the COM marker.
258  * @param       p_manager               the user event manager.
259 */
260 static opj_bool j2k_read_com_v2 (
261                                         opj_j2k_v2_t *p_j2k,
262                                         OPJ_BYTE * p_header_data,
263                                         OPJ_UINT32 p_header_size,
264                                         struct opj_event_mgr * p_manager
265                                         );
266 /**
267 Write the value concerning the specified component in the marker COD and COC
268 @param j2k J2K handle
269 @param compno Number of the component concerned by the information written
270 */
271 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
272 /**
273 Read the value concerning the specified component in the marker COD and COC
274 @param j2k J2K handle
275 @param compno Number of the component concerned by the information read
276 */
277 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
278 /**
279 Write the COD marker (coding style default)
280 @param j2k J2K handle
281 */
282 static void j2k_write_cod(opj_j2k_t *j2k);
283 /**
284 Read the COD marker (coding style default)
285 @param j2k J2K handle
286 */
287 static void j2k_read_cod(opj_j2k_t *j2k);
288
289 /**
290  * Reads a COD marker (Coding Styke defaults)
291  * @param       p_header_data   the data contained in the COD box.
292  * @param       p_j2k                   the jpeg2000 codec.
293  * @param       p_header_size   the size of the data contained in the COD marker.
294  * @param       p_manager               the user event manager.
295 */
296 static opj_bool j2k_read_cod_v2 (
297                                         opj_j2k_v2_t *p_j2k,
298                                         OPJ_BYTE * p_header_data,
299                                         OPJ_UINT32 p_header_size,
300                                         struct opj_event_mgr * p_manager
301                                         );
302
303 /**
304 Write the COC marker (coding style component)
305 @param j2k J2K handle
306 @param compno Number of the component concerned by the information written
307 */
308 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
309 /**
310 Read the COC marker (coding style component)
311 @param j2k J2K handle
312 */
313 static void j2k_read_coc(opj_j2k_t *j2k);
314
315 /**
316  * Reads a COC marker (Coding Style Component)
317  * @param       p_header_data   the data contained in the COC box.
318  * @param       p_j2k                   the jpeg2000 codec.
319  * @param       p_header_size   the size of the data contained in the COC marker.
320  * @param       p_manager               the user event manager.
321 */
322 static opj_bool j2k_read_coc_v2 (
323                                         opj_j2k_v2_t *p_j2k,
324                                         OPJ_BYTE * p_header_data,
325                                         OPJ_UINT32 p_header_size,
326                                         struct opj_event_mgr * p_manager
327                                         );
328
329 /**
330 Write the value concerning the specified component in the marker QCD and QCC
331 @param j2k J2K handle
332 @param compno Number of the component concerned by the information written
333 */
334 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
335 /**
336 Read the value concerning the specified component in the marker QCD and QCC
337 @param j2k J2K handle
338 @param compno Number of the component concern by the information read
339 @param len Length of the information in the QCX part of the marker QCD/QCC
340 */
341 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
342 /**
343 Write the QCD marker (quantization default)
344 @param j2k J2K handle
345 */
346 static void j2k_write_qcd(opj_j2k_t *j2k);
347 /**
348 Read the QCD marker (quantization default)
349 @param j2k J2K handle
350 */
351 static void j2k_read_qcd(opj_j2k_t *j2k);
352
353 /**
354  * Reads a QCD marker (Quantization defaults)
355  * @param       p_header_data   the data contained in the QCD box.
356  * @param       p_j2k                   the jpeg2000 codec.
357  * @param       p_header_size   the size of the data contained in the QCD marker.
358  * @param       p_manager               the user event manager.
359 */
360 static opj_bool j2k_read_qcd_v2 (
361                                         opj_j2k_v2_t *p_j2k,
362                                         OPJ_BYTE * p_header_data,
363                                         OPJ_UINT32 p_header_size,
364                                         struct opj_event_mgr * p_manager
365                                         );
366
367 /**
368 Write the QCC marker (quantization component)
369 @param j2k J2K handle
370 @param compno Number of the component concerned by the information written
371 */
372 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
373 /**
374 Read the QCC marker (quantization component)
375 @param j2k J2K handle
376 */
377 static void j2k_read_qcc(opj_j2k_t *j2k);
378 /**
379  * Reads a QCC marker (Quantization component)
380  * @param       p_header_data   the data contained in the QCC box.
381  * @param       p_j2k                   the jpeg2000 codec.
382  * @param       p_header_size   the size of the data contained in the QCC marker.
383  * @param       p_manager               the user event manager.
384 */
385 static opj_bool j2k_read_qcc_v2(
386                                                         opj_j2k_v2_t *p_j2k,
387                                                         OPJ_BYTE * p_header_data,
388                                                         OPJ_UINT32 p_header_size,
389                                                         struct opj_event_mgr * p_manager);
390
391 /**
392 Write the POC marker (progression order change)
393 @param j2k J2K handle
394 */
395 static void j2k_write_poc(opj_j2k_t *j2k);
396 /**
397 Read the POC marker (progression order change)
398 @param j2k J2K handle
399 */
400 static void j2k_read_poc(opj_j2k_t *j2k);
401 /**
402  * Reads a POC marker (Progression Order Change)
403  *
404  * @param       p_header_data   the data contained in the POC box.
405  * @param       p_j2k                   the jpeg2000 codec.
406  * @param       p_header_size   the size of the data contained in the POC marker.
407  * @param       p_manager               the user event manager.
408 */
409 static opj_bool j2k_read_poc_v2 (
410                                                 opj_j2k_v2_t *p_j2k,
411                                                 OPJ_BYTE * p_header_data,
412                                                 OPJ_UINT32 p_header_size,
413                                                 struct opj_event_mgr * p_manager
414                                         );
415 /**
416 Read the CRG marker (component registration)
417 @param j2k J2K handle
418 */
419 static void j2k_read_crg(opj_j2k_t *j2k);
420 /**
421  * Reads a CRG marker (Component registration)
422  *
423  * @param       p_header_data   the data contained in the TLM box.
424  * @param       p_j2k                   the jpeg2000 codec.
425  * @param       p_header_size   the size of the data contained in the TLM marker.
426  * @param       p_manager               the user event manager.
427 */
428 static opj_bool j2k_read_crg_v2 (
429                                                 opj_j2k_v2_t *p_j2k,
430                                                 OPJ_BYTE * p_header_data,
431                                                 OPJ_UINT32 p_header_size,
432                                                 struct opj_event_mgr * p_manager
433                                         );
434 /**
435 Read the TLM marker (tile-part lengths)
436 @param j2k J2K handle
437 */
438 static void j2k_read_tlm(opj_j2k_t *j2k);
439 /**
440  * Reads a TLM marker (Tile Length Marker)
441  *
442  * @param       p_header_data   the data contained in the TLM box.
443  * @param       p_j2k                   the jpeg2000 codec.
444  * @param       p_header_size   the size of the data contained in the TLM marker.
445  * @param       p_manager               the user event manager.
446 */
447 static opj_bool j2k_read_tlm_v2 (
448                                                 opj_j2k_v2_t *p_j2k,
449                                                 OPJ_BYTE * p_header_data,
450                                                 OPJ_UINT32 p_header_size,
451                                                 struct opj_event_mgr * p_manager
452                                         );
453 /**
454 Read the PLM marker (packet length, main header)
455 @param j2k J2K handle
456 */
457 static void j2k_read_plm(opj_j2k_t *j2k);
458
459 /**
460  * Reads a PLM marker (Packet length, main header marker)
461  *
462  * @param       p_header_data   the data contained in the TLM box.
463  * @param       p_j2k                   the jpeg2000 codec.
464  * @param       p_header_size   the size of the data contained in the TLM marker.
465  * @param       p_manager               the user event manager.
466 */
467 static opj_bool j2k_read_plm_v2 (
468                                                 opj_j2k_v2_t *p_j2k,
469                                                 OPJ_BYTE * p_header_data,
470                                                 OPJ_UINT32 p_header_size,
471                                                 struct opj_event_mgr * p_manager
472                                         );
473 /**
474 Read the PLT marker (packet length, tile-part header)
475 @param j2k J2K handle
476 */
477 static void j2k_read_plt(opj_j2k_t *j2k);
478 /**
479  * Reads a PLT marker (Packet length, tile-part header)
480  *
481  * @param       p_header_data   the data contained in the PLT box.
482  * @param       p_j2k                   the jpeg2000 codec.
483  * @param       p_header_size   the size of the data contained in the PLT marker.
484  * @param       p_manager               the user event manager.
485 */
486 static opj_bool j2k_read_plt_v2 (
487                                                 opj_j2k_v2_t *p_j2k,
488                                                 OPJ_BYTE * p_header_data,
489                                                 OPJ_UINT32 p_header_size,
490                                                 struct opj_event_mgr * p_manager
491                                         );
492 /**
493 Read the PPM marker (packet packet headers, main header)
494 @param j2k J2K handle
495 */
496 static void j2k_read_ppm(opj_j2k_t *j2k);
497 /**
498  * Reads a PPM marker (Packed packet headers, main header)
499  *
500  * @param       p_header_data   the data contained in the POC box.
501  * @param       p_j2k                   the jpeg2000 codec.
502  * @param       p_header_size   the size of the data contained in the POC marker.
503  * @param       p_manager               the user event manager.
504 */
505 static opj_bool j2k_read_ppm_v2 (
506                                                 opj_j2k_v2_t *p_j2k,
507                                                 OPJ_BYTE * p_header_data,
508                                                 OPJ_UINT32 p_header_size,
509                                                 struct opj_event_mgr * p_manager
510                                         );
511
512 static opj_bool j2k_read_ppm_v3 (
513                                                 opj_j2k_v2_t *p_j2k,
514                                                 OPJ_BYTE * p_header_data,
515                                                 OPJ_UINT32 p_header_size,
516                                                 struct opj_event_mgr * p_manager
517                                         );
518
519 /**
520 Read the PPT marker (packet packet headers, tile-part header)
521 @param j2k J2K handle
522 */
523 static void j2k_read_ppt(opj_j2k_t *j2k);
524 /**
525  * Reads a PPT marker (Packed packet headers, tile-part header)
526  *
527  * @param       p_header_data   the data contained in the PPT box.
528  * @param       p_j2k                   the jpeg2000 codec.
529  * @param       p_header_size   the size of the data contained in the PPT marker.
530  * @param       p_manager               the user event manager.
531 */
532 static opj_bool j2k_read_ppt_v2 (
533                                                 opj_j2k_v2_t *p_j2k,
534                                                 OPJ_BYTE * p_header_data,
535                                                 OPJ_UINT32 p_header_size,
536                                                 struct opj_event_mgr * p_manager
537                                         );
538 /**
539 Write the TLM marker (Mainheader)
540 @param j2k J2K handle
541 */
542 static void j2k_write_tlm(opj_j2k_t *j2k);
543 /**
544 Write the SOT marker (start of tile-part)
545 @param j2k J2K handle
546 */
547 static void j2k_write_sot(opj_j2k_t *j2k);
548 /**
549 Read the SOT marker (start of tile-part)
550 @param j2k J2K handle
551 */
552 static void j2k_read_sot(opj_j2k_t *j2k);
553
554 /**
555  * Reads a PPT marker (Packed packet headers, tile-part header)
556  *
557  * @param       p_header_data   the data contained in the PPT box.
558  * @param       p_j2k                   the jpeg2000 codec.
559  * @param       p_header_size   the size of the data contained in the PPT marker.
560  * @param       p_manager               the user event manager.
561 */
562 static opj_bool j2k_read_sot_v2 (
563                                                 opj_j2k_v2_t *p_j2k,
564                                                 OPJ_BYTE * p_header_data,
565                                                 OPJ_UINT32 p_header_size,
566                                                 struct opj_event_mgr * p_manager
567                                         );
568 /**
569 Write the SOD marker (start of data)
570 @param j2k J2K handle
571 @param tile_coder Pointer to a TCD handle
572 */
573 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
574 /**
575 Read the SOD marker (start of data)
576 @param j2k J2K handle
577 */
578 static void j2k_read_sod(opj_j2k_t *j2k);
579
580 /**
581  * Reads a SOD marker (Start Of Data)
582  *
583  * @param       p_header_data   the data contained in the SOD box.
584  * @param       p_j2k                   the jpeg2000 codec.
585  * @param       p_header_size   the size of the data contained in the SOD marker.
586  * @param       p_manager               the user event manager.
587 */
588 static opj_bool j2k_read_sod_v2 (
589                                                 opj_j2k_v2_t *p_j2k,
590                                                 struct opj_stream_private *p_stream,
591                                                 struct opj_event_mgr * p_manager
592                                         );
593
594 /**
595 Write the RGN marker (region-of-interest)
596 @param j2k J2K handle
597 @param compno Number of the component concerned by the information written
598 @param tileno Number of the tile concerned by the information written
599 */
600 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
601 /**
602 Read the RGN marker (region-of-interest)
603 @param j2k J2K handle
604 */
605 static void j2k_read_rgn(opj_j2k_t *j2k);
606
607 /**
608  * Reads a RGN marker (Region Of Interest)
609  *
610  * @param       p_header_data   the data contained in the POC box.
611  * @param       p_j2k                   the jpeg2000 codec.
612  * @param       p_header_size   the size of the data contained in the POC marker.
613  * @param       p_manager               the user event manager.
614 */
615 static opj_bool j2k_read_rgn_v2 (
616                                                 opj_j2k_v2_t *p_j2k,
617                                                 OPJ_BYTE * p_header_data,
618                                                 OPJ_UINT32 p_header_size,
619                                                 struct opj_event_mgr * p_manager
620                                         ) ;
621
622 /**
623 Write the EOC marker (end of codestream)
624 @param j2k J2K handle
625 */
626 static void j2k_write_eoc(opj_j2k_t *j2k);
627 /**
628 Read the EOC marker (end of codestream)
629 @param j2k J2K handle
630 */
631 static void j2k_read_eoc(opj_j2k_t *j2k);
632
633 /**
634  * Reads a EOC marker (End Of Codestream)
635  *
636  * @param       p_header_data   the data contained in the SOD box.
637  * @param       p_j2k                   the jpeg2000 codec.
638  * @param       p_header_size   the size of the data contained in the SOD marker.
639  * @param       p_manager               the user event manager.
640 */
641 static opj_bool j2k_read_eoc_v2 (
642                                             opj_j2k_v2_t *p_j2k,
643                                                 struct opj_stream_private *p_stream,
644                                                 struct opj_event_mgr * p_manager
645                                         ) ;
646
647 /**
648 Read an unknown marker
649 @param j2k J2K handle
650 */
651 static void j2k_read_unk(opj_j2k_t *j2k);
652 /**
653 Add main header marker information
654 @param cstr_info Codestream information structure
655 @param type marker type
656 @param pos byte offset of marker segment
657 @param len length of marker segment
658  */
659 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
660 /**
661 Add tile header marker information
662 @param tileno tile index number
663 @param cstr_info Codestream information structure
664 @param type marker type
665 @param pos byte offset of marker segment
666 @param len length of marker segment
667  */
668 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
669
670
671 /**
672  * Reads an unknown marker
673  *
674  * @param       p_stream                                the stream object to read from.
675  * @param       p_j2k                   the jpeg2000 codec.
676  * @param       p_manager               the user event manager.
677  *
678  * @return      true                    if the marker could be deduced.
679 */
680 static opj_bool j2k_read_unk_v2 (       opj_j2k_v2_t *p_j2k,
681                                                                         struct opj_stream_private *p_stream,
682                                                                         struct opj_event_mgr * p_manager );
683
684 /*@}*/
685
686 /*@}*/
687
688 /* ----------------------------------------------------------------------- */
689 typedef struct j2k_prog_order{
690         OPJ_PROG_ORDER enum_prog;
691         char str_prog[5];
692 }j2k_prog_order_t;
693
694 j2k_prog_order_t j2k_prog_order_list[] = {
695         {CPRL, "CPRL"},
696         {LRCP, "LRCP"},
697         {PCRL, "PCRL"},
698         {RLCP, "RLCP"},
699         {RPCL, "RPCL"},
700         {(OPJ_PROG_ORDER)-1, ""}
701 };
702
703 typedef struct opj_dec_memory_marker_handler
704 {
705         /** marker value */
706         OPJ_UINT32 id;
707         /** value of the state when the marker can appear */
708         OPJ_UINT32 states;
709         /** action linked to the marker */
710         opj_bool (*handler) (
711                                         opj_j2k_v2_t *p_j2k,
712                                         OPJ_BYTE * p_header_data,
713                                         OPJ_UINT32 p_header_size,
714                                         struct opj_event_mgr * p_manager
715                                                 );
716 }
717 opj_dec_memory_marker_handler_t;
718
719 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
720 {
721 #ifdef TODO_MS
722   {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
723   {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
724   {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
725   {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
726   {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
727   {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
728   {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
729   {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
730   {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
731   {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
732   {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
733   {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
734   {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
735   {J2K_MS_SOP, 0, 0},
736   {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
737   {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
738   {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
739   {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
740   {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
741   {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
742 #endif
743   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
744   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
745   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
746   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
747   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
748   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
749   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
750   {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
751   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
752   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
753   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
754   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
755   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
756   {J2K_MS_SOP, 0, 0},
757   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
758   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
759   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
760 #ifdef TODO_MS
761   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
762   {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
763   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
764   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
765 #endif
766 #ifdef USE_JPWL
767   {J2K_MS_EPC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epc},
768   {J2K_MS_EPB, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epb},
769   {J2K_MS_ESD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_esd},
770   {J2K_MS_RED, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_red},
771 #endif /* USE_JPWL */
772 #ifdef USE_JPSEC
773   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
774   {J2K_MS_INSEC, 0, j2k_read_insec}
775 #endif /* USE_JPSEC */
776 };
777
778
779
780 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
781         j2k_prog_order_t *po;
782         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
783                 if(po->enum_prog == prg_order){
784                         break;
785                 }
786         }
787         return po->str_prog;
788 }
789
790 /* ----------------------------------------------------------------------- */
791 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
792         char *prog;
793         int i;
794         int tpnum=1,tpend=0;
795         opj_tcp_t *tcp = &cp->tcps[tileno];
796         prog = j2k_convert_progression_order(tcp->prg);
797         
798         if(cp->tp_on == 1){
799                 for(i=0;i<4;i++){
800                         if(tpend!=1){
801                                 if( cp->tp_flag == prog[i] ){
802                                         tpend=1;cp->tp_pos=i;
803                                 }
804                                 switch(prog[i]){
805                                 case 'C':
806                                         tpnum= tpnum * tcp->pocs[pino].compE;
807                                         break;
808                                 case 'R':
809                                         tpnum= tpnum * tcp->pocs[pino].resE;
810                                         break;
811                                 case 'P':
812                                         tpnum= tpnum * tcp->pocs[pino].prcE;
813                                         break;
814                                 case 'L':
815                                         tpnum= tpnum * tcp->pocs[pino].layE;
816                                         break;
817                                 }
818                         }
819                 }
820         }else{
821                 tpnum=1;
822         }
823         return tpnum;
824 }
825
826 /**     mem allocation for TLM marker*/
827 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
828         int pino,tileno,totnum_tp=0;
829
830         OPJ_ARG_NOT_USED(img_numcomp);
831
832         j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
833         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
834                 int cur_totnum_tp = 0;
835                 opj_tcp_t *tcp = &cp->tcps[tileno];
836                 for(pino = 0; pino <= tcp->numpocs; pino++) {
837                         int tp_num=0;
838                         opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
839                         if(!pi) { return -1;}
840                         tp_num = j2k_get_num_tp(cp,pino,tileno);
841                         totnum_tp = totnum_tp + tp_num;
842                         cur_totnum_tp = cur_totnum_tp + tp_num;
843                         pi_destroy(pi, cp, tileno);
844                 }
845                 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
846                 /* INDEX >> */
847                 if (j2k->cstr_info) {
848                         j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
849                         j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
850                 }
851                 /* << INDEX */
852         }
853         return totnum_tp;
854 }
855
856 static void j2k_write_soc(opj_j2k_t *j2k) {
857         opj_cio_t *cio = j2k->cio;
858         cio_write(cio, J2K_MS_SOC, 2);
859
860         if(j2k->cstr_info)
861           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
862
863 /* UniPG>> */
864 #ifdef USE_JPWL
865
866         /* update markers struct */
867         j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
868 #endif /* USE_JPWL */
869 /* <<UniPG */
870 }
871
872 static void j2k_read_soc(opj_j2k_t *j2k) {      
873         j2k->state = J2K_STATE_MHSIZ;
874         /* Index */
875         if (j2k->cstr_info) {
876                 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
877                 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
878         }
879 }
880
881 /**
882  * Reads a SOC marker (Start of Codestream)
883  * @param       p_header_data   the data contained in the SOC box.
884  * @param       jp2                             the jpeg2000 file codec.
885  * @param       p_header_size   the size of the data contained in the SOC marker.
886  * @param       p_manager               the user event manager.
887 */
888 static opj_bool j2k_read_soc_v2(
889                                         opj_j2k_v2_t *p_j2k,
890                                         struct opj_stream_private *p_stream,
891                                         struct opj_event_mgr * p_manager
892                                  )
893 {
894         OPJ_BYTE l_data [2];
895         OPJ_UINT32 l_marker;
896
897         // preconditions
898         assert(p_j2k != 00);
899         assert(p_manager != 00);
900         assert(p_stream != 00);
901
902         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
903                 return OPJ_FALSE;
904         }
905
906         opj_read_bytes(l_data,&l_marker,2);
907         if (l_marker != J2K_MS_SOC) {
908                 return OPJ_FALSE;
909         }
910
911         /* assure length of data is correct (0) */
912         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; // FIXME J2K_DEC_STATE_MHSIZ;
913
914         /* Index */
915         if (p_j2k->cstr_info) {
916                 //TODO p_j2k->cstr_info->main_head_start = opj_stream_tell(p_stream) - 2; // why - 2 ?
917                 p_j2k->cstr_info->codestream_size = 0;/*p_stream_numbytesleft(p_j2k->p_stream) + 2 - p_j2k->cstr_info->main_head_start*/;
918         }
919
920         return OPJ_TRUE;
921 }
922
923 static void j2k_write_siz(opj_j2k_t *j2k) {
924         int i;
925         int lenp, len;
926
927         opj_cio_t *cio = j2k->cio;
928         opj_image_t *image = j2k->image;
929         opj_cp_t *cp = j2k->cp;
930
931         cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
932         lenp = cio_tell(cio);
933         cio_skip(cio, 2);
934         cio_write(cio, cp->rsiz, 2);                    /* Rsiz (capabilities) */
935         cio_write(cio, image->x1, 4);   /* Xsiz */
936         cio_write(cio, image->y1, 4);   /* Ysiz */
937         cio_write(cio, image->x0, 4);   /* X0siz */
938         cio_write(cio, image->y0, 4);   /* Y0siz */
939         cio_write(cio, cp->tdx, 4);             /* XTsiz */
940         cio_write(cio, cp->tdy, 4);             /* YTsiz */
941         cio_write(cio, cp->tx0, 4);             /* XT0siz */
942         cio_write(cio, cp->ty0, 4);             /* YT0siz */
943         cio_write(cio, image->numcomps, 2);     /* Csiz */
944         for (i = 0; i < image->numcomps; i++) {
945                 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);      /* Ssiz_i */
946                 cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
947                 cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
948         }
949         len = cio_tell(cio) - lenp;
950         cio_seek(cio, lenp);
951         cio_write(cio, len, 2);         /* Lsiz */
952         cio_seek(cio, lenp + len);
953         
954         if(j2k->cstr_info)
955           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
956 }
957
958 static void j2k_read_siz(opj_j2k_t *j2k) {
959         int len, i;
960         
961         opj_cio_t *cio = j2k->cio;
962         opj_image_t *image = j2k->image;
963         opj_cp_t *cp = j2k->cp;
964         
965         len = cio_read(cio, 2);                 /* Lsiz */
966         cio_read(cio, 2);                               /* Rsiz (capabilities) */
967         image->x1 = cio_read(cio, 4);   /* Xsiz */
968         image->y1 = cio_read(cio, 4);   /* Ysiz */
969         image->x0 = cio_read(cio, 4);   /* X0siz */
970         image->y0 = cio_read(cio, 4);   /* Y0siz */
971         cp->tdx = cio_read(cio, 4);             /* XTsiz */
972         cp->tdy = cio_read(cio, 4);             /* YTsiz */
973         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
974         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
975         
976         if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
977                 opj_event_msg(j2k->cinfo, EVT_ERROR,
978                                                                         "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
979                                                                         image->x0,image->x1,image->y0,image->y1);
980                 return;
981         }
982         
983         image->numcomps = cio_read(cio, 2);     /* Csiz */
984
985 #ifdef USE_JPWL
986         if (j2k->cp->correct) {
987                 /* if JPWL is on, we check whether TX errors have damaged
988                   too much the SIZ parameters */
989                 if (!(image->x1 * image->y1)) {
990                         opj_event_msg(j2k->cinfo, EVT_ERROR,
991                                 "JPWL: bad image size (%d x %d)\n",
992                                 image->x1, image->y1);
993                         if (!JPWL_ASSUME || JPWL_ASSUME) {
994                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
995                                 return;
996                         }
997                 }
998                 if (image->numcomps != ((len - 38) / 3)) {
999                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1000                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1001                                 image->numcomps, ((len - 38) / 3));
1002                         if (!JPWL_ASSUME) {
1003                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1004                                 return;
1005                         }
1006                         /* we try to correct */
1007                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1008                         if (image->numcomps < ((len - 38) / 3)) {
1009                                 len = 38 + 3 * image->numcomps;
1010                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1011                                         len);                           
1012                         } else {
1013                                 image->numcomps = ((len - 38) / 3);
1014                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1015                                         image->numcomps);                               
1016                         }
1017                 }
1018
1019                 /* update components number in the jpwl_exp_comps filed */
1020                 cp->exp_comps = image->numcomps;
1021         }
1022 #endif /* USE_JPWL */
1023
1024         image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1025         for (i = 0; i < image->numcomps; i++) {
1026                 int tmp, w, h;
1027                 tmp = cio_read(cio, 1);         /* Ssiz_i */
1028                 image->comps[i].prec = (tmp & 0x7f) + 1;
1029                 image->comps[i].sgnd = tmp >> 7;
1030                 image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
1031                 image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
1032                 
1033 #ifdef USE_JPWL
1034                 if (j2k->cp->correct) {
1035                 /* if JPWL is on, we check whether TX errors have damaged
1036                         too much the SIZ parameters, again */
1037                         if (!(image->comps[i].dx * image->comps[i].dy)) {
1038                                 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1039                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1040                                         i, i, image->comps[i].dx, image->comps[i].dy);
1041                                 if (!JPWL_ASSUME) {
1042                                         opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1043                                         return;
1044                                 }
1045                                 /* we try to correct */
1046                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1047                                 if (!image->comps[i].dx) {
1048                                         image->comps[i].dx = 1;
1049                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1050                                                 i, image->comps[i].dx);
1051                                 }
1052                                 if (!image->comps[i].dy) {
1053                                         image->comps[i].dy = 1;
1054                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1055                                                 i, image->comps[i].dy);
1056                                 }
1057                         }
1058                         
1059                 }
1060 #endif /* USE_JPWL */
1061
1062                 /* TODO: unused ? */
1063                 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1064                 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1065
1066                 image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
1067                 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1068         }
1069         
1070         cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1071         cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1072
1073 #ifdef USE_JPWL
1074         if (j2k->cp->correct) {
1075                 /* if JPWL is on, we check whether TX errors have damaged
1076                   too much the SIZ parameters */
1077                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1078                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1079                                 "JPWL: bad number of tiles (%d x %d)\n",
1080                                 cp->tw, cp->th);
1081                         if (!JPWL_ASSUME) {
1082                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1083                                 return;
1084                         }
1085                         /* we try to correct */
1086                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1087                         if (cp->tw < 1) {
1088                                 cp->tw= 1;
1089                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1090                                         cp->tw);
1091                         }
1092                         if (cp->tw > cp->max_tiles) {
1093                                 cp->tw= 1;
1094                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1095                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
1096                                         cp->max_tiles, cp->tw);
1097                         }
1098                         if (cp->th < 1) {
1099                                 cp->th= 1;
1100                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1101                                         cp->th);
1102                         }
1103                         if (cp->th > cp->max_tiles) {
1104                                 cp->th= 1;
1105                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1106                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
1107                                         cp->max_tiles, cp->th);
1108                         }
1109                 }
1110         }
1111 #endif /* USE_JPWL */
1112
1113         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1114         cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1115         cp->tileno_size = 0;
1116         
1117 #ifdef USE_JPWL
1118         if (j2k->cp->correct) {
1119                 if (!cp->tcps) {
1120                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1121                                 "JPWL: could not alloc tcps field of cp\n");
1122                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1123                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1124                                 return;
1125                         }
1126                 }
1127         }
1128 #endif /* USE_JPWL */
1129
1130         for (i = 0; i < cp->tw * cp->th; i++) {
1131                 cp->tcps[i].POC = 0;
1132                 cp->tcps[i].numpocs = 0;
1133                 cp->tcps[i].first = 1;
1134         }
1135         
1136         /* Initialization for PPM marker */
1137         cp->ppm = 0;
1138         cp->ppm_data = NULL;
1139         cp->ppm_data_first = NULL;
1140         cp->ppm_previous = 0;
1141         cp->ppm_store = 0;
1142
1143         j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1144         for (i = 0; i < cp->tw * cp->th; i++) {
1145                 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1146         }       
1147         j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1148         j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1149         j2k->state = J2K_STATE_MH;
1150
1151         /* Index */
1152         if (j2k->cstr_info) {
1153                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1154                 cstr_info->image_w = image->x1 - image->x0;
1155                 cstr_info->image_h = image->y1 - image->y0;
1156                 cstr_info->numcomps = image->numcomps;
1157                 cstr_info->tw = cp->tw;
1158                 cstr_info->th = cp->th;
1159                 cstr_info->tile_x = cp->tdx;    
1160                 cstr_info->tile_y = cp->tdy;    
1161                 cstr_info->tile_Ox = cp->tx0;   
1162                 cstr_info->tile_Oy = cp->ty0;                   
1163                 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
1164         }
1165 }
1166
1167
1168 /**
1169  * Reads a SIZ marker (image and tile size)
1170  * @param       p_header_data   the data contained in the SIZ box.
1171  * @param       jp2                             the jpeg2000 file codec.
1172  * @param       p_header_size   the size of the data contained in the SIZ marker.
1173  * @param       p_manager               the user event manager.
1174 */
1175 opj_bool j2k_read_siz_v2 (
1176                                     opj_j2k_v2_t *p_j2k,
1177                                         OPJ_BYTE * p_header_data,
1178                                         OPJ_UINT32 p_header_size,
1179                                         struct opj_event_mgr * p_manager
1180                                         )
1181 {
1182         OPJ_UINT32 l_size, i;
1183         OPJ_UINT32 l_nb_comp;
1184         OPJ_UINT32 l_nb_comp_remain;
1185         OPJ_UINT32 l_remaining_size;
1186         OPJ_UINT32 l_nb_tiles;
1187         OPJ_UINT32 l_tmp;
1188         opj_image_header_t *l_image = 00;
1189         opj_cp_v2_t *l_cp = 00;
1190         opj_image_comp_header_t * l_img_comp = 00;
1191         opj_tcp_v2_t * l_current_tile_param = 00;
1192
1193         // preconditions
1194         assert(p_j2k != 00);
1195         assert(p_manager != 00);
1196         assert(p_header_data != 00);
1197
1198         l_image = p_j2k->m_image_header;
1199         l_cp = &(p_j2k->m_cp);
1200
1201         // minimum size == 39 - 3 (= minimum component parameter)
1202         if (p_header_size < 36) {
1203                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1204                 return OPJ_FALSE;
1205         }
1206
1207         l_remaining_size = p_header_size - 36;
1208         l_nb_comp = l_remaining_size / 3;
1209         l_nb_comp_remain = l_remaining_size % 3;
1210         if (l_nb_comp_remain != 0){
1211                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1212                 return OPJ_FALSE;
1213         }
1214
1215         l_size = p_header_size + 2;                                                                             /* Lsiz */
1216
1217         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
1218         p_header_data+=2;
1219         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1220         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
1221         p_header_data+=4;
1222         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
1223         p_header_data+=4;
1224         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
1225         p_header_data+=4;
1226         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
1227         p_header_data+=4;
1228         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
1229         p_header_data+=4;
1230         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
1231         p_header_data+=4;
1232         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
1233         p_header_data+=4;
1234         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
1235         p_header_data+=4;
1236         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
1237         p_header_data+=2;
1238         if (l_tmp < 16385)
1239                 l_image->numcomps = (OPJ_UINT16) l_tmp;
1240         else {
1241                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1242                 return OPJ_FALSE;
1243         }
1244
1245         if (l_image->numcomps != l_nb_comp) {
1246                 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);
1247                 return OPJ_FALSE;
1248         }
1249
1250 #ifdef USE_JPWL
1251         if (p_j2k->m_cp->correct) {
1252                 /* if JPWL is on, we check whether TX errors have damaged
1253                   too much the SIZ parameters */
1254                 if (!(image->x1 * image->y1)) {
1255                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
1256                                 "JPWL: bad image size (%d x %d)\n",
1257                                 image->x1, image->y1);
1258                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1259                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1260                                 return;
1261                         }
1262                 }
1263                 if (image->numcomps != ((len - 38) / 3)) {
1264                         opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1265                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1266                                 image->numcomps, ((len - 38) / 3));
1267                         if (!JPWL_ASSUME) {
1268                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1269                                 return;
1270                         }
1271                         /* we try to correct */
1272                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1273                         if (image->numcomps < ((len - 38) / 3)) {
1274                                 len = 38 + 3 * image->numcomps;
1275                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1276                                         len);
1277                         } else {
1278                                 image->numcomps = ((len - 38) / 3);
1279                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1280                                         image->numcomps);
1281                         }
1282                 }
1283
1284                 /* update components number in the jpwl_exp_comps filed */
1285                 cp->exp_comps = image->numcomps;
1286         }
1287 #endif /* USE_JPWL */
1288
1289         // Allocate the resulting image components
1290         l_image->comps = (opj_image_comp_header_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_header_t));
1291         if (l_image->comps == 00){
1292                 l_image->numcomps = 0;
1293                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1294                 return OPJ_FALSE;
1295         }
1296
1297         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_header_t));
1298         l_img_comp = l_image->comps;
1299
1300         // Read the component information
1301         for (i = 0; i < l_image->numcomps; ++i){
1302                 OPJ_UINT32 tmp;
1303                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
1304                 ++p_header_data;
1305                 l_img_comp->prec = (tmp & 0x7f) + 1;
1306                 l_img_comp->sgnd = tmp >> 7;
1307                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
1308                 ++p_header_data;
1309                 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1310                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
1311                 ++p_header_data;
1312                 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1313
1314 #ifdef USE_JPWL
1315                 if (p_j2k->m_cp->correct) {
1316                 /* if JPWL is on, we check whether TX errors have damaged
1317                         too much the SIZ parameters, again */
1318                         if (!(image->comps[i].dx * image->comps[i].dy)) {
1319                                 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1320                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1321                                         i, i, image->comps[i].dx, image->comps[i].dy);
1322                                 if (!JPWL_ASSUME) {
1323                                         opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1324                                         return;
1325                                 }
1326                                 /* we try to correct */
1327                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1328                                 if (!image->comps[i].dx) {
1329                                         image->comps[i].dx = 1;
1330                                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1331                                                 i, image->comps[i].dx);
1332                                 }
1333                                 if (!image->comps[i].dy) {
1334                                         image->comps[i].dy = 1;
1335                                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1336                                                 i, image->comps[i].dy);
1337                                 }
1338                         }
1339                 }
1340 #endif /* USE_JPWL */
1341                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
1342                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1343                 ++l_img_comp;
1344         }
1345
1346         // Compute the number of tiles
1347         l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1348         l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1349         l_nb_tiles = l_cp->tw * l_cp->th;
1350
1351         // Define the tiles which will be decoded
1352         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1353                 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;
1354                 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;
1355                 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);
1356                 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);
1357         }
1358         else {
1359                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1360                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1361                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1362                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1363         }
1364
1365 #ifdef USE_JPWL
1366         if (p_j2k->m_cp->correct) {
1367                 /* if JPWL is on, we check whether TX errors have damaged
1368                   too much the SIZ parameters */
1369                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1370                         opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1371                                 "JPWL: bad number of tiles (%d x %d)\n",
1372                                 cp->tw, cp->th);
1373                         if (!JPWL_ASSUME) {
1374                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1375                                 return;
1376                         }
1377                         /* we try to correct */
1378                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1379                         if (cp->tw < 1) {
1380                                 cp->tw= 1;
1381                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1382                                         cp->tw);
1383                         }
1384                         if (cp->tw > cp->max_tiles) {
1385                                 cp->tw= 1;
1386                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1387                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
1388                                         cp->max_tiles, cp->tw);
1389                         }
1390                         if (cp->th < 1) {
1391                                 cp->th= 1;
1392                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1393                                         cp->th);
1394                         }
1395                         if (cp->th > cp->max_tiles) {
1396                                 cp->th= 1;
1397                                 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1398                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
1399                                         cp->max_tiles, cp->th);
1400                         }
1401                 }
1402         }
1403 #endif /* USE_JPWL */
1404
1405         /* memory allocations */
1406         l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1407         if (l_cp->tcps == 00) {
1408                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1409                 return OPJ_FALSE;
1410         }
1411         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1412
1413 #ifdef USE_JPWL
1414         if (p_j2k->m_cp->correct) {
1415                 if (!cp->tcps) {
1416                         opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1417                                 "JPWL: could not alloc tcps field of cp\n");
1418                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1419                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1420                                 return;
1421                         }
1422                 }
1423         }
1424 #endif /* USE_JPWL */
1425
1426         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1427                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1428         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
1429                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1430                 return OPJ_FALSE;
1431         }
1432         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1433
1434         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1435                         (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1436
1437         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1438                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1439                 return OPJ_FALSE;
1440         }
1441         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));
1442         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1443
1444         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1445                         (opj_simple_mcc_decorrelation_data_t*)
1446                         opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1447
1448         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1449                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1450                 return OPJ_FALSE;
1451         }
1452         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));
1453         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1454
1455         /* set up default dc level shift */
1456         for (i=0;i<l_image->numcomps;++i) {
1457                 if (! l_image->comps[i].sgnd) {
1458                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1459                 }
1460         }
1461
1462         l_current_tile_param = l_cp->tcps;
1463         for     (i = 0; i < l_nb_tiles; ++i) {
1464                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1465                 if (l_current_tile_param->tccps == 00) {
1466                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1467                         return OPJ_FALSE;
1468                 }
1469                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1470
1471                 ++l_current_tile_param;
1472         }
1473
1474         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1475         opj_image_comp_header_update(l_image,l_cp);
1476
1477         /* Index */
1478         if (p_j2k->cstr_info) {
1479                 int it_tile = 0;
1480
1481                 p_j2k->cstr_info->image_w = l_image->x1 - l_image->x0;
1482                 p_j2k->cstr_info->image_h = l_image->y1 - l_image->y0;
1483                 p_j2k->cstr_info->numcomps = l_image->numcomps;
1484                 p_j2k->cstr_info->tw = l_cp->tw;
1485                 p_j2k->cstr_info->th = l_cp->th;
1486                 p_j2k->cstr_info->tdx = l_cp->tdx;
1487                 p_j2k->cstr_info->tdy = l_cp->tdy;
1488                 p_j2k->cstr_info->tx0 = l_cp->tx0;
1489                 p_j2k->cstr_info->ty0 = l_cp->ty0;
1490
1491                 p_j2k->cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tile_info_v2_t));
1492                 if (! p_j2k->cstr_info->tile) {
1493                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory [SIZ marker]\n");
1494                         return OPJ_FALSE;
1495                 }
1496
1497                 for (it_tile = 0; it_tile < l_nb_tiles; it_tile++ ) {
1498                         p_j2k->cstr_info->tile[it_tile].tccp_info =
1499                                         (opj_tccp_info_t*) opj_calloc( l_image->numcomps, sizeof(opj_tccp_info_t));
1500                         if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
1501                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory [SIZ marker]\n");
1502                                 return OPJ_FALSE;
1503                         }
1504                 }
1505         }
1506
1507         return OPJ_TRUE;
1508 }
1509
1510
1511
1512 static void j2k_write_com(opj_j2k_t *j2k) {
1513         unsigned int i;
1514         int lenp, len;
1515
1516         if(j2k->cp->comment) {
1517                 opj_cio_t *cio = j2k->cio;
1518                 char *comment = j2k->cp->comment;
1519
1520                 cio_write(cio, J2K_MS_COM, 2);
1521                 lenp = cio_tell(cio);
1522                 cio_skip(cio, 2);
1523                 cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
1524                 for (i = 0; i < strlen(comment); i++) {
1525                         cio_write(cio, comment[i], 1);
1526                 }
1527                 len = cio_tell(cio) - lenp;
1528                 cio_seek(cio, lenp);
1529                 cio_write(cio, len, 2);
1530                 cio_seek(cio, lenp + len);
1531
1532                 
1533                 if(j2k->cstr_info)
1534                   j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1535
1536         }
1537 }
1538
1539 static void j2k_read_com(opj_j2k_t *j2k) {
1540         int len;
1541         
1542         opj_cio_t *cio = j2k->cio;
1543
1544         len = cio_read(cio, 2);
1545         cio_skip(cio, len - 2);  
1546 }
1547 /**
1548  * Reads a COM marker (comments)
1549  * @param       p_header_data   the data contained in the COM box.
1550  * @param       jp2                             the jpeg2000 file codec.
1551  * @param       p_header_size   the size of the data contained in the COM marker.
1552  * @param       p_manager               the user event manager.
1553 */
1554 opj_bool j2k_read_com_v2 (
1555                                         opj_j2k_v2_t *p_j2k,
1556                                         OPJ_BYTE * p_header_data,
1557                                         OPJ_UINT32 p_header_size,
1558                                         struct opj_event_mgr * p_manager
1559                                         )
1560 {
1561         // preconditions
1562         assert(p_j2k != 00);
1563         assert(p_manager != 00);
1564         assert(p_header_data != 00);
1565
1566         return OPJ_TRUE;
1567 }
1568
1569 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1570         int i;
1571
1572         opj_cp_t *cp = j2k->cp;
1573         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1574         opj_tccp_t *tccp = &tcp->tccps[compno];
1575         opj_cio_t *cio = j2k->cio;
1576         
1577         cio_write(cio, tccp->numresolutions - 1, 1);    /* SPcox (D) */
1578         cio_write(cio, tccp->cblkw - 2, 1);                             /* SPcox (E) */
1579         cio_write(cio, tccp->cblkh - 2, 1);                             /* SPcox (F) */
1580         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (G) */
1581         cio_write(cio, tccp->qmfbid, 1);                                /* SPcox (H) */
1582         
1583         if (tccp->csty & J2K_CCP_CSTY_PRT) {
1584                 for (i = 0; i < tccp->numresolutions; i++) {
1585                         cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);        /* SPcox (I_i) */
1586                 }
1587         }
1588 }
1589
1590 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1591         int i;
1592
1593         opj_cp_t *cp = j2k->cp;
1594         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1595         opj_tccp_t *tccp = &tcp->tccps[compno];
1596         opj_cio_t *cio = j2k->cio;
1597
1598         tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
1599
1600         // If user wants to remove more resolutions than the codestream contains, return error
1601         if (cp->reduce >= tccp->numresolutions) {
1602                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1603                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1604                 j2k->state |= J2K_STATE_ERR;
1605         }
1606
1607         tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
1608         tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
1609         tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
1610         tccp->qmfbid = cio_read(cio, 1);        /* SPcox (H) */
1611         if (tccp->csty & J2K_CP_CSTY_PRT) {
1612                 for (i = 0; i < tccp->numresolutions; i++) {
1613                         int tmp = cio_read(cio, 1);     /* SPcox (I_i) */
1614                         tccp->prcw[i] = tmp & 0xf;
1615                         tccp->prch[i] = tmp >> 4;
1616                 }
1617         }
1618
1619         /* INDEX >> */
1620         if(j2k->cstr_info && compno == 0) {
1621                 for (i = 0; i < tccp->numresolutions; i++) {
1622                         if (tccp->csty & J2K_CP_CSTY_PRT) {
1623                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1624                                 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1625                         }
1626                         else {
1627                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1628                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1629                         }
1630                 }
1631         }
1632         /* << INDEX */
1633 }
1634
1635 static void j2k_write_cod(opj_j2k_t *j2k) {
1636         opj_cp_t *cp = NULL;
1637         opj_tcp_t *tcp = NULL;
1638         int lenp, len;
1639
1640         opj_cio_t *cio = j2k->cio;
1641         
1642         cio_write(cio, J2K_MS_COD, 2);  /* COD */
1643         
1644         lenp = cio_tell(cio);
1645         cio_skip(cio, 2);
1646         
1647         cp = j2k->cp;
1648         tcp = &cp->tcps[j2k->curtileno];
1649
1650         cio_write(cio, tcp->csty, 1);           /* Scod */
1651         cio_write(cio, tcp->prg, 1);            /* SGcod (A) */
1652         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) */
1653         cio_write(cio, tcp->mct, 1);            /* SGcod (C) */
1654         
1655         j2k_write_cox(j2k, 0);
1656         len = cio_tell(cio) - lenp;
1657         cio_seek(cio, lenp);
1658         cio_write(cio, len, 2);         /* Lcod */
1659         cio_seek(cio, lenp + len);
1660
1661         if(j2k->cstr_info)
1662           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1663
1664 }
1665
1666 static void j2k_read_cod(opj_j2k_t *j2k) {
1667         int len, i, pos;
1668         
1669         opj_cio_t *cio = j2k->cio;
1670         opj_cp_t *cp = j2k->cp;
1671         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1672         opj_image_t *image = j2k->image;
1673         
1674         len = cio_read(cio, 2);                         /* Lcod */
1675         tcp->csty = cio_read(cio, 1);           /* Scod */
1676         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);            /* SGcod (A) */
1677         tcp->numlayers = cio_read(cio, 2);      /* SGcod (B) */
1678         tcp->mct = cio_read(cio, 1);            /* SGcod (C) */
1679         
1680         pos = cio_tell(cio);
1681         for (i = 0; i < image->numcomps; i++) {
1682                 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1683                 cio_seek(cio, pos);
1684                 j2k_read_cox(j2k, i);
1685         }
1686
1687         /* Index */
1688         if (j2k->cstr_info) {
1689                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1690                 cstr_info->prog = tcp->prg;
1691                 cstr_info->numlayers = tcp->numlayers;
1692                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1693                 for (i = 0; i < image->numcomps; i++) {
1694                         cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
1695                 }
1696         }
1697 }
1698
1699 /**
1700  * Reads a COD marker (Coding Styke defaults)
1701  * @param       p_header_data   the data contained in the COD box.
1702  * @param       p_j2k                   the jpeg2000 codec.
1703  * @param       p_header_size   the size of the data contained in the COD marker.
1704  * @param       p_manager               the user event manager.
1705 */
1706 opj_bool j2k_read_cod_v2 (
1707                                         opj_j2k_v2_t *p_j2k,
1708                                         OPJ_BYTE * p_header_data,
1709                                         OPJ_UINT32 p_header_size,
1710                                         struct opj_event_mgr * p_manager
1711                                         )
1712 {
1713         // loop
1714         OPJ_UINT32 i;
1715         OPJ_UINT32 l_tmp;
1716         opj_cp_v2_t *l_cp = 00;
1717         opj_tcp_v2_t *l_tcp = 00;
1718         opj_image_header_t *l_image = 00;
1719
1720         /* preconditions */
1721         assert(p_header_data != 00);
1722         assert(p_j2k != 00);
1723         assert(p_manager != 00);
1724
1725         l_image = p_j2k->m_image_header;
1726         l_cp = &(p_j2k->m_cp);
1727
1728         /* If we are in the first tile-part header of the current tile */
1729         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
1730                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
1731                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1732
1733         /* Make sure room is sufficient */
1734         if (p_header_size < 5) {
1735                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1736                 return OPJ_FALSE;
1737         }
1738
1739         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
1740         ++p_header_data;
1741         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
1742         ++p_header_data;
1743         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
1744         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
1745         p_header_data+=2;
1746
1747         // If user didn't set a number layer to decode take the max specify in the codestream.
1748         if      (l_cp->m_specific_param.m_dec.m_layer) {
1749                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
1750         }
1751         else {
1752                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
1753         }
1754
1755         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
1756         ++p_header_data;
1757
1758         p_header_size -= 5;
1759         for     (i = 0; i < l_image->numcomps; ++i) {
1760                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
1761         }
1762
1763         if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
1764                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1765                 return OPJ_FALSE;
1766         }
1767
1768         if (p_header_size != 0) {
1769                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1770                 return OPJ_FALSE;
1771         }
1772
1773         j2k_copy_tile_component_parameters(p_j2k);
1774
1775         /* Index */
1776         if (p_j2k->cstr_info) {
1777                 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
1778                 p_j2k->cstr_info->prog = l_tcp->prg;
1779                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
1780                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
1781                 for     (i = 0; i < l_image->numcomps; ++i) {
1782                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
1783                 }
1784         }
1785         return OPJ_TRUE;
1786 }
1787
1788 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
1789         int lenp, len;
1790
1791         opj_cp_t *cp = j2k->cp;
1792         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1793         opj_image_t *image = j2k->image;
1794         opj_cio_t *cio = j2k->cio;
1795         
1796         cio_write(cio, J2K_MS_COC, 2);  /* COC */
1797         lenp = cio_tell(cio);
1798         cio_skip(cio, 2);
1799         cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1800         cio_write(cio, tcp->tccps[compno].csty, 1);     /* Scoc */
1801         j2k_write_cox(j2k, compno);
1802         len = cio_tell(cio) - lenp;
1803         cio_seek(cio, lenp);
1804         cio_write(cio, len, 2);                 /* Lcoc */
1805         cio_seek(cio, lenp + len);
1806 }
1807
1808 static void j2k_read_coc(opj_j2k_t *j2k) {
1809         int len, compno;
1810
1811         opj_cp_t *cp = j2k->cp;
1812         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1813         opj_image_t *image = j2k->image;
1814         opj_cio_t *cio = j2k->cio;
1815         
1816         len = cio_read(cio, 2);         /* Lcoc */
1817         compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1818         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
1819         j2k_read_cox(j2k, compno);
1820 }
1821
1822 /**
1823  * Reads a COC marker (Coding Style Component)
1824  * @param       p_header_data   the data contained in the COC box.
1825  * @param       p_j2k                   the jpeg2000 codec.
1826  * @param       p_header_size   the size of the data contained in the COC marker.
1827  * @param       p_manager               the user event manager.
1828 */
1829 opj_bool j2k_read_coc_v2 (
1830                                         opj_j2k_v2_t *p_j2k,
1831                                         OPJ_BYTE * p_header_data,
1832                                         OPJ_UINT32 p_header_size,
1833                                         struct opj_event_mgr * p_manager
1834                                         )
1835 {
1836         opj_cp_v2_t *l_cp = NULL;
1837         opj_tcp_v2_t *l_tcp = NULL;
1838         opj_image_header_t *l_image = NULL;
1839         OPJ_UINT32 l_comp_room;
1840         OPJ_UINT32 l_comp_no;
1841
1842         // preconditions
1843         assert(p_header_data != 00);
1844         assert(p_j2k != 00);
1845         assert(p_manager != 00);
1846
1847         l_cp = &(p_j2k->m_cp);
1848         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
1849                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
1850                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1851         l_image = p_j2k->m_image_header;
1852
1853         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
1854
1855         // make sure room is sufficient
1856         if (p_header_size < l_comp_room + 1) {
1857                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1858                 return OPJ_FALSE;
1859         }
1860         p_header_size -= l_comp_room + 1;
1861
1862         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
1863         p_header_data += l_comp_room;
1864         if (l_comp_no >= l_image->numcomps) {
1865                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
1866                 return OPJ_FALSE;
1867         }
1868
1869         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
1870         ++p_header_data ;
1871
1872         if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
1873                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1874                 return OPJ_FALSE;
1875         }
1876
1877         if (p_header_size != 0) {
1878                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1879                 return OPJ_FALSE;
1880         }
1881         return OPJ_TRUE;
1882 }
1883
1884 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
1885         int bandno, numbands;
1886         int expn, mant;
1887         
1888         opj_cp_t *cp = j2k->cp;
1889         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1890         opj_tccp_t *tccp = &tcp->tccps[compno];
1891         opj_cio_t *cio = j2k->cio;
1892         
1893         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx */
1894         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
1895         
1896         for (bandno = 0; bandno < numbands; bandno++) {
1897                 expn = tccp->stepsizes[bandno].expn;
1898                 mant = tccp->stepsizes[bandno].mant;
1899                 
1900                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1901                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
1902                 } else {
1903                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1904                 }
1905         }
1906 }
1907
1908 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
1909         int tmp;
1910         int bandno, numbands;
1911
1912         opj_cp_t *cp = j2k->cp;
1913         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1914         opj_tccp_t *tccp = &tcp->tccps[compno];
1915         opj_cio_t *cio = j2k->cio;
1916
1917         tmp = cio_read(cio, 1);         /* Sqcx */
1918         tccp->qntsty = tmp & 0x1f;
1919         tccp->numgbits = tmp >> 5;
1920         numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
1921                 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1922
1923 #ifdef USE_JPWL
1924         if (j2k->cp->correct) {
1925
1926                 /* if JPWL is on, we check whether there are too many subbands */
1927                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1928                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1929                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
1930                                 numbands);
1931                         if (!JPWL_ASSUME) {
1932                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1933                                 return;
1934                         }
1935                         /* we try to correct */
1936                         numbands = 1;
1937                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
1938                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
1939                                 numbands);
1940                 };
1941
1942         };
1943
1944 #else
1945         /* We check whether there are too many subbands */
1946         if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1947                 opj_event_msg(j2k->cinfo, EVT_WARNING ,
1948                                         "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
1949                                     "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
1950         }
1951
1952 #endif /* USE_JPWL */
1953
1954         for (bandno = 0; bandno < numbands; bandno++) {
1955                 int expn, mant;
1956                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1957                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
1958                         mant = 0;
1959                 } else {
1960                         tmp = cio_read(cio, 2); /* SPqcx_i */
1961                         expn = tmp >> 11;
1962                         mant = tmp & 0x7ff;
1963                 }
1964                 if (bandno < J2K_MAXBANDS){
1965                         tccp->stepsizes[bandno].expn = expn;
1966                         tccp->stepsizes[bandno].mant = mant;
1967                 }
1968         }
1969         
1970         /* Add Antonin : if scalar_derived -> compute other stepsizes */
1971         if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
1972                 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
1973                         tccp->stepsizes[bandno].expn = 
1974                                 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
1975                                         (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
1976                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1977                 }
1978         }
1979         /* ddA */
1980 }
1981
1982 static void j2k_write_qcd(opj_j2k_t *j2k) {
1983         int lenp, len;
1984
1985         opj_cio_t *cio = j2k->cio;
1986         
1987         cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
1988         lenp = cio_tell(cio);
1989         cio_skip(cio, 2);
1990         j2k_write_qcx(j2k, 0);
1991         len = cio_tell(cio) - lenp;
1992         cio_seek(cio, lenp);
1993         cio_write(cio, len, 2);                 /* Lqcd */
1994         cio_seek(cio, lenp + len);
1995
1996         if(j2k->cstr_info)
1997           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
1998 }
1999
2000 static void j2k_read_qcd(opj_j2k_t *j2k) {
2001         int len, i, pos;
2002
2003         opj_cio_t *cio = j2k->cio;
2004         opj_image_t *image = j2k->image;
2005         
2006         len = cio_read(cio, 2);         /* Lqcd */
2007         pos = cio_tell(cio);
2008         for (i = 0; i < image->numcomps; i++) {
2009                 cio_seek(cio, pos);
2010                 j2k_read_qcx(j2k, i, len - 2);
2011         }
2012 }
2013
2014 /**
2015  * Reads a QCD marker (Quantization defaults)
2016  * @param       p_header_data   the data contained in the QCD box.
2017  * @param       p_j2k                   the jpeg2000 codec.
2018  * @param       p_header_size   the size of the data contained in the QCD marker.
2019  * @param       p_manager               the user event manager.
2020 */
2021 opj_bool j2k_read_qcd_v2 (
2022                                     opj_j2k_v2_t *p_j2k,
2023                                         OPJ_BYTE * p_header_data,
2024                                         OPJ_UINT32 p_header_size,
2025                                         struct opj_event_mgr * p_manager
2026                                         )
2027 {
2028         // preconditions
2029         assert(p_header_data != 00);
2030         assert(p_j2k != 00);
2031         assert(p_manager != 00);
2032
2033         if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2034                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2035                 return OPJ_FALSE;
2036         }
2037         if (p_header_size != 0) {
2038                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2039                 return OPJ_FALSE;
2040         }
2041         j2k_copy_tile_quantization_parameters(p_j2k);
2042
2043         return OPJ_TRUE;
2044 }
2045
2046 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2047         int lenp, len;
2048
2049         opj_cio_t *cio = j2k->cio;
2050         
2051         cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
2052         lenp = cio_tell(cio);
2053         cio_skip(cio, 2);
2054         cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);    /* Cqcc */
2055         j2k_write_qcx(j2k, compno);
2056         len = cio_tell(cio) - lenp;
2057         cio_seek(cio, lenp);
2058         cio_write(cio, len, 2);                 /* Lqcc */
2059         cio_seek(cio, lenp + len);
2060 }
2061
2062 static void j2k_read_qcc(opj_j2k_t *j2k) {
2063         int len, compno;
2064         int numcomp = j2k->image->numcomps;
2065         opj_cio_t *cio = j2k->cio;
2066
2067         len = cio_read(cio, 2); /* Lqcc */
2068         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2069
2070 #ifdef USE_JPWL
2071         if (j2k->cp->correct) {
2072
2073                 static int backup_compno = 0;
2074
2075                 /* compno is negative or larger than the number of components!!! */
2076                 if ((compno < 0) || (compno >= numcomp)) {
2077                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2078                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2079                                 compno, numcomp);
2080                         if (!JPWL_ASSUME) {
2081                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2082                                 return;
2083                         }
2084                         /* we try to correct */
2085                         compno = backup_compno % numcomp;
2086                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2087                                 "- setting component number to %d\n",
2088                                 compno);
2089                 }
2090
2091                 /* keep your private count of tiles */
2092                 backup_compno++;
2093         };
2094 #endif /* USE_JPWL */
2095
2096         j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2097 }
2098
2099 /**
2100  * Reads a QCC marker (Quantization component)
2101  * @param       p_header_data   the data contained in the QCC box.
2102  * @param       p_j2k                   the jpeg2000 codec.
2103  * @param       p_header_size   the size of the data contained in the QCC marker.
2104  * @param       p_manager               the user event manager.
2105 */
2106 opj_bool j2k_read_qcc_v2(
2107                                                         opj_j2k_v2_t *p_j2k,
2108                                                         OPJ_BYTE * p_header_data,
2109                                                         OPJ_UINT32 p_header_size,
2110                                                         struct opj_event_mgr * p_manager)
2111 {
2112         OPJ_UINT32 l_num_comp,l_comp_no;
2113
2114         // preconditions
2115         assert(p_header_data != 00);
2116         assert(p_j2k != 00);
2117         assert(p_manager != 00);
2118
2119         l_num_comp = p_j2k->m_image_header->numcomps;
2120
2121 #ifdef USE_JPWL
2122         if (p_j2k->m_cp->correct) {
2123
2124                 static OPJ_UINT32 backup_compno = 0;
2125
2126                 /* compno is negative or larger than the number of components!!! */
2127                 if ((compno < 0) || (compno >= numcomp)) {
2128                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
2129                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2130                                 compno, numcomp);
2131                         if (!JPWL_ASSUME) {
2132                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2133                                 return;
2134                         }
2135                         /* we try to correct */
2136                         compno = backup_compno % numcomp;
2137                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2138                                 "- setting component number to %d\n",
2139                                 compno);
2140                 }
2141
2142                 /* keep your private count of tiles */
2143                 backup_compno++;
2144         };
2145 #endif /* USE_JPWL */
2146
2147         if (l_num_comp <= 256) {
2148                 if (p_header_size < 1) {
2149                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2150                         return OPJ_FALSE;
2151                 }
2152                 opj_read_bytes(p_header_data,&l_comp_no,1);
2153                 ++p_header_data;
2154                 --p_header_size;
2155         }
2156         else {
2157                 if (p_header_size < 2) {
2158                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2159                         return OPJ_FALSE;
2160                 }
2161                 opj_read_bytes(p_header_data,&l_comp_no,2);
2162                 p_header_data+=2;
2163                 p_header_size-=2;
2164         }
2165
2166         if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2167                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2168                 return OPJ_FALSE;
2169         }
2170
2171         if (p_header_size != 0) {
2172                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2173                 return OPJ_FALSE;
2174         }
2175
2176         return OPJ_TRUE;
2177 }
2178
2179
2180 static void j2k_write_poc(opj_j2k_t *j2k) {
2181         int len, numpchgs, i;
2182
2183         int numcomps = j2k->image->numcomps;
2184         
2185         opj_cp_t *cp = j2k->cp;
2186         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2187         opj_tccp_t *tccp = &tcp->tccps[0];
2188         opj_cio_t *cio = j2k->cio;
2189
2190         numpchgs = 1 + tcp->numpocs;
2191         cio_write(cio, J2K_MS_POC, 2);  /* POC  */
2192         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2193         cio_write(cio, len, 2);         /* Lpoc */
2194         for (i = 0; i < numpchgs; i++) {
2195                 opj_poc_t *poc = &tcp->pocs[i];
2196                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2197                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
2198                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2199                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2200                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2201                 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2202                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
2203                 poc->compno1 = int_min(poc->compno1, numcomps);
2204                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
2205         }
2206 }
2207
2208 static void j2k_read_poc(opj_j2k_t *j2k) {
2209         int len, numpchgs, i, old_poc;
2210
2211         int numcomps = j2k->image->numcomps;
2212         
2213         opj_cp_t *cp = j2k->cp;
2214         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2215         opj_cio_t *cio = j2k->cio;
2216         
2217         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2218         tcp->POC = 1;
2219         len = cio_read(cio, 2);         /* Lpoc */
2220         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2221         
2222         for (i = old_poc; i < numpchgs + old_poc; i++) {
2223                 opj_poc_t *poc;
2224                 poc = &tcp->pocs[i];
2225                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2226                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
2227                 poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
2228                 poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
2229                 poc->compno1 = int_min(
2230                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
2231                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
2232         }
2233         
2234         tcp->numpocs = numpchgs + old_poc - 1;
2235 }
2236
2237 /**
2238  * Reads a POC marker (Progression Order Change)
2239  *
2240  * @param       p_header_data   the data contained in the POC box.
2241  * @param       p_j2k                   the jpeg2000 codec.
2242  * @param       p_header_size   the size of the data contained in the POC marker.
2243  * @param       p_manager               the user event manager.
2244 */
2245 opj_bool j2k_read_poc_v2 (
2246                                                 opj_j2k_v2_t *p_j2k,
2247                                                 OPJ_BYTE * p_header_data,
2248                                                 OPJ_UINT32 p_header_size,
2249                                                 struct opj_event_mgr * p_manager)
2250 {
2251         OPJ_UINT32 i, l_nb_comp, l_tmp;
2252         opj_image_header_t * l_image = 00;
2253         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2254         OPJ_UINT32 l_chunk_size, l_comp_room;
2255
2256         opj_cp_v2_t *l_cp = 00;
2257         opj_tcp_v2_t *l_tcp = 00;
2258         opj_poc_t *l_current_poc = 00;
2259
2260         // preconditions
2261         assert(p_header_data != 00);
2262         assert(p_j2k != 00);
2263         assert(p_manager != 00);
2264
2265         l_image = p_j2k->m_image_header;
2266         l_nb_comp = l_image->numcomps;
2267         if (l_nb_comp <= 256) {
2268                 l_comp_room = 1;
2269         }
2270         else {
2271                 l_comp_room = 2;
2272         }
2273         l_chunk_size = 5 + 2 * l_comp_room;
2274         l_current_poc_nb = p_header_size / l_chunk_size;
2275         l_current_poc_remaining = p_header_size % l_chunk_size;
2276
2277         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2278                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2279                 return OPJ_FALSE;
2280         }
2281
2282         l_cp = &(p_j2k->m_cp);
2283         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
2284                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2285                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2286         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2287         l_current_poc_nb += l_old_poc_nb;
2288
2289         assert(l_current_poc_nb < 32);
2290
2291         /* now poc is in use.*/
2292         l_tcp->POC = 1;
2293
2294         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2295         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2296                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
2297                 ++p_header_data;
2298                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
2299                 p_header_data+=l_comp_room;
2300                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
2301                 p_header_data+=2;
2302                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
2303                 ++p_header_data;
2304                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
2305                 p_header_data+=l_comp_room;
2306                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
2307                 ++p_header_data;
2308                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2309                 /* make sure comp is in acceptable bounds */
2310                 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2311                 ++l_current_poc;
2312         }
2313
2314         l_tcp->numpocs = l_current_poc_nb - 1;
2315         return OPJ_TRUE;
2316 }
2317
2318 static void j2k_read_crg(opj_j2k_t *j2k) {
2319         int len, i, Xcrg_i, Ycrg_i;
2320         
2321         opj_cio_t *cio = j2k->cio;
2322         int numcomps = j2k->image->numcomps;
2323         
2324         len = cio_read(cio, 2);                 /* Lcrg */
2325         for (i = 0; i < numcomps; i++) {
2326                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
2327                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
2328         }
2329 }
2330
2331 /**
2332  * Reads a CRG marker (Component registration)
2333  *
2334  * @param       p_header_data   the data contained in the TLM box.
2335  * @param       p_j2k                   the jpeg2000 codec.
2336  * @param       p_header_size   the size of the data contained in the TLM marker.
2337  * @param       p_manager               the user event manager.
2338 */
2339 opj_bool j2k_read_crg_v2 (
2340                                                 opj_j2k_v2_t *p_j2k,
2341                                                 OPJ_BYTE * p_header_data,
2342                                                 OPJ_UINT32 p_header_size,
2343                                                 struct opj_event_mgr * p_manager
2344                                         )
2345 {
2346         OPJ_UINT32 l_nb_comp;
2347         // preconditions
2348         assert(p_header_data != 00);
2349         assert(p_j2k != 00);
2350         assert(p_manager != 00);
2351
2352         l_nb_comp = p_j2k->m_image_header->numcomps;
2353
2354         if (p_header_size != l_nb_comp *4) {
2355                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2356                 return OPJ_FALSE;
2357         }
2358         /* Do not care of this at the moment since only local variables are set here */
2359         /*
2360         for
2361                 (i = 0; i < l_nb_comp; ++i)
2362         {
2363                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
2364                 p_header_data+=2;
2365                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
2366                 p_header_data+=2;
2367         }
2368         */
2369         return OPJ_TRUE;
2370 }
2371
2372 static void j2k_read_tlm(opj_j2k_t *j2k) {
2373         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2374         long int Ttlm_i, Ptlm_i;
2375
2376         opj_cio_t *cio = j2k->cio;
2377         
2378         len = cio_read(cio, 2);         /* Ltlm */
2379         Ztlm = cio_read(cio, 1);        /* Ztlm */
2380         Stlm = cio_read(cio, 1);        /* Stlm */
2381         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2382         SP = (Stlm >> 6) & 0x01;
2383         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2384         for (i = 0; i < tile_tlm; i++) {
2385                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
2386                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
2387         }
2388 }
2389
2390 /**
2391  * Reads a TLM marker (Tile Length Marker)
2392  *
2393  * @param       p_header_data   the data contained in the TLM box.
2394  * @param       p_j2k                   the jpeg2000 codec.
2395  * @param       p_header_size   the size of the data contained in the TLM marker.
2396  * @param       p_manager               the user event manager.
2397 */
2398 opj_bool j2k_read_tlm_v2 (
2399                                                 opj_j2k_v2_t *p_j2k,
2400                                                 OPJ_BYTE * p_header_data,
2401                                                 OPJ_UINT32 p_header_size,
2402                                                 struct opj_event_mgr * p_manager
2403                                         )
2404 {
2405         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2406         // preconditions
2407         assert(p_header_data != 00);
2408         assert(p_j2k != 00);
2409         assert(p_manager != 00);
2410
2411         if (p_header_size < 2) {
2412                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2413                 return OPJ_FALSE;
2414         }
2415         p_header_size -= 2;
2416
2417         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
2418         ++p_header_data;
2419         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
2420         ++p_header_data;
2421
2422         l_ST = ((l_Stlm >> 4) & 0x3);
2423         l_SP = (l_Stlm >> 6) & 0x1;
2424
2425         l_Ptlm_size = (l_SP + 1) * 2;
2426         l_quotient = l_Ptlm_size + l_ST;
2427
2428         l_tot_num_tp = p_header_size / l_quotient;
2429         l_tot_num_tp_remaining = p_header_size % l_quotient;
2430
2431         if (l_tot_num_tp_remaining != 0) {
2432                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2433                 return OPJ_FALSE;
2434         }
2435         /* FIXME Do not care of this at the moment since only local variables are set here */
2436         /*
2437         for
2438                 (i = 0; i < l_tot_num_tp; ++i)
2439         {
2440                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
2441                 p_header_data += l_ST;
2442                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
2443                 p_header_data += l_Ptlm_size;
2444         }*/
2445         return OPJ_TRUE;
2446 }
2447
2448 static void j2k_read_plm(opj_j2k_t *j2k) {
2449         int len, i, Zplm, Nplm, add, packet_len = 0;
2450         
2451         opj_cio_t *cio = j2k->cio;
2452
2453         len = cio_read(cio, 2);         /* Lplm */
2454         Zplm = cio_read(cio, 1);        /* Zplm */
2455         len -= 3;
2456         while (len > 0) {
2457                 Nplm = cio_read(cio, 4);                /* Nplm */
2458                 len -= 4;
2459                 for (i = Nplm; i > 0; i--) {
2460                         add = cio_read(cio, 1);
2461                         len--;
2462                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
2463                         if ((add & 0x80) == 0) {
2464                                 /* New packet */
2465                                 packet_len = 0;
2466                         }
2467                         if (len <= 0)
2468                                 break;
2469                 }
2470         }
2471 }
2472
2473 /**
2474  * Reads a PLM marker (Packet length, main header marker)
2475  *
2476  * @param       p_header_data   the data contained in the TLM box.
2477  * @param       p_j2k                   the jpeg2000 codec.
2478  * @param       p_header_size   the size of the data contained in the TLM marker.
2479  * @param       p_manager               the user event manager.
2480 */
2481 opj_bool j2k_read_plm_v2 (
2482                                                 opj_j2k_v2_t *p_j2k,
2483                                                 OPJ_BYTE * p_header_data,
2484                                                 OPJ_UINT32 p_header_size,
2485                                                 struct opj_event_mgr * p_manager
2486                                         )
2487 {
2488         // preconditions
2489         assert(p_header_data != 00);
2490         assert(p_j2k != 00);
2491         assert(p_manager != 00);
2492
2493         if (p_header_size < 1) {
2494                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2495                 return OPJ_FALSE;
2496         }
2497         /* Do not care of this at the moment since only local variables are set here */
2498         /*
2499         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
2500         ++p_header_data;
2501         --p_header_size;
2502
2503         while
2504                 (p_header_size > 0)
2505         {
2506                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
2507                 ++p_header_data;
2508                 p_header_size -= (1+l_Nplm);
2509                 if
2510                         (p_header_size < 0)
2511                 {
2512                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2513                         return false;
2514                 }
2515                 for
2516                         (i = 0; i < l_Nplm; ++i)
2517                 {
2518                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
2519                         ++p_header_data;
2520                         // take only the last seven bytes
2521                         l_packet_len |= (l_tmp & 0x7f);
2522                         if
2523                                 (l_tmp & 0x80)
2524                         {
2525                                 l_packet_len <<= 7;
2526                         }
2527                         else
2528                         {
2529                 // store packet length and proceed to next packet
2530                                 l_packet_len = 0;
2531                         }
2532                 }
2533                 if
2534                         (l_packet_len != 0)
2535                 {
2536                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2537                         return false;
2538                 }
2539         }
2540         */
2541         return OPJ_TRUE;
2542 }
2543
2544 static void j2k_read_plt(opj_j2k_t *j2k) {
2545         int len, i, Zplt, packet_len = 0, add;
2546         
2547         opj_cio_t *cio = j2k->cio;
2548         
2549         len = cio_read(cio, 2);         /* Lplt */
2550         Zplt = cio_read(cio, 1);        /* Zplt */
2551         for (i = len - 3; i > 0; i--) {
2552                 add = cio_read(cio, 1);
2553                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
2554                 if ((add & 0x80) == 0) {
2555                         /* New packet */
2556                         packet_len = 0;
2557                 }
2558         }
2559 }
2560
2561 /**
2562  * Reads a PLT marker (Packet length, tile-part header)
2563  *
2564  * @param       p_header_data   the data contained in the PLT box.
2565  * @param       p_j2k                   the jpeg2000 codec.
2566  * @param       p_header_size   the size of the data contained in the PLT marker.
2567  * @param       p_manager               the user event manager.
2568 */
2569 opj_bool j2k_read_plt_v2 (
2570                                                 opj_j2k_v2_t *p_j2k,
2571                                                 OPJ_BYTE * p_header_data,
2572                                                 OPJ_UINT32 p_header_size,
2573                                                 struct opj_event_mgr * p_manager
2574                                         )
2575 {
2576         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2577
2578         // preconditions
2579         assert(p_header_data != 00);
2580         assert(p_j2k != 00);
2581         assert(p_manager != 00);
2582
2583         if (p_header_size < 1) {
2584                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2585                 return OPJ_FALSE;
2586         }
2587
2588         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
2589         ++p_header_data;
2590         --p_header_size;
2591
2592         for (i = 0; i < p_header_size; ++i) {
2593                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
2594                 ++p_header_data;
2595                 // take only the last seven bytes
2596                 l_packet_len |= (l_tmp & 0x7f);
2597                 if (l_tmp & 0x80) {
2598                         l_packet_len <<= 7;
2599                 }
2600                 else {
2601             // store packet length and proceed to next packet
2602                         l_packet_len = 0;
2603                 }
2604         }
2605
2606         if (l_packet_len != 0) {
2607                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2608                 return OPJ_FALSE;
2609         }
2610
2611         return OPJ_TRUE;
2612 }
2613
2614 static void j2k_read_ppm(opj_j2k_t *j2k) {
2615         int len, Z_ppm, i, j;
2616         int N_ppm;
2617
2618         opj_cp_t *cp = j2k->cp;
2619         opj_cio_t *cio = j2k->cio;
2620         
2621         len = cio_read(cio, 2);
2622         cp->ppm = 1;
2623         
2624         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
2625         len -= 3;
2626         while (len > 0) {
2627                 if (cp->ppm_previous == 0) {
2628                         N_ppm = cio_read(cio, 4);       /* N_ppm */
2629                         len -= 4;
2630                 } else {
2631                         N_ppm = cp->ppm_previous;
2632                 }
2633                 j = cp->ppm_store;
2634                 if (Z_ppm == 0) {       /* First PPM marker */
2635                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2636                         cp->ppm_data_first = cp->ppm_data;
2637                         cp->ppm_len = N_ppm;
2638                 } else {                        /* NON-first PPM marker */
2639                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
2640
2641 #ifdef USE_JPWL
2642                         /* this memory allocation check could be done even in non-JPWL cases */
2643                         if (cp->correct) {
2644                                 if (!cp->ppm_data) {
2645                                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2646                                                 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2647                                                 cio_tell(cio));
2648                                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2649                                                 opj_free(cp->ppm_data);
2650                                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2651                                                 return;
2652                                         }
2653                                 }
2654                         }
2655 #endif
2656
2657                         cp->ppm_data_first = cp->ppm_data;
2658                         cp->ppm_len = N_ppm + cp->ppm_store;
2659                 }
2660                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
2661                         cp->ppm_data[j] = cio_read(cio, 1);
2662                         j++;
2663                         len--;
2664                         if (len == 0)
2665                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
2666                 }
2667                 cp->ppm_previous = i - 1;
2668                 cp->ppm_store = j;
2669         }
2670 }
2671 /**
2672  * Reads a PPM marker (Packed packet headers, main header)
2673  *
2674  * @param       p_header_data   the data contained in the POC box.
2675  * @param       p_j2k                   the jpeg2000 codec.
2676  * @param       p_header_size   the size of the data contained in the POC marker.
2677  * @param       p_manager               the user event manager.
2678 */
2679 opj_bool j2k_read_ppm_v2 (
2680                                                 opj_j2k_v2_t *p_j2k,
2681                                                 OPJ_BYTE * p_header_data,
2682                                                 OPJ_UINT32 p_header_size,
2683                                                 struct opj_event_mgr * p_manager
2684                                         )
2685 {
2686
2687         opj_cp_v2_t *l_cp = 00;
2688         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2689
2690         // preconditions
2691         assert(p_header_data != 00);
2692         assert(p_j2k != 00);
2693         assert(p_manager != 00);
2694
2695         if (p_header_size < 1) {
2696                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2697                 return OPJ_FALSE;
2698         }
2699
2700         l_cp = &(p_j2k->m_cp);
2701         l_cp->ppm = 1;
2702
2703         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
2704         ++p_header_data;
2705         --p_header_size;
2706
2707         // First PPM marker
2708         if (l_Z_ppm == 0) {
2709                 if (p_header_size < 4) {
2710                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2711                         return OPJ_FALSE;
2712                 }
2713
2714                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
2715                 p_header_data+=4;
2716                 p_header_size-=4;
2717
2718                 /* First PPM marker: Initialization */
2719                 l_cp->ppm_len = l_N_ppm;
2720                 l_cp->ppm_data_size = 0;
2721
2722                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2723                 if (l_cp->ppm_buffer == 00) {
2724                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2725                         return OPJ_FALSE;
2726                 }
2727                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
2728
2729                 l_cp->ppm_data = l_cp->ppm_buffer;
2730         }
2731
2732         while (1) {
2733                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2734                         if (p_header_size >= 4) {
2735                                 // read a N_ppm
2736                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
2737                                 p_header_data+=4;
2738                                 p_header_size-=4;
2739                                 l_cp->ppm_len += l_N_ppm ;
2740
2741                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2742                                 if (l_cp->ppm_buffer == 00) {
2743                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2744                                         return OPJ_FALSE;
2745                                 }
2746                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2747
2748                                 l_cp->ppm_data = l_cp->ppm_buffer;
2749                         }
2750                         else {
2751                                 return OPJ_FALSE;
2752                         }
2753                 }
2754
2755                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2756
2757                 if (l_remaining_data <= p_header_size) {
2758                         /* we must store less information than available in the packet */
2759                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2760                         l_cp->ppm_data_size = l_cp->ppm_len;
2761                         p_header_size -= l_remaining_data;
2762                         p_header_data += l_remaining_data;
2763                 }
2764                 else {
2765                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2766                         l_cp->ppm_data_size += p_header_size;
2767                         p_header_data += p_header_size;
2768                         p_header_size = 0;
2769                         break;
2770                 }
2771         }
2772
2773         return OPJ_TRUE;
2774 }
2775
2776
2777
2778 /**
2779  * Reads a PPM marker (Packed packet headers, main header)
2780  *
2781  * @param       p_header_data   the data contained in the POC box.
2782  * @param       p_j2k                   the jpeg2000 codec.
2783  * @param       p_header_size   the size of the data contained in the POC marker.
2784  * @param       p_manager               the user event manager.
2785 */
2786 opj_bool j2k_read_ppm_v3 (
2787                                                 opj_j2k_v2_t *p_j2k,
2788                                                 OPJ_BYTE * p_header_data,
2789                                                 OPJ_UINT32 p_header_size,
2790                                                 struct opj_event_mgr * p_manager
2791                                         )
2792 {
2793         opj_cp_v2_t *l_cp = 00;
2794         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2795
2796         // preconditions
2797         assert(p_header_data != 00);
2798         assert(p_j2k != 00);
2799         assert(p_manager != 00);
2800
2801         // Minimum size of PPM marker is equal to the size of Zppm element
2802         if (p_header_size < 1) {
2803                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2804                 return OPJ_FALSE;
2805         }
2806
2807         l_cp = &(p_j2k->m_cp);
2808         l_cp->ppm = 1;
2809
2810         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
2811         ++p_header_data;
2812         --p_header_size;
2813
2814         // First PPM marker
2815         if (l_Z_ppm == 0) {
2816                 // We need now at least the Nppm^0 element
2817                 if (p_header_size < 4) {
2818                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2819                         return OPJ_FALSE;
2820                 }
2821
2822                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
2823                 p_header_data+=4;
2824                 p_header_size-=4;
2825
2826                 /* First PPM marker: Initialization */
2827                 l_cp->ppm_len = l_N_ppm;
2828                 l_cp->ppm_data_read = 0;
2829
2830                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2831                 if (l_cp->ppm_data == 00) {
2832                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2833                         return OPJ_FALSE;
2834                 }
2835                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
2836
2837                 l_cp->ppm_data_current = l_cp->ppm_data;
2838
2839                 //l_cp->ppm_data = l_cp->ppm_buffer;
2840         }
2841         else {
2842                 if (p_header_size < 4) {
2843                         opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
2844                         return OPJ_TRUE;
2845                 }
2846                 else {
2847                         // Uncompleted Ippm series in the previous PPM marker?
2848                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
2849                                 // Get the place where add the remaining Ippm series
2850                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
2851                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
2852                         }
2853                         else {
2854                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
2855                                 p_header_data+=4;
2856                                 p_header_size-=4;
2857
2858                                 // Increase the size of ppm_data to add the new Ippm series
2859                                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2860
2861                                 // Keep the position of the place where concatenate the new series
2862                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2863                                 l_cp->ppm_len += l_N_ppm;
2864                         }
2865                 }
2866         }
2867
2868         l_remaining_data = p_header_size;
2869
2870         while (l_remaining_data >= l_N_ppm) {
2871                 // read a complete Ippm series
2872                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
2873                 p_header_size -= l_N_ppm;
2874                 p_header_data += l_N_ppm;
2875
2876                 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
2877
2878                 if (p_header_size)
2879                 {
2880                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
2881                         p_header_data+=4;
2882                         p_header_size-=4;
2883                 }
2884                 else {
2885                         l_remaining_data = p_header_size;
2886                         break;
2887                 }
2888
2889                 l_remaining_data = p_header_size;
2890
2891                 // Next Ippm series is a complete series ?
2892                 if (l_remaining_data > l_N_ppm) {
2893                         // Increase the size of ppm_data to add the new Ippm series
2894                         l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2895
2896                         // Keep the position of the place where concatenate the new series
2897                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2898                         l_cp->ppm_len += l_N_ppm;
2899                 }
2900
2901         }
2902
2903         // Need to read an incomplete Ippm series
2904         if (l_remaining_data) {
2905                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2906
2907                 // Keep the position of the place where concatenate the new series
2908                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2909                 l_cp->ppm_len += l_N_ppm;
2910
2911                 // Read incomplete Ippm series
2912                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
2913                 p_header_size -= l_remaining_data;
2914                 p_header_data += l_remaining_data;
2915
2916                 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
2917         }
2918
2919 #ifdef CLEAN_MSD
2920
2921                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2922                         if (p_header_size >= 4) {
2923                                 // read a N_ppm
2924                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
2925                                 p_header_data+=4;
2926                                 p_header_size-=4;
2927                                 l_cp->ppm_len += l_N_ppm ;
2928
2929                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2930                                 if (l_cp->ppm_buffer == 00) {
2931                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2932                                         return OPJ_FALSE;
2933                                 }
2934                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2935
2936                                 l_cp->ppm_data = l_cp->ppm_buffer;
2937                         }
2938                         else {
2939                                 return OPJ_FALSE;
2940                         }
2941                 }
2942
2943                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2944
2945                 if (l_remaining_data <= p_header_size) {
2946                         /* we must store less information than available in the packet */
2947                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2948                         l_cp->ppm_data_size = l_cp->ppm_len;
2949                         p_header_size -= l_remaining_data;
2950                         p_header_data += l_remaining_data;
2951                 }
2952                 else {
2953                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2954                         l_cp->ppm_data_size += p_header_size;
2955                         p_header_data += p_header_size;
2956                         p_header_size = 0;
2957                         break;
2958                 }
2959         }
2960 #endif
2961         return OPJ_TRUE;
2962 }
2963
2964 static void j2k_read_ppt(opj_j2k_t *j2k) {
2965         int len, Z_ppt, i, j = 0;
2966
2967         opj_cp_t *cp = j2k->cp;
2968         opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
2969         opj_cio_t *cio = j2k->cio;
2970
2971         len = cio_read(cio, 2);
2972         Z_ppt = cio_read(cio, 1);
2973         tcp->ppt = 1;
2974         if (Z_ppt == 0) {               /* First PPT marker */
2975                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
2976                 tcp->ppt_data_first = tcp->ppt_data;
2977                 tcp->ppt_store = 0;
2978                 tcp->ppt_len = len - 3;
2979         } else {                        /* NON-first PPT marker */
2980                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
2981                 tcp->ppt_data_first = tcp->ppt_data;
2982                 tcp->ppt_len = len - 3 + tcp->ppt_store;
2983         }
2984         j = tcp->ppt_store;
2985         for (i = len - 3; i > 0; i--) {
2986                 tcp->ppt_data[j] = cio_read(cio, 1);
2987                 j++;
2988         }
2989         tcp->ppt_store = j;
2990 }
2991
2992 /**
2993  * Reads a PPT marker (Packed packet headers, tile-part header)
2994  *
2995  * @param       p_header_data   the data contained in the PPT box.
2996  * @param       p_j2k                   the jpeg2000 codec.
2997  * @param       p_header_size   the size of the data contained in the PPT marker.
2998  * @param       p_manager               the user event manager.
2999 */
3000 opj_bool j2k_read_ppt_v2 (
3001                                                 opj_j2k_v2_t *p_j2k,
3002                                                 OPJ_BYTE * p_header_data,
3003                                                 OPJ_UINT32 p_header_size,
3004                                                 struct opj_event_mgr * p_manager
3005                                         )
3006 {
3007
3008         opj_cp_v2_t *l_cp = 00;
3009         opj_tcp_v2_t *l_tcp = 00;
3010         OPJ_UINT32 l_Z_ppt;
3011
3012         // preconditions
3013         assert(p_header_data != 00);
3014         assert(p_j2k != 00);
3015         assert(p_manager != 00);
3016
3017         if (p_header_size < 1) {
3018                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3019                 return OPJ_FALSE;
3020         }
3021
3022         l_cp = &(p_j2k->m_cp);
3023         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3024         l_tcp->ppt = 1;
3025
3026         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
3027         ++p_header_data;
3028         --p_header_size;
3029
3030         // first PPT marker
3031         if (l_Z_ppt == 0) {
3032                 /* First PPT marker */
3033                 l_tcp->ppt_len = p_header_size;
3034                 l_tcp->ppt_data_size = 0;
3035                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_tcp->ppt_len);
3036                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3037
3038                 if (l_tcp->ppt_buffer == 00) {
3039                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3040                         return OPJ_FALSE;
3041                 }
3042                 memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len);
3043         }
3044         else {
3045                 l_tcp->ppt_len += p_header_size;
3046                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3047
3048                 if (l_tcp->ppt_buffer == 00) {
3049                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3050                         return OPJ_FALSE;
3051                 }
3052
3053                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3054                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3055         }
3056         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3057
3058         l_tcp->ppt_data_size += p_header_size;
3059         return OPJ_TRUE;
3060 }
3061
3062 static void j2k_write_tlm(opj_j2k_t *j2k){
3063         int lenp;
3064         opj_cio_t *cio = j2k->cio;
3065         j2k->tlm_start = cio_tell(cio);
3066         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3067         lenp = 4 + (5*j2k->totnum_tp);
3068         cio_write(cio,lenp,2);                          /* Ltlm */
3069         cio_write(cio, 0,1);                                    /* Ztlm=0*/
3070         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3071         cio_skip(cio,5*j2k->totnum_tp);
3072 }
3073
3074 static void j2k_write_sot(opj_j2k_t *j2k) {
3075         int lenp, len;
3076
3077         opj_cio_t *cio = j2k->cio;
3078
3079         j2k->sot_start = cio_tell(cio);
3080         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
3081         lenp = cio_tell(cio);
3082         cio_skip(cio, 2);                                       /* Lsot (further) */
3083         cio_write(cio, j2k->curtileno, 2);      /* Isot */
3084         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
3085         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
3086         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
3087         len = cio_tell(cio) - lenp;
3088         cio_seek(cio, lenp);
3089         cio_write(cio, len, 2);                         /* Lsot */
3090         cio_seek(cio, lenp + len);
3091
3092         /* UniPG>> */
3093 #ifdef USE_JPWL
3094         /* update markers struct */
3095         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3096 #endif /* USE_JPWL */
3097         /* <<UniPG */
3098
3099         if( j2k->cstr_info && j2k->cur_tp_num==0){
3100           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3101         }
3102 }
3103
3104 static void j2k_read_sot(opj_j2k_t *j2k) {
3105         int len, tileno, totlen, partno, numparts, i;
3106         opj_tcp_t *tcp = NULL;
3107         char status = 0;
3108
3109         opj_cp_t *cp = j2k->cp;
3110         opj_cio_t *cio = j2k->cio;
3111
3112         len = cio_read(cio, 2);
3113         tileno = cio_read(cio, 2);
3114
3115 #ifdef USE_JPWL
3116         if (j2k->cp->correct) {
3117
3118                 static int backup_tileno = 0;
3119
3120                 /* tileno is negative or larger than the number of tiles!!! */
3121                 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3122                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3123                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3124                                 tileno, (cp->tw * cp->th));
3125                         if (!JPWL_ASSUME) {
3126                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3127                                 return;
3128                         }
3129                         /* we try to correct */
3130                         tileno = backup_tileno;
3131                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3132                                 "- setting tile number to %d\n",
3133                                 tileno);
3134                 }
3135
3136                 /* keep your private count of tiles */
3137                 backup_tileno++;
3138         };
3139 #endif /* USE_JPWL */
3140         
3141         if (cp->tileno_size == 0) {
3142                 cp->tileno[cp->tileno_size] = tileno;
3143                 cp->tileno_size++;
3144         } else {
3145                 i = 0;
3146                 while (i < cp->tileno_size && status == 0) {
3147                         status = cp->tileno[i] == tileno ? 1 : 0;
3148                         i++;
3149                 }
3150                 if (status == 0) {
3151                         cp->tileno[cp->tileno_size] = tileno;
3152                         cp->tileno_size++;
3153                 }
3154         }
3155         
3156         totlen = cio_read(cio, 4);
3157
3158 #ifdef USE_JPWL
3159         if (j2k->cp->correct) {
3160
3161                 /* totlen is negative or larger than the bytes left!!! */
3162                 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3163                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3164                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3165                                 totlen, cio_numbytesleft(cio) + 8);
3166                         if (!JPWL_ASSUME) {
3167                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3168                                 return;
3169                         }
3170                         /* we try to correct */
3171                         totlen = 0;
3172                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3173                                 "- setting Psot to %d => assuming it is the last tile\n",
3174                                 totlen);
3175                 }
3176
3177         };
3178 #endif /* USE_JPWL */
3179
3180         if (!totlen)
3181                 totlen = cio_numbytesleft(cio) + 8;
3182         
3183         partno = cio_read(cio, 1);
3184         numparts = cio_read(cio, 1);
3185   
3186   if (partno >= numparts) {
3187     opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
3188     numparts = partno+1;
3189   }
3190         
3191         j2k->curtileno = tileno;
3192         j2k->cur_tp_num = partno;
3193         j2k->eot = cio_getbp(cio) - 12 + totlen;
3194         j2k->state = J2K_STATE_TPH;
3195         tcp = &cp->tcps[j2k->curtileno];
3196
3197         /* Index */
3198         if (j2k->cstr_info) {
3199                 if (tcp->first) {
3200                         if (tileno == 0) 
3201                                 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3202                         j2k->cstr_info->tile[tileno].tileno = tileno;
3203                         j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3204                         j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
3205     } else {
3206                         j2k->cstr_info->tile[tileno].end_pos += totlen;
3207                 }
3208     j2k->cstr_info->tile[tileno].num_tps = numparts;
3209     if (numparts)
3210       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3211     else
3212       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); // Fixme (10)
3213                 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3214                 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
3215                         j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3216         }
3217         
3218         if (tcp->first == 1) {          
3219                 /* Initialization PPT */
3220                 opj_tccp_t *tmp = tcp->tccps;
3221                 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3222                 tcp->ppt = 0;
3223                 tcp->ppt_data = NULL;
3224                 tcp->ppt_data_first = NULL;
3225                 tcp->tccps = tmp;
3226
3227                 for (i = 0; i < j2k->image->numcomps; i++) {
3228                         tcp->tccps[i] = j2k->default_tcp->tccps[i];
3229                 }
3230                 cp->tcps[j2k->curtileno].first = 0;
3231         }
3232 }
3233
3234 /**
3235  * Reads a PPT marker (Packed packet headers, tile-part header)
3236  *
3237  * @param       p_header_data   the data contained in the PPT box.
3238  * @param       p_j2k                   the jpeg2000 codec.
3239  * @param       p_header_size   the size of the data contained in the PPT marker.
3240  * @param       p_manager               the user event manager.
3241 */
3242 opj_bool j2k_read_sot_v2 (
3243                                                 opj_j2k_v2_t *p_j2k,
3244                                                 OPJ_BYTE * p_header_data,
3245                                                 OPJ_UINT32 p_header_size,
3246                                                 struct opj_event_mgr * p_manager
3247                                         )
3248 {
3249
3250         opj_cp_v2_t *l_cp = 00;
3251         opj_tcp_v2_t *l_tcp = 00;
3252         OPJ_UINT32 l_tot_len, l_num_parts = 0;
3253         OPJ_UINT32 l_current_part;
3254         OPJ_UINT32 l_tile_x,l_tile_y;
3255
3256         // preconditions
3257         assert(p_header_data != 00);
3258         assert(p_j2k != 00);
3259         assert(p_manager != 00);
3260
3261         if (p_header_size != 8) {
3262                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3263                 return OPJ_FALSE;
3264         }
3265
3266         l_cp = &(p_j2k->m_cp);
3267         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
3268         p_header_data+=2;
3269
3270         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3271         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3272         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3273
3274 #ifdef USE_JPWL
3275         if (p_j2k->m_cp->correct) {
3276
3277                 static int backup_tileno = 0;
3278
3279                 /* tileno is negative or larger than the number of tiles!!! */
3280                 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3281                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3282                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3283                                 tileno, (cp->tw * cp->th));
3284                         if (!JPWL_ASSUME) {
3285                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3286                                 return;
3287                         }
3288                         /* we try to correct */
3289                         tileno = backup_tileno;
3290                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3291                                 "- setting tile number to %d\n",
3292                                 tileno);
3293                 }
3294
3295                 /* keep your private count of tiles */
3296                 backup_tileno++;
3297         };
3298 #endif /* USE_JPWL */
3299
3300         /* look for the tile in the list of already processed tile (in parts). */
3301         /* Optimization possible here with a more complex data structure and with the removing of tiles */
3302         /* since the time taken by this function can only grow at the time */
3303
3304         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
3305         p_header_data+=4;
3306
3307 #ifdef USE_JPWL
3308         if (p_j2k->m_cp->correct) {
3309
3310                 /* totlen is negative or larger than the bytes left!!! */
3311                 if ((totlen < 0) || (totlen > (p_stream_numbytesleft(p_stream) + 8))) {
3312                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3313                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3314                                 totlen, p_stream_numbytesleft(p_stream) + 8);
3315                         if (!JPWL_ASSUME) {
3316                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3317                                 return;
3318                         }
3319                         /* we try to correct */
3320                         totlen = 0;
3321                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3322                                 "- setting Psot to %d => assuming it is the last tile\n",
3323                                 totlen);
3324                 }
3325
3326         };
3327 #endif /* USE_JPWL */
3328
3329         if (!l_tot_len) {
3330                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot read data with no size known, giving up\n");
3331                 return OPJ_FALSE;
3332         }
3333
3334         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
3335         ++p_header_data;
3336
3337         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
3338         ++p_header_data;
3339
3340         if (l_num_parts != 0) {
3341                 l_tcp->m_nb_tile_parts = l_num_parts;
3342         }
3343
3344         if (l_tcp->m_nb_tile_parts) {
3345                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3346                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1;
3347                 }
3348         }
3349
3350         p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; // SOT_marker_size = 12
3351         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;// FIXME J2K_DEC_STATE_TPH;
3352         p_j2k->m_specific_param.m_decoder.m_skip_data =
3353                         (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3354                 ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3355                 ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3356                 ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3357
3358         /* Index */
3359
3360         /* TODO move this onto a separate method to call before reading any SOT */
3361         /* if (p_j2k->cstr_info) {
3362                 if (l_tcp->first) {
3363                         if (tileno == 0) {
3364                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3365                         }
3366
3367                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
3368                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3369                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3370                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3371
3372                         if (numparts) {
3373                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3374                         }
3375                         else {
3376                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3377                         }
3378                 }
3379                 else {
3380                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3381                 }
3382
3383                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3384                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3385                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3386         }*/
3387         return OPJ_TRUE;
3388 }
3389
3390 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3391         int l, layno;
3392         int totlen;
3393         opj_tcp_t *tcp = NULL;
3394         opj_codestream_info_t *cstr_info = NULL;
3395         
3396         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
3397         opj_cp_t *cp = j2k->cp;
3398         opj_cio_t *cio = j2k->cio;
3399
3400         tcd->tp_num = j2k->tp_num ;
3401         tcd->cur_tp_num = j2k->cur_tp_num;
3402         
3403         cio_write(cio, J2K_MS_SOD, 2);
3404
3405         if( j2k->cstr_info && j2k->cur_tp_num==0){
3406           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3407         }
3408
3409         if (j2k->curtileno == 0) {
3410                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3411         }
3412
3413         /* INDEX >> */
3414         cstr_info = j2k->cstr_info;
3415         if (cstr_info) {
3416                 if (!j2k->cur_tp_num ) {
3417                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3418                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3419                 }
3420                 else{
3421                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3422                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3423                 }
3424                 /* UniPG>> */
3425 #ifdef USE_JPWL
3426                 /* update markers struct */
3427                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3428 #endif /* USE_JPWL */
3429                 /* <<UniPG */
3430         }
3431         /* << INDEX */
3432         
3433         tcp = &cp->tcps[j2k->curtileno];
3434         for (layno = 0; layno < tcp->numlayers; layno++) {
3435                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3436                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3437                 } else if (tcp->rates[layno]) {
3438                         tcp->rates[layno]=1;
3439                 }
3440         }
3441         if(j2k->cur_tp_num == 0){
3442                 tcd->tcd_image->tiles->packno = 0;
3443                 if(cstr_info)
3444                         cstr_info->packno = 0;
3445         }
3446         
3447         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3448         
3449         /* Writing Psot in SOT marker */
3450         totlen = cio_tell(cio) + l - j2k->sot_start;
3451         cio_seek(cio, j2k->sot_start + 6);
3452         cio_write(cio, totlen, 4);
3453         cio_seek(cio, j2k->sot_start + totlen);
3454         /* Writing Ttlm and Ptlm in TLM marker */
3455         if(cp->cinema){
3456                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3457                 cio_write(cio, j2k->curtileno, 1);
3458                 cio_write(cio, totlen, 4);
3459         }
3460         cio_seek(cio, j2k->sot_start + totlen);
3461 }
3462
3463 static void j2k_read_sod(opj_j2k_t *j2k) {
3464         int len, truncate = 0, i;
3465         unsigned char *data = NULL, *data_ptr = NULL;
3466
3467         opj_cio_t *cio = j2k->cio;
3468         int curtileno = j2k->curtileno;
3469
3470         /* Index */
3471         if (j2k->cstr_info) {
3472                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3473                         cio_tell(cio) + j2k->pos_correction - 1;
3474                 if (j2k->cur_tp_num == 0)
3475                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3476                 j2k->cstr_info->packno = 0;
3477         }
3478         
3479         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3480
3481         if (len == cio_numbytesleft(cio) + 1) {
3482                 truncate = 1;           /* Case of a truncate codestream */
3483         }       
3484
3485         data = j2k->tile_data[curtileno];
3486         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3487
3488         data_ptr = data + j2k->tile_len[curtileno];
3489         for (i = 0; i < len; i++) {
3490                 data_ptr[i] = cio_read(cio, 1);
3491         }
3492
3493         j2k->tile_len[curtileno] += len;
3494         j2k->tile_data[curtileno] = data;
3495         
3496         if (!truncate) {
3497                 j2k->state = J2K_STATE_TPHSOT;
3498         } else {
3499                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
3500         }
3501         j2k->cur_tp_num++;
3502 }
3503
3504 /**
3505  * Reads a SOD marker (Start Of Data)
3506  *
3507  * @param       p_header_data   the data contained in the SOD box.
3508  * @param       p_j2k                   the jpeg2000 codec.
3509  * @param       p_header_size   the size of the data contained in the SOD marker.
3510  * @param       p_manager               the user event manager.
3511 */
3512 opj_bool j2k_read_sod_v2 (
3513                                                 opj_j2k_v2_t *p_j2k,
3514                                                 struct opj_stream_private *p_stream,
3515                                                 struct opj_event_mgr * p_manager
3516                                         )
3517 {
3518         OPJ_UINT32 l_current_read_size;
3519         opj_codestream_info_v2_t * l_cstr_info = 00;
3520         OPJ_BYTE ** l_current_data = 00;
3521         opj_tcp_v2_t * l_tcp = 00;
3522         OPJ_UINT32 * l_tile_len = 00;
3523
3524         // preconditions
3525         assert(p_j2k != 00);
3526         assert(p_manager != 00);
3527         assert(p_stream != 00);
3528
3529         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3530         p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3531
3532         l_current_data = &(l_tcp->m_data);
3533         l_tile_len = &l_tcp->m_data_size;
3534
3535         if (! *l_current_data) {
3536                 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3537         }
3538         else {
3539                 *l_current_data = (OPJ_BYTE*) opj_realloc/*FIXME V2 -> my_opj_realloc*/(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
3540         }
3541
3542         if (*l_current_data == 00) {
3543                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3544                 return OPJ_FALSE;
3545         }
3546
3547         l_cstr_info = p_j2k->cstr_info;
3548         /* Index */
3549         if (l_cstr_info) {
3550                 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3551                 l_cstr_info->tile[p_j2k->m_current_tile_number].tp[p_j2k->m_specific_param.m_encoder.m_current_tile_part_number].tp_end_header = l_current_pos;
3552
3553                 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
3554                         l_cstr_info->tile[p_j2k->m_current_tile_number].end_header = l_current_pos;
3555                 }
3556
3557                 l_cstr_info->packno = 0;
3558         }
3559
3560         l_current_read_size = opj_stream_read_data(     p_stream,
3561                                                                                                 *l_current_data + *l_tile_len,
3562                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
3563                                                                                                 p_manager);
3564
3565         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3566                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; // FIXME J2K_DEC_STATE_NEOC;
3567         }
3568         else {
3569                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT;
3570         }
3571
3572         *l_tile_len +=  l_current_read_size;
3573
3574         return OPJ_TRUE;
3575 }
3576
3577
3578 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3579         opj_cp_t *cp = j2k->cp;
3580         opj_tcp_t *tcp = &cp->tcps[tileno];
3581         opj_cio_t *cio = j2k->cio;
3582         int numcomps = j2k->image->numcomps;
3583         
3584         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
3585         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
3586         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
3587         cio_write(cio, 0, 1);                                                           /* Srgn */
3588         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
3589 }
3590
3591 static void j2k_read_rgn(opj_j2k_t *j2k) {
3592         int len, compno, roisty;
3593
3594         opj_cp_t *cp = j2k->cp;
3595         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3596         opj_cio_t *cio = j2k->cio;
3597         int numcomps = j2k->image->numcomps;
3598
3599         len = cio_read(cio, 2);                                                                         /* Lrgn */
3600         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
3601         roisty = cio_read(cio, 1);                                                                      /* Srgn */
3602
3603 #ifdef USE_JPWL
3604         if (j2k->cp->correct) {
3605                 /* totlen is negative or larger than the bytes left!!! */
3606                 if (compno >= numcomps) {
3607                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3608                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3609                                 compno, numcomps);
3610                         if (!JPWL_ASSUME || JPWL_ASSUME) {
3611                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3612                                 return;
3613                         }
3614                 }
3615         };
3616 #endif /* USE_JPWL */
3617
3618         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
3619 }
3620
3621 static void j2k_write_eoc(opj_j2k_t *j2k) {
3622         opj_cio_t *cio = j2k->cio;
3623         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3624         cio_write(cio, J2K_MS_EOC, 2);
3625
3626 /* UniPG>> */
3627 #ifdef USE_JPWL
3628         /* update markers struct */
3629         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3630 #endif /* USE_JPWL */
3631 /* <<UniPG */
3632 }
3633
3634 /**
3635  * Reads a RGN marker (Region Of Interest)
3636  *
3637  * @param       p_header_data   the data contained in the POC box.
3638  * @param       p_j2k                   the jpeg2000 codec.
3639  * @param       p_header_size   the size of the data contained in the POC marker.
3640  * @param       p_manager               the user event manager.
3641 */
3642 opj_bool j2k_read_rgn_v2 (
3643                                                 opj_j2k_v2_t *p_j2k,
3644                                                 OPJ_BYTE * p_header_data,
3645                                                 OPJ_UINT32 p_header_size,
3646                                                 struct opj_event_mgr * p_manager
3647                                         )
3648 {
3649         OPJ_UINT32 l_nb_comp;
3650         opj_image_header_t * l_image = 00;
3651
3652         opj_cp_v2_t *l_cp = 00;
3653         opj_tcp_v2_t *l_tcp = 00;
3654         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
3655
3656         // preconditions
3657         assert(p_header_data != 00);
3658         assert(p_j2k != 00);
3659         assert(p_manager != 00);
3660
3661         l_image = p_j2k->m_image_header;
3662         l_nb_comp = l_image->numcomps;
3663
3664         if (l_nb_comp <= 256) {
3665                 l_comp_room = 1; }
3666         else {
3667                 l_comp_room = 2; }
3668
3669         if (p_header_size != 2 + l_comp_room) {
3670                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
3671                 return OPJ_FALSE;
3672         }
3673
3674         l_cp = &(p_j2k->m_cp);
3675         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
3676                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3677                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3678
3679         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
3680         p_header_data+=l_comp_room;
3681         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
3682         ++p_header_data;
3683
3684 #ifdef USE_JPWL
3685         if (p_j2k->m_cp->correct) {
3686                 /* totlen is negative or larger than the bytes left!!! */
3687                 if (compno >= numcomps) {
3688                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3689                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3690                                 compno, numcomps);
3691                         if (!JPWL_ASSUME || JPWL_ASSUME) {
3692                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3693                                 return;
3694                         }
3695                 }
3696         };
3697 #endif /* USE_JPWL */
3698
3699         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
3700         ++p_header_data;
3701
3702         return OPJ_TRUE;
3703
3704 }
3705
3706 static void j2k_read_eoc(opj_j2k_t *j2k) {
3707         int i, tileno;
3708         opj_bool success;
3709
3710         /* if packets should be decoded */
3711         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
3712                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
3713                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
3714                 for (i = 0; i < j2k->cp->tileno_size; i++) {
3715                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
3716                         tileno = j2k->cp->tileno[i];
3717                         success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
3718                         opj_free(j2k->tile_data[tileno]);
3719                         j2k->tile_data[tileno] = NULL;
3720                         tcd_free_decode_tile(tcd, i);
3721                         if (success == OPJ_FALSE) {
3722                                 j2k->state |= J2K_STATE_ERR;
3723                                 break;
3724                         }
3725                 }
3726                 tcd_free_decode(tcd);
3727                 tcd_destroy(tcd);
3728         }
3729         /* if packets should not be decoded  */
3730         else {
3731                 for (i = 0; i < j2k->cp->tileno_size; i++) {
3732                         tileno = j2k->cp->tileno[i];
3733                         opj_free(j2k->tile_data[tileno]);
3734                         j2k->tile_data[tileno] = NULL;
3735                 }
3736         }       
3737         if (j2k->state & J2K_STATE_ERR)
3738                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
3739         else
3740                 j2k->state = J2K_STATE_MT; 
3741 }
3742
3743 /**
3744  * Reads a EOC marker (End Of Codestream)
3745  *
3746  * @param       p_header_data   the data contained in the SOD box.
3747  * @param       p_j2k                   the jpeg2000 codec.
3748  * @param       p_header_size   the size of the data contained in the SOD marker.
3749  * @param       p_manager               the user event manager.
3750 */
3751 opj_bool j2k_read_eoc_v2 (
3752                                                 opj_j2k_v2_t *p_j2k,
3753                                                 struct opj_stream_private *p_stream,
3754                                                 struct opj_event_mgr * p_manager
3755                                         )
3756 {
3757         OPJ_UINT32 i;
3758         opj_tcd_v2_t * l_tcd = 00;
3759         OPJ_UINT32 l_nb_tiles;
3760         opj_tcp_v2_t * l_tcp = 00;
3761         opj_bool l_success;
3762
3763         // preconditions
3764         assert(p_j2k != 00);
3765         assert(p_manager != 00);
3766         assert(p_stream != 00);
3767
3768         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3769         l_tcp = p_j2k->m_cp.tcps;
3770
3771         l_tcd = tcd_create_v2(OPJ_TRUE);
3772         if (l_tcd == 00) {
3773                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3774                 return OPJ_FALSE;
3775         }
3776
3777         for (i = 0; i < l_nb_tiles; ++i) {
3778                 if (l_tcp->m_data) {
3779                         if (! tcd_init_decode_tile(l_tcd, i)) {
3780                                 tcd_destroy_v2(l_tcd);
3781                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3782                                 return OPJ_FALSE;
3783                         }
3784
3785                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_info);
3786                         /* cleanup */
3787
3788                         if (! l_success) {
3789                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; // TODO MSD J2K_DEC_STATE_ERR;
3790                                 break;
3791                         }
3792                 }
3793
3794                 j2k_tcp_destroy(l_tcp);
3795                 ++l_tcp;
3796         }
3797
3798         tcd_destroy_v2(l_tcd);
3799         return OPJ_TRUE;
3800 }
3801
3802 typedef struct opj_dec_mstabent {
3803         /** marker value */
3804         int id;
3805         /** value of the state when the marker can appear */
3806         int states;
3807         /** action linked to the marker */
3808         void (*handler) (opj_j2k_t *j2k);
3809 } opj_dec_mstabent_t;
3810
3811 opj_dec_mstabent_t j2k_dec_mstab[] = {
3812   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
3813   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
3814   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
3815   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
3816   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
3817   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
3818   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
3819   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
3820   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
3821   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
3822   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
3823   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
3824   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
3825   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
3826   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
3827   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
3828   {J2K_MS_SOP, 0, 0},
3829   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
3830   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
3831
3832 #ifdef USE_JPWL
3833   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
3834   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
3835   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
3836   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
3837 #endif /* USE_JPWL */
3838 #ifdef USE_JPSEC
3839   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
3840   {J2K_MS_INSEC, 0, j2k_read_insec},
3841 #endif /* USE_JPSEC */
3842
3843   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
3844 };
3845
3846 static void j2k_read_unk(opj_j2k_t *j2k) {
3847         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
3848
3849 #ifdef USE_JPWL
3850         if (j2k->cp->correct) {
3851                 int m = 0, id, i;
3852                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
3853                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3854                 id = cio_read(j2k->cio, 2);
3855                 opj_event_msg(j2k->cinfo, EVT_ERROR,
3856                         "JPWL: really don't know this marker %x\n",
3857                         id);
3858                 if (!JPWL_ASSUME) {
3859                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3860                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
3861                         return;
3862                 }
3863                 /* OK, activate this at your own risk!!! */
3864                 /* we look for the marker at the minimum hamming distance from this */
3865                 while (j2k_dec_mstab[m].id) {
3866                         
3867                         /* 1's where they differ */
3868                         tmp_id = j2k_dec_mstab[m].id ^ id;
3869
3870                         /* compute the hamming distance between our id and the current */
3871                         cur_dist = 0;
3872                         for (i = 0; i < 16; i++) {
3873                                 if ((tmp_id >> i) & 0x0001) {
3874                                         cur_dist++;
3875                                 }
3876                         }
3877
3878                         /* if current distance is smaller, set the minimum */
3879                         if (cur_dist < min_dist) {
3880                                 min_dist = cur_dist;
3881                                 min_id = j2k_dec_mstab[m].id;
3882                         }
3883                         
3884                         /* jump to the next marker */
3885                         m++;
3886                 }
3887
3888                 /* do we substitute the marker? */
3889                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
3890                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3891                                 "- marker %x is at distance %d from the read %x\n",
3892                                 min_id, min_dist, id);
3893                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3894                                 "- trying to substitute in place and crossing fingers!\n");
3895                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3896                         cio_write(j2k->cio, min_id, 2);
3897
3898                         /* rewind */
3899                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3900
3901                 }
3902
3903         };
3904 #endif /* USE_JPWL */
3905
3906 }
3907
3908 /**
3909  * Reads an unknown marker
3910  *
3911  * @param       p_stream                                the stream object to read from.
3912  * @param       p_j2k                   the jpeg2000 codec.
3913  * @param       p_manager               the user event manager.
3914  *
3915  * @return      true                    if the marker could be deduced.
3916 */
3917 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
3918                                                         struct opj_stream_private *p_stream,
3919                                                         struct opj_event_mgr * p_manager
3920                                                         )
3921 {
3922         OPJ_BYTE l_data [2];
3923         OPJ_UINT32 l_unknown_size, l_unknown_marker;
3924         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
3925
3926         // preconditions
3927         assert(p_j2k != 00);
3928         assert(p_manager != 00);
3929         assert(p_stream != 00);
3930
3931         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3932
3933
3934
3935 /*      if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
3936                 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3937                 return OPJ_FALSE;
3938         }
3939
3940         opj_read_bytes(l_data,&l_unknown_size,2);
3941         if (l_unknown_size < 2) {
3942                 return OPJ_FALSE;
3943         }
3944         l_unknown_size-=2;
3945
3946         if (opj_stream_skip(p_stream,l_unknown_size,p_manager) != l_unknown_size) {
3947                 return OPJ_FALSE;
3948         }*/
3949         while(1) {
3950                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
3951                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
3952                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
3953                         return OPJ_FALSE;
3954                 }
3955
3956                 // read 2 bytes as the new marker ID
3957                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
3958
3959                 if (!(l_unknown_marker < 0xff00)) {
3960
3961                         // Get the marker handler from the marker ID
3962                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
3963
3964                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
3965                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
3966                                 return OPJ_FALSE;
3967                         }
3968                         else {
3969                                 if (l_marker_handler->id != J2K_MS_UNK)
3970                                         break; // next marker is known and well located
3971                         }
3972                 }
3973         }
3974
3975         return OPJ_TRUE;
3976 }
3977
3978 /**
3979 Read the lookup table containing all the marker, status and action
3980 @param id Marker value
3981 */
3982 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
3983         opj_dec_mstabent_t *e;
3984         for (e = j2k_dec_mstab; e->id != 0; e++) {
3985                 if (e->id == id) {
3986                         break;
3987                 }
3988         }
3989         return e;
3990 }
3991
3992 /* ----------------------------------------------------------------------- */
3993 /* J2K / JPT decoder interface                                             */
3994 /* ----------------------------------------------------------------------- */
3995
3996 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
3997         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
3998         if(!j2k)
3999                 return NULL;
4000
4001         j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4002         if(!j2k->default_tcp) {
4003                 opj_free(j2k);
4004                 return NULL;
4005         }
4006
4007         j2k->cinfo = cinfo;
4008         j2k->tile_data = NULL;
4009
4010         return j2k;
4011 }
4012
4013 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4014         int i = 0;
4015
4016         if(j2k->tile_len != NULL) {
4017                 opj_free(j2k->tile_len);
4018         }
4019         if(j2k->tile_data != NULL) {
4020                 opj_free(j2k->tile_data);
4021         }
4022         if(j2k->default_tcp != NULL) {
4023                 opj_tcp_t *default_tcp = j2k->default_tcp;
4024                 if(default_tcp->ppt_data_first != NULL) {
4025                         opj_free(default_tcp->ppt_data_first);
4026                 }
4027                 if(j2k->default_tcp->tccps != NULL) {
4028                         opj_free(j2k->default_tcp->tccps);
4029                 }
4030                 opj_free(j2k->default_tcp);
4031         }
4032         if(j2k->cp != NULL) {
4033                 opj_cp_t *cp = j2k->cp;
4034                 if(cp->tcps != NULL) {
4035                         for(i = 0; i < cp->tw * cp->th; i++) {
4036                                 if(cp->tcps[i].ppt_data_first != NULL) {
4037                                         opj_free(cp->tcps[i].ppt_data_first);
4038                                 }
4039                                 if(cp->tcps[i].tccps != NULL) {
4040                                         opj_free(cp->tcps[i].tccps);
4041                                 }
4042                         }
4043                         opj_free(cp->tcps);
4044                 }
4045                 if(cp->ppm_data_first != NULL) {
4046                         opj_free(cp->ppm_data_first);
4047                 }
4048                 if(cp->tileno != NULL) {
4049                         opj_free(cp->tileno);  
4050                 }
4051                 if(cp->comment != NULL) {
4052                         opj_free(cp->comment);
4053                 }
4054
4055                 opj_free(cp);
4056         }
4057         opj_free(j2k);
4058 }
4059
4060 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4061         if(j2k && parameters) {
4062                 /* create and initialize the coding parameters structure */
4063                 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4064                 cp->reduce = parameters->cp_reduce;     
4065                 cp->layer = parameters->cp_layer;
4066                 cp->limit_decoding = parameters->cp_limit_decoding;
4067
4068 #ifdef USE_JPWL
4069                 cp->correct = parameters->jpwl_correct;
4070                 cp->exp_comps = parameters->jpwl_exp_comps;
4071                 cp->max_tiles = parameters->jpwl_max_tiles;
4072 #endif /* USE_JPWL */
4073
4074
4075                 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4076                 j2k->cp = cp;
4077         }
4078 }
4079
4080 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) {
4081         if(j2k && parameters) {
4082                 /* create and initialize the coding parameters structure */
4083                 //opj_cp_v2_t *cp = (opj_cp_v2_t*) opj_calloc(1, sizeof(opj_cp_v2_t));
4084                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4085                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4086
4087                 /*cp->reduce = parameters->cp_reduce;
4088                 cp->layer = parameters->cp_layer;
4089                 cp->limit_decoding = parameters->cp_limit_decoding;*/
4090
4091                 // TODO MS
4092 #ifdef USE_JPWL
4093                 cp->correct = parameters->jpwl_correct;
4094                 cp->exp_comps = parameters->jpwl_exp_comps;
4095                 cp->max_tiles = parameters->jpwl_max_tiles;
4096 #endif /* USE_JPWL */
4097
4098         }
4099 }
4100
4101 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4102         opj_image_t *image = NULL;
4103
4104         opj_common_ptr cinfo = j2k->cinfo;      
4105
4106         j2k->cio = cio;
4107         j2k->cstr_info = cstr_info;
4108         if (cstr_info)
4109                 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4110
4111         /* create an empty image */
4112         image = opj_image_create0();
4113         j2k->image = image;
4114
4115         j2k->state = J2K_STATE_MHSOC;
4116
4117         for (;;) {
4118                 opj_dec_mstabent_t *e;
4119                 int id = cio_read(cio, 2);
4120
4121 #ifdef USE_JPWL
4122                 /* we try to honor JPWL correction power */
4123                 if (j2k->cp->correct) {
4124
4125                         int orig_pos = cio_tell(cio);
4126                         opj_bool status;
4127
4128                         /* call the corrector */
4129                         status = jpwl_correct(j2k);
4130
4131                         /* go back to where you were */
4132                         cio_seek(cio, orig_pos - 2);
4133
4134                         /* re-read the marker */
4135                         id = cio_read(cio, 2);
4136
4137                         /* check whether it begins with ff */
4138                         if (id >> 8 != 0xff) {
4139                                 opj_event_msg(cinfo, EVT_ERROR,
4140                                         "JPWL: possible bad marker %x at %d\n",
4141                                         id, cio_tell(cio) - 2);
4142                                 if (!JPWL_ASSUME) {
4143                                         opj_image_destroy(image);
4144                                         opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
4145                                         return 0;
4146                                 }
4147                                 /* we try to correct */
4148                                 id = id | 0xff00;
4149                                 cio_seek(cio, cio_tell(cio) - 2);
4150                                 cio_write(cio, id, 2);
4151                                 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
4152                                         "- setting marker to %x\n",
4153                                         id);
4154                         }
4155
4156                 }
4157 #endif /* USE_JPWL */
4158
4159                 if (id >> 8 != 0xff) {
4160                         opj_image_destroy(image);
4161                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4162                         return 0;
4163                 }
4164                 e = j2k_dec_mstab_lookup(id);
4165                 // Check if the marker is known
4166                 if (!(j2k->state & e->states)) {
4167                         opj_image_destroy(image);
4168                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4169                         return 0;
4170                 }
4171                 // Check if the decoding is limited to the main header
4172                 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
4173                         opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
4174                         return image;
4175                 }               
4176
4177                 if (e->handler) {
4178                         (*e->handler)(j2k);
4179                 }
4180                 if (j2k->state & J2K_STATE_ERR) 
4181                         return NULL;    
4182
4183                 if (j2k->state == J2K_STATE_MT) {
4184                         break;
4185                 }
4186                 if (j2k->state == J2K_STATE_NEOC) {
4187                         break;
4188                 }
4189         }
4190         if (j2k->state == J2K_STATE_NEOC) {
4191                 j2k_read_eoc(j2k);
4192         }
4193
4194         if (j2k->state != J2K_STATE_MT) {
4195                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4196         }
4197         return image;
4198 }
4199
4200 /*
4201 * Read a JPT-stream and decode file
4202 *
4203 */
4204 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
4205         opj_image_t *image = NULL;
4206         opj_jpt_msg_header_t header;
4207         int position;
4208         opj_common_ptr cinfo = j2k->cinfo;
4209
4210         OPJ_ARG_NOT_USED(cstr_info);
4211
4212         j2k->cio = cio;
4213
4214         /* create an empty image */
4215         image = opj_image_create0();
4216         j2k->image = image;
4217
4218         j2k->state = J2K_STATE_MHSOC;
4219         
4220         /* Initialize the header */
4221         jpt_init_msg_header(&header);
4222         /* Read the first header of the message */
4223         jpt_read_msg_header(cinfo, cio, &header);
4224         
4225         position = cio_tell(cio);
4226         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
4227                 opj_image_destroy(image);
4228                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
4229                 return 0;
4230         }
4231         
4232         for (;;) {
4233                 opj_dec_mstabent_t *e = NULL;
4234                 int id;
4235                 
4236                 if (!cio_numbytesleft(cio)) {
4237                         j2k_read_eoc(j2k);
4238                         return image;
4239                 }
4240                 /* data-bin read -> need to read a new header */
4241                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
4242                         jpt_read_msg_header(cinfo, cio, &header);
4243                         position = cio_tell(cio);
4244                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
4245                                 opj_image_destroy(image);
4246                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
4247                                 return 0;
4248                         }
4249                 }
4250                 
4251                 id = cio_read(cio, 2);
4252                 if (id >> 8 != 0xff) {
4253                         opj_image_destroy(image);
4254                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4255                         return 0;
4256                 }
4257                 e = j2k_dec_mstab_lookup(id);
4258                 if (!(j2k->state & e->states)) {
4259                         opj_image_destroy(image);
4260                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4261                         return 0;
4262                 }
4263                 if (e->handler) {
4264                         (*e->handler)(j2k);
4265                 }
4266                 if (j2k->state == J2K_STATE_MT) {
4267                         break;
4268                 }
4269                 if (j2k->state == J2K_STATE_NEOC) {
4270                         break;
4271                 }
4272         }
4273         if (j2k->state == J2K_STATE_NEOC) {
4274                 j2k_read_eoc(j2k);
4275         }
4276         
4277         if (j2k->state != J2K_STATE_MT) {
4278                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4279         }
4280
4281         return image;
4282 }
4283
4284 /* ----------------------------------------------------------------------- */
4285 /* J2K encoder interface                                                       */
4286 /* ----------------------------------------------------------------------- */
4287
4288 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
4289         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4290         if(j2k) {
4291                 j2k->cinfo = cinfo;
4292         }
4293         return j2k;
4294 }
4295
4296 opj_j2k_v2_t* j2k_create_compress_v2()
4297 {
4298         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
4299         if (!l_j2k) {
4300                 return NULL;
4301         }
4302
4303         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
4304
4305         l_j2k->m_is_decoder = 0;
4306         l_j2k->m_cp.m_is_decoder = 0;
4307
4308         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
4309         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4310                 j2k_destroy(l_j2k);
4311                 return NULL;
4312         }
4313
4314         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
4315
4316         // validation list creation
4317         l_j2k->m_validation_list = opj_procedure_list_create();
4318         if (! l_j2k->m_validation_list) {
4319                 j2k_destroy(l_j2k);
4320                 return NULL;
4321         }
4322
4323         // execution list creation
4324         l_j2k->m_procedure_list = opj_procedure_list_create();
4325         if (! l_j2k->m_procedure_list) {
4326                 j2k_destroy(l_j2k);
4327                 return NULL;
4328         }
4329
4330         return l_j2k;
4331 }
4332
4333 void j2k_destroy_compress(opj_j2k_t *j2k) {
4334         int tileno;
4335
4336         if(!j2k) return;
4337         if(j2k->cp != NULL) {
4338                 opj_cp_t *cp = j2k->cp;
4339
4340                 if(cp->comment) {
4341                         opj_free(cp->comment);
4342                 }
4343                 if(cp->matrice) {
4344                         opj_free(cp->matrice);
4345                 }
4346                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4347                         opj_free(cp->tcps[tileno].tccps);
4348                 }
4349                 opj_free(cp->tcps);
4350                 opj_free(cp);
4351         }
4352
4353         opj_free(j2k);
4354 }
4355
4356 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
4357         int i, j, tileno, numpocs_tile;
4358         opj_cp_t *cp = NULL;
4359
4360         if(!j2k || !parameters || ! image) {
4361                 return;
4362         }
4363
4364         /* create and initialize the coding parameters structure */
4365         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4366
4367         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
4368         j2k->cp = cp;
4369
4370         /* set default values for cp */
4371         cp->tw = 1;
4372         cp->th = 1;
4373
4374         /* 
4375         copy user encoding parameters 
4376         */
4377         cp->cinema = parameters->cp_cinema;
4378         cp->max_comp_size =     parameters->max_comp_size;
4379         cp->rsiz   = parameters->cp_rsiz;
4380         cp->disto_alloc = parameters->cp_disto_alloc;
4381         cp->fixed_alloc = parameters->cp_fixed_alloc;
4382         cp->fixed_quality = parameters->cp_fixed_quality;
4383
4384         /* mod fixed_quality */
4385         if(parameters->cp_matrice) {
4386                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
4387                 cp->matrice = (int *) opj_malloc(array_size);
4388                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
4389         }
4390
4391         /* tiles */
4392         cp->tdx = parameters->cp_tdx;
4393         cp->tdy = parameters->cp_tdy;
4394
4395         /* tile offset */
4396         cp->tx0 = parameters->cp_tx0;
4397         cp->ty0 = parameters->cp_ty0;
4398
4399         /* comment string */
4400         if(parameters->cp_comment) {
4401                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
4402                 if(cp->comment) {
4403                         strcpy(cp->comment, parameters->cp_comment);
4404                 }
4405         }
4406
4407         /*
4408         calculate other encoding parameters
4409         */
4410
4411         if (parameters->tile_size_on) {
4412                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
4413                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
4414         } else {
4415                 cp->tdx = image->x1 - cp->tx0;
4416                 cp->tdy = image->y1 - cp->ty0;
4417         }
4418
4419         if(parameters->tp_on){
4420                 cp->tp_flag = parameters->tp_flag;
4421                 cp->tp_on = 1;
4422         }
4423         
4424         cp->img_size = 0;
4425         for(i=0;i<image->numcomps ;i++){
4426         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
4427         }
4428
4429
4430 #ifdef USE_JPWL
4431         /*
4432         calculate JPWL encoding parameters
4433         */
4434
4435         if (parameters->jpwl_epc_on) {
4436                 int i;
4437
4438                 /* set JPWL on */
4439                 cp->epc_on = OPJ_TRUE;
4440                 cp->info_on = OPJ_FALSE; /* no informative technique */
4441
4442                 /* set EPB on */
4443                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
4444                         cp->epb_on = OPJ_TRUE;
4445                         
4446                         cp->hprot_MH = parameters->jpwl_hprot_MH;
4447                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4448                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
4449                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
4450                         }
4451                         /* if tile specs are not specified, copy MH specs */
4452                         if (cp->hprot_TPH[0] == -1) {
4453                                 cp->hprot_TPH_tileno[0] = 0;
4454                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
4455                         }
4456                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
4457                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
4458                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
4459                                 cp->pprot[i] = parameters->jpwl_pprot[i];
4460                         }
4461                 }
4462
4463                 /* set ESD writing */
4464                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
4465                         cp->esd_on = OPJ_TRUE;
4466
4467                         cp->sens_size = parameters->jpwl_sens_size;
4468                         cp->sens_addr = parameters->jpwl_sens_addr;
4469                         cp->sens_range = parameters->jpwl_sens_range;
4470
4471                         cp->sens_MH = parameters->jpwl_sens_MH;
4472                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4473                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
4474                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
4475                         }
4476                 }
4477
4478                 /* always set RED writing to false: we are at the encoder */
4479                 cp->red_on = OPJ_FALSE;
4480
4481         } else {
4482                 cp->epc_on = OPJ_FALSE;
4483         }
4484 #endif /* USE_JPWL */
4485
4486
4487         /* initialize the mutiple tiles */
4488         /* ---------------------------- */
4489         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
4490
4491         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4492                 opj_tcp_t *tcp = &cp->tcps[tileno];
4493                 tcp->numlayers = parameters->tcp_numlayers;
4494                 for (j = 0; j < tcp->numlayers; j++) {
4495                         if(cp->cinema){
4496                                 if (cp->fixed_quality) {
4497                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
4498                                 }
4499                                 tcp->rates[j] = parameters->tcp_rates[j];
4500                         }else{
4501                                 if (cp->fixed_quality) {        /* add fixed_quality */
4502                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
4503                                 } else {
4504                                         tcp->rates[j] = parameters->tcp_rates[j];
4505                                 }
4506                         }
4507                 }
4508                 tcp->csty = parameters->csty;
4509                 tcp->prg = parameters->prog_order;
4510                 tcp->mct = parameters->tcp_mct; 
4511
4512                 numpocs_tile = 0;
4513                 tcp->POC = 0;
4514                 if (parameters->numpocs) {
4515                         /* initialisation of POC */
4516                         tcp->POC = 1;
4517                         for (i = 0; i < parameters->numpocs; i++) {
4518                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
4519                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
4520                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
4521                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
4522                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
4523                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
4524                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
4525                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
4526                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
4527                                         numpocs_tile++;
4528                                 }
4529                         }
4530                         tcp->numpocs = numpocs_tile -1 ;
4531                 }else{ 
4532                         tcp->numpocs = 0;
4533                 }
4534
4535                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
4536
4537                 for (i = 0; i < image->numcomps; i++) {
4538                         opj_tccp_t *tccp = &tcp->tccps[i];
4539                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
4540                         tccp->numresolutions = parameters->numresolution;
4541                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
4542                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
4543                         tccp->cblksty = parameters->mode;
4544                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
4545                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
4546                         tccp->numgbits = 2;
4547                         if (i == parameters->roi_compno) {
4548                                 tccp->roishift = parameters->roi_shift;
4549                         } else {
4550                                 tccp->roishift = 0;
4551                         }
4552
4553                         if(parameters->cp_cinema)
4554                         {
4555                                 //Precinct size for lowest frequency subband=128
4556                                 tccp->prcw[0] = 7;
4557                                 tccp->prch[0] = 7;
4558                                 //Precinct size at all other resolutions = 256
4559                                 for (j = 1; j < tccp->numresolutions; j++) {
4560                                         tccp->prcw[j] = 8;
4561                                         tccp->prch[j] = 8;
4562                                 }
4563                         }else{
4564                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
4565                                         int p = 0;
4566                                         for (j = tccp->numresolutions - 1; j >= 0; j--) {
4567                                                 if (p < parameters->res_spec) {
4568                                                         
4569                                                         if (parameters->prcw_init[p] < 1) {
4570                                                                 tccp->prcw[j] = 1;
4571                                                         } else {
4572                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
4573                                                         }
4574                                                         
4575                                                         if (parameters->prch_init[p] < 1) {
4576                                                                 tccp->prch[j] = 1;
4577                                                         }else {
4578                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
4579                                                         }
4580
4581                                                 } else {
4582                                                         int res_spec = parameters->res_spec;
4583                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
4584                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
4585                                                         
4586                                                         if (size_prcw < 1) {
4587                                                                 tccp->prcw[j] = 1;
4588                                                         } else {
4589                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
4590                                                         }
4591                                                         
4592                                                         if (size_prch < 1) {
4593                                                                 tccp->prch[j] = 1;
4594                                                         } else {
4595                                                                 tccp->prch[j] = int_floorlog2(size_prch);
4596                                                         }
4597                                                 }
4598                                                 p++;
4599                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
4600                                         }       //end for
4601                                 } else {
4602                                         for (j = 0; j < tccp->numresolutions; j++) {
4603                                                 tccp->prcw[j] = 15;
4604                                                 tccp->prch[j] = 15;
4605                                         }
4606                                 }
4607                         }
4608
4609                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
4610                 }
4611         }
4612 }
4613
4614 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
4615         int tileno, compno;
4616         opj_cp_t *cp = NULL;
4617
4618         opj_tcd_t *tcd = NULL;  /* TCD component */
4619
4620         j2k->cio = cio; 
4621         j2k->image = image;
4622
4623         cp = j2k->cp;
4624
4625         /* INDEX >> */
4626         j2k->cstr_info = cstr_info;
4627         if (cstr_info) {
4628                 int compno;
4629                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
4630                 cstr_info->image_w = image->x1 - image->x0;
4631                 cstr_info->image_h = image->y1 - image->y0;
4632                 cstr_info->prog = (&cp->tcps[0])->prg;
4633                 cstr_info->tw = cp->tw;
4634                 cstr_info->th = cp->th;
4635                 cstr_info->tile_x = cp->tdx;    /* new version parser */
4636                 cstr_info->tile_y = cp->tdy;    /* new version parser */
4637                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
4638                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
4639                 cstr_info->numcomps = image->numcomps;
4640                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
4641                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
4642                 for (compno=0; compno < image->numcomps; compno++) {
4643                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
4644                 }
4645                 cstr_info->D_max = 0.0;         /* ADD Marcela */
4646                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
4647                 cstr_info->maxmarknum = 100;
4648                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
4649                 cstr_info->marknum = 0;
4650         }
4651         /* << INDEX */
4652
4653         j2k_write_soc(j2k);
4654         j2k_write_siz(j2k);
4655         j2k_write_cod(j2k);
4656         j2k_write_qcd(j2k);
4657
4658         if(cp->cinema){
4659                 for (compno = 1; compno < image->numcomps; compno++) {
4660                         j2k_write_coc(j2k, compno);
4661                         j2k_write_qcc(j2k, compno);
4662                 }
4663         }
4664
4665         for (compno = 0; compno < image->numcomps; compno++) {
4666                 opj_tcp_t *tcp = &cp->tcps[0];
4667                 if (tcp->tccps[compno].roishift)
4668                         j2k_write_rgn(j2k, compno, 0);
4669         }
4670         if (cp->comment != NULL) {
4671                 j2k_write_com(j2k);
4672         }
4673
4674         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
4675         /* TLM Marker*/
4676         if(cp->cinema){
4677                 j2k_write_tlm(j2k);
4678                 if (cp->cinema == CINEMA4K_24) {
4679                         j2k_write_poc(j2k);
4680                 }
4681         }
4682
4683         /* uncomment only for testing JPSEC marker writing */
4684         /* j2k_write_sec(j2k); */
4685
4686         /* INDEX >> */
4687         if(cstr_info) {
4688                 cstr_info->main_head_end = cio_tell(cio) - 1;
4689         }
4690         /* << INDEX */
4691         /**** Main Header ENDS here ***/
4692
4693         /* create the tile encoder */
4694         tcd = tcd_create(j2k->cinfo);
4695
4696         /* encode each tile */
4697         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4698                 int pino;
4699                 int tilepartno=0;
4700                 /* UniPG>> */
4701                 int acc_pack_num = 0;
4702                 /* <<UniPG */
4703
4704
4705                 opj_tcp_t *tcp = &cp->tcps[tileno];
4706                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
4707
4708                 j2k->curtileno = tileno;
4709                 j2k->cur_tp_num = 0;
4710                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
4711                 /* initialisation before tile encoding  */
4712                 if (tileno == 0) {
4713                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
4714                 } else {
4715                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
4716                 }
4717
4718                 /* INDEX >> */
4719                 if(cstr_info) {
4720                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
4721                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
4722                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
4723                         cstr_info->tile[j2k->curtileno].marknum = 0;
4724                 }
4725                 /* << INDEX */
4726
4727                 for(pino = 0; pino <= tcp->numpocs; pino++) {
4728                         int tot_num_tp;
4729                         tcd->cur_pino=pino;
4730
4731                         /*Get number of tile parts*/
4732                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
4733                         tcd->tp_pos = cp->tp_pos;
4734
4735                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
4736                                 j2k->tp_num = tilepartno;
4737                                 /* INDEX >> */
4738                                 if(cstr_info)
4739                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
4740                                         cio_tell(cio) + j2k->pos_correction;
4741                                 /* << INDEX */
4742                                 j2k_write_sot(j2k);
4743
4744                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
4745                                         for (compno = 1; compno < image->numcomps; compno++) {
4746                                                 j2k_write_coc(j2k, compno);
4747                                                 j2k_write_qcc(j2k, compno);
4748                                         }
4749                                         if (cp->tcps[tileno].numpocs) {
4750                                                 j2k_write_poc(j2k);
4751                                         }
4752                                 }
4753
4754                                 /* INDEX >> */
4755                                 if(cstr_info)
4756                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
4757                                         cio_tell(cio) + j2k->pos_correction + 1;
4758                                 /* << INDEX */
4759
4760                                 j2k_write_sod(j2k, tcd);
4761
4762                                 /* INDEX >> */
4763                                 if(cstr_info) {
4764                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
4765                                                 cio_tell(cio) + j2k->pos_correction - 1;
4766                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
4767                                                 acc_pack_num;
4768                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
4769                                                 cstr_info->packno - acc_pack_num;
4770                                         acc_pack_num = cstr_info->packno;
4771                                 }
4772                                 /* << INDEX */
4773
4774                                 j2k->cur_tp_num++;
4775                         }                       
4776                 }
4777                 if(cstr_info) {
4778                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
4779                 }
4780
4781
4782                 /*
4783                 if (tile->PPT) { // BAD PPT !!! 
4784                 FILE *PPT_file;
4785                 int i;
4786                 PPT_file=fopen("PPT","rb");
4787                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
4788                 for (i=0;i<tile->len_ppt;i++) {
4789                 unsigned char elmt;
4790                 fread(&elmt, 1, 1, PPT_file);
4791                 fwrite(&elmt,1,1,f);
4792                 }
4793                 fclose(PPT_file);
4794                 unlink("PPT");
4795                 }
4796                 */
4797
4798         }
4799
4800         /* destroy the tile encoder */
4801         tcd_free_encode(tcd);
4802         tcd_destroy(tcd);
4803
4804         opj_free(j2k->cur_totnum_tp);
4805
4806         j2k_write_eoc(j2k);
4807
4808         if(cstr_info) {
4809                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
4810                 /* UniPG>> */
4811                 /* The following adjustment is done to adjust the codestream size */
4812                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4813                 /* the first bunch of bytes is not in the codestream              */
4814                 cstr_info->codestream_size -= cstr_info->main_head_start;
4815                 /* <<UniPG */
4816         }
4817
4818 #ifdef USE_JPWL
4819         /*
4820         preparation of JPWL marker segments
4821         */
4822         if(cp->epc_on) {
4823
4824                 /* encode according to JPWL */
4825                 jpwl_encode(j2k, cio, image);
4826
4827         }
4828 #endif /* USE_JPWL */
4829
4830         return OPJ_TRUE;
4831 }
4832
4833 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4834
4835         if (!cstr_info)
4836                 return;
4837
4838         /* expand the list? */
4839         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
4840                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
4841                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
4842         }
4843
4844         /* add the marker */
4845         cstr_info->marker[cstr_info->marknum].type = type;
4846         cstr_info->marker[cstr_info->marknum].pos = pos;
4847         cstr_info->marker[cstr_info->marknum].len = len;
4848         cstr_info->marknum++;
4849
4850 }
4851
4852 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4853
4854
4855   opj_marker_info_t *marker;
4856
4857         if (!cstr_info)
4858                 return;
4859
4860         /* expand the list? */
4861         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
4862                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
4863                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
4864         }
4865
4866         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
4867
4868         /* add the marker */
4869         marker->type = type;
4870         marker->pos = pos;
4871         marker->len = len;
4872         cstr_info->tile[tileno].marknum++;
4873 }
4874
4875
4876
4877
4878 /*
4879  * -----------------------------------------------------------------------
4880  * -----------------------------------------------------------------------
4881  * -----------------------------------------------------------------------
4882  */
4883
4884 /**
4885  * Ends the decompression procedures and possibiliy add data to be read after the
4886  * codestream.
4887  */
4888 opj_bool j2k_end_decompress(
4889                                                 opj_j2k_v2_t *p_j2k,
4890                                                 opj_stream_private_t *p_stream,
4891                                                 opj_event_mgr_t * p_manager)
4892 {
4893         return OPJ_TRUE;
4894 }
4895
4896 /**
4897  * Reads a jpeg2000 codestream header structure.
4898
4899  *
4900  * @param p_stream the stream to read data from.
4901  * @param p_j2k the jpeg2000 codec.
4902  * @param p_manager the user event manager.
4903  *
4904  * @return true if the box is valid.
4905  */
4906 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
4907                                                         opj_j2k_v2_t* p_j2k,
4908                                                         opj_file_info_t* p_file_info,
4909                                                         struct opj_event_mgr* p_manager )
4910 {
4911         /* preconditions */
4912         assert(p_j2k != 00);
4913         assert(p_stream != 00);
4914         assert(p_manager != 00);
4915
4916         /* create an empty image header */
4917         p_j2k->m_image_header = opj_image_header_create0();
4918         if (! p_j2k->m_image_header) {
4919                 return OPJ_FALSE;
4920         }
4921
4922         /* customization of the validation */
4923         j2k_setup_decoding_validation(p_j2k);
4924
4925         /* validation of the parameters codec */
4926         if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
4927                 opj_image_header_destroy(p_j2k->m_image_header);
4928                 p_j2k->m_image_header = NULL;
4929                 return OPJ_FALSE;
4930         }
4931
4932         /* customization of the encoding */
4933         j2k_setup_header_reading(p_j2k);
4934
4935         /* read header */
4936         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
4937                 opj_image_header_destroy(p_j2k->m_image_header);
4938                 p_j2k->m_image_header = NULL;
4939                 return OPJ_FALSE;
4940         }
4941
4942         /* If necessary copy j2k header information into the output structure */
4943         if (p_file_info->file_info_flag & OPJ_J2K_INFO){
4944                 fill_cstr_info( &(p_file_info->codestream_info), p_j2k, p_manager);
4945         }
4946
4947         /* If necessary copy image header information into the output structure */
4948         if (p_file_info->file_info_flag & OPJ_IMG_INFO){
4949                 fill_img_info( &(p_file_info->img_info), p_j2k, p_manager);
4950         }
4951
4952         return OPJ_TRUE;
4953 }
4954
4955 /**
4956  * Fill the image info struct from information read from main header.
4957  */
4958 opj_bool fill_img_info(opj_image_header_t* img_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4959 {
4960         opj_image_header_t* l_image_header = p_j2k->m_image_header;
4961
4962         img_info->x0 = l_image_header->x0;
4963         img_info->y0 = l_image_header->y0;
4964         img_info->x1 = l_image_header->x1;
4965         img_info->y1 = l_image_header->y1;
4966         img_info->numcomps = l_image_header->numcomps;
4967
4968         if (img_info->numcomps) {
4969                 img_info->comps = (opj_image_comp_header_t*)opj_malloc(img_info->numcomps * sizeof(opj_image_comp_header_t));
4970                 if (!img_info->comps){
4971                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4972                         return OPJ_FALSE;
4973                 }
4974
4975                 memcpy(img_info->comps, l_image_header->comps, img_info->numcomps * sizeof(opj_image_comp_header_t) );
4976         }
4977
4978
4979         img_info->icc_profile_len = l_image_header->icc_profile_len;
4980         if (img_info->icc_profile_len) {
4981                 img_info->icc_profile_buf = (unsigned char*)opj_malloc(img_info->icc_profile_len * sizeof(unsigned char));
4982                 if (!img_info->icc_profile_buf){
4983                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4984                         return OPJ_FALSE;
4985                 }
4986
4987                 memcpy(img_info->icc_profile_buf, l_image_header->icc_profile_buf, img_info->icc_profile_len);
4988         }
4989
4990         return OPJ_TRUE;
4991 }
4992
4993 /**
4994  * Fill the codestream info struct from information read from main header.
4995  */
4996 opj_bool fill_cstr_info(opj_codestream_info_v2_t* cstr_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4997 {
4998
4999         //opj_cp_v2_t l_cp = p_j2k->m_cp;
5000         opj_image_header_t* l_image_header = p_j2k->m_image_header;
5001         int it_tile, it_comp;
5002         int nb_tiles, nb_comps;
5003
5004         cstr_info->image_w = l_image_header->x1 - l_image_header->x0;
5005         cstr_info->image_h = l_image_header->y1 - l_image_header->y0;
5006         cstr_info->numcomps = l_image_header->numcomps;
5007         cstr_info->tw = p_j2k->m_cp.tw;
5008         cstr_info->th = p_j2k->m_cp.th;
5009         cstr_info->tdx = p_j2k->m_cp.tdx;
5010         cstr_info->tdy = p_j2k->m_cp.tdy;
5011         cstr_info->tx0 = p_j2k->m_cp.tx0;
5012         cstr_info->ty0 = p_j2k->m_cp.ty0;
5013
5014         nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
5015         nb_comps = l_image_header->numcomps;
5016
5017         cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(nb_tiles, sizeof(opj_tile_info_v2_t));
5018         if (! p_j2k->cstr_info->tile) {
5019                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5020                 return OPJ_FALSE;
5021         }
5022
5023         for (it_tile=0; it_tile < nb_tiles; it_tile++){
5024                 cstr_info->tile[it_tile].tileno = it_tile;
5025                 cstr_info->tile[it_tile].csty = p_j2k->m_cp.tcps[it_tile].csty;
5026                 cstr_info->tile[it_tile].prg = p_j2k->m_cp.tcps[it_tile].prg;
5027                 cstr_info->tile[it_tile].mct = p_j2k->m_cp.tcps[it_tile].mct;
5028                 cstr_info->tile[it_tile].numlayers = p_j2k->m_cp.tcps[it_tile].numlayers;
5029                 // FIXME Number of tile part l_cstr_info->tile[it_tile].num_tps = ;
5030
5031                 cstr_info->tile[it_tile].tccp_info =
5032                         (opj_tccp_info_t*) opj_calloc(nb_comps, sizeof(opj_tccp_info_t));
5033                 if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
5034                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5035                         return OPJ_FALSE;
5036                 }
5037
5038                 for (it_comp=0; it_comp < nb_comps; it_comp++){
5039                         cstr_info->tile[it_tile].tccp_info[it_comp].compno = it_comp;
5040                         cstr_info->tile[it_tile].tccp_info[it_comp].cblkh = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkh;
5041                         cstr_info->tile[it_tile].tccp_info[it_comp].cblkw = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkw;
5042                         cstr_info->tile[it_tile].tccp_info[it_comp].cblksty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblksty;
5043                         cstr_info->tile[it_tile].tccp_info[it_comp].csty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].csty;
5044                         cstr_info->tile[it_tile].tccp_info[it_comp].numgbits = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numgbits;
5045                         cstr_info->tile[it_tile].tccp_info[it_comp].numresolutions = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numresolutions;
5046                         cstr_info->tile[it_tile].tccp_info[it_comp].qmfbid = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qmfbid;
5047                         cstr_info->tile[it_tile].tccp_info[it_comp].qntsty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qntsty;
5048                         cstr_info->tile[it_tile].tccp_info[it_comp].roishift = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].roishift;
5049                 }
5050         }
5051
5052         return OPJ_TRUE;
5053 }
5054
5055
5056
5057 /**
5058  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5059  */
5060 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5061 {
5062         // preconditions
5063         assert(p_j2k != 00);
5064
5065         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5066
5067         /* DEVELOPER CORNER, add your custom procedures */
5068         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5069
5070 }
5071
5072 /**
5073  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5074  * are valid. Developpers wanting to extend the library can add their own validation procedures.
5075  */
5076 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5077 {
5078         // preconditions
5079         assert(p_j2k != 00);
5080
5081         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5082         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5083         /* DEVELOPER CORNER, add your custom validation procedure */
5084
5085 }
5086
5087 /**
5088  * Builds the cp decoder parameters to use to decode tile.
5089  */
5090 opj_bool j2k_build_decoder (
5091                                                 opj_j2k_v2_t * p_j2k,
5092                                                 opj_stream_private_t *p_stream,
5093                                                 opj_event_mgr_t * p_manager
5094                                                 )
5095 {
5096         // add here initialization of cp
5097         // copy paste of setup_decoder
5098         return OPJ_TRUE;
5099 }
5100
5101 /**
5102  * The default decoding validation procedure without any extension.
5103  *
5104  * @param       p_j2k                   the jpeg2000 codec to validate.
5105  * @param       p_stream                                the input stream to validate.
5106  * @param       p_manager               the user event manager.
5107  *
5108  * @return true if the parameters are correct.
5109  */
5110 opj_bool j2k_decoding_validation (
5111                                                                 opj_j2k_v2_t *p_j2k,
5112                                                                 opj_stream_private_t *p_stream,
5113                                                                 opj_event_mgr_t * p_manager
5114                                                           )
5115 {
5116         opj_bool l_is_valid = OPJ_TRUE;
5117
5118         // preconditions
5119         assert(p_j2k != 00);
5120         assert(p_stream != 00);
5121         assert(p_manager != 00);
5122
5123
5124         /* STATE checking */
5125         /* make sure the state is at 0 */
5126 #ifdef TODO_MSD
5127         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5128 #endif
5129         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5130
5131         /* POINTER validation */
5132         /* make sure a p_j2k codec is present */
5133         /* make sure a procedure list is present */
5134         l_is_valid &= (p_j2k->m_procedure_list != 00);
5135         /* make sure a validation list is present */
5136         l_is_valid &= (p_j2k->m_validation_list != 00);
5137
5138         /* PARAMETER VALIDATION */
5139         return l_is_valid;
5140 }
5141
5142 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
5143                                                                         struct opj_stream_private *p_stream,
5144                                                                         struct opj_event_mgr * p_manager)
5145 {
5146         OPJ_UINT32 l_current_marker;
5147         OPJ_UINT32 l_marker_size;
5148         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5149
5150         // preconditions
5151         assert(p_stream != 00);
5152         assert(p_j2k != 00);
5153         assert(p_manager != 00);
5154
5155         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; // The codestream must begin with SOC marker FIXME J2K_DEC_STATE_MHSOC
5156
5157         // Try to read the SOC marker
5158         if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5159                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5160                 return OPJ_FALSE;
5161         }
5162
5163         // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5164         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5165                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5166                 return OPJ_FALSE;
5167         }
5168
5169         // Read 2 bytes as the new marker ID
5170         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5171
5172         // Try to read until the SOT is detected
5173         while (l_current_marker != J2K_MS_SOT) {
5174
5175                 // Check if the current marker ID is valid
5176                 if (l_current_marker < 0xff00) {
5177                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
5178                         return OPJ_FALSE;
5179                 }
5180
5181                 // Get the marker handler from the marker ID
5182                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5183
5184                 // Check if the marker is known and if it is the right place to find it
5185                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5186                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5187                         return OPJ_FALSE;
5188                 }
5189
5190                 // Manage case where marker is unknown
5191                 if (l_marker_handler->id == J2K_MS_UNK) {
5192
5193                         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5194
5195                         // Try to detect the next valid marker
5196                         while(1) {
5197                                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5198                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5199                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5200                                         return OPJ_FALSE;
5201                                 }
5202
5203                                 // Read 2 bytes as the new marker ID
5204                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5205
5206                                 // Check if the current marker ID is valid
5207                                 if ( !(l_current_marker < 0xff00) ) {
5208                                         // Get the marker handler from the marker ID
5209                                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
5210
5211                                         if ( !(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5212                                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5213                                                 return OPJ_FALSE;
5214                                         }
5215                                         else{
5216                                                 if (l_marker_handler->id != J2K_MS_UNK)
5217                                                         break; // next marker is known and well located
5218                                         }
5219                                 }
5220                         }
5221                         if (l_current_marker == J2K_MS_SOT)
5222                                 break; // SOT marker is detected main header is completely read.
5223                 }
5224
5225                 // Try to read 2 bytes (the marker size) from stream and copy them into the buffer
5226                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5227                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5228                         return OPJ_FALSE;
5229                 }
5230
5231                 // read 2 bytes as the marker size
5232                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5233                 l_marker_size -= 2; // Subtract the size of the marker ID already read
5234
5235                 // Check if the marker size is compatible with the header data size
5236                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5237                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5238                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5239                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5240                                 return OPJ_FALSE;
5241                         }
5242                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5243                 }
5244
5245                 // Try to read the rest of the marker segment from stream and copy them into the buffer
5246                 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) {
5247                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5248                         return OPJ_FALSE;
5249                 }
5250
5251                 // Read the marker segment with the correct marker handler
5252                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5253                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
5254                         return OPJ_FALSE;
5255                 }
5256
5257                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5258                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5259                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5260                         return OPJ_FALSE;
5261                 }
5262
5263                 // read 2 bytes as the new marker ID
5264                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5265         }
5266
5267         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT
5268         return OPJ_TRUE;
5269 }
5270
5271 /**
5272  * Excutes the given procedures on the given codec.
5273  *
5274  * @param       p_procedure_list        the list of procedures to execute
5275  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
5276  * @param       p_stream                                        the stream to execute the procedures on.
5277  * @param       p_manager                       the user manager.
5278  *
5279  * @return      true                            if all the procedures were successfully executed.
5280  */
5281 opj_bool j2k_exec (
5282                                         opj_j2k_v2_t * p_j2k,
5283                                         opj_procedure_list_t * p_procedure_list,
5284                                         opj_stream_private_t *p_stream,
5285                                         opj_event_mgr_t * p_manager
5286                                   )
5287 {
5288         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
5289         opj_bool l_result = OPJ_TRUE;
5290         OPJ_UINT32 l_nb_proc, i;
5291
5292         // preconditions
5293         assert(p_procedure_list != 00);
5294         assert(p_j2k != 00);
5295         assert(p_stream != 00);
5296         assert(p_manager != 00);
5297
5298
5299         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
5300         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);
5301
5302         for     (i=0;i<l_nb_proc;++i) {
5303                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
5304                 ++l_procedure;
5305         }
5306
5307         // and clear the procedure list at the end.
5308         opj_procedure_list_clear(p_procedure_list);
5309         return l_result;
5310 }
5311
5312
5313 opj_bool j2k_copy_default_tcp_and_create_tcd
5314                                                 (
5315                                                 opj_j2k_v2_t * p_j2k,
5316                                                 opj_stream_private_t *p_stream,
5317                                                 opj_event_mgr_t * p_manager
5318                                                 )
5319 {
5320         opj_tcp_v2_t * l_tcp = 00;
5321         opj_tcp_v2_t * l_default_tcp = 00;
5322         OPJ_UINT32 l_nb_tiles;
5323         OPJ_UINT32 i,j;
5324         opj_tccp_t *l_current_tccp = 00;
5325         OPJ_UINT32 l_tccp_size;
5326         OPJ_UINT32 l_mct_size;
5327         opj_image_header_t * l_image;
5328         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
5329         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
5330         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
5331         OPJ_UINT32 l_offset;
5332
5333         // preconditions in debug
5334         assert(p_j2k != 00);
5335         assert(p_stream != 00);
5336         assert(p_manager != 00);
5337
5338
5339         l_image = p_j2k->m_image_header;
5340         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5341         l_tcp = p_j2k->m_cp.tcps;
5342         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
5343         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
5344         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
5345         for
5346                 (i=0;i<l_nb_tiles;++i)
5347         {
5348                 l_current_tccp = l_tcp->tccps;
5349                 memcpy(l_tcp,l_default_tcp, sizeof(opj_tcp_v2_t));
5350                 l_tcp->ppt = 0;
5351                 l_tcp->ppt_data = 00;
5352                 l_tcp->tccps = l_current_tccp;
5353                 if
5354                         (l_default_tcp->m_mct_decoding_matrix)
5355                 {
5356                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5357                         if
5358                                 (! l_tcp->m_mct_decoding_matrix )
5359                         {
5360                                 return OPJ_FALSE;
5361                         }
5362                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
5363                 }
5364                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
5365                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
5366                 if
5367                         (! l_tcp->m_mct_records)
5368                 {
5369                         return OPJ_FALSE;
5370                 }
5371                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
5372                 l_src_mct_rec = l_default_tcp->m_mct_records;
5373                 l_dest_mct_rec = l_tcp->m_mct_records;
5374                 for
5375                         (j=0;j<l_default_tcp->m_nb_mct_records;++j)
5376                 {
5377                         if
5378                                 (l_src_mct_rec->m_data)
5379                         {
5380                                 l_dest_mct_rec->m_data = (OPJ_BYTE*)
5381                                 opj_malloc(l_src_mct_rec->m_data_size);
5382                                 if
5383                                         (! l_dest_mct_rec->m_data)
5384                                 {
5385                                         return OPJ_FALSE;
5386                                 }
5387                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
5388                         }
5389                         ++l_src_mct_rec;
5390                         ++l_dest_mct_rec;
5391                 }
5392                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
5393                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5394                 opj_malloc(l_mcc_records_size);
5395                 if
5396                         (! l_tcp->m_mcc_records)
5397                 {
5398                         return OPJ_FALSE;
5399                 }
5400                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
5401                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
5402                 l_dest_mcc_rec = l_tcp->m_mcc_records;
5403                 for
5404                         (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j)
5405                 {
5406                         if
5407                                 (l_src_mcc_rec->m_decorrelation_array)
5408                         {
5409                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
5410                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
5411                         }
5412                         if
5413                                 (l_src_mcc_rec->m_offset_array)
5414                         {
5415                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
5416                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
5417                         }
5418                         ++l_src_mcc_rec;
5419                         ++l_dest_mcc_rec;
5420                 }
5421                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
5422                 ++l_tcp;
5423         }
5424         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
5425         if
5426                 (! p_j2k->m_tcd )
5427         {
5428                 return OPJ_FALSE;
5429         }
5430         if
5431                 (! tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)))
5432         {
5433                 tcd_destroy_v2(p_j2k->m_tcd);
5434                 p_j2k->m_tcd = 00;
5435                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5436                 return OPJ_FALSE;
5437         }
5438         return OPJ_TRUE;
5439 }
5440
5441 /**
5442  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
5443  * with the marker value.
5444  * @param       p_id            Marker value to look up
5445  *
5446  * @return      the handler associated with the id.
5447 */
5448 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
5449 {
5450         const opj_dec_memory_marker_handler_t *e;
5451         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
5452                 if (e->id == p_id) {
5453                         break; // we find a handler corresponding to the marker ID
5454                 }
5455         }
5456         return e;
5457 }
5458
5459
5460 /**
5461  * Destroys a jpeg2000 codec.
5462  *
5463  * @param       p_j2k   the jpeg20000 structure to destroy.
5464  */
5465 void j2k_destroy (opj_j2k_v2_t *p_j2k)
5466 {
5467         if
5468                 (p_j2k == 00)
5469         {
5470                 return;
5471         }
5472
5473         if
5474                 (p_j2k->m_is_decoder)
5475         {
5476                 if
5477                         (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00)
5478                 {
5479                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5480                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5481                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
5482                 }
5483                 if
5484                         (p_j2k->m_specific_param.m_decoder.m_header_data != 00)
5485                 {
5486                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
5487                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
5488                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
5489                 }
5490
5491         }
5492         else
5493         {
5494                 if
5495                         (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data)
5496                 {
5497                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
5498                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
5499                 }
5500                 if
5501                         (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer)
5502                 {
5503                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
5504                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
5505                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
5506                 }
5507                 if
5508                         (p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5509                 {
5510                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5511                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
5512                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5513                 }
5514         }
5515         tcd_destroy_v2(p_j2k->m_tcd);
5516
5517         j2k_cp_destroy(&(p_j2k->m_cp));
5518         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
5519
5520         opj_procedure_list_destroy(p_j2k->m_procedure_list);
5521         p_j2k->m_procedure_list = 00;
5522
5523         opj_procedure_list_destroy(p_j2k->m_validation_list);
5524         p_j2k->m_procedure_list = 00;
5525
5526         opj_free(p_j2k);
5527 }
5528
5529
5530
5531 /**
5532  * Destroys a tile coding parameter structure.
5533  *
5534  * @param       p_tcp           the tile coding parameter to destroy.
5535  */
5536 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
5537 {
5538         if
5539                 (p_tcp == 00)
5540         {
5541                 return;
5542         }
5543         if
5544                 (p_tcp->ppt_buffer != 00)
5545         {
5546                 opj_free(p_tcp->ppt_buffer);
5547                 p_tcp->ppt_buffer = 00;
5548         }
5549         if
5550                 (p_tcp->tccps != 00)
5551         {
5552                 opj_free(p_tcp->tccps);
5553                 p_tcp->tccps = 00;
5554         }
5555         if
5556                 (p_tcp->m_mct_coding_matrix != 00)
5557         {
5558                 opj_free(p_tcp->m_mct_coding_matrix);
5559                 p_tcp->m_mct_coding_matrix = 00;
5560         }
5561         if
5562                 (p_tcp->m_mct_decoding_matrix != 00)
5563         {
5564                 opj_free(p_tcp->m_mct_decoding_matrix);
5565                 p_tcp->m_mct_decoding_matrix = 00;
5566         }
5567         if
5568                 (p_tcp->m_mcc_records)
5569         {
5570                 opj_free(p_tcp->m_mcc_records);
5571                 p_tcp->m_mcc_records = 00;
5572                 p_tcp->m_nb_max_mcc_records = 0;
5573                 p_tcp->m_nb_mcc_records = 0;
5574         }
5575         if
5576                 (p_tcp->m_mct_records)
5577         {
5578                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
5579                 OPJ_UINT32 i;
5580                 for
5581                         (i=0;i<p_tcp->m_nb_mct_records;++i)
5582                 {
5583                         if
5584                                 (l_mct_data->m_data)
5585                         {
5586                                 opj_free(l_mct_data->m_data);
5587                                 l_mct_data->m_data = 00;
5588                         }
5589                         ++l_mct_data;
5590                 }
5591                 opj_free(p_tcp->m_mct_records);
5592                 p_tcp->m_mct_records = 00;
5593         }
5594
5595         if
5596                 (p_tcp->mct_norms != 00)
5597         {
5598                 opj_free(p_tcp->mct_norms);
5599                 p_tcp->mct_norms = 00;
5600         }
5601         if
5602                 (p_tcp->m_data)
5603         {
5604                 opj_free(p_tcp->m_data);
5605                 p_tcp->m_data = 00;
5606         }
5607 }
5608
5609
5610 /**
5611  * Destroys a coding parameter structure.
5612  *
5613  * @param       p_cp            the coding parameter to destroy.
5614  */
5615 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
5616 {
5617         OPJ_UINT32 l_nb_tiles;
5618         opj_tcp_v2_t * l_current_tile = 00;
5619         OPJ_UINT32 i;
5620
5621         if
5622                 (p_cp == 00)
5623         {
5624                 return;
5625         }
5626         if
5627                 (p_cp->tcps != 00)
5628         {
5629                 l_current_tile = p_cp->tcps;
5630                 l_nb_tiles = p_cp->th * p_cp->tw;
5631
5632                 for
5633                         (i = 0; i < l_nb_tiles; ++i)
5634                 {
5635                         j2k_tcp_destroy(l_current_tile);
5636                         ++l_current_tile;
5637                 }
5638                 opj_free(p_cp->tcps);
5639                 p_cp->tcps = 00;
5640         }
5641         if
5642                 (p_cp->ppm_buffer != 00)
5643         {
5644                 opj_free(p_cp->ppm_buffer);
5645                 p_cp->ppm_buffer = 00;
5646         }
5647         if
5648                 (p_cp->comment != 00)
5649         {
5650                 opj_free(p_cp->comment);
5651                 p_cp->comment = 00;
5652         }
5653         if
5654                 (! p_cp->m_is_decoder)
5655         {
5656                 if
5657                         (p_cp->m_specific_param.m_enc.m_matrice)
5658                 {
5659                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
5660                         p_cp->m_specific_param.m_enc.m_matrice = 00;
5661                 }
5662         }
5663 }
5664
5665
5666
5667 /**
5668  * Reads a tile header.
5669  * @param       p_j2k           the jpeg2000 codec.
5670  * @param       p_stream                        the stream to write data to.
5671  * @param       p_manager       the user event manager.
5672  */
5673 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
5674                                                                 OPJ_UINT32 * p_tile_index,
5675                                                                 OPJ_UINT32 * p_data_size,
5676                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
5677                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
5678                                                                 OPJ_UINT32 * p_nb_comps,
5679                                                                 opj_bool * p_go_on,
5680                                                                 opj_stream_private_t *p_stream,
5681                                                                 opj_event_mgr_t * p_manager )
5682 {
5683         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
5684         OPJ_UINT32 l_marker_size;
5685         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5686         opj_tcp_v2_t * l_tcp = 00;
5687         OPJ_UINT32 l_nb_tiles;
5688
5689         /* preconditions */
5690         assert(p_stream != 00);
5691         assert(p_j2k != 00);
5692         assert(p_manager != 00);
5693
5694         if (p_j2k->m_specific_param.m_decoder.m_state == 0x0100){// FIXME J2K_DEC_STATE_EOC)
5695                 l_current_marker = J2K_MS_EOC;
5696         }
5697         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){ // FIXME J2K_DEC_STATE_TPHSOT)
5698                 return OPJ_FALSE;
5699         }
5700
5701         while (! p_j2k->m_specific_param.m_decoder.m_can_decode && l_current_marker != J2K_MS_EOC) {
5702                 while (l_current_marker != J2K_MS_SOD) {
5703
5704                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5705                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5706                                 return OPJ_FALSE;
5707                         }
5708
5709                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5710
5711                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){ // FIXME J2K_DEC_STATE_TPH)
5712                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
5713                         }
5714                         l_marker_size -= 2;
5715
5716                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
5717
5718                         /* Check if the marker is known */
5719                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5720                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5721                                 return OPJ_FALSE;
5722                         }
5723
5724                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5725                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5726                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5727                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5728                                         return OPJ_FALSE;
5729                                 }
5730
5731                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5732                         }
5733
5734                         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) {
5735                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5736                                 return OPJ_FALSE;
5737                         }
5738
5739                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5740                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5741                                 return OPJ_FALSE;
5742                         }
5743
5744                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
5745                                 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) {
5746                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5747                                         return OPJ_FALSE;
5748                                 }
5749                                 l_current_marker = J2K_MS_SOD;
5750                         }
5751                         else {
5752                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5753                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5754                                         return OPJ_FALSE;
5755                                 }
5756                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5757                         }
5758                 }
5759
5760                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
5761                         if (! j2k_read_sod_v2(p_j2k,p_stream,p_manager)) {
5762                                 return OPJ_FALSE;
5763                         }
5764                 }
5765                 else {
5766                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
5767                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5768                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT;
5769
5770                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5771                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5772                                 return OPJ_FALSE;
5773                         }
5774                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5775                 }
5776         }
5777
5778         if (l_current_marker == J2K_MS_EOC) {
5779                 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100 ){// FIXME J2K_DEC_STATE_EOC)
5780                         p_j2k->m_current_tile_number = 0;
5781                         p_j2k->m_specific_param.m_decoder.m_state = 0x0100;// FIXME J2K_DEC_STATE_EOC;
5782                 }
5783         }
5784
5785         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
5786                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
5787                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5788
5789                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
5790                         ++p_j2k->m_current_tile_number;
5791                         ++l_tcp;
5792                 }
5793
5794                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
5795                         *p_go_on = OPJ_FALSE;
5796                         return OPJ_TRUE;
5797                 }
5798         }
5799
5800         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
5801                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5802                 return OPJ_FALSE;
5803         }
5804
5805         *p_tile_index = p_j2k->m_current_tile_number;
5806         *p_go_on = OPJ_TRUE;
5807         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
5808         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
5809         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
5810         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
5811         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
5812         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
5813         p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
5814
5815         return OPJ_TRUE;
5816 }
5817
5818
5819 opj_bool j2k_decode_tile (
5820                                         opj_j2k_v2_t * p_j2k,
5821                                         OPJ_UINT32 p_tile_index,
5822                                         OPJ_BYTE * p_data,
5823                                         OPJ_UINT32 p_data_size,
5824                                         opj_stream_private_t *p_stream,
5825                                         opj_event_mgr_t * p_manager
5826                                         )
5827 {
5828         OPJ_UINT32 l_current_marker;
5829         OPJ_BYTE l_data [2];
5830         opj_tcp_v2_t * l_tcp;
5831
5832         // preconditions
5833         assert(p_stream != 00);
5834         assert(p_j2k != 00);
5835         assert(p_manager != 00);
5836
5837         if
5838                 (! (p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) || p_tile_index != p_j2k->m_current_tile_number)
5839         {
5840                 return OPJ_FALSE;
5841         }
5842         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
5843         if
5844                 (! l_tcp->m_data)
5845         {
5846                 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
5847                 return OPJ_FALSE;
5848         }
5849         if
5850                 (! tcd_decode_tile_v2(p_j2k->m_tcd, l_tcp->m_data, l_tcp->m_data_size, p_tile_index, p_j2k->cstr_info))
5851         {
5852                 j2k_tcp_destroy(l_tcp);
5853                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
5854                 return OPJ_FALSE;
5855         }
5856         if
5857                 (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size))
5858         {
5859                 return OPJ_FALSE;
5860         }
5861         j2k_tcp_destroy(l_tcp);
5862         p_j2k->m_tcd->tcp = 0;
5863
5864         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5865         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
5866         if
5867                 (p_j2k->m_specific_param.m_decoder.m_state != 0x0100)//FIXME J2K_DEC_STATE_EOC)
5868         {
5869                 if
5870                         (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2)
5871                 {
5872                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5873                         return OPJ_FALSE;
5874                 }
5875                 opj_read_bytes(l_data,&l_current_marker,2);
5876                 if
5877                         (l_current_marker == J2K_MS_EOC)
5878                 {
5879                         p_j2k->m_current_tile_number = 0;
5880                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;//FIXME J2K_DEC_STATE_EOC;
5881                 }
5882                 else if
5883                         (l_current_marker != J2K_MS_SOT)
5884                 {
5885                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
5886                         return OPJ_FALSE;
5887                 }
5888         }
5889         return OPJ_TRUE;
5890 }
5891
5892
5893 /**
5894  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
5895  *
5896  * @param       p_j2k                   the jpeg2000 codec.
5897  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
5898  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
5899  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
5900  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
5901  * @param       p_manager               the user event manager
5902  *
5903  * @return      true                    if the area could be set.
5904  */
5905 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
5906                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
5907                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
5908                                                                 struct opj_event_mgr * p_manager )
5909 {
5910         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
5911
5912         /* Check if we are read the main header */
5913         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
5914                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
5915                 return OPJ_FALSE;
5916         }
5917
5918         /* ----- */
5919         /* Check if the positions provided by the user are correct */
5920
5921         /* Left */
5922         if (p_start_x > l_cp->tw * l_cp->tdx) {
5923                 opj_event_msg_v2(p_manager, EVT_ERROR,
5924                         "Left position of the decoded area (ROI_x0=%d) is outside the tile area (nb_tw x XTsiz=%d).\n",
5925                         p_start_x, l_cp->tw * l_cp->tdx);
5926                 return OPJ_FALSE;
5927         }
5928         else if (p_start_x < l_cp->tx0){
5929                 opj_event_msg_v2(p_manager, EVT_WARNING,
5930                                 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
5931                                 p_start_x, l_cp->tx0);
5932                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
5933         }
5934         else
5935                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
5936
5937         /* Up */
5938         if (p_start_y > l_cp->th * l_cp->tdy){
5939                 opj_event_msg_v2(p_manager, EVT_ERROR,
5940                                 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (nb_th x YTsiz=%d).\n",
5941                                 p_start_y, l_cp->th * l_cp->tdy);
5942                 return OPJ_FALSE;
5943         }
5944         else if (p_start_y < l_cp->ty0){
5945                 opj_event_msg_v2(p_manager, EVT_WARNING,
5946                                 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
5947                                 p_start_y, l_cp->ty0);
5948                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
5949         }
5950         else
5951                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
5952
5953         /* Right */
5954         if (p_end_x < l_cp->tx0) {
5955                 opj_event_msg_v2(p_manager, EVT_ERROR,
5956                         "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
5957                         p_end_x, l_cp->tx0);
5958                 return OPJ_FALSE;
5959         }
5960         else if (p_end_x > l_cp->tw * l_cp->tdx) {
5961                 opj_event_msg_v2(p_manager, EVT_WARNING,
5962                         "Right position of the decoded area (ROI_x1=%d) is outside the tile area (nb_tw x XTsiz=%d).\n",
5963                         p_end_x, l_cp->tw * l_cp->tdx);
5964                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
5965         }
5966         else
5967                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
5968
5969         /* Bottom */
5970         if (p_end_x < l_cp->ty0) {
5971                 opj_event_msg_v2(p_manager, EVT_ERROR,
5972                         "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
5973                         p_end_x, l_cp->ty0);
5974                 return OPJ_FALSE;
5975         }
5976         if (p_end_y > l_cp->th * l_cp->tdy){
5977                 opj_event_msg_v2(p_manager, EVT_WARNING,
5978                         "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (nb_th x YTsiz=%d).\n",
5979                         p_end_y, l_cp->th * l_cp->tdy);
5980                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
5981         }
5982         else
5983                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
5984         /* ----- */
5985
5986         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
5987
5988         return OPJ_TRUE;
5989 }
5990
5991
5992 /* ----------------------------------------------------------------------- */
5993 /* J2K / JPT decoder interface                                             */
5994 /* ----------------------------------------------------------------------- */
5995 /**
5996  * Creates a J2K decompression structure.
5997  *
5998  * @return a handle to a J2K decompressor if successful, NULL otherwise.
5999 */
6000 opj_j2k_v2_t* j2k_create_decompress_v2()
6001 {
6002         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6003         if (!l_j2k) {
6004                 return 00;
6005         }
6006         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6007
6008         l_j2k->m_is_decoder = 1;
6009         l_j2k->m_cp.m_is_decoder = 1;
6010         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
6011
6012         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
6013                 opj_free(l_j2k);
6014                 return 00;
6015         }
6016         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
6017
6018         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6019         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
6020                 j2k_destroy(l_j2k);
6021                 return 00;
6022         }
6023
6024         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
6025
6026         // codestream info creation
6027         l_j2k->cstr_info = (opj_codestream_info_v2_t*) opj_malloc(sizeof(opj_codestream_info_v2_t));
6028         if (!l_j2k->cstr_info){
6029                 opj_free(l_j2k);
6030                 return NULL;
6031         }
6032
6033         // validation list creation
6034         l_j2k->m_validation_list = opj_procedure_list_create();
6035         if (! l_j2k->m_validation_list) {
6036                 j2k_destroy(l_j2k);
6037                 return 00;
6038         }
6039
6040         // execution list creation
6041         l_j2k->m_procedure_list = opj_procedure_list_create();
6042         if (! l_j2k->m_procedure_list) {
6043                 j2k_destroy(l_j2k);
6044                 return 00;
6045         }
6046
6047         return l_j2k;
6048 }
6049
6050
6051 /**
6052  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
6053  * @param       p_header_data   the data contained in the COM box.
6054  * @param       p_j2k                   the jpeg2000 codec.
6055  * @param       p_header_size   the size of the data contained in the COM marker.
6056  * @param       p_manager               the user event manager.
6057 */
6058 opj_bool j2k_read_SPCod_SPCoc(
6059                                                     opj_j2k_v2_t *p_j2k,
6060                                                         OPJ_UINT32 compno,
6061                                                         OPJ_BYTE * p_header_data,
6062                                                         OPJ_UINT32 * p_header_size,
6063                                                         struct opj_event_mgr * p_manager
6064                                                         )
6065 {
6066         OPJ_UINT32 i, l_tmp;
6067         opj_cp_v2_t *l_cp = NULL;
6068         opj_tcp_v2_t *l_tcp = NULL;
6069         opj_tccp_t *l_tccp = NULL;
6070         OPJ_BYTE * l_current_ptr = NULL;
6071
6072         /* preconditions */
6073         assert(p_j2k != 00);
6074         assert(p_manager != 00);
6075         assert(p_header_data != 00);
6076
6077         l_cp = &(p_j2k->m_cp);
6078         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6079                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6080                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6081
6082         /* precondition again */
6083         assert(compno < p_j2k->m_image_header->numcomps);
6084
6085         l_tccp = &l_tcp->tccps[compno];
6086         l_current_ptr = p_header_data;
6087
6088         /* make sure room is sufficient */
6089         if (*p_header_size < 5) {
6090                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6091                 return OPJ_FALSE;
6092         }
6093
6094         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
6095         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
6096         ++l_current_ptr;
6097
6098         /* If user wants to remove more resolutions than the codestream contains, return error */
6099         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
6100                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
6101                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
6102                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
6103                 return OPJ_FALSE;
6104         }
6105
6106         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
6107         ++l_current_ptr;
6108         l_tccp->cblkw += 2;
6109
6110         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
6111         ++l_current_ptr;
6112         l_tccp->cblkh += 2;
6113
6114         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
6115         ++l_current_ptr;
6116
6117         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
6118         ++l_current_ptr;
6119
6120         *p_header_size = *p_header_size - 5;
6121
6122         /* use custom precinct size ? */
6123         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
6124                 if (*p_header_size < l_tccp->numresolutions) {
6125                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6126                         return OPJ_FALSE;
6127                 }
6128
6129                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
6130                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
6131                         ++l_current_ptr;
6132                         l_tccp->prcw[i] = l_tmp & 0xf;
6133                         l_tccp->prch[i] = l_tmp >> 4;
6134                 }
6135
6136                 *p_header_size = *p_header_size - l_tccp->numresolutions;
6137         }
6138         else {
6139                 /* set default size for the precinct width and height */
6140                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
6141                         l_tccp->prcw[i] = 15;
6142                         l_tccp->prch[i] = 15;
6143                 }
6144         }
6145
6146         /* INDEX >> */
6147         if (p_j2k->cstr_info && compno == 0) {
6148                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
6149
6150                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
6151                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
6152                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
6153                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
6154                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
6155
6156
6157                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
6158                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
6159         }
6160         /* << INDEX */
6161         return OPJ_TRUE;
6162 }
6163
6164 /**
6165  * Copies the tile component parameters of all the component from the first tile component.
6166  *
6167  * @param               p_j2k           the J2k codec.
6168  */
6169 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
6170 {
6171         // loop
6172         OPJ_UINT32 i;
6173         opj_cp_v2_t *l_cp = NULL;
6174         opj_tcp_v2_t *l_tcp = NULL;
6175         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
6176         OPJ_UINT32 l_prc_size;
6177
6178         /* preconditions */
6179         assert(p_j2k != 00);
6180
6181         l_cp = &(p_j2k->m_cp);
6182         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6183                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6184                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6185
6186         l_ref_tccp = &l_tcp->tccps[0];
6187         l_copied_tccp = l_ref_tccp + 1;
6188         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
6189
6190         for     (i=1; i<p_j2k->m_image_header->numcomps; ++i) {
6191                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
6192                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
6193                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
6194                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
6195                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
6196                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
6197                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
6198                 ++l_copied_tccp;
6199         }
6200 }
6201
6202 /**
6203  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
6204  *
6205  * @param       p_comp_no               the component being targeted.
6206  * @param       p_header_data   the data contained in the COM box.
6207  * @param       p_j2k                   the jpeg2000 codec.
6208  * @param       p_header_size   the size of the data contained in the COM marker.
6209  * @param       p_manager               the user event manager.
6210 */
6211 opj_bool j2k_read_SQcd_SQcc(
6212                                                         opj_j2k_v2_t *p_j2k,
6213                                                         OPJ_UINT32 p_comp_no,
6214                                                         OPJ_BYTE* p_header_data,
6215                                                         OPJ_UINT32 * p_header_size,
6216                                                         struct opj_event_mgr * p_manager
6217                                                         )
6218 {
6219         // loop
6220         OPJ_UINT32 l_band_no;
6221         opj_cp_v2_t *l_cp = 00;
6222         opj_tcp_v2_t *l_tcp = 00;
6223         opj_tccp_t *l_tccp = 00;
6224         OPJ_BYTE * l_current_ptr = 00;
6225         OPJ_UINT32 l_tmp, l_num_band;
6226
6227         // preconditions
6228         assert(p_j2k != 00);
6229         assert(p_manager != 00);
6230         assert(p_header_data != 00);
6231
6232         l_cp = &(p_j2k->m_cp);
6233         // come from tile part header or main header ?
6234         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
6235                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6236                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6237
6238         // precondition again
6239         assert(p_comp_no <  p_j2k->m_image_header->numcomps);
6240
6241         l_tccp = &l_tcp->tccps[p_comp_no];
6242         l_current_ptr = p_header_data;
6243
6244         if (*p_header_size < 1) {
6245                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
6246                 return OPJ_FALSE;
6247         }
6248         *p_header_size -= 1;
6249
6250         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
6251         ++l_current_ptr;
6252
6253         l_tccp->qntsty = l_tmp & 0x1f;
6254         l_tccp->numgbits = l_tmp >> 5;
6255         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6256         l_num_band = 1;
6257         }
6258         else {
6259                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
6260                         (*p_header_size) :
6261                         (*p_header_size) / 2;
6262
6263                 if( l_num_band > J2K_MAXBANDS ) {
6264                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
6265                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
6266                                 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
6267                         //return OPJ_FALSE;
6268                 }
6269         }
6270
6271 #ifdef USE_JPWL
6272         if (p_j2k->m_cp->correct) {
6273
6274                 /* if JPWL is on, we check whether there are too many subbands */
6275                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
6276                         opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
6277                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
6278                                 numbands);
6279                         if (!JPWL_ASSUME) {
6280                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6281                                 return;
6282                         }
6283                         /* we try to correct */
6284                         numbands = 1;
6285                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
6286                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
6287                                 numbands);
6288                 };
6289
6290         };
6291 #endif /* USE_JPWL */
6292
6293         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
6294                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6295                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
6296                         ++l_current_ptr;
6297                         if (l_band_no < J2K_MAXBANDS){
6298                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
6299                                 l_tccp->stepsizes[l_band_no].mant = 0;
6300                         }
6301                 }
6302                 *p_header_size = *p_header_size - l_num_band;
6303         }
6304         else {
6305                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6306                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
6307                         l_current_ptr+=2;
6308                         if (l_band_no < J2K_MAXBANDS){
6309                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
6310                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
6311                         }
6312                 }
6313                 *p_header_size = *p_header_size - 2*l_num_band;
6314         }
6315
6316         /* Add Antonin : if scalar_derived -> compute other stepsizes */
6317         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6318                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
6319                         l_tccp->stepsizes[l_band_no].expn =
6320                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
6321                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
6322                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
6323                 }
6324         }
6325
6326         return OPJ_TRUE;
6327 }
6328
6329
6330
6331 /**
6332  * Copies the tile component parameters of all the component from the first tile component.
6333  *
6334  * @param               p_j2k           the J2k codec.
6335  */
6336 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
6337 {
6338         OPJ_UINT32 i;
6339         opj_cp_v2_t *l_cp = NULL;
6340         opj_tcp_v2_t *l_tcp = NULL;
6341         opj_tccp_t *l_ref_tccp = NULL;
6342         opj_tccp_t *l_copied_tccp = NULL;
6343         OPJ_UINT32 l_size;
6344
6345         // preconditions
6346         assert(p_j2k != 00);
6347
6348         l_cp = &(p_j2k->m_cp);
6349         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH /* FIXME J2K_DEC_STATE_TPH*/ ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp;
6350         // precondition again
6351         l_ref_tccp = &l_tcp->tccps[0];
6352         l_copied_tccp = l_ref_tccp + 1;
6353         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
6354
6355         for     (i=1;i<p_j2k->m_image_header->numcomps;++i) {
6356                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
6357                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
6358                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
6359                 ++l_copied_tccp;
6360         }
6361 }