[trunk] updated copyright and added copyright notice required by ISO, in each file...
[openjpeg.git] / src / lib / openjp3d / jp3d.h
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2001-2003, David Janssens
8  * Copyright (c) 2002-2003, Yannick Verschueren
9  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10  * Copyright (c) 2005, Herve Drolon, FreeImage Team
11  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12  * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 #ifndef __J3D_H
37 #define __J3D_H
38 /**
39 @file jp3d.h
40 @brief The JPEG-2000 Codestream Reader/Writer (J3D)
41
42 The functions in J3D.C have for goal to read/write the several parts of the codestream: markers and data.
43 */
44
45 /** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
46 /*@{*/
47
48 #define J3D_CP_CSTY_PRT 0x01
49 #define J3D_CP_CSTY_SOP 0x02
50 #define J3D_CP_CSTY_EPH 0x04
51 #define J3D_CCP_CSTY_PRT 0x01
52 /** Table A-8 */
53 #define J3D_CCP_CBLKSTY_LAZY 0x01        /* Selective arithmetic coding bypass */
54 #define J3D_CCP_CBLKSTY_RESET 0x02   /* Reset context probabilities on coding pass boundaries */
55 #define J3D_CCP_CBLKSTY_TERMALL 0x04 /* Termination on each coding pass */
56 #define J3D_CCP_CBLKSTY_VSC 0x08         /* Vertically causal context, add also hook for switching off and on 3D context models */      
57 #define J3D_CCP_CBLKSTY_PTERM 0x10       /* Predictable termination */
58 #define J3D_CCP_CBLKSTY_SEGSYM 0x20      /* Segmentation symbols are used */
59 #define J3D_CCP_CBLKSTY_3DCTXT 0x40  /* 3D context models (3D-EBCOT) vs 2D context models */
60
61 #define J3D_CCP_QNTSTY_NOQNT 0  /* Quantization style : no quantization */
62 #define J3D_CCP_QNTSTY_SIQNT 1  /* Quantization style : scalar derived (values signalled only in LLL subband) */
63 #define J3D_CCP_QNTSTY_SEQNT 2  /* Quantization style : scalar expounded (values signalled for each subband) */
64
65 /* ----------------------------------------------------------------------- */
66
67 #define J3D_MS_SOC 0xff4f       /**< SOC marker value */
68 #define J3D_MS_SOT 0xff90       /**< SOT marker value */
69 #define J3D_MS_SOD 0xff93       /**< SOD marker value */
70 #define J3D_MS_EOC 0xffd9       /**< EOC marker value */
71 #define J3D_MS_CAP 0xff50       /**< CAP marker value */
72 #define J3D_MS_SIZ 0xff51       /**< SIZ marker value */
73 #define J3D_MS_NSI 0xff54       /**< NSI marker value */
74 #define J3D_MS_COD 0xff52       /**< COD marker value */
75 #define J3D_MS_COC 0xff53       /**< COC marker value */
76 #define J3D_MS_RGN 0xff5e       /**< RGN marker value */
77 #define J3D_MS_QCD 0xff5c       /**< QCD marker value */
78 #define J3D_MS_QCC 0xff5d       /**< QCC marker value */
79 #define J3D_MS_POC 0xff5f       /**< POC marker value */
80 #define J3D_MS_TLM 0xff55       /**< TLM marker value */
81 #define J3D_MS_PLM 0xff57       /**< PLM marker value */
82 #define J3D_MS_PLT 0xff58       /**< PLT marker value */
83 #define J3D_MS_PPM 0xff60       /**< PPM marker value */
84 #define J3D_MS_PPT 0xff61       /**< PPT marker value */
85 #define J3D_MS_SOP 0xff91       /**< SOP marker value */
86 #define J3D_MS_EPH 0xff92       /**< EPH marker value */
87 #define J3D_MS_CRG 0xff63       /**< CRG marker value */
88 #define J3D_MS_COM 0xff64       /**< COM marker value */
89 /*15444-2*/
90 #define J3D_MS_DCO 0xff70       /**< DCO marker value */
91 #define J3D_MS_VMS 0xff71   /**< VMS marker value */
92 #define J3D_MS_DFS 0xff72       /**< DFS marker value */
93 #define J3D_MS_ADS 0xff73       /**< ADS marker value */
94 #define J3D_MS_ATK 0xff79       /**< ATK marker value */
95 #define J3D_MS_CBD 0xff78       /**< CBD marker value */
96 #define J3D_MS_MCT 0xff74       /**< MCT marker value */
97 #define J3D_MS_MCC 0xff75       /**< MCC marker value */
98 #define J3D_MS_MCO 0xff77       /**< MCO marker value */
99 #define J3D_MS_NLT 0xff76       /**< NLT marker value */
100 #define J3D_MS_QPD 0xff5a       /**< QPD marker value */
101 #define J3D_MS_QPC 0xff5b       /**< QPC marker value */
102
103 /* ----------------------------------------------------------------------- */
104 /* Capability RSIZ parameter, extended */
105 #define J3D_RSIZ_BASIC 0x0000
106
107 #define J3D_RSIZ_DCO   0x8001 /* Required */
108 #define J3D_RSIZ_VSQNT 0x8002
109 #define J3D_RSIZ_TCQNT 0x8004
110 #define J3D_RSIZ_VMASK 0x8008
111 #define J3D_RSIZ_SSOVL 0x8010
112 #define J3D_RSIZ_ADECS 0x8020
113 #define J3D_RSIZ_ATK   0x8040 /*Required*/
114 #define J3D_RSIZ_SSYMK 0x8080
115 #define J3D_RSIZ_MCT   0x8100 /*Not compatible with DCO*/
116 #define J3D_RSIZ_NLT   0x8200 /*Required*/
117 #define J3D_RSIZ_ASHAP 0x8400
118 #define J3D_RSIZ_PRQNT 0x8800
119
120 #define J3D_CAP_10              0x00400000
121 /* Arbitrary transformation kernel, 15444-2 */
122 #define J3D_ATK_IRR 0
123 #define J3D_ATK_REV 1
124 #define J3D_ATK_ARB 0
125 #define J3D_ATK_WS 1
126 #define J3D_ATK_CON 0
127 /* ----------------------------------------------------------------------- */
128
129 /**
130 Values that specify the status of the decoding process when decoding the main header. 
131 These values may be combined with a | operator. 
132 */
133 typedef enum J3D_STATUS {
134         /**< a SOC marker is expected */
135         J3D_STATE_MHSOC  = 0x0001, 
136         /**< a SIZ marker is expected */
137         J3D_STATE_MHSIZ  = 0x0002, 
138         /**< the decoding process is in the main header */
139         J3D_STATE_MH     = 0x0004, 
140         /**< the decoding process is in a tile part header and expects a SOT marker */
141         J3D_STATE_TPHSOT = 0x0008, 
142         /**< the decoding process is in a tile part header */
143         J3D_STATE_TPH    = 0x0010, 
144         /**< the EOC marker has just been read */
145         J3D_STATE_MT     = 0x0020, 
146         /**< the decoding process must not expect a EOC marker because the codestream is truncated */
147         J3D_STATE_NEOC   = 0x0040  
148 } J3D_STATUS;
149
150
151
152 /**
153 Arbitrary transformation kernel
154 */
155 typedef struct opj_atk {
156 /** index of wavelet kernel */
157         int index;
158 /** Numerical type of scaling factor and lifting step parameters */
159         int coeff_typ;          
160 /** Wavelet filter category */
161         int filt_cat;           
162 /** Wavelet transformation type (REV/IRR) */
163         int wt_typ;                     
164 /** Initial odd/even subsequence */
165         int minit;                      
166 /** Boundary extension method (constant CON / whole-sample symmetric WS) */
167         int exten;                      
168 /** Scaling factor. Only for wt_typ=IRR */
169         double Katk;                    
170 /** Number of lifting steps */
171         int Natk;                       
172 /** Offset for lifting step s. Only for filt_cat=ARB */
173         int Oatk[256];          
174 /** Base 2 scaling exponent for lifting step s. Only for wt_typ=REV */
175         int Eatk[256];          
176 /** Additive residue for lifting step s. Only for wt_typ=REV */
177         int Batk[256];          
178 /** Number of lifting coefficients signaled for lifting step s  */
179         int LCatk[256]; 
180 /** Lifting coefficient k for lifting step s */
181         double Aatk[256][256];  
182 } opj_atk_t;
183
184
185 /**
186 Quantization stepsize
187 */
188 typedef struct opj_stepsize {
189 /** exponent */
190         int expn;       
191 /** mantissa */
192         int mant;       
193 } opj_stepsize_t;
194
195 /**
196 Tile-component coding parameters
197 */
198 typedef struct opj_tccp {
199         /** coding style */
200         int csty;                                                       
201         /** number of resolutions of x, y and z-axis */
202         int numresolution[3];   
203         /** code-blocks width height & depth*/
204         int cblk[3];                    
205         /** code-block coding style */
206         int cblksty;                    
207         /** 0: no ATK (only 9-7 or 5-3) 1: ATK defined WT*/
208         int atk_wt[3];                          
209         /** Arbitrary transformation kernel (15444-2)*/
210         opj_atk_t *atk;                 
211         /** DWT identifier for x, y and z-axis (0:WT9-7 1:WT5-3 >1:WT-atk->index) */
212         int dwtid[3];
213         /** reversible/irreversible wavelet transfomation (0:irrev 1:reversible)*/      
214         int reversible;                 
215         /** quantisation style */
216         int qntsty;                             
217         /** stepsizes used for quantization */
218         opj_stepsize_t stepsizes[J3D_MAXBANDS]; 
219         /** number of guard bits. Table A28 de 15444-1*/
220         int numgbits;                   
221         /** Region Of Interest shift */
222         int roishift;                   
223         /** precinct width heigth & depth*/
224         int prctsiz[3][J3D_MAXRLVLS];           
225 } opj_tccp_t;
226
227 /**
228 Tile coding parameters : coding/decoding parameters common to all tiles 
229 (information like COD, COC in main header)
230 */
231 typedef struct opj_tcp {
232 /** 1 : first part-tile of a tile */
233         int first;                              
234         /** coding style */
235         int csty;                               
236         /** progression order */
237         OPJ_PROG_ORDER prg;             
238         /** number of layers */
239         int numlayers;                  
240         /** multi-component transform identifier */
241         int mct;                                
242         /** rates of layers */
243         float rates[100];                       
244         /** number of progression order changes */
245         int numpocs;                    
246         /** indicates if a POC marker has been used O:NO, 1:YES */
247         int POC;                                
248         /** progression order changes */
249         opj_poc_t pocs[J3D_MAXRLVLS - 1];
250         /** add fixed_quality */
251         float distoratio[100];
252         /** tile-component coding parameters */
253         opj_tccp_t *tccps;              
254 /** packet header store there for futur use in t2_decode_packet */
255         unsigned char *ppt_data;                
256         /** pointer remaining on the first byte of the first header if ppt is used */
257         unsigned char *ppt_data_first;  
258         /** If ppt == 1 --> there was a PPT marker for the present tile */
259         int ppt;                
260         /** used in case of multiple marker PPT (number of info already stored) */
261         int ppt_store;  
262         int ppt_len;    
263 } opj_tcp_t;
264
265 /**
266 Coding parameters
267 */
268 typedef struct opj_cp {
269 /** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
270         OPJ_TRANSFORM transform_format;         
271         /** entropy coding format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/
272         OPJ_ENTROPY_CODING encoding_format;     
273         /** allocation by rate/distortion */
274         int disto_alloc;        
275         /** allocation by fixed layer */
276         int fixed_alloc;        
277         /** add fixed_quality */
278         int fixed_quality;      
279         /** Rsiz: capabilities */
280         int rsiz;                       
281         /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */
282         int reduce[3];          
283         /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
284         int layer;                      
285         /** 0 = no index || 1 = index */
286         int index_on;           
287         /** Big-Endian/Little-endian order */
288         int bigendian;
289         /** XTOsiz */
290         int tx0;        
291         /** YTOsiz */
292         int ty0;                
293         /** ZTOsiz */
294         int tz0;        
295         /** XTsiz */
296         int tdx;        
297         /** YTsiz */
298         int tdy;        
299         /** ZTsiz */
300         int tdz;        
301         /** comment for coding */
302         char *comment;          
303         /** number of tiles in width, heigth and depth */
304         int tw;         
305         int th;
306         int tl;
307         /** ID number of the tiles present in the codestream */
308         int *tileno;    
309         /** size of the vector tileno */
310         int tileno_size;
311         /** tile coding parameters */
312         opj_tcp_t *tcps;
313         /** fixed layer */
314         int *matrice;           
315
316         /** packet header store there for futur use in t2_decode_packet */
317         unsigned char *ppm_data;                
318         /** pointer remaining on the first byte of the first header if ppm is used */
319         unsigned char *ppm_data_first;  
320         /** if ppm == 1 --> there was a PPM marker for the present tile */
321         int ppm;                        
322         /** use in case of multiple marker PPM (number of info already store) */
323         int ppm_store;          
324         /** use in case of multiple marker PPM (case on non-finished previous info) */
325         int ppm_previous;       
326         int ppm_len;            
327 } opj_cp_t;
328
329 /**
330 Information concerning a packet inside tile
331 */
332 typedef struct opj_packet_info {
333         /** start position */
334         int start_pos;  
335         /** end position */
336         int end_pos;    
337         /** distorsion introduced */
338         double disto;   
339 } opj_packet_info_t;
340
341 /**
342 Index structure : information regarding tiles inside volume
343 */
344 typedef struct opj_tile_info {
345         /** value of thresh for each layer by tile cfr. Marcela   */
346         double *thresh;         
347         /** number of tile */
348         int num_tile;           
349         /** start position */
350         int start_pos;          
351         /** end position of the header */
352         int end_header;         
353         /** end position */
354         int end_pos;            
355         /** precinct number for each resolution level (width, heigth and depth) */
356         int prctno[3][J3D_MAXRLVLS];    
357         /** precinct size (in power of 2), in X for each resolution level */
358         int prctsiz[3][J3D_MAXRLVLS];   
359         /** information concerning packets inside tile */
360         opj_packet_info_t *packet;              
361         
362         /** add fixed_quality */
363         int nbpix;                      
364         /** add fixed_quality */
365         double distotile;       
366 } opj_tile_info_t;
367
368 /**
369 Index structure
370 */
371 typedef struct opj_volume_info {
372         
373         /** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */
374         OPJ_TRANSFORM transform_format;         
375         /** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/
376         OPJ_ENTROPY_CODING encoding_format;     /** 0 = no index || 1 = index */
377         int index_on;   
378         /** 0 = wt 9-7 || 1 = wt 5-3 || >=2 wt atk defined */
379         int dwtid[3];   
380         /** maximum distortion reduction on the whole volume (add for Marcela) */
381         double D_max;   
382         /** packet number */
383         int num;                
384         /** writing the packet in the index with t2_encode_packets */
385         int index_write;        
386         /** volume width, height and depth */
387         int volume_w;   
388         int volume_h;
389         int volume_l;
390         /** progression order */
391         OPJ_PROG_ORDER prog;    
392         /** tile size in x, y and z */
393         int tile_x;             
394         int tile_y;
395         int tile_z;
396         /** tile origin in x, y and z */
397         int tile_Ox;    
398         int tile_Oy;
399         int tile_Oz;
400         /** number of tiles in X, Y and Z */
401         int tw;                 
402         int th;
403         int tl;
404         /** component numbers */
405         int comp;                               
406         /** number of layer */
407         int layer;                              
408         /** number of decomposition in X, Y and Z*/
409         int decomposition[3];   
410         /** DC offset (15444-2) */
411         int dcoffset;   
412         /** main header position */
413         int main_head_end;              
414         /** codestream's size */
415         int codestream_size;    
416         /** information regarding tiles inside volume */
417         opj_tile_info_t *tile;  
418 } opj_volume_info_t;
419
420 /**
421 JPEG-2000 codestream reader/writer
422 */
423 typedef struct opj_j3d {
424         /** codec context */
425         opj_common_ptr cinfo;   
426         /** locate in which part of the codestream the decoder is (main header, tile header, end) */
427         int state;                              
428         /** number of the tile curently concern by coding/decoding */
429         int curtileno;                  
430         /** locate the position of the end of the tile in the codestream, used to detect a truncated codestream (in j3d_read_sod)       */
431         unsigned char *eot;     
432         /**     locate the start position of the SOT marker of the current coded tile:  */
433         int sot_start;          
434         /*  after encoding the tile, a jump (in j3d_write_sod) is done to the SOT marker to store the value of its length. */
435         int sod_start;          
436         /**     as the J3D-file is written in several parts during encoding, it enables to make the right correction in position return by cio_tell     */
437         int pos_correction;     
438         /** array used to store the data of each tile */
439         unsigned char **tile_data;      
440         /** array used to store the length of each tile */
441         int *tile_len;                          
442
443         /** decompression only : store decoding parameters common to all tiles */
444         opj_tcp_t *default_tcp;         
445         /** pointer to the encoded / decoded volume */
446         opj_volume_t *volume;           
447         /** pointer to the coding parameters */
448         opj_cp_t *cp;                           
449         /** helper used to write the index file */
450         opj_volume_info_t *volume_info; 
451         /** pointer to the byte i/o stream */
452     opj_cio_t *cio;                                             
453 } opj_j3d_t;
454
455 /** @name Funciones generales */
456 /*@{*/
457 /* ----------------------------------------------------------------------- */
458 /**
459 Creates a J3D decompression structure
460 @param cinfo Codec context info
461 @return Returns a handle to a J3D decompressor if successful, returns NULL otherwise
462 */
463 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo);
464 /**
465 Destroy a J3D decompressor handle
466 @param j3d J3D decompressor handle to destroy
467 */
468 void j3d_destroy_decompress(opj_j3d_t *j3d);
469 /**
470 Setup the decoder decoding parameters using user parameters.
471 Decoding parameters are returned in j3d->cp. 
472 @param j3d J3D decompressor handle
473 @param parameters decompression parameters
474 */
475 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters);
476 /**
477 Decode an volume from a JPEG-2000 codestream
478 @param j3d J3D decompressor handle
479 @param cio Input buffer stream
480 @return Returns a decoded volume if successful, returns NULL otherwise
481 */
482 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio);
483 /**
484 Decode an volume form a JPT-stream (JPEG 2000, JPIP)
485 @param j3d J3D decompressor handle
486 @param cio Input buffer stream
487 @return Returns a decoded volume if successful, returns NULL otherwise
488 */
489 opj_volume_t* j3d_decode_jpt_stream(opj_j3d_t *j3d, opj_cio_t *cio);
490 /**
491 Creates a J3D compression structure
492 @param cinfo Codec context info
493 @return Returns a handle to a J3D compressor if successful, returns NULL otherwise
494 */
495 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo);
496 /**
497 Destroy a J3D compressor handle
498 @param j3d J3D compressor handle to destroy
499 */
500 void j3d_destroy_compress(opj_j3d_t *j3d);
501 /**
502 Setup the encoder parameters using the current volume and using user parameters. 
503 Coding parameters are returned in j3d->cp. 
504 @param j3d J3D compressor handle
505 @param parameters compression parameters
506 @param volume input filled volume
507 */
508 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume);
509 /**
510 Encode an volume into a JPEG-2000 codestream
511 @param j3d J3D compressor handle
512 @param cio Output buffer stream
513 @param volume Volume to encode
514 @param index Name of the index file if required, NULL otherwise
515 @return Returns true if successful, returns false otherwise
516 */
517 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index);
518 /* ----------------------------------------------------------------------- */
519 /*@}*/
520
521 /*@}*/
522
523 #endif /* __J3D_H */