jp2.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /**
  213. Set the strict mode parameter. When strict mode is enabled, the entire
  214. bitstream must be decoded or an error is returned. When it is disabled,
  215. the decoder will decode partial bitstreams.
  216. @param jp2 JP2 decompressor handle
  217. @param strict OPJ_TRUE for strict mode
  218. */
  219. void opj_jp2_decoder_set_strict_mode(opj_jp2_t *jp2, OPJ_BOOL strict);
  220. /** Allocates worker threads for the compressor/decompressor.
  221. *
  222. * @param jp2 JP2 decompressor handle
  223. * @param num_threads Number of threads.
  224. * @return OPJ_TRUE in case of success.
  225. */
  226. OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads);
  227. /**
  228. * Decode an image from a JPEG-2000 file stream
  229. * @param jp2 JP2 decompressor handle
  230. * @param p_stream FIXME DOC
  231. * @param p_image FIXME DOC
  232. * @param p_manager FIXME DOC
  233. *
  234. * @return Returns a decoded image if successful, returns NULL otherwise
  235. */
  236. OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
  237. opj_stream_private_t *p_stream,
  238. opj_image_t* p_image,
  239. opj_event_mgr_t * p_manager);
  240. /**
  241. * Setup the encoder parameters using the current image and using user parameters.
  242. * Coding parameters are returned in jp2->j2k->cp.
  243. *
  244. * @param jp2 JP2 compressor handle
  245. * @param parameters compression parameters
  246. * @param image input filled image
  247. * @param p_manager FIXME DOC
  248. * @return OPJ_TRUE if successful, OPJ_FALSE otherwise
  249. */
  250. OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
  251. opj_cparameters_t *parameters,
  252. opj_image_t *image,
  253. opj_event_mgr_t * p_manager);
  254. /**
  255. Encode an image into a JPEG-2000 file stream
  256. @param jp2 JP2 compressor handle
  257. @param stream Output buffer stream
  258. @param p_manager event manager
  259. @return Returns true if successful, returns false otherwise
  260. */
  261. OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
  262. opj_stream_private_t *stream,
  263. opj_event_mgr_t * p_manager);
  264. /**
  265. * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
  266. *
  267. * @param jp2 the jpeg2000 file codec.
  268. * @param stream the stream object.
  269. * @param p_image FIXME DOC
  270. * @param p_manager FIXME DOC
  271. *
  272. * @return true if the codec is valid.
  273. */
  274. OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
  275. opj_stream_private_t *stream,
  276. opj_image_t * p_image,
  277. opj_event_mgr_t * p_manager);
  278. /**
  279. * Ends the compression procedures and possibiliy add data to be read after the
  280. * codestream.
  281. */
  282. OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
  283. opj_stream_private_t *cio,
  284. opj_event_mgr_t * p_manager);
  285. /* ----------------------------------------------------------------------- */
  286. /**
  287. * Ends the decompression procedures and possibiliy add data to be read after the
  288. * codestream.
  289. */
  290. OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
  291. opj_stream_private_t *cio,
  292. opj_event_mgr_t * p_manager);
  293. /**
  294. * Reads a jpeg2000 file header structure.
  295. *
  296. * @param p_stream the stream to read data from.
  297. * @param jp2 the jpeg2000 file header structure.
  298. * @param p_image FIXME DOC
  299. * @param p_manager the user event manager.
  300. *
  301. * @return true if the box is valid.
  302. */
  303. OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
  304. opj_jp2_t *jp2,
  305. opj_image_t ** p_image,
  306. opj_event_mgr_t * p_manager);
  307. /** Sets the indices of the components to decode.
  308. *
  309. * @param jp2 JP2 decompressor handle
  310. * @param numcomps Number of components to decode.
  311. * @param comps_indices Array of num_compts indices (numbering starting at 0)
  312. * corresponding to the components to decode.
  313. * @param p_manager Event manager;
  314. *
  315. * @return OPJ_TRUE in case of success.
  316. */
  317. OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *jp2,
  318. OPJ_UINT32 numcomps,
  319. const OPJ_UINT32* comps_indices,
  320. opj_event_mgr_t * p_manager);
  321. /**
  322. * Reads a tile header.
  323. * @param p_jp2 the jpeg2000 codec.
  324. * @param p_tile_index FIXME DOC
  325. * @param p_data_size FIXME DOC
  326. * @param p_tile_x0 FIXME DOC
  327. * @param p_tile_y0 FIXME DOC
  328. * @param p_tile_x1 FIXME DOC
  329. * @param p_tile_y1 FIXME DOC
  330. * @param p_nb_comps FIXME DOC
  331. * @param p_go_on FIXME DOC
  332. * @param p_stream the stream to write data to.
  333. * @param p_manager the user event manager.
  334. */
  335. OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
  336. OPJ_UINT32 * p_tile_index,
  337. OPJ_UINT32 * p_data_size,
  338. OPJ_INT32 * p_tile_x0,
  339. OPJ_INT32 * p_tile_y0,
  340. OPJ_INT32 * p_tile_x1,
  341. OPJ_INT32 * p_tile_y1,
  342. OPJ_UINT32 * p_nb_comps,
  343. OPJ_BOOL * p_go_on,
  344. opj_stream_private_t *p_stream,
  345. opj_event_mgr_t * p_manager);
  346. /**
  347. * Writes a tile.
  348. *
  349. * @param p_jp2 the jpeg2000 codec.
  350. * @param p_tile_index FIXME DOC
  351. * @param p_data FIXME DOC
  352. * @param p_data_size FIXME DOC
  353. * @param p_stream the stream to write data to.
  354. * @param p_manager the user event manager.
  355. */
  356. OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
  357. OPJ_UINT32 p_tile_index,
  358. OPJ_BYTE * p_data,
  359. OPJ_UINT32 p_data_size,
  360. opj_stream_private_t *p_stream,
  361. opj_event_mgr_t * p_manager);
  362. /**
  363. * Decode tile data.
  364. * @param p_jp2 the jpeg2000 codec.
  365. * @param p_tile_index FIXME DOC
  366. * @param p_data FIXME DOC
  367. * @param p_data_size FIXME DOC
  368. * @param p_stream the stream to write data to.
  369. * @param p_manager the user event manager.
  370. *
  371. * @return FIXME DOC
  372. */
  373. OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
  374. OPJ_UINT32 p_tile_index,
  375. OPJ_BYTE * p_data,
  376. OPJ_UINT32 p_data_size,
  377. opj_stream_private_t *p_stream,
  378. opj_event_mgr_t * p_manager);
  379. /**
  380. * Creates a jpeg2000 file decompressor.
  381. *
  382. * @return an empty jpeg2000 file codec.
  383. */
  384. opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder);
  385. /**
  386. Destroy a JP2 decompressor handle
  387. @param jp2 JP2 decompressor handle to destroy
  388. */
  389. void opj_jp2_destroy(opj_jp2_t *jp2);
  390. /**
  391. * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
  392. *
  393. * @param p_jp2 the jpeg2000 codec.
  394. * @param p_image FIXME DOC
  395. * @param p_start_x the left position of the rectangle to decode (in image coordinates).
  396. * @param p_start_y the up position of the rectangle to decode (in image coordinates).
  397. * @param p_end_x the right position of the rectangle to decode (in image coordinates).
  398. * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
  399. * @param p_manager the user event manager
  400. *
  401. * @return true if the area could be set.
  402. */
  403. OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
  404. opj_image_t* p_image,
  405. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  406. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  407. opj_event_mgr_t * p_manager);
  408. /**
  409. *
  410. */
  411. OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
  412. opj_stream_private_t *p_stream,
  413. opj_image_t* p_image,
  414. opj_event_mgr_t * p_manager,
  415. OPJ_UINT32 tile_index);
  416. /**
  417. *
  418. */
  419. OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
  420. OPJ_UINT32 res_factor,
  421. opj_event_mgr_t * p_manager);
  422. /**
  423. * Specify extra options for the encoder.
  424. *
  425. * @param p_jp2 the jpeg2000 codec.
  426. * @param p_options options
  427. * @param p_manager the user event manager
  428. *
  429. * @see opj_encoder_set_extra_options() for more details.
  430. */
  431. OPJ_BOOL opj_jp2_encoder_set_extra_options(
  432. opj_jp2_t *p_jp2,
  433. const char* const* p_options,
  434. opj_event_mgr_t * p_manager);
  435. /* TODO MSD: clean these 3 functions */
  436. /**
  437. * Dump some elements from the JP2 decompression structure .
  438. *
  439. *@param p_jp2 the jp2 codec.
  440. *@param flag flag to describe what elements are dump.
  441. *@param out_stream output stream where dump the elements.
  442. *
  443. */
  444. void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
  445. /**
  446. * Get the codestream info from a JPEG2000 codec.
  447. *
  448. *@param p_jp2 jp2 codec.
  449. *
  450. *@return the codestream information extract from the jpg2000 codec
  451. */
  452. opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2);
  453. /**
  454. * Get the codestream index from a JPEG2000 codec.
  455. *
  456. *@param p_jp2 jp2 codec.
  457. *
  458. *@return the codestream index extract from the jpg2000 codec
  459. */
  460. opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2);
  461. /*@}*/
  462. /*@}*/
  463. #endif /* OPJ_JP2_H */