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