0f35fbd334b8c91736e668e78daa58df3c2d620f
[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    Information on the JPEG 2000 codestream
606 ==========================================================
607 */
608
609 /**
610 Index structure : Information concerning a packet inside tile
611 */
612 typedef struct opj_packet_info {
613         /** packet start position (including SOP marker if it exists) */
614         int start_pos;
615         /** end of packet header position (including EPH marker if it exists)*/
616         int end_ph_pos;
617         /** packet end position */
618         int end_pos;
619         /** packet distorsion */
620         double disto;
621 } opj_packet_info_t;
622
623
624 /* UniPG>> */
625 /**
626 Marker structure
627 */
628 typedef struct opj_marker_info_t {
629         /** marker type */
630         unsigned short int type;
631         /** position in codestream */
632         int pos;
633         /** length, marker val included */
634         int len;
635 } opj_marker_info_t;
636 /* <<UniPG */
637
638 /**
639 Index structure : Information concerning tile-parts
640 */
641 typedef struct opj_tp_info {
642         /** start position of tile part */
643         int tp_start_pos;
644         /** end position of tile part header */
645         int tp_end_header;
646         /** end position of tile part */
647         int tp_end_pos;
648         /** start packet of tile part */
649         int tp_start_pack;
650         /** number of packets of tile part */
651         int tp_numpacks;
652 } opj_tp_info_t;
653
654 /**
655 Index structure : information regarding tiles 
656 */
657 typedef struct opj_tile_info {
658         /** value of thresh for each layer by tile cfr. Marcela   */
659         double *thresh;
660         /** number of tile */
661         int tileno;
662         /** start position */
663         int start_pos;
664         /** end position of the header */
665         int end_header;
666         /** end position */
667         int end_pos;
668         /** precinct number for each resolution level (width) */
669         int pw[33];
670         /** precinct number for each resolution level (height) */
671         int ph[33];
672         /** precinct size (in power of 2), in X for each resolution level */
673         int pdx[33];
674         /** precinct size (in power of 2), in Y for each resolution level */
675         int pdy[33];
676         /** information concerning packets inside tile */
677         opj_packet_info_t *packet;
678         /** add fixed_quality */
679         int numpix;
680         /** add fixed_quality */
681         double distotile;
682         /** number of markers */
683         int marknum;
684         /** list of markers */
685         opj_marker_info_t *marker;
686         /** actual size of markers array */
687         int maxmarknum;
688         /** number of tile parts */
689         int num_tps;
690         /** information concerning tile parts */
691         opj_tp_info_t *tp;
692 } opj_tile_info_t;
693
694 /**
695 Index structure of the codestream
696 */
697 typedef struct opj_codestream_info {
698         /** maximum distortion reduction on the whole image (add for Marcela) */
699         double D_max;
700         /** packet number */
701         int packno;
702         /** writing the packet in the index with t2_encode_packets */
703         int index_write;
704         /** image width */
705         int image_w;
706         /** image height */
707         int image_h;
708         /** progression order */
709         OPJ_PROG_ORDER prog;
710         /** tile size in x */
711         int tile_x;
712         /** tile size in y */
713         int tile_y;
714         /** */
715         int tile_Ox;
716         /** */
717         int tile_Oy;
718         /** number of tiles in X */
719         int tw;
720         /** number of tiles in Y */
721         int th;
722         /** component numbers */
723         int numcomps;
724         /** number of layer */
725         int numlayers;
726         /** number of decomposition for each component */
727         int *numdecompos;
728 /* UniPG>> */
729         /** number of markers */
730         int marknum;
731         /** list of markers */
732         opj_marker_info_t *marker;
733         /** actual size of markers array */
734         int maxmarknum;
735 /* <<UniPG */
736         /** main header position */
737         int main_head_start;
738         /** main header position */
739         int main_head_end;
740         /** codestream's size */
741         int codestream_size;
742         /** information regarding tiles inside image */
743         opj_tile_info_t *tile;
744 } opj_codestream_info_t;
745
746 #ifdef __cplusplus
747 extern "C" {
748 #endif
749
750
751 /* 
752 ==========================================================
753    openjpeg version
754 ==========================================================
755 */
756
757 OPJ_API const char * OPJ_CALLCONV opj_version(void);
758
759 /* 
760 ==========================================================
761    image functions definitions
762 ==========================================================
763 */
764
765 /**
766 Create an image
767 @param numcmpts number of components
768 @param cmptparms components parameters
769 @param clrspc image color space
770 @return returns a new image structure if successful, returns NULL otherwise
771 */
772 OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
773
774 /**
775 Deallocate any resources associated with an image
776 @param image image to be destroyed
777 */
778 OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image);
779
780 /* 
781 ==========================================================
782    stream functions definitions
783 ==========================================================
784 */
785
786 /**
787 Open and allocate a memory stream for read / write. 
788 On reading, the user must provide a buffer containing encoded data. The buffer will be 
789 wrapped by the returned CIO handle. 
790 On writing, buffer parameters must be set to 0: a buffer will be allocated by the library 
791 to contain encoded data. 
792 @param cinfo Codec context info
793 @param buffer Reading: buffer address. Writing: NULL
794 @param length Reading: buffer length. Writing: 0
795 @return Returns a CIO handle if successful, returns NULL otherwise
796 */
797 OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length);
798
799 /**
800 Close and free a CIO handle
801 @param cio CIO handle to free
802 */
803 OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio);
804
805 /**
806 Get position in byte stream
807 @param cio CIO handle
808 @return Returns the position in bytes
809 */
810 OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio);
811 /**
812 Set position in byte stream
813 @param cio CIO handle
814 @param pos Position, in number of bytes, from the beginning of the stream
815 */
816 OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos);
817
818
819 /**
820  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
821  *
822  * @param       l_is_reader             if set to true then the stream will be an input stream, an output stream else.
823  *
824  * @return      a stream object.
825 */
826 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool p_is_input);
827 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_UINT32 p_size, opj_bool p_is_input);
828
829 /**
830  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
831  * close its own implementation of the stream.
832  *
833  * @param       p_stream        the stream to destroy.
834  */
835 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream);
836
837 /**
838  * Sets the given function to be used as a read function.
839  * @param               p_stream        the stream to modify
840  * @param               p_function      the function to use a read function.
841 */
842 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function);
843
844 /**
845  * Sets the given function to be used as a write function.
846  * @param               p_stream        the stream to modify
847  * @param               p_function      the function to use a write function.
848 */
849 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function);
850
851 /**
852  * Sets the given function to be used as a skip function.
853  * @param               p_stream        the stream to modify
854  * @param               p_function      the function to use a skip function.
855 */
856 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function);
857
858 /**
859  * Sets the given function to be used as a seek function, the stream is then seekable.
860  * @param               p_stream        the stream to modify
861  * @param               p_function      the function to use a skip function.
862 */
863 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function);
864
865
866 /**
867  * Sets the given data to be used as a user data for the stream.
868  * @param               p_stream        the stream to modify
869  * @param               p_data          the data to set.
870 */
871 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data (opj_stream_t* p_stream, void * p_data);
872
873
874 /**
875  * Helper function.
876  * Sets the stream to be a file stream. The FILE must have been open previously.
877  * @param               p_stream        the stream to modify
878  * @param               p_file          handler to an already open file.
879 */
880 OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream);
881 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);
882
883
884
885 /* 
886 ==========================================================
887    event manager functions definitions
888 ==========================================================
889 */
890
891 OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context);
892
893 /* 
894 ==========================================================
895    codec functions definitions
896 ==========================================================
897 */
898 /**
899 Creates a J2K/JPT/JP2 decompression structure
900 @param format Decoder to select
901 @return Returns a handle to a decompressor if successful, returns NULL otherwise
902 */
903 OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT format);
904
905
906 /**
907 Creates a J2K/JPT/JP2 decompression structure
908 @param format Decoder to select
909 @return Returns a handle to a decompressor if successful, returns NULL otherwise
910 */
911 OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
912 /**
913 Destroy a decompressor handle
914 @param dinfo decompressor handle to destroy
915 */
916 OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo);
917 /**
918 Set decoding parameters to default values
919 @param parameters Decompression parameters
920 */
921 OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
922 /**
923 Setup the decoder decoding parameters using user parameters.
924 Decoding parameters are returned in j2k->cp. 
925 @param dinfo decompressor handle
926 @param parameters decompression parameters
927 */
928 OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters);
929
930 OPJ_API opj_bool OPJ_CALLCONV opj_setup_decoder_v2(     opj_codec_t *p_info,
931                                                                                                         opj_dparameters_t *parameters,
932                                                                                                         opj_event_mgr_t* event_mgr);
933
934 /**
935 Decode an image from a JPEG-2000 codestream 
936 @param dinfo decompressor handle
937 @param cio Input buffer stream
938 @return Returns a decoded image if successful, returns NULL otherwise
939 */
940 OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio);
941
942 /**
943 Decode an image from a JPEG-2000 codestream and extract the codestream information
944 @param dinfo decompressor handle
945 @param cio Input buffer stream
946 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
947 @return Returns a decoded image if successful, returns NULL otherwise
948 */
949 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);
950 /**
951 Creates a J2K/JP2 compression structure
952 @param format Coder to select
953 @return Returns a handle to a compressor if successful, returns NULL otherwise
954 */
955 OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
956 /**
957 Destroy a compressor handle
958 @param cinfo compressor handle to destroy
959 */
960 OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo);
961 /**
962 Set encoding parameters to default values, that means : 
963 <ul>
964 <li>Lossless
965 <li>1 tile
966 <li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
967 <li>Size of code-block : 64 x 64
968 <li>Number of resolutions: 6
969 <li>No SOP marker in the codestream
970 <li>No EPH marker in the codestream
971 <li>No sub-sampling in x or y direction
972 <li>No mode switch activated
973 <li>Progression order: LRCP
974 <li>No index file
975 <li>No ROI upshifted
976 <li>No offset of the origin of the image
977 <li>No offset of the origin of the tiles
978 <li>Reversible DWT 5-3
979 </ul>
980 @param parameters Compression parameters
981 */
982 OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
983 /**
984 Setup the encoder parameters using the current image and using user parameters. 
985 @param cinfo Compressor handle
986 @param parameters Compression parameters
987 @param image Input filled image
988 */
989 OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image);
990 /**
991 Encode an image into a JPEG-2000 codestream
992 3@param cinfo compressor handle
993 @param cio Output buffer stream
994 @param image Image to encode
995 @param index Depreacted -> Set to NULL. To extract index, used opj_encode_wci()
996 @return Returns true if successful, returns false otherwise
997 */
998 OPJ_API opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index);
999 /**
1000 Encode an image into a JPEG-2000 codestream and extract the codestream information
1001 @param cinfo compressor handle
1002 @param cio Output buffer stream
1003 @param image Image to encode
1004 @param cstr_info Codestream information structure if needed afterwards, NULL otherwise
1005 @return Returns true if successful, returns false otherwise
1006 */
1007 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);
1008 /**
1009 Destroy Codestream information after compression or decompression
1010 @param cstr_info Codestream information structure
1011 */
1012 OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info);
1013
1014
1015 /**
1016  * Decodes an image header.
1017  *
1018  * @param       p_codec                 codec to use to decode the image.
1019  * @param       p_image                 pointer to a previously created image.
1020  * @param       p_tile_x0               pointer to a value that will hold the reference point x0 of the tiling grid.
1021  * @param       p_tile_y0               pointer to a value that will hold the reference point y0 of the tiling grid.
1022  * @param       p_tile_width    pointer to a value that will hold the size in x of a tile in the grid.
1023  * @param       p_tile_height   pointer to a value that will hold the size in y of a tile in the grid.
1024  * @param       p_nb_tiles_x    pointer to a value that will hold the number of tiles in the x direction.
1025  * @param       p_nb_tiles_y    pointer to a value that will hold the number of tiles in the y direction.
1026  */
1027 OPJ_API opj_bool OPJ_CALLCONV opj_read_header (
1028                                                                                 opj_codec_t *p_codec,
1029                                                                                 opj_image_t ** p_image,
1030                                                                                 OPJ_INT32 * p_tile_x0,
1031                                                                                 OPJ_INT32 * p_tile_y0,
1032                                                                                 OPJ_UINT32 * p_tile_width,
1033                                                                                 OPJ_UINT32 * p_tile_height,
1034                                                                                 OPJ_UINT32 * p_nb_tiles_x,
1035                                                                                 OPJ_UINT32 * p_nb_tiles_y,
1036                                                                                 opj_stream_t *p_cio);
1037
1038 /**
1039 Destroy a decompressor handle
1040 @param dinfo decompressor handle to destroy
1041 */
1042 OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec);
1043
1044 #ifdef __cplusplus
1045 }
1046 #endif
1047
1048 #endif /* OPENJPEG_H */