j2k.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. typedef struct opj_j2k_dec {
  425. /** locate in which part of the codestream the decoder is (main header, tile header, end) */
  426. OPJ_UINT32 m_state;
  427. /**
  428. * store decoding parameters common to all tiles (information like COD, COC in main header)
  429. */
  430. opj_tcp_t *m_default_tcp;
  431. OPJ_BYTE *m_header_data;
  432. OPJ_UINT32 m_header_data_size;
  433. /** to tell the tile part length */
  434. OPJ_UINT32 m_sot_length;
  435. /** Only tiles index in the correct range will be decoded.*/
  436. OPJ_UINT32 m_start_tile_x;
  437. OPJ_UINT32 m_start_tile_y;
  438. OPJ_UINT32 m_end_tile_x;
  439. OPJ_UINT32 m_end_tile_y;
  440. /** Index of the tile to decode (used in get_tile) */
  441. OPJ_INT32 m_tile_ind_to_dec;
  442. /** Position of the last SOT marker read */
  443. OPJ_OFF_T m_last_sot_read_pos;
  444. /**
  445. * Indicate that the current tile-part is assume as the last tile part of the codestream.
  446. * It is useful in the case of PSot is equal to zero. The sot length will be compute in the
  447. * SOD reader function. FIXME NOT USED for the moment
  448. */
  449. OPJ_BOOL m_last_tile_part;
  450. OPJ_UINT32 m_numcomps_to_decode;
  451. OPJ_UINT32 *m_comps_indices_to_decode;
  452. /** to tell that a tile can be decoded. */
  453. OPJ_BITFIELD m_can_decode : 1;
  454. OPJ_BITFIELD m_discard_tiles : 1;
  455. OPJ_BITFIELD m_skip_data : 1;
  456. /** TNsot correction : see issue 254 **/
  457. OPJ_BITFIELD m_nb_tile_parts_correction_checked : 1;
  458. OPJ_BITFIELD m_nb_tile_parts_correction : 1;
  459. } opj_j2k_dec_t;
  460. typedef struct opj_j2k_enc {
  461. /** Tile part number, regardless of poc, for each new poc, tp is reset to 1*/
  462. OPJ_UINT32 m_current_poc_tile_part_number; /* tp_num */
  463. /** 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.*/
  464. OPJ_UINT32 m_current_tile_part_number; /*cur_tp_num */
  465. /* whether to generate TLM markers */
  466. OPJ_BOOL m_TLM;
  467. /* whether the Ttlmi field in a TLM marker is a byte (otherwise a uint16) */
  468. OPJ_BOOL m_Ttlmi_is_byte;
  469. /**
  470. locate the start position of the TLM marker
  471. after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length.
  472. */
  473. OPJ_OFF_T m_tlm_start;
  474. /**
  475. * Stores the sizes of the tlm.
  476. */
  477. OPJ_BYTE * m_tlm_sot_offsets_buffer;
  478. /**
  479. * The current offset of the tlm buffer.
  480. */
  481. OPJ_BYTE * m_tlm_sot_offsets_current;
  482. /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/
  483. /** used in TLMmarker*/
  484. OPJ_UINT32 m_total_tile_parts; /* totnum_tp */
  485. /* encoded data for a tile */
  486. OPJ_BYTE * m_encoded_tile_data;
  487. /* size of the encoded_data */
  488. OPJ_UINT32 m_encoded_tile_size;
  489. /* encoded data for a tile */
  490. OPJ_BYTE * m_header_tile_data;
  491. /* size of the encoded_data */
  492. OPJ_UINT32 m_header_tile_data_size;
  493. /* whether to generate PLT markers */
  494. OPJ_BOOL m_PLT;
  495. /* reserved bytes in m_encoded_tile_size for PLT markers */
  496. OPJ_UINT32 m_reserved_bytes_for_PLT;
  497. /** Number of components */
  498. OPJ_UINT32 m_nb_comps;
  499. } opj_j2k_enc_t;
  500. struct opj_tcd;
  501. /**
  502. JPEG-2000 codestream reader/writer
  503. */
  504. typedef struct opj_j2k {
  505. /* J2K codestream is decoded*/
  506. OPJ_BOOL m_is_decoder;
  507. /* FIXME DOC*/
  508. union {
  509. opj_j2k_dec_t m_decoder;
  510. opj_j2k_enc_t m_encoder;
  511. }
  512. m_specific_param;
  513. /** pointer to the internal/private encoded / decoded image */
  514. opj_image_t* m_private_image;
  515. /* pointer to the output image (decoded)*/
  516. opj_image_t* m_output_image;
  517. /** Coding parameters */
  518. opj_cp_t m_cp;
  519. /** the list of procedures to exec **/
  520. opj_procedure_list_t * m_procedure_list;
  521. /** the list of validation procedures to follow to make sure the code is valid **/
  522. opj_procedure_list_t * m_validation_list;
  523. /** helper used to write the index file */
  524. opj_codestream_index_t *cstr_index;
  525. /** number of the tile currently concern by coding/decoding */
  526. OPJ_UINT32 m_current_tile_number;
  527. /** the current tile coder/decoder **/
  528. struct opj_tcd * m_tcd;
  529. /** Thread pool */
  530. opj_thread_pool_t* m_tp;
  531. /** Image width coming from JP2 IHDR box. 0 from a pure codestream */
  532. OPJ_UINT32 ihdr_w;
  533. /** Image height coming from JP2 IHDR box. 0 from a pure codestream */
  534. OPJ_UINT32 ihdr_h;
  535. /** Set to 1 by the decoder initialization if OPJ_DPARAMETERS_DUMP_FLAG is set */
  536. unsigned int dump_state;
  537. }
  538. opj_j2k_t;
  539. /** @name Exported functions */
  540. /*@{*/
  541. /* ----------------------------------------------------------------------- */
  542. /**
  543. Setup the decoder decoding parameters using user parameters.
  544. Decoding parameters are returned in j2k->cp.
  545. @param j2k J2K decompressor handle
  546. @param parameters decompression parameters
  547. */
  548. void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
  549. void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict);
  550. OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads);
  551. /**
  552. * Creates a J2K compression structure
  553. *
  554. * @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
  555. */
  556. opj_j2k_t* opj_j2k_create_compress(void);
  557. OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
  558. opj_cparameters_t *parameters,
  559. opj_image_t *image,
  560. opj_event_mgr_t * p_manager);
  561. /**
  562. Converts an enum type progression order to string type
  563. */
  564. const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order);
  565. /* ----------------------------------------------------------------------- */
  566. /*@}*/
  567. /*@}*/
  568. /**
  569. * Ends the decompression procedures and possibiliy add data to be read after the
  570. * codestream.
  571. */
  572. OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *j2k,
  573. opj_stream_private_t *p_stream,
  574. opj_event_mgr_t * p_manager);
  575. /**
  576. * Reads a jpeg2000 codestream header structure.
  577. *
  578. * @param p_stream the stream to read data from.
  579. * @param p_j2k the jpeg2000 codec.
  580. * @param p_image FIXME DOC
  581. * @param p_manager the user event manager.
  582. *
  583. * @return true if the box is valid.
  584. */
  585. OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
  586. opj_j2k_t* p_j2k,
  587. opj_image_t** p_image,
  588. opj_event_mgr_t* p_manager);
  589. /**
  590. * Destroys a jpeg2000 codec.
  591. *
  592. * @param p_j2k the jpeg20000 structure to destroy.
  593. */
  594. void opj_j2k_destroy(opj_j2k_t *p_j2k);
  595. /**
  596. * Destroys a codestream index structure.
  597. *
  598. * @param p_cstr_ind the codestream index parameter to destroy.
  599. */
  600. void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind);
  601. /**
  602. * Decode tile data.
  603. * @param p_j2k the jpeg2000 codec.
  604. * @param p_tile_index
  605. * @param p_data FIXME DOC
  606. * @param p_data_size FIXME DOC
  607. * @param p_stream the stream to write data to.
  608. * @param p_manager the user event manager.
  609. */
  610. OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
  611. OPJ_UINT32 p_tile_index,
  612. OPJ_BYTE * p_data,
  613. OPJ_UINT32 p_data_size,
  614. opj_stream_private_t *p_stream,
  615. opj_event_mgr_t * p_manager);
  616. /**
  617. * Reads a tile header.
  618. * @param p_j2k the jpeg2000 codec.
  619. * @param p_tile_index FIXME DOC
  620. * @param p_data_size FIXME DOC
  621. * @param p_tile_x0 FIXME DOC
  622. * @param p_tile_y0 FIXME DOC
  623. * @param p_tile_x1 FIXME DOC
  624. * @param p_tile_y1 FIXME DOC
  625. * @param p_nb_comps FIXME DOC
  626. * @param p_go_on FIXME DOC
  627. * @param p_stream the stream to write data to.
  628. * @param p_manager the user event manager.
  629. */
  630. OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
  631. OPJ_UINT32 * p_tile_index,
  632. OPJ_UINT32 * p_data_size,
  633. OPJ_INT32 * p_tile_x0,
  634. OPJ_INT32 * p_tile_y0,
  635. OPJ_INT32 * p_tile_x1,
  636. OPJ_INT32 * p_tile_y1,
  637. OPJ_UINT32 * p_nb_comps,
  638. OPJ_BOOL * p_go_on,
  639. opj_stream_private_t *p_stream,
  640. opj_event_mgr_t * p_manager);
  641. /** Sets the indices of the components to decode.
  642. *
  643. * @param p_j2k the jpeg2000 codec.
  644. * @param numcomps Number of components to decode.
  645. * @param comps_indices Array of num_compts indices (numbering starting at 0)
  646. * corresponding to the components to decode.
  647. * @param p_manager Event manager
  648. *
  649. * @return OPJ_TRUE in case of success.
  650. */
  651. OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
  652. OPJ_UINT32 numcomps,
  653. const OPJ_UINT32* comps_indices,
  654. opj_event_mgr_t * p_manager);
  655. /**
  656. * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  657. *
  658. * @param p_j2k the jpeg2000 codec.
  659. * @param p_image FIXME DOC
  660. * @param p_start_x the left position of the rectangle to decode (in image coordinates).
  661. * @param p_start_y the up position of the rectangle to decode (in image coordinates).
  662. * @param p_end_x the right position of the rectangle to decode (in image coordinates).
  663. * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
  664. * @param p_manager the user event manager
  665. *
  666. * @return true if the area could be set.
  667. */
  668. OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
  669. opj_image_t* p_image,
  670. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  671. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  672. opj_event_mgr_t * p_manager);
  673. /**
  674. * Creates a J2K decompression structure.
  675. *
  676. * @return a handle to a J2K decompressor if successful, NULL otherwise.
  677. */
  678. opj_j2k_t* opj_j2k_create_decompress(void);
  679. /**
  680. * Dump some elements from the J2K decompression structure .
  681. *
  682. *@param p_j2k the jpeg2000 codec.
  683. *@param flag flag to describe what elements are dump.
  684. *@param out_stream output stream where dump the elements.
  685. *
  686. */
  687. void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream);
  688. /**
  689. * Dump an image header structure.
  690. *
  691. *@param image the image header to dump.
  692. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  693. *@param out_stream output stream where dump the elements.
  694. */
  695. void j2k_dump_image_header(opj_image_t* image, OPJ_BOOL dev_dump_flag,
  696. FILE* out_stream);
  697. /**
  698. * Dump a component image header structure.
  699. *
  700. *@param comp the component image header to dump.
  701. *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
  702. *@param out_stream output stream where dump the elements.
  703. */
  704. void j2k_dump_image_comp_header(opj_image_comp_t* comp, OPJ_BOOL dev_dump_flag,
  705. FILE* out_stream);
  706. /**
  707. * Get the codestream info from a JPEG2000 codec.
  708. *
  709. *@param p_j2k the component image header to dump.
  710. *
  711. *@return the codestream information extract from the jpg2000 codec
  712. */
  713. opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k);
  714. /**
  715. * Get the codestream index from a JPEG2000 codec.
  716. *
  717. *@param p_j2k the component image header to dump.
  718. *
  719. *@return the codestream index extract from the jpg2000 codec
  720. */
  721. opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k);
  722. /**
  723. * Decode an image from a JPEG-2000 codestream
  724. * @param j2k J2K decompressor handle
  725. * @param p_stream FIXME DOC
  726. * @param p_image FIXME DOC
  727. * @param p_manager FIXME DOC
  728. * @return FIXME DOC
  729. */
  730. OPJ_BOOL opj_j2k_decode(opj_j2k_t *j2k,
  731. opj_stream_private_t *p_stream,
  732. opj_image_t *p_image,
  733. opj_event_mgr_t *p_manager);
  734. OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
  735. opj_stream_private_t *p_stream,
  736. opj_image_t* p_image,
  737. opj_event_mgr_t * p_manager,
  738. OPJ_UINT32 tile_index);
  739. OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
  740. OPJ_UINT32 res_factor,
  741. opj_event_mgr_t * p_manager);
  742. /**
  743. * Specify extra options for the encoder.
  744. *
  745. * @param p_j2k the jpeg2000 codec.
  746. * @param p_options options
  747. * @param p_manager the user event manager
  748. *
  749. * @see opj_encoder_set_extra_options() for more details.
  750. */
  751. OPJ_BOOL opj_j2k_encoder_set_extra_options(
  752. opj_j2k_t *p_j2k,
  753. const char* const* p_options,
  754. opj_event_mgr_t * p_manager);
  755. /**
  756. * Writes a tile.
  757. * @param p_j2k the jpeg2000 codec.
  758. * @param p_tile_index FIXME DOC
  759. * @param p_data FIXME DOC
  760. * @param p_data_size FIXME DOC
  761. * @param p_stream the stream to write data to.
  762. * @param p_manager the user event manager.
  763. */
  764. OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
  765. OPJ_UINT32 p_tile_index,
  766. OPJ_BYTE * p_data,
  767. OPJ_UINT32 p_data_size,
  768. opj_stream_private_t *p_stream,
  769. opj_event_mgr_t * p_manager);
  770. /**
  771. * Encodes an image into a JPEG-2000 codestream
  772. */
  773. OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
  774. opj_stream_private_t *cio,
  775. opj_event_mgr_t * p_manager);
  776. /**
  777. * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
  778. *
  779. * @param p_j2k the jpeg2000 codec.
  780. * @param p_stream the stream object.
  781. * @param p_image FIXME DOC
  782. * @param p_manager the user event manager.
  783. *
  784. * @return true if the codec is valid.
  785. */
  786. OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
  787. opj_stream_private_t *p_stream,
  788. opj_image_t * p_image,
  789. opj_event_mgr_t * p_manager);
  790. /**
  791. * Ends the compression procedures and possibiliy add data to be read after the
  792. * codestream.
  793. */
  794. OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
  795. opj_stream_private_t *cio,
  796. opj_event_mgr_t * p_manager);
  797. OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image);
  798. #endif /* OPJ_J2K_H */