0b37901fe535e2818dc34a55768234c427cdd042
[openjpeg.git] / libopenjpeg / j2k.h
1 /*
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, Herv� Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef __J2K_H
31 #define __J2K_H
32 /**
33 @file j2k.h
34 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
35
36 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
37 */
38
39 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
40 /*@{*/
41
42 #define J2K_CP_CSTY_PRT 0x01
43 #define J2K_CP_CSTY_SOP 0x02
44 #define J2K_CP_CSTY_EPH 0x04
45 #define J2K_CCP_CSTY_PRT 0x01
46 #define J2K_CCP_CBLKSTY_LAZY 0x01
47 #define J2K_CCP_CBLKSTY_RESET 0x02
48 #define J2K_CCP_CBLKSTY_TERMALL 0x04
49 #define J2K_CCP_CBLKSTY_VSC 0x08
50 #define J2K_CCP_CBLKSTY_PTERM 0x10
51 #define J2K_CCP_CBLKSTY_SEGSYM 0x20
52 #define J2K_CCP_QNTSTY_NOQNT 0
53 #define J2K_CCP_QNTSTY_SIQNT 1
54 #define J2K_CCP_QNTSTY_SEQNT 2
55
56 /* ----------------------------------------------------------------------- */
57
58 #define J2K_MS_SOC 0xff4f /**< SOC marker value */
59 #define J2K_MS_SOT 0xff90 /**< SOT marker value */
60 #define J2K_MS_SOD 0xff93 /**< SOD marker value */
61 #define J2K_MS_EOC 0xffd9 /**< EOC marker value */
62 #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
63 #define J2K_MS_COD 0xff52 /**< COD marker value */
64 #define J2K_MS_COC 0xff53 /**< COC marker value */
65 #define J2K_MS_RGN 0xff5e /**< RGN marker value */
66 #define J2K_MS_QCD 0xff5c /**< QCD marker value */
67 #define J2K_MS_QCC 0xff5d /**< QCC marker value */
68 #define J2K_MS_POC 0xff5f /**< POC marker value */
69 #define J2K_MS_TLM 0xff55 /**< TLM marker value */
70 #define J2K_MS_PLM 0xff57 /**< PLM marker value */
71 #define J2K_MS_PLT 0xff58 /**< PLT marker value */
72 #define J2K_MS_PPM 0xff60 /**< PPM marker value */
73 #define J2K_MS_PPT 0xff61 /**< PPT marker value */
74 #define J2K_MS_SOP 0xff91 /**< SOP marker value */
75 #define J2K_MS_EPH 0xff92 /**< EPH marker value */
76 #define J2K_MS_CRG 0xff63 /**< CRG marker value */
77 #define J2K_MS_COM 0xff64 /**< COM marker value */
78
79 /* ----------------------------------------------------------------------- */
80
81 /**
82 Values that specify the status of the decoding process when decoding the main header. 
83 These values may be combined with a | operator. 
84 */
85 typedef enum J2K_STATUS {
86   J2K_STATE_MHSOC  = 0x0001, /**< a SOC marker is expected */
87   J2K_STATE_MHSIZ  = 0x0002, /**< a SIZ marker is expected */
88   J2K_STATE_MH     = 0x0004, /**< the decoding process is in the main header */
89   J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
90   J2K_STATE_TPH    = 0x0010, /**< the decoding process is in a tile part header */
91   J2K_STATE_MT     = 0x0020, /**< the EOC marker has just been read */
92   J2K_STATE_NEOC   = 0x0040  /**< the decoding process must not expect a EOC marker because the codestream is truncated */
93 } J2K_STATUS;
94
95 /* ----------------------------------------------------------------------- */
96
97 /**
98 Quantization stepsize
99 */
100 typedef struct opj_stepsize {
101   /** exponent */
102   int expn;
103   /** mantissa */
104   int mant;
105 } opj_stepsize_t;
106
107 /**
108 Tile-component coding parameters
109 */
110 typedef struct opj_tccp {
111   /** coding style */
112   int csty;
113   /** number of resolutions */
114   int numresolutions;
115   /** code-blocks width */
116   int cblkw;
117   /** code-blocks height */
118   int cblkh;
119   /** code-block coding style */
120   int cblksty;
121   /** discrete wavelet transform identifier */
122   int qmfbid;
123   /** quantisation style */
124   int qntsty;
125   /** stepsizes used for quantization */
126   opj_stepsize_t stepsizes[J2K_MAXBANDS];
127   /** number of guard bits */
128   int numgbits;
129   /** Region Of Interest shift */
130   int roishift;
131   /** precinct width */
132   int prcw[J2K_MAXRLVLS];
133   /** precinct height */
134   int prch[J2K_MAXRLVLS]; 
135 } opj_tccp_t;
136
137 /**
138 Tile coding parameters : 
139 this structure is used to store coding/decoding parameters common to all
140 tiles (information like COD, COC in main header)
141 */
142 typedef struct opj_tcp {
143   /** 1 : first part-tile of a tile */
144   int first;
145   /** coding style */
146   int csty;
147   /** progression order */
148   OPJ_PROG_ORDER prg;
149   /** number of layers */
150   int numlayers;
151   /** multi-component transform identifier */
152   int mct;
153   /** rates of layers */
154   int rates[100];
155   /** number of progression order changes */
156   int numpocs;
157   /** indicates if a POC marker has been used O:NO, 1:YES */
158   int POC;
159   /** progression order changes */
160   opj_poc_t pocs[32];
161   /** packet header store there for futur use in t2_decode_packet */
162   unsigned char *ppt_data;
163   /** pointer remaining on the first byte of the first header if ppt is used */
164   unsigned char *ppt_data_first;
165   /** If ppt == 1 --> there was a PPT marker for the present tile */
166   int ppt;
167   /** used in case of multiple marker PPT (number of info already stored) */
168   int ppt_store;
169   /** ppmbug1 */
170   int ppt_len;
171   /** add fixed_quality */
172   float distoratio[100];
173   /** tile-component coding parameters */
174   opj_tccp_t *tccps;
175 } opj_tcp_t;
176
177 /**
178 Coding parameters
179 */
180 typedef struct opj_cp {
181   /** allocation by rate/distortion */
182   int disto_alloc;
183   /** allocation by fixed layer */
184   int fixed_alloc;
185   /** add fixed_quality */
186   int fixed_quality;
187   /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
188   int reduce;
189   /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
190   int layer;
191   /** 0 = no index || 1 = index */
192   int index_on;
193   /** XTOsiz */
194   int tx0;
195   /** YTOsiz */
196   int ty0;
197   /** XTsiz */
198   int tdx;
199   /** YTsiz */
200   int tdy;
201   /** comment for coding */
202   char *comment;
203   /** number of tiles in width */
204   int tw;
205   /** number of tiles in heigth */
206   int th;
207   /** ID number of the tiles present in the codestream */
208   int *tileno;
209   /** size of the vector tileno */
210   int tileno_size;
211   /** packet header store there for futur use in t2_decode_packet */
212   unsigned char *ppm_data;
213   /** pointer remaining on the first byte of the first header if ppm is used */
214   unsigned char *ppm_data_first;
215   /** if ppm == 1 --> there was a PPM marker for the present tile */
216   int ppm;
217   /** use in case of multiple marker PPM (number of info already store) */
218   int ppm_store;
219   /** use in case of multiple marker PPM (case on non-finished previous info) */
220   int ppm_previous;
221   /** ppmbug1 */
222   int ppm_len;
223   /** tile coding parameters */
224   opj_tcp_t *tcps;
225   /** fixed layer */
226   int *matrice;
227 } opj_cp_t;
228
229 /**
230 Information concerning a packet inside tile
231 */
232 typedef struct opj_packet_info {
233   /** start position */
234   int start_pos;
235   /** end position */
236   int end_pos;
237   /** ADD for Marcela */
238   double disto;
239 } opj_packet_info_t;
240
241 /**
242 Index structure : information regarding tiles inside image
243 */
244 typedef struct opj_tile_info {
245   /** value of thresh for each layer by tile cfr. Marcela   */
246   double *thresh;
247   /** number of tile */
248   int num_tile;
249   /** start position */
250   int start_pos;
251   /** end position of the header */
252   int end_header;
253   /** end position */
254   int end_pos;
255   /** precinct number for each resolution level (width) */
256   int pw[33];
257   /** precinct number for each resolution level (height) */
258   int ph[33];
259   /** precinct size (in power of 2), in X for each resolution level */
260   int pdx[33];
261   /** precinct size (in power of 2), in Y for each resolution level */
262   int pdy[33];
263   /** information concerning packets inside tile */
264   opj_packet_info_t *packet;
265   /** add fixed_quality */
266   int nbpix;
267   /** add fixed_quality */
268   double distotile;
269 } opj_tile_info_t;
270
271 /**
272 Index structure
273 */
274 typedef struct opj_image_info {
275   /** 0 = no index || 1 = index */
276   int index_on;
277   /** maximum distortion reduction on the whole image (add for Marcela) */
278   double D_max;
279   /** packet number */
280   int num;
281   /** writing the packet in the index with t2_encode_packets */
282   int index_write;
283   /** image width */
284   int image_w;
285   /** image height */
286   int image_h;
287   /** progression order */
288   OPJ_PROG_ORDER prog;
289   /** tile size in x */
290   int tile_x;
291   /** tile size in y */
292   int tile_y;
293   /** */
294   int tile_Ox;
295   /** */
296   int tile_Oy;
297   /** number of tiles in X */
298   int tw;
299   /** number of tiles in Y */
300   int th;
301   /** component numbers */
302   int comp;
303   /** number of layer */
304   int layer;
305   /** number of decomposition */
306   int decomposition;
307   /** main header position */
308   int main_head_end;
309   /** codestream's size */
310   int codestream_size;
311   /** information regarding tiles inside image */
312   opj_tile_info_t *tile;
313 } opj_image_info_t;
314
315 /**
316 JPEG-2000 codestream reader/writer
317 */
318 typedef struct opj_j2k {
319   /** codec context */
320   opj_common_ptr cinfo;
321
322   /** locate in which part of the codestream the decoder is (main header, tile header, end) */
323   int state;
324   /** number of the tile curently concern by coding/decoding */
325   int curtileno;
326   /** 
327   locate the position of the end of the tile in the codestream, 
328   used to detect a truncated codestream (in j2k_read_sod)
329   */
330   unsigned char *eot;
331   /**
332   locate the start position of the SOT marker of the current coded tile:  
333   after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. 
334   */
335   int sot_start;
336   int sod_start;
337   /**
338   as the J2K-file is written in several parts during encoding, 
339   it enables to make the right correction in position return by cio_tell
340   */
341   int pos_correction;
342   /** array used to store the data of each tile */
343   unsigned char **tile_data;
344   /** array used to store the length of each tile */
345   int *tile_len;
346   /** 
347   decompression only : 
348   store decoding parameters common to all tiles (information like COD, COC in main header)
349   */
350   opj_tcp_t *default_tcp;
351   /** pointer to the encoded / decoded image */
352   opj_image_t *image;
353   /** pointer to the coding parameters */
354   opj_cp_t *cp;
355   /** helper used to write the index file */
356   opj_image_info_t *image_info;
357   /** pointer to the byte i/o stream */
358   opj_cio_t *cio;
359 } opj_j2k_t;
360
361 /** @name Local static functions */
362 /*@{*/
363 /* ----------------------------------------------------------------------- */
364 /**
365 Write the SOC marker (Start Of Codestream)
366 @param j2k J2K handle
367 */
368 static void j2k_write_soc(opj_j2k_t *j2k);
369 /**
370 Read the SOC marker (Start of Codestream)
371 @param j2k J2K handle
372 */
373 static void j2k_read_soc(opj_j2k_t *j2k);
374 /**
375 Write the SIZ marker (image and tile size)
376 @param j2k J2K handle
377 */
378 static void j2k_write_siz(opj_j2k_t *j2k);
379 /**
380 Read the SIZ marker (image and tile size)
381 @param j2k J2K handle
382 */
383 static void j2k_read_siz(opj_j2k_t *j2k);
384 /**
385 Write the COM marker (comment)
386 @param j2k J2K handle
387 */
388 static void j2k_write_com(opj_j2k_t *j2k);
389 /**
390 Read the COM marker (comment)
391 @param j2k J2K handle
392 */
393 static void j2k_read_com(opj_j2k_t *j2k);
394 /**
395 Write the value concerning the specified component in the marker COD and COC
396 @param j2k J2K handle
397 @param compno Number of the component concerned by the information written
398 */
399 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
400 /**
401 Read the value concerning the specified component in the marker COD and COC
402 @param j2k J2K handle
403 @param compno Number of the component concerned by the information read
404 */
405 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
406 /**
407 Write the COD marker (coding style default)
408 @param j2k J2K handle
409 */
410 static void j2k_write_cod(opj_j2k_t *j2k);
411 /**
412 Read the COD marker (coding style default)
413 @param j2k J2K handle
414 */
415 static void j2k_read_cod(opj_j2k_t *j2k);
416 /**
417 Write the COC marker (coding style component)
418 @param j2k J2K handle
419 @param compno Number of the component concerned by the information written
420 */
421 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
422 /**
423 Read the COC marker (coding style component)
424 @param j2k J2K handle
425 */
426 static void j2k_read_coc(opj_j2k_t *j2k);
427 /**
428 Write the value concerning the specified component in the marker QCD and QCC
429 @param j2k J2K handle
430 @param compno Number of the component concerned by the information written
431 */
432 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
433 /**
434 Read the value concerning the specified component in the marker QCD and QCC
435 @param j2k J2K handle
436 @param compno Number of the component concern by the information read
437 @param len Length of the information in the QCX part of the marker QCD/QCC
438 */
439 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
440 /**
441 Write the QCD marker (quantization default)
442 @param j2k J2K handle
443 */
444 static void j2k_write_qcd(opj_j2k_t *j2k);
445 /**
446 Read the QCD marker (quantization default)
447 @param j2k J2K handle
448 */
449 static void j2k_read_qcd(opj_j2k_t *j2k);
450 /**
451 Write the QCC marker (quantization component)
452 @param j2k J2K handle
453 @param compno Number of the component concerned by the information written
454 */
455 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
456 /**
457 Read the QCC marker (quantization component)
458 @param j2k J2K handle
459 */
460 static void j2k_read_qcc(opj_j2k_t *j2k);
461 /**
462 Write the POC marker (progression order change)
463 @param j2k J2K handle
464 */
465 static void j2k_write_poc(opj_j2k_t *j2k);
466 /**
467 Read the POC marker (progression order change)
468 @param j2k J2K handle
469 */
470 static void j2k_read_poc(opj_j2k_t *j2k);
471 /**
472 Read the CRG marker (component registration)
473 @param j2k J2K handle
474 */
475 static void j2k_read_crg(opj_j2k_t *j2k);
476 /**
477 Read the TLM marker (tile-part lengths)
478 @param j2k J2K handle
479 */
480 static void j2k_read_tlm(opj_j2k_t *j2k);
481 /**
482 Read the PLM marker (packet length, main header)
483 @param j2k J2K handle
484 */
485 static void j2k_read_plm(opj_j2k_t *j2k);
486 /**
487 Read the PLT marker (packet length, tile-part header)
488 @param j2k J2K handle
489 */
490 static void j2k_read_plt(opj_j2k_t *j2k);
491 /**
492 Read the PPM marker (packet packet headers, main header)
493 @param j2k J2K handle
494 */
495 static void j2k_read_ppm(opj_j2k_t *j2k);
496 /**
497 Read the PPT marker (packet packet headers, tile-part header)
498 @param j2k J2K handle
499 */
500 static void j2k_read_ppt(opj_j2k_t *j2k);
501 /**
502 Write the SOT marker (start of tile-part)
503 @param j2k J2K handle
504 */
505 static void j2k_write_sot(opj_j2k_t *j2k);
506 /**
507 Read the SOT marker (start of tile-part)
508 @param j2k J2K handle
509 */
510 static void j2k_read_sot(opj_j2k_t *j2k);
511 /**
512 Write the SOD marker (start of data)
513 @param j2k J2K handle
514 @param tile_coder Pointer to a TCD handle
515 */
516 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
517 /**
518 Read the SOD marker (start of data)
519 @param j2k J2K handle
520 */
521 static void j2k_read_sod(opj_j2k_t *j2k);
522 /**
523 Write the RGN marker (region-of-interest)
524 @param j2k J2K handle
525 @param compno Number of the component concerned by the information written
526 @param tileno Number of the tile concerned by the information written
527 */
528 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
529 /**
530 Read the RGN marker (region-of-interest)
531 @param j2k J2K handle
532 */
533 static void j2k_read_rgn(opj_j2k_t *j2k);
534 /**
535 Write the EOC marker (end of codestream)
536 @param j2k J2K handle
537 */
538 static void j2k_write_eoc(opj_j2k_t *j2k);
539 /**
540 Read the EOC marker (end of codestream)
541 @param j2k J2K handle
542 */
543 static void j2k_read_eoc(opj_j2k_t *j2k);
544 /**
545 Read an unknown marker
546 @param j2k J2K handle
547 */
548 static void j2k_read_unk(opj_j2k_t *j2k);
549 /* ----------------------------------------------------------------------- */
550 /*@}*/
551
552 /** @name Exported functions */
553 /*@{*/
554 /* ----------------------------------------------------------------------- */
555 /**
556 Creates a J2K decompression structure
557 @param cinfo Codec context info
558 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
559 */
560 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
561 /**
562 Destroy a J2K decompressor handle
563 @param j2k J2K decompressor handle to destroy
564 */
565 void j2k_destroy_decompress(opj_j2k_t *j2k);
566 /**
567 Setup the decoder decoding parameters using user parameters.
568 Decoding parameters are returned in j2k->cp. 
569 @param j2k J2K decompressor handle
570 @param parameters decompression parameters
571 */
572 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
573 /**
574 Decode an image from a JPEG-2000 codestream
575 @param j2k J2K decompressor handle
576 @param cio Input buffer stream
577 @return Returns a decoded image if successful, returns NULL otherwise
578 */
579 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio);
580 /**
581 Decode an image form a JPT-stream (JPEG 2000, JPIP)
582 @param j2k J2K decompressor handle
583 @param cio Input buffer stream
584 @return Returns a decoded image if successful, returns NULL otherwise
585 */
586 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio);
587 /**
588 Creates a J2K compression structure
589 @param cinfo Codec context info
590 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
591 */
592 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
593 /**
594 Destroy a J2K compressor handle
595 @param j2k J2K compressor handle to destroy
596 */
597 void j2k_destroy_compress(opj_j2k_t *j2k);
598 /**
599 Setup the encoder parameters using the current image and using user parameters. 
600 Coding parameters are returned in j2k->cp. 
601 @param j2k J2K compressor handle
602 @param parameters compression parameters
603 @param image input filled image
604 */
605 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
606 /**
607 Encode an image into a JPEG-2000 codestream
608 @param j2k J2K compressor handle
609 @param cio Output buffer stream
610 @param image Image to encode
611 @param index Name of the index file if required, NULL otherwise
612 @return Returns true if successful, returns false otherwise
613 */
614 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index);
615 /* ----------------------------------------------------------------------- */
616 /*@}*/
617
618 /*@}*/
619
620 #endif /* __J2K_H */