]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/libopenjpeg/j2k.h
e80b68bfcadbe61501b32ca03507b25c3c198d96
[gdcm.git] / src / gdcmopenjpeg / libopenjpeg / j2k.h
1 /*\r
2  * Copyright (c) 2001-2003, David Janssens\r
3  * Copyright (c) 2002-2003, Yannick Verschueren\r
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
5  * Copyright (c) 2005, HervĂ© Drolon, FreeImage Team\r
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
7  * All rights reserved.\r
8  *\r
9  * Redistribution and use in source and binary forms, with or without\r
10  * modification, are permitted provided that the following conditions\r
11  * are met:\r
12  * 1. Redistributions of source code must retain the above copyright\r
13  *    notice, this list of conditions and the following disclaimer.\r
14  * 2. Redistributions in binary form must reproduce the above copyright\r
15  *    notice, this list of conditions and the following disclaimer in the\r
16  *    documentation and/or other materials provided with the distribution.\r
17  *\r
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  */\r
30 #ifndef __J2K_H\r
31 #define __J2K_H\r
32 /**\r
33 @file j2k.h\r
34 @brief The JPEG-2000 Codestream Reader/Writer (J2K)\r
35 \r
36 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.\r
37 */\r
38 \r
39 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */\r
40 /*@{*/\r
41 \r
42 #define J2K_CP_CSTY_PRT 0x01\r
43 #define J2K_CP_CSTY_SOP 0x02\r
44 #define J2K_CP_CSTY_EPH 0x04\r
45 #define J2K_CCP_CSTY_PRT 0x01\r
46 #define J2K_CCP_CBLKSTY_LAZY 0x01\r
47 #define J2K_CCP_CBLKSTY_RESET 0x02\r
48 #define J2K_CCP_CBLKSTY_TERMALL 0x04\r
49 #define J2K_CCP_CBLKSTY_VSC 0x08\r
50 #define J2K_CCP_CBLKSTY_PTERM 0x10\r
51 #define J2K_CCP_CBLKSTY_SEGSYM 0x20\r
52 #define J2K_CCP_QNTSTY_NOQNT 0\r
53 #define J2K_CCP_QNTSTY_SIQNT 1\r
54 #define J2K_CCP_QNTSTY_SEQNT 2\r
55 \r
56 /* ----------------------------------------------------------------------- */\r
57 \r
58 #define J2K_MS_SOC 0xff4f  /**< SOC marker value */\r
59 #define J2K_MS_SOT 0xff90  /**< SOT marker value */\r
60 #define J2K_MS_SOD 0xff93  /**< SOD marker value */\r
61 #define J2K_MS_EOC 0xffd9  /**< EOC marker value */\r
62 #define J2K_MS_SIZ 0xff51  /**< SIZ marker value */\r
63 #define J2K_MS_COD 0xff52  /**< COD marker value */\r
64 #define J2K_MS_COC 0xff53  /**< COC marker value */\r
65 #define J2K_MS_RGN 0xff5e  /**< RGN marker value */\r
66 #define J2K_MS_QCD 0xff5c  /**< QCD marker value */\r
67 #define J2K_MS_QCC 0xff5d  /**< QCC marker value */\r
68 #define J2K_MS_POC 0xff5f  /**< POC marker value */\r
69 #define J2K_MS_TLM 0xff55  /**< TLM marker value */\r
70 #define J2K_MS_PLM 0xff57  /**< PLM marker value */\r
71 #define J2K_MS_PLT 0xff58  /**< PLT marker value */\r
72 #define J2K_MS_PPM 0xff60  /**< PPM marker value */\r
73 #define J2K_MS_PPT 0xff61  /**< PPT marker value */\r
74 #define J2K_MS_SOP 0xff91  /**< SOP marker value */\r
75 #define J2K_MS_EPH 0xff92  /**< EPH marker value */\r
76 #define J2K_MS_CRG 0xff63  /**< CRG marker value */\r
77 #define J2K_MS_COM 0xff64  /**< COM marker value */\r
78 \r
79 /* ----------------------------------------------------------------------- */\r
80 \r
81 /**\r
82 Values that specify the status of the decoding process when decoding the main header. \r
83 These values may be combined with a | operator. \r
84 */\r
85 typedef enum J2K_STATUS {\r
86   J2K_STATE_MHSOC  = 0x0001, /**< a SOC marker is expected */\r
87   J2K_STATE_MHSIZ  = 0x0002, /**< a SIZ marker is expected */\r
88   J2K_STATE_MH     = 0x0004, /**< the decoding process is in the main header */\r
89   J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */\r
90   J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */\r
91   J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */\r
92   J2K_STATE_NEOC   = 0x0040  /**< the decoding process must not expect a EOC marker because the codestream is truncated */\r
93 } J2K_STATUS;\r
94 \r
95 /* ----------------------------------------------------------------------- */\r
96 \r
97 /**\r
98 Quantization stepsize\r
99 */\r
100 typedef struct opj_stepsize {\r
101   /** exponent */\r
102   int expn;\r
103   /** mantissa */\r
104   int mant;\r
105 } opj_stepsize_t;\r
106 \r
107 /**\r
108 Tile-component coding parameters\r
109 */\r
110 typedef struct opj_tccp {\r
111   /** coding style */\r
112   int csty;\r
113   /** number of resolutions */\r
114   int numresolutions;\r
115   /** code-blocks width */\r
116   int cblkw;\r
117   /** code-blocks height */\r
118   int cblkh;\r
119   /** code-block coding style */\r
120   int cblksty;\r
121   /** discrete wavelet transform identifier */\r
122   int qmfbid;\r
123   /** quantisation style */\r
124   int qntsty;\r
125   /** stepsizes used for quantization */\r
126   opj_stepsize_t stepsizes[J2K_MAXBANDS];\r
127   /** number of guard bits */\r
128   int numgbits;\r
129   /** Region Of Interest shift */\r
130   int roishift;\r
131   /** precinct width */\r
132   int prcw[J2K_MAXRLVLS];\r
133   /** precinct height */\r
134   int prch[J2K_MAXRLVLS];  \r
135 } opj_tccp_t;\r
136 \r
137 /**\r
138 Tile coding parameters : \r
139 this structure is used to store coding/decoding parameters common to all\r
140 tiles (information like COD, COC in main header)\r
141 */\r
142 typedef struct opj_tcp {\r
143   /** 1 : first part-tile of a tile */\r
144   int first;\r
145   /** coding style */\r
146   int csty;\r
147   /** progression order */\r
148   OPJ_PROG_ORDER prg;\r
149   /** number of layers */\r
150   int numlayers;\r
151   /** multi-component transform identifier */\r
152   int mct;\r
153   /** rates of layers */\r
154   int rates[100];\r
155   /** number of progression order changes */\r
156   int numpocs;\r
157   /** indicates if a POC marker has been used O:NO, 1:YES */\r
158   int POC;\r
159   /** progression order changes */\r
160   opj_poc_t pocs[32];\r
161   /** packet header store there for futur use in t2_decode_packet */\r
162   unsigned char *ppt_data;\r
163   /** pointer remaining on the first byte of the first header if ppt is used */\r
164   unsigned char *ppt_data_first;\r
165   /** If ppt == 1 --> there was a PPT marker for the present tile */\r
166   int ppt;\r
167   /** used in case of multiple marker PPT (number of info already stored) */\r
168   int ppt_store;\r
169   /** ppmbug1 */\r
170   int ppt_len;\r
171   /** add fixed_quality */\r
172   float distoratio[100];\r
173   /** tile-component coding parameters */\r
174   opj_tccp_t *tccps;\r
175 } opj_tcp_t;\r
176 \r
177 /**\r
178 Coding parameters\r
179 */\r
180 typedef struct opj_cp {\r
181   /** allocation by rate/distortion */\r
182   int disto_alloc;\r
183   /** allocation by fixed layer */\r
184   int fixed_alloc;\r
185   /** add fixed_quality */\r
186   int fixed_quality;\r
187   /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */\r
188   int reduce;\r
189   /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */\r
190   int layer;\r
191   /** 0 = no index || 1 = index */\r
192   int index_on;\r
193   /** XTOsiz */\r
194   int tx0;\r
195   /** YTOsiz */\r
196   int ty0;\r
197   /** XTsiz */\r
198   int tdx;\r
199   /** YTsiz */\r
200   int tdy;\r
201   /** comment for coding */\r
202   char *comment;\r
203   /** number of tiles in width */\r
204   int tw;\r
205   /** number of tiles in heigth */\r
206   int th;\r
207   /** ID number of the tiles present in the codestream */\r
208   int *tileno;\r
209   /** size of the vector tileno */\r
210   int tileno_size;\r
211   /** packet header store there for futur use in t2_decode_packet */\r
212   unsigned char *ppm_data;\r
213   /** pointer remaining on the first byte of the first header if ppm is used */\r
214   unsigned char *ppm_data_first;\r
215   /** if ppm == 1 --> there was a PPM marker for the present tile */\r
216   int ppm;\r
217   /** use in case of multiple marker PPM (number of info already store) */\r
218   int ppm_store;\r
219   /** use in case of multiple marker PPM (case on non-finished previous info) */\r
220   int ppm_previous;\r
221   /** ppmbug1 */\r
222   int ppm_len;\r
223   /** tile coding parameters */\r
224   opj_tcp_t *tcps;\r
225   /** fixed layer */\r
226   int *matrice;\r
227 } opj_cp_t;\r
228 \r
229 /**\r
230 Information concerning a packet inside tile\r
231 */\r
232 typedef struct opj_packet_info {\r
233   /** start position */\r
234   int start_pos;\r
235   /** end position */\r
236   int end_pos;\r
237   /** ADD for Marcela */\r
238   double disto;\r
239 } opj_packet_info_t;\r
240 \r
241 /**\r
242 Index structure : information regarding tiles inside image\r
243 */\r
244 typedef struct opj_tile_info {\r
245   /** value of thresh for each layer by tile cfr. Marcela   */\r
246   double *thresh;\r
247   /** number of tile */\r
248   int num_tile;\r
249   /** start position */\r
250   int start_pos;\r
251   /** end position of the header */\r
252   int end_header;\r
253   /** end position */\r
254   int end_pos;\r
255   /** precinct number for each resolution level (width) */\r
256   int pw[33];\r
257   /** precinct number for each resolution level (height) */\r
258   int ph[33];\r
259   /** precinct size (in power of 2), in X for each resolution level */\r
260   int pdx[33];\r
261   /** precinct size (in power of 2), in Y for each resolution level */\r
262   int pdy[33];\r
263   /** information concerning packets inside tile */\r
264   opj_packet_info_t *packet;\r
265   /** add fixed_quality */\r
266   int nbpix;\r
267   /** add fixed_quality */\r
268   double distotile;\r
269 } opj_tile_info_t;\r
270 \r
271 /**\r
272 Index structure\r
273 */\r
274 typedef struct opj_image_info {\r
275   /** 0 = no index || 1 = index */\r
276   int index_on;\r
277   /** maximum distortion reduction on the whole image (add for Marcela) */\r
278   double D_max;\r
279   /** packet number */\r
280   int num;\r
281   /** writing the packet in the index with t2_encode_packets */\r
282   int index_write;\r
283   /** image width */\r
284   int image_w;\r
285   /** image height */\r
286   int image_h;\r
287   /** progression order */\r
288   OPJ_PROG_ORDER prog;\r
289   /** tile size in x */\r
290   int tile_x;\r
291   /** tile size in y */\r
292   int tile_y;\r
293   /** */\r
294   int tile_Ox;\r
295   /** */\r
296   int tile_Oy;\r
297   /** number of tiles in X */\r
298   int tw;\r
299   /** number of tiles in Y */\r
300   int th;\r
301   /** component numbers */\r
302   int comp;\r
303   /** number of layer */\r
304   int layer;\r
305   /** number of decomposition */\r
306   int decomposition;\r
307   /** main header position */\r
308   int main_head_end;\r
309   /** codestream's size */\r
310   int codestream_size;\r
311   /** information regarding tiles inside image */\r
312   opj_tile_info_t *tile;\r
313 } opj_image_info_t;\r
314 \r
315 /**\r
316 JPEG-2000 codestream reader/writer\r
317 */\r
318 typedef struct opj_j2k {\r
319   /** codec context */\r
320   opj_common_ptr cinfo;\r
321 \r
322   /** locate in which part of the codestream the decoder is (main header, tile header, end) */\r
323   int state;\r
324   /** number of the tile curently concern by coding/decoding */\r
325   int curtileno;\r
326   /** \r
327   locate the position of the end of the tile in the codestream, \r
328   used to detect a truncated codestream (in j2k_read_sod)\r
329   */\r
330   unsigned char *eot;\r
331   /**\r
332   locate the start position of the SOT marker of the current coded tile:  \r
333   after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. \r
334   */\r
335   int sot_start;\r
336   int sod_start;\r
337   /**\r
338   as the J2K-file is written in several parts during encoding, \r
339   it enables to make the right correction in position return by cio_tell\r
340   */\r
341   int pos_correction;\r
342   /** array used to store the data of each tile */\r
343   unsigned char **tile_data;\r
344   /** array used to store the length of each tile */\r
345   int *tile_len;\r
346   /** \r
347   decompression only : \r
348   store decoding parameters common to all tiles (information like COD, COC in main header)\r
349   */\r
350   opj_tcp_t *default_tcp;\r
351   /** pointer to the encoded / decoded image */\r
352   opj_image_t *image;\r
353   /** pointer to the coding parameters */\r
354   opj_cp_t *cp;\r
355   /** helper used to write the index file */\r
356   opj_image_info_t *image_info;\r
357   /** pointer to the byte i/o stream */\r
358   opj_cio_t *cio;\r
359 } opj_j2k_t;\r
360 \r
361 /** @name Exported functions */\r
362 /*@{*/\r
363 /* ----------------------------------------------------------------------- */\r
364 /**\r
365 Creates a J2K decompression structure\r
366 @param cinfo Codec context info\r
367 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise\r
368 */\r
369 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);\r
370 /**\r
371 Destroy a J2K decompressor handle\r
372 @param j2k J2K decompressor handle to destroy\r
373 */\r
374 void j2k_destroy_decompress(opj_j2k_t *j2k);\r
375 /**\r
376 Setup the decoder decoding parameters using user parameters.\r
377 Decoding parameters are returned in j2k->cp. \r
378 @param j2k J2K decompressor handle\r
379 @param parameters decompression parameters\r
380 */\r
381 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);\r
382 /**\r
383 Decode an image from a JPEG-2000 codestream\r
384 @param j2k J2K decompressor handle\r
385 @param cio Input buffer stream\r
386 @return Returns a decoded image if successful, returns NULL otherwise\r
387 */\r
388 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio);\r
389 /**\r
390 Decode an image form a JPT-stream (JPEG 2000, JPIP)\r
391 @param j2k J2K decompressor handle\r
392 @param cio Input buffer stream\r
393 @return Returns a decoded image if successful, returns NULL otherwise\r
394 */\r
395 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio);\r
396 /**\r
397 Creates a J2K compression structure\r
398 @param cinfo Codec context info\r
399 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise\r
400 */\r
401 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);\r
402 /**\r
403 Destroy a J2K compressor handle\r
404 @param j2k J2K compressor handle to destroy\r
405 */\r
406 void j2k_destroy_compress(opj_j2k_t *j2k);\r
407 /**\r
408 Setup the encoder parameters using the current image and using user parameters. \r
409 Coding parameters are returned in j2k->cp. \r
410 @param j2k J2K compressor handle\r
411 @param parameters compression parameters\r
412 @param image input filled image\r
413 */\r
414 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);\r
415 /**\r
416 Encode an image into a JPEG-2000 codestream\r
417 @param j2k J2K compressor handle\r
418 @param cio Output buffer stream\r
419 @param image Image to encode\r
420 @param index Name of the index file if required, NULL otherwise\r
421 @return Returns true if successful, returns false otherwise\r
422 */\r
423 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index);\r
424 /* ----------------------------------------------------------------------- */\r
425 /*@}*/\r
426 \r
427 /*@}*/\r
428 \r
429 #endif /* __J2K_H */\r