2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 ==========================================================
37 ==========================================================
40 #if defined(OPJ_STATIC) || !defined(_WIN32)
41 /* http://gcc.gnu.org/wiki/Visibility */
43 #define OPJ_API __attribute__ ((visibility ("default")))
44 #define OPJ_LOCAL __attribute__ ((visibility ("hidden")))
51 #define OPJ_CALLCONV __stdcall
54 The following ifdef block is the standard way of creating macros which make exporting
55 from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS
56 symbol defined on the command line. this symbol should not be defined on any project
57 that uses this DLL. This way any other project whose source files include this file see
58 OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols
59 defined with this macro as being exported.
61 #if defined(OPJ_EXPORTS) || defined(DLL_EXPORT)
62 #define OPJ_API __declspec(dllexport)
64 #define OPJ_API __declspec(dllimport)
65 #endif /* OPJ_EXPORTS */
66 #endif /* !OPJ_STATIC || !WIN32 */
69 #if defined(HAVE_STDBOOL_H)
71 The C language implementation does correctly provide the standard header
77 The C language implementation does not provide the standard header file
78 "stdbool.h" as required by ISO/IEC 9899:1999. Try to compensate for this
91 #endif /* __cplusplus */
94 ==========================================================
95 Useful constant definitions
96 ==========================================================
99 #define MAX_SLICES 300 /**< Maximum allowed size for slices */
100 #endif /* MAX_PATH */
103 #define MAX_PATH 260 /**< Maximum allowed size for filenames */
104 #endif /* MAX_PATH */
106 #define J3D_MAXRLVLS 32 /**< Number of maximum resolution level authorized */
107 #define J3D_MAXBANDS (7*J3D_MAXRLVLS + 1) /**< Number of maximum sub-band linked to number of resolution level */
111 ==========================================================
113 ==========================================================
123 /* ----------------------------------------------------------------------- */
125 /** Progression order */
126 typedef enum PROG_ORDER {
129 /**< layer-resolution-component-precinct order */
131 /**< resolution-layer-component-precinct order */
133 /**< resolution-precinct-component-layer order */
135 /**< precinct-component-resolution-layer order */
137 /**< component-precinct-resolution-layer order */
142 Supported volume color spaces
144 typedef enum COLOR_SPACE {
158 typedef enum CODEC_FORMAT {
161 /**< JPEG-2000 codestream : read/write */
163 /**< JPEG-2000 Part 10 file format : read/write */
168 Supported entropy coding algorithms
170 typedef enum ENTROPY_CODING {
173 /**< 2D EBCOT encoding */
175 /**< 3D EBCOT encoding */
177 /**< Golomb-Rice coding with 2D context */
179 /**< Golomb-Rice coding with 3D context */
181 } OPJ_ENTROPY_CODING;
186 typedef enum TRANSFORM {
189 /**< 2D DWT, no transform in axial dim */
198 ==========================================================
199 event manager typedef definitions
200 ==========================================================
204 Callback function prototype for events
205 @param msg Event message
208 typedef void (*opj_msg_callback) (const char *msg, void *client_data);
211 Message handler object
216 <li>Debugging messages
219 typedef struct opj_event_mgr {
220 /** Error message callback if available, NULL otherwise */
221 opj_msg_callback error_handler;
222 /** Warning message callback if available, NULL otherwise */
223 opj_msg_callback warning_handler;
224 /** Debug message callback if available, NULL otherwise */
225 opj_msg_callback info_handler;
230 ==========================================================
231 codec typedef definitions
232 ==========================================================
236 Progression order changes
238 typedef struct opj_poc {
240 int layno1, resno1, compno1;
248 Compression parameters
250 typedef struct opj_cparameters {
251 /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */
267 /** allocation by rate/distortion */
269 /** allocation by fixed layer */
271 /** add fixed_quality */
272 int cp_fixed_quality;
275 /** number of layers */
277 /** rates for successive layers */
278 float tcp_rates[100];
279 /** psnr's for successive layers */
280 float tcp_distoratio[100];
281 /** comment for coding */
283 /** csty : coding style */
285 /** DC offset (DCO) */
287 /** progression order (default LRCP) */
288 OPJ_PROG_ORDER prog_order;
289 /** progression order changes */
290 opj_poc_t POC[J3D_MAXRLVLS-1];
291 /** number of progression order changes (POC), default to 0 */
294 /** number of resolutions */
295 int numresolution[3];
296 /** initial code block width, height and depth, default to 64 */
298 /** mode switch (1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)) */
301 /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */
303 /** WT from ATK, default to 0 (false), no of atk used */
305 /** region of interest: affected component in [0..3], -1 means no ROI */
307 /** region of interest: upshift value */
310 /* number of precinct size specifications */
312 /** initial precinct width */
313 int prct_init[3][J3D_MAXRLVLS];
315 /** transform format 0: 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
316 OPJ_TRANSFORM transform_format;
317 /** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI */
318 OPJ_ENTROPY_CODING encoding_format;
320 /**@name command line encoder parameters (not used inside the library) */
322 char infile[MAX_PATH]; /** input file name */
323 char outfile[MAX_PATH]; /** output file name */
324 char imgfile[MAX_PATH]; /** IMG file name for BIN volumes*/
325 int index_on; /** creation of an index file, default to 0 (false) */
326 char index[MAX_PATH]; /** index file name */
328 int volume_offset_x0; /** subvolume encoding: origin volume offset in x, y and z direction */
329 int volume_offset_y0;
330 int volume_offset_z0;
332 int subsampling_dx; /** subsampling value for dx */
336 int decod_format; /** input file format 0: BIN, 1: PGX */
337 int cod_format; /** output file format 0: JP3D */
342 Decompression parameters
344 typedef struct opj_dparameters {
345 /** Set the number of highest resolution levels to be discarded. if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */
347 /** Set the maximum number of quality layers to decode. if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
351 /**@name command line encoder parameters (not used inside the library) */
353 /** input file name */
354 char infile[MAX_PATH];
355 /** output file name */
356 char outfile[MAX_PATH];
357 /** IMG file name for BIN volumes*/
358 char imgfile[MAX_PATH];
359 /** Original file name for PSNR measures*/
360 char original[MAX_PATH];
361 /** input file format 0: J2K, 1: JP3D */
363 /** input file format 0: BIN, 1: PGM */
365 /** original file format 0: BIN, 1: PGM */
370 /** Common fields between JPEG-2000 compression and decompression master structs. */
371 #define opj_common_fields \
372 opj_event_mgr_t *event_mgr; /**< pointer to the event manager */\
373 void * client_data; /**< Available for use by application */\
374 bool is_decompressor; /**< So common code can tell which is which */\
375 OPJ_CODEC_FORMAT codec_format; /**< selected codec */\
376 OPJ_ENTROPY_CODING encoding_format; /**< selected entropy coding */\
377 OPJ_TRANSFORM transform_format; /**< selected transform */\
378 void *j3d_handle /**< pointer to the J3D codec */
380 /* Routines that are to be used by both halves of the library are declared
381 * to receive a pointer to this structure. There are no actual instances of
382 * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t.
384 typedef struct opj_common_struct {
385 opj_common_fields; /* Fields common to both master struct types */
386 /* Additional fields follow in an actual opj_cinfo_t or
387 * opj_dinfo_t. All three structs must agree on these
388 * initial fields! (This would be a lot cleaner in C++.)
390 } opj_common_struct_t;
392 typedef opj_common_struct_t * opj_common_ptr;
395 Compression context info
397 typedef struct opj_cinfo {
398 /** Fields shared with opj_dinfo_t */
400 /* other specific fields go here */
404 Decompression context info
406 typedef struct opj_dinfo {
407 /** Fields shared with opj_cinfo_t */
409 /* other specific fields go here */
413 ==========================================================
414 I/O stream typedef definitions
415 ==========================================================
421 /** The stream was opened for reading. */
422 #define OPJ_STREAM_READ 0x0001
423 /** The stream was opened for writing. */
424 #define OPJ_STREAM_WRITE 0x0002
427 Byte input-output stream (CIO)
429 typedef struct opj_cio {
431 opj_common_ptr cinfo;
432 /** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */
434 /** pointer to the start of the buffer */
435 unsigned char *buffer;
436 /** buffer size in bytes */
438 /** pointer to the start of the stream */
439 unsigned char *start;
440 /** pointer to the end of the stream */
442 /** pointer to the current position */
447 ==========================================================
448 volume typedef definitions
449 ==========================================================
453 Defines a single volume component
455 typedef struct opj_volume_comp {
456 /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
458 /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
460 /** ZRsiz: vertical separation of a sample of ith component with respect to the reference grid */
466 /** data length : no of slices */
468 /** x component offset compared to the whole volume */
470 /** y component offset compared to the whole volume */
472 /** z component offset compared to the whole volume */
476 /** volume depth in bits */
478 /** DC offset (15444-2) */
480 /** signed (1) / unsigned (0) */
482 /** BE byte order (1) / LE byte order (0) */
484 /** number of decoded resolution */
485 int resno_decoded[3];
486 /** number of division by 2 of the out volume compared to the original size of volume */
488 /** volume component data */
493 Defines volume data and characteristics
495 typedef struct opj_volume {
496 /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the volume area */
498 /** YOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */
500 /** ZOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */
502 /** Xsiz: width of the reference grid */
504 /** Ysiz: height of the reference grid */
506 /** Zsiz: length of the reference grid */
508 /** number of components in the volume */
510 /** number of slices in the volume */
512 /** color space: sRGB, Greyscale or YUV */
513 OPJ_COLOR_SPACE color_space;
514 /** volume components */
515 opj_volume_comp_t *comps;
519 Component parameters structure used by the opj_volume_create function
521 typedef struct opj_volume_comptparm {
522 /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */
524 /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */
526 /** ZRsiz: axial separation of a sample of ith component with respect to the reference grid */
534 /** x component offset compared to the whole volume */
536 /** y component offset compared to the whole volume */
538 /** z component offset compared to the whole volume */
542 /** volume depth in bits */
544 /** signed (1) / unsigned (0) */
548 /** BE byte order (1) / LE byte order (0) */
550 } opj_volume_cmptparm_t;
558 ==========================================================
560 ==========================================================
563 OPJ_API const char * OPJ_CALLCONV opj_version(void);
566 ==========================================================
567 volume functions definitions
568 ==========================================================
573 @param numcmpts number of components
574 @param cmptparms components parameters
575 @param clrspc volume color space
576 @return returns a new volume structure if successful, returns NULL otherwise
578 OPJ_API opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc);
581 Deallocate any resources associated with an volume
582 @param volume volume to be destroyed
584 OPJ_API void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume);
587 ==========================================================
588 stream functions definitions
589 ==========================================================
593 Open and allocate a memory stream for read / write.
594 On reading, the user must provide a buffer containing encoded data. The buffer will be
595 wrapped by the returned CIO handle.
596 On writing, buffer parameters must be set to 0: a buffer will be allocated by the library
597 to contain encoded data.
598 @param cinfo Codec context info
599 @param buffer Reading: buffer address. Writing: NULL
600 @param length Reading: buffer length. Writing: 0
601 @return Returns a CIO handle if successful, returns NULL otherwise
603 OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length);
606 Close and free a CIO handle
607 @param cio CIO handle to free
609 OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio);
612 Get position in byte stream
613 @param cio CIO handle
614 @return Returns the position in bytes
616 OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio);
618 Set position in byte stream
619 @param cio CIO handle
620 @param pos Position, in number of bytes, from the beginning of the stream
622 OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos);
625 ==========================================================
626 event manager functions definitions
627 ==========================================================
630 OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context);
633 ==========================================================
634 codec functions definitions
635 ==========================================================
638 Creates a J3D decompression structure
639 @param format Decoder to select
640 @return Returns a handle to a decompressor if successful, returns NULL otherwise
642 OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format);
644 Destroy a decompressor handle
645 @param dinfo decompressor handle to destroy
647 OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo);
649 Set decoding parameters to default values
650 @param parameters Decompression parameters
652 OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters);
654 Setup the decoder decoding parameters using user parameters.
655 Decoding parameters are returned in j3d->cp.
656 @param dinfo decompressor handle
657 @param parameters decompression parameters
659 OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters);
661 Decode an volume from a JPEG-2000 codestream
662 @param dinfo decompressor handle
663 @param cio Input buffer stream
664 @return Returns a decoded volume if successful, returns NULL otherwise
666 OPJ_API opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio);
668 Creates a J3D/JP2 compression structure
669 @param format Coder to select
670 @return Returns a handle to a compressor if successful, returns NULL otherwise
672 OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format);
674 Destroy a compressor handle
675 @param cinfo compressor handle to destroy
677 OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo);
679 Set encoding parameters to default values, that means :
683 <li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
684 <li>Size of code-block : 64 x 64
685 <li>Number of resolutions: 6
686 <li>No SOP marker in the codestream
687 <li>No EPH marker in the codestream
688 <li>No sub-sampling in x or y direction
689 <li>No mode switch activated
690 <li>Progression order: LRCP
693 <li>No offset of the origin of the volume
694 <li>No offset of the origin of the tiles
695 <li>Reversible DWT 5-3
697 @param parameters Compression parameters
699 OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters);
701 Setup the encoder parameters using the current volume and using user parameters.
702 @param cinfo compressor handle
703 @param parameters compression parameters
704 @param volume input filled volume
706 OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume);
708 Encode an volume into a JPEG-2000 codestream
709 @param cinfo compressor handle
710 @param cio Output buffer stream
711 @param volume Volume to encode
712 @param index Name of the index file if required, NULL otherwise
713 @return Returns true if successful, returns false otherwise
715 OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index);
721 #endif /* OPENJPEG_H */