j2k.h 30 KB

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