5dbb1b2511fa1e38615fa44b4e70963ccf4d7311
[openjpeg.git] / libopenjpeg / openjpeg.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  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 #ifndef OPENJPEG_H
34 #define OPENJPEG_H
35
36 #if defined(OPJ_STATIC) || (!defined(WIN32) && !defined(__MINGW32__)) || defined(__WIN32__)
37         #define OPJ_API
38         #define OPJ_CALLCONV
39 #else
40         #define OPJ_CALLCONV __stdcall
41         #ifdef OPJ_EXPORTS
42                 #define OPJ_API __declspec(dllexport)
43         #else
44                 #define OPJ_API __declspec(dllimport)
45         #endif /* OPJ_EXPORTS */
46 #endif /* !OPJ_STATIC || !WIN32 */
47
48 #ifndef __cplusplus
49         #if defined(HAVE_STDBOOL_H)
50                 #include <stdbool.h>
51         #else
52                 #if !defined(bool)
53                         #define bool    int
54                 #endif
55                 #if !defined(true)
56                         #define true    1
57                 #endif
58                 #if !defined(false)
59                         #define false   0
60                 #endif
61         #endif
62 #endif /* __cplusplus */
63 typedef unsigned int    OPJ_UINT32;
64 typedef int             OPJ_INT32;      
65 typedef unsigned short  OPJ_UINT16;     
66 typedef short           OPJ_INT16;      
67 typedef char            OPJ_CHAR;
68 typedef unsigned char   OPJ_BYTE;
69 typedef unsigned int    OPJ_SIZE_T;
70 typedef double          OPJ_FLOAT64;
71 typedef float           OPJ_FLOAT32;
72 #if defined(_MSC_VER) || defined(__BORLANDC__)
73         typedef __int64         OPJ_INT64;
74 #else
75         typedef long long       OPJ_INT64;
76 #endif
77
78 #define OPENJPEG_VERSION "1.2.0"
79 /* 
80 ==========================================================
81    Compiler directives
82 ==========================================================
83 */
84 #include <stdio.h>
85
86
87
88
89
90 /* 
91 ==========================================================
92    Useful constant definitions
93 ==========================================================
94 */
95
96 #define OPJ_PATH_LEN                                    4096 /**< Maximum allowed size for filenames */
97 #define J2K_MAXRLVLS                                    33                                      /**< Number of maximum resolution level authorized */
98 #define J2K_MAXBANDS                                    (3*J2K_MAXRLVLS-2)      /**< Number of maximum sub-band linked to number of resolution level */
99 #define J2K_DEFAULT_NB_SEGS                             10
100 #define J2K_STREAM_CHUNK_SIZE                   0x100000 /** 1 mega by default */
101 #define J2K_DEFAULT_HEADER_SIZE                 1000
102 #define J2K_MCC_DEFAULT_NB_RECORDS              10
103 #define J2K_MCT_DEFAULT_NB_RECORDS              10
104
105 /* UniPG>> */
106 #define JPWL_MAX_NO_TILESPECS   16 /**< Maximum number of tile parts expected by JPWL: increase at your will */
107 #define JPWL_MAX_NO_PACKSPECS   16 /**< Maximum number of packet parts expected by JPWL: increase at your will */
108 #define JPWL_MAX_NO_MARKERS     512 /**< Maximum number of JPWL markers: increase at your will */
109 #define JPWL_PRIVATEINDEX_NAME "jpwl_index_privatefilename" /**< index file name used when JPWL is on */
110 #define JPWL_EXPECTED_COMPONENTS 3 /**< Expect this number of components, so you'll find better the first EPB */
111 #define JPWL_MAXIMUM_TILES 8192 /**< Expect this maximum number of tiles, to avoid some crashes */
112 #define JPWL_MAXIMUM_HAMMING 2 /**< Expect this maximum number of bit errors in marker id's */
113 #define JPWL_MAXIMUM_EPB_ROOM 65450 /**< Expect this maximum number of bytes for composition of EPBs */
114 /* <<UniPG */
115
116 /* 
117 ==========================================================
118    enum definitions
119 ==========================================================
120 */
121 /** 
122 Rsiz Capabilities
123 */
124 typedef enum RSIZ_CAPABILITIES {
125         STD_RSIZ = 0,           /** Standard JPEG2000 profile*/
126         CINEMA2K = 3,           /** Profile name for a 2K image*/
127         CINEMA4K = 4,           /** Profile name for a 4K image*/
128         MCT = 0x8100
129 } OPJ_RSIZ_CAPABILITIES;
130
131 /** 
132 Digital cinema operation mode 
133 */
134 typedef enum CINEMA_MODE {
135         OFF = 0,                                        /** Not Digital Cinema*/
136         CINEMA2K_24 = 1,        /** 2K Digital Cinema at 24 fps*/
137         CINEMA2K_48 = 2,        /** 2K Digital Cinema at 48 fps*/
138         CINEMA4K_24 = 3         /** 4K Digital Cinema at 24 fps*/
139 }OPJ_CINEMA_MODE;
140
141 /** 
142 Progression order 
143 */
144 typedef enum PROG_ORDER {
145         PROG_UNKNOWN = -1,      /**< place-holder */
146         LRCP = 0,               /**< layer-resolution-component-precinct order */
147         RLCP = 1,               /**< resolution-layer-component-precinct order */
148         RPCL = 2,               /**< resolution-precinct-component-layer order */
149         PCRL = 3,               /**< precinct-component-resolution-layer order */
150         CPRL = 4                /**< component-precinct-resolution-layer order */
151 } OPJ_PROG_ORDER;
152
153 /**
154 Supported image color spaces
155 */
156 typedef enum COLOR_SPACE {      
157         CLRSPC_UNKNOWN = -1,    /**< not supported by the library */
158         CLRSPC_UNSPECIFIED = 0, /**< not specified in the codestream */ 
159         CLRSPC_SRGB = 1,                /**< sRGB */
160         CLRSPC_GRAY = 2,                /**< grayscale */
161         CLRSPC_SYCC = 3                 /**< YUV */
162 } OPJ_COLOR_SPACE;
163
164 /**
165 Supported codec
166 */
167 typedef enum CODEC_FORMAT {
168         CODEC_UNKNOWN = -1,     /**< place-holder */
169         CODEC_J2K = 0,          /**< JPEG-2000 codestream : read/write */
170         CODEC_JPT = 1,          /**< JPT-stream (JPEG 2000, JPIP) : read only */
171         CODEC_JP2 = 2           /**< JPEG-2000 file format : read/write */
172 } OPJ_CODEC_FORMAT;
173
174
175
176
177 /* 
178 ==========================================================
179    event manager typedef definitions
180 ==========================================================
181 */
182
183 /**
184 Callback function prototype for events
185 @param msg Event message
186 @param client_data 
187 */
188 typedef void (*opj_msg_callback) (const OPJ_CHAR *msg, void *client_data);
189
190
191
192
193 /* 
194 ==========================================================
195    codec typedef definitions
196 ==========================================================
197 */
198
199 /**
200 Progression order changes
201 */
202 typedef struct opj_poc 
203 {
204         /** Resolution num start, Component num start, given by POC */
205         OPJ_UINT32 resno0, compno0;
206         /** Layer num end,Resolution num end, Component num end, given by POC */
207         OPJ_UINT32 layno1, resno1, compno1;
208         /** Layer num start,Precinct num start, Precinct num end */
209         OPJ_UINT32 layno0, precno0, precno1;
210         /** Progression order enum*/
211         OPJ_PROG_ORDER prg1,prg;
212         /** Progression order string*/
213         OPJ_CHAR progorder[5];
214         /** Tile number */
215         OPJ_UINT32 tile;
216         /** Start and end values for Tile width and height*/
217         OPJ_INT32 tx0,tx1,ty0,ty1;
218         /** Start value, initialised in pi_initialise_encode*/
219         OPJ_UINT32 layS, resS, compS, prcS;
220         /** End value, initialised in pi_initialise_encode */
221         OPJ_UINT32 layE, resE, compE, prcE;
222         /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/
223         OPJ_UINT32 txS,txE,tyS,tyE,dx,dy;
224         /** Temporary values for Tile parts, initialised in pi_create_encode */
225         OPJ_UINT32 lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t;
226 } opj_poc_t;
227
228 /**
229 Compression parameters
230 */
231 typedef struct opj_cparameters {
232         /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */
233         bool tile_size_on;
234         /** XTOsiz */
235         int cp_tx0;
236         /** YTOsiz */
237         int cp_ty0;
238         /** XTsiz */
239         int cp_tdx;
240         /** YTsiz */
241         int cp_tdy;
242         /** allocation by rate/distortion */
243         int cp_disto_alloc;
244         /** allocation by fixed layer */
245         int cp_fixed_alloc;
246         /** add fixed_quality */
247         int cp_fixed_quality;
248         /** fixed layer */
249         int *cp_matrice;
250         /** comment for coding */
251         char *cp_comment;
252         /** csty : coding style */
253         int csty;
254         /** progression order (default LRCP) */
255         OPJ_PROG_ORDER prog_order;
256         /** progression order changes */
257         opj_poc_t POC[32];
258         /** number of progression order changes (POC), default to 0 */
259         int numpocs;
260         /** number of layers */
261         int tcp_numlayers;
262         /** rates of layers */
263         float tcp_rates[100];
264         /** different psnr for successive layers */
265         float tcp_distoratio[100];
266         /** number of resolutions */
267         int numresolution;
268         /** initial code block width, default to 64 */
269         int cblockw_init;
270         /** initial code block height, default to 64 */
271         int cblockh_init;
272         /** mode switch (cblk_style) */
273         int mode;
274         /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */
275         int irreversible;
276         /** region of interest: affected component in [0..3], -1 means no ROI */
277         int roi_compno;
278         /** region of interest: upshift value */
279         int roi_shift;
280         /* number of precinct size specifications */
281         int res_spec;
282         /** initial precinct width */
283         int prcw_init[J2K_MAXRLVLS];
284         /** initial precinct height */
285         int prch_init[J2K_MAXRLVLS];
286
287         /**@name command line encoder parameters (not used inside the library) */
288         /*@{*/
289         /** input file name */
290         char infile[OPJ_PATH_LEN];
291         /** output file name */
292         char outfile[OPJ_PATH_LEN];
293         /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
294         int index_on;
295         /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
296         char index[OPJ_PATH_LEN];
297         /** subimage encoding: origin image offset in x direction */
298         int image_offset_x0;
299         /** subimage encoding: origin image offset in y direction */
300         int image_offset_y0;
301         /** subsampling value for dx */
302         int subsampling_dx;
303         /** subsampling value for dy */
304         int subsampling_dy;
305         /** input file format 0: PGX, 1: PxM, 2: BMP 3:TIF*/
306         int decod_format;
307         /** output file format 0: J2K, 1: JP2, 2: JPT */
308         int cod_format;
309         /*@}*/
310
311 /* UniPG>> */
312         /**@name JPWL encoding parameters */
313         /*@{*/
314         /** enables writing of EPC in MH, thus activating JPWL */
315         bool jpwl_epc_on;
316         /** error protection method for MH (0,1,16,32,37-128) */
317         int jpwl_hprot_MH;
318         /** tile number of header protection specification (>=0) */
319         int jpwl_hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
320         /** error protection methods for TPHs (0,1,16,32,37-128) */
321         int jpwl_hprot_TPH[JPWL_MAX_NO_TILESPECS];
322         /** tile number of packet protection specification (>=0) */
323         int jpwl_pprot_tileno[JPWL_MAX_NO_PACKSPECS];
324         /** packet number of packet protection specification (>=0) */
325         int jpwl_pprot_packno[JPWL_MAX_NO_PACKSPECS];
326         /** error protection methods for packets (0,1,16,32,37-128) */
327         int jpwl_pprot[JPWL_MAX_NO_PACKSPECS];
328         /** enables writing of ESD, (0=no/1/2 bytes) */
329         int jpwl_sens_size;
330         /** sensitivity addressing size (0=auto/2/4 bytes) */
331         int jpwl_sens_addr;
332         /** sensitivity range (0-3) */
333         int jpwl_sens_range;
334         /** sensitivity method for MH (-1=no,0-7) */
335         int jpwl_sens_MH;
336         /** tile number of sensitivity specification (>=0) */
337         int jpwl_sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
338         /** sensitivity methods for TPHs (-1=no,0-7) */
339         int jpwl_sens_TPH[JPWL_MAX_NO_TILESPECS];
340         /*@}*/
341 /* <<UniPG */
342
343         /** Digital Cinema compliance 0-not compliant, 1-compliant*/
344         OPJ_CINEMA_MODE cp_cinema;
345         /** Maximum rate for each component. If == 0, component size limitation is not considered */
346         int max_comp_size;
347         /** Profile name*/
348         OPJ_RSIZ_CAPABILITIES cp_rsiz;
349         /** Tile part generation*/
350         char tp_on;
351         /** Flag for Tile part generation*/
352         char tp_flag;
353         /** MCT (multiple component transform) */
354         char tcp_mct;
355         /** Naive implementation of MCT restricted to a single reversible array based encoding without offset concerning all the components. */
356         void * mct_data;
357
358 } opj_cparameters_t;
359
360 /**
361 Decompression parameters
362 */
363 typedef struct opj_dparameters {
364         /** 
365         Set the number of highest resolution levels to be discarded. 
366         The image resolution is effectively divided by 2 to the power of the number of discarded levels. 
367         The reduce factor is limited by the smallest total number of decomposition levels among tiles.
368         if != 0, then original dimension divided by 2^(reduce); 
369         if == 0 or not used, image is decoded to the full resolution 
370         */
371         int cp_reduce;
372         /** 
373         Set the maximum number of quality layers to decode. 
374         If there are less quality layers than the specified number, all the quality layers are decoded.
375         if != 0, then only the first "layer" layers are decoded; 
376         if == 0 or not used, all the quality layers are decoded 
377         */
378         int cp_layer;
379
380         /**
381          * Restrictive decoding parameters.
382          */
383         OPJ_INT32 m_decode_start_x;
384         OPJ_INT32 m_decode_start_y ;
385         OPJ_INT32 m_decode_end_x ;
386         OPJ_INT32 m_decode_end_y ;
387
388         /**@name command line encoder parameters (not used inside the library) */
389         /*@{*/
390         /** input file name */
391         char infile[OPJ_PATH_LEN];
392         /** output file name */
393         char outfile[OPJ_PATH_LEN];
394         /** input file format 0: J2K, 1: JP2, 2: JPT */
395         int decod_format;
396         /** output file format 0: PGX, 1: PxM, 2: BMP */
397         int cod_format;
398         /*@}*/
399
400 /* UniPG>> */
401         /**@name JPWL decoding parameters */
402         /*@{*/
403         /** activates the JPWL correction capabilities */
404         bool jpwl_correct;
405         /** expected number of components */
406         int jpwl_exp_comps;
407         /** maximum number of tiles */
408         int jpwl_max_tiles;
409         
410         /** use restrictive decoding ? */
411         OPJ_UINT32 m_use_restrict_decode : 1;
412         /*@}*/
413 /* <<UniPG */
414
415 } opj_dparameters_t;
416
417 /**
418  * J2k codec.
419  */
420 typedef void * opj_codec_t;
421
422
423 /* 
424 ==========================================================
425    I/O stream typedef definitions
426 ==========================================================
427 */
428
429 /*
430  * Stream open flags.
431  */
432 /** The stream was opened for reading. */
433 #define OPJ_STREAM_READ 0x0001
434 /** The stream was opened for writing. */
435 #define OPJ_STREAM_WRITE 0x0002
436
437
438 typedef OPJ_UINT32 (* opj_stream_read_fn) (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) ;
439 typedef OPJ_UINT32 (* opj_stream_write_fn) (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) ;
440 typedef OPJ_SIZE_T (* opj_stream_skip_fn) (OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
441 typedef bool (* opj_stream_seek_fn) (OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
442
443
444 typedef void * opj_stream_t;
445
446 /* 
447 ==========================================================
448    image typedef definitions
449 ==========================================================
450 */
451
452 /**
453 Defines a single image component
454 */
455 typedef struct opj_image_comp {
456         /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
457         OPJ_UINT32 dx;
458         /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
459         OPJ_UINT32 dy;
460         /** data width */
461         OPJ_UINT32 w;
462         /** data height */
463         OPJ_UINT32 h;
464         /** x component offset compared to the whole image */
465         OPJ_INT32 x0;
466         /** y component offset compared to the whole image */
467         OPJ_INT32 y0;
468         /** precision */
469         OPJ_UINT32 prec;
470         /** number of decoded resolution */
471         OPJ_UINT32 resno_decoded;
472         /** number of division by 2 of the out image compared to the original size of image */
473         OPJ_UINT32 factor;
474         /** image component data */
475         int *data;
476         /** signed (1) / unsigned (0) */
477         OPJ_UINT32 sgnd : 1;
478         
479 } opj_image_comp_t;
480
481 /** 
482 Defines image data and characteristics
483 */
484 typedef struct opj_image 
485 {
486         /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area */
487         OPJ_INT32 x0;
488         /** YOsiz: vertical offset from the origin of the reference grid to the top side of the image area */
489         OPJ_INT32 y0;
490         /** Xsiz: width of the reference grid */
491         OPJ_INT32 x1;
492         /** Ysiz: height of the reference grid */
493         OPJ_INT32 y1;
494         /** number of components in the image */
495         OPJ_UINT32 numcomps;
496         /** color space: sRGB, Greyscale or YUV */
497         OPJ_COLOR_SPACE color_space;
498         /** image components */
499         opj_image_comp_t *comps;
500 } opj_image_t;
501
502 /**
503 Component parameters structure used by the opj_image_create function
504 */
505 typedef struct opj_image_comptparm {
506         /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
507         unsigned int dx;
508         /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
509         unsigned int dy;
510         /** data width */
511         unsigned int w;
512         /** data height */
513         unsigned int h;
514         /** x component offset compared to the whole image */
515         int x0;
516         /** y component offset compared to the whole image */
517         int y0;
518         /** precision */
519         int prec;
520         /** image depth in bits */
521         int bpp;
522         /** signed (1) / unsigned (0) */
523         int sgnd;
524 } opj_image_cmptparm_t;
525
526 /* 
527 ==========================================================
528    Information on the JPEG 2000 codestream
529 ==========================================================
530 */
531
532 /**
533 Index structure : Information concerning a packet inside tile
534 */
535 typedef struct opj_packet_info {
536         /** packet start position (including SOP marker if it exists) */
537         int start_pos;
538         /** end of packet header position (including EPH marker if it exists)*/
539         int end_ph_pos;
540         /** packet end position */
541         int end_pos;
542         /** packet distorsion */
543         double disto;
544 } opj_packet_info_t;
545
546 /**
547 Index structure : Information concerning tile-parts
548 */
549 typedef struct opj_tp_info {
550         /** start position of tile part */
551         int tp_start_pos;
552         /** end position of tile part header */
553         int tp_end_header;
554         /** end position of tile part */
555         int tp_end_pos;
556         /** start packet of tile part */
557         int tp_start_pack;
558         /** number of packets of tile part */
559         int tp_numpacks;
560 } opj_tp_info_t;
561
562 /**
563 Index structure : information regarding tiles 
564 */
565 typedef struct opj_tile_info {
566         /** value of thresh for each layer by tile cfr. Marcela   */
567         double *thresh;
568         /** number of tile */
569         int tileno;
570         /** start position */
571         int start_pos;
572         /** end position of the header */
573         int end_header;
574         /** end position */
575         int end_pos;
576         /** precinct number for each resolution level (width) */
577         int pw[33];
578         /** precinct number for each resolution level (height) */
579         int ph[33];
580         /** precinct size (in power of 2), in X for each resolution level */
581         int pdx[33];
582         /** precinct size (in power of 2), in Y for each resolution level */
583         int pdy[33];
584         /** information concerning packets inside tile */
585         opj_packet_info_t *packet;
586         /** add fixed_quality */
587         int numpix;
588         /** add fixed_quality */
589         double distotile;
590         /** number of tile parts */
591         int num_tps;
592         /** information concerning tile parts */
593         opj_tp_info_t *tp;
594 } opj_tile_info_t;
595
596 /* UniPG>> */
597 /**
598 Marker structure
599 */
600 typedef struct opj_marker_info_t {
601         /** marker type */
602         unsigned short int type;
603         /** position in codestream */
604         int pos;
605         /** length, marker val included */
606         int len;
607 } opj_marker_info_t;
608 /* <<UniPG */
609
610 /**
611 Index structure of the codestream
612 */
613 typedef struct opj_codestream_info {
614         /** maximum distortion reduction on the whole image (add for Marcela) */
615         double D_max;
616         /** packet number */
617         int packno;
618         /** writing the packet in the index with t2_encode_packets */
619         int index_write;
620         /** image width */
621         int image_w;
622         /** image height */
623         int image_h;
624         /** progression order */
625         OPJ_PROG_ORDER prog;
626         /** tile size in x */
627         int tile_x;
628         /** tile size in y */
629         int tile_y;
630         /** */
631         int tile_Ox;
632         /** */
633         int tile_Oy;
634         /** number of tiles in X */
635         int tw;
636         /** number of tiles in Y */
637         int th;
638         /** component numbers */
639         int numcomps;
640         /** number of layer */
641         int numlayers;
642         /** number of decomposition for each component */
643         int *numdecompos;
644 /* UniPG>> */
645         /** number of markers */
646         int marknum;
647         /** list of markers */
648         opj_marker_info_t *marker;
649         /** actual size of markers array */
650         int maxmarknum;
651 /* <<UniPG */
652         /** main header position */
653         int main_head_start;
654         /** main header position */
655         int main_head_end;
656         /** codestream's size */
657         int codestream_size;
658         /** information regarding tiles inside image */
659         opj_tile_info_t *tile;
660 } opj_codestream_info_t;
661
662 #ifdef __cplusplus
663 extern "C" {
664 #endif
665
666
667 /* 
668 ==========================================================
669    openjpeg version
670 ==========================================================
671 */
672
673 OPJ_API const OPJ_CHAR * OPJ_CALLCONV opj_version(void);
674
675 /* 
676 ==========================================================
677    image functions definitions
678 ==========================================================
679 */
680
681 /**
682 Create an image
683 @param numcmpts number of components
684 @param cmptparms components parameters
685 @param clrspc image color space
686 @return returns a new image structure if successful, returns NULL otherwise
687 */
688 OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
689
690 /**
691  * Creates an image without allocating memory for the image (used in the new version of the library).
692  * 
693  * @param       p_num_cmpts                     the number of components
694  * @param       p_cmpt_parms            the components parameters
695  * @param       p_clr_spc                       the image color space
696  * 
697  * @return      a new image structure if successful, NULL otherwise.
698 */
699 OPJ_API opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
700
701 /**
702 Deallocate any resources associated with an image
703 @param image image to be destroyed
704 */
705 OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image);
706
707 /* 
708 ==========================================================
709    stream functions definitions
710 ==========================================================
711 */
712
713 /**
714  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
715  *
716  * @param       l_is_reader             if set to true then the stream will be an input stream, an output stream else.
717  * 
718  * @return      a stream object.
719 */
720 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(bool p_is_input);
721 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_UINT32 p_size,bool p_is_input);
722
723 /**
724  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must 
725  * close its own implementation of the stream.
726  * 
727  * @param       p_stream        the stream to destroy.
728  */
729 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream);
730
731 /**
732  * Sets the given function to be used as a read function.
733  * @param               p_stream        the stream to modify
734  * @param               p_function      the function to use a read function.
735 */
736 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function);
737
738 /**
739  * Sets the given function to be used as a write function.
740  * @param               p_stream        the stream to modify
741  * @param               p_function      the function to use a write function.
742 */
743 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function);
744
745 /**
746  * Sets the given function to be used as a skip function.
747  * @param               p_stream        the stream to modify
748  * @param               p_function      the function to use a skip function.
749 */
750 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function);
751
752 /**
753  * Sets the given function to be used as a seek function, the stream is then seekable.
754  * @param               p_stream        the stream to modify
755  * @param               p_function      the function to use a skip function.
756 */
757 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function);
758
759
760 /**
761  * Sets the given data to be used as a user data for the stream.
762  * @param               p_stream        the stream to modify
763  * @param               p_data          the data to set.
764 */
765 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data);
766
767 /**
768  * Helper function.
769  * Sets the stream to be a file stream. The FILE must have been open previously.
770  * @param               p_stream        the stream to modify
771  * @param               p_file          handler to an already open file.
772 */
773 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file,bool p_is_read_stream);
774 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file,OPJ_UINT32 p_buffer_size, bool p_is_read_stream);
775
776
777
778 /* 
779 ==========================================================
780    event manager functions definitions
781 ==========================================================
782 */
783 OPJ_API bool OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data);
784 OPJ_API bool OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data);
785 OPJ_API bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data);
786
787
788
789 /* 
790 ==========================================================
791    codec functions definitions
792 ==========================================================
793 */
794 /**
795 Creates a J2K/JPT/JP2 decompression structure
796 @param format Decoder to select
797 @return Returns a handle to a decompressor if successful, returns NULL otherwise
798 */
799 OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
800
801 /**
802 Set decoding parameters to default values
803 @param parameters Decompression parameters
804 */
805 OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
806
807 #ifdef USE_OPJ_DEPRECATED
808 #ifdef _MSC_VER
809 #pragma message ( "warning, opj_setup_decoder is deprecated")
810 #else
811 #warning "warning, opj_setup_decoder is deprecated"
812 #endif
813 /**
814 Setup the decoder decoding parameters using user parameters.
815 Decoding parameters are returned in j2k->cp. 
816 @param dinfo decompressor handle
817 @param parameters decompression parameters
818 */
819 OPJ_API bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *dinfo, opj_dparameters_t *parameters);
820 #endif
821
822 /**
823 Decode an image from a JPEG-2000 codestream 
824 @param dinfo decompressor handle
825 @param cio Input buffer stream
826 @return Returns a decoded image if successful, returns NULL otherwise
827 */
828 OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_codec_t *p_decompressor, opj_stream_t * cio);
829
830 /**
831  * Writes a tile with the given data.
832  * 
833  * @param       p_compressor            the jpeg2000 codec.
834  * @param       p_tile_index            the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
835  * @param       p_data                          pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
836  * @param       p_data_size                     this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
837  *                                                              depending on the precision of the given component.
838  * @param       p_stream                        the stream to write data to.
839  * 
840  * @return      true if the data could be written.
841  */
842 OPJ_API bool OPJ_CALLCONV opj_write_tile (
843                                          opj_codec_t *p_codec,
844                                          OPJ_UINT32 p_tile_index,
845                                          OPJ_BYTE * p_data,
846                                          OPJ_UINT32 p_data_size,
847                                          opj_stream_t *p_stream
848                                         );
849
850 /**
851  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
852  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
853  *
854  * @param       p_codec                 the jpeg2000 codec.
855  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
856  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
857  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
858  *                                                      as depicted in opj_write_tile.
859  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
860  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
861  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
862  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
863  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
864  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
865  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
866  *                                                      the decoding.
867  * @param       p_stream                the stream to decode.
868  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
869  *                                                      returning false may be the result of a shortage of memory or an internal error.
870  */                             
871 OPJ_API bool OPJ_CALLCONV opj_read_tile_header(
872                                         opj_codec_t *p_codec,
873                                         OPJ_UINT32 * p_tile_index,
874                                         OPJ_UINT32 * p_data_size,
875                                         OPJ_INT32 * p_tile_x0,
876                                         OPJ_INT32 * p_tile_y0,
877                                         OPJ_INT32 * p_tile_x1,
878                                         OPJ_INT32 * p_tile_y1,
879                                         OPJ_UINT32 * p_nb_comps,
880                                         bool * p_should_go_on,
881                                         opj_stream_t * p_stream);
882
883
884 /**
885  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
886  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
887  *
888  * @param       p_codec                 the jpeg2000 codec.
889  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
890  * @param       p_data                  pointer to a memory block that will hold the decoded data.
891  * @param       p_data_size             size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
892  * @param       p_stream                the stream to decode.
893  *
894  * @return      true                    if the data could be decoded.
895  */                             
896 OPJ_API bool OPJ_CALLCONV opj_decode_tile_data(
897                                         opj_codec_t *p_codec,
898                                         OPJ_UINT32 p_tile_index,
899                                         OPJ_BYTE * p_data,
900                                         OPJ_UINT32 p_data_size,
901                                         opj_stream_t *p_stream
902                                         );
903
904 /**
905  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
906  * 
907  * @param       p_codec                 the jpeg2000 codec.
908  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
909  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
910  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
911  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
912  *
913  * @return      true                    if the area could be set.
914  */                             
915 OPJ_API bool OPJ_CALLCONV opj_set_decode_area(
916                                         opj_codec_t *p_codec,
917                                         OPJ_INT32 p_start_x,
918                                         OPJ_INT32 p_start_y,
919                                         OPJ_INT32 p_end_x,
920                                         OPJ_INT32 p_end_y
921                                         );
922
923
924 /**
925 Decode an image from a JPEG-2000 codestream and extract the codestream information
926 @param dinfo decompressor handle
927 @param cio Input buffer stream
928 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
929 @return Returns a decoded image if successful, returns NULL otherwise
930 */
931 //OPJ_API opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_stream_t cio, opj_codestream_info_t *cstr_info);
932 /**
933 Creates a J2K/JP2 compression structure
934 @param format Coder to select
935 @return Returns a handle to a compressor if successful, returns NULL otherwise
936 */
937 OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
938
939 /**
940 Destroy a decompressor handle
941 @param dinfo decompressor handle to destroy
942 */
943 OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec);
944
945 /**
946 Set encoding parameters to default values, that means : 
947 <ul>
948 <li>Lossless
949 <li>1 tile
950 <li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
951 <li>Size of code-block : 64 x 64
952 <li>Number of resolutions: 6
953 <li>No SOP marker in the codestream
954 <li>No EPH marker in the codestream
955 <li>No sub-sampling in x or y direction
956 <li>No mode switch activated
957 <li>Progression order: LRCP
958 <li>No index file
959 <li>No ROI upshifted
960 <li>No offset of the origin of the image
961 <li>No offset of the origin of the tiles
962 <li>Reversible DWT 5-3
963 </ul>
964 @param parameters Compression parameters
965 */
966 OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
967
968 /**
969  * Sets the MCT matrix to use.
970  * 
971  * @param       parameters              the parameters to change.
972  * @param       pEncodingMatrix the encoding matrix.
973  * @param       p_dc_shift              the dc shift coefficients to use.
974  * @param       pNbComp                 the number of components of the image.
975  *
976  * @return      true if the parameters could be set.
977  */
978 OPJ_API bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp);
979
980 /**
981  * Restricts the decoding to the given image area.
982  * 
983  * @param       parameters              the parameters to update.
984  * @param       p_start_x               the starting x position of the area to decode.
985  * @param       p_start_y               the starting y position of the area to decode.
986  * @param       p_end_x                 the x end position of the area to decode. 
987  * @param       p_end_x                 the y end position of the area to decode.
988  */
989 OPJ_API bool OPJ_CALLCONV opj_restrict_decoding (opj_dparameters_t *parameters,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y);
990
991 #ifdef USE_OPJ_DEPRECATED
992 #ifdef _MSC_VER
993 #pragma message ("warning, opj_setup_encoder is deprecated")
994 #else
995 #warning "warning, opj_setup_encoder is deprecated"
996 #endif
997 /**
998 Setup the encoder parameters using the current image and using user parameters. 
999 @param cinfo Compressor handle
1000 @param parameters Compression parameters
1001 @param image Input filled image
1002 */
1003 OPJ_API bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image);
1004 #endif
1005
1006 /**
1007  * Decodes an image header.
1008  * 
1009  * @param       p_codec                 codec to use to decode the image.
1010  * @param       p_image                 pointer to a previously created image.
1011  * @param       p_tile_x0               pointer to a value that will hold the reference point x0 of the tiling grid.
1012  * @param       p_tile_y0               pointer to a value that will hold the reference point y0 of the tiling grid.
1013  * @param       p_tile_width    pointer to a value that will hold the size in x of a tile in the grid.
1014  * @param       p_tile_height   pointer to a value that will hold the size in y of a tile in the grid.
1015  * @param       p_nb_tiles_x    pointer to a value that will hold the number of tiles in the x direction.
1016  * @param       p_nb_tiles_y    pointer to a value that will hold the number of tiles in the y direction.
1017  */
1018 OPJ_API bool OPJ_CALLCONV opj_read_header (
1019                                                                                 opj_codec_t *p_codec,
1020                                                                                 opj_image_t ** p_image,
1021                                                                                 OPJ_INT32 * p_tile_x0,
1022                                                                                 OPJ_INT32 * p_tile_y0,
1023                                                                                 OPJ_UINT32 * p_tile_width,
1024                                                                                 OPJ_UINT32 * p_tile_height,
1025                                                                                 OPJ_UINT32 * p_nb_tiles_x,
1026                                                                                 OPJ_UINT32 * p_nb_tiles_y,
1027                                                                                 opj_stream_t *p_cio);
1028
1029
1030 OPJ_API bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio);
1031 /**
1032 Encode an image into a JPEG-2000 codestream
1033 @param cinfo compressor handle
1034 @param cio Output buffer stream
1035 @param image Image to encode
1036 @param index Depreacted -> Set to NULL. To extract index, used opj_encode_wci()
1037 @return Returns true if successful, returns false otherwise
1038 */
1039 OPJ_API bool OPJ_CALLCONV opj_encode(opj_codec_t *cinfo, opj_stream_t * cio);
1040
1041 OPJ_API bool OPJ_CALLCONV opj_start_compress (opj_codec_t *p_codec,opj_image_t * p_image,opj_stream_t *p_cio);
1042
1043 OPJ_API bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,opj_stream_t *p_cio);
1044 /**
1045 Encode an image into a JPEG-2000 codestream and extract the codestream information
1046 @param cinfo compressor handle
1047 @param cio Output buffer stream
1048 @param image Image to encode
1049 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
1050 @return Returns true if successful, returns false otherwise
1051 */
1052 //OPJ_API bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_stream_t cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
1053 /**
1054 Destroy Codestream information after compression or decompression
1055 @param cstr_info Codestream information structure
1056 */
1057 OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info);
1058
1059 //==============================================================================
1060 //==============================================================================
1061 /** profiling part */
1062 #ifdef _PROFILE
1063 void _ProfInit(void);
1064 void _ProfPrint(void);
1065 #define PROFINIT() _ProfInit();
1066 #define PROFSAVE(file) _ProfSave(file);
1067 #define PROFPRINT() _ProfPrint();
1068 #else
1069 #define PROFINIT()
1070 #define PROFSAVE(file)
1071 #define PROFPRINT() 
1072 #endif // !_PROFILE
1073
1074 #ifdef __cplusplus
1075 }
1076 #endif
1077
1078 #endif /* OPENJPEG_H */
1079
1080