Changed the ROI parameter in the image_to_j2k codec to make it correspond to the...
[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
49 #define J2K_CCP_CBLKSTY_RESET 0x02
50 #define J2K_CCP_CBLKSTY_TERMALL 0x04
51 #define J2K_CCP_CBLKSTY_VSC 0x08
52 #define J2K_CCP_CBLKSTY_PTERM 0x10
53 #define J2K_CCP_CBLKSTY_SEGSYM 0x20
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 /* UniPG>> */
81 #ifdef USE_JPWL
82 #define J2K_MS_EPC 0xff68       /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
83 #define J2K_MS_EPB 0xff66       /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */ 
84 #define J2K_MS_ESD 0xff67       /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */ 
85 #define J2K_MS_RED 0xff69       /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
86 #endif /* USE_JPWL */
87 #ifdef USE_JPSEC
88 #define J2K_MS_SEC 0xff65    /**< SEC marker value (Part 8: Secure JPEG 2000) */
89 #define J2K_MS_INSEC 0xff94  /**< INSEC marker value (Part 8: Secure JPEG 2000) */
90 #endif /* USE_JPSEC */
91 /* <<UniPG */
92
93
94 /* ----------------------------------------------------------------------- */
95
96 /**
97 Values that specify the status of the decoding process when decoding the main header. 
98 These values may be combined with a | operator. 
99 */
100 typedef enum J2K_STATUS {
101         J2K_STATE_MHSOC  = 0x0001, /**< a SOC marker is expected */
102         J2K_STATE_MHSIZ  = 0x0002, /**< a SIZ marker is expected */
103         J2K_STATE_MH     = 0x0004, /**< the decoding process is in the main header */
104         J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
105         J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */
106         J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */
107         J2K_STATE_NEOC   = 0x0040  /**< the decoding process must not expect a EOC marker because the codestream is truncated */
108 } J2K_STATUS;
109
110 /* ----------------------------------------------------------------------- */
111
112 /** 
113 T2 encoding mode 
114 */
115 typedef enum T2_MODE {
116         THRESH_CALC = 0,        /** Function called in Rate allocation process*/
117         FINAL_PASS = 1          /** Function called in Tier 2 process*/
118 }J2K_T2_MODE;
119
120 /**
121 Quantization stepsize
122 */
123 typedef struct opj_stepsize {
124         /** exponent */
125         int expn;
126         /** mantissa */
127         int mant;
128 } opj_stepsize_t;
129
130 /**
131 Tile-component coding parameters
132 */
133 typedef struct opj_tccp {
134         /** coding style */
135         int csty;
136         /** number of resolutions */
137         int numresolutions;
138         /** code-blocks width */
139         int cblkw;
140         /** code-blocks height */
141         int cblkh;
142         /** code-block coding style */
143         int cblksty;
144         /** discrete wavelet transform identifier */
145         int qmfbid;
146         /** quantisation style */
147         int qntsty;
148         /** stepsizes used for quantization */
149         opj_stepsize_t stepsizes[J2K_MAXBANDS];
150         /** number of guard bits */
151         int numgbits;
152         /** Region Of Interest shift */
153         int roishift;
154         /** precinct width */
155         int prcw[J2K_MAXRLVLS];
156         /** precinct height */
157         int prch[J2K_MAXRLVLS]; 
158 } opj_tccp_t;
159
160 /**
161 Tile coding parameters : 
162 this structure is used to store coding/decoding parameters common to all
163 tiles (information like COD, COC in main header)
164 */
165 typedef struct opj_tcp {
166         /** 1 : first part-tile of a tile */
167         int first;
168         /** coding style */
169         int csty;
170         /** progression order */
171         OPJ_PROG_ORDER prg;
172         /** number of layers */
173         int numlayers;
174         /** multi-component transform identifier */
175         int mct;
176         /** rates of layers */
177         float rates[100];
178         /** number of progression order changes */
179         int numpocs;
180         /** indicates if a POC marker has been used O:NO, 1:YES */
181         int POC;
182         /** progression order changes */
183         opj_poc_t pocs[32];
184         /** packet header store there for futur use in t2_decode_packet */
185         unsigned char *ppt_data;
186         /** pointer remaining on the first byte of the first header if ppt is used */
187         unsigned char *ppt_data_first;
188         /** If ppt == 1 --> there was a PPT marker for the present tile */
189         int ppt;
190         /** used in case of multiple marker PPT (number of info already stored) */
191         int ppt_store;
192         /** ppmbug1 */
193         int ppt_len;
194         /** add fixed_quality */
195         float distoratio[100];
196         /** tile-component coding parameters */
197         opj_tccp_t *tccps;
198 } opj_tcp_t;
199
200 /**
201 Coding parameters
202 */
203 typedef struct opj_cp {
204         /** Digital cinema profile*/
205         OPJ_CINEMA_MODE cinema;
206         /** Maximum rate for each component. If == 0, component size limitation is not considered */
207         int max_comp_size;
208         /** Size of the image in bits*/
209         int img_size;
210         /** Rsiz*/
211         OPJ_RSIZ_CAPABILITIES rsiz;
212         /** Enabling Tile part generation*/
213         char tp_on;
214         /** Flag determining tile part generation*/
215         char tp_flag;
216         /** Position of tile part flag in progression order*/
217         int tp_pos;
218         /** allocation by rate/distortion */
219         int disto_alloc;
220         /** allocation by fixed layer */
221         int fixed_alloc;
222         /** add fixed_quality */
223         int fixed_quality;
224         /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
225         int reduce;
226         /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
227         int layer;
228         /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */
229         OPJ_LIMIT_DECODING limit_decoding;
230         /** XTOsiz */
231         int tx0;
232         /** YTOsiz */
233         int ty0;
234         /** XTsiz */
235         int tdx;
236         /** YTsiz */
237         int tdy;
238         /** comment for coding */
239         char *comment;
240         /** number of tiles in width */
241         int tw;
242         /** number of tiles in heigth */
243         int th;
244         /** ID number of the tiles present in the codestream */
245         int *tileno;
246         /** size of the vector tileno */
247         int tileno_size;
248         /** packet header store there for futur use in t2_decode_packet */
249         unsigned char *ppm_data;
250         /** pointer remaining on the first byte of the first header if ppm is used */
251         unsigned char *ppm_data_first;
252         /** if ppm == 1 --> there was a PPM marker for the present tile */
253         int ppm;
254         /** use in case of multiple marker PPM (number of info already store) */
255         int ppm_store;
256         /** use in case of multiple marker PPM (case on non-finished previous info) */
257         int ppm_previous;
258         /** ppmbug1 */
259         int ppm_len;
260         /** tile coding parameters */
261         opj_tcp_t *tcps;
262         /** fixed layer */
263         int *matrice;
264 /* UniPG>> */
265 #ifdef USE_JPWL
266         /** enables writing of EPC in MH, thus activating JPWL */
267         bool epc_on;
268         /** enables writing of EPB, in case of activated JPWL */
269         bool epb_on;
270         /** enables writing of ESD, in case of activated JPWL */
271         bool esd_on;
272         /** enables writing of informative techniques of ESD, in case of activated JPWL */
273         bool info_on;
274         /** enables writing of RED, in case of activated JPWL */
275         bool red_on;
276         /** error protection method for MH (0,1,16,32,37-128) */
277         int hprot_MH;
278         /** tile number of header protection specification (>=0) */
279         int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
280         /** error protection methods for TPHs (0,1,16,32,37-128) */
281         int hprot_TPH[JPWL_MAX_NO_TILESPECS];
282         /** tile number of packet protection specification (>=0) */
283         int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
284         /** packet number of packet protection specification (>=0) */
285         int pprot_packno[JPWL_MAX_NO_PACKSPECS];
286         /** error protection methods for packets (0,1,16,32,37-128) */
287         int pprot[JPWL_MAX_NO_PACKSPECS];
288         /** enables writing of ESD, (0/2/4 bytes) */
289         int sens_size;
290         /** sensitivity addressing size (0=auto/2/4 bytes) */
291         int sens_addr;
292         /** sensitivity range (0-3) */
293         int sens_range;
294         /** sensitivity method for MH (-1,0-7) */
295         int sens_MH;
296         /** tile number of sensitivity specification (>=0) */
297         int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
298         /** sensitivity methods for TPHs (-1,0-7) */
299         int sens_TPH[JPWL_MAX_NO_TILESPECS];
300         /** enables JPWL correction at the decoder */
301         bool correct;
302         /** expected number of components at the decoder */
303         int exp_comps;
304         /** maximum number of tiles at the decoder */
305         int max_tiles;
306 #endif /* USE_JPWL */
307 /* <<UniPG */
308 } opj_cp_t;
309
310 /**
311 JPEG-2000 codestream reader/writer
312 */
313 typedef struct opj_j2k {
314         /** codec context */
315         opj_common_ptr cinfo;
316
317         /** locate in which part of the codestream the decoder is (main header, tile header, end) */
318         int state;
319         /** number of the tile curently concern by coding/decoding */
320         int curtileno;
321         /** Tile part number*/
322         int tp_num;
323         /** Tilepart number currently coding*/
324         int cur_tp_num;
325         /** Total number of tileparts of the current tile*/
326         int *cur_totnum_tp;
327         /**
328         locate the start position of the TLM marker  
329         after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length. 
330         */
331         int tlm_start;
332         /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
333         /** used in TLMmarker*/
334         int totnum_tp;  
335         /** 
336         locate the position of the end of the tile in the codestream, 
337         used to detect a truncated codestream (in j2k_read_sod)
338         */
339         unsigned char *eot;
340         /**
341         locate the start position of the SOT marker of the current coded tile:  
342         after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. 
343         */
344         int sot_start;
345         int sod_start;
346         /**
347         as the J2K-file is written in several parts during encoding, 
348         it enables to make the right correction in position return by cio_tell
349         */
350         int pos_correction;
351         /** array used to store the data of each tile */
352         unsigned char **tile_data;
353         /** array used to store the length of each tile */
354         int *tile_len;
355         /** 
356         decompression only : 
357         store decoding parameters common to all tiles (information like COD, COC in main header)
358         */
359         opj_tcp_t *default_tcp;
360         /** pointer to the encoded / decoded image */
361         opj_image_t *image;
362         /** pointer to the coding parameters */
363         opj_cp_t *cp;
364         /** helper used to write the index file */
365         opj_codestream_info_t *cstr_info;
366         /** pointer to the byte i/o stream */
367         opj_cio_t *cio;
368 } opj_j2k_t;
369
370 /** @name Exported functions */
371 /*@{*/
372 /* ----------------------------------------------------------------------- */
373 /**
374 Creates a J2K decompression structure
375 @param cinfo Codec context info
376 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
377 */
378 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
379 /**
380 Destroy a J2K decompressor handle
381 @param j2k J2K decompressor handle to destroy
382 */
383 void j2k_destroy_decompress(opj_j2k_t *j2k);
384 /**
385 Setup the decoder decoding parameters using user parameters.
386 Decoding parameters are returned in j2k->cp. 
387 @param j2k J2K decompressor handle
388 @param parameters decompression parameters
389 */
390 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
391 /**
392 Decode an image from a JPEG-2000 codestream
393 @param j2k J2K decompressor handle
394 @param cio Input buffer stream
395 @param cstr_info Codestream information structure if required, NULL otherwise
396 @return Returns a decoded image if successful, returns NULL otherwise
397 */
398 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
399 /**
400 Decode an image form a JPT-stream (JPEG 2000, JPIP)
401 @param j2k J2K decompressor handle
402 @param cio Input buffer stream
403 @param cstr_info Codestream information structure if required, NULL otherwise
404 @return Returns a decoded image if successful, returns NULL otherwise
405 */
406 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
407 /**
408 Creates a J2K compression structure
409 @param cinfo Codec context info
410 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
411 */
412 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
413 /**
414 Destroy a J2K compressor handle
415 @param j2k J2K compressor handle to destroy
416 */
417 void j2k_destroy_compress(opj_j2k_t *j2k);
418 /**
419 Setup the encoder parameters using the current image and using user parameters. 
420 Coding parameters are returned in j2k->cp. 
421 @param j2k J2K compressor handle
422 @param parameters compression parameters
423 @param image input filled image
424 */
425 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
426 /**
427 Converts an enum type progression order to string type
428 */
429 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
430 /**
431 Encode an image into a JPEG-2000 codestream
432 @param j2k J2K compressor handle
433 @param cio Output buffer stream
434 @param image Image to encode
435 @param cstr_info Codestream information structure if required, NULL otherwise
436 @return Returns true if successful, returns false otherwise
437 */
438 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
439
440 /* ----------------------------------------------------------------------- */
441 /*@}*/
442
443 /*@}*/
444
445 #endif /* __J2K_H */