WIP: update t1, t2, tcd to use same strut as in V2
[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 (      opj_j2k_v2_t *p_j2k,
3001                                                         OPJ_BYTE * p_header_data,
3002                                                         OPJ_UINT32 p_header_size,
3003                                                         struct opj_event_mgr * p_manager )
3004 {
3005         opj_cp_v2_t *l_cp = 00;
3006         opj_tcp_v2_t *l_tcp = 00;
3007         OPJ_UINT32 l_Z_ppt;
3008
3009         /* preconditions */
3010         assert(p_header_data != 00);
3011         assert(p_j2k != 00);
3012         assert(p_manager != 00);
3013
3014         /* We need to have the Z_ppt element at minimum */
3015         if (p_header_size < 1) {
3016                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3017                 return OPJ_FALSE;
3018         }
3019
3020         l_cp = &(p_j2k->m_cp);
3021         if (l_cp->ppm){
3022                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
3023                 return OPJ_FALSE;
3024         }
3025
3026         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3027         l_tcp->ppt = 1;
3028
3029         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
3030         ++p_header_data;
3031         --p_header_size;
3032
3033         /* Allocate buffer to read the packet header */
3034         if (l_Z_ppt == 0) {
3035                 /* First PPT marker */
3036                 l_tcp->ppt_data_size = 0;
3037                 l_tcp->ppt_len = p_header_size;
3038
3039                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3040                 if (l_tcp->ppt_buffer == 00) {
3041                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3042                         return OPJ_FALSE;
3043                 }
3044                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3045
3046                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3047         }
3048         else {
3049                 l_tcp->ppt_len += p_header_size;
3050
3051                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3052                 if (l_tcp->ppt_buffer == 00) {
3053                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3054                         return OPJ_FALSE;
3055                 }
3056                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3057
3058                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3059         }
3060
3061         /* Read packet header from buffer */
3062         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3063
3064         l_tcp->ppt_data_size += p_header_size;
3065
3066         return OPJ_TRUE;
3067 }
3068
3069 static void j2k_write_tlm(opj_j2k_t *j2k){
3070         int lenp;
3071         opj_cio_t *cio = j2k->cio;
3072         j2k->tlm_start = cio_tell(cio);
3073         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3074         lenp = 4 + (5*j2k->totnum_tp);
3075         cio_write(cio,lenp,2);                          /* Ltlm */
3076         cio_write(cio, 0,1);                                    /* Ztlm=0*/
3077         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3078         cio_skip(cio,5*j2k->totnum_tp);
3079 }
3080
3081 static void j2k_write_sot(opj_j2k_t *j2k) {
3082         int lenp, len;
3083
3084         opj_cio_t *cio = j2k->cio;
3085
3086         j2k->sot_start = cio_tell(cio);
3087         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
3088         lenp = cio_tell(cio);
3089         cio_skip(cio, 2);                                       /* Lsot (further) */
3090         cio_write(cio, j2k->curtileno, 2);      /* Isot */
3091         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
3092         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
3093         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
3094         len = cio_tell(cio) - lenp;
3095         cio_seek(cio, lenp);
3096         cio_write(cio, len, 2);                         /* Lsot */
3097         cio_seek(cio, lenp + len);
3098
3099         /* UniPG>> */
3100 #ifdef USE_JPWL
3101         /* update markers struct */
3102         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3103 #endif /* USE_JPWL */
3104         /* <<UniPG */
3105
3106         if( j2k->cstr_info && j2k->cur_tp_num==0){
3107           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3108         }
3109 }
3110
3111 static void j2k_read_sot(opj_j2k_t *j2k) {
3112         int len, tileno, totlen, partno, numparts, i;
3113         opj_tcp_t *tcp = NULL;
3114         char status = 0;
3115
3116         opj_cp_t *cp = j2k->cp;
3117         opj_cio_t *cio = j2k->cio;
3118
3119         len = cio_read(cio, 2);
3120         tileno = cio_read(cio, 2);
3121
3122 #ifdef USE_JPWL
3123         if (j2k->cp->correct) {
3124
3125                 static int backup_tileno = 0;
3126
3127                 /* tileno is negative or larger than the number of tiles!!! */
3128                 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3129                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3130                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3131                                 tileno, (cp->tw * cp->th));
3132                         if (!JPWL_ASSUME) {
3133                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3134                                 return;
3135                         }
3136                         /* we try to correct */
3137                         tileno = backup_tileno;
3138                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3139                                 "- setting tile number to %d\n",
3140                                 tileno);
3141                 }
3142
3143                 /* keep your private count of tiles */
3144                 backup_tileno++;
3145         };
3146 #endif /* USE_JPWL */
3147         
3148         if (cp->tileno_size == 0) {
3149                 cp->tileno[cp->tileno_size] = tileno;
3150                 cp->tileno_size++;
3151         } else {
3152                 i = 0;
3153                 while (i < cp->tileno_size && status == 0) {
3154                         status = cp->tileno[i] == tileno ? 1 : 0;
3155                         i++;
3156                 }
3157                 if (status == 0) {
3158                         cp->tileno[cp->tileno_size] = tileno;
3159                         cp->tileno_size++;
3160                 }
3161         }
3162         
3163         totlen = cio_read(cio, 4);
3164
3165 #ifdef USE_JPWL
3166         if (j2k->cp->correct) {
3167
3168                 /* totlen is negative or larger than the bytes left!!! */
3169                 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3170                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3171                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3172                                 totlen, cio_numbytesleft(cio) + 8);
3173                         if (!JPWL_ASSUME) {
3174                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3175                                 return;
3176                         }
3177                         /* we try to correct */
3178                         totlen = 0;
3179                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3180                                 "- setting Psot to %d => assuming it is the last tile\n",
3181                                 totlen);
3182                 }
3183
3184         };
3185 #endif /* USE_JPWL */
3186
3187         if (!totlen)
3188                 totlen = cio_numbytesleft(cio) + 8;
3189         
3190         partno = cio_read(cio, 1);
3191         numparts = cio_read(cio, 1);
3192   
3193   if (partno >= numparts) {
3194     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);
3195     numparts = partno+1;
3196   }
3197         
3198         j2k->curtileno = tileno;
3199         j2k->cur_tp_num = partno;
3200         j2k->eot = cio_getbp(cio) - 12 + totlen;
3201         j2k->state = J2K_STATE_TPH;
3202         tcp = &cp->tcps[j2k->curtileno];
3203
3204         /* Index */
3205         if (j2k->cstr_info) {
3206                 if (tcp->first) {
3207                         if (tileno == 0) 
3208                                 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3209                         j2k->cstr_info->tile[tileno].tileno = tileno;
3210                         j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3211                         j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
3212     } else {
3213                         j2k->cstr_info->tile[tileno].end_pos += totlen;
3214                 }
3215     j2k->cstr_info->tile[tileno].num_tps = numparts;
3216     if (numparts)
3217       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3218     else
3219       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)
3220                 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3221                 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
3222                         j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3223         }
3224         
3225         if (tcp->first == 1) {          
3226                 /* Initialization PPT */
3227                 opj_tccp_t *tmp = tcp->tccps;
3228                 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3229                 tcp->ppt = 0;
3230                 tcp->ppt_data = NULL;
3231                 tcp->ppt_data_first = NULL;
3232                 tcp->tccps = tmp;
3233
3234                 for (i = 0; i < j2k->image->numcomps; i++) {
3235                         tcp->tccps[i] = j2k->default_tcp->tccps[i];
3236                 }
3237                 cp->tcps[j2k->curtileno].first = 0;
3238         }
3239 }
3240
3241 /**
3242  * Reads a PPT marker (Packed packet headers, tile-part header)
3243  *
3244  * @param       p_header_data   the data contained in the PPT box.
3245  * @param       p_j2k                   the jpeg2000 codec.
3246  * @param       p_header_size   the size of the data contained in the PPT marker.
3247  * @param       p_manager               the user event manager.
3248 */
3249 opj_bool j2k_read_sot_v2 (
3250                                                 opj_j2k_v2_t *p_j2k,
3251                                                 OPJ_BYTE * p_header_data,
3252                                                 OPJ_UINT32 p_header_size,
3253                                                 struct opj_event_mgr * p_manager
3254                                         )
3255 {
3256
3257         opj_cp_v2_t *l_cp = 00;
3258         opj_tcp_v2_t *l_tcp = 00;
3259         OPJ_UINT32 l_tot_len, l_num_parts = 0;
3260         OPJ_UINT32 l_current_part;
3261         OPJ_UINT32 l_tile_x,l_tile_y;
3262
3263         /* preconditions */
3264         assert(p_header_data != 00);
3265         assert(p_j2k != 00);
3266         assert(p_manager != 00);
3267
3268         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3269         if (p_header_size != 8) {
3270                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3271                 return OPJ_FALSE;
3272         }
3273
3274         l_cp = &(p_j2k->m_cp);
3275         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
3276         p_header_data+=2;
3277
3278         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3279         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3280         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3281
3282 #ifdef USE_JPWL
3283         if (p_j2k->m_cp->correct) {
3284
3285                 static int backup_tileno = 0;
3286
3287                 /* tileno is negative or larger than the number of tiles!!! */
3288                 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3289                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3290                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3291                                 tileno, (cp->tw * cp->th));
3292                         if (!JPWL_ASSUME) {
3293                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3294                                 return;
3295                         }
3296                         /* we try to correct */
3297                         tileno = backup_tileno;
3298                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3299                                 "- setting tile number to %d\n",
3300                                 tileno);
3301                 }
3302
3303                 /* keep your private count of tiles */
3304                 backup_tileno++;
3305         };
3306 #endif /* USE_JPWL */
3307
3308         /* look for the tile in the list of already processed tile (in parts). */
3309         /* Optimization possible here with a more complex data structure and with the removing of tiles */
3310         /* since the time taken by this function can only grow at the time */
3311
3312         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
3313         p_header_data+=4;
3314
3315 #ifdef USE_JPWL
3316         if (p_j2k->m_cp->correct) {
3317
3318                 /* totlen is negative or larger than the bytes left!!! */
3319                 if ((totlen < 0) || (totlen > (p_stream_numbytesleft(p_stream) + 8))) {
3320                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3321                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3322                                 totlen, p_stream_numbytesleft(p_stream) + 8);
3323                         if (!JPWL_ASSUME) {
3324                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3325                                 return;
3326                         }
3327                         /* we try to correct */
3328                         totlen = 0;
3329                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3330                                 "- setting Psot to %d => assuming it is the last tile\n",
3331                                 totlen);
3332                 }
3333         };
3334 #endif /* USE_JPWL */
3335
3336         if (!l_tot_len) {
3337                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot read data with no size known, giving up\n");
3338                 return OPJ_FALSE;
3339         }
3340
3341         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
3342         ++p_header_data;
3343
3344         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
3345         ++p_header_data;
3346
3347         if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3348                 l_tcp->m_nb_tile_parts = l_num_parts;
3349         }
3350
3351         /* If know the number of tile part header we will check if we didn't read the last*/
3352         if (l_tcp->m_nb_tile_parts) {
3353                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3354                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3355                 }
3356         }
3357
3358         /* Keep the size of data to skip after this marker */
3359         p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3360         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3361
3362         /* Check if the current tile is outside the area we want decode (in tile index)*/
3363         p_j2k->m_specific_param.m_decoder.m_skip_data =
3364                         (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3365                 ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3366                 ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3367                 ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3368
3369         /* Index */
3370
3371         /* TODO move this onto a separate method to call before reading any SOT */
3372         /* if (p_j2k->cstr_info) {
3373                 if (l_tcp->first) {
3374                         if (tileno == 0) {
3375                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3376                         }
3377
3378                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
3379                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3380                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3381                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3382
3383                         if (numparts) {
3384                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3385                         }
3386                         else {
3387                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3388                         }
3389                 }
3390                 else {
3391                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3392                 }
3393
3394                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3395                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3396                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3397         }*/
3398         return OPJ_TRUE;
3399 }
3400
3401 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3402         int l, layno;
3403         int totlen;
3404         opj_tcp_t *tcp = NULL;
3405         opj_codestream_info_t *cstr_info = NULL;
3406         
3407         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
3408         opj_cp_t *cp = j2k->cp;
3409         opj_cio_t *cio = j2k->cio;
3410
3411         tcd->tp_num = j2k->tp_num ;
3412         tcd->cur_tp_num = j2k->cur_tp_num;
3413         
3414         cio_write(cio, J2K_MS_SOD, 2);
3415
3416         if( j2k->cstr_info && j2k->cur_tp_num==0){
3417           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3418         }
3419
3420         if (j2k->curtileno == 0) {
3421                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3422         }
3423
3424         /* INDEX >> */
3425         cstr_info = j2k->cstr_info;
3426         if (cstr_info) {
3427                 if (!j2k->cur_tp_num ) {
3428                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3429                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3430                 }
3431                 else{
3432                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3433                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3434                 }
3435                 /* UniPG>> */
3436 #ifdef USE_JPWL
3437                 /* update markers struct */
3438                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3439 #endif /* USE_JPWL */
3440                 /* <<UniPG */
3441         }
3442         /* << INDEX */
3443         
3444         tcp = &cp->tcps[j2k->curtileno];
3445         for (layno = 0; layno < tcp->numlayers; layno++) {
3446                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3447                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3448                 } else if (tcp->rates[layno]) {
3449                         tcp->rates[layno]=1;
3450                 }
3451         }
3452         if(j2k->cur_tp_num == 0){
3453                 tcd->tcd_image->tiles->packno = 0;
3454                 if(cstr_info)
3455                         cstr_info->packno = 0;
3456         }
3457         
3458         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3459         
3460         /* Writing Psot in SOT marker */
3461         totlen = cio_tell(cio) + l - j2k->sot_start;
3462         cio_seek(cio, j2k->sot_start + 6);
3463         cio_write(cio, totlen, 4);
3464         cio_seek(cio, j2k->sot_start + totlen);
3465         /* Writing Ttlm and Ptlm in TLM marker */
3466         if(cp->cinema){
3467                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3468                 cio_write(cio, j2k->curtileno, 1);
3469                 cio_write(cio, totlen, 4);
3470         }
3471         cio_seek(cio, j2k->sot_start + totlen);
3472 }
3473
3474 static void j2k_read_sod(opj_j2k_t *j2k) {
3475         int len, truncate = 0, i;
3476         unsigned char *data = NULL, *data_ptr = NULL;
3477
3478         opj_cio_t *cio = j2k->cio;
3479         int curtileno = j2k->curtileno;
3480
3481         /* Index */
3482         if (j2k->cstr_info) {
3483                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3484                         cio_tell(cio) + j2k->pos_correction - 1;
3485                 if (j2k->cur_tp_num == 0)
3486                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3487                 j2k->cstr_info->packno = 0;
3488         }
3489         
3490         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3491
3492         if (len == cio_numbytesleft(cio) + 1) {
3493                 truncate = 1;           /* Case of a truncate codestream */
3494         }       
3495
3496         data = j2k->tile_data[curtileno];
3497         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3498
3499         data_ptr = data + j2k->tile_len[curtileno];
3500         for (i = 0; i < len; i++) {
3501                 data_ptr[i] = cio_read(cio, 1);
3502         }
3503
3504         j2k->tile_len[curtileno] += len;
3505         j2k->tile_data[curtileno] = data;
3506         
3507         if (!truncate) {
3508                 j2k->state = J2K_STATE_TPHSOT;
3509         } else {
3510                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
3511         }
3512         j2k->cur_tp_num++;
3513 }
3514
3515 /**
3516  * Reads a SOD marker (Start Of Data)
3517  *
3518  * @param       p_header_data   the data contained in the SOD box.
3519  * @param       p_j2k                   the jpeg2000 codec.
3520  * @param       p_header_size   the size of the data contained in the SOD marker.
3521  * @param       p_manager               the user event manager.
3522 */
3523 opj_bool j2k_read_sod_v2 (
3524                                                 opj_j2k_v2_t *p_j2k,
3525                                                 struct opj_stream_private *p_stream,
3526                                                 struct opj_event_mgr * p_manager
3527                                         )
3528 {
3529         OPJ_UINT32 l_current_read_size;
3530         opj_codestream_info_v2_t * l_cstr_info = 00;
3531         OPJ_BYTE ** l_current_data = 00;
3532         opj_tcp_v2_t * l_tcp = 00;
3533         OPJ_UINT32 * l_tile_len = 00;
3534
3535         // preconditions
3536         assert(p_j2k != 00);
3537         assert(p_manager != 00);
3538         assert(p_stream != 00);
3539
3540         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3541         p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3542
3543         l_current_data = &(l_tcp->m_data);
3544         l_tile_len = &l_tcp->m_data_size;
3545
3546         if (! *l_current_data) {
3547                 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3548         }
3549         else {
3550                 *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);
3551         }
3552
3553         if (*l_current_data == 00) {
3554                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3555                 return OPJ_FALSE;
3556         }
3557
3558         l_cstr_info = p_j2k->cstr_info;
3559
3560         /* Index */
3561 #ifdef TODO_MSD
3562         if (l_cstr_info) {
3563                 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3564                 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;
3565
3566                 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
3567                         l_cstr_info->tile[p_j2k->m_current_tile_number].end_header = l_current_pos;
3568                 }
3569
3570                 l_cstr_info->packno = 0;
3571         }
3572 #endif
3573
3574         l_current_read_size = opj_stream_read_data(     p_stream,
3575                                                                                                 *l_current_data + *l_tile_len,
3576                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
3577                                                                                                 p_manager);
3578
3579         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3580                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3581         }
3582         else {
3583                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3584         }
3585
3586         *l_tile_len +=  l_current_read_size;
3587
3588         return OPJ_TRUE;
3589 }
3590
3591
3592 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3593         opj_cp_t *cp = j2k->cp;
3594         opj_tcp_t *tcp = &cp->tcps[tileno];
3595         opj_cio_t *cio = j2k->cio;
3596         int numcomps = j2k->image->numcomps;
3597         
3598         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
3599         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
3600         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
3601         cio_write(cio, 0, 1);                                                           /* Srgn */
3602         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
3603 }
3604
3605 static void j2k_read_rgn(opj_j2k_t *j2k) {
3606         int len, compno, roisty;
3607
3608         opj_cp_t *cp = j2k->cp;
3609         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3610         opj_cio_t *cio = j2k->cio;
3611         int numcomps = j2k->image->numcomps;
3612
3613         len = cio_read(cio, 2);                                                                         /* Lrgn */
3614         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
3615         roisty = cio_read(cio, 1);                                                                      /* Srgn */
3616
3617 #ifdef USE_JPWL
3618         if (j2k->cp->correct) {
3619                 /* totlen is negative or larger than the bytes left!!! */
3620                 if (compno >= numcomps) {
3621                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3622                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3623                                 compno, numcomps);
3624                         if (!JPWL_ASSUME || JPWL_ASSUME) {
3625                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3626                                 return;
3627                         }
3628                 }
3629         };
3630 #endif /* USE_JPWL */
3631
3632         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
3633 }
3634
3635 static void j2k_write_eoc(opj_j2k_t *j2k) {
3636         opj_cio_t *cio = j2k->cio;
3637         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3638         cio_write(cio, J2K_MS_EOC, 2);
3639
3640 /* UniPG>> */
3641 #ifdef USE_JPWL
3642         /* update markers struct */
3643         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3644 #endif /* USE_JPWL */
3645 /* <<UniPG */
3646 }
3647
3648 /**
3649  * Reads a RGN marker (Region Of Interest)
3650  *
3651  * @param       p_header_data   the data contained in the POC box.
3652  * @param       p_j2k                   the jpeg2000 codec.
3653  * @param       p_header_size   the size of the data contained in the POC marker.
3654  * @param       p_manager               the user event manager.
3655 */
3656 opj_bool j2k_read_rgn_v2 (
3657                                                 opj_j2k_v2_t *p_j2k,
3658                                                 OPJ_BYTE * p_header_data,
3659                                                 OPJ_UINT32 p_header_size,
3660                                                 struct opj_event_mgr * p_manager
3661                                         )
3662 {
3663         OPJ_UINT32 l_nb_comp;
3664         opj_image_header_t * l_image = 00;
3665
3666         opj_cp_v2_t *l_cp = 00;
3667         opj_tcp_v2_t *l_tcp = 00;
3668         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
3669
3670         // preconditions
3671         assert(p_header_data != 00);
3672         assert(p_j2k != 00);
3673         assert(p_manager != 00);
3674
3675         l_image = p_j2k->m_image_header;
3676         l_nb_comp = l_image->numcomps;
3677
3678         if (l_nb_comp <= 256) {
3679                 l_comp_room = 1; }
3680         else {
3681                 l_comp_room = 2; }
3682
3683         if (p_header_size != 2 + l_comp_room) {
3684                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
3685                 return OPJ_FALSE;
3686         }
3687
3688         l_cp = &(p_j2k->m_cp);
3689         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
3690                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3691                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3692
3693         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
3694         p_header_data+=l_comp_room;
3695         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
3696         ++p_header_data;
3697
3698 #ifdef USE_JPWL
3699         if (p_j2k->m_cp->correct) {
3700                 /* totlen is negative or larger than the bytes left!!! */
3701                 if (compno >= numcomps) {
3702                         opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3703                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3704                                 compno, numcomps);
3705                         if (!JPWL_ASSUME || JPWL_ASSUME) {
3706                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3707                                 return;
3708                         }
3709                 }
3710         };
3711 #endif /* USE_JPWL */
3712
3713         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
3714         ++p_header_data;
3715
3716         return OPJ_TRUE;
3717
3718 }
3719
3720 static void j2k_read_eoc(opj_j2k_t *j2k) {
3721         int i, tileno;
3722         opj_bool success;
3723
3724         /* if packets should be decoded */
3725         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
3726                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
3727                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
3728                 for (i = 0; i < j2k->cp->tileno_size; i++) {
3729                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
3730                         tileno = j2k->cp->tileno[i];
3731                         success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
3732                         opj_free(j2k->tile_data[tileno]);
3733                         j2k->tile_data[tileno] = NULL;
3734                         tcd_free_decode_tile(tcd, i);
3735                         if (success == OPJ_FALSE) {
3736                                 j2k->state |= J2K_STATE_ERR;
3737                                 break;
3738                         }
3739                 }
3740                 tcd_free_decode(tcd);
3741                 tcd_destroy(tcd);
3742         }
3743         /* if packets should not be decoded  */
3744         else {
3745                 for (i = 0; i < j2k->cp->tileno_size; i++) {
3746                         tileno = j2k->cp->tileno[i];
3747                         opj_free(j2k->tile_data[tileno]);
3748                         j2k->tile_data[tileno] = NULL;
3749                 }
3750         }       
3751         if (j2k->state & J2K_STATE_ERR)
3752                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
3753         else
3754                 j2k->state = J2K_STATE_MT; 
3755 }
3756
3757 /**
3758  * Reads a EOC marker (End Of Codestream)
3759  *
3760  * @param       p_header_data   the data contained in the SOD box.
3761  * @param       p_j2k                   the jpeg2000 codec.
3762  * @param       p_header_size   the size of the data contained in the SOD marker.
3763  * @param       p_manager               the user event manager.
3764 */
3765 opj_bool j2k_read_eoc_v2 (
3766                                                 opj_j2k_v2_t *p_j2k,
3767                                                 struct opj_stream_private *p_stream,
3768                                                 struct opj_event_mgr * p_manager
3769                                         )
3770 {
3771         OPJ_UINT32 i;
3772         opj_tcd_v2_t * l_tcd = 00;
3773         OPJ_UINT32 l_nb_tiles;
3774         opj_tcp_v2_t * l_tcp = 00;
3775         opj_bool l_success;
3776
3777         // preconditions
3778         assert(p_j2k != 00);
3779         assert(p_manager != 00);
3780         assert(p_stream != 00);
3781
3782         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3783         l_tcp = p_j2k->m_cp.tcps;
3784
3785         l_tcd = tcd_create_v2(OPJ_TRUE);
3786         if (l_tcd == 00) {
3787                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3788                 return OPJ_FALSE;
3789         }
3790
3791         for (i = 0; i < l_nb_tiles; ++i) {
3792                 if (l_tcp->m_data) {
3793                         if (! tcd_init_decode_tile(l_tcd, i)) {
3794                                 tcd_destroy_v2(l_tcd);
3795                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3796                                 return OPJ_FALSE;
3797                         }
3798
3799                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_info);
3800                         /* cleanup */
3801
3802                         if (! l_success) {
3803                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; // TODO MSD J2K_DEC_STATE_ERR;
3804                                 break;
3805                         }
3806                 }
3807
3808                 j2k_tcp_destroy(l_tcp);
3809                 ++l_tcp;
3810         }
3811
3812         tcd_destroy_v2(l_tcd);
3813         return OPJ_TRUE;
3814 }
3815
3816 typedef struct opj_dec_mstabent {
3817         /** marker value */
3818         int id;
3819         /** value of the state when the marker can appear */
3820         int states;
3821         /** action linked to the marker */
3822         void (*handler) (opj_j2k_t *j2k);
3823 } opj_dec_mstabent_t;
3824
3825 opj_dec_mstabent_t j2k_dec_mstab[] = {
3826   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
3827   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
3828   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
3829   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
3830   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
3831   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
3832   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
3833   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
3834   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
3835   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
3836   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
3837   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
3838   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
3839   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
3840   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
3841   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
3842   {J2K_MS_SOP, 0, 0},
3843   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
3844   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
3845
3846 #ifdef USE_JPWL
3847   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
3848   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
3849   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
3850   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
3851 #endif /* USE_JPWL */
3852 #ifdef USE_JPSEC
3853   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
3854   {J2K_MS_INSEC, 0, j2k_read_insec},
3855 #endif /* USE_JPSEC */
3856
3857   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
3858 };
3859
3860 static void j2k_read_unk(opj_j2k_t *j2k) {
3861         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
3862
3863 #ifdef USE_JPWL
3864         if (j2k->cp->correct) {
3865                 int m = 0, id, i;
3866                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
3867                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3868                 id = cio_read(j2k->cio, 2);
3869                 opj_event_msg(j2k->cinfo, EVT_ERROR,
3870                         "JPWL: really don't know this marker %x\n",
3871                         id);
3872                 if (!JPWL_ASSUME) {
3873                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3874                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
3875                         return;
3876                 }
3877                 /* OK, activate this at your own risk!!! */
3878                 /* we look for the marker at the minimum hamming distance from this */
3879                 while (j2k_dec_mstab[m].id) {
3880                         
3881                         /* 1's where they differ */
3882                         tmp_id = j2k_dec_mstab[m].id ^ id;
3883
3884                         /* compute the hamming distance between our id and the current */
3885                         cur_dist = 0;
3886                         for (i = 0; i < 16; i++) {
3887                                 if ((tmp_id >> i) & 0x0001) {
3888                                         cur_dist++;
3889                                 }
3890                         }
3891
3892                         /* if current distance is smaller, set the minimum */
3893                         if (cur_dist < min_dist) {
3894                                 min_dist = cur_dist;
3895                                 min_id = j2k_dec_mstab[m].id;
3896                         }
3897                         
3898                         /* jump to the next marker */
3899                         m++;
3900                 }
3901
3902                 /* do we substitute the marker? */
3903                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
3904                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3905                                 "- marker %x is at distance %d from the read %x\n",
3906                                 min_id, min_dist, id);
3907                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3908                                 "- trying to substitute in place and crossing fingers!\n");
3909                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3910                         cio_write(j2k->cio, min_id, 2);
3911
3912                         /* rewind */
3913                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3914
3915                 }
3916
3917         };
3918 #endif /* USE_JPWL */
3919
3920 }
3921
3922 /**
3923  * Reads an unknown marker
3924  *
3925  * @param       p_stream                                the stream object to read from.
3926  * @param       p_j2k                   the jpeg2000 codec.
3927  * @param       p_manager               the user event manager.
3928  *
3929  * @return      true                    if the marker could be deduced.
3930 */
3931 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
3932                                                         struct opj_stream_private *p_stream,
3933                                                         struct opj_event_mgr * p_manager
3934                                                         )
3935 {
3936         OPJ_BYTE l_data [2];
3937         OPJ_UINT32 l_unknown_size, l_unknown_marker;
3938         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
3939
3940         // preconditions
3941         assert(p_j2k != 00);
3942         assert(p_manager != 00);
3943         assert(p_stream != 00);
3944
3945         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3946
3947
3948
3949 /*      if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
3950                 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3951                 return OPJ_FALSE;
3952         }
3953
3954         opj_read_bytes(l_data,&l_unknown_size,2);
3955         if (l_unknown_size < 2) {
3956                 return OPJ_FALSE;
3957         }
3958         l_unknown_size-=2;
3959
3960         if (opj_stream_skip(p_stream,l_unknown_size,p_manager) != l_unknown_size) {
3961                 return OPJ_FALSE;
3962         }*/
3963         while(1) {
3964                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
3965                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
3966                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
3967                         return OPJ_FALSE;
3968                 }
3969
3970                 // read 2 bytes as the new marker ID
3971                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
3972
3973                 if (!(l_unknown_marker < 0xff00)) {
3974
3975                         // Get the marker handler from the marker ID
3976                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
3977
3978                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
3979                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
3980                                 return OPJ_FALSE;
3981                         }
3982                         else {
3983                                 if (l_marker_handler->id != J2K_MS_UNK)
3984                                         break; // next marker is known and well located
3985                         }
3986                 }
3987         }
3988
3989         return OPJ_TRUE;
3990 }
3991
3992 /**
3993 Read the lookup table containing all the marker, status and action
3994 @param id Marker value
3995 */
3996 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
3997         opj_dec_mstabent_t *e;
3998         for (e = j2k_dec_mstab; e->id != 0; e++) {
3999                 if (e->id == id) {
4000                         break;
4001                 }
4002         }
4003         return e;
4004 }
4005
4006 /* ----------------------------------------------------------------------- */
4007 /* J2K / JPT decoder interface                                             */
4008 /* ----------------------------------------------------------------------- */
4009
4010 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4011         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4012         if(!j2k)
4013                 return NULL;
4014
4015         j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4016         if(!j2k->default_tcp) {
4017                 opj_free(j2k);
4018                 return NULL;
4019         }
4020
4021         j2k->cinfo = cinfo;
4022         j2k->tile_data = NULL;
4023
4024         return j2k;
4025 }
4026
4027 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4028         int i = 0;
4029
4030         if(j2k->tile_len != NULL) {
4031                 opj_free(j2k->tile_len);
4032         }
4033         if(j2k->tile_data != NULL) {
4034                 opj_free(j2k->tile_data);
4035         }
4036         if(j2k->default_tcp != NULL) {
4037                 opj_tcp_t *default_tcp = j2k->default_tcp;
4038                 if(default_tcp->ppt_data_first != NULL) {
4039                         opj_free(default_tcp->ppt_data_first);
4040                 }
4041                 if(j2k->default_tcp->tccps != NULL) {
4042                         opj_free(j2k->default_tcp->tccps);
4043                 }
4044                 opj_free(j2k->default_tcp);
4045         }
4046         if(j2k->cp != NULL) {
4047                 opj_cp_t *cp = j2k->cp;
4048                 if(cp->tcps != NULL) {
4049                         for(i = 0; i < cp->tw * cp->th; i++) {
4050                                 if(cp->tcps[i].ppt_data_first != NULL) {
4051                                         opj_free(cp->tcps[i].ppt_data_first);
4052                                 }
4053                                 if(cp->tcps[i].tccps != NULL) {
4054                                         opj_free(cp->tcps[i].tccps);
4055                                 }
4056                         }
4057                         opj_free(cp->tcps);
4058                 }
4059                 if(cp->ppm_data_first != NULL) {
4060                         opj_free(cp->ppm_data_first);
4061                 }
4062                 if(cp->tileno != NULL) {
4063                         opj_free(cp->tileno);  
4064                 }
4065                 if(cp->comment != NULL) {
4066                         opj_free(cp->comment);
4067                 }
4068
4069                 opj_free(cp);
4070         }
4071         opj_free(j2k);
4072 }
4073
4074 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4075         if(j2k && parameters) {
4076                 /* create and initialize the coding parameters structure */
4077                 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4078                 cp->reduce = parameters->cp_reduce;     
4079                 cp->layer = parameters->cp_layer;
4080                 cp->limit_decoding = parameters->cp_limit_decoding;
4081
4082 #ifdef USE_JPWL
4083                 cp->correct = parameters->jpwl_correct;
4084                 cp->exp_comps = parameters->jpwl_exp_comps;
4085                 cp->max_tiles = parameters->jpwl_max_tiles;
4086 #endif /* USE_JPWL */
4087
4088
4089                 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4090                 j2k->cp = cp;
4091         }
4092 }
4093
4094 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) {
4095         if(j2k && parameters) {
4096                 /* create and initialize the coding parameters structure */
4097                 //opj_cp_v2_t *cp = (opj_cp_v2_t*) opj_calloc(1, sizeof(opj_cp_v2_t));
4098                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4099                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4100
4101                 /*cp->reduce = parameters->cp_reduce;
4102                 cp->layer = parameters->cp_layer;
4103                 cp->limit_decoding = parameters->cp_limit_decoding;*/
4104
4105                 // TODO MS
4106 #ifdef USE_JPWL
4107                 cp->correct = parameters->jpwl_correct;
4108                 cp->exp_comps = parameters->jpwl_exp_comps;
4109                 cp->max_tiles = parameters->jpwl_max_tiles;
4110 #endif /* USE_JPWL */
4111
4112         }
4113 }
4114
4115 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4116         opj_image_t *image = NULL;
4117
4118         opj_common_ptr cinfo = j2k->cinfo;      
4119
4120         j2k->cio = cio;
4121         j2k->cstr_info = cstr_info;
4122         if (cstr_info)
4123                 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4124
4125         /* create an empty image */
4126         image = opj_image_create0();
4127         j2k->image = image;
4128
4129         j2k->state = J2K_STATE_MHSOC;
4130
4131         for (;;) {
4132                 opj_dec_mstabent_t *e;
4133                 int id = cio_read(cio, 2);
4134
4135 #ifdef USE_JPWL
4136                 /* we try to honor JPWL correction power */
4137                 if (j2k->cp->correct) {
4138
4139                         int orig_pos = cio_tell(cio);
4140                         opj_bool status;
4141
4142                         /* call the corrector */
4143                         status = jpwl_correct(j2k);
4144
4145                         /* go back to where you were */
4146                         cio_seek(cio, orig_pos - 2);
4147
4148                         /* re-read the marker */
4149                         id = cio_read(cio, 2);
4150
4151                         /* check whether it begins with ff */
4152                         if (id >> 8 != 0xff) {
4153                                 opj_event_msg(cinfo, EVT_ERROR,
4154                                         "JPWL: possible bad marker %x at %d\n",
4155                                         id, cio_tell(cio) - 2);
4156                                 if (!JPWL_ASSUME) {
4157                                         opj_image_destroy(image);
4158                                         opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
4159                                         return 0;
4160                                 }
4161                                 /* we try to correct */
4162                                 id = id | 0xff00;
4163                                 cio_seek(cio, cio_tell(cio) - 2);
4164                                 cio_write(cio, id, 2);
4165                                 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
4166                                         "- setting marker to %x\n",
4167                                         id);
4168                         }
4169
4170                 }
4171 #endif /* USE_JPWL */
4172
4173                 if (id >> 8 != 0xff) {
4174                         opj_image_destroy(image);
4175                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4176                         return 0;
4177                 }
4178                 e = j2k_dec_mstab_lookup(id);
4179                 // Check if the marker is known
4180                 if (!(j2k->state & e->states)) {
4181                         opj_image_destroy(image);
4182                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4183                         return 0;
4184                 }
4185                 // Check if the decoding is limited to the main header
4186                 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
4187                         opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
4188                         return image;
4189                 }               
4190
4191                 if (e->handler) {
4192                         (*e->handler)(j2k);
4193                 }
4194                 if (j2k->state & J2K_STATE_ERR) 
4195                         return NULL;    
4196
4197                 if (j2k->state == J2K_STATE_MT) {
4198                         break;
4199                 }
4200                 if (j2k->state == J2K_STATE_NEOC) {
4201                         break;
4202                 }
4203         }
4204         if (j2k->state == J2K_STATE_NEOC) {
4205                 j2k_read_eoc(j2k);
4206         }
4207
4208         if (j2k->state != J2K_STATE_MT) {
4209                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4210         }
4211         return image;
4212 }
4213
4214 /*
4215 * Read a JPT-stream and decode file
4216 *
4217 */
4218 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
4219         opj_image_t *image = NULL;
4220         opj_jpt_msg_header_t header;
4221         int position;
4222         opj_common_ptr cinfo = j2k->cinfo;
4223
4224         OPJ_ARG_NOT_USED(cstr_info);
4225
4226         j2k->cio = cio;
4227
4228         /* create an empty image */
4229         image = opj_image_create0();
4230         j2k->image = image;
4231
4232         j2k->state = J2K_STATE_MHSOC;
4233         
4234         /* Initialize the header */
4235         jpt_init_msg_header(&header);
4236         /* Read the first header of the message */
4237         jpt_read_msg_header(cinfo, cio, &header);
4238         
4239         position = cio_tell(cio);
4240         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
4241                 opj_image_destroy(image);
4242                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
4243                 return 0;
4244         }
4245         
4246         for (;;) {
4247                 opj_dec_mstabent_t *e = NULL;
4248                 int id;
4249                 
4250                 if (!cio_numbytesleft(cio)) {
4251                         j2k_read_eoc(j2k);
4252                         return image;
4253                 }
4254                 /* data-bin read -> need to read a new header */
4255                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
4256                         jpt_read_msg_header(cinfo, cio, &header);
4257                         position = cio_tell(cio);
4258                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
4259                                 opj_image_destroy(image);
4260                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
4261                                 return 0;
4262                         }
4263                 }
4264                 
4265                 id = cio_read(cio, 2);
4266                 if (id >> 8 != 0xff) {
4267                         opj_image_destroy(image);
4268                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4269                         return 0;
4270                 }
4271                 e = j2k_dec_mstab_lookup(id);
4272                 if (!(j2k->state & e->states)) {
4273                         opj_image_destroy(image);
4274                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4275                         return 0;
4276                 }
4277                 if (e->handler) {
4278                         (*e->handler)(j2k);
4279                 }
4280                 if (j2k->state == J2K_STATE_MT) {
4281                         break;
4282                 }
4283                 if (j2k->state == J2K_STATE_NEOC) {
4284                         break;
4285                 }
4286         }
4287         if (j2k->state == J2K_STATE_NEOC) {
4288                 j2k_read_eoc(j2k);
4289         }
4290         
4291         if (j2k->state != J2K_STATE_MT) {
4292                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4293         }
4294
4295         return image;
4296 }
4297
4298 /* ----------------------------------------------------------------------- */
4299 /* J2K encoder interface                                                       */
4300 /* ----------------------------------------------------------------------- */
4301
4302 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
4303         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4304         if(j2k) {
4305                 j2k->cinfo = cinfo;
4306         }
4307         return j2k;
4308 }
4309
4310 opj_j2k_v2_t* j2k_create_compress_v2()
4311 {
4312         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
4313         if (!l_j2k) {
4314                 return NULL;
4315         }
4316
4317         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
4318
4319         l_j2k->m_is_decoder = 0;
4320         l_j2k->m_cp.m_is_decoder = 0;
4321
4322         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
4323         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4324                 j2k_destroy(l_j2k);
4325                 return NULL;
4326         }
4327
4328         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
4329
4330         // validation list creation
4331         l_j2k->m_validation_list = opj_procedure_list_create();
4332         if (! l_j2k->m_validation_list) {
4333                 j2k_destroy(l_j2k);
4334                 return NULL;
4335         }
4336
4337         // execution list creation
4338         l_j2k->m_procedure_list = opj_procedure_list_create();
4339         if (! l_j2k->m_procedure_list) {
4340                 j2k_destroy(l_j2k);
4341                 return NULL;
4342         }
4343
4344         return l_j2k;
4345 }
4346
4347 void j2k_destroy_compress(opj_j2k_t *j2k) {
4348         int tileno;
4349
4350         if(!j2k) return;
4351         if(j2k->cp != NULL) {
4352                 opj_cp_t *cp = j2k->cp;
4353
4354                 if(cp->comment) {
4355                         opj_free(cp->comment);
4356                 }
4357                 if(cp->matrice) {
4358                         opj_free(cp->matrice);
4359                 }
4360                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4361                         opj_free(cp->tcps[tileno].tccps);
4362                 }
4363                 opj_free(cp->tcps);
4364                 opj_free(cp);
4365         }
4366
4367         opj_free(j2k);
4368 }
4369
4370 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
4371         int i, j, tileno, numpocs_tile;
4372         opj_cp_t *cp = NULL;
4373
4374         if(!j2k || !parameters || ! image) {
4375                 return;
4376         }
4377
4378         /* create and initialize the coding parameters structure */
4379         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4380
4381         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
4382         j2k->cp = cp;
4383
4384         /* set default values for cp */
4385         cp->tw = 1;
4386         cp->th = 1;
4387
4388         /* 
4389         copy user encoding parameters 
4390         */
4391         cp->cinema = parameters->cp_cinema;
4392         cp->max_comp_size =     parameters->max_comp_size;
4393         cp->rsiz   = parameters->cp_rsiz;
4394         cp->disto_alloc = parameters->cp_disto_alloc;
4395         cp->fixed_alloc = parameters->cp_fixed_alloc;
4396         cp->fixed_quality = parameters->cp_fixed_quality;
4397
4398         /* mod fixed_quality */
4399         if(parameters->cp_matrice) {
4400                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
4401                 cp->matrice = (int *) opj_malloc(array_size);
4402                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
4403         }
4404
4405         /* tiles */
4406         cp->tdx = parameters->cp_tdx;
4407         cp->tdy = parameters->cp_tdy;
4408
4409         /* tile offset */
4410         cp->tx0 = parameters->cp_tx0;
4411         cp->ty0 = parameters->cp_ty0;
4412
4413         /* comment string */
4414         if(parameters->cp_comment) {
4415                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
4416                 if(cp->comment) {
4417                         strcpy(cp->comment, parameters->cp_comment);
4418                 }
4419         }
4420
4421         /*
4422         calculate other encoding parameters
4423         */
4424
4425         if (parameters->tile_size_on) {
4426                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
4427                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
4428         } else {
4429                 cp->tdx = image->x1 - cp->tx0;
4430                 cp->tdy = image->y1 - cp->ty0;
4431         }
4432
4433         if(parameters->tp_on){
4434                 cp->tp_flag = parameters->tp_flag;
4435                 cp->tp_on = 1;
4436         }
4437         
4438         cp->img_size = 0;
4439         for(i=0;i<image->numcomps ;i++){
4440         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
4441         }
4442
4443
4444 #ifdef USE_JPWL
4445         /*
4446         calculate JPWL encoding parameters
4447         */
4448
4449         if (parameters->jpwl_epc_on) {
4450                 int i;
4451
4452                 /* set JPWL on */
4453                 cp->epc_on = OPJ_TRUE;
4454                 cp->info_on = OPJ_FALSE; /* no informative technique */
4455
4456                 /* set EPB on */
4457                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
4458                         cp->epb_on = OPJ_TRUE;
4459                         
4460                         cp->hprot_MH = parameters->jpwl_hprot_MH;
4461                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4462                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
4463                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
4464                         }
4465                         /* if tile specs are not specified, copy MH specs */
4466                         if (cp->hprot_TPH[0] == -1) {
4467                                 cp->hprot_TPH_tileno[0] = 0;
4468                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
4469                         }
4470                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
4471                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
4472                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
4473                                 cp->pprot[i] = parameters->jpwl_pprot[i];
4474                         }
4475                 }
4476
4477                 /* set ESD writing */
4478                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
4479                         cp->esd_on = OPJ_TRUE;
4480
4481                         cp->sens_size = parameters->jpwl_sens_size;
4482                         cp->sens_addr = parameters->jpwl_sens_addr;
4483                         cp->sens_range = parameters->jpwl_sens_range;
4484
4485                         cp->sens_MH = parameters->jpwl_sens_MH;
4486                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4487                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
4488                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
4489                         }
4490                 }
4491
4492                 /* always set RED writing to false: we are at the encoder */
4493                 cp->red_on = OPJ_FALSE;
4494
4495         } else {
4496                 cp->epc_on = OPJ_FALSE;
4497         }
4498 #endif /* USE_JPWL */
4499
4500
4501         /* initialize the mutiple tiles */
4502         /* ---------------------------- */
4503         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
4504
4505         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4506                 opj_tcp_t *tcp = &cp->tcps[tileno];
4507                 tcp->numlayers = parameters->tcp_numlayers;
4508                 for (j = 0; j < tcp->numlayers; j++) {
4509                         if(cp->cinema){
4510                                 if (cp->fixed_quality) {
4511                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
4512                                 }
4513                                 tcp->rates[j] = parameters->tcp_rates[j];
4514                         }else{
4515                                 if (cp->fixed_quality) {        /* add fixed_quality */
4516                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
4517                                 } else {
4518                                         tcp->rates[j] = parameters->tcp_rates[j];
4519                                 }
4520                         }
4521                 }
4522                 tcp->csty = parameters->csty;
4523                 tcp->prg = parameters->prog_order;
4524                 tcp->mct = parameters->tcp_mct; 
4525
4526                 numpocs_tile = 0;
4527                 tcp->POC = 0;
4528                 if (parameters->numpocs) {
4529                         /* initialisation of POC */
4530                         tcp->POC = 1;
4531                         for (i = 0; i < parameters->numpocs; i++) {
4532                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
4533                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
4534                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
4535                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
4536                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
4537                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
4538                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
4539                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
4540                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
4541                                         numpocs_tile++;
4542                                 }
4543                         }
4544                         tcp->numpocs = numpocs_tile -1 ;
4545                 }else{ 
4546                         tcp->numpocs = 0;
4547                 }
4548
4549                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
4550
4551                 for (i = 0; i < image->numcomps; i++) {
4552                         opj_tccp_t *tccp = &tcp->tccps[i];
4553                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
4554                         tccp->numresolutions = parameters->numresolution;
4555                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
4556                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
4557                         tccp->cblksty = parameters->mode;
4558                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
4559                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
4560                         tccp->numgbits = 2;
4561                         if (i == parameters->roi_compno) {
4562                                 tccp->roishift = parameters->roi_shift;
4563                         } else {
4564                                 tccp->roishift = 0;
4565                         }
4566
4567                         if(parameters->cp_cinema)
4568                         {
4569                                 //Precinct size for lowest frequency subband=128
4570                                 tccp->prcw[0] = 7;
4571                                 tccp->prch[0] = 7;
4572                                 //Precinct size at all other resolutions = 256
4573                                 for (j = 1; j < tccp->numresolutions; j++) {
4574                                         tccp->prcw[j] = 8;
4575                                         tccp->prch[j] = 8;
4576                                 }
4577                         }else{
4578                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
4579                                         int p = 0;
4580                                         for (j = tccp->numresolutions - 1; j >= 0; j--) {
4581                                                 if (p < parameters->res_spec) {
4582                                                         
4583                                                         if (parameters->prcw_init[p] < 1) {
4584                                                                 tccp->prcw[j] = 1;
4585                                                         } else {
4586                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
4587                                                         }
4588                                                         
4589                                                         if (parameters->prch_init[p] < 1) {
4590                                                                 tccp->prch[j] = 1;
4591                                                         }else {
4592                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
4593                                                         }
4594
4595                                                 } else {
4596                                                         int res_spec = parameters->res_spec;
4597                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
4598                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
4599                                                         
4600                                                         if (size_prcw < 1) {
4601                                                                 tccp->prcw[j] = 1;
4602                                                         } else {
4603                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
4604                                                         }
4605                                                         
4606                                                         if (size_prch < 1) {
4607                                                                 tccp->prch[j] = 1;
4608                                                         } else {
4609                                                                 tccp->prch[j] = int_floorlog2(size_prch);
4610                                                         }
4611                                                 }
4612                                                 p++;
4613                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
4614                                         }       //end for
4615                                 } else {
4616                                         for (j = 0; j < tccp->numresolutions; j++) {
4617                                                 tccp->prcw[j] = 15;
4618                                                 tccp->prch[j] = 15;
4619                                         }
4620                                 }
4621                         }
4622
4623                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
4624                 }
4625         }
4626 }
4627
4628 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
4629         int tileno, compno;
4630         opj_cp_t *cp = NULL;
4631
4632         opj_tcd_t *tcd = NULL;  /* TCD component */
4633
4634         j2k->cio = cio; 
4635         j2k->image = image;
4636
4637         cp = j2k->cp;
4638
4639         /* INDEX >> */
4640         j2k->cstr_info = cstr_info;
4641         if (cstr_info) {
4642                 int compno;
4643                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
4644                 cstr_info->image_w = image->x1 - image->x0;
4645                 cstr_info->image_h = image->y1 - image->y0;
4646                 cstr_info->prog = (&cp->tcps[0])->prg;
4647                 cstr_info->tw = cp->tw;
4648                 cstr_info->th = cp->th;
4649                 cstr_info->tile_x = cp->tdx;    /* new version parser */
4650                 cstr_info->tile_y = cp->tdy;    /* new version parser */
4651                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
4652                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
4653                 cstr_info->numcomps = image->numcomps;
4654                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
4655                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
4656                 for (compno=0; compno < image->numcomps; compno++) {
4657                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
4658                 }
4659                 cstr_info->D_max = 0.0;         /* ADD Marcela */
4660                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
4661                 cstr_info->maxmarknum = 100;
4662                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
4663                 cstr_info->marknum = 0;
4664         }
4665         /* << INDEX */
4666
4667         j2k_write_soc(j2k);
4668         j2k_write_siz(j2k);
4669         j2k_write_cod(j2k);
4670         j2k_write_qcd(j2k);
4671
4672         if(cp->cinema){
4673                 for (compno = 1; compno < image->numcomps; compno++) {
4674                         j2k_write_coc(j2k, compno);
4675                         j2k_write_qcc(j2k, compno);
4676                 }
4677         }
4678
4679         for (compno = 0; compno < image->numcomps; compno++) {
4680                 opj_tcp_t *tcp = &cp->tcps[0];
4681                 if (tcp->tccps[compno].roishift)
4682                         j2k_write_rgn(j2k, compno, 0);
4683         }
4684         if (cp->comment != NULL) {
4685                 j2k_write_com(j2k);
4686         }
4687
4688         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
4689         /* TLM Marker*/
4690         if(cp->cinema){
4691                 j2k_write_tlm(j2k);
4692                 if (cp->cinema == CINEMA4K_24) {
4693                         j2k_write_poc(j2k);
4694                 }
4695         }
4696
4697         /* uncomment only for testing JPSEC marker writing */
4698         /* j2k_write_sec(j2k); */
4699
4700         /* INDEX >> */
4701         if(cstr_info) {
4702                 cstr_info->main_head_end = cio_tell(cio) - 1;
4703         }
4704         /* << INDEX */
4705         /**** Main Header ENDS here ***/
4706
4707         /* create the tile encoder */
4708         tcd = tcd_create(j2k->cinfo);
4709
4710         /* encode each tile */
4711         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4712                 int pino;
4713                 int tilepartno=0;
4714                 /* UniPG>> */
4715                 int acc_pack_num = 0;
4716                 /* <<UniPG */
4717
4718
4719                 opj_tcp_t *tcp = &cp->tcps[tileno];
4720                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
4721
4722                 j2k->curtileno = tileno;
4723                 j2k->cur_tp_num = 0;
4724                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
4725                 /* initialisation before tile encoding  */
4726                 if (tileno == 0) {
4727                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
4728                 } else {
4729                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
4730                 }
4731
4732                 /* INDEX >> */
4733                 if(cstr_info) {
4734                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
4735                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
4736                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
4737                         cstr_info->tile[j2k->curtileno].marknum = 0;
4738                 }
4739                 /* << INDEX */
4740
4741                 for(pino = 0; pino <= tcp->numpocs; pino++) {
4742                         int tot_num_tp;
4743                         tcd->cur_pino=pino;
4744
4745                         /*Get number of tile parts*/
4746                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
4747                         tcd->tp_pos = cp->tp_pos;
4748
4749                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
4750                                 j2k->tp_num = tilepartno;
4751                                 /* INDEX >> */
4752                                 if(cstr_info)
4753                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
4754                                         cio_tell(cio) + j2k->pos_correction;
4755                                 /* << INDEX */
4756                                 j2k_write_sot(j2k);
4757
4758                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
4759                                         for (compno = 1; compno < image->numcomps; compno++) {
4760                                                 j2k_write_coc(j2k, compno);
4761                                                 j2k_write_qcc(j2k, compno);
4762                                         }
4763                                         if (cp->tcps[tileno].numpocs) {
4764                                                 j2k_write_poc(j2k);
4765                                         }
4766                                 }
4767
4768                                 /* INDEX >> */
4769                                 if(cstr_info)
4770                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
4771                                         cio_tell(cio) + j2k->pos_correction + 1;
4772                                 /* << INDEX */
4773
4774                                 j2k_write_sod(j2k, tcd);
4775
4776                                 /* INDEX >> */
4777                                 if(cstr_info) {
4778                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
4779                                                 cio_tell(cio) + j2k->pos_correction - 1;
4780                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
4781                                                 acc_pack_num;
4782                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
4783                                                 cstr_info->packno - acc_pack_num;
4784                                         acc_pack_num = cstr_info->packno;
4785                                 }
4786                                 /* << INDEX */
4787
4788                                 j2k->cur_tp_num++;
4789                         }                       
4790                 }
4791                 if(cstr_info) {
4792                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
4793                 }
4794
4795
4796                 /*
4797                 if (tile->PPT) { // BAD PPT !!! 
4798                 FILE *PPT_file;
4799                 int i;
4800                 PPT_file=fopen("PPT","rb");
4801                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
4802                 for (i=0;i<tile->len_ppt;i++) {
4803                 unsigned char elmt;
4804                 fread(&elmt, 1, 1, PPT_file);
4805                 fwrite(&elmt,1,1,f);
4806                 }
4807                 fclose(PPT_file);
4808                 unlink("PPT");
4809                 }
4810                 */
4811
4812         }
4813
4814         /* destroy the tile encoder */
4815         tcd_free_encode(tcd);
4816         tcd_destroy(tcd);
4817
4818         opj_free(j2k->cur_totnum_tp);
4819
4820         j2k_write_eoc(j2k);
4821
4822         if(cstr_info) {
4823                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
4824                 /* UniPG>> */
4825                 /* The following adjustment is done to adjust the codestream size */
4826                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4827                 /* the first bunch of bytes is not in the codestream              */
4828                 cstr_info->codestream_size -= cstr_info->main_head_start;
4829                 /* <<UniPG */
4830         }
4831
4832 #ifdef USE_JPWL
4833         /*
4834         preparation of JPWL marker segments
4835         */
4836         if(cp->epc_on) {
4837
4838                 /* encode according to JPWL */
4839                 jpwl_encode(j2k, cio, image);
4840
4841         }
4842 #endif /* USE_JPWL */
4843
4844         return OPJ_TRUE;
4845 }
4846
4847 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4848
4849         if (!cstr_info)
4850                 return;
4851
4852         /* expand the list? */
4853         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
4854                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
4855                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
4856         }
4857
4858         /* add the marker */
4859         cstr_info->marker[cstr_info->marknum].type = type;
4860         cstr_info->marker[cstr_info->marknum].pos = pos;
4861         cstr_info->marker[cstr_info->marknum].len = len;
4862         cstr_info->marknum++;
4863
4864 }
4865
4866 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4867
4868
4869   opj_marker_info_t *marker;
4870
4871         if (!cstr_info)
4872                 return;
4873
4874         /* expand the list? */
4875         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
4876                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
4877                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
4878         }
4879
4880         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
4881
4882         /* add the marker */
4883         marker->type = type;
4884         marker->pos = pos;
4885         marker->len = len;
4886         cstr_info->tile[tileno].marknum++;
4887 }
4888
4889
4890
4891
4892 /*
4893  * -----------------------------------------------------------------------
4894  * -----------------------------------------------------------------------
4895  * -----------------------------------------------------------------------
4896  */
4897
4898 /**
4899  * Ends the decompression procedures and possibiliy add data to be read after the
4900  * codestream.
4901  */
4902 opj_bool j2k_end_decompress(
4903                                                 opj_j2k_v2_t *p_j2k,
4904                                                 opj_stream_private_t *p_stream,
4905                                                 opj_event_mgr_t * p_manager)
4906 {
4907         return OPJ_TRUE;
4908 }
4909
4910 /**
4911  * Reads a jpeg2000 codestream header structure.
4912
4913  *
4914  * @param p_stream the stream to read data from.
4915  * @param p_j2k the jpeg2000 codec.
4916  * @param p_manager the user event manager.
4917  *
4918  * @return true if the box is valid.
4919  */
4920 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
4921                                                         opj_j2k_v2_t* p_j2k,
4922                                                         opj_file_info_t* p_file_info,
4923                                                         struct opj_event_mgr* p_manager )
4924 {
4925         /* preconditions */
4926         assert(p_j2k != 00);
4927         assert(p_stream != 00);
4928         assert(p_manager != 00);
4929
4930         /* create an empty image header */
4931         p_j2k->m_image_header = opj_image_header_create0();
4932         if (! p_j2k->m_image_header) {
4933                 return OPJ_FALSE;
4934         }
4935
4936         /* customization of the validation */
4937         j2k_setup_decoding_validation(p_j2k);
4938
4939         /* validation of the parameters codec */
4940         if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
4941                 opj_image_header_destroy(p_j2k->m_image_header);
4942                 p_j2k->m_image_header = NULL;
4943                 return OPJ_FALSE;
4944         }
4945
4946         /* customization of the encoding */
4947         j2k_setup_header_reading(p_j2k);
4948
4949         /* read header */
4950         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
4951                 opj_image_header_destroy(p_j2k->m_image_header);
4952                 p_j2k->m_image_header = NULL;
4953                 return OPJ_FALSE;
4954         }
4955
4956         /* If necessary copy j2k header information into the output structure */
4957         if (p_file_info->file_info_flag & OPJ_J2K_INFO){
4958                 fill_cstr_info( &(p_file_info->codestream_info), p_j2k, p_manager);
4959         }
4960
4961         /* If necessary copy image header information into the output structure */
4962         if (p_file_info->file_info_flag & OPJ_IMG_INFO){
4963                 fill_img_info( &(p_file_info->img_info), p_j2k, p_manager);
4964         }
4965
4966         return OPJ_TRUE;
4967 }
4968
4969 /**
4970  * Fill the image info struct from information read from main header.
4971  */
4972 opj_bool fill_img_info(opj_image_header_t* img_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4973 {
4974         opj_image_header_t* l_image_header = p_j2k->m_image_header;
4975
4976         img_info->x0 = l_image_header->x0;
4977         img_info->y0 = l_image_header->y0;
4978         img_info->x1 = l_image_header->x1;
4979         img_info->y1 = l_image_header->y1;
4980         img_info->numcomps = l_image_header->numcomps;
4981
4982         if (img_info->numcomps) {
4983                 img_info->comps = (opj_image_comp_header_t*)opj_malloc(img_info->numcomps * sizeof(opj_image_comp_header_t));
4984                 if (!img_info->comps){
4985                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4986                         return OPJ_FALSE;
4987                 }
4988
4989                 memcpy(img_info->comps, l_image_header->comps, img_info->numcomps * sizeof(opj_image_comp_header_t) );
4990         }
4991
4992
4993         img_info->icc_profile_len = l_image_header->icc_profile_len;
4994         if (img_info->icc_profile_len) {
4995                 img_info->icc_profile_buf = (unsigned char*)opj_malloc(img_info->icc_profile_len * sizeof(unsigned char));
4996                 if (!img_info->icc_profile_buf){
4997                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4998                         return OPJ_FALSE;
4999                 }
5000
5001                 memcpy(img_info->icc_profile_buf, l_image_header->icc_profile_buf, img_info->icc_profile_len);
5002         }
5003
5004         return OPJ_TRUE;
5005 }
5006
5007 /**
5008  * Fill the codestream info struct from information read from main header.
5009  */
5010 opj_bool fill_cstr_info(opj_codestream_info_v2_t* cstr_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
5011 {
5012
5013         //opj_cp_v2_t l_cp = p_j2k->m_cp;
5014         opj_image_header_t* l_image_header = p_j2k->m_image_header;
5015         int it_tile, it_comp;
5016         int nb_tiles, nb_comps;
5017
5018         cstr_info->image_w = l_image_header->x1 - l_image_header->x0;
5019         cstr_info->image_h = l_image_header->y1 - l_image_header->y0;
5020         cstr_info->numcomps = l_image_header->numcomps;
5021         cstr_info->tw = p_j2k->m_cp.tw;
5022         cstr_info->th = p_j2k->m_cp.th;
5023         cstr_info->tdx = p_j2k->m_cp.tdx;
5024         cstr_info->tdy = p_j2k->m_cp.tdy;
5025         cstr_info->tx0 = p_j2k->m_cp.tx0;
5026         cstr_info->ty0 = p_j2k->m_cp.ty0;
5027
5028         nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
5029         nb_comps = l_image_header->numcomps;
5030
5031         cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(nb_tiles, sizeof(opj_tile_info_v2_t));
5032         if (! p_j2k->cstr_info->tile) {
5033                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5034                 return OPJ_FALSE;
5035         }
5036
5037         for (it_tile=0; it_tile < nb_tiles; it_tile++){
5038                 cstr_info->tile[it_tile].tileno = it_tile;
5039                 cstr_info->tile[it_tile].csty = p_j2k->m_cp.tcps[it_tile].csty;
5040                 cstr_info->tile[it_tile].prg = p_j2k->m_cp.tcps[it_tile].prg;
5041                 cstr_info->tile[it_tile].mct = p_j2k->m_cp.tcps[it_tile].mct;
5042                 cstr_info->tile[it_tile].numlayers = p_j2k->m_cp.tcps[it_tile].numlayers;
5043                 // FIXME Number of tile part l_cstr_info->tile[it_tile].num_tps = ;
5044
5045                 cstr_info->tile[it_tile].tccp_info =
5046                         (opj_tccp_info_t*) opj_calloc(nb_comps, sizeof(opj_tccp_info_t));
5047                 if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
5048                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5049                         return OPJ_FALSE;
5050                 }
5051
5052                 for (it_comp=0; it_comp < nb_comps; it_comp++){
5053                         cstr_info->tile[it_tile].tccp_info[it_comp].compno = it_comp;
5054                         cstr_info->tile[it_tile].tccp_info[it_comp].cblkh = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkh;
5055                         cstr_info->tile[it_tile].tccp_info[it_comp].cblkw = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkw;
5056                         cstr_info->tile[it_tile].tccp_info[it_comp].cblksty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblksty;
5057                         cstr_info->tile[it_tile].tccp_info[it_comp].csty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].csty;
5058                         cstr_info->tile[it_tile].tccp_info[it_comp].numgbits = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numgbits;
5059                         cstr_info->tile[it_tile].tccp_info[it_comp].numresolutions = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numresolutions;
5060                         cstr_info->tile[it_tile].tccp_info[it_comp].qmfbid = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qmfbid;
5061                         cstr_info->tile[it_tile].tccp_info[it_comp].qntsty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qntsty;
5062                         cstr_info->tile[it_tile].tccp_info[it_comp].roishift = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].roishift;
5063                 }
5064         }
5065
5066         return OPJ_TRUE;
5067 }
5068
5069
5070
5071 /**
5072  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5073  */
5074 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5075 {
5076         // preconditions
5077         assert(p_j2k != 00);
5078
5079         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5080
5081         /* DEVELOPER CORNER, add your custom procedures */
5082         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5083
5084 }
5085
5086 /**
5087  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5088  * are valid. Developpers wanting to extend the library can add their own validation procedures.
5089  */
5090 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5091 {
5092         // preconditions
5093         assert(p_j2k != 00);
5094
5095         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5096         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5097         /* DEVELOPER CORNER, add your custom validation procedure */
5098
5099 }
5100
5101 /**
5102  * Builds the cp decoder parameters to use to decode tile.
5103  */
5104 opj_bool j2k_build_decoder (
5105                                                 opj_j2k_v2_t * p_j2k,
5106                                                 opj_stream_private_t *p_stream,
5107                                                 opj_event_mgr_t * p_manager
5108                                                 )
5109 {
5110         // add here initialization of cp
5111         // copy paste of setup_decoder
5112         return OPJ_TRUE;
5113 }
5114
5115 /**
5116  * The default decoding validation procedure without any extension.
5117  *
5118  * @param       p_j2k                   the jpeg2000 codec to validate.
5119  * @param       p_stream                                the input stream to validate.
5120  * @param       p_manager               the user event manager.
5121  *
5122  * @return true if the parameters are correct.
5123  */
5124 opj_bool j2k_decoding_validation (
5125                                                                 opj_j2k_v2_t *p_j2k,
5126                                                                 opj_stream_private_t *p_stream,
5127                                                                 opj_event_mgr_t * p_manager
5128                                                           )
5129 {
5130         opj_bool l_is_valid = OPJ_TRUE;
5131
5132         // preconditions
5133         assert(p_j2k != 00);
5134         assert(p_stream != 00);
5135         assert(p_manager != 00);
5136
5137
5138         /* STATE checking */
5139         /* make sure the state is at 0 */
5140 #ifdef TODO_MSD
5141         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5142 #endif
5143         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5144
5145         /* POINTER validation */
5146         /* make sure a p_j2k codec is present */
5147         /* make sure a procedure list is present */
5148         l_is_valid &= (p_j2k->m_procedure_list != 00);
5149         /* make sure a validation list is present */
5150         l_is_valid &= (p_j2k->m_validation_list != 00);
5151
5152         /* PARAMETER VALIDATION */
5153         return l_is_valid;
5154 }
5155
5156 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
5157                                                                         struct opj_stream_private *p_stream,
5158                                                                         struct opj_event_mgr * p_manager)
5159 {
5160         OPJ_UINT32 l_current_marker;
5161         OPJ_UINT32 l_marker_size;
5162         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5163
5164         // preconditions
5165         assert(p_stream != 00);
5166         assert(p_j2k != 00);
5167         assert(p_manager != 00);
5168
5169         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; // The codestream must begin with SOC marker FIXME J2K_DEC_STATE_MHSOC
5170
5171         // Try to read the SOC marker
5172         if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5173                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5174                 return OPJ_FALSE;
5175         }
5176
5177         // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5178         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5179                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5180                 return OPJ_FALSE;
5181         }
5182
5183         // Read 2 bytes as the new marker ID
5184         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5185
5186         /* Try to read until the SOT is detected */
5187         while (l_current_marker != J2K_MS_SOT) {
5188
5189                 /* Check if the current marker ID is valid */
5190                 if (l_current_marker < 0xff00) {
5191                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
5192                         return OPJ_FALSE;
5193                 }
5194
5195                 // Get the marker handler from the marker ID
5196                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5197
5198                 // Check if the marker is known and if it is the right place to find it
5199                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5200                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5201                         return OPJ_FALSE;
5202                 }
5203
5204                 // Manage case where marker is unknown
5205                 if (l_marker_handler->id == J2K_MS_UNK) {
5206
5207                         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5208
5209                         // Try to detect the next valid marker
5210                         while(1) {
5211                                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5212                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5213                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5214                                         return OPJ_FALSE;
5215                                 }
5216
5217                                 // Read 2 bytes as the new marker ID
5218                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5219
5220                                 // Check if the current marker ID is valid
5221                                 if ( !(l_current_marker < 0xff00) ) {
5222                                         // Get the marker handler from the marker ID
5223                                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
5224
5225                                         if ( !(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5226                                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5227                                                 return OPJ_FALSE;
5228                                         }
5229                                         else{
5230                                                 if (l_marker_handler->id != J2K_MS_UNK)
5231                                                         break; // next marker is known and well located
5232                                         }
5233                                 }
5234                         }
5235                         if (l_current_marker == J2K_MS_SOT)
5236                                 break; // SOT marker is detected main header is completely read.
5237                 }
5238
5239                 // Try to read 2 bytes (the marker size) from stream and copy them into the buffer
5240                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5241                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5242                         return OPJ_FALSE;
5243                 }
5244
5245                 // read 2 bytes as the marker size
5246                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5247                 l_marker_size -= 2; // Subtract the size of the marker ID already read
5248
5249                 // Check if the marker size is compatible with the header data size
5250                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5251                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5252                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5253                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5254                                 return OPJ_FALSE;
5255                         }
5256                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5257                 }
5258
5259                 // Try to read the rest of the marker segment from stream and copy them into the buffer
5260                 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) {
5261                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5262                         return OPJ_FALSE;
5263                 }
5264
5265                 // Read the marker segment with the correct marker handler
5266                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5267                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
5268                         return OPJ_FALSE;
5269                 }
5270
5271                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5272                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5273                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5274                         return OPJ_FALSE;
5275                 }
5276
5277                 // read 2 bytes as the new marker ID
5278                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5279         }
5280
5281         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT
5282         return OPJ_TRUE;
5283 }
5284
5285 /**
5286  * Excutes the given procedures on the given codec.
5287  *
5288  * @param       p_procedure_list        the list of procedures to execute
5289  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
5290  * @param       p_stream                                        the stream to execute the procedures on.
5291  * @param       p_manager                       the user manager.
5292  *
5293  * @return      true                            if all the procedures were successfully executed.
5294  */
5295 opj_bool j2k_exec (
5296                                         opj_j2k_v2_t * p_j2k,
5297                                         opj_procedure_list_t * p_procedure_list,
5298                                         opj_stream_private_t *p_stream,
5299                                         opj_event_mgr_t * p_manager
5300                                   )
5301 {
5302         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
5303         opj_bool l_result = OPJ_TRUE;
5304         OPJ_UINT32 l_nb_proc, i;
5305
5306         // preconditions
5307         assert(p_procedure_list != 00);
5308         assert(p_j2k != 00);
5309         assert(p_stream != 00);
5310         assert(p_manager != 00);
5311
5312
5313         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
5314         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);
5315
5316         for     (i=0;i<l_nb_proc;++i) {
5317                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
5318                 ++l_procedure;
5319         }
5320
5321         // and clear the procedure list at the end.
5322         opj_procedure_list_clear(p_procedure_list);
5323         return l_result;
5324 }
5325
5326
5327 opj_bool j2k_copy_default_tcp_and_create_tcd
5328                                                 (
5329                                                 opj_j2k_v2_t * p_j2k,
5330                                                 opj_stream_private_t *p_stream,
5331                                                 opj_event_mgr_t * p_manager
5332                                                 )
5333 {
5334         opj_tcp_v2_t * l_tcp = 00;
5335         opj_tcp_v2_t * l_default_tcp = 00;
5336         OPJ_UINT32 l_nb_tiles;
5337         OPJ_UINT32 i,j;
5338         opj_tccp_t *l_current_tccp = 00;
5339         OPJ_UINT32 l_tccp_size;
5340         OPJ_UINT32 l_mct_size;
5341         opj_image_header_t * l_image;
5342         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
5343         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
5344         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
5345         OPJ_UINT32 l_offset;
5346
5347         // preconditions in debug
5348         assert(p_j2k != 00);
5349         assert(p_stream != 00);
5350         assert(p_manager != 00);
5351
5352
5353         l_image = p_j2k->m_image_header;
5354         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5355         l_tcp = p_j2k->m_cp.tcps;
5356         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
5357         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
5358         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
5359         for
5360                 (i=0;i<l_nb_tiles;++i)
5361         {
5362                 l_current_tccp = l_tcp->tccps;
5363                 memcpy(l_tcp,l_default_tcp, sizeof(opj_tcp_v2_t));
5364                 l_tcp->ppt = 0;
5365                 l_tcp->ppt_data = 00;
5366                 l_tcp->tccps = l_current_tccp;
5367                 if
5368                         (l_default_tcp->m_mct_decoding_matrix)
5369                 {
5370                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5371                         if
5372                                 (! l_tcp->m_mct_decoding_matrix )
5373                         {
5374                                 return OPJ_FALSE;
5375                         }
5376                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
5377                 }
5378                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
5379                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
5380                 if
5381                         (! l_tcp->m_mct_records)
5382                 {
5383                         return OPJ_FALSE;
5384                 }
5385                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
5386                 l_src_mct_rec = l_default_tcp->m_mct_records;
5387                 l_dest_mct_rec = l_tcp->m_mct_records;
5388                 for
5389                         (j=0;j<l_default_tcp->m_nb_mct_records;++j)
5390                 {
5391                         if
5392                                 (l_src_mct_rec->m_data)
5393                         {
5394                                 l_dest_mct_rec->m_data = (OPJ_BYTE*)
5395                                 opj_malloc(l_src_mct_rec->m_data_size);
5396                                 if
5397                                         (! l_dest_mct_rec->m_data)
5398                                 {
5399                                         return OPJ_FALSE;
5400                                 }
5401                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
5402                         }
5403                         ++l_src_mct_rec;
5404                         ++l_dest_mct_rec;
5405                 }
5406                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
5407                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5408                 opj_malloc(l_mcc_records_size);
5409                 if
5410                         (! l_tcp->m_mcc_records)
5411                 {
5412                         return OPJ_FALSE;
5413                 }
5414                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
5415                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
5416                 l_dest_mcc_rec = l_tcp->m_mcc_records;
5417                 for
5418                         (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j)
5419                 {
5420                         if
5421                                 (l_src_mcc_rec->m_decorrelation_array)
5422                         {
5423                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
5424                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
5425                         }
5426                         if
5427                                 (l_src_mcc_rec->m_offset_array)
5428                         {
5429                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
5430                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
5431                         }
5432                         ++l_src_mcc_rec;
5433                         ++l_dest_mcc_rec;
5434                 }
5435                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
5436                 ++l_tcp;
5437         }
5438         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
5439         if
5440                 (! p_j2k->m_tcd )
5441         {
5442                 return OPJ_FALSE;
5443         }
5444         if
5445                 (! tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)))
5446         {
5447                 tcd_destroy_v2(p_j2k->m_tcd);
5448                 p_j2k->m_tcd = 00;
5449                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5450                 return OPJ_FALSE;
5451         }
5452         return OPJ_TRUE;
5453 }
5454
5455 /**
5456  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
5457  * with the marker value.
5458  * @param       p_id            Marker value to look up
5459  *
5460  * @return      the handler associated with the id.
5461 */
5462 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
5463 {
5464         const opj_dec_memory_marker_handler_t *e;
5465         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
5466                 if (e->id == p_id) {
5467                         break; // we find a handler corresponding to the marker ID
5468                 }
5469         }
5470         return e;
5471 }
5472
5473
5474 /**
5475  * Destroys a jpeg2000 codec.
5476  *
5477  * @param       p_j2k   the jpeg20000 structure to destroy.
5478  */
5479 void j2k_destroy (opj_j2k_v2_t *p_j2k)
5480 {
5481         if
5482                 (p_j2k == 00)
5483         {
5484                 return;
5485         }
5486
5487         if
5488                 (p_j2k->m_is_decoder)
5489         {
5490                 if
5491                         (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00)
5492                 {
5493                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5494                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5495                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
5496                 }
5497                 if
5498                         (p_j2k->m_specific_param.m_decoder.m_header_data != 00)
5499                 {
5500                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
5501                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
5502                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
5503                 }
5504
5505         }
5506         else
5507         {
5508                 if
5509                         (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data)
5510                 {
5511                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
5512                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
5513                 }
5514                 if
5515                         (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer)
5516                 {
5517                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
5518                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
5519                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
5520                 }
5521                 if
5522                         (p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5523                 {
5524                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5525                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
5526                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5527                 }
5528         }
5529         tcd_destroy_v2(p_j2k->m_tcd);
5530
5531         j2k_cp_destroy(&(p_j2k->m_cp));
5532         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
5533
5534         opj_procedure_list_destroy(p_j2k->m_procedure_list);
5535         p_j2k->m_procedure_list = 00;
5536
5537         opj_procedure_list_destroy(p_j2k->m_validation_list);
5538         p_j2k->m_procedure_list = 00;
5539
5540         opj_free(p_j2k);
5541 }
5542
5543
5544
5545 /**
5546  * Destroys a tile coding parameter structure.
5547  *
5548  * @param       p_tcp           the tile coding parameter to destroy.
5549  */
5550 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
5551 {
5552         if
5553                 (p_tcp == 00)
5554         {
5555                 return;
5556         }
5557         if
5558                 (p_tcp->ppt_buffer != 00)
5559         {
5560                 opj_free(p_tcp->ppt_buffer);
5561                 p_tcp->ppt_buffer = 00;
5562         }
5563         if
5564                 (p_tcp->tccps != 00)
5565         {
5566                 opj_free(p_tcp->tccps);
5567                 p_tcp->tccps = 00;
5568         }
5569         if
5570                 (p_tcp->m_mct_coding_matrix != 00)
5571         {
5572                 opj_free(p_tcp->m_mct_coding_matrix);
5573                 p_tcp->m_mct_coding_matrix = 00;
5574         }
5575         if
5576                 (p_tcp->m_mct_decoding_matrix != 00)
5577         {
5578                 opj_free(p_tcp->m_mct_decoding_matrix);
5579                 p_tcp->m_mct_decoding_matrix = 00;
5580         }
5581         if
5582                 (p_tcp->m_mcc_records)
5583         {
5584                 opj_free(p_tcp->m_mcc_records);
5585                 p_tcp->m_mcc_records = 00;
5586                 p_tcp->m_nb_max_mcc_records = 0;
5587                 p_tcp->m_nb_mcc_records = 0;
5588         }
5589         if
5590                 (p_tcp->m_mct_records)
5591         {
5592                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
5593                 OPJ_UINT32 i;
5594                 for
5595                         (i=0;i<p_tcp->m_nb_mct_records;++i)
5596                 {
5597                         if
5598                                 (l_mct_data->m_data)
5599                         {
5600                                 opj_free(l_mct_data->m_data);
5601                                 l_mct_data->m_data = 00;
5602                         }
5603                         ++l_mct_data;
5604                 }
5605                 opj_free(p_tcp->m_mct_records);
5606                 p_tcp->m_mct_records = 00;
5607         }
5608
5609         if
5610                 (p_tcp->mct_norms != 00)
5611         {
5612                 opj_free(p_tcp->mct_norms);
5613                 p_tcp->mct_norms = 00;
5614         }
5615         if
5616                 (p_tcp->m_data)
5617         {
5618                 opj_free(p_tcp->m_data);
5619                 p_tcp->m_data = 00;
5620         }
5621 }
5622
5623
5624 /**
5625  * Destroys a coding parameter structure.
5626  *
5627  * @param       p_cp            the coding parameter to destroy.
5628  */
5629 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
5630 {
5631         OPJ_UINT32 l_nb_tiles;
5632         opj_tcp_v2_t * l_current_tile = 00;
5633         OPJ_UINT32 i;
5634
5635         if
5636                 (p_cp == 00)
5637         {
5638                 return;
5639         }
5640         if
5641                 (p_cp->tcps != 00)
5642         {
5643                 l_current_tile = p_cp->tcps;
5644                 l_nb_tiles = p_cp->th * p_cp->tw;
5645
5646                 for
5647                         (i = 0; i < l_nb_tiles; ++i)
5648                 {
5649                         j2k_tcp_destroy(l_current_tile);
5650                         ++l_current_tile;
5651                 }
5652                 opj_free(p_cp->tcps);
5653                 p_cp->tcps = 00;
5654         }
5655         if
5656                 (p_cp->ppm_buffer != 00)
5657         {
5658                 opj_free(p_cp->ppm_buffer);
5659                 p_cp->ppm_buffer = 00;
5660         }
5661         if
5662                 (p_cp->comment != 00)
5663         {
5664                 opj_free(p_cp->comment);
5665                 p_cp->comment = 00;
5666         }
5667         if
5668                 (! p_cp->m_is_decoder)
5669         {
5670                 if
5671                         (p_cp->m_specific_param.m_enc.m_matrice)
5672                 {
5673                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
5674                         p_cp->m_specific_param.m_enc.m_matrice = 00;
5675                 }
5676         }
5677 }
5678
5679
5680
5681 /**
5682  * Reads a tile header.
5683  * @param       p_j2k           the jpeg2000 codec.
5684  * @param       p_stream                        the stream to write data to.
5685  * @param       p_manager       the user event manager.
5686  */
5687 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
5688                                                                 OPJ_UINT32 * p_tile_index,
5689                                                                 OPJ_UINT32 * p_data_size,
5690                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
5691                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
5692                                                                 OPJ_UINT32 * p_nb_comps,
5693                                                                 opj_bool * p_go_on,
5694                                                                 opj_stream_private_t *p_stream,
5695                                                                 opj_event_mgr_t * p_manager )
5696 {
5697         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
5698         OPJ_UINT32 l_marker_size;
5699         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5700         opj_tcp_v2_t * l_tcp = NULL;
5701         OPJ_UINT32 l_nb_tiles;
5702
5703         /* preconditions */
5704         assert(p_stream != 00);
5705         assert(p_j2k != 00);
5706         assert(p_manager != 00);
5707
5708         /* Reach the End Of Codestream ?*/
5709         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
5710                 l_current_marker = J2K_MS_EOC;
5711         }
5712         /* We need to encounter a SOT marker (a new tile-part header) */
5713         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
5714                 return OPJ_FALSE;
5715         }
5716
5717         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
5718         while (! p_j2k->m_specific_param.m_decoder.m_can_decode && l_current_marker != J2K_MS_EOC) {
5719
5720                 /* Try to read until the Start Of Data is detected */
5721                 while (l_current_marker != J2K_MS_SOD) {
5722
5723                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
5724                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5725                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5726                                 return OPJ_FALSE;
5727                         }
5728
5729                         /* Read 2 bytes from the buffer as the marker size */
5730                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5731
5732                         /* Why this condition? FIXME */
5733                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
5734                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
5735                         }
5736                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
5737
5738                         /* Get the marker handler from the marker ID */
5739                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
5740
5741                         /* Check if the marker is known and if it is the right place to find it */
5742                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5743                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5744                                 return OPJ_FALSE;
5745                         }
5746 /* FIXME manage case of unknown marker as in the main header ? */
5747
5748                         /* Check if the marker size is compatible with the header data size */
5749                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5750                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5751                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5752                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5753                                         return OPJ_FALSE;
5754                                 }
5755                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5756                         }
5757
5758                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
5759                         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) {
5760                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5761                                 return OPJ_FALSE;
5762                         }
5763
5764                         /* Read the marker segment with the correct marker handler */
5765                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5766                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5767                                 return OPJ_FALSE;
5768                         }
5769
5770                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
5771                                 /* Skip the rest of the tile part header*/
5772                                 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) {
5773                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5774                                         return OPJ_FALSE;
5775                                 }
5776                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
5777                         }
5778                         else {
5779                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5780                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5781                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5782                                         return OPJ_FALSE;
5783                                 }
5784                                 /* Read 2 bytes from the buffer as the new marker ID */
5785                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5786                         }
5787                 }
5788
5789                 /* If we didn't skip data before, we need to read the SOD marker*/
5790                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
5791                         /* Try to read the SOD marker and skip data ? FIXME */
5792                         if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
5793                                 return OPJ_FALSE;
5794                         }
5795                 }
5796                 else {
5797                         /* Indicate we will try to read a new tile-part header*/
5798                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
5799                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5800                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5801
5802                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5803                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5804                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5805                                 return OPJ_FALSE;
5806                         }
5807
5808                         /* Read 2 bytes from buffer as the new marker ID */
5809                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5810                 }
5811         }
5812
5813         /* Current marker is the EOC marker ?*/
5814         if (l_current_marker == J2K_MS_EOC) {
5815                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
5816                         p_j2k->m_current_tile_number = 0;
5817                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
5818                 }
5819         }
5820
5821         /* FIXME ???*/
5822         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
5823                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
5824                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5825
5826                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
5827                         ++p_j2k->m_current_tile_number;
5828                         ++l_tcp;
5829                 }
5830
5831                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
5832                         *p_go_on = OPJ_FALSE;
5833                         return OPJ_TRUE;
5834                 }
5835         }
5836
5837         /*FIXME ???*/
5838         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
5839                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5840                 return OPJ_FALSE;
5841         }
5842
5843         *p_tile_index = p_j2k->m_current_tile_number;
5844         *p_go_on = OPJ_TRUE;
5845         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
5846         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
5847         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
5848         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
5849         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
5850         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
5851         p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
5852
5853         return OPJ_TRUE;
5854 }
5855
5856
5857 opj_bool j2k_decode_tile (
5858                                         opj_j2k_v2_t * p_j2k,
5859                                         OPJ_UINT32 p_tile_index,
5860                                         OPJ_BYTE * p_data,
5861                                         OPJ_UINT32 p_data_size,
5862                                         opj_stream_private_t *p_stream,
5863                                         opj_event_mgr_t * p_manager
5864                                         )
5865 {
5866         OPJ_UINT32 l_current_marker;
5867         OPJ_BYTE l_data [2];
5868         opj_tcp_v2_t * l_tcp;
5869
5870         // preconditions
5871         assert(p_stream != 00);
5872         assert(p_j2k != 00);
5873         assert(p_manager != 00);
5874
5875         if
5876                 (! (p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) || p_tile_index != p_j2k->m_current_tile_number)
5877         {
5878                 return OPJ_FALSE;
5879         }
5880         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
5881         if
5882                 (! l_tcp->m_data)
5883         {
5884                 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
5885                 return OPJ_FALSE;
5886         }
5887         if
5888                 (! tcd_decode_tile_v2(p_j2k->m_tcd, l_tcp->m_data, l_tcp->m_data_size, p_tile_index, p_j2k->cstr_info))
5889         {
5890                 j2k_tcp_destroy(l_tcp);
5891                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
5892                 return OPJ_FALSE;
5893         }
5894         if
5895                 (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size))
5896         {
5897                 return OPJ_FALSE;
5898         }
5899         j2k_tcp_destroy(l_tcp);
5900         p_j2k->m_tcd->tcp = 0;
5901
5902         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5903         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
5904         if
5905                 (p_j2k->m_specific_param.m_decoder.m_state != 0x0100)//FIXME J2K_DEC_STATE_EOC)
5906         {
5907                 if
5908                         (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2)
5909                 {
5910                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5911                         return OPJ_FALSE;
5912                 }
5913                 opj_read_bytes(l_data,&l_current_marker,2);
5914                 if
5915                         (l_current_marker == J2K_MS_EOC)
5916                 {
5917                         p_j2k->m_current_tile_number = 0;
5918                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;//FIXME J2K_DEC_STATE_EOC;
5919                 }
5920                 else if
5921                         (l_current_marker != J2K_MS_SOT)
5922                 {
5923                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
5924                         return OPJ_FALSE;
5925                 }
5926         }
5927         return OPJ_TRUE;
5928 }
5929
5930
5931 /**
5932  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
5933  *
5934  * @param       p_j2k                   the jpeg2000 codec.
5935  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
5936  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
5937  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
5938  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
5939  * @param       p_manager               the user event manager
5940  *
5941  * @return      true                    if the area could be set.
5942  */
5943 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
5944                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
5945                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
5946                                                                 struct opj_event_mgr * p_manager )
5947 {
5948         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
5949
5950         /* Check if we are read the main header */
5951         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
5952                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
5953                 return OPJ_FALSE;
5954         }
5955
5956         /* ----- */
5957         /* Check if the positions provided by the user are correct */
5958
5959         /* Left */
5960         if (p_start_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
5961                 opj_event_msg_v2(p_manager, EVT_ERROR,
5962                         "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
5963                         p_start_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
5964                 return OPJ_FALSE;
5965         }
5966         else if (p_start_x < l_cp->tx0){
5967                 opj_event_msg_v2(p_manager, EVT_WARNING,
5968                                 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
5969                                 p_start_x, l_cp->tx0);
5970                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
5971         }
5972         else
5973                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
5974
5975         /* Up */
5976         if (p_start_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
5977                 opj_event_msg_v2(p_manager, EVT_ERROR,
5978                                 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
5979                                 p_start_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
5980                 return OPJ_FALSE;
5981         }
5982         else if (p_start_y < l_cp->ty0){
5983                 opj_event_msg_v2(p_manager, EVT_WARNING,
5984                                 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
5985                                 p_start_y, l_cp->ty0);
5986                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
5987         }
5988         else
5989                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
5990
5991         /* Right */
5992         if (p_end_x < l_cp->tx0) {
5993                 opj_event_msg_v2(p_manager, EVT_ERROR,
5994                         "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
5995                         p_end_x, l_cp->tx0);
5996                 return OPJ_FALSE;
5997         }
5998         else if (p_end_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
5999                 opj_event_msg_v2(p_manager, EVT_WARNING,
6000                         "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
6001                         p_end_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
6002                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
6003         }
6004         else
6005                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
6006
6007         /* Bottom */
6008         if (p_end_x < l_cp->ty0) {
6009                 opj_event_msg_v2(p_manager, EVT_ERROR,
6010                         "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
6011                         p_end_x, l_cp->ty0);
6012                 return OPJ_FALSE;
6013         }
6014         if (p_end_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6015                 opj_event_msg_v2(p_manager, EVT_WARNING,
6016                         "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6017                         p_end_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6018                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
6019         }
6020         else
6021                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
6022         /* ----- */
6023
6024         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
6025
6026         return OPJ_TRUE;
6027 }
6028
6029
6030 /* ----------------------------------------------------------------------- */
6031 /* J2K / JPT decoder interface                                             */
6032 /* ----------------------------------------------------------------------- */
6033 /**
6034  * Creates a J2K decompression structure.
6035  *
6036  * @return a handle to a J2K decompressor if successful, NULL otherwise.
6037 */
6038 opj_j2k_v2_t* j2k_create_decompress_v2()
6039 {
6040         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6041         if (!l_j2k) {
6042                 return 00;
6043         }
6044         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6045
6046         l_j2k->m_is_decoder = 1;
6047         l_j2k->m_cp.m_is_decoder = 1;
6048         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
6049
6050         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
6051                 opj_free(l_j2k);
6052                 return 00;
6053         }
6054         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
6055
6056         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6057         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
6058                 j2k_destroy(l_j2k);
6059                 return 00;
6060         }
6061
6062         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
6063
6064         // codestream info creation
6065         l_j2k->cstr_info = (opj_codestream_info_v2_t*) opj_malloc(sizeof(opj_codestream_info_v2_t));
6066         if (!l_j2k->cstr_info){
6067                 opj_free(l_j2k);
6068                 return NULL;
6069         }
6070
6071         // validation list creation
6072         l_j2k->m_validation_list = opj_procedure_list_create();
6073         if (! l_j2k->m_validation_list) {
6074                 j2k_destroy(l_j2k);
6075                 return 00;
6076         }
6077
6078         // execution list creation
6079         l_j2k->m_procedure_list = opj_procedure_list_create();
6080         if (! l_j2k->m_procedure_list) {
6081                 j2k_destroy(l_j2k);
6082                 return 00;
6083         }
6084
6085         return l_j2k;
6086 }
6087
6088
6089 /**
6090  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
6091  * @param       p_header_data   the data contained in the COM box.
6092  * @param       p_j2k                   the jpeg2000 codec.
6093  * @param       p_header_size   the size of the data contained in the COM marker.
6094  * @param       p_manager               the user event manager.
6095 */
6096 opj_bool j2k_read_SPCod_SPCoc(
6097                                                     opj_j2k_v2_t *p_j2k,
6098                                                         OPJ_UINT32 compno,
6099                                                         OPJ_BYTE * p_header_data,
6100                                                         OPJ_UINT32 * p_header_size,
6101                                                         struct opj_event_mgr * p_manager
6102                                                         )
6103 {
6104         OPJ_UINT32 i, l_tmp;
6105         opj_cp_v2_t *l_cp = NULL;
6106         opj_tcp_v2_t *l_tcp = NULL;
6107         opj_tccp_t *l_tccp = NULL;
6108         OPJ_BYTE * l_current_ptr = NULL;
6109
6110         /* preconditions */
6111         assert(p_j2k != 00);
6112         assert(p_manager != 00);
6113         assert(p_header_data != 00);
6114
6115         l_cp = &(p_j2k->m_cp);
6116         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6117                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6118                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6119
6120         /* precondition again */
6121         assert(compno < p_j2k->m_image_header->numcomps);
6122
6123         l_tccp = &l_tcp->tccps[compno];
6124         l_current_ptr = p_header_data;
6125
6126         /* make sure room is sufficient */
6127         if (*p_header_size < 5) {
6128                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6129                 return OPJ_FALSE;
6130         }
6131
6132         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
6133         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
6134         ++l_current_ptr;
6135
6136         /* If user wants to remove more resolutions than the codestream contains, return error */
6137         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
6138                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
6139                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
6140                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
6141                 return OPJ_FALSE;
6142         }
6143
6144         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
6145         ++l_current_ptr;
6146         l_tccp->cblkw += 2;
6147
6148         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
6149         ++l_current_ptr;
6150         l_tccp->cblkh += 2;
6151
6152         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
6153         ++l_current_ptr;
6154
6155         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
6156         ++l_current_ptr;
6157
6158         *p_header_size = *p_header_size - 5;
6159
6160         /* use custom precinct size ? */
6161         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
6162                 if (*p_header_size < l_tccp->numresolutions) {
6163                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6164                         return OPJ_FALSE;
6165                 }
6166
6167                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
6168                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
6169                         ++l_current_ptr;
6170                         l_tccp->prcw[i] = l_tmp & 0xf;
6171                         l_tccp->prch[i] = l_tmp >> 4;
6172                 }
6173
6174                 *p_header_size = *p_header_size - l_tccp->numresolutions;
6175         }
6176         else {
6177                 /* set default size for the precinct width and height */
6178                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
6179                         l_tccp->prcw[i] = 15;
6180                         l_tccp->prch[i] = 15;
6181                 }
6182         }
6183
6184         /* INDEX >> */
6185         if (p_j2k->cstr_info && compno == 0) {
6186                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
6187
6188                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
6189                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
6190                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
6191                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
6192                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
6193
6194
6195                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
6196                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
6197         }
6198         /* << INDEX */
6199         return OPJ_TRUE;
6200 }
6201
6202 /**
6203  * Copies the tile component parameters of all the component from the first tile component.
6204  *
6205  * @param               p_j2k           the J2k codec.
6206  */
6207 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
6208 {
6209         // loop
6210         OPJ_UINT32 i;
6211         opj_cp_v2_t *l_cp = NULL;
6212         opj_tcp_v2_t *l_tcp = NULL;
6213         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
6214         OPJ_UINT32 l_prc_size;
6215
6216         /* preconditions */
6217         assert(p_j2k != 00);
6218
6219         l_cp = &(p_j2k->m_cp);
6220         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6221                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6222                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6223
6224         l_ref_tccp = &l_tcp->tccps[0];
6225         l_copied_tccp = l_ref_tccp + 1;
6226         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
6227
6228         for     (i=1; i<p_j2k->m_image_header->numcomps; ++i) {
6229                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
6230                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
6231                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
6232                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
6233                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
6234                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
6235                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
6236                 ++l_copied_tccp;
6237         }
6238 }
6239
6240 /**
6241  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
6242  *
6243  * @param       p_comp_no               the component being targeted.
6244  * @param       p_header_data   the data contained in the COM box.
6245  * @param       p_j2k                   the jpeg2000 codec.
6246  * @param       p_header_size   the size of the data contained in the COM marker.
6247  * @param       p_manager               the user event manager.
6248 */
6249 opj_bool j2k_read_SQcd_SQcc(
6250                                                         opj_j2k_v2_t *p_j2k,
6251                                                         OPJ_UINT32 p_comp_no,
6252                                                         OPJ_BYTE* p_header_data,
6253                                                         OPJ_UINT32 * p_header_size,
6254                                                         struct opj_event_mgr * p_manager
6255                                                         )
6256 {
6257         // loop
6258         OPJ_UINT32 l_band_no;
6259         opj_cp_v2_t *l_cp = 00;
6260         opj_tcp_v2_t *l_tcp = 00;
6261         opj_tccp_t *l_tccp = 00;
6262         OPJ_BYTE * l_current_ptr = 00;
6263         OPJ_UINT32 l_tmp, l_num_band;
6264
6265         // preconditions
6266         assert(p_j2k != 00);
6267         assert(p_manager != 00);
6268         assert(p_header_data != 00);
6269
6270         l_cp = &(p_j2k->m_cp);
6271         // come from tile part header or main header ?
6272         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
6273                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6274                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6275
6276         // precondition again
6277         assert(p_comp_no <  p_j2k->m_image_header->numcomps);
6278
6279         l_tccp = &l_tcp->tccps[p_comp_no];
6280         l_current_ptr = p_header_data;
6281
6282         if (*p_header_size < 1) {
6283                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
6284                 return OPJ_FALSE;
6285         }
6286         *p_header_size -= 1;
6287
6288         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
6289         ++l_current_ptr;
6290
6291         l_tccp->qntsty = l_tmp & 0x1f;
6292         l_tccp->numgbits = l_tmp >> 5;
6293         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6294         l_num_band = 1;
6295         }
6296         else {
6297                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
6298                         (*p_header_size) :
6299                         (*p_header_size) / 2;
6300
6301                 if( l_num_band > J2K_MAXBANDS ) {
6302                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
6303                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
6304                                 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
6305                         //return OPJ_FALSE;
6306                 }
6307         }
6308
6309 #ifdef USE_JPWL
6310         if (p_j2k->m_cp->correct) {
6311
6312                 /* if JPWL is on, we check whether there are too many subbands */
6313                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
6314                         opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
6315                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
6316                                 numbands);
6317                         if (!JPWL_ASSUME) {
6318                                 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6319                                 return;
6320                         }
6321                         /* we try to correct */
6322                         numbands = 1;
6323                         opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
6324                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
6325                                 numbands);
6326                 };
6327
6328         };
6329 #endif /* USE_JPWL */
6330
6331         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
6332                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6333                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
6334                         ++l_current_ptr;
6335                         if (l_band_no < J2K_MAXBANDS){
6336                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
6337                                 l_tccp->stepsizes[l_band_no].mant = 0;
6338                         }
6339                 }
6340                 *p_header_size = *p_header_size - l_num_band;
6341         }
6342         else {
6343                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6344                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
6345                         l_current_ptr+=2;
6346                         if (l_band_no < J2K_MAXBANDS){
6347                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
6348                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
6349                         }
6350                 }
6351                 *p_header_size = *p_header_size - 2*l_num_band;
6352         }
6353
6354         /* Add Antonin : if scalar_derived -> compute other stepsizes */
6355         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6356                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
6357                         l_tccp->stepsizes[l_band_no].expn =
6358                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
6359                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
6360                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
6361                 }
6362         }
6363
6364         return OPJ_TRUE;
6365 }
6366
6367
6368
6369 /**
6370  * Copies the tile component parameters of all the component from the first tile component.
6371  *
6372  * @param               p_j2k           the J2k codec.
6373  */
6374 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
6375 {
6376         OPJ_UINT32 i;
6377         opj_cp_v2_t *l_cp = NULL;
6378         opj_tcp_v2_t *l_tcp = NULL;
6379         opj_tccp_t *l_ref_tccp = NULL;
6380         opj_tccp_t *l_copied_tccp = NULL;
6381         OPJ_UINT32 l_size;
6382
6383         // preconditions
6384         assert(p_j2k != 00);
6385
6386         l_cp = &(p_j2k->m_cp);
6387         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;
6388         // precondition again
6389         l_ref_tccp = &l_tcp->tccps[0];
6390         l_copied_tccp = l_ref_tccp + 1;
6391         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
6392
6393         for     (i=1;i<p_j2k->m_image_header->numcomps;++i) {
6394                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
6395                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
6396                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
6397                 ++l_copied_tccp;
6398         }
6399 }