229982d931c22ce55643844adfbd3ee80d71f7b7
[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) 2010-2011, Kaori Hagihara
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
37 /* 
38 ==========================================================
39    Compiler directives
40 ==========================================================
41 */
42
43 #if defined(OPJ_STATIC) || !defined(_WIN32)
44 #define OPJ_API
45 #define OPJ_CALLCONV
46 #else
47 #define OPJ_CALLCONV __stdcall
48 /*
49 The following ifdef block is the standard way of creating macros which make exporting 
50 from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS
51 symbol defined on the command line. this symbol should not be defined on any project
52 that uses this DLL. This way any other project whose source files include this file see 
53 OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols
54 defined with this macro as being exported.
55 */
56 #if defined(OPJ_EXPORTS) || defined(DLL_EXPORT)
57 #define OPJ_API __declspec(dllexport)
58 #else
59 #define OPJ_API __declspec(dllimport)
60 #endif /* OPJ_EXPORTS */
61 #endif /* !OPJ_STATIC || !_WIN32 */
62
63 typedef int opj_bool;
64 #define OPJ_TRUE 1
65 #define OPJ_FALSE 0
66
67 typedef unsigned int    OPJ_UINT32;
68 typedef int                             OPJ_INT32;
69 typedef unsigned short  OPJ_UINT16;
70 typedef short                   OPJ_INT16;
71 typedef char                    OPJ_CHAR;
72 typedef unsigned char   OPJ_BYTE;
73 typedef unsigned int    OPJ_SIZE_T;
74 typedef double                  OPJ_FLOAT64;
75 typedef float                   OPJ_FLOAT32;
76
77 // Avoid compile-time warning because parameter is not used
78 #define OPJ_ARG_NOT_USED(x) (void)(x)
79
80 /* 
81 ==========================================================
82    Useful constant definitions
83 ==========================================================
84 */
85
86 #define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */
87
88 #define J2K_MAXRLVLS 33                                 /**< Number of maximum resolution level authorized */
89 #define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */
90
91 #define J2K_DEFAULT_NB_SEGS                             10
92 #define J2K_STREAM_CHUNK_SIZE                   0x100000 /** 1 mega by default */
93 #define J2K_DEFAULT_HEADER_SIZE                 1000
94 #define J2K_MCC_DEFAULT_NB_RECORDS              10
95 #define J2K_MCT_DEFAULT_NB_RECORDS              10
96
97 /* UniPG>> */
98 #define JPWL_MAX_NO_TILESPECS   16 /**< Maximum number of tile parts expected by JPWL: increase at your will */
99 #define JPWL_MAX_NO_PACKSPECS   16 /**< Maximum number of packet parts expected by JPWL: increase at your will */
100 #define JPWL_MAX_NO_MARKERS     512 /**< Maximum number of JPWL markers: increase at your will */
101 #define JPWL_PRIVATEINDEX_NAME "jpwl_index_privatefilename" /**< index file name used when JPWL is on */
102 #define JPWL_EXPECTED_COMPONENTS 3 /**< Expect this number of components, so you'll find better the first EPB */
103 #define JPWL_MAXIMUM_TILES 8192 /**< Expect this maximum number of tiles, to avoid some crashes */
104 #define JPWL_MAXIMUM_HAMMING 2 /**< Expect this maximum number of bit errors in marker id's */
105 #define JPWL_MAXIMUM_EPB_ROOM 65450 /**< Expect this maximum number of bytes for composition of EPBs */
106 /* <<UniPG */
107
108 /* 
109 ==========================================================
110    enum definitions
111 ==========================================================
112 */
113 /** 
114 Rsiz Capabilities
115 */
116 typedef enum RSIZ_CAPABILITIES {
117         STD_RSIZ = 0,           /** Standard JPEG2000 profile*/
118         CINEMA2K = 3,           /** Profile name for a 2K image*/
119         CINEMA4K = 4            /** Profile name for a 4K image*/
120 } OPJ_RSIZ_CAPABILITIES;
121
122 /** 
123 Digital cinema operation mode 
124 */
125 typedef enum CINEMA_MODE {
126         OFF = 0,                                        /** Not Digital Cinema*/
127         CINEMA2K_24 = 1,        /** 2K Digital Cinema at 24 fps*/
128         CINEMA2K_48 = 2,        /** 2K Digital Cinema at 48 fps*/
129         CINEMA4K_24 = 3         /** 4K Digital Cinema at 24 fps*/
130 }OPJ_CINEMA_MODE;
131
132 /** 
133 Progression order 
134 */
135 typedef enum PROG_ORDER {
136         PROG_UNKNOWN = -1,      /**< place-holder */
137         LRCP = 0,               /**< layer-resolution-component-precinct order */
138         RLCP = 1,               /**< resolution-layer-component-precinct order */
139         RPCL = 2,               /**< resolution-precinct-component-layer order */
140         PCRL = 3,               /**< precinct-component-resolution-layer order */
141         CPRL = 4                /**< component-precinct-resolution-layer order */
142 } OPJ_PROG_ORDER;
143
144 /**
145 Supported image color spaces
146 */
147 typedef enum COLOR_SPACE {
148         CLRSPC_UNKNOWN = -1,    /**< not supported by the library */
149         CLRSPC_UNSPECIFIED = 0, /**< not specified in the codestream */ 
150         CLRSPC_SRGB = 1,                /**< sRGB */
151         CLRSPC_GRAY = 2,                /**< grayscale */
152         CLRSPC_SYCC = 3                 /**< YUV */
153 } OPJ_COLOR_SPACE;
154
155 /**
156 Supported codec
157 */
158 typedef enum CODEC_FORMAT {
159         CODEC_UNKNOWN = -1,     /**< place-holder */
160         CODEC_J2K  = 0,         /**< JPEG-2000 codestream : read/write */
161         CODEC_JPT  = 1,         /**< JPT-stream (JPEG 2000, JPIP) : read only */
162         CODEC_JP2  = 2,         /**< JPEG-2000 file format : read/write */
163 } OPJ_CODEC_FORMAT;
164
165 /** 
166 Limit decoding to certain portions of the codestream. 
167 */
168 typedef enum LIMIT_DECODING {
169         NO_LIMITATION = 0,                                /**< No limitation for the decoding. The entire codestream will de decoded */
170         LIMIT_TO_MAIN_HEADER = 1,               /**< The decoding is limited to the Main Header */
171         DECODE_ALL_BUT_PACKETS = 2      /**< Decode everything except the JPEG 2000 packets */
172 } OPJ_LIMIT_DECODING;
173
174 /* 
175 ==========================================================
176    event manager typedef definitions
177 ==========================================================
178 */
179
180 /**
181 Callback function prototype for events
182 @param msg Event message
183 @param client_data 
184 */
185 typedef void (*opj_msg_callback) (const char *msg, void *client_data);
186
187 /**
188 Message handler object
189 used for 
190 <ul>
191 <li>Error messages
192 <li>Warning messages
193 <li>Debugging messages
194 </ul>
195 */
196 typedef struct opj_event_mgr {
197         void* client_data;
198         /** Error message callback if available, NULL otherwise */
199         opj_msg_callback error_handler;
200         /** Warning message callback if available, NULL otherwise */
201         opj_msg_callback warning_handler;
202         /** Debug message callback if available, NULL otherwise */
203         opj_msg_callback info_handler;
204 } opj_event_mgr_t;
205
206
207 /* 
208 ==========================================================
209    codec typedef definitions
210 ==========================================================
211 */
212
213 /**
214 Progression order changes
215 */
216 typedef struct opj_poc {
217         /** Resolution num start, Component num start, given by POC */
218         int resno0, compno0;
219         /** Layer num end,Resolution num end, Component num end, given by POC */
220         int layno1, resno1, compno1;
221         /** Layer num start,Precinct num start, Precinct num end */
222         int layno0, precno0, precno1;
223         /** Progression order enum*/
224         OPJ_PROG_ORDER prg1,prg;
225         /** Progression order string*/
226         char progorder[5];
227         /** Tile number */
228         int tile;
229         /** Start and end values for Tile width and height*/
230         int tx0,tx1,ty0,ty1;
231         /** Start value, initialised in pi_initialise_encode*/
232         int layS, resS, compS, prcS;
233         /** End value, initialised in pi_initialise_encode */
234         int layE, resE, compE, prcE;
235         /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/
236         int txS,txE,tyS,tyE,dx,dy;
237         /** Temporary values for Tile parts, initialised in pi_create_encode */
238         int lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t;
239 } opj_poc_t;
240
241 /**
242 Compression parameters
243 */
244 typedef struct opj_cparameters {
245         /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */
246         opj_bool tile_size_on;
247         /** XTOsiz */
248         int cp_tx0;
249         /** YTOsiz */
250         int cp_ty0;
251         /** XTsiz */
252         int cp_tdx;
253         /** YTsiz */
254         int cp_tdy;
255         /** allocation by rate/distortion */
256         int cp_disto_alloc;
257         /** allocation by fixed layer */
258         int cp_fixed_alloc;
259         /** add fixed_quality */
260         int cp_fixed_quality;
261         /** fixed layer */
262         int *cp_matrice;
263         /** comment for coding */
264         char *cp_comment;
265         /** csty : coding style */
266         int csty;
267         /** progression order (default LRCP) */
268         OPJ_PROG_ORDER prog_order;
269         /** progression order changes */
270         opj_poc_t POC[32];
271         /** number of progression order changes (POC), default to 0 */
272         int numpocs;
273         /** number of layers */
274         int tcp_numlayers;
275         /** rates of layers */
276         float tcp_rates[100];
277         /** different psnr for successive layers */
278         float tcp_distoratio[100];
279         /** number of resolutions */
280         int numresolution;
281         /** initial code block width, default to 64 */
282         int cblockw_init;
283         /** initial code block height, default to 64 */
284         int cblockh_init;
285         /** mode switch (cblk_style) */
286         int mode;
287         /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */
288         int irreversible;
289         /** region of interest: affected component in [0..3], -1 means no ROI */
290         int roi_compno;
291         /** region of interest: upshift value */
292         int roi_shift;
293         /* number of precinct size specifications */
294         int res_spec;
295         /** initial precinct width */
296         int prcw_init[J2K_MAXRLVLS];
297         /** initial precinct height */
298         int prch_init[J2K_MAXRLVLS];
299
300         /**@name command line encoder parameters (not used inside the library) */
301         /*@{*/
302         /** input file name */
303         char infile[OPJ_PATH_LEN];
304         /** output file name */
305         char outfile[OPJ_PATH_LEN];
306         /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
307         int index_on;
308         /** DEPRECATED. Index generation is now handeld with the opj_encode_with_info() function. Set to NULL */
309         char index[OPJ_PATH_LEN];
310         /** subimage encoding: origin image offset in x direction */
311         int image_offset_x0;
312         /** subimage encoding: origin image offset in y direction */
313         int image_offset_y0;
314         /** subsampling value for dx */
315         int subsampling_dx;
316         /** subsampling value for dy */
317         int subsampling_dy;
318         /** input file format 0: PGX, 1: PxM, 2: BMP 3:TIF*/
319         int decod_format;
320         /** output file format 0: J2K, 1: JP2, 2: JPT */
321         int cod_format;
322         /*@}*/
323
324 /* UniPG>> */
325         /**@name JPWL encoding parameters */
326         /*@{*/
327         /** enables writing of EPC in MH, thus activating JPWL */
328         opj_bool jpwl_epc_on;
329         /** error protection method for MH (0,1,16,32,37-128) */
330         int jpwl_hprot_MH;
331         /** tile number of header protection specification (>=0) */
332         int jpwl_hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
333         /** error protection methods for TPHs (0,1,16,32,37-128) */
334         int jpwl_hprot_TPH[JPWL_MAX_NO_TILESPECS];
335         /** tile number of packet protection specification (>=0) */
336         int jpwl_pprot_tileno[JPWL_MAX_NO_PACKSPECS];
337         /** packet number of packet protection specification (>=0) */
338         int jpwl_pprot_packno[JPWL_MAX_NO_PACKSPECS];
339         /** error protection methods for packets (0,1,16,32,37-128) */
340         int jpwl_pprot[JPWL_MAX_NO_PACKSPECS];
341         /** enables writing of ESD, (0=no/1/2 bytes) */
342         int jpwl_sens_size;
343         /** sensitivity addressing size (0=auto/2/4 bytes) */
344         int jpwl_sens_addr;
345         /** sensitivity range (0-3) */
346         int jpwl_sens_range;
347         /** sensitivity method for MH (-1=no,0-7) */
348         int jpwl_sens_MH;
349         /** tile number of sensitivity specification (>=0) */
350         int jpwl_sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
351         /** sensitivity methods for TPHs (-1=no,0-7) */
352         int jpwl_sens_TPH[JPWL_MAX_NO_TILESPECS];
353         /*@}*/
354 /* <<UniPG */
355
356         /** Digital Cinema compliance 0-not compliant, 1-compliant*/
357         OPJ_CINEMA_MODE cp_cinema;
358         /** Maximum rate for each component. If == 0, component size limitation is not considered */
359         int max_comp_size;
360         /** Profile name*/
361         OPJ_RSIZ_CAPABILITIES cp_rsiz;
362         /** Tile part generation*/
363         char tp_on;
364         /** Flag for Tile part generation*/
365         char tp_flag;
366         /** MCT (multiple component transform) */
367         char tcp_mct;
368         /** Enable JPIP indexing*/
369         opj_bool jpip_on;
370 } opj_cparameters_t;
371
372 /**
373 Decompression parameters
374 */
375 typedef struct opj_dparameters {
376         /** 
377         Set the number of highest resolution levels to be discarded. 
378         The image resolution is effectively divided by 2 to the power of the number of discarded levels. 
379         The reduce factor is limited by the smallest total number of decomposition levels among tiles.
380         if != 0, then original dimension divided by 2^(reduce); 
381         if == 0 or not used, image is decoded to the full resolution 
382         */
383         int cp_reduce;
384         /** 
385         Set the maximum number of quality layers to decode. 
386         If there are less quality layers than the specified number, all the quality layers are decoded.
387         if != 0, then only the first "layer" layers are decoded; 
388         if == 0 or not used, all the quality layers are decoded 
389         */
390         int cp_layer;
391
392         /**@name command line encoder parameters (not used inside the library) */
393         /*@{*/
394         /** input file name */
395         char infile[OPJ_PATH_LEN];
396         /** output file name */
397         char outfile[OPJ_PATH_LEN];
398         /** input file format 0: J2K, 1: JP2, 2: JPT */
399         int decod_format;
400         /** output file format 0: PGX, 1: PxM, 2: BMP */
401         int cod_format;
402         /*@}*/
403
404 /* UniPG>> */
405         /**@name JPWL decoding parameters */
406         /*@{*/
407         /** activates the JPWL correction capabilities */
408         opj_bool jpwl_correct;
409         /** expected number of components */
410         int jpwl_exp_comps;
411         /** maximum number of tiles */
412         int jpwl_max_tiles;
413         /*@}*/
414 /* <<UniPG */
415
416         /** 
417         Specify whether the decoding should be done on the entire codestream, or be limited to the main header
418         Limiting the decoding to the main header makes it possible to extract the characteristics of the codestream
419         if == NO_LIMITATION, the entire codestream is decoded; 
420         if == LIMIT_TO_MAIN_HEADER, only the main header is decoded; 
421         */
422         OPJ_LIMIT_DECODING cp_limit_decoding;
423
424 } opj_dparameters_t;
425
426 /** Common fields between JPEG-2000 compression and decompression master structs. */
427
428 #define opj_common_fields \
429         opj_event_mgr_t *event_mgr;     /**< pointer to the event manager */\
430         void * client_data;                     /**< Available for use by application */\
431         opj_bool is_decompressor;       /**< So common code can tell which is which */\
432         OPJ_CODEC_FORMAT codec_format;  /**< selected codec */\
433         void *j2k_handle;                       /**< pointer to the J2K codec */\
434         void *jp2_handle;                       /**< pointer to the JP2 codec */\
435         void *mj2_handle                        /**< pointer to the MJ2 codec */
436         
437 /* Routines that are to be used by both halves of the library are declared
438  * to receive a pointer to this structure.  There are no actual instances of
439  * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t.
440  */
441 typedef struct opj_common_struct {
442   opj_common_fields;            /* Fields common to both master struct types */
443   /* Additional fields follow in an actual opj_cinfo_t or
444    * opj_dinfo_t.  All three structs must agree on these
445    * initial fields!  (This would be a lot cleaner in C++.)
446    */
447 } opj_common_struct_t;
448
449 typedef opj_common_struct_t * opj_common_ptr;
450
451 /**
452 Compression context info
453 */
454 typedef struct opj_cinfo {
455         /** Fields shared with opj_dinfo_t */
456         opj_common_fields;      
457         /* other specific fields go here */
458 } opj_cinfo_t;
459
460 /**
461 Decompression context info
462 */
463 typedef struct opj_dinfo {
464         /** Fields shared with opj_cinfo_t */
465         opj_common_fields;      
466         /* other specific fields go here */
467 } opj_dinfo_t;
468
469 /**
470  * J2k codec.
471  */
472 typedef void * opj_codec_t;
473
474 /* 
475 ==========================================================
476    I/O stream typedef definitions
477 ==========================================================
478 */
479
480 /*
481  * Stream open flags.
482  */
483 /** The stream was opened for reading. */
484 #define OPJ_STREAM_READ 0x0001
485 /** The stream was opened for writing. */
486 #define OPJ_STREAM_WRITE 0x0002
487
488 /**
489 Byte input-output stream (CIO)
490 */
491 typedef struct opj_cio {
492         /** codec context */
493         opj_common_ptr cinfo;
494
495         /** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */
496         int openmode;
497         /** pointer to the start of the buffer */
498         unsigned char *buffer;
499         /** buffer size in bytes */
500         int length;
501
502         /** pointer to the start of the stream */
503         unsigned char *start;
504         /** pointer to the end of the stream */
505         unsigned char *end;
506         /** pointer to the current position */
507         unsigned char *bp;
508 } opj_cio_t;
509
510 typedef OPJ_UINT32 (* opj_stream_read_fn) (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) ;
511 typedef OPJ_UINT32 (* opj_stream_write_fn) (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) ;
512 typedef OPJ_SIZE_T (* opj_stream_skip_fn) (OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
513 typedef opj_bool (* opj_stream_seek_fn) (OPJ_SIZE_T p_nb_bytes, void * p_user_data) ;
514
515
516 typedef void * opj_stream_t;
517
518 /* 
519 ==========================================================
520    image typedef definitions
521 ==========================================================
522 */
523
524 /**
525 Defines a single image component
526 */
527 typedef struct opj_image_comp {
528         /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
529         int dx;
530         /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
531         int dy;
532         /** data width */
533         int w;
534         /** data height */
535         int h;
536         /** x component offset compared to the whole image */
537         int x0;
538         /** y component offset compared to the whole image */
539         int y0;
540         /** precision */
541         int prec;
542         /** image depth in bits */
543         int bpp;
544         /** signed (1) / unsigned (0) */
545         int sgnd;
546         /** number of decoded resolution */
547         int resno_decoded;
548         /** number of division by 2 of the out image compared to the original size of image */
549         int factor;
550         /** image component data */
551         int *data;
552 } opj_image_comp_t;
553
554 /** 
555 Defines image data and characteristics
556 */
557 typedef struct opj_image {
558         /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area */
559         OPJ_UINT32 x0;
560         /** YOsiz: vertical offset from the origin of the reference grid to the top side of the image area */
561         OPJ_UINT32 y0;
562         /** Xsiz: width of the reference grid */
563         OPJ_UINT32 x1;
564         /** Ysiz: height of the reference grid */
565         OPJ_UINT32 y1;
566         /** number of components in the image */
567         OPJ_UINT16 numcomps;
568         /** color space: sRGB, Greyscale or YUV */
569         OPJ_COLOR_SPACE color_space;
570         /** image components */
571         opj_image_comp_t *comps;
572         /** 'restricted' ICC profile */
573         unsigned char *icc_profile_buf;
574         /** size of ICC profile */
575         int icc_profile_len;
576 } opj_image_t;
577
578
579 /**
580 Component parameters structure used by the opj_image_create function
581 */
582 typedef struct opj_image_comptparm {
583         /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
584         int dx;
585         /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
586         int dy;
587         /** data width */
588         int w;
589         /** data height */
590         int h;
591         /** x component offset compared to the whole image */
592         int x0;
593         /** y component offset compared to the whole image */
594         int y0;
595         /** precision */
596         int prec;
597         /** image depth in bits */
598         int bpp;
599         /** signed (1) / unsigned (0) */
600         int sgnd;
601 } opj_image_cmptparm_t;
602
603
604
605 /**
606 Defines a single image component characteristics (uses in new API)
607 */
608 typedef struct opj_image_comp_header {
609         /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
610         int dx;
611         /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
612         int dy;
613         /** data width */
614         int w;
615         /** data height */
616         int h;
617         /** x component offset compared to the whole image */
618         int x0;
619         /** y component offset compared to the whole image */
620         int y0;
621         /** precision */
622         int prec;
623         /** image depth in bits */
624         int bpp;
625         /** signed (1) / unsigned (0) */
626         int sgnd;
627         /** number of decoded resolution */
628         int resno_decoded;
629         /** number of division by 2 of the out image compared to the original size of image */
630         int factor;
631 } opj_image_comp_header_t;
632
633 /**
634 Defines image characteristics (uses in new API)
635 */
636 typedef struct opj_image_header {
637         /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area */
638         OPJ_UINT32 x0;
639         /** YOsiz: vertical offset from the origin of the reference grid to the top side of the image area */
640         OPJ_UINT32 y0;
641         /** Xsiz: width of the reference grid */
642         OPJ_UINT32 x1;
643         /** Ysiz: height of the reference grid */
644         OPJ_UINT32 y1;
645         /** number of components in the image */
646         OPJ_UINT16 numcomps;
647         /** color space: sRGB, Greyscale or YUV */
648         OPJ_COLOR_SPACE color_space;
649         /** image components */
650         opj_image_comp_header_t *comps;
651
652         /** XTOsiz */
653         OPJ_UINT32 tile_x0;
654         /** YTOsiz */
655         OPJ_UINT32 tile_y0;
656         /** XTsiz */
657         OPJ_UINT32 tile_width;
658         /** YTsiz */
659         OPJ_UINT32 tile_height;
660         /** number of tiles in width */
661         OPJ_UINT32 nb_tiles_x;
662         /** number of tiles in height */
663         OPJ_UINT32 nb_tiles_y;
664
665         /** 'restricted' ICC profile */
666         unsigned char *icc_profile_buf;
667         /** size of ICC profile */
668         int icc_profile_len;
669 } opj_image_header_t;
670
671
672 /* 
673 ==========================================================
674    Information on the JPEG 2000 codestream
675 ==========================================================
676 */
677
678 /**
679 Index structure : Information concerning a packet inside tile
680 */
681 typedef struct opj_packet_info {
682         /** packet start position (including SOP marker if it exists) */
683         int start_pos;
684         /** end of packet header position (including EPH marker if it exists)*/
685         int end_ph_pos;
686         /** packet end position */
687         int end_pos;
688         /** packet distorsion */
689         double disto;
690 } opj_packet_info_t;
691
692
693 /* UniPG>> */
694 /**
695 Marker structure
696 */
697 typedef struct opj_marker_info_t {
698         /** marker type */
699         unsigned short int type;
700         /** position in codestream */
701         int pos;
702         /** length, marker val included */
703         int len;
704 } opj_marker_info_t;
705 /* <<UniPG */
706
707 /**
708 Index structure : Information concerning tile-parts
709 */
710 typedef struct opj_tp_info {
711         /** start position of tile part */
712         int tp_start_pos;
713         /** end position of tile part header */
714         int tp_end_header;
715         /** end position of tile part */
716         int tp_end_pos;
717         /** start packet of tile part */
718         int tp_start_pack;
719         /** number of packets of tile part */
720         int tp_numpacks;
721 } opj_tp_info_t;
722
723 /**
724 Index structure : information regarding tiles 
725 */
726 typedef struct opj_tile_info {
727         /** value of thresh for each layer by tile cfr. Marcela   */
728         double *thresh;
729         /** number of tile */
730         int tileno;
731         /** start position */
732         int start_pos;
733         /** end position of the header */
734         int end_header;
735         /** end position */
736         int end_pos;
737         /** precinct number for each resolution level (width) */
738         int pw[33];
739         /** precinct number for each resolution level (height) */
740         int ph[33];
741         /** precinct size (in power of 2), in X for each resolution level */
742         int pdx[33];
743         /** precinct size (in power of 2), in Y for each resolution level */
744         int pdy[33];
745         /** information concerning packets inside tile */
746         opj_packet_info_t *packet;
747         /** add fixed_quality */
748         int numpix;
749         /** add fixed_quality */
750         double distotile;
751         /** number of markers */
752         int marknum;
753         /** list of markers */
754         opj_marker_info_t *marker;
755         /** actual size of markers array */
756         int maxmarknum;
757         /** number of tile parts */
758         int num_tps;
759         /** information concerning tile parts */
760         opj_tp_info_t *tp;
761 } opj_tile_info_t;
762
763 /**
764 Index structure of the codestream
765 */
766 typedef struct opj_codestream_info {
767         /** maximum distortion reduction on the whole image (add for Marcela) */
768         double D_max;
769         /** packet number */
770         int packno;
771         /** writing the packet in the index with t2_encode_packets */
772         int index_write;
773         /** image width */
774         int image_w;
775         /** image height */
776         int image_h;
777         /** progression order */
778         OPJ_PROG_ORDER prog;
779         /** tile size in x */
780         int tile_x;
781         /** tile size in y */
782         int tile_y;
783         /** */
784         int tile_Ox;
785         /** */
786         int tile_Oy;
787         /** number of tiles in X */
788         int tw;
789         /** number of tiles in Y */
790         int th;
791         /** component numbers */
792         int numcomps;
793         /** number of layer */
794         int numlayers;
795         /** number of decomposition for each component */
796         int *numdecompos;
797 /* UniPG>> */
798         /** number of markers */
799         int marknum;
800         /** list of markers */
801         opj_marker_info_t *marker;
802         /** actual size of markers array */
803         int maxmarknum;
804 /* <<UniPG */
805         /** main header position */
806         int main_head_start;
807         /** main header position */
808         int main_head_end;
809         /** codestream's size */
810         int codestream_size;
811         /** information regarding tiles inside image */
812         opj_tile_info_t *tile;
813 } opj_codestream_info_t;
814
815 // NEW codestream
816
817 typedef struct opj_tile_v2_info {
818
819         /** number of tile */
820         int tileno;
821
822         /** start position */
823         int start_pos;
824         /** end position of the header */
825         int end_header;
826         /** end position */
827         int end_pos;
828
829         /** add fixed_quality */
830         int numpix;
831         /** add fixed_quality */
832         double distotile;
833
834         /** precinct number for each resolution level (width) */
835         int pw[33];
836         /** precinct number for each resolution level (height) */
837         int ph[33];
838         /** precinct size (in power of 2), in X for each resolution level */
839         int pdx[33];
840         /** precinct size (in power of 2), in Y for each resolution level */
841         int pdy[33];
842         /** information concerning packets inside tile */
843         opj_packet_info_t *packet;
844
845
846         /** number of tile parts */
847         int num_tps;
848         /** information concerning tile parts */
849         opj_tp_info_t *tp;
850
851         /** value of thresh for each layer by tile cfr. Marcela   */
852                 double *thresh;
853 } opj_tile_info_v2_t;
854
855 /**
856 Index structure of the codestream
857 */
858 typedef struct opj_codestream_v2_info {
859         /** image width */
860         int image_w;
861         /** image height */
862         int image_h;
863         /** numbers of component */
864         int numcomps;
865
866         /** progression order */
867         OPJ_PROG_ORDER prog;
868         /** number of layer */
869         int numlayers;
870
871         /** */
872         int tx0;
873         /** */
874         int ty0;
875         /** tile size in x */
876         int tdx;
877         /** tile size in y */
878         int tdy;
879         /** number of tiles in X */
880         int tw;
881         /** number of tiles in Y */
882         int th;
883
884         /** number of decomposition for each component */
885         int *numdecompos;
886
887         /** maximum distortion reduction on the whole image (add for Marcela) */
888         double D_max;
889         /** packet number */
890         int packno;
891         /** writing the packet in the index with t2_encode_packets */
892         int index_write;
893
894
895
896 /* UniPG>> */
897         /** number of markers */
898         int marknum;
899         /** list of markers */
900         opj_marker_info_t *marker;
901         /** actual size of markers array */
902         int maxmarknum;
903 /* <<UniPG */
904
905         /** main header position */
906         int main_head_start;
907         /** main header position */
908         int main_head_end;
909         /** codestream's size */
910         int codestream_size;
911
912         /** information regarding tiles inside image */
913         opj_tile_info_v2_t *tile;
914 } opj_codestream_info_v2_t;
915
916 #ifdef __cplusplus
917 extern "C" {
918 #endif
919
920
921 /* 
922 ==========================================================
923    openjpeg version
924 ==========================================================
925 */
926
927 OPJ_API const char * OPJ_CALLCONV opj_version(void);
928
929 /* 
930 ==========================================================
931    image functions definitions
932 ==========================================================
933 */
934
935 /**
936 Create an image
937 @param numcmpts number of components
938 @param cmptparms components parameters
939 @param clrspc image color space
940 @return returns a new image structure if successful, returns NULL otherwise
941 */
942 OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
943
944 /**
945 Deallocate any resources associated with an image
946 @param image image to be destroyed
947 */
948 OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image);
949
950 OPJ_API void OPJ_CALLCONV opj_image_header_destroy(opj_image_header_t *image);
951
952 /* 
953 ==========================================================
954    stream functions definitions
955 ==========================================================
956 */
957
958 /**
959 Open and allocate a memory stream for read / write. 
960 On reading, the user must provide a buffer containing encoded data. The buffer will be 
961 wrapped by the returned CIO handle. 
962 On writing, buffer parameters must be set to 0: a buffer will be allocated by the library 
963 to contain encoded data. 
964 @param cinfo Codec context info
965 @param buffer Reading: buffer address. Writing: NULL
966 @param length Reading: buffer length. Writing: 0
967 @return Returns a CIO handle if successful, returns NULL otherwise
968 */
969 OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length);
970
971 /**
972 Close and free a CIO handle
973 @param cio CIO handle to free
974 */
975 OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio);
976
977 /**
978 Get position in byte stream
979 @param cio CIO handle
980 @return Returns the position in bytes
981 */
982 OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio);
983 /**
984 Set position in byte stream
985 @param cio CIO handle
986 @param pos Position, in number of bytes, from the beginning of the stream
987 */
988 OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos);
989
990
991 /**
992  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
993  *
994  * @param       l_is_reader             if set to true then the stream will be an input stream, an output stream else.
995  *
996  * @return      a stream object.
997 */
998 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool p_is_input);
999 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_UINT32 p_size, opj_bool p_is_input);
1000
1001 /**
1002  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
1003  * close its own implementation of the stream.
1004  *
1005  * @param       p_stream        the stream to destroy.
1006  */
1007 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream);
1008
1009 /**
1010  * Sets the given function to be used as a read function.
1011  * @param               p_stream        the stream to modify
1012  * @param               p_function      the function to use a read function.
1013 */
1014 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function);
1015
1016 /**
1017  * Sets the given function to be used as a write function.
1018  * @param               p_stream        the stream to modify
1019  * @param               p_function      the function to use a write function.
1020 */
1021 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function);
1022
1023 /**
1024  * Sets the given function to be used as a skip function.
1025  * @param               p_stream        the stream to modify
1026  * @param               p_function      the function to use a skip function.
1027 */
1028 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function);
1029
1030 /**
1031  * Sets the given function to be used as a seek function, the stream is then seekable.
1032  * @param               p_stream        the stream to modify
1033  * @param               p_function      the function to use a skip function.
1034 */
1035 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function);
1036
1037
1038 /**
1039  * Sets the given data to be used as a user data for the stream.
1040  * @param               p_stream        the stream to modify
1041  * @param               p_data          the data to set.
1042 */
1043 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data);
1044
1045
1046 /**
1047  * Helper function.
1048  * Sets the stream to be a file stream. The FILE must have been open previously.
1049  * @param               p_stream        the stream to modify
1050  * @param               p_file          handler to an already open file.
1051 */
1052 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream);
1053 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_buffer_size, opj_bool p_is_read_stream);
1054
1055
1056
1057 /* 
1058 ==========================================================
1059    event manager functions definitions
1060 ==========================================================
1061 */
1062
1063 OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context);
1064
1065 /* 
1066 ==========================================================
1067    codec functions definitions
1068 ==========================================================
1069 */
1070 /**
1071 Creates a J2K/JPT/JP2 decompression structure
1072 @param format Decoder to select
1073 @return Returns a handle to a decompressor if successful, returns NULL otherwise
1074 */
1075 OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT format);
1076
1077
1078 /**
1079 Creates a J2K/JPT/JP2 decompression structure
1080 @param format Decoder to select
1081 @return Returns a handle to a decompressor if successful, returns NULL otherwise
1082 */
1083 OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
1084 /**
1085 Destroy a decompressor handle
1086 @param dinfo decompressor handle to destroy
1087 */
1088 OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo);
1089 /**
1090 Set decoding parameters to default values
1091 @param parameters Decompression parameters
1092 */
1093 OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
1094 /**
1095 Setup the decoder decoding parameters using user parameters.
1096 Decoding parameters are returned in j2k->cp. 
1097 @param dinfo decompressor handle
1098 @param parameters decompression parameters
1099 */
1100 OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters);
1101
1102 OPJ_API opj_bool OPJ_CALLCONV opj_setup_decoder_v2(     opj_codec_t *p_info,
1103                                                                                                         opj_dparameters_t *parameters,
1104                                                                                                         opj_event_mgr_t* event_mgr);
1105
1106 /**
1107 Decode an image from a JPEG-2000 codestream 
1108 @param dinfo decompressor handle
1109 @param cio Input buffer stream
1110 @return Returns a decoded image if successful, returns NULL otherwise
1111 */
1112 OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio);
1113
1114 /**
1115 Decode an image from a JPEG-2000 codestream and extract the codestream information
1116 @param dinfo decompressor handle
1117 @param cio Input buffer stream
1118 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
1119 @return Returns a decoded image if successful, returns NULL otherwise
1120 */
1121 OPJ_API opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
1122 /**
1123 Creates a J2K/JP2 compression structure
1124 @param format Coder to select
1125 @return Returns a handle to a compressor if successful, returns NULL otherwise
1126 */
1127 OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
1128 /**
1129 Destroy a compressor handle
1130 @param cinfo compressor handle to destroy
1131 */
1132 OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo);
1133 /**
1134 Set encoding parameters to default values, that means : 
1135 <ul>
1136 <li>Lossless
1137 <li>1 tile
1138 <li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
1139 <li>Size of code-block : 64 x 64
1140 <li>Number of resolutions: 6
1141 <li>No SOP marker in the codestream
1142 <li>No EPH marker in the codestream
1143 <li>No sub-sampling in x or y direction
1144 <li>No mode switch activated
1145 <li>Progression order: LRCP
1146 <li>No index file
1147 <li>No ROI upshifted
1148 <li>No offset of the origin of the image
1149 <li>No offset of the origin of the tiles
1150 <li>Reversible DWT 5-3
1151 </ul>
1152 @param parameters Compression parameters
1153 */
1154 OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
1155 /**
1156 Setup the encoder parameters using the current image and using user parameters. 
1157 @param cinfo Compressor handle
1158 @param parameters Compression parameters
1159 @param image Input filled image
1160 */
1161 OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image);
1162 /**
1163 Encode an image into a JPEG-2000 codestream
1164 3@param cinfo compressor handle
1165 @param cio Output buffer stream
1166 @param image Image to encode
1167 @param index Depreacted -> Set to NULL. To extract index, used opj_encode_wci()
1168 @return Returns true if successful, returns false otherwise
1169 */
1170 OPJ_API opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index);
1171 /**
1172 Encode an image into a JPEG-2000 codestream and extract the codestream information
1173 @param cinfo compressor handle
1174 @param cio Output buffer stream
1175 @param image Image to encode
1176 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
1177 @return Returns true if successful, returns false otherwise
1178 */
1179 OPJ_API opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
1180 /**
1181 Destroy Codestream information after compression or decompression
1182 @param cstr_info Codestream information structure
1183 */
1184 OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info);
1185
1186
1187 /**
1188  * Decodes an image header.
1189  *
1190  * @param       p_codec                 codec to use to decode the image.
1191  * @param       p_image                 pointer to a previously created image.
1192  * @param       p_tile_x0               pointer to a value that will hold the reference point x0 of the tiling grid.
1193  * @param       p_tile_y0               pointer to a value that will hold the reference point y0 of the tiling grid.
1194  * @param       p_tile_width    pointer to a value that will hold the size in x of a tile in the grid.
1195  * @param       p_tile_height   pointer to a value that will hold the size in y of a tile in the grid.
1196  * @param       p_nb_tiles_x    pointer to a value that will hold the number of tiles in the x direction.
1197  * @param       p_nb_tiles_y    pointer to a value that will hold the number of tiles in the y direction.
1198  */
1199 OPJ_API opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
1200                                                                                                 opj_codec_t *p_codec,
1201                                                                                                 opj_image_header_t **p_image_header,
1202                                                                                                 opj_codestream_info_t **p_cstr_info     );
1203
1204 /**
1205 Destroy a decompressor handle
1206 @param dinfo decompressor handle to destroy
1207 */
1208 OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec);
1209
1210 #ifdef __cplusplus
1211 }
1212 #endif
1213
1214 #endif /* OPENJPEG_H */