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