[trunk] WIP: fix the decoding process with conformance files and V2 framework
[openjpeg.git] / libopenjpeg / j2k.h
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  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __J2K_H
33 #define __J2K_H
34 /**
35 @file j2k.h
36 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
37
38 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
39 */
40
41 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
42 /*@{*/
43
44 #define J2K_CP_CSTY_PRT 0x01
45 #define J2K_CP_CSTY_SOP 0x02
46 #define J2K_CP_CSTY_EPH 0x04
47 #define J2K_CCP_CSTY_PRT 0x01
48 #define J2K_CCP_CBLKSTY_LAZY 0x01     /**< Selective arithmetic coding bypass */
49 #define J2K_CCP_CBLKSTY_RESET 0x02    /**< Reset context probabilities on coding pass boundaries */
50 #define J2K_CCP_CBLKSTY_TERMALL 0x04  /**< Termination on each coding pass */
51 #define J2K_CCP_CBLKSTY_VSC 0x08      /**< Vertically stripe causal context */
52 #define J2K_CCP_CBLKSTY_PTERM 0x10    /**< Predictable termination */
53 #define J2K_CCP_CBLKSTY_SEGSYM 0x20   /**< Segmentation symbols are used */
54 #define J2K_CCP_QNTSTY_NOQNT 0
55 #define J2K_CCP_QNTSTY_SIQNT 1
56 #define J2K_CCP_QNTSTY_SEQNT 2
57
58 /* ----------------------------------------------------------------------- */
59
60 #define J2K_MS_SOC 0xff4f       /**< SOC marker value */
61 #define J2K_MS_SOT 0xff90       /**< SOT marker value */
62 #define J2K_MS_SOD 0xff93       /**< SOD marker value */
63 #define J2K_MS_EOC 0xffd9       /**< EOC marker value */
64 #define J2K_MS_SIZ 0xff51       /**< SIZ marker value */
65 #define J2K_MS_COD 0xff52       /**< COD marker value */
66 #define J2K_MS_COC 0xff53       /**< COC marker value */
67 #define J2K_MS_RGN 0xff5e       /**< RGN marker value */
68 #define J2K_MS_QCD 0xff5c       /**< QCD marker value */
69 #define J2K_MS_QCC 0xff5d       /**< QCC marker value */
70 #define J2K_MS_POC 0xff5f       /**< POC marker value */
71 #define J2K_MS_TLM 0xff55       /**< TLM marker value */
72 #define J2K_MS_PLM 0xff57       /**< PLM marker value */
73 #define J2K_MS_PLT 0xff58       /**< PLT marker value */
74 #define J2K_MS_PPM 0xff60       /**< PPM marker value */
75 #define J2K_MS_PPT 0xff61       /**< PPT marker value */
76 #define J2K_MS_SOP 0xff91       /**< SOP marker value */
77 #define J2K_MS_EPH 0xff92       /**< EPH marker value */
78 #define J2K_MS_CRG 0xff63       /**< CRG marker value */
79 #define J2K_MS_COM 0xff64       /**< COM marker value */
80
81 #define J2K_MS_UNK 0            /**< UNKNOWN marker value */
82
83 #ifdef TODO_MS /* FIXME */
84 #define J2K_MS_CBD 0xff78       /**< CBD marker value */
85 #define J2K_MS_MCC 0xff75       /**< MCC marker value */
86 #define J2K_MS_MCT 0xff74       /**< MCT marker value */
87 #define J2K_MS_MCO 0xff77       /**< MCO marker value */
88 #endif
89
90 /* UniPG>> */
91 #ifdef USE_JPWL
92 #define J2K_MS_EPC 0xff68       /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
93 #define J2K_MS_EPB 0xff66       /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */ 
94 #define J2K_MS_ESD 0xff67       /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */ 
95 #define J2K_MS_RED 0xff69       /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
96 #endif /* USE_JPWL */
97 #ifdef USE_JPSEC
98 #define J2K_MS_SEC 0xff65    /**< SEC marker value (Part 8: Secure JPEG 2000) */
99 #define J2K_MS_INSEC 0xff94  /**< INSEC marker value (Part 8: Secure JPEG 2000) */
100 #endif /* USE_JPSEC */
101 /* <<UniPG */
102
103
104 struct opj_stream_private;
105
106 /* ----------------------------------------------------------------------- */
107
108 /**
109 Values that specify the status of the decoding process when decoding the main header. 
110 These values may be combined with a | operator. 
111 */
112 typedef enum J2K_STATUS {
113         J2K_STATE_MHSOC  = 0x0001, /**< a SOC marker is expected */
114         J2K_STATE_MHSIZ  = 0x0002, /**< a SIZ marker is expected */
115         J2K_STATE_MH     = 0x0004, /**< the decoding process is in the main header */
116         J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
117         J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */
118         J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */
119         J2K_STATE_NEOC   = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
120
121         J2K_STATE_EOC    = 0x0100,  /**< the decoding process has encountered the EOC marker */
122         J2K_STATE_ERR    = 0x8000 /**< the decoding process has encountered an error (FIXME warning V1 = 0x0080)*/
123 } J2K_STATUS;
124
125 /* ----------------------------------------------------------------------- */
126
127 /** 
128 T2 encoding mode 
129 */
130 typedef enum T2_MODE {
131         THRESH_CALC = 0,        /** Function called in Rate allocation process*/
132         FINAL_PASS = 1          /** Function called in Tier 2 process*/
133 }J2K_T2_MODE;
134
135 /**
136 Quantization stepsize
137 */
138 typedef struct opj_stepsize {
139         /** exponent */
140         int expn;
141         /** mantissa */
142         int mant;
143 } opj_stepsize_t;
144
145 //OPJ_V1/**
146 //Tile-component coding parameters
147 //*/
148 //typedef struct opj_tccp {
149 //      /** coding style */
150 //      int csty;
151 //      /** number of resolutions */
152 //      int numresolutions;
153 //      /** code-blocks width */
154 //      int cblkw;
155 //      /** code-blocks height */
156 //      int cblkh;
157 //      /** code-block coding style */
158 //      int cblksty;
159 //      /** discrete wavelet transform identifier */
160 //      int qmfbid;
161 //      /** quantisation style */
162 //      int qntsty;
163 //      /** stepsizes used for quantization */
164 //      opj_stepsize_t stepsizes[J2K_MAXBANDS];
165 //      /** number of guard bits */
166 //      int numgbits;
167 //      /** Region Of Interest shift */
168 //      int roishift;
169 //      /** precinct width */
170 //      int prcw[J2K_MAXRLVLS];
171 //      /** precinct height */
172 //      int prch[J2K_MAXRLVLS];
173 //} opj_tccp_t;
174
175 /**
176 Tile-component coding parameters
177 */
178 typedef struct opj_tccp
179 {
180         /** coding style */
181         OPJ_UINT32 csty;
182         /** number of resolutions */
183         OPJ_UINT32 numresolutions;
184         /** code-blocks width */
185         OPJ_UINT32 cblkw;
186         /** code-blocks height */
187         OPJ_UINT32 cblkh;
188         /** code-block coding style */
189         OPJ_UINT32 cblksty;
190         /** discrete wavelet transform identifier */
191         OPJ_UINT32 qmfbid;
192         /** quantisation style */
193         OPJ_UINT32 qntsty;
194         /** stepsizes used for quantization */
195         opj_stepsize_t stepsizes[J2K_MAXBANDS];
196         /** number of guard bits */
197         OPJ_UINT32 numgbits;
198         /** Region Of Interest shift */
199         OPJ_INT32 roishift;
200         /** precinct width */
201         OPJ_UINT32 prcw[J2K_MAXRLVLS];
202         /** precinct height */
203         OPJ_UINT32 prch[J2K_MAXRLVLS];
204         /** the dc_level_shift **/
205         OPJ_INT32 m_dc_level_shift;
206 }
207 opj_tccp_t;
208
209
210 /**
211 Tile coding parameters : 
212 this structure is used to store coding/decoding parameters common to all
213 tiles (information like COD, COC in main header)
214 */
215 typedef struct opj_tcp {
216         /** 1 : first part-tile of a tile */
217         int first;
218         /** coding style */
219         int csty;
220         /** progression order */
221         OPJ_PROG_ORDER prg;
222         /** number of layers */
223         int numlayers;
224         /** multi-component transform identifier */
225         int mct;
226         /** rates of layers */
227         float rates[100];
228         /** number of progression order changes */
229         int numpocs;
230         /** indicates if a POC marker has been used O:NO, 1:YES */
231         int POC;
232         /** progression order changes */
233         opj_poc_t pocs[32];
234         /** packet header store there for futur use in t2_decode_packet */
235         unsigned char *ppt_data;
236         /** pointer remaining on the first byte of the first header if ppt is used */
237         unsigned char *ppt_data_first;
238         /** If ppt == 1 --> there was a PPT marker for the present tile */
239         int ppt;
240         /** used in case of multiple marker PPT (number of info already stored) */
241         int ppt_store;
242         /** ppmbug1 */
243         int ppt_len;
244         /** add fixed_quality */
245         float distoratio[100];
246         /** tile-component coding parameters */
247         opj_tccp_t *tccps;
248 } opj_tcp_t;
249
250
251 /**
252  * Type of data for storing the MCT data
253  */
254 typedef enum MCT_ELEMENT_TYPE
255 {
256         MCT_TYPE_INT16 = 0,             /** MCT data is stored as signed shorts*/
257         MCT_TYPE_INT32 = 1,             /** MCT data is stored as signed integers*/
258         MCT_TYPE_FLOAT = 2,             /** MCT data is stored as floats*/
259         MCT_TYPE_DOUBLE = 3             /** MCT data is stored as doubles*/
260 } J2K_MCT_ELEMENT_TYPE;
261
262 /**
263  * Type of data for storing the MCT data
264  */
265 typedef enum MCT_ARRAY_TYPE
266 {
267         MCT_TYPE_DEPENDENCY = 0,
268         MCT_TYPE_DECORRELATION = 1,
269         MCT_TYPE_OFFSET = 2
270 } J2K_MCT_ARRAY_TYPE;
271
272 typedef struct opj_mct_data
273 {
274         J2K_MCT_ELEMENT_TYPE m_element_type;
275         J2K_MCT_ARRAY_TYPE       m_array_type;
276         OPJ_UINT32                       m_index;
277         OPJ_BYTE *                       m_data;
278         OPJ_UINT32                       m_data_size;
279 }
280 opj_mct_data_t;
281
282 typedef struct opj_simple_mcc_decorrelation_data
283 {
284         OPJ_UINT32                       m_index;
285         OPJ_UINT32                       m_nb_comps;
286         opj_mct_data_t *         m_decorrelation_array;
287         opj_mct_data_t *         m_offset_array;
288         OPJ_UINT32                       m_is_irreversible : 1;
289 }
290 opj_simple_mcc_decorrelation_data_t;
291
292 /**
293 Tile coding parameters :
294 this structure is used to store coding/decoding parameters common to all
295 tiles (information like COD, COC in main header)
296 */
297 typedef struct opj_tcp_v2
298 {
299         /** coding style */
300         OPJ_UINT32 csty;
301         /** progression order */
302         OPJ_PROG_ORDER prg;
303         /** number of layers */
304         OPJ_UINT32 numlayers;
305         OPJ_UINT32 num_layers_to_decode;
306         /** multi-component transform identifier */
307         OPJ_UINT32 mct;
308         /** rates of layers */
309         OPJ_FLOAT32 rates[100];
310         /** number of progression order changes */
311         OPJ_UINT32 numpocs;
312         /** progression order changes */
313         opj_poc_t pocs[32];
314         /** packet header store there for futur use in t2_decode_packet */
315         OPJ_BYTE *ppt_data;
316         /** used to keep a track of the allocated memory */
317         OPJ_BYTE *ppt_buffer;
318         /** Number of bytes stored inside ppt_data*/
319         OPJ_UINT32 ppt_data_size;
320         /** size of ppt_data*/
321         OPJ_UINT32 ppt_len;
322         /** add fixed_quality */
323         OPJ_FLOAT32 distoratio[100];
324         /** tile-component coding parameters */
325         opj_tccp_t *tccps;
326         /** number of tile parts for the tile. */
327         OPJ_UINT32 m_nb_tile_parts;
328         /** data for the tile */
329         OPJ_BYTE *              m_data;
330         /** size of data */
331         OPJ_UINT32              m_data_size;
332         /** encoding norms */
333         OPJ_FLOAT64 *   mct_norms;
334         /** the mct decoding matrix */
335         OPJ_FLOAT32 *   m_mct_decoding_matrix;
336         /** the mct coding matrix */
337         OPJ_FLOAT32 *   m_mct_coding_matrix;
338         /** mct records */
339         opj_mct_data_t * m_mct_records;
340         /** the number of mct records. */
341         OPJ_UINT32 m_nb_mct_records;
342         /** the max number of mct records. */
343         OPJ_UINT32 m_nb_max_mct_records;
344         /** mcc records */
345         opj_simple_mcc_decorrelation_data_t * m_mcc_records;
346         /** the number of mct records. */
347         OPJ_UINT32 m_nb_mcc_records;
348         /** the max number of mct records. */
349         OPJ_UINT32 m_nb_max_mcc_records;
350
351
352
353         /***** FLAGS *******/
354         /** If ppt == 1 --> there was a PPT marker for the present tile */
355         OPJ_UINT32 ppt : 1;
356         /** indicates if a POC marker has been used O:NO, 1:YES */
357         OPJ_UINT32 POC : 1;
358 } opj_tcp_v2_t;
359
360
361
362
363
364 /**
365 Coding parameters
366 */
367 typedef struct opj_cp {
368         /** Digital cinema profile*/
369         OPJ_CINEMA_MODE cinema;
370         /** Maximum rate for each component. If == 0, component size limitation is not considered */
371         int max_comp_size;
372         /** Size of the image in bits*/
373         int img_size;
374         /** Rsiz*/
375         OPJ_RSIZ_CAPABILITIES rsiz;
376         /** Enabling Tile part generation*/
377         char tp_on;
378         /** Flag determining tile part generation*/
379         char tp_flag;
380         /** Position of tile part flag in progression order*/
381         int tp_pos;
382         /** allocation by rate/distortion */
383         int disto_alloc;
384         /** allocation by fixed layer */
385         int fixed_alloc;
386         /** add fixed_quality */
387         int fixed_quality;
388         /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
389         int reduce;
390         /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
391         int layer;
392         /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */
393         OPJ_LIMIT_DECODING limit_decoding;
394         /** XTOsiz */
395         int tx0;
396         /** YTOsiz */
397         int ty0;
398         /** XTsiz */
399         int tdx;
400         /** YTsiz */
401         int tdy;
402         /** comment for coding */
403         char *comment;
404         /** number of tiles in width */
405         int tw;
406         /** number of tiles in heigth */
407         int th;
408         /** ID number of the tiles present in the codestream */
409         int *tileno;
410         /** size of the vector tileno */
411         int tileno_size;
412         /** packet header store there for futur use in t2_decode_packet */
413         unsigned char *ppm_data;
414         /** pointer remaining on the first byte of the first header if ppm is used */
415         unsigned char *ppm_data_first;
416         /** if ppm == 1 --> there was a PPM marker for the present tile */
417         int ppm;
418         /** use in case of multiple marker PPM (number of info already store) */
419         int ppm_store;
420         /** use in case of multiple marker PPM (case on non-finished previous info) */
421         int ppm_previous;
422         /** ppmbug1 */
423         int ppm_len;
424         /** tile coding parameters */
425         opj_tcp_t *tcps;
426         /** fixed layer */
427         int *matrice;
428 /* UniPG>> */
429 #ifdef USE_JPWL
430         /** enables writing of EPC in MH, thus activating JPWL */
431         opj_bool epc_on;
432         /** enables writing of EPB, in case of activated JPWL */
433         opj_bool epb_on;
434         /** enables writing of ESD, in case of activated JPWL */
435         opj_bool esd_on;
436         /** enables writing of informative techniques of ESD, in case of activated JPWL */
437         opj_bool info_on;
438         /** enables writing of RED, in case of activated JPWL */
439         opj_bool red_on;
440         /** error protection method for MH (0,1,16,32,37-128) */
441         int hprot_MH;
442         /** tile number of header protection specification (>=0) */
443         int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
444         /** error protection methods for TPHs (0,1,16,32,37-128) */
445         int hprot_TPH[JPWL_MAX_NO_TILESPECS];
446         /** tile number of packet protection specification (>=0) */
447         int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
448         /** packet number of packet protection specification (>=0) */
449         int pprot_packno[JPWL_MAX_NO_PACKSPECS];
450         /** error protection methods for packets (0,1,16,32,37-128) */
451         int pprot[JPWL_MAX_NO_PACKSPECS];
452         /** enables writing of ESD, (0/2/4 bytes) */
453         int sens_size;
454         /** sensitivity addressing size (0=auto/2/4 bytes) */
455         int sens_addr;
456         /** sensitivity range (0-3) */
457         int sens_range;
458         /** sensitivity method for MH (-1,0-7) */
459         int sens_MH;
460         /** tile number of sensitivity specification (>=0) */
461         int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
462         /** sensitivity methods for TPHs (-1,0-7) */
463         int sens_TPH[JPWL_MAX_NO_TILESPECS];
464         /** enables JPWL correction at the decoder */
465         opj_bool correct;
466         /** expected number of components at the decoder */
467         int exp_comps;
468         /** maximum number of tiles at the decoder */
469         int max_tiles;
470 #endif /* USE_JPWL */
471 /* <<UniPG */
472 } opj_cp_t;
473
474 typedef struct opj_encoding_param
475 {
476         /** Digital cinema profile*/
477         OPJ_CINEMA_MODE m_cinema;
478         /** Maximum rate for each component. If == 0, component size limitation is not considered */
479         OPJ_UINT32 m_max_comp_size;
480         /** Position of tile part flag in progression order*/
481         OPJ_INT32 m_tp_pos;
482         /** fixed layer */
483         OPJ_INT32 *m_matrice;
484         /** Flag determining tile part generation*/
485         OPJ_BYTE m_tp_flag;
486         /** allocation by rate/distortion */
487         OPJ_UINT32 m_disto_alloc : 1;
488         /** allocation by fixed layer */
489         OPJ_UINT32 m_fixed_alloc : 1;
490         /** add fixed_quality */
491         OPJ_UINT32 m_fixed_quality : 1;
492         /** Enabling Tile part generation*/
493         OPJ_UINT32 m_tp_on : 1;
494 }
495 opj_encoding_param_t;
496
497 typedef struct opj_decoding_param
498 {
499         /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
500         OPJ_UINT32 m_reduce;
501         /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
502         OPJ_UINT32 m_layer;
503 }
504 opj_decoding_param_t;
505
506
507 /**
508 Coding parameters
509 */
510 typedef struct opj_cp_v2
511 {
512         /** Size of the image in bits*/
513         /*int img_size;*/
514         /** Rsiz*/
515         OPJ_RSIZ_CAPABILITIES rsiz;
516         /** XTOsiz */
517         OPJ_UINT32 tx0; // MSD see norm
518         /** YTOsiz */
519         OPJ_UINT32 ty0; // MSD see norm
520         /** XTsiz */
521         OPJ_UINT32 tdx;
522         /** YTsiz */
523         OPJ_UINT32 tdy;
524         /** comment */
525         OPJ_CHAR *comment;
526         /** number of tiles in width */
527         OPJ_UINT32 tw;
528         /** number of tiles in heigth */
529         OPJ_UINT32 th;
530
531
532
533
534         /** packet header store there for futur use in t2_decode_packet */
535         OPJ_BYTE *ppm_data;
536         /** size of the ppm_data*/
537         OPJ_UINT32 ppm_len;
538         /** size of the ppm_data*/
539         OPJ_UINT32 ppm_data_read;
540
541         OPJ_BYTE *ppm_data_current;
542
543         /** packet header storage original buffer */
544         OPJ_BYTE *ppm_buffer;
545         /** pointer remaining on the first byte of the first header if ppm is used */
546         OPJ_BYTE *ppm_data_first;
547         /** Number of bytes actually stored inside the ppm_data */
548         OPJ_UINT32 ppm_data_size;
549         /** use in case of multiple marker PPM (number of info already store) */
550         OPJ_INT32 ppm_store;
551         /** use in case of multiple marker PPM (case on non-finished previous info) */
552         OPJ_INT32 ppm_previous;
553
554         /** tile coding parameters */
555         opj_tcp_v2_t *tcps;
556
557         union
558         {
559                 opj_decoding_param_t m_dec;
560                 opj_encoding_param_t m_enc;
561         }
562         m_specific_param;
563
564
565 /* UniPG>> */
566 #ifdef USE_JPWL
567         /** enables writing of EPC in MH, thus activating JPWL */
568         opj_bool epc_on;
569         /** enables writing of EPB, in case of activated JPWL */
570         opj_bool epb_on;
571         /** enables writing of ESD, in case of activated JPWL */
572         opj_bool esd_on;
573         /** enables writing of informative techniques of ESD, in case of activated JPWL */
574         opj_bool info_on;
575         /** enables writing of RED, in case of activated JPWL */
576         opj_bool red_on;
577         /** error protection method for MH (0,1,16,32,37-128) */
578         int hprot_MH;
579         /** tile number of header protection specification (>=0) */
580         int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
581         /** error protection methods for TPHs (0,1,16,32,37-128) */
582         int hprot_TPH[JPWL_MAX_NO_TILESPECS];
583         /** tile number of packet protection specification (>=0) */
584         int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
585         /** packet number of packet protection specification (>=0) */
586         int pprot_packno[JPWL_MAX_NO_PACKSPECS];
587         /** error protection methods for packets (0,1,16,32,37-128) */
588         int pprot[JPWL_MAX_NO_PACKSPECS];
589         /** enables writing of ESD, (0/2/4 bytes) */
590         int sens_size;
591         /** sensitivity addressing size (0=auto/2/4 bytes) */
592         int sens_addr;
593         /** sensitivity range (0-3) */
594         int sens_range;
595         /** sensitivity method for MH (-1,0-7) */
596         int sens_MH;
597         /** tile number of sensitivity specification (>=0) */
598         int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
599         /** sensitivity methods for TPHs (-1,0-7) */
600         int sens_TPH[JPWL_MAX_NO_TILESPECS];
601         /** enables JPWL correction at the decoder */
602         opj_bool correct;
603         /** expected number of components at the decoder */
604         int exp_comps;
605         /** maximum number of tiles at the decoder */
606         int max_tiles;
607 #endif /* USE_JPWL */
608
609         /******** FLAGS *********/
610         /** if ppm == 1 --> there was a PPM marker*/
611         OPJ_UINT32 ppm : 1;
612         /** tells if the parameter is a coding or decoding one */
613         OPJ_UINT32 m_is_decoder : 1;
614 /* <<UniPG */
615 } opj_cp_v2_t;
616
617
618 typedef struct opj_j2k_dec
619 {
620         /** locate in which part of the codestream the decoder is (main header, tile header, end) */
621         OPJ_UINT32 m_state;
622         /**
623          * store decoding parameters common to all tiles (information like COD, COC in main header)
624          */
625         opj_tcp_v2_t *m_default_tcp;
626         OPJ_BYTE  *m_header_data;
627         OPJ_UINT32 m_header_data_size;
628         /** to tell the tile part length */
629         OPJ_UINT32 m_sot_length;
630         /** Only tiles index in the correct range will be decoded.*/
631         OPJ_UINT32 m_start_tile_x;
632         OPJ_UINT32 m_start_tile_y;
633         OPJ_UINT32 m_end_tile_x;
634         OPJ_UINT32 m_end_tile_y;
635         /** to tell that a tile can be decoded. */
636         OPJ_UINT32 m_can_decode                 : 1;
637         OPJ_UINT32 m_discard_tiles              : 1;
638         OPJ_UINT32 m_skip_data                  : 1;
639
640 } opj_j2k_dec_t;
641
642 typedef struct opj_j2k_enc
643 {
644         /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
645         OPJ_UINT32 m_current_poc_tile_part_number; // tp_num
646
647         /** Tile part number currently coding, taking into account POC. m_current_tile_part_number holds the total number of tile parts while encoding the last tile part.*/
648         OPJ_UINT32 m_current_tile_part_number; //cur_tp_num
649
650         /**
651         locate the start position of the TLM marker
652         after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
653         */
654         OPJ_SIZE_T m_tlm_start;
655         /**
656          * Stores the sizes of the tlm.
657          */
658         OPJ_BYTE * m_tlm_sot_offsets_buffer;
659         /**
660          * The current offset of the tlm buffer.
661          */
662         OPJ_BYTE * m_tlm_sot_offsets_current;
663
664         /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
665         /** used in TLMmarker*/
666         OPJ_UINT32 m_total_tile_parts;   // totnum_tp
667
668         /* encoded data for a tile */
669         OPJ_BYTE * m_encoded_tile_data;
670
671         /* size of the encoded_data */
672         OPJ_UINT32 m_encoded_tile_size;
673
674         /* encoded data for a tile */
675         OPJ_BYTE * m_header_tile_data;
676
677         /* size of the encoded_data */
678         OPJ_UINT32 m_header_tile_data_size;
679
680
681 } opj_j2k_enc_t;
682
683 /**
684 JPEG-2000 codestream reader/writer
685 */
686 typedef struct opj_j2k {
687         /** codec context */
688         opj_common_ptr cinfo;
689
690         /** locate in which part of the codestream the decoder is (main header, tile header, end) */
691         int state;
692         /** number of the tile curently concern by coding/decoding */
693         int curtileno;
694         /** Tile part number*/
695         int tp_num;
696         /** Tilepart number currently coding*/
697         int cur_tp_num;
698         /** Total number of tileparts of the current tile*/
699         int *cur_totnum_tp;
700         /**
701         locate the start position of the TLM marker  
702         after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length. 
703         */
704         int tlm_start;
705         /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
706         /** used in TLMmarker*/
707         int totnum_tp;  
708         /** 
709         locate the position of the end of the tile in the codestream, 
710         used to detect a truncated codestream (in j2k_read_sod)
711         */
712         unsigned char *eot;
713         /**
714         locate the start position of the SOT marker of the current coded tile:  
715         after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. 
716         */
717         int sot_start;
718         int sod_start;
719         /**
720         as the J2K-file is written in several parts during encoding, 
721         it enables to make the right correction in position return by cio_tell
722         */
723         int pos_correction;
724         /** array used to store the data of each tile */
725         unsigned char **tile_data;
726         /** array used to store the length of each tile */
727         int *tile_len;
728         /** 
729         decompression only : 
730         store decoding parameters common to all tiles (information like COD, COC in main header)
731         */
732         opj_tcp_t *default_tcp;
733         /** pointer to the encoded / decoded image */
734         opj_image_t *image;
735         /** pointer to the coding parameters */
736         opj_cp_t *cp;
737         /** helper used to write the index file */
738         opj_codestream_info_t *cstr_info;
739         /** pointer to the byte i/o stream */
740         opj_cio_t *cio;
741 } opj_j2k_t;
742
743 struct opj_tcd_v2;
744 /**
745 JPEG-2000 codestream reader/writer
746 */
747 typedef struct opj_j2k_v2
748 {
749         /* J2K codestream is decoded*/
750         opj_bool m_is_decoder;
751
752         /* FIXME DOC*/
753         union
754         {
755                 opj_j2k_dec_t m_decoder;
756                 opj_j2k_enc_t m_encoder;
757         }
758         m_specific_param;
759
760         /** pointer to the encoded / decoded image */
761         //opj_image_t *m_image;
762         opj_image_t* m_image;
763
764         /** Coding parameters */
765         opj_cp_v2_t m_cp;
766
767         /** the list of procedures to exec **/
768         struct opj_procedure_list *     m_procedure_list;
769
770         /** the list of validation procedures to follow to make sure the code is valid **/
771         struct opj_procedure_list *     m_validation_list;
772
773         /** helper used to write the index file */
774         opj_codestream_index_t *cstr_index;
775
776         /** number of the tile curently concern by coding/decoding */
777         OPJ_UINT32 m_current_tile_number;
778
779         /** the current tile coder/decoder **/
780         struct opj_tcd_v2 *     m_tcd;
781         //opj_tcd_v2_t *        m_tcd;
782
783
784
785 }
786 opj_j2k_v2_t;
787
788
789
790
791 /** @name Exported functions */
792 /*@{*/
793 /* ----------------------------------------------------------------------- */
794 /**
795 Creates a J2K decompression structure
796 @param cinfo Codec context info
797 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
798 */
799 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
800 /**
801 Destroy a J2K decompressor handle
802 @param j2k J2K decompressor handle to destroy
803 */
804 void j2k_destroy_decompress(opj_j2k_t *j2k);
805 /**
806 Setup the decoder decoding parameters using user parameters.
807 Decoding parameters are returned in j2k->cp. 
808 @param j2k J2K decompressor handle
809 @param parameters decompression parameters
810 */
811 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
812
813 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters);
814
815 /**
816 Decode an image from a JPEG-2000 codestream
817 @param j2k J2K decompressor handle
818 @param cio Input buffer stream
819 @param cstr_info Codestream information structure if required, NULL otherwise
820 @return Returns a decoded image if successful, returns NULL otherwise
821 */
822 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
823 /**
824 Decode an image form a JPT-stream (JPEG 2000, JPIP)
825 @param j2k J2K decompressor handle
826 @param cio Input buffer stream
827 @param cstr_info Codestream information structure if required, NULL otherwise
828 @return Returns a decoded image if successful, returns NULL otherwise
829 */
830 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
831 /**
832 Creates a J2K compression structure
833 @param cinfo Codec context info
834 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
835 */
836 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
837
838 /**
839 Creates a J2K compression structure
840 @param cinfo Codec context info
841 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
842 */
843 opj_j2k_v2_t* j2k_create_compress_v2();
844
845 /**
846 Destroy a J2K compressor handle
847 @param j2k J2K compressor handle to destroy
848 */
849 void j2k_destroy_compress(opj_j2k_t *j2k);
850 /**
851 Setup the encoder parameters using the current image and using user parameters. 
852 Coding parameters are returned in j2k->cp. 
853 @param j2k J2K compressor handle
854 @param parameters compression parameters
855 @param image input filled image
856 */
857 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
858 /**
859 Converts an enum type progression order to string type
860 */
861 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
862 /**
863 Encode an image into a JPEG-2000 codestream
864 @param j2k J2K compressor handle
865 @param cio Output buffer stream
866 @param image Image to encode
867 @param cstr_info Codestream information structure if required, NULL otherwise
868 @return Returns true if successful, returns false otherwise
869 */
870 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
871
872 /* ----------------------------------------------------------------------- */
873 /*@}*/
874
875 /*@}*/
876
877 /**
878  * Ends the decompression procedures and possibiliy add data to be read after the
879  * codestream.
880  */
881 opj_bool j2k_end_decompress(opj_j2k_v2_t *j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
882
883 /**
884  * Reads a jpeg2000 codestream header structure.
885  *
886  * @param cio the stream to read data from.
887  * @param p_j2k the jpeg2000 codec.
888  * @param p_manager the user event manager.
889  *
890  * @return true if the box is valid.
891  */
892 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
893                                                         opj_j2k_v2_t* p_j2k,
894                                                         opj_image_t* p_image,
895                                                         struct opj_event_mgr* p_manager );
896
897
898 /**
899  * Destroys a jpeg2000 codec.
900  *
901  * @param       p_j2k   the jpeg20000 structure to destroy.
902  */
903 void j2k_destroy (opj_j2k_v2_t *p_j2k);
904
905 /**
906  * Decode tile data.
907  * @param       p_j2k           the jpeg2000 codec.
908  * @param       p_stream                        the stream to write data to.
909  * @param       p_manager       the user event manager.
910  */
911 opj_bool j2k_decode_tile (
912                                         opj_j2k_v2_t * p_j2k,
913                                         OPJ_UINT32 p_tile_index,
914                                         OPJ_BYTE * p_data,
915                                         OPJ_UINT32 p_data_size,
916                                         struct opj_stream_private *p_stream,
917                                         struct opj_event_mgr * p_manager
918                                         );
919
920 /**
921  * Reads a tile header.
922  * @param       p_j2k           the jpeg2000 codec.
923  * @param       p_stream                        the stream to write data to.
924  * @param       p_manager       the user event manager.
925  */
926 opj_bool j2k_read_tile_header (
927                                          opj_j2k_v2_t * p_j2k,
928                                          OPJ_UINT32 * p_tile_index,
929                                          OPJ_UINT32 * p_data_size,
930                                          OPJ_INT32 * p_tile_x0,
931                                          OPJ_INT32 * p_tile_y0,
932                                          OPJ_INT32 * p_tile_x1,
933                                          OPJ_INT32 * p_tile_y1,
934                                          OPJ_UINT32 * p_nb_comps,
935                                          opj_bool * p_go_on,
936                                          struct opj_stream_private *p_stream,
937                                          struct opj_event_mgr * p_manager
938                                         );
939
940
941 /**
942  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
943  *
944  * @param       p_j2k                   the jpeg2000 codec.
945  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
946  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
947  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
948  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
949  * @param       p_manager               the user event manager
950  *
951  * @return      true                    if the area could be set.
952  */
953 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
954                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
955                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
956                                                                 struct opj_event_mgr * p_manager );
957
958 /**
959  * Creates a J2K decompression structure.
960  *
961  * @return a handle to a J2K decompressor if successful, NULL otherwise.
962  */
963 opj_j2k_v2_t* j2k_create_decompress_v2();
964
965
966 /**
967  * Dump some elements from the J2K decompression structure .
968  *
969  *@param p_j2k                          the jpeg2000 codec.
970  *@param flag                           flag to describe what elments are dump.
971  *@param out_stream                     output stream where dump the elements.
972  *
973 */
974 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream);
975
976
977
978 /**
979  * Dump an image header structure.
980  *
981  *@param img_header                     the image header to dump.
982  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
983  *@param out_stream                     output stream where dump the elements.
984  */
985 void j2k_dump_image_header(opj_image_t* image, opj_bool dev_dump_flag, FILE* out_stream);
986
987 /**
988  * Dump a component image header structure.
989  *
990  *@param comp_header            the component image header to dump.
991  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
992  *@param out_stream                     output stream where dump the elements.
993  */
994 void j2k_dump_image_comp_header(opj_image_comp_t* comp, opj_bool dev_dump_flag, FILE* out_stream);
995
996 /**
997  * Get the codestream info from a JPEG2000 codec.
998  *
999  *@param        p_j2k                           the component image header to dump.
1000  *
1001  *@return       the codestream information extract from the jpg2000 codec
1002  */
1003 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k);
1004
1005 /**
1006  * Get the codestream index from a JPEG2000 codec.
1007  *
1008  *@param        p_j2k                           the component image header to dump.
1009  *
1010  *@return       the codestream index extract from the jpg2000 codec
1011  */
1012 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k);
1013
1014 /**
1015  * Decode an image from a JPEG-2000 codestream
1016  * @param j2k J2K decompressor handle
1017  * @param cio Input buffer stream
1018  * @param cstr_info Codestream information structure if required, NULL otherwise
1019  * @return Returns a decoded image if successful, returns NULL otherwise
1020 */
1021 opj_bool j2k_decode_v2(opj_j2k_v2_t *j2k, struct opj_stream_private *cio, opj_image_t* p_image, opj_event_mgr_t * p_manager);
1022
1023
1024 #endif /* __J2K_H */