j2k.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2006-2007, Parvatha Elangovan
  15. * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
  16. * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
  17. * Copyright (c) 2012, CS Systemes d'Information, France
  18. *
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. #ifndef OPJ_J2K_H
  43. #define OPJ_J2K_H
  44. /**
  45. @file j2k.h
  46. @brief The JPEG-2000 Codestream Reader/Writer (J2K)
  47. The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
  48. */
  49. /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
  50. /*@{*/
  51. #define J2K_CP_CSTY_PRT 0x01
  52. #define J2K_CP_CSTY_SOP 0x02
  53. #define J2K_CP_CSTY_EPH 0x04
  54. #define J2K_CCP_CSTY_PRT 0x01
  55. #define J2K_CCP_CBLKSTY_LAZY 0x01 /**< Selective arithmetic coding bypass */
  56. #define J2K_CCP_CBLKSTY_RESET 0x02 /**< Reset context probabilities on coding pass boundaries */
  57. #define J2K_CCP_CBLKSTY_TERMALL 0x04 /**< Termination on each coding pass */
  58. #define J2K_CCP_CBLKSTY_VSC 0x08 /**< Vertically stripe causal context */
  59. #define J2K_CCP_CBLKSTY_PTERM 0x10 /**< Predictable termination */
  60. #define J2K_CCP_CBLKSTY_SEGSYM 0x20 /**< Segmentation symbols are used */
  61. #define J2K_CCP_CBLKSTY_HT 0x40 /**< (high throughput) HT codeblocks */
  62. #define J2K_CCP_CBLKSTY_HTMIXED 0x80 /**< MIXED mode HT codeblocks */
  63. #define J2K_CCP_QNTSTY_NOQNT 0
  64. #define J2K_CCP_QNTSTY_SIQNT 1
  65. #define J2K_CCP_QNTSTY_SEQNT 2
  66. /* ----------------------------------------------------------------------- */
  67. #define J2K_MS_SOC 0xff4f /**< SOC marker value */
  68. #define J2K_MS_SOT 0xff90 /**< SOT marker value */
  69. #define J2K_MS_SOD 0xff93 /**< SOD marker value */
  70. #define J2K_MS_EOC 0xffd9 /**< EOC marker value */
  71. #define J2K_MS_CAP 0xff50 /**< CAP marker value */
  72. #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
  73. #define J2K_MS_COD 0xff52 /**< COD marker value */
  74. #define J2K_MS_COC 0xff53 /**< COC marker value */
  75. #define J2K_MS_CPF 0xff59 /**< CPF marker value */
  76. #define J2K_MS_RGN 0xff5e /**< RGN marker value */
  77. #define J2K_MS_QCD 0xff5c /**< QCD marker value */
  78. #define J2K_MS_QCC 0xff5d /**< QCC marker value */
  79. #define J2K_MS_POC 0xff5f /**< POC marker value */
  80. #define J2K_MS_TLM 0xff55 /**< TLM marker value */
  81. #define J2K_MS_PLM 0xff57 /**< PLM marker value */
  82. #define J2K_MS_PLT 0xff58 /**< PLT marker value */
  83. #define J2K_MS_PPM 0xff60 /**< PPM marker value */
  84. #define J2K_MS_PPT 0xff61 /**< PPT marker value */
  85. #define J2K_MS_SOP 0xff91 /**< SOP marker value */
  86. #define J2K_MS_EPH 0xff92 /**< EPH marker value */
  87. #define J2K_MS_CRG 0xff63 /**< CRG marker value */
  88. #define J2K_MS_COM 0xff64 /**< COM marker value */
  89. #define J2K_MS_CBD 0xff78 /**< CBD marker value */
  90. #define J2K_MS_MCC 0xff75 /**< MCC marker value */
  91. #define J2K_MS_MCT 0xff74 /**< MCT marker value */
  92. #define J2K_MS_MCO 0xff77 /**< MCO marker value */
  93. #define J2K_MS_UNK 0 /**< UNKNOWN marker value */
  94. /* UniPG>> */
  95. #ifdef USE_JPWL
  96. #define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */
  97. #define J2K_MS_EPB 0xff66 /**< EPB marker value (Part 11: JPEG 2000 for Wireless) */
  98. #define J2K_MS_ESD 0xff67 /**< ESD marker value (Part 11: JPEG 2000 for Wireless) */
  99. #define J2K_MS_RED 0xff69 /**< RED marker value (Part 11: JPEG 2000 for Wireless) */
  100. #endif /* USE_JPWL */
  101. #ifdef USE_JPSEC
  102. #define J2K_MS_SEC 0xff65 /**< SEC marker value (Part 8: Secure JPEG 2000) */
  103. #define J2K_MS_INSEC 0xff94 /**< INSEC marker value (Part 8: Secure JPEG 2000) */
  104. #endif /* USE_JPSEC */
  105. /* <<UniPG */
  106. #define J2K_MAX_POCS 32 /**< Maximum number of POCs */
  107. #define J2K_TCD_MATRIX_MAX_LAYER_COUNT 10
  108. #define J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT 10
  109. /* ----------------------------------------------------------------------- */
  110. /**
  111. * Values that specify the status of the decoding process when decoding the main header.
  112. * These values may be combined with a | operator.
  113. * */
  114. typedef enum J2K_STATUS {
  115. J2K_STATE_NONE = 0x0000, /**< a SOC marker is expected */
  116. J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
  117. J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
  118. J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */
  119. J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
  120. J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
  121. J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */
  122. J2K_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
  123. J2K_STATE_DATA = 0x0080, /**< a tile header has been successfully read and codestream is expected */
  124. J2K_STATE_EOC = 0x0100, /**< the decoding process has encountered the EOC marker */
  125. J2K_STATE_ERR = 0x8000 /**< the decoding process has encountered an error (FIXME warning V1 = 0x0080)*/
  126. } J2K_STATUS;
  127. /**
  128. * Type of elements storing in the MCT data
  129. */
  130. typedef enum MCT_ELEMENT_TYPE {
  131. MCT_TYPE_INT16 = 0, /** MCT data is stored as signed shorts*/
  132. MCT_TYPE_INT32 = 1, /** MCT data is stored as signed integers*/
  133. MCT_TYPE_FLOAT = 2, /** MCT data is stored as floats*/
  134. MCT_TYPE_DOUBLE = 3 /** MCT data is stored as doubles*/
  135. } J2K_MCT_ELEMENT_TYPE;
  136. /**
  137. * Type of MCT array
  138. */
  139. typedef enum MCT_ARRAY_TYPE {
  140. MCT_TYPE_DEPENDENCY = 0,
  141. MCT_TYPE_DECORRELATION = 1,
  142. MCT_TYPE_OFFSET = 2
  143. } J2K_MCT_ARRAY_TYPE;
  144. /* ----------------------------------------------------------------------- */
  145. /**
  146. T2 encoding mode
  147. */
  148. typedef enum T2_MODE {
  149. THRESH_CALC = 0, /** Function called in Rate allocation process*/
  150. FINAL_PASS = 1 /** Function called in Tier 2 process*/
  151. } J2K_T2_MODE;
  152. /**
  153. * Quantization stepsize
  154. */
  155. typedef struct opj_stepsize {
  156. /** exponent */
  157. OPJ_INT32 expn;
  158. /** mantissa */
  159. OPJ_INT32 mant;
  160. } opj_stepsize_t;
  161. /**
  162. Tile-component coding parameters
  163. */
  164. typedef struct opj_tccp {
  165. /** coding style */
  166. OPJ_UINT32 csty;
  167. /** number of resolutions */
  168. OPJ_UINT32 numresolutions;
  169. /** code-blocks width */
  170. OPJ_UINT32 cblkw;
  171. /** code-blocks height */
  172. OPJ_UINT32 cblkh;
  173. /** code-block coding style */
  174. OPJ_UINT32 cblksty;
  175. /** discrete wavelet transform identifier */
  176. OPJ_UINT32 qmfbid;
  177. /** quantisation style */
  178. OPJ_UINT32 qntsty;
  179. /** stepsizes used for quantization */
  180. opj_stepsize_t stepsizes[OPJ_J2K_MAXBANDS];
  181. /** number of guard bits */
  182. OPJ_UINT32 numgbits;
  183. /** Region Of Interest shift */
  184. OPJ_INT32 roishift;
  185. /** precinct width */
  186. OPJ_UINT32 prcw[OPJ_J2K_MAXRLVLS];
  187. /** precinct height */
  188. OPJ_UINT32 prch[OPJ_J2K_MAXRLVLS];
  189. /** the dc_level_shift **/
  190. OPJ_INT32 m_dc_level_shift;
  191. }
  192. opj_tccp_t;
  193. /**
  194. * FIXME DOC
  195. */
  196. typedef struct opj_mct_data {
  197. J2K_MCT_ELEMENT_TYPE m_element_type;
  198. J2K_MCT_ARRAY_TYPE m_array_type;
  199. OPJ_UINT32 m_index;
  200. OPJ_BYTE * m_data;
  201. OPJ_UINT32 m_data_size;
  202. }
  203. opj_mct_data_t;
  204. /**
  205. * FIXME DOC
  206. */
  207. typedef struct opj_simple_mcc_decorrelation_data {
  208. OPJ_UINT32 m_index;
  209. OPJ_UINT32 m_nb_comps;
  210. opj_mct_data_t * m_decorrelation_array;
  211. opj_mct_data_t * m_offset_array;
  212. OPJ_BITFIELD m_is_irreversible : 1;
  213. }
  214. opj_simple_mcc_decorrelation_data_t;
  215. typedef struct opj_ppx_struct {
  216. OPJ_BYTE* m_data; /* m_data == NULL => Zppx not read yet */
  217. OPJ_UINT32 m_data_size;
  218. } opj_ppx;
  219. /**
  220. Tile coding parameters :
  221. this structure is used to store coding/decoding parameters common to all
  222. tiles (information like COD, COC in main header)
  223. */
  224. typedef struct opj_tcp {
  225. /** coding style */
  226. OPJ_UINT32 csty;
  227. /** progression order */
  228. OPJ_PROG_ORDER prg;
  229. /** number of layers */
  230. OPJ_UINT32 numlayers;
  231. OPJ_UINT32 num_layers_to_decode;
  232. /** multi-component transform identifier */
  233. OPJ_UINT32 mct;
  234. /** rates of layers */
  235. OPJ_FLOAT32 rates[100];
  236. /** number of progression order changes */
  237. OPJ_UINT32 numpocs;
  238. /** progression order changes */
  239. opj_poc_t pocs[J2K_MAX_POCS];
  240. /** number of ppt markers (reserved size) */
  241. OPJ_UINT32 ppt_markers_count;
  242. /** ppt markers data (table indexed by Zppt) */
  243. opj_ppx* ppt_markers;
  244. /** packet header store there for future use in t2_decode_packet */
  245. OPJ_BYTE *ppt_data;
  246. /** used to keep a track of the allocated memory */
  247. OPJ_BYTE *ppt_buffer;
  248. /** Number of bytes stored inside ppt_data*/
  249. OPJ_UINT32 ppt_data_size;
  250. /** size of ppt_data*/
  251. OPJ_UINT32 ppt_len;
  252. /** PSNR values */
  253. OPJ_FLOAT32 distoratio[100];
  254. /** tile-component coding parameters */
  255. opj_tccp_t *tccps;
  256. /** current tile part number or -1 if first time into this tile */
  257. OPJ_INT32 m_current_tile_part_number;
  258. /** number of tile parts for the tile. */
  259. OPJ_UINT32 m_nb_tile_parts;
  260. /** data for the tile */
  261. OPJ_BYTE * m_data;
  262. /** size of data */
  263. OPJ_UINT32 m_data_size;
  264. /** encoding norms */
  265. OPJ_FLOAT64 * mct_norms;
  266. /** the mct decoding matrix */
  267. OPJ_FLOAT32 * m_mct_decoding_matrix;
  268. /** the mct coding matrix */
  269. OPJ_FLOAT32 * m_mct_coding_matrix;
  270. /** mct records */
  271. opj_mct_data_t * m_mct_records;
  272. /** the number of mct records. */
  273. OPJ_UINT32 m_nb_mct_records;
  274. /** the max number of mct records. */
  275. OPJ_UINT32 m_nb_max_mct_records;
  276. /** mcc records */
  277. opj_simple_mcc_decorrelation_data_t * m_mcc_records;
  278. /** the number of mct records. */
  279. OPJ_UINT32 m_nb_mcc_records;
  280. /** the max number of mct records. */
  281. OPJ_UINT32 m_nb_max_mcc_records;
  282. /***** FLAGS *******/
  283. /** If cod == 1 --> there was a COD marker for the present tile */
  284. OPJ_BITFIELD cod : 1;
  285. /** If ppt == 1 --> there was a PPT marker for the present tile */
  286. OPJ_BITFIELD ppt : 1;
  287. /** indicates if a POC marker has been used O:NO, 1:YES */
  288. OPJ_BITFIELD POC : 1;
  289. } opj_tcp_t;
  290. /**
  291. Rate allocation strategy
  292. */
  293. typedef enum {
  294. RATE_DISTORTION_RATIO = 0, /** allocation by rate/distortion */
  295. FIXED_DISTORTION_RATIO = 1, /** allocation by fixed distortion ratio (PSNR) (fixed quality) */
  296. FIXED_LAYER = 2, /** allocation by fixed layer (number of passes per layer / resolution / subband) */
  297. } J2K_QUALITY_LAYER_ALLOCATION_STRATEGY;
  298. typedef struct opj_encoding_param {
  299. /** Maximum rate for each component. If == 0, component size limitation is not considered */
  300. OPJ_UINT32 m_max_comp_size;
  301. /** Position of tile part flag in progression order*/
  302. OPJ_INT32 m_tp_pos;
  303. /** fixed layer */
  304. OPJ_INT32 *m_matrice;
  305. /** Flag determining tile part generation*/
  306. OPJ_BYTE m_tp_flag;
  307. /** Quality layer allocation strategy */
  308. J2K_QUALITY_LAYER_ALLOCATION_STRATEGY m_quality_layer_alloc_strategy;
  309. /** Enabling Tile part generation*/
  310. OPJ_BITFIELD m_tp_on : 1;
  311. }
  312. opj_encoding_param_t;
  313. typedef struct opj_decoding_param {
  314. /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
  315. OPJ_UINT32 m_reduce;
  316. /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
  317. OPJ_UINT32 m_layer;
  318. }
  319. opj_decoding_param_t;
  320. /**
  321. * Coding parameters
  322. */
  323. typedef struct opj_cp {
  324. /** Size of the image in bits*/
  325. /*int img_size;*/
  326. /** Rsiz*/
  327. OPJ_UINT16 rsiz;
  328. /** XTOsiz */
  329. OPJ_UINT32 tx0; /* MSD see norm */
  330. /** YTOsiz */
  331. OPJ_UINT32 ty0; /* MSD see norm */
  332. /** XTsiz */
  333. OPJ_UINT32 tdx;
  334. /** YTsiz */
  335. OPJ_UINT32 tdy;
  336. /** comment */
  337. OPJ_CHAR *comment;
  338. /** number of tiles in width */
  339. OPJ_UINT32 tw;
  340. /** number of tiles in height */
  341. OPJ_UINT32 th;
  342. /** number of ppm markers (reserved size) */
  343. OPJ_UINT32 ppm_markers_count;
  344. /** ppm markers data (table indexed by Zppm) */
  345. opj_ppx* ppm_markers;
  346. /** packet header store there for future use in t2_decode_packet */
  347. OPJ_BYTE *ppm_data;
  348. /** size of the ppm_data*/
  349. OPJ_UINT32 ppm_len;
  350. /** size of the ppm_data*/
  351. OPJ_UINT32 ppm_data_read;
  352. OPJ_BYTE *ppm_data_current;
  353. /** packet header storage original buffer */
  354. OPJ_BYTE *ppm_buffer;
  355. /** pointer remaining on the first byte of the first header if ppm is used */
  356. OPJ_BYTE *ppm_data_first;
  357. /** Number of bytes actually stored inside the ppm_data */
  358. OPJ_UINT32 ppm_data_size;
  359. /** use in case of multiple marker PPM (number of info already store) */
  360. OPJ_INT32 ppm_store;
  361. /** use in case of multiple marker PPM (case on non-finished previous info) */
  362. OPJ_INT32 ppm_previous;
  363. /** tile coding parameters */
  364. opj_tcp_t *tcps;
  365. union {
  366. opj_decoding_param_t m_dec;
  367. opj_encoding_param_t m_enc;
  368. }
  369. m_specific_param;
  370. /** OPJ_TRUE if entire bit stream must be decoded, OPJ_FALSE if partial bitstream decoding allowed */
  371. OPJ_BOOL strict;
  372. /* UniPG>> */
  373. #ifdef USE_JPWL
  374. /** enables writing of EPC in MH, thus activating JPWL */
  375. OPJ_BOOL epc_on;
  376. /** enables writing of EPB, in case of activated JPWL */
  377. OPJ_BOOL epb_on;
  378. /** enables writing of ESD, in case of activated JPWL */
  379. OPJ_BOOL esd_on;
  380. /** enables writing of informative techniques of ESD, in case of activated JPWL */
  381. OPJ_BOOL info_on;
  382. /** enables writing of RED, in case of activated JPWL */
  383. OPJ_BOOL red_on;
  384. /** error protection method for MH (0,1,16,32,37-128) */
  385. int hprot_MH;
  386. /** tile number of header protection specification (>=0) */
  387. int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  388. /** error protection methods for TPHs (0,1,16,32,37-128) */
  389. int hprot_TPH[JPWL_MAX_NO_TILESPECS];
  390. /** tile number of packet protection specification (>=0) */
  391. int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
  392. /** packet number of packet protection specification (>=0) */
  393. int pprot_packno[JPWL_MAX_NO_PACKSPECS];
  394. /** error protection methods for packets (0,1,16,32,37-128) */
  395. int pprot[JPWL_MAX_NO_PACKSPECS];
  396. /** enables writing of ESD, (0/2/4 bytes) */
  397. int sens_size;
  398. /** sensitivity addressing size (0=auto/2/4 bytes) */
  399. int sens_addr;
  400. /** sensitivity range (0-3) */
  401. int sens_range;
  402. /** sensitivity method for MH (-1,0-7) */
  403. int sens_MH;
  404. /** tile number of sensitivity specification (>=0) */
  405. int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  406. /** sensitivity methods for TPHs (-1,0-7) */
  407. int sens_TPH[JPWL_MAX_NO_TILESPECS];
  408. /** enables JPWL correction at the decoder */
  409. OPJ_BOOL correct;
  410. /** expected number of components at the decoder */
  411. int exp_comps;
  412. /** maximum number of tiles at the decoder */
  413. OPJ_UINT32 max_tiles;
  414. #endif /* USE_JPWL */
  415. /******** FLAGS *********/
  416. /** if ppm == 1 --> there was a PPM marker*/
  417. OPJ_BITFIELD ppm : 1;
  418. /** tells if the parameter is a coding or decoding one */
  419. OPJ_BITFIELD m_is_decoder : 1;
  420. /** whether different bit depth or sign per component is allowed. Decoder only for ow */
  421. OPJ_BITFIELD allow_different_bit_depth_sign : 1;
  422. /* <<UniPG */
  423. } opj_cp_t;
  424. /** Entry of a TLM marker segment */
  425. typedef struct opj_j2k_tlm_tile_part_info {
  426. /** Tile index of the tile part. Ttlmi field */
  427. OPJ_UINT16 m_tile_index;
  428. /** Length in bytes, from the beginning of the SOT marker to the end of
  429. * the bit stream data for that tile-part. Ptlmi field */
  430. OPJ_UINT32 m_length;
  431. } opj_j2k_tlm_tile_part_info_t;
  432. /** Information got from the concatenation of TLM marker semgnets. */
  433. typedef struct opj_j2k_tlm_info {
  434. /** Number of entries in m_tile_part_infos. */
  435. OPJ_UINT32 m_entries_count;
  436. /** Array of m_entries_count values. */
  437. opj_j2k_tlm_tile_part_info_t* m_tile_part_infos;
  438. OPJ_BOOL m_is_invalid;
  439. } opj_j2k_tlm_info_t;
  440. typedef struct opj_j2k_dec {
  441. /** locate in which part of the codestream the decoder is (main header, tile header, end) */
  442. OPJ_UINT32 m_state;
  443. /**
  444. * store decoding parameters common to all tiles (information like COD, COC in main header)
  445. */
  446. opj_tcp_t *m_default_tcp;
  447. OPJ_BYTE *m_header_data;
  448. OPJ_UINT32 m_header_data_size;
  449. /** to tell the tile part length */
  450. OPJ_UINT32 m_sot_length;
  451. /** Only tiles index in the correct range will be decoded.*/
  452. OPJ_UINT32 m_start_tile_x;
  453. OPJ_UINT32 m_start_tile_y;
  454. OPJ_UINT32 m_end_tile_x;
  455. OPJ_UINT32 m_end_tile_y;
  456. /** Index of the tile to decode (used in get_tile) */
  457. OPJ_INT32 m_tile_ind_to_dec;
  458. /** Position of the last SOT marker read */
  459. OPJ_OFF_T m_last_sot_read_pos;
  460. /**
  461. * Indicate that the current tile-part is assume as the last tile part of the codestream.
  462. * It is useful in the case of PSot is equal to zero. The sot length will be compute in the
  463. * SOD reader function. FIXME NOT USED for the moment
  464. */
  465. OPJ_BOOL m_last_tile_part;
  466. OPJ_UINT32 m_numcomps_to_decode;
  467. OPJ_UINT32 *m_comps_indices_to_decode;
  468. opj_j2k_tlm_info_t m_tlm;
  469. /** Below if used when there's TLM information available and we use
  470. * opj_set_decoded_area() to a subset of all tiles.
  471. */
  472. /* Current index in m_intersecting_tile_parts_offset[] to seek to */
  473. OPJ_UINT32 m_idx_intersecting_tile_parts;
  474. /* Number of elements of m_intersecting_tile_parts_offset[] */
  475. OPJ_UINT32 m_num_intersecting_tile_parts;
  476. /* Start offset of contributing tile parts */
  477. OPJ_OFF_T* m_intersecting_tile_parts_offset;
  478. /** to tell that a tile can be decoded. */
  479. OPJ_BITFIELD m_can_decode : 1;
  480. OPJ_BITFIELD m_discard_tiles : 1;
  481. OPJ_BITFIELD m_skip_data : 1;
  482. /** TNsot correction : see issue 254 **/
  483. OPJ_BITFIELD m_nb_tile_parts_correction_checked : 1;
  484. OPJ_BITFIELD m_nb_tile_parts_correction : 1;
  485. } opj_j2k_dec_t;
  486. typedef struct opj_j2k_enc {
  487. /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
  488. OPJ_UINT32 m_current_poc_tile_part_number; /* tp_num */
  489. /** Tile part number currently coding, taking into account POC. m_current_tile_part_number holds the total number of tile parts while encoding the last tile part.*/
  490. OPJ_UINT32 m_current_tile_part_number; /*cur_tp_num */
  491. /* whether to generate TLM markers */
  492. OPJ_BOOL m_TLM;
  493. /* whether the Ttlmi field in a TLM marker is a byte (otherwise a uint16) */
  494. OPJ_BOOL m_Ttlmi_is_byte;
  495. /**
  496. locate the start position of the TLM marker
  497. after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
  498. */
  499. OPJ_OFF_T m_tlm_start;
  500. /**
  501. * Stores the sizes of the tlm.
  502. */
  503. OPJ_BYTE * m_tlm_sot_offsets_buffer;
  504. /**
  505. * The current offset of the tlm buffer.
  506. */
  507. OPJ_BYTE * m_tlm_sot_offsets_current;
  508. /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
  509. /** used in TLMmarker*/
  510. OPJ_UINT32 m_total_tile_parts; /* totnum_tp */
  511. /* encoded data for a tile */
  512. OPJ_BYTE * m_encoded_tile_data;
  513. /* size of the encoded_data */
  514. OPJ_UINT32 m_encoded_tile_size;
  515. /* encoded data for a tile */
  516. OPJ_BYTE * m_header_tile_data;
  517. /* size of the encoded_data */
  518. OPJ_UINT32 m_header_tile_data_size;
  519. /* whether to generate PLT markers */
  520. OPJ_BOOL m_PLT;
  521. /* reserved bytes in m_encoded_tile_size for PLT markers */
  522. OPJ_UINT32 m_reserved_bytes_for_PLT;
  523. /** Number of components */
  524. OPJ_UINT32 m_nb_comps;
  525. } opj_j2k_enc_t;
  526. struct opj_tcd;
  527. /**
  528. JPEG-2000 codestream reader/writer
  529. */
  530. typedef struct opj_j2k {
  531. /* J2K codestream is decoded*/
  532. OPJ_BOOL m_is_decoder;
  533. /* FIXME DOC*/
  534. union {
  535. opj_j2k_dec_t m_decoder;
  536. opj_j2k_enc_t m_encoder;
  537. }
  538. m_specific_param;
  539. /** pointer to the internal/private encoded / decoded image */
  540. opj_image_t* m_private_image;
  541. /* pointer to the output image (decoded)*/
  542. opj_image_t* m_output_image;
  543. /** Coding parameters */
  544. opj_cp_t m_cp;
  545. /** the list of procedures to exec **/
  546. opj_procedure_list_t * m_procedure_list;
  547. /** the list of validation procedures to follow to make sure the code is valid **/
  548. opj_procedure_list_t * m_validation_list;
  549. /** helper used to write the index file */
  550. opj_codestream_index_t *cstr_index;
  551. /** number of the tile currently concern by coding/decoding */
  552. OPJ_UINT32 m_current_tile_number;
  553. /** the current tile coder/decoder **/
  554. struct opj_tcd * m_tcd;
  555. /** Thread pool */
  556. opj_thread_pool_t* m_tp;
  557. /** Image width coming from JP2 IHDR box. 0 from a pure codestream */
  558. OPJ_UINT32 ihdr_w;
  559. /** Image height coming from JP2 IHDR box. 0 from a pure codestream */
  560. OPJ_UINT32 ihdr_h;
  561. /** Set to 1 by the decoder initialization if OPJ_DPARAMETERS_DUMP_FLAG is set */
  562. unsigned int dump_state;
  563. }
  564. opj_j2k_t;
  565. /** @name Exported functions */
  566. /*@{*/
  567. /* ----------------------------------------------------------------------- */
  568. /**
  569. Setup the decoder decoding parameters using user parameters.
  570. Decoding parameters are returned in j2k->cp.
  571. @param j2k J2K decompressor handle
  572. @param parameters decompression parameters
  573. */
  574. void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
  575. void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict);
  576. OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads);
  577. /**
  578. * Creates a J2K compression structure
  579. *
  580. * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
  581. */
  582. opj_j2k_t* opj_j2k_create_compress(void);
  583. OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
  584. opj_cparameters_t *parameters,
  585. opj_image_t *image,
  586. opj_event_mgr_t * p_manager);
  587. /**
  588. Converts an enum type progression order to string type
  589. */
  590. const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
  591. /* ----------------------------------------------------------------------- */
  592. /*@}*/
  593. /*@}*/
  594. /**
  595. * Ends the decompression procedures and possibiliy add data to be read after the
  596. * codestream.
  597. */
  598. OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k,
  599. opj_stream_private_t *p_stream,
  600. opj_event_mgr_t * p_manager);
  601. /**
  602. * Reads a jpeg2000 codestream header structure.
  603. *
  604. * @param p_stream the stream to read data from.
  605. * @param p_j2k the jpeg2000 codec.
  606. * @param p_image FIXME DOC
  607. * @param p_manager the user event manager.
  608. *
  609. * @return true if the box is valid.
  610. */
  611. OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
  612. opj_j2k_t* p_j2k,
  613. opj_image_t** p_image,
  614. opj_event_mgr_t* p_manager);
  615. /**
  616. * Destroys a jpeg2000 codec.
  617. *
  618. * @param p_j2k the jpeg20000 structure to destroy.
  619. */
  620. void opj_j2k_destroy(opj_j2k_t *p_j2k);
  621. /**
  622. * Destroys a codestream index structure.
  623. *
  624. * @param p_cstr_ind the codestream index parameter to destroy.
  625. */
  626. void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind);
  627. /**
  628. * Decode tile data.
  629. * @param p_j2k the jpeg2000 codec.
  630. * @param p_tile_index
  631. * @param p_data FIXME DOC
  632. * @param p_data_size FIXME DOC
  633. * @param p_stream the stream to write data to.
  634. * @param p_manager the user event manager.
  635. */
  636. OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
  637. OPJ_UINT32 p_tile_index,
  638. OPJ_BYTE * p_data,
  639. OPJ_UINT32 p_data_size,
  640. opj_stream_private_t *p_stream,
  641. opj_event_mgr_t * p_manager);
  642. /**
  643. * Reads a tile header.
  644. * @param p_j2k the jpeg2000 codec.
  645. * @param p_tile_index FIXME DOC
  646. * @param p_data_size FIXME DOC
  647. * @param p_tile_x0 FIXME DOC
  648. * @param p_tile_y0 FIXME DOC
  649. * @param p_tile_x1 FIXME DOC
  650. * @param p_tile_y1 FIXME DOC
  651. * @param p_nb_comps FIXME DOC
  652. * @param p_go_on FIXME DOC
  653. * @param p_stream the stream to write data to.
  654. * @param p_manager the user event manager.
  655. */
  656. OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
  657. OPJ_UINT32 * p_tile_index,
  658. OPJ_UINT32 * p_data_size,
  659. OPJ_INT32 * p_tile_x0,
  660. OPJ_INT32 * p_tile_y0,
  661. OPJ_INT32 * p_tile_x1,
  662. OPJ_INT32 * p_tile_y1,
  663. OPJ_UINT32 * p_nb_comps,
  664. OPJ_BOOL * p_go_on,
  665. opj_stream_private_t *p_stream,
  666. opj_event_mgr_t * p_manager);
  667. /** Sets the indices of the components to decode.
  668. *
  669. * @param p_j2k the jpeg2000 codec.
  670. * @param numcomps Number of components to decode.
  671. * @param comps_indices Array of num_compts indices (numbering starting at 0)
  672. * corresponding to the components to decode.
  673. * @param p_manager Event manager
  674. *
  675. * @return OPJ_TRUE in case of success.
  676. */
  677. OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
  678. OPJ_UINT32 numcomps,
  679. const OPJ_UINT32* comps_indices,
  680. opj_event_mgr_t * p_manager);
  681. /**
  682. * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  683. *
  684. * @param p_j2k the jpeg2000 codec.
  685. * @param p_image FIXME DOC
  686. * @param p_start_x the left position of the rectangle to decode (in image coordinates).
  687. * @param p_start_y the up position of the rectangle to decode (in image coordinates).
  688. * @param p_end_x the right position of the rectangle to decode (in image coordinates).
  689. * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
  690. * @param p_manager the user event manager
  691. *
  692. * @return true if the area could be set.
  693. */
  694. OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
  695. opj_image_t* p_image,
  696. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  697. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  698. opj_event_mgr_t * p_manager);
  699. /**
  700. * Creates a J2K decompression structure.
  701. *
  702. * @return a handle to a J2K decompressor if successful, NULL otherwise.
  703. */
  704. opj_j2k_t* opj_j2k_create_decompress(void);
  705. /**
  706. * Dump some elements from the J2K decompression structure .
  707. *
  708. *@param p_j2k the jpeg2000 codec.
  709. *@param flag flag to describe what elements are dump.
  710. *@param out_stream output stream where dump the elements.
  711. *
  712. */
  713. void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream);
  714. /**
  715. * Dump an image header structure.
  716. *
  717. *@param image the image header to dump.
  718. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  719. *@param out_stream output stream where dump the elements.
  720. */
  721. void j2k_dump_image_header(opj_image_t* image, OPJ_BOOL dev_dump_flag,
  722. FILE* out_stream);
  723. /**
  724. * Dump a component image header structure.
  725. *
  726. *@param comp the component image header to dump.
  727. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  728. *@param out_stream output stream where dump the elements.
  729. */
  730. void j2k_dump_image_comp_header(opj_image_comp_t* comp, OPJ_BOOL dev_dump_flag,
  731. FILE* out_stream);
  732. /**
  733. * Get the codestream info from a JPEG2000 codec.
  734. *
  735. *@param p_j2k the component image header to dump.
  736. *
  737. *@return the codestream information extract from the jpg2000 codec
  738. */
  739. opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k);
  740. /**
  741. * Get the codestream index from a JPEG2000 codec.
  742. *
  743. *@param p_j2k the component image header to dump.
  744. *
  745. *@return the codestream index extract from the jpg2000 codec
  746. */
  747. opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k);
  748. /**
  749. * Decode an image from a JPEG-2000 codestream
  750. * @param j2k J2K decompressor handle
  751. * @param p_stream FIXME DOC
  752. * @param p_image FIXME DOC
  753. * @param p_manager FIXME DOC
  754. * @return FIXME DOC
  755. */
  756. OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k,
  757. opj_stream_private_t *p_stream,
  758. opj_image_t *p_image,
  759. opj_event_mgr_t *p_manager);
  760. OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
  761. opj_stream_private_t *p_stream,
  762. opj_image_t* p_image,
  763. opj_event_mgr_t * p_manager,
  764. OPJ_UINT32 tile_index);
  765. OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
  766. OPJ_UINT32 res_factor,
  767. opj_event_mgr_t * p_manager);
  768. /**
  769. * Specify extra options for the encoder.
  770. *
  771. * @param p_j2k the jpeg2000 codec.
  772. * @param p_options options
  773. * @param p_manager the user event manager
  774. *
  775. * @see opj_encoder_set_extra_options() for more details.
  776. */
  777. OPJ_BOOL opj_j2k_encoder_set_extra_options(
  778. opj_j2k_t *p_j2k,
  779. const char* const* p_options,
  780. opj_event_mgr_t * p_manager);
  781. /**
  782. * Writes a tile.
  783. * @param p_j2k the jpeg2000 codec.
  784. * @param p_tile_index FIXME DOC
  785. * @param p_data FIXME DOC
  786. * @param p_data_size FIXME DOC
  787. * @param p_stream the stream to write data to.
  788. * @param p_manager the user event manager.
  789. */
  790. OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
  791. OPJ_UINT32 p_tile_index,
  792. OPJ_BYTE * p_data,
  793. OPJ_UINT32 p_data_size,
  794. opj_stream_private_t *p_stream,
  795. opj_event_mgr_t * p_manager);
  796. /**
  797. * Encodes an image into a JPEG-2000 codestream
  798. */
  799. OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
  800. opj_stream_private_t *cio,
  801. opj_event_mgr_t * p_manager);
  802. /**
  803. * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
  804. *
  805. * @param p_j2k the jpeg2000 codec.
  806. * @param p_stream the stream object.
  807. * @param p_image FIXME DOC
  808. * @param p_manager the user event manager.
  809. *
  810. * @return true if the codec is valid.
  811. */
  812. OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
  813. opj_stream_private_t *p_stream,
  814. opj_image_t * p_image,
  815. opj_event_mgr_t * p_manager);
  816. /**
  817. * Ends the compression procedures and possibiliy add data to be read after the
  818. * codestream.
  819. */
  820. OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
  821. opj_stream_private_t *cio,
  822. opj_event_mgr_t * p_manager);
  823. OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image);
  824. #endif /* OPJ_J2K_H */