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