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