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