j2k.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __J2K_H
  33. #define __J2K_H
  34. /**
  35. @file j2k.h
  36. @brief The JPEG-2000 Codestream Reader/Writer (J2K)
  37. The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
  38. */
  39. /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
  40. /*@{*/
  41. #define J2K_CP_CSTY_PRT 0x01
  42. #define J2K_CP_CSTY_SOP 0x02
  43. #define J2K_CP_CSTY_EPH 0x04
  44. #define J2K_CCP_CSTY_PRT 0x01
  45. #define J2K_CCP_CBLKSTY_LAZY 0x01 /**< Selective arithmetic coding bypass */
  46. #define J2K_CCP_CBLKSTY_RESET 0x02 /**< Reset context probabilities on coding pass boundaries */
  47. #define J2K_CCP_CBLKSTY_TERMALL 0x04 /**< Termination on each coding pass */
  48. #define J2K_CCP_CBLKSTY_VSC 0x08 /**< Vertically stripe causal context */
  49. #define J2K_CCP_CBLKSTY_PTERM 0x10 /**< Predictable termination */
  50. #define J2K_CCP_CBLKSTY_SEGSYM 0x20 /**< Segmentation symbols are used */
  51. #define J2K_CCP_QNTSTY_NOQNT 0
  52. #define J2K_CCP_QNTSTY_SIQNT 1
  53. #define J2K_CCP_QNTSTY_SEQNT 2
  54. /* ----------------------------------------------------------------------- */
  55. #define J2K_MS_SOC 0xff4f /**< SOC marker value */
  56. #define J2K_MS_SOT 0xff90 /**< SOT marker value */
  57. #define J2K_MS_SOD 0xff93 /**< SOD marker value */
  58. #define J2K_MS_EOC 0xffd9 /**< EOC marker value */
  59. #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
  60. #define J2K_MS_COD 0xff52 /**< COD marker value */
  61. #define J2K_MS_COC 0xff53 /**< COC marker value */
  62. #define J2K_MS_RGN 0xff5e /**< RGN marker value */
  63. #define J2K_MS_QCD 0xff5c /**< QCD marker value */
  64. #define J2K_MS_QCC 0xff5d /**< QCC marker value */
  65. #define J2K_MS_POC 0xff5f /**< POC marker value */
  66. #define J2K_MS_TLM 0xff55 /**< TLM marker value */
  67. #define J2K_MS_PLM 0xff57 /**< PLM marker value */
  68. #define J2K_MS_PLT 0xff58 /**< PLT marker value */
  69. #define J2K_MS_PPM 0xff60 /**< PPM marker value */
  70. #define J2K_MS_PPT 0xff61 /**< PPT marker value */
  71. #define J2K_MS_SOP 0xff91 /**< SOP marker value */
  72. #define J2K_MS_EPH 0xff92 /**< EPH marker value */
  73. #define J2K_MS_CRG 0xff63 /**< CRG marker value */
  74. #define J2K_MS_COM 0xff64 /**< COM marker value */
  75. /* UniPG>> */
  76. #ifdef USE_JPWL
  77. #define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
  78. #define J2K_MS_EPB 0xff66 /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */
  79. #define J2K_MS_ESD 0xff67 /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */
  80. #define J2K_MS_RED 0xff69 /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
  81. #endif /* USE_JPWL */
  82. #ifdef USE_JPSEC
  83. #define J2K_MS_SEC 0xff65 /**< SEC marker value (Part 8: Secure JPEG 2000) */
  84. #define J2K_MS_INSEC 0xff94 /**< INSEC marker value (Part 8: Secure JPEG 2000) */
  85. #endif /* USE_JPSEC */
  86. /* <<UniPG */
  87. /* ----------------------------------------------------------------------- */
  88. /**
  89. Values that specify the status of the decoding process when decoding the main header.
  90. These values may be combined with a | operator.
  91. */
  92. typedef enum J2K_STATUS {
  93. J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
  94. J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
  95. J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */
  96. J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
  97. J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
  98. J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */
  99. J2K_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
  100. J2K_STATE_ERR = 0x0080 /**< the decoding process has encountered an error */
  101. } J2K_STATUS;
  102. /* ----------------------------------------------------------------------- */
  103. /**
  104. T2 encoding mode
  105. */
  106. typedef enum T2_MODE {
  107. THRESH_CALC = 0, /** Function called in Rate allocation process*/
  108. FINAL_PASS = 1 /** Function called in Tier 2 process*/
  109. }J2K_T2_MODE;
  110. /**
  111. Quantization stepsize
  112. */
  113. typedef struct opj_stepsize {
  114. /** exponent */
  115. int expn;
  116. /** mantissa */
  117. int mant;
  118. } opj_stepsize_t;
  119. /**
  120. Tile-component coding parameters
  121. */
  122. typedef struct opj_tccp {
  123. /** coding style */
  124. int csty;
  125. /** number of resolutions */
  126. int numresolutions;
  127. /** code-blocks width */
  128. int cblkw;
  129. /** code-blocks height */
  130. int cblkh;
  131. /** code-block coding style */
  132. int cblksty;
  133. /** discrete wavelet transform identifier */
  134. int qmfbid;
  135. /** quantisation style */
  136. int qntsty;
  137. /** stepsizes used for quantization */
  138. opj_stepsize_t stepsizes[J2K_MAXBANDS];
  139. /** number of guard bits */
  140. int numgbits;
  141. /** Region Of Interest shift */
  142. int roishift;
  143. /** precinct width */
  144. int prcw[J2K_MAXRLVLS];
  145. /** precinct height */
  146. int prch[J2K_MAXRLVLS];
  147. } opj_tccp_t;
  148. /**
  149. Tile coding parameters :
  150. this structure is used to store coding/decoding parameters common to all
  151. tiles (information like COD, COC in main header)
  152. */
  153. typedef struct opj_tcp {
  154. /** 1 : first part-tile of a tile */
  155. int first;
  156. /** coding style */
  157. int csty;
  158. /** progression order */
  159. OPJ_PROG_ORDER prg;
  160. /** number of layers */
  161. int numlayers;
  162. /** multi-component transform identifier */
  163. int mct;
  164. /** rates of layers */
  165. float rates[100];
  166. /** number of progression order changes */
  167. int numpocs;
  168. /** indicates if a POC marker has been used O:NO, 1:YES */
  169. int POC;
  170. /** progression order changes */
  171. opj_poc_t pocs[32];
  172. /** packet header store there for futur use in t2_decode_packet */
  173. unsigned char *ppt_data;
  174. /** pointer remaining on the first byte of the first header if ppt is used */
  175. unsigned char *ppt_data_first;
  176. /** If ppt == 1 --> there was a PPT marker for the present tile */
  177. int ppt;
  178. /** used in case of multiple marker PPT (number of info already stored) */
  179. int ppt_store;
  180. /** ppmbug1 */
  181. int ppt_len;
  182. /** add fixed_quality */
  183. float distoratio[100];
  184. /** tile-component coding parameters */
  185. opj_tccp_t *tccps;
  186. } opj_tcp_t;
  187. /**
  188. Coding parameters
  189. */
  190. typedef struct opj_cp {
  191. /** Digital cinema profile*/
  192. OPJ_CINEMA_MODE cinema;
  193. /** Maximum rate for each component. If == 0, component size limitation is not considered */
  194. int max_comp_size;
  195. /** Size of the image in bits*/
  196. int img_size;
  197. /** Rsiz*/
  198. OPJ_RSIZ_CAPABILITIES rsiz;
  199. /** Enabling Tile part generation*/
  200. char tp_on;
  201. /** Flag determining tile part generation*/
  202. char tp_flag;
  203. /** Position of tile part flag in progression order*/
  204. int tp_pos;
  205. /** allocation by rate/distortion */
  206. int disto_alloc;
  207. /** allocation by fixed layer */
  208. int fixed_alloc;
  209. /** add fixed_quality */
  210. int fixed_quality;
  211. /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
  212. int reduce;
  213. /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
  214. int layer;
  215. /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */
  216. OPJ_LIMIT_DECODING limit_decoding;
  217. /** XTOsiz */
  218. int tx0;
  219. /** YTOsiz */
  220. int ty0;
  221. /** XTsiz */
  222. int tdx;
  223. /** YTsiz */
  224. int tdy;
  225. /** comment for coding */
  226. char *comment;
  227. /** number of tiles in width */
  228. int tw;
  229. /** number of tiles in heigth */
  230. int th;
  231. /** ID number of the tiles present in the codestream */
  232. int *tileno;
  233. /** size of the vector tileno */
  234. int tileno_size;
  235. /** packet header store there for futur use in t2_decode_packet */
  236. unsigned char *ppm_data;
  237. /** pointer remaining on the first byte of the first header if ppm is used */
  238. unsigned char *ppm_data_first;
  239. /** if ppm == 1 --> there was a PPM marker for the present tile */
  240. int ppm;
  241. /** use in case of multiple marker PPM (number of info already store) */
  242. int ppm_store;
  243. /** use in case of multiple marker PPM (case on non-finished previous info) */
  244. int ppm_previous;
  245. /** ppmbug1 */
  246. int ppm_len;
  247. /** tile coding parameters */
  248. opj_tcp_t *tcps;
  249. /** fixed layer */
  250. int *matrice;
  251. /* UniPG>> */
  252. #ifdef USE_JPWL
  253. /** enables writing of EPC in MH, thus activating JPWL */
  254. bool epc_on;
  255. /** enables writing of EPB, in case of activated JPWL */
  256. bool epb_on;
  257. /** enables writing of ESD, in case of activated JPWL */
  258. bool esd_on;
  259. /** enables writing of informative techniques of ESD, in case of activated JPWL */
  260. bool info_on;
  261. /** enables writing of RED, in case of activated JPWL */
  262. bool red_on;
  263. /** error protection method for MH (0,1,16,32,37-128) */
  264. int hprot_MH;
  265. /** tile number of header protection specification (>=0) */
  266. int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  267. /** error protection methods for TPHs (0,1,16,32,37-128) */
  268. int hprot_TPH[JPWL_MAX_NO_TILESPECS];
  269. /** tile number of packet protection specification (>=0) */
  270. int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
  271. /** packet number of packet protection specification (>=0) */
  272. int pprot_packno[JPWL_MAX_NO_PACKSPECS];
  273. /** error protection methods for packets (0,1,16,32,37-128) */
  274. int pprot[JPWL_MAX_NO_PACKSPECS];
  275. /** enables writing of ESD, (0/2/4 bytes) */
  276. int sens_size;
  277. /** sensitivity addressing size (0=auto/2/4 bytes) */
  278. int sens_addr;
  279. /** sensitivity range (0-3) */
  280. int sens_range;
  281. /** sensitivity method for MH (-1,0-7) */
  282. int sens_MH;
  283. /** tile number of sensitivity specification (>=0) */
  284. int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  285. /** sensitivity methods for TPHs (-1,0-7) */
  286. int sens_TPH[JPWL_MAX_NO_TILESPECS];
  287. /** enables JPWL correction at the decoder */
  288. bool correct;
  289. /** expected number of components at the decoder */
  290. int exp_comps;
  291. /** maximum number of tiles at the decoder */
  292. int max_tiles;
  293. #endif /* USE_JPWL */
  294. /* <<UniPG */
  295. } opj_cp_t;
  296. /**
  297. JPEG-2000 codestream reader/writer
  298. */
  299. typedef struct opj_j2k {
  300. /** codec context */
  301. opj_common_ptr cinfo;
  302. /** locate in which part of the codestream the decoder is (main header, tile header, end) */
  303. int state;
  304. /** number of the tile curently concern by coding/decoding */
  305. int curtileno;
  306. /** Tile part number*/
  307. int tp_num;
  308. /** Tilepart number currently coding*/
  309. int cur_tp_num;
  310. /** Total number of tileparts of the current tile*/
  311. int *cur_totnum_tp;
  312. /**
  313. locate the start position of the TLM marker
  314. after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
  315. */
  316. int tlm_start;
  317. /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
  318. /** used in TLMmarker*/
  319. int totnum_tp;
  320. /**
  321. locate the position of the end of the tile in the codestream,
  322. used to detect a truncated codestream (in j2k_read_sod)
  323. */
  324. unsigned char *eot;
  325. /**
  326. locate the start position of the SOT marker of the current coded tile:
  327. after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length.
  328. */
  329. int sot_start;
  330. int sod_start;
  331. /**
  332. as the J2K-file is written in several parts during encoding,
  333. it enables to make the right correction in position return by cio_tell
  334. */
  335. int pos_correction;
  336. /** array used to store the data of each tile */
  337. unsigned char **tile_data;
  338. /** array used to store the length of each tile */
  339. int *tile_len;
  340. /**
  341. decompression only :
  342. store decoding parameters common to all tiles (information like COD, COC in main header)
  343. */
  344. opj_tcp_t *default_tcp;
  345. /** pointer to the encoded / decoded image */
  346. opj_image_t *image;
  347. /** pointer to the coding parameters */
  348. opj_cp_t *cp;
  349. /** helper used to write the index file */
  350. opj_codestream_info_t *cstr_info;
  351. /** pointer to the byte i/o stream */
  352. opj_cio_t *cio;
  353. } opj_j2k_t;
  354. /** @name Exported functions */
  355. /*@{*/
  356. /* ----------------------------------------------------------------------- */
  357. /**
  358. Creates a J2K decompression structure
  359. @param cinfo Codec context info
  360. @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
  361. */
  362. opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
  363. /**
  364. Destroy a J2K decompressor handle
  365. @param j2k J2K decompressor handle to destroy
  366. */
  367. void j2k_destroy_decompress(opj_j2k_t *j2k);
  368. /**
  369. Setup the decoder decoding parameters using user parameters.
  370. Decoding parameters are returned in j2k->cp.
  371. @param j2k J2K decompressor handle
  372. @param parameters decompression parameters
  373. */
  374. void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
  375. /**
  376. Decode an image from a JPEG-2000 codestream
  377. @param j2k J2K decompressor handle
  378. @param cio Input buffer stream
  379. @param cstr_info Codestream information structure if required, NULL otherwise
  380. @return Returns a decoded image if successful, returns NULL otherwise
  381. */
  382. opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
  383. /**
  384. Decode an image form a JPT-stream (JPEG 2000, JPIP)
  385. @param j2k J2K decompressor handle
  386. @param cio Input buffer stream
  387. @param cstr_info Codestream information structure if required, NULL otherwise
  388. @return Returns a decoded image if successful, returns NULL otherwise
  389. */
  390. opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
  391. /**
  392. Creates a J2K compression structure
  393. @param cinfo Codec context info
  394. @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
  395. */
  396. opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
  397. /**
  398. Destroy a J2K compressor handle
  399. @param j2k J2K compressor handle to destroy
  400. */
  401. void j2k_destroy_compress(opj_j2k_t *j2k);
  402. /**
  403. Setup the encoder parameters using the current image and using user parameters.
  404. Coding parameters are returned in j2k->cp.
  405. @param j2k J2K compressor handle
  406. @param parameters compression parameters
  407. @param image input filled image
  408. */
  409. void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
  410. /**
  411. Converts an enum type progression order to string type
  412. */
  413. char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
  414. /**
  415. Encode an image into a JPEG-2000 codestream
  416. @param j2k J2K compressor handle
  417. @param cio Output buffer stream
  418. @param image Image to encode
  419. @param cstr_info Codestream information structure if required, NULL otherwise
  420. @return Returns true if successful, returns false otherwise
  421. */
  422. bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
  423. void j2k_dump_image(FILE *fd, opj_image_t * img);
  424. void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp);
  425. /* ----------------------------------------------------------------------- */
  426. /*@}*/
  427. /*@}*/
  428. #endif /* __J2K_H */