5f8e08248809018a385ef97caacf12d4ebc5441e
[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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef __J2K_H
32 #define __J2K_H
33 /**
34 @file j2k.h
35 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
36
37 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
38 */
39
40 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /*@{*/
42
43 #define J2K_CP_CSTY_PRT 0x01
44 #define J2K_CP_CSTY_SOP 0x02
45 #define J2K_CP_CSTY_EPH 0x04
46 #define J2K_CCP_CSTY_PRT 0x01
47 #define J2K_CCP_CBLKSTY_LAZY 0x01
48 #define J2K_CCP_CBLKSTY_RESET 0x02
49 #define J2K_CCP_CBLKSTY_TERMALL 0x04
50 #define J2K_CCP_CBLKSTY_VSC 0x08
51 #define J2K_CCP_CBLKSTY_PTERM 0x10
52 #define J2K_CCP_CBLKSTY_SEGSYM 0x20
53 #define J2K_CCP_QNTSTY_NOQNT 0
54 #define J2K_CCP_QNTSTY_SIQNT 1
55 #define J2K_CCP_QNTSTY_SEQNT 2
56
57 /* ----------------------------------------------------------------------- */
58
59 #define J2K_MS_SOC 0xff4f       /**< SOC marker value */
60 #define J2K_MS_SOT 0xff90       /**< SOT marker value */
61 #define J2K_MS_SOD 0xff93       /**< SOD marker value */
62 #define J2K_MS_EOC 0xffd9       /**< EOC marker value */
63 #define J2K_MS_SIZ 0xff51       /**< SIZ marker value */
64 #define J2K_MS_COD 0xff52       /**< COD marker value */
65 #define J2K_MS_COC 0xff53       /**< COC marker value */
66 #define J2K_MS_RGN 0xff5e       /**< RGN marker value */
67 #define J2K_MS_QCD 0xff5c       /**< QCD marker value */
68 #define J2K_MS_QCC 0xff5d       /**< QCC marker value */
69 #define J2K_MS_POC 0xff5f       /**< POC marker value */
70 #define J2K_MS_TLM 0xff55       /**< TLM marker value */
71 #define J2K_MS_PLM 0xff57       /**< PLM marker value */
72 #define J2K_MS_PLT 0xff58       /**< PLT marker value */
73 #define J2K_MS_PPM 0xff60       /**< PPM marker value */
74 #define J2K_MS_PPT 0xff61       /**< PPT marker value */
75 #define J2K_MS_SOP 0xff91       /**< SOP marker value */
76 #define J2K_MS_EPH 0xff92       /**< EPH marker value */
77 #define J2K_MS_CRG 0xff63       /**< CRG marker value */
78 #define J2K_MS_COM 0xff64       /**< COM marker value */
79 /* UniPG>> */
80 #ifdef USE_JPWL
81 #define J2K_MS_EPC 0xff68       /**< EPC marker value (Part11) */
82 #define J2K_MS_EPB 0xff66       /**< EPB marker value (Part11) */ 
83 #define J2K_MS_ESD 0xff67       /**< ESD marker value (Part11) */ 
84 #define J2K_MS_RED 0xff69       /**< RED marker value (Part11) */
85 #endif /* USE_JPWL */
86 /* <<UniPG */
87
88 /* ----------------------------------------------------------------------- */
89
90 /**
91 Values that specify the status of the decoding process when decoding the main header. 
92 These values may be combined with a | operator. 
93 */
94 typedef enum J2K_STATUS {
95         J2K_STATE_MHSOC  = 0x0001, /**< a SOC marker is expected */
96         J2K_STATE_MHSIZ  = 0x0002, /**< a SIZ marker is expected */
97         J2K_STATE_MH     = 0x0004, /**< the decoding process is in the main header */
98         J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
99         J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */
100         J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */
101         J2K_STATE_NEOC   = 0x0040  /**< the decoding process must not expect a EOC marker because the codestream is truncated */
102 } J2K_STATUS;
103
104 /* ----------------------------------------------------------------------- */
105
106 /**
107 Quantization stepsize
108 */
109 typedef struct opj_stepsize {
110         /** exponent */
111         int expn;
112         /** mantissa */
113         int mant;
114 } opj_stepsize_t;
115
116 /**
117 Tile-component coding parameters
118 */
119 typedef struct opj_tccp {
120         /** coding style */
121         int csty;
122         /** number of resolutions */
123         int numresolutions;
124         /** code-blocks width */
125         int cblkw;
126         /** code-blocks height */
127         int cblkh;
128         /** code-block coding style */
129         int cblksty;
130         /** discrete wavelet transform identifier */
131         int qmfbid;
132         /** quantisation style */
133         int qntsty;
134         /** stepsizes used for quantization */
135         opj_stepsize_t stepsizes[J2K_MAXBANDS];
136         /** number of guard bits */
137         int numgbits;
138         /** Region Of Interest shift */
139         int roishift;
140         /** precinct width */
141         int prcw[J2K_MAXRLVLS];
142         /** precinct height */
143         int prch[J2K_MAXRLVLS]; 
144 } opj_tccp_t;
145
146 /**
147 Tile coding parameters : 
148 this structure is used to store coding/decoding parameters common to all
149 tiles (information like COD, COC in main header)
150 */
151 typedef struct opj_tcp {
152         /** 1 : first part-tile of a tile */
153         int first;
154         /** coding style */
155         int csty;
156         /** progression order */
157         OPJ_PROG_ORDER prg;
158         /** number of layers */
159         int numlayers;
160         /** multi-component transform identifier */
161         int mct;
162         /** rates of layers */
163         float rates[100];
164         /** number of progression order changes */
165         int numpocs;
166         /** indicates if a POC marker has been used O:NO, 1:YES */
167         int POC;
168         /** progression order changes */
169         opj_poc_t pocs[32];
170         /** packet header store there for futur use in t2_decode_packet */
171         unsigned char *ppt_data;
172         /** pointer remaining on the first byte of the first header if ppt is used */
173         unsigned char *ppt_data_first;
174         /** If ppt == 1 --> there was a PPT marker for the present tile */
175         int ppt;
176         /** used in case of multiple marker PPT (number of info already stored) */
177         int ppt_store;
178         /** ppmbug1 */
179         int ppt_len;
180         /** add fixed_quality */
181         float distoratio[100];
182         /** tile-component coding parameters */
183         opj_tccp_t *tccps;
184 } opj_tcp_t;
185
186 /**
187 Coding parameters
188 */
189 typedef struct opj_cp {
190         /** Digital cinema profile*/
191         OPJ_CINEMA_MODE cinema;
192         /** Size of the image in bits*/
193         int img_size;
194         /** Rsiz*/
195         OPJ_RSIZ_CAPABILITIES rsiz;
196         /** allocation by rate/distortion */
197         int disto_alloc;
198         /** allocation by fixed layer */
199         int fixed_alloc;
200         /** add fixed_quality */
201         int fixed_quality;
202         /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
203         int reduce;
204         /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
205         int layer;
206         /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */
207         OPJ_LIMIT_DECODING limit_decoding;
208         /** 0 = no index || 1 = index */
209         int index_on;
210         /** XTOsiz */
211         int tx0;
212         /** YTOsiz */
213         int ty0;
214         /** XTsiz */
215         int tdx;
216         /** YTsiz */
217         int tdy;
218         /** comment for coding */
219         char *comment;
220         /** number of tiles in width */
221         int tw;
222         /** number of tiles in heigth */
223         int th;
224         /** ID number of the tiles present in the codestream */
225         int *tileno;
226         /** size of the vector tileno */
227         int tileno_size;
228         /** packet header store there for futur use in t2_decode_packet */
229         unsigned char *ppm_data;
230         /** pointer remaining on the first byte of the first header if ppm is used */
231         unsigned char *ppm_data_first;
232         /** if ppm == 1 --> there was a PPM marker for the present tile */
233         int ppm;
234         /** use in case of multiple marker PPM (number of info already store) */
235         int ppm_store;
236         /** use in case of multiple marker PPM (case on non-finished previous info) */
237         int ppm_previous;
238         /** ppmbug1 */
239         int ppm_len;
240         /** tile coding parameters */
241         opj_tcp_t *tcps;
242         /** fixed layer */
243         int *matrice;
244 /* UniPG>> */
245 #ifdef USE_JPWL
246         /** enables writing of EPC in MH, thus activating JPWL */
247         bool epc_on;
248         /** enables writing of EPB, in case of activated JPWL */
249         bool epb_on;
250         /** enables writing of ESD, in case of activated JPWL */
251         bool esd_on;
252         /** enables writing of informative techniques of ESD, in case of activated JPWL */
253         bool info_on;
254         /** enables writing of RED, in case of activated JPWL */
255         bool red_on;
256         /** error protection method for MH (0,1,16,32,37-128) */
257         int hprot_MH;
258         /** tile number of header protection specification (>=0) */
259         int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
260         /** error protection methods for TPHs (0,1,16,32,37-128) */
261         int hprot_TPH[JPWL_MAX_NO_TILESPECS];
262         /** tile number of packet protection specification (>=0) */
263         int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
264         /** packet number of packet protection specification (>=0) */
265         int pprot_packno[JPWL_MAX_NO_PACKSPECS];
266         /** error protection methods for packets (0,1,16,32,37-128) */
267         int pprot[JPWL_MAX_NO_PACKSPECS];
268         /** enables writing of ESD, (0/2/4 bytes) */
269         int sens_size;
270         /** sensitivity addressing size (0=auto/2/4 bytes) */
271         int sens_addr;
272         /** sensitivity range (0-3) */
273         int sens_range;
274         /** sensitivity method for MH (-1,0-7) */
275         int sens_MH;
276         /** tile number of sensitivity specification (>=0) */
277         int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
278         /** sensitivity methods for TPHs (-1,0-7) */
279         int sens_TPH[JPWL_MAX_NO_TILESPECS];
280         /** enables JPWL correction at the decoder */
281         bool correct;
282         /** expected number of components at the decoder */
283         int exp_comps;
284         /** maximum number of tiles at the decoder */
285         int max_tiles;
286 #endif /* USE_JPWL */
287 /* <<UniPG */
288 } opj_cp_t;
289
290 /**
291 Information concerning a packet inside tile
292 */
293 typedef struct opj_packet_info {
294         /** start position */
295         int start_pos;
296         /** end position */
297         int end_pos;
298         /** ADD for Marcela */
299         double disto;
300 } opj_packet_info_t;
301
302 /**
303 Index structure : information regarding tiles inside image
304 */
305 typedef struct opj_tile_info {
306         /** value of thresh for each layer by tile cfr. Marcela   */
307         double *thresh;
308         /** number of tile */
309         int num_tile;
310         /** start position */
311         int start_pos;
312         /** end position of the header */
313         int end_header;
314         /** end position */
315         int end_pos;
316         /** precinct number for each resolution level (width) */
317         int pw[33];
318         /** precinct number for each resolution level (height) */
319         int ph[33];
320         /** precinct size (in power of 2), in X for each resolution level */
321         int pdx[33];
322         /** precinct size (in power of 2), in Y for each resolution level */
323         int pdy[33];
324         /** information concerning packets inside tile */
325         opj_packet_info_t *packet;
326         /** add fixed_quality */
327         int nbpix;
328         /** add fixed_quality */
329         double distotile;
330 } opj_tile_info_t;
331
332 /**
333 Index structure
334 */
335 typedef struct opj_image_info {
336         /** 0 = no index || 1 = index */
337         int index_on;
338         /** maximum distortion reduction on the whole image (add for Marcela) */
339         double D_max;
340         /** packet number */
341         int num;
342         /** writing the packet in the index with t2_encode_packets */
343         int index_write;
344         /** image width */
345         int image_w;
346         /** image height */
347         int image_h;
348         /** progression order */
349         OPJ_PROG_ORDER prog;
350         /** tile size in x */
351         int tile_x;
352         /** tile size in y */
353         int tile_y;
354         /** */
355         int tile_Ox;
356         /** */
357         int tile_Oy;
358         /** number of tiles in X */
359         int tw;
360         /** number of tiles in Y */
361         int th;
362         /** component numbers */
363         int comp;
364         /** number of layer */
365         int layer;
366         /** number of decomposition */
367         int decomposition;
368         /** main header position */
369         int main_head_end;
370         /** codestream's size */
371         int codestream_size;
372         /** information regarding tiles inside image */
373         opj_tile_info_t *tile;
374 } opj_image_info_t;
375
376 /**
377 JPEG-2000 codestream reader/writer
378 */
379 typedef struct opj_j2k {
380         /** codec context */
381         opj_common_ptr cinfo;
382
383         /** locate in which part of the codestream the decoder is (main header, tile header, end) */
384         int state;
385         /** number of the tile curently concern by coding/decoding */
386         int curtileno;
387         /** 
388         locate the position of the end of the tile in the codestream, 
389         used to detect a truncated codestream (in j2k_read_sod)
390         */
391         unsigned char *eot;
392         /**
393         locate the start position of the SOT marker of the current coded tile:  
394         after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. 
395         */
396         int sot_start;
397         int sod_start;
398         /**
399         as the J2K-file is written in several parts during encoding, 
400         it enables to make the right correction in position return by cio_tell
401         */
402         int pos_correction;
403         /** array used to store the data of each tile */
404         unsigned char **tile_data;
405         /** array used to store the length of each tile */
406         int *tile_len;
407         /** 
408         decompression only : 
409         store decoding parameters common to all tiles (information like COD, COC in main header)
410         */
411         opj_tcp_t *default_tcp;
412         /** pointer to the encoded / decoded image */
413         opj_image_t *image;
414         /** pointer to the coding parameters */
415         opj_cp_t *cp;
416         /** helper used to write the index file */
417         opj_image_info_t *image_info;
418         /** pointer to the byte i/o stream */
419         opj_cio_t *cio;
420 } opj_j2k_t;
421
422 /** @name Exported functions */
423 /*@{*/
424 /* ----------------------------------------------------------------------- */
425 /**
426 Creates a J2K decompression structure
427 @param cinfo Codec context info
428 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
429 */
430 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
431 /**
432 Destroy a J2K decompressor handle
433 @param j2k J2K decompressor handle to destroy
434 */
435 void j2k_destroy_decompress(opj_j2k_t *j2k);
436 /**
437 Setup the decoder decoding parameters using user parameters.
438 Decoding parameters are returned in j2k->cp. 
439 @param j2k J2K decompressor handle
440 @param parameters decompression parameters
441 */
442 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
443 /**
444 Decode an image from a JPEG-2000 codestream
445 @param j2k J2K decompressor handle
446 @param cio Input buffer stream
447 @return Returns a decoded image if successful, returns NULL otherwise
448 */
449 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio);
450 /**
451 Decode an image form a JPT-stream (JPEG 2000, JPIP)
452 @param j2k J2K decompressor handle
453 @param cio Input buffer stream
454 @return Returns a decoded image if successful, returns NULL otherwise
455 */
456 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio);
457 /**
458 Creates a J2K compression structure
459 @param cinfo Codec context info
460 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
461 */
462 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
463 /**
464 Destroy a J2K compressor handle
465 @param j2k J2K compressor handle to destroy
466 */
467 void j2k_destroy_compress(opj_j2k_t *j2k);
468 /**
469 Setup the encoder parameters using the current image and using user parameters. 
470 Coding parameters are returned in j2k->cp. 
471 @param j2k J2K compressor handle
472 @param parameters compression parameters
473 @param image input filled image
474 */
475 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
476 /**
477 Encode an image into a JPEG-2000 codestream
478 @param j2k J2K compressor handle
479 @param cio Output buffer stream
480 @param image Image to encode
481 @param index Name of the index file if required, NULL otherwise
482 @return Returns true if successful, returns false otherwise
483 */
484 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index);
485 /* ----------------------------------------------------------------------- */
486 /*@}*/
487
488 /*@}*/
489
490 #endif /* __J2K_H */