jp2.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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) 2002-2003, Yannick Verschueren
  10. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  11. * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
  12. * Copyright (c) 2012, CS Systemes d'Information, France
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef OPJ_JP2_H
  37. #define OPJ_JP2_H
  38. /**
  39. @file jp2.h
  40. @brief The JPEG-2000 file format Reader/Writer (JP2)
  41. */
  42. /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
  43. /*@{*/
  44. /*#define JPIP_JPIP 0x6a706970*/
  45. #define JP2_JP 0x6a502020 /**< JPEG 2000 signature box */
  46. #define JP2_FTYP 0x66747970 /**< File type box */
  47. #define JP2_JP2H 0x6a703268 /**< JP2 header box (super-box) */
  48. #define JP2_IHDR 0x69686472 /**< Image header box */
  49. #define JP2_COLR 0x636f6c72 /**< Colour specification box */
  50. #define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */
  51. #define JP2_URL 0x75726c20 /**< Data entry URL box */
  52. #define JP2_PCLR 0x70636c72 /**< Palette box */
  53. #define JP2_CMAP 0x636d6170 /**< Component Mapping box */
  54. #define JP2_CDEF 0x63646566 /**< Channel Definition box */
  55. #define JP2_DTBL 0x6474626c /**< Data Reference box */
  56. #define JP2_BPCC 0x62706363 /**< Bits per component box */
  57. #define JP2_JP2 0x6a703220 /**< File type fields */
  58. /* For the future */
  59. /* #define JP2_RES 0x72657320 */ /**< Resolution box (super-box) */
  60. /* #define JP2_JP2I 0x6a703269 */ /**< Intellectual property box */
  61. /* #define JP2_XML 0x786d6c20 */ /**< XML box */
  62. /* #define JP2_UUID 0x75756994 */ /**< UUID box */
  63. /* #define JP2_UINF 0x75696e66 */ /**< UUID info box (super-box) */
  64. /* #define JP2_ULST 0x756c7374 */ /**< UUID list box */
  65. /* ----------------------------------------------------------------------- */
  66. typedef enum {
  67. JP2_STATE_NONE = 0x0,
  68. JP2_STATE_SIGNATURE = 0x1,
  69. JP2_STATE_FILE_TYPE = 0x2,
  70. JP2_STATE_HEADER = 0x4,
  71. JP2_STATE_CODESTREAM = 0x8,
  72. JP2_STATE_END_CODESTREAM = 0x10,
  73. JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
  74. }
  75. JP2_STATE;
  76. typedef enum {
  77. JP2_IMG_STATE_NONE = 0x0,
  78. JP2_IMG_STATE_UNKNOWN = 0x7fffffff
  79. }
  80. JP2_IMG_STATE;
  81. /**
  82. Channel description: channel index, type, association
  83. */
  84. typedef struct opj_jp2_cdef_info {
  85. OPJ_UINT16 cn, typ, asoc;
  86. } opj_jp2_cdef_info_t;
  87. /**
  88. Channel descriptions and number of descriptions
  89. */
  90. typedef struct opj_jp2_cdef {
  91. opj_jp2_cdef_info_t *info;
  92. OPJ_UINT16 n;
  93. } opj_jp2_cdef_t;
  94. /**
  95. Component mappings: channel index, mapping type, palette index
  96. */
  97. typedef struct opj_jp2_cmap_comp {
  98. OPJ_UINT16 cmp;
  99. OPJ_BYTE mtyp, pcol;
  100. } opj_jp2_cmap_comp_t;
  101. /**
  102. Palette data: table entries, palette columns
  103. */
  104. typedef struct opj_jp2_pclr {
  105. OPJ_UINT32 *entries;
  106. OPJ_BYTE *channel_sign;
  107. OPJ_BYTE *channel_size;
  108. opj_jp2_cmap_comp_t *cmap;
  109. OPJ_UINT16 nr_entries;
  110. OPJ_BYTE nr_channels;
  111. } opj_jp2_pclr_t;
  112. /**
  113. Collector for ICC profile, palette, component mapping, channel description
  114. */
  115. typedef struct opj_jp2_color {
  116. OPJ_BYTE *icc_profile_buf;
  117. OPJ_UINT32 icc_profile_len;
  118. opj_jp2_cdef_t *jp2_cdef;
  119. opj_jp2_pclr_t *jp2_pclr;
  120. OPJ_BYTE jp2_has_colr;
  121. } opj_jp2_color_t;
  122. /**
  123. JP2 component
  124. */
  125. typedef struct opj_jp2_comps {
  126. OPJ_UINT32 depth;
  127. OPJ_UINT32 sgnd;
  128. OPJ_UINT32 bpcc;
  129. } opj_jp2_comps_t;
  130. /**
  131. JPEG-2000 file format reader/writer
  132. */
  133. typedef struct opj_jp2 {
  134. /** handle to the J2K codec */
  135. opj_j2k_t *j2k;
  136. /** list of validation procedures */
  137. struct opj_procedure_list * m_validation_list;
  138. /** list of execution procedures */
  139. struct opj_procedure_list * m_procedure_list;
  140. /* width of image */
  141. OPJ_UINT32 w;
  142. /* height of image */
  143. OPJ_UINT32 h;
  144. /* number of components in the image */
  145. OPJ_UINT32 numcomps;
  146. OPJ_UINT32 bpc;
  147. OPJ_UINT32 C;
  148. OPJ_UINT32 UnkC;
  149. OPJ_UINT32 IPR;
  150. OPJ_UINT32 meth;
  151. OPJ_UINT32 approx;
  152. OPJ_UINT32 enumcs;
  153. OPJ_UINT32 precedence;
  154. OPJ_UINT32 brand;
  155. OPJ_UINT32 minversion;
  156. OPJ_UINT32 numcl;
  157. OPJ_UINT32 *cl;
  158. opj_jp2_comps_t *comps;
  159. /* FIXME: The following two variables are used to save offset
  160. as we write out a JP2 file to disk. This mechanism is not flexible
  161. as codec writers will need to extand those fields as new part
  162. of the standard are implemented.
  163. */
  164. OPJ_OFF_T j2k_codestream_offset;
  165. OPJ_OFF_T jpip_iptr_offset;
  166. OPJ_BOOL jpip_on;
  167. OPJ_UINT32 jp2_state;
  168. OPJ_UINT32 jp2_img_state;
  169. opj_jp2_color_t color;
  170. OPJ_BOOL ignore_pclr_cmap_cdef;
  171. OPJ_BYTE has_jp2h;
  172. OPJ_BYTE has_ihdr;
  173. }
  174. opj_jp2_t;
  175. /**
  176. JP2 Box
  177. */
  178. typedef struct opj_jp2_box {
  179. OPJ_UINT32 length;
  180. OPJ_UINT32 type;
  181. OPJ_INT32 init_pos;
  182. } opj_jp2_box_t;
  183. typedef struct opj_jp2_header_handler {
  184. /* marker value */
  185. OPJ_UINT32 id;
  186. /* action linked to the marker */
  187. OPJ_BOOL(*handler)(opj_jp2_t *jp2,
  188. OPJ_BYTE *p_header_data,
  189. OPJ_UINT32 p_header_size,
  190. opj_event_mgr_t * p_manager);
  191. }
  192. opj_jp2_header_handler_t;
  193. typedef struct opj_jp2_img_header_writer_handler {
  194. /* action to perform */
  195. OPJ_BYTE* (*handler)(opj_jp2_t *jp2, OPJ_UINT32 * p_data_size);
  196. /* result of the action : data */
  197. OPJ_BYTE* m_data;
  198. /* size of data */
  199. OPJ_UINT32 m_size;
  200. }
  201. opj_jp2_img_header_writer_handler_t;
  202. /** @name Exported functions */
  203. /*@{*/
  204. /* ----------------------------------------------------------------------- */
  205. /**
  206. Setup the decoder decoding parameters using user parameters.
  207. Decoding parameters are returned in jp2->j2k->cp.
  208. @param jp2 JP2 decompressor handle
  209. @param parameters decompression parameters
  210. */
  211. void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters);
  212. /** Allocates worker threads for the compressor/decompressor.
  213. *
  214. * @param jp2 JP2 decompressor handle
  215. * @param num_threads Number of threads.
  216. * @return OPJ_TRUE in case of success.
  217. */
  218. OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads);
  219. /**
  220. * Decode an image from a JPEG-2000 file stream
  221. * @param jp2 JP2 decompressor handle
  222. * @param p_stream FIXME DOC
  223. * @param p_image FIXME DOC
  224. * @param p_manager FIXME DOC
  225. *
  226. * @return Returns a decoded image if successful, returns NULL otherwise
  227. */
  228. OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
  229. opj_stream_private_t *p_stream,
  230. opj_image_t* p_image,
  231. opj_event_mgr_t * p_manager);
  232. /**
  233. * Setup the encoder parameters using the current image and using user parameters.
  234. * Coding parameters are returned in jp2->j2k->cp.
  235. *
  236. * @param jp2 JP2 compressor handle
  237. * @param parameters compression parameters
  238. * @param image input filled image
  239. * @param p_manager FIXME DOC
  240. * @return OPJ_TRUE if successful, OPJ_FALSE otherwise
  241. */
  242. OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
  243. opj_cparameters_t *parameters,
  244. opj_image_t *image,
  245. opj_event_mgr_t * p_manager);
  246. /**
  247. Encode an image into a JPEG-2000 file stream
  248. @param jp2 JP2 compressor handle
  249. @param stream Output buffer stream
  250. @param p_manager event manager
  251. @return Returns true if successful, returns false otherwise
  252. */
  253. OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
  254. opj_stream_private_t *stream,
  255. opj_event_mgr_t * p_manager);
  256. /**
  257. * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
  258. *
  259. * @param jp2 the jpeg2000 file codec.
  260. * @param stream the stream object.
  261. * @param p_image FIXME DOC
  262. * @param p_manager FIXME DOC
  263. *
  264. * @return true if the codec is valid.
  265. */
  266. OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
  267. opj_stream_private_t *stream,
  268. opj_image_t * p_image,
  269. opj_event_mgr_t * p_manager);
  270. /**
  271. * Ends the compression procedures and possibiliy add data to be read after the
  272. * codestream.
  273. */
  274. OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
  275. opj_stream_private_t *cio,
  276. opj_event_mgr_t * p_manager);
  277. /* ----------------------------------------------------------------------- */
  278. /**
  279. * Ends the decompression procedures and possibiliy add data to be read after the
  280. * codestream.
  281. */
  282. OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
  283. opj_stream_private_t *cio,
  284. opj_event_mgr_t * p_manager);
  285. /**
  286. * Reads a jpeg2000 file header structure.
  287. *
  288. * @param p_stream the stream to read data from.
  289. * @param jp2 the jpeg2000 file header structure.
  290. * @param p_image FIXME DOC
  291. * @param p_manager the user event manager.
  292. *
  293. * @return true if the box is valid.
  294. */
  295. OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
  296. opj_jp2_t *jp2,
  297. opj_image_t ** p_image,
  298. opj_event_mgr_t * p_manager);
  299. /** Sets the indices of the components to decode.
  300. *
  301. * @param jp2 JP2 decompressor handle
  302. * @param numcomps Number of components to decode.
  303. * @param comps_indices Array of num_compts indices (numbering starting at 0)
  304. * corresponding to the components to decode.
  305. * @param p_manager Event manager;
  306. *
  307. * @return OPJ_TRUE in case of success.
  308. */
  309. OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *jp2,
  310. OPJ_UINT32 numcomps,
  311. const OPJ_UINT32* comps_indices,
  312. opj_event_mgr_t * p_manager);
  313. /**
  314. * Reads a tile header.
  315. * @param p_jp2 the jpeg2000 codec.
  316. * @param p_tile_index FIXME DOC
  317. * @param p_data_size FIXME DOC
  318. * @param p_tile_x0 FIXME DOC
  319. * @param p_tile_y0 FIXME DOC
  320. * @param p_tile_x1 FIXME DOC
  321. * @param p_tile_y1 FIXME DOC
  322. * @param p_nb_comps FIXME DOC
  323. * @param p_go_on FIXME DOC
  324. * @param p_stream the stream to write data to.
  325. * @param p_manager the user event manager.
  326. */
  327. OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
  328. OPJ_UINT32 * p_tile_index,
  329. OPJ_UINT32 * p_data_size,
  330. OPJ_INT32 * p_tile_x0,
  331. OPJ_INT32 * p_tile_y0,
  332. OPJ_INT32 * p_tile_x1,
  333. OPJ_INT32 * p_tile_y1,
  334. OPJ_UINT32 * p_nb_comps,
  335. OPJ_BOOL * p_go_on,
  336. opj_stream_private_t *p_stream,
  337. opj_event_mgr_t * p_manager);
  338. /**
  339. * Writes a tile.
  340. *
  341. * @param p_jp2 the jpeg2000 codec.
  342. * @param p_tile_index FIXME DOC
  343. * @param p_data FIXME DOC
  344. * @param p_data_size FIXME DOC
  345. * @param p_stream the stream to write data to.
  346. * @param p_manager the user event manager.
  347. */
  348. OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
  349. OPJ_UINT32 p_tile_index,
  350. OPJ_BYTE * p_data,
  351. OPJ_UINT32 p_data_size,
  352. opj_stream_private_t *p_stream,
  353. opj_event_mgr_t * p_manager);
  354. /**
  355. * Decode tile data.
  356. * @param p_jp2 the jpeg2000 codec.
  357. * @param p_tile_index FIXME DOC
  358. * @param p_data FIXME DOC
  359. * @param p_data_size FIXME DOC
  360. * @param p_stream the stream to write data to.
  361. * @param p_manager the user event manager.
  362. *
  363. * @return FIXME DOC
  364. */
  365. OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
  366. OPJ_UINT32 p_tile_index,
  367. OPJ_BYTE * p_data,
  368. OPJ_UINT32 p_data_size,
  369. opj_stream_private_t *p_stream,
  370. opj_event_mgr_t * p_manager);
  371. /**
  372. * Creates a jpeg2000 file decompressor.
  373. *
  374. * @return an empty jpeg2000 file codec.
  375. */
  376. opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder);
  377. /**
  378. Destroy a JP2 decompressor handle
  379. @param jp2 JP2 decompressor handle to destroy
  380. */
  381. void opj_jp2_destroy(opj_jp2_t *jp2);
  382. /**
  383. * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  384. *
  385. * @param p_jp2 the jpeg2000 codec.
  386. * @param p_image FIXME DOC
  387. * @param p_start_x the left position of the rectangle to decode (in image coordinates).
  388. * @param p_start_y the up position of the rectangle to decode (in image coordinates).
  389. * @param p_end_x the right position of the rectangle to decode (in image coordinates).
  390. * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
  391. * @param p_manager the user event manager
  392. *
  393. * @return true if the area could be set.
  394. */
  395. OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
  396. opj_image_t* p_image,
  397. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  398. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  399. opj_event_mgr_t * p_manager);
  400. /**
  401. *
  402. */
  403. OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
  404. opj_stream_private_t *p_stream,
  405. opj_image_t* p_image,
  406. opj_event_mgr_t * p_manager,
  407. OPJ_UINT32 tile_index);
  408. /**
  409. *
  410. */
  411. OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
  412. OPJ_UINT32 res_factor,
  413. opj_event_mgr_t * p_manager);
  414. /**
  415. * Specify extra options for the encoder.
  416. *
  417. * @param p_jp2 the jpeg2000 codec.
  418. * @param p_options options
  419. * @param p_manager the user event manager
  420. *
  421. * @see opj_encoder_set_extra_options() for more details.
  422. */
  423. OPJ_BOOL opj_jp2_encoder_set_extra_options(
  424. opj_jp2_t *p_jp2,
  425. const char* const* p_options,
  426. opj_event_mgr_t * p_manager);
  427. /* TODO MSD: clean these 3 functions */
  428. /**
  429. * Dump some elements from the JP2 decompression structure .
  430. *
  431. *@param p_jp2 the jp2 codec.
  432. *@param flag flag to describe what elements are dump.
  433. *@param out_stream output stream where dump the elements.
  434. *
  435. */
  436. void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
  437. /**
  438. * Get the codestream info from a JPEG2000 codec.
  439. *
  440. *@param p_jp2 jp2 codec.
  441. *
  442. *@return the codestream information extract from the jpg2000 codec
  443. */
  444. opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2);
  445. /**
  446. * Get the codestream index from a JPEG2000 codec.
  447. *
  448. *@param p_jp2 jp2 codec.
  449. *
  450. *@return the codestream index extract from the jpg2000 codec
  451. */
  452. opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2);
  453. /*@}*/
  454. /*@}*/
  455. #endif /* OPJ_JP2_H */