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