j2k.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. /* ----------------------------------------------------------------------- */
  108. /**
  109. * Values that specify the status of the decoding process when decoding the main header.
  110. * These values may be combined with a | operator.
  111. * */
  112. typedef enum J2K_STATUS {
  113. J2K_STATE_NONE = 0x0000, /**< a SOC marker is expected */
  114. J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
  115. J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
  116. J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */
  117. J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
  118. J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
  119. J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */
  120. J2K_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */
  121. J2K_STATE_DATA = 0x0080, /**< a tile header has been successfully read and codestream is expected */
  122. J2K_STATE_EOC = 0x0100, /**< the decoding process has encountered the EOC marker */
  123. J2K_STATE_ERR = 0x8000 /**< the decoding process has encountered an error (FIXME warning V1 = 0x0080)*/
  124. } J2K_STATUS;
  125. /**
  126. * Type of elements storing in the MCT data
  127. */
  128. typedef enum MCT_ELEMENT_TYPE {
  129. MCT_TYPE_INT16 = 0, /** MCT data is stored as signed shorts*/
  130. MCT_TYPE_INT32 = 1, /** MCT data is stored as signed integers*/
  131. MCT_TYPE_FLOAT = 2, /** MCT data is stored as floats*/
  132. MCT_TYPE_DOUBLE = 3 /** MCT data is stored as doubles*/
  133. } J2K_MCT_ELEMENT_TYPE;
  134. /**
  135. * Type of MCT array
  136. */
  137. typedef enum MCT_ARRAY_TYPE {
  138. MCT_TYPE_DEPENDENCY = 0,
  139. MCT_TYPE_DECORRELATION = 1,
  140. MCT_TYPE_OFFSET = 2
  141. } J2K_MCT_ARRAY_TYPE;
  142. /* ----------------------------------------------------------------------- */
  143. /**
  144. T2 encoding mode
  145. */
  146. typedef enum T2_MODE {
  147. THRESH_CALC = 0, /** Function called in Rate allocation process*/
  148. FINAL_PASS = 1 /** Function called in Tier 2 process*/
  149. } J2K_T2_MODE;
  150. /**
  151. * Quantization stepsize
  152. */
  153. typedef struct opj_stepsize {
  154. /** exponent */
  155. OPJ_INT32 expn;
  156. /** mantissa */
  157. OPJ_INT32 mant;
  158. } opj_stepsize_t;
  159. /**
  160. Tile-component coding parameters
  161. */
  162. typedef struct opj_tccp {
  163. /** coding style */
  164. OPJ_UINT32 csty;
  165. /** number of resolutions */
  166. OPJ_UINT32 numresolutions;
  167. /** code-blocks width */
  168. OPJ_UINT32 cblkw;
  169. /** code-blocks height */
  170. OPJ_UINT32 cblkh;
  171. /** code-block coding style */
  172. OPJ_UINT32 cblksty;
  173. /** discrete wavelet transform identifier */
  174. OPJ_UINT32 qmfbid;
  175. /** quantisation style */
  176. OPJ_UINT32 qntsty;
  177. /** stepsizes used for quantization */
  178. opj_stepsize_t stepsizes[OPJ_J2K_MAXBANDS];
  179. /** number of guard bits */
  180. OPJ_UINT32 numgbits;
  181. /** Region Of Interest shift */
  182. OPJ_INT32 roishift;
  183. /** precinct width */
  184. OPJ_UINT32 prcw[OPJ_J2K_MAXRLVLS];
  185. /** precinct height */
  186. OPJ_UINT32 prch[OPJ_J2K_MAXRLVLS];
  187. /** the dc_level_shift **/
  188. OPJ_INT32 m_dc_level_shift;
  189. }
  190. opj_tccp_t;
  191. /**
  192. * FIXME DOC
  193. */
  194. typedef struct opj_mct_data {
  195. J2K_MCT_ELEMENT_TYPE m_element_type;
  196. J2K_MCT_ARRAY_TYPE m_array_type;
  197. OPJ_UINT32 m_index;
  198. OPJ_BYTE * m_data;
  199. OPJ_UINT32 m_data_size;
  200. }
  201. opj_mct_data_t;
  202. /**
  203. * FIXME DOC
  204. */
  205. typedef struct opj_simple_mcc_decorrelation_data {
  206. OPJ_UINT32 m_index;
  207. OPJ_UINT32 m_nb_comps;
  208. opj_mct_data_t * m_decorrelation_array;
  209. opj_mct_data_t * m_offset_array;
  210. OPJ_BITFIELD m_is_irreversible : 1;
  211. }
  212. opj_simple_mcc_decorrelation_data_t;
  213. typedef struct opj_ppx_struct {
  214. OPJ_BYTE* m_data; /* m_data == NULL => Zppx not read yet */
  215. OPJ_UINT32 m_data_size;
  216. } opj_ppx;
  217. /**
  218. Tile coding parameters :
  219. this structure is used to store coding/decoding parameters common to all
  220. tiles (information like COD, COC in main header)
  221. */
  222. typedef struct opj_tcp {
  223. /** coding style */
  224. OPJ_UINT32 csty;
  225. /** progression order */
  226. OPJ_PROG_ORDER prg;
  227. /** number of layers */
  228. OPJ_UINT32 numlayers;
  229. OPJ_UINT32 num_layers_to_decode;
  230. /** multi-component transform identifier */
  231. OPJ_UINT32 mct;
  232. /** rates of layers */
  233. OPJ_FLOAT32 rates[100];
  234. /** number of progression order changes */
  235. OPJ_UINT32 numpocs;
  236. /** progression order changes */
  237. opj_poc_t pocs[J2K_MAX_POCS];
  238. /** number of ppt markers (reserved size) */
  239. OPJ_UINT32 ppt_markers_count;
  240. /** ppt markers data (table indexed by Zppt) */
  241. opj_ppx* ppt_markers;
  242. /** packet header store there for future use in t2_decode_packet */
  243. OPJ_BYTE *ppt_data;
  244. /** used to keep a track of the allocated memory */
  245. OPJ_BYTE *ppt_buffer;
  246. /** Number of bytes stored inside ppt_data*/
  247. OPJ_UINT32 ppt_data_size;
  248. /** size of ppt_data*/
  249. OPJ_UINT32 ppt_len;
  250. /** add fixed_quality */
  251. OPJ_FLOAT32 distoratio[100];
  252. /** tile-component coding parameters */
  253. opj_tccp_t *tccps;
  254. /** current tile part number or -1 if first time into this tile */
  255. OPJ_INT32 m_current_tile_part_number;
  256. /** number of tile parts for the tile. */
  257. OPJ_UINT32 m_nb_tile_parts;
  258. /** data for the tile */
  259. OPJ_BYTE * m_data;
  260. /** size of data */
  261. OPJ_UINT32 m_data_size;
  262. /** encoding norms */
  263. OPJ_FLOAT64 * mct_norms;
  264. /** the mct decoding matrix */
  265. OPJ_FLOAT32 * m_mct_decoding_matrix;
  266. /** the mct coding matrix */
  267. OPJ_FLOAT32 * m_mct_coding_matrix;
  268. /** mct records */
  269. opj_mct_data_t * m_mct_records;
  270. /** the number of mct records. */
  271. OPJ_UINT32 m_nb_mct_records;
  272. /** the max number of mct records. */
  273. OPJ_UINT32 m_nb_max_mct_records;
  274. /** mcc records */
  275. opj_simple_mcc_decorrelation_data_t * m_mcc_records;
  276. /** the number of mct records. */
  277. OPJ_UINT32 m_nb_mcc_records;
  278. /** the max number of mct records. */
  279. OPJ_UINT32 m_nb_max_mcc_records;
  280. /***** FLAGS *******/
  281. /** If cod == 1 --> there was a COD marker for the present tile */
  282. OPJ_BITFIELD cod : 1;
  283. /** If ppt == 1 --> there was a PPT marker for the present tile */
  284. OPJ_BITFIELD ppt : 1;
  285. /** indicates if a POC marker has been used O:NO, 1:YES */
  286. OPJ_BITFIELD POC : 1;
  287. } opj_tcp_t;
  288. typedef struct opj_encoding_param {
  289. /** Maximum rate for each component. If == 0, component size limitation is not considered */
  290. OPJ_UINT32 m_max_comp_size;
  291. /** Position of tile part flag in progression order*/
  292. OPJ_INT32 m_tp_pos;
  293. /** fixed layer */
  294. OPJ_INT32 *m_matrice;
  295. /** Flag determining tile part generation*/
  296. OPJ_BYTE m_tp_flag;
  297. /** allocation by rate/distortion */
  298. OPJ_BITFIELD m_disto_alloc : 1;
  299. /** allocation by fixed layer */
  300. OPJ_BITFIELD m_fixed_alloc : 1;
  301. /** add fixed_quality */
  302. OPJ_BITFIELD m_fixed_quality : 1;
  303. /** Enabling Tile part generation*/
  304. OPJ_BITFIELD m_tp_on : 1;
  305. }
  306. opj_encoding_param_t;
  307. typedef struct opj_decoding_param {
  308. /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
  309. OPJ_UINT32 m_reduce;
  310. /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
  311. OPJ_UINT32 m_layer;
  312. }
  313. opj_decoding_param_t;
  314. /**
  315. * Coding parameters
  316. */
  317. typedef struct opj_cp {
  318. /** Size of the image in bits*/
  319. /*int img_size;*/
  320. /** Rsiz*/
  321. OPJ_UINT16 rsiz;
  322. /** XTOsiz */
  323. OPJ_UINT32 tx0; /* MSD see norm */
  324. /** YTOsiz */
  325. OPJ_UINT32 ty0; /* MSD see norm */
  326. /** XTsiz */
  327. OPJ_UINT32 tdx;
  328. /** YTsiz */
  329. OPJ_UINT32 tdy;
  330. /** comment */
  331. OPJ_CHAR *comment;
  332. /** number of tiles in width */
  333. OPJ_UINT32 tw;
  334. /** number of tiles in height */
  335. OPJ_UINT32 th;
  336. /** number of ppm markers (reserved size) */
  337. OPJ_UINT32 ppm_markers_count;
  338. /** ppm markers data (table indexed by Zppm) */
  339. opj_ppx* ppm_markers;
  340. /** packet header store there for future use in t2_decode_packet */
  341. OPJ_BYTE *ppm_data;
  342. /** size of the ppm_data*/
  343. OPJ_UINT32 ppm_len;
  344. /** size of the ppm_data*/
  345. OPJ_UINT32 ppm_data_read;
  346. OPJ_BYTE *ppm_data_current;
  347. /** packet header storage original buffer */
  348. OPJ_BYTE *ppm_buffer;
  349. /** pointer remaining on the first byte of the first header if ppm is used */
  350. OPJ_BYTE *ppm_data_first;
  351. /** Number of bytes actually stored inside the ppm_data */
  352. OPJ_UINT32 ppm_data_size;
  353. /** use in case of multiple marker PPM (number of info already store) */
  354. OPJ_INT32 ppm_store;
  355. /** use in case of multiple marker PPM (case on non-finished previous info) */
  356. OPJ_INT32 ppm_previous;
  357. /** tile coding parameters */
  358. opj_tcp_t *tcps;
  359. union {
  360. opj_decoding_param_t m_dec;
  361. opj_encoding_param_t m_enc;
  362. }
  363. m_specific_param;
  364. /** OPJ_TRUE if entire bit stream must be decoded, OPJ_FALSE if partial bitstream decoding allowed */
  365. OPJ_BOOL strict;
  366. /* UniPG>> */
  367. #ifdef USE_JPWL
  368. /** enables writing of EPC in MH, thus activating JPWL */
  369. OPJ_BOOL epc_on;
  370. /** enables writing of EPB, in case of activated JPWL */
  371. OPJ_BOOL epb_on;
  372. /** enables writing of ESD, in case of activated JPWL */
  373. OPJ_BOOL esd_on;
  374. /** enables writing of informative techniques of ESD, in case of activated JPWL */
  375. OPJ_BOOL info_on;
  376. /** enables writing of RED, in case of activated JPWL */
  377. OPJ_BOOL red_on;
  378. /** error protection method for MH (0,1,16,32,37-128) */
  379. int hprot_MH;
  380. /** tile number of header protection specification (>=0) */
  381. int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  382. /** error protection methods for TPHs (0,1,16,32,37-128) */
  383. int hprot_TPH[JPWL_MAX_NO_TILESPECS];
  384. /** tile number of packet protection specification (>=0) */
  385. int pprot_tileno[JPWL_MAX_NO_PACKSPECS];
  386. /** packet number of packet protection specification (>=0) */
  387. int pprot_packno[JPWL_MAX_NO_PACKSPECS];
  388. /** error protection methods for packets (0,1,16,32,37-128) */
  389. int pprot[JPWL_MAX_NO_PACKSPECS];
  390. /** enables writing of ESD, (0/2/4 bytes) */
  391. int sens_size;
  392. /** sensitivity addressing size (0=auto/2/4 bytes) */
  393. int sens_addr;
  394. /** sensitivity range (0-3) */
  395. int sens_range;
  396. /** sensitivity method for MH (-1,0-7) */
  397. int sens_MH;
  398. /** tile number of sensitivity specification (>=0) */
  399. int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS];
  400. /** sensitivity methods for TPHs (-1,0-7) */
  401. int sens_TPH[JPWL_MAX_NO_TILESPECS];
  402. /** enables JPWL correction at the decoder */
  403. OPJ_BOOL correct;
  404. /** expected number of components at the decoder */
  405. int exp_comps;
  406. /** maximum number of tiles at the decoder */
  407. OPJ_UINT32 max_tiles;
  408. #endif /* USE_JPWL */
  409. /******** FLAGS *********/
  410. /** if ppm == 1 --> there was a PPM marker*/
  411. OPJ_BITFIELD ppm : 1;
  412. /** tells if the parameter is a coding or decoding one */
  413. OPJ_BITFIELD m_is_decoder : 1;
  414. /** whether different bit depth or sign per component is allowed. Decoder only for ow */
  415. OPJ_BITFIELD allow_different_bit_depth_sign : 1;
  416. /* <<UniPG */
  417. } opj_cp_t;
  418. typedef struct opj_j2k_dec {
  419. /** locate in which part of the codestream the decoder is (main header, tile header, end) */
  420. OPJ_UINT32 m_state;
  421. /**
  422. * store decoding parameters common to all tiles (information like COD, COC in main header)
  423. */
  424. opj_tcp_t *m_default_tcp;
  425. OPJ_BYTE *m_header_data;
  426. OPJ_UINT32 m_header_data_size;
  427. /** to tell the tile part length */
  428. OPJ_UINT32 m_sot_length;
  429. /** Only tiles index in the correct range will be decoded.*/
  430. OPJ_UINT32 m_start_tile_x;
  431. OPJ_UINT32 m_start_tile_y;
  432. OPJ_UINT32 m_end_tile_x;
  433. OPJ_UINT32 m_end_tile_y;
  434. /** Index of the tile to decode (used in get_tile) */
  435. OPJ_INT32 m_tile_ind_to_dec;
  436. /** Position of the last SOT marker read */
  437. OPJ_OFF_T m_last_sot_read_pos;
  438. /**
  439. * Indicate that the current tile-part is assume as the last tile part of the codestream.
  440. * It is useful in the case of PSot is equal to zero. The sot length will be compute in the
  441. * SOD reader function. FIXME NOT USED for the moment
  442. */
  443. OPJ_BOOL m_last_tile_part;
  444. OPJ_UINT32 m_numcomps_to_decode;
  445. OPJ_UINT32 *m_comps_indices_to_decode;
  446. /** to tell that a tile can be decoded. */
  447. OPJ_BITFIELD m_can_decode : 1;
  448. OPJ_BITFIELD m_discard_tiles : 1;
  449. OPJ_BITFIELD m_skip_data : 1;
  450. /** TNsot correction : see issue 254 **/
  451. OPJ_BITFIELD m_nb_tile_parts_correction_checked : 1;
  452. OPJ_BITFIELD m_nb_tile_parts_correction : 1;
  453. } opj_j2k_dec_t;
  454. typedef struct opj_j2k_enc {
  455. /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
  456. OPJ_UINT32 m_current_poc_tile_part_number; /* tp_num */
  457. /** 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.*/
  458. OPJ_UINT32 m_current_tile_part_number; /*cur_tp_num */
  459. /* whether to generate TLM markers */
  460. OPJ_BOOL m_TLM;
  461. /* whether the Ttlmi field in a TLM marker is a byte (otherwise a uint16) */
  462. OPJ_BOOL m_Ttlmi_is_byte;
  463. /**
  464. locate the start position of the TLM marker
  465. after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
  466. */
  467. OPJ_OFF_T m_tlm_start;
  468. /**
  469. * Stores the sizes of the tlm.
  470. */
  471. OPJ_BYTE * m_tlm_sot_offsets_buffer;
  472. /**
  473. * The current offset of the tlm buffer.
  474. */
  475. OPJ_BYTE * m_tlm_sot_offsets_current;
  476. /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
  477. /** used in TLMmarker*/
  478. OPJ_UINT32 m_total_tile_parts; /* totnum_tp */
  479. /* encoded data for a tile */
  480. OPJ_BYTE * m_encoded_tile_data;
  481. /* size of the encoded_data */
  482. OPJ_UINT32 m_encoded_tile_size;
  483. /* encoded data for a tile */
  484. OPJ_BYTE * m_header_tile_data;
  485. /* size of the encoded_data */
  486. OPJ_UINT32 m_header_tile_data_size;
  487. /* whether to generate PLT markers */
  488. OPJ_BOOL m_PLT;
  489. /* reserved bytes in m_encoded_tile_size for PLT markers */
  490. OPJ_UINT32 m_reserved_bytes_for_PLT;
  491. /** Number of components */
  492. OPJ_UINT32 m_nb_comps;
  493. } opj_j2k_enc_t;
  494. struct opj_tcd;
  495. /**
  496. JPEG-2000 codestream reader/writer
  497. */
  498. typedef struct opj_j2k {
  499. /* J2K codestream is decoded*/
  500. OPJ_BOOL m_is_decoder;
  501. /* FIXME DOC*/
  502. union {
  503. opj_j2k_dec_t m_decoder;
  504. opj_j2k_enc_t m_encoder;
  505. }
  506. m_specific_param;
  507. /** pointer to the internal/private encoded / decoded image */
  508. opj_image_t* m_private_image;
  509. /* pointer to the output image (decoded)*/
  510. opj_image_t* m_output_image;
  511. /** Coding parameters */
  512. opj_cp_t m_cp;
  513. /** the list of procedures to exec **/
  514. opj_procedure_list_t * m_procedure_list;
  515. /** the list of validation procedures to follow to make sure the code is valid **/
  516. opj_procedure_list_t * m_validation_list;
  517. /** helper used to write the index file */
  518. opj_codestream_index_t *cstr_index;
  519. /** number of the tile currently concern by coding/decoding */
  520. OPJ_UINT32 m_current_tile_number;
  521. /** the current tile coder/decoder **/
  522. struct opj_tcd * m_tcd;
  523. /** Thread pool */
  524. opj_thread_pool_t* m_tp;
  525. /** Image width coming from JP2 IHDR box. 0 from a pure codestream */
  526. OPJ_UINT32 ihdr_w;
  527. /** Image height coming from JP2 IHDR box. 0 from a pure codestream */
  528. OPJ_UINT32 ihdr_h;
  529. /** Set to 1 by the decoder initialization if OPJ_DPARAMETERS_DUMP_FLAG is set */
  530. unsigned int dump_state;
  531. }
  532. opj_j2k_t;
  533. /** @name Exported functions */
  534. /*@{*/
  535. /* ----------------------------------------------------------------------- */
  536. /**
  537. Setup the decoder decoding parameters using user parameters.
  538. Decoding parameters are returned in j2k->cp.
  539. @param j2k J2K decompressor handle
  540. @param parameters decompression parameters
  541. */
  542. void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
  543. void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict);
  544. OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads);
  545. /**
  546. * Creates a J2K compression structure
  547. *
  548. * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
  549. */
  550. opj_j2k_t* opj_j2k_create_compress(void);
  551. OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
  552. opj_cparameters_t *parameters,
  553. opj_image_t *image,
  554. opj_event_mgr_t * p_manager);
  555. /**
  556. Converts an enum type progression order to string type
  557. */
  558. const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
  559. /* ----------------------------------------------------------------------- */
  560. /*@}*/
  561. /*@}*/
  562. /**
  563. * Ends the decompression procedures and possibiliy add data to be read after the
  564. * codestream.
  565. */
  566. OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k,
  567. opj_stream_private_t *p_stream,
  568. opj_event_mgr_t * p_manager);
  569. /**
  570. * Reads a jpeg2000 codestream header structure.
  571. *
  572. * @param p_stream the stream to read data from.
  573. * @param p_j2k the jpeg2000 codec.
  574. * @param p_image FIXME DOC
  575. * @param p_manager the user event manager.
  576. *
  577. * @return true if the box is valid.
  578. */
  579. OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
  580. opj_j2k_t* p_j2k,
  581. opj_image_t** p_image,
  582. opj_event_mgr_t* p_manager);
  583. /**
  584. * Destroys a jpeg2000 codec.
  585. *
  586. * @param p_j2k the jpeg20000 structure to destroy.
  587. */
  588. void opj_j2k_destroy(opj_j2k_t *p_j2k);
  589. /**
  590. * Destroys a codestream index structure.
  591. *
  592. * @param p_cstr_ind the codestream index parameter to destroy.
  593. */
  594. void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind);
  595. /**
  596. * Decode tile data.
  597. * @param p_j2k the jpeg2000 codec.
  598. * @param p_tile_index
  599. * @param p_data FIXME DOC
  600. * @param p_data_size FIXME DOC
  601. * @param p_stream the stream to write data to.
  602. * @param p_manager the user event manager.
  603. */
  604. OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
  605. OPJ_UINT32 p_tile_index,
  606. OPJ_BYTE * p_data,
  607. OPJ_UINT32 p_data_size,
  608. opj_stream_private_t *p_stream,
  609. opj_event_mgr_t * p_manager);
  610. /**
  611. * Reads a tile header.
  612. * @param p_j2k the jpeg2000 codec.
  613. * @param p_tile_index FIXME DOC
  614. * @param p_data_size FIXME DOC
  615. * @param p_tile_x0 FIXME DOC
  616. * @param p_tile_y0 FIXME DOC
  617. * @param p_tile_x1 FIXME DOC
  618. * @param p_tile_y1 FIXME DOC
  619. * @param p_nb_comps FIXME DOC
  620. * @param p_go_on FIXME DOC
  621. * @param p_stream the stream to write data to.
  622. * @param p_manager the user event manager.
  623. */
  624. OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
  625. OPJ_UINT32 * p_tile_index,
  626. OPJ_UINT32 * p_data_size,
  627. OPJ_INT32 * p_tile_x0,
  628. OPJ_INT32 * p_tile_y0,
  629. OPJ_INT32 * p_tile_x1,
  630. OPJ_INT32 * p_tile_y1,
  631. OPJ_UINT32 * p_nb_comps,
  632. OPJ_BOOL * p_go_on,
  633. opj_stream_private_t *p_stream,
  634. opj_event_mgr_t * p_manager);
  635. /** Sets the indices of the components to decode.
  636. *
  637. * @param p_j2k the jpeg2000 codec.
  638. * @param numcomps Number of components to decode.
  639. * @param comps_indices Array of num_compts indices (numbering starting at 0)
  640. * corresponding to the components to decode.
  641. * @param p_manager Event manager
  642. *
  643. * @return OPJ_TRUE in case of success.
  644. */
  645. OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
  646. OPJ_UINT32 numcomps,
  647. const OPJ_UINT32* comps_indices,
  648. opj_event_mgr_t * p_manager);
  649. /**
  650. * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  651. *
  652. * @param p_j2k the jpeg2000 codec.
  653. * @param p_image FIXME DOC
  654. * @param p_start_x the left position of the rectangle to decode (in image coordinates).
  655. * @param p_start_y the up position of the rectangle to decode (in image coordinates).
  656. * @param p_end_x the right position of the rectangle to decode (in image coordinates).
  657. * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
  658. * @param p_manager the user event manager
  659. *
  660. * @return true if the area could be set.
  661. */
  662. OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
  663. opj_image_t* p_image,
  664. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  665. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  666. opj_event_mgr_t * p_manager);
  667. /**
  668. * Creates a J2K decompression structure.
  669. *
  670. * @return a handle to a J2K decompressor if successful, NULL otherwise.
  671. */
  672. opj_j2k_t* opj_j2k_create_decompress(void);
  673. /**
  674. * Dump some elements from the J2K decompression structure .
  675. *
  676. *@param p_j2k the jpeg2000 codec.
  677. *@param flag flag to describe what elements are dump.
  678. *@param out_stream output stream where dump the elements.
  679. *
  680. */
  681. void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream);
  682. /**
  683. * Dump an image header structure.
  684. *
  685. *@param image the image header to dump.
  686. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  687. *@param out_stream output stream where dump the elements.
  688. */
  689. void j2k_dump_image_header(opj_image_t* image, OPJ_BOOL dev_dump_flag,
  690. FILE* out_stream);
  691. /**
  692. * Dump a component image header structure.
  693. *
  694. *@param comp the component image header to dump.
  695. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  696. *@param out_stream output stream where dump the elements.
  697. */
  698. void j2k_dump_image_comp_header(opj_image_comp_t* comp, OPJ_BOOL dev_dump_flag,
  699. FILE* out_stream);
  700. /**
  701. * Get the codestream info from a JPEG2000 codec.
  702. *
  703. *@param p_j2k the component image header to dump.
  704. *
  705. *@return the codestream information extract from the jpg2000 codec
  706. */
  707. opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k);
  708. /**
  709. * Get the codestream index from a JPEG2000 codec.
  710. *
  711. *@param p_j2k the component image header to dump.
  712. *
  713. *@return the codestream index extract from the jpg2000 codec
  714. */
  715. opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k);
  716. /**
  717. * Decode an image from a JPEG-2000 codestream
  718. * @param j2k J2K decompressor handle
  719. * @param p_stream FIXME DOC
  720. * @param p_image FIXME DOC
  721. * @param p_manager FIXME DOC
  722. * @return FIXME DOC
  723. */
  724. OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k,
  725. opj_stream_private_t *p_stream,
  726. opj_image_t *p_image,
  727. opj_event_mgr_t *p_manager);
  728. OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
  729. opj_stream_private_t *p_stream,
  730. opj_image_t* p_image,
  731. opj_event_mgr_t * p_manager,
  732. OPJ_UINT32 tile_index);
  733. OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
  734. OPJ_UINT32 res_factor,
  735. opj_event_mgr_t * p_manager);
  736. /**
  737. * Specify extra options for the encoder.
  738. *
  739. * @param p_j2k the jpeg2000 codec.
  740. * @param p_options options
  741. * @param p_manager the user event manager
  742. *
  743. * @see opj_encoder_set_extra_options() for more details.
  744. */
  745. OPJ_BOOL opj_j2k_encoder_set_extra_options(
  746. opj_j2k_t *p_j2k,
  747. const char* const* p_options,
  748. opj_event_mgr_t * p_manager);
  749. /**
  750. * Writes a tile.
  751. * @param p_j2k the jpeg2000 codec.
  752. * @param p_tile_index FIXME DOC
  753. * @param p_data FIXME DOC
  754. * @param p_data_size FIXME DOC
  755. * @param p_stream the stream to write data to.
  756. * @param p_manager the user event manager.
  757. */
  758. OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
  759. OPJ_UINT32 p_tile_index,
  760. OPJ_BYTE * p_data,
  761. OPJ_UINT32 p_data_size,
  762. opj_stream_private_t *p_stream,
  763. opj_event_mgr_t * p_manager);
  764. /**
  765. * Encodes an image into a JPEG-2000 codestream
  766. */
  767. OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
  768. opj_stream_private_t *cio,
  769. opj_event_mgr_t * p_manager);
  770. /**
  771. * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
  772. *
  773. * @param p_j2k the jpeg2000 codec.
  774. * @param p_stream the stream object.
  775. * @param p_image FIXME DOC
  776. * @param p_manager the user event manager.
  777. *
  778. * @return true if the codec is valid.
  779. */
  780. OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
  781. opj_stream_private_t *p_stream,
  782. opj_image_t * p_image,
  783. opj_event_mgr_t * p_manager);
  784. /**
  785. * Ends the compression procedures and possibiliy add data to be read after the
  786. * codestream.
  787. */
  788. OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
  789. opj_stream_private_t *cio,
  790. opj_event_mgr_t * p_manager);
  791. OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image);
  792. #endif /* OPJ_J2K_H */