jp2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
  33. /*@{*/
  34. /** @name Local static functions */
  35. /*@{*/
  36. /**
  37. Read box headers
  38. @param cinfo Codec context info
  39. @param cio Input stream
  40. @param box
  41. @return Returns true if successful, returns false otherwise
  42. */
  43. static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
  44. /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
  45. /**
  46. Read the IHDR box - Image Header box
  47. @param jp2 JP2 handle
  48. @param cio Input buffer stream
  49. @return Returns true if successful, returns false otherwise
  50. */
  51. static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  52. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  53. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  54. static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  55. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
  56. static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
  57. /**
  58. Write the FTYP box - File type box
  59. @param jp2 JP2 handle
  60. @param cio Output buffer stream
  61. */
  62. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  63. /**
  64. Read the FTYP box - File type box
  65. @param jp2 JP2 handle
  66. @param cio Input buffer stream
  67. @return Returns true if successful, returns false otherwise
  68. */
  69. static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  70. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
  71. static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
  72. static void jp2_write_jp(opj_cio_t *cio);
  73. /**
  74. Read the JP box - JPEG 2000 signature
  75. @param jp2 JP2 handle
  76. @param cio Input buffer stream
  77. @return Returns true if successful, returns false otherwise
  78. */
  79. static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
  80. /**
  81. Decode the structure of a JP2 file
  82. @param jp2 JP2 handle
  83. @param cio Input buffer stream
  84. @return Returns true if successful, returns false otherwise
  85. */
  86. static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
  87. /*@}*/
  88. /*@}*/
  89. /* ----------------------------------------------------------------------- */
  90. static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
  91. box->init_pos = cio_tell(cio);
  92. box->length = cio_read(cio, 4);
  93. box->type = cio_read(cio, 4);
  94. if (box->length == 1) {
  95. if (cio_read(cio, 4) != 0) {
  96. opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
  97. return false;
  98. }
  99. box->length = cio_read(cio, 4);
  100. if (box->length == 0)
  101. box->length = cio_numbytesleft(cio) + 12;
  102. }
  103. else if (box->length == 0) {
  104. box->length = cio_numbytesleft(cio) + 8;
  105. } else if (box->length < 0) {
  106. opj_event_msg(cinfo, EVT_ERROR, "Invalid, negative, size of box\n");
  107. return false;
  108. }
  109. return true;
  110. }
  111. #if 0
  112. static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
  113. unsigned int i;
  114. opj_jp2_box_t box;
  115. box.init_pos = cio_tell(cio);
  116. cio_skip(cio, 4);
  117. cio_write(cio, JP2_URL, 4); /* DBTL */
  118. cio_write(cio, 0, 1); /* VERS */
  119. cio_write(cio, 0, 3); /* FLAG */
  120. if(Idx_file) {
  121. for (i = 0; i < strlen(Idx_file); ++i) {
  122. cio_write(cio, Idx_file[i], 1);
  123. }
  124. }
  125. box.length = cio_tell(cio) - box.init_pos;
  126. cio_seek(cio, box.init_pos);
  127. cio_write(cio, box.length, 4); /* L */
  128. cio_seek(cio, box.init_pos + box.length);
  129. }
  130. #endif
  131. static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  132. opj_jp2_box_t box;
  133. opj_common_ptr cinfo = jp2->cinfo;
  134. jp2_read_boxhdr(cinfo, cio, &box);
  135. if (JP2_IHDR != box.type) {
  136. opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
  137. return false;
  138. }
  139. jp2->h = cio_read(cio, 4); /* HEIGHT */
  140. jp2->w = cio_read(cio, 4); /* WIDTH */
  141. jp2->numcomps = cio_read(cio, 2); /* NC */
  142. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  143. if (!jp2->comps) {
  144. opj_event_msg(cinfo, EVT_ERROR, "out of memory\n");
  145. return false;
  146. }
  147. jp2->bpc = cio_read(cio, 1); /* BPC */
  148. jp2->C = cio_read(cio, 1); /* C */
  149. jp2->UnkC = cio_read(cio, 1); /* UnkC */
  150. jp2->IPR = cio_read(cio, 1); /* IPR */
  151. if (cio_tell(cio) - box.init_pos != box.length) {
  152. opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
  153. return false;
  154. }
  155. return true;
  156. }
  157. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  158. opj_jp2_box_t box;
  159. box.init_pos = cio_tell(cio);
  160. cio_skip(cio, 4);
  161. cio_write(cio, JP2_IHDR, 4); /* IHDR */
  162. cio_write(cio, jp2->h, 4); /* HEIGHT */
  163. cio_write(cio, jp2->w, 4); /* WIDTH */
  164. cio_write(cio, jp2->numcomps, 2); /* NC */
  165. cio_write(cio, jp2->bpc, 1); /* BPC */
  166. cio_write(cio, jp2->C, 1); /* C : Always 7 */
  167. cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
  168. cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
  169. box.length = cio_tell(cio) - box.init_pos;
  170. cio_seek(cio, box.init_pos);
  171. cio_write(cio, box.length, 4); /* L */
  172. cio_seek(cio, box.init_pos + box.length);
  173. }
  174. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  175. unsigned int i;
  176. opj_jp2_box_t box;
  177. box.init_pos = cio_tell(cio);
  178. cio_skip(cio, 4);
  179. cio_write(cio, JP2_BPCC, 4); /* BPCC */
  180. for (i = 0; i < jp2->numcomps; ++i) {
  181. cio_write(cio, jp2->comps[i].bpcc, 1);
  182. }
  183. box.length = cio_tell(cio) - box.init_pos;
  184. cio_seek(cio, box.init_pos);
  185. cio_write(cio, box.length, 4); /* L */
  186. cio_seek(cio, box.init_pos + box.length);
  187. }
  188. static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  189. unsigned int i;
  190. opj_jp2_box_t box;
  191. opj_common_ptr cinfo = jp2->cinfo;
  192. jp2_read_boxhdr(cinfo, cio, &box);
  193. if (JP2_BPCC != box.type) {
  194. opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
  195. return false;
  196. }
  197. for (i = 0; i < jp2->numcomps; ++i) {
  198. jp2->comps[i].bpcc = cio_read(cio, 1);
  199. }
  200. if (cio_tell(cio) - box.init_pos != box.length) {
  201. opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
  202. return false;
  203. }
  204. return true;
  205. }
  206. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
  207. opj_jp2_box_t box;
  208. box.init_pos = cio_tell(cio);
  209. cio_skip(cio, 4);
  210. cio_write(cio, JP2_COLR, 4); /* COLR */
  211. cio_write(cio, jp2->meth, 1); /* METH */
  212. cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
  213. cio_write(cio, jp2->approx, 1); /* APPROX */
  214. if (jp2->meth == 1) {
  215. cio_write(cio, jp2->enumcs, 4); /* EnumCS */
  216. } else {
  217. cio_write(cio, 0, 1); /* PROFILE (??) */
  218. }
  219. box.length = cio_tell(cio) - box.init_pos;
  220. cio_seek(cio, box.init_pos);
  221. cio_write(cio, box.length, 4); /* L */
  222. cio_seek(cio, box.init_pos + box.length);
  223. }
  224. static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
  225. opj_jp2_box_t box;
  226. int skip_len;
  227. opj_common_ptr cinfo = jp2->cinfo;
  228. jp2_read_boxhdr(cinfo, cio, &box);
  229. do {
  230. if (JP2_COLR != box.type) {
  231. cio_skip(cio, box.length - 8);
  232. jp2_read_boxhdr(cinfo, cio, &box);
  233. }
  234. } while(JP2_COLR != box.type);
  235. jp2->meth = cio_read(cio, 1); /* METH */
  236. jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
  237. jp2->approx = cio_read(cio, 1); /* APPROX */
  238. if (jp2->meth == 1) {
  239. jp2->enumcs = cio_read(cio, 4); /* EnumCS */
  240. } else {
  241. /* skip PROFILE */
  242. skip_len = box.init_pos + box.length - cio_tell(cio);
  243. if (skip_len < 0) {
  244. opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
  245. return false;
  246. }
  247. cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
  248. }
  249. if (cio_tell(cio) - box.init_pos != box.length) {
  250. opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
  251. return false;
  252. }
  253. return true;
  254. }
  255. void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
  256. opj_jp2_box_t box;
  257. box.init_pos = cio_tell(cio);
  258. cio_skip(cio, 4);
  259. cio_write(cio, JP2_JP2H, 4); /* JP2H */
  260. jp2_write_ihdr(jp2, cio);
  261. if (jp2->bpc == 255) {
  262. jp2_write_bpcc(jp2, cio);
  263. }
  264. jp2_write_colr(jp2, cio);
  265. box.length = cio_tell(cio) - box.init_pos;
  266. cio_seek(cio, box.init_pos);
  267. cio_write(cio, box.length, 4); /* L */
  268. cio_seek(cio, box.init_pos + box.length);
  269. }
  270. bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
  271. opj_jp2_box_t box;
  272. int skip_len;
  273. opj_common_ptr cinfo = jp2->cinfo;
  274. jp2_read_boxhdr(cinfo, cio, &box);
  275. do {
  276. if (JP2_JP2H != box.type) {
  277. if (box.type == JP2_JP2C) {
  278. opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
  279. return false;
  280. }
  281. cio_skip(cio, box.length - 8);
  282. jp2_read_boxhdr(cinfo, cio, &box);
  283. }
  284. } while(JP2_JP2H != box.type);
  285. if (!jp2_read_ihdr(jp2, cio))
  286. return false;
  287. int curpos = cio_tell(cio);
  288. cio_seek(cio, box.init_pos);
  289. cio_skip(cio, box.length);
  290. if (cio_tell(cio) - box.init_pos != box.length) {
  291. opj_event_msg(cinfo, EVT_ERROR,
  292. "Box size exceeds size of codestream (expected: %d, real: %d)\n",
  293. box.length, (cio_tell(cio) - box.init_pos));
  294. return false;
  295. }
  296. cio_seek(cio, curpos);
  297. if (jp2->bpc == 255) {
  298. if (!jp2_read_bpcc(jp2, cio))
  299. return false;
  300. }
  301. if (!jp2_read_colr(jp2, cio))
  302. return false;
  303. skip_len = box.init_pos + box.length - cio_tell(cio);
  304. if (skip_len < 0) {
  305. opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
  306. return false;
  307. }
  308. cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
  309. return true;
  310. }
  311. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  312. unsigned int i;
  313. opj_jp2_box_t box;
  314. box.init_pos = cio_tell(cio);
  315. cio_skip(cio, 4);
  316. cio_write(cio, JP2_FTYP, 4); /* FTYP */
  317. cio_write(cio, jp2->brand, 4); /* BR */
  318. cio_write(cio, jp2->minversion, 4); /* MinV */
  319. for (i = 0; i < jp2->numcl; ++i) {
  320. cio_write(cio, jp2->cl[i], 4); /* CL */
  321. }
  322. box.length = cio_tell(cio) - box.init_pos;
  323. cio_seek(cio, box.init_pos);
  324. cio_write(cio, box.length, 4); /* L */
  325. cio_seek(cio, box.init_pos + box.length);
  326. }
  327. static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  328. int i;
  329. opj_jp2_box_t box;
  330. opj_common_ptr cinfo = jp2->cinfo;
  331. jp2_read_boxhdr(cinfo, cio, &box);
  332. if (JP2_FTYP != box.type) {
  333. opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
  334. return false;
  335. }
  336. jp2->brand = cio_read(cio, 4); /* BR */
  337. jp2->minversion = cio_read(cio, 4); /* MinV */
  338. jp2->numcl = (box.length - 16) / 4;
  339. jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
  340. if (!jp2->cl) {
  341. opj_event_msg(cinfo, EVT_ERROR, "out of memory\n");
  342. return false;
  343. }
  344. if (cio_numbytesleft(cio) < (int)jp2->numcl * 4) {
  345. opj_event_msg(cinfo, EVT_ERROR, "Not enough bytes in FTYP Box "
  346. "(expected %d, but only %d left)\n",
  347. (int)jp2->numcl * 4, cio_numbytesleft(cio));
  348. return false;
  349. }
  350. for (i = 0; i < (int)jp2->numcl; ++i) {
  351. jp2->cl[i] = cio_read(cio, 4); /* CLi */
  352. }
  353. if (cio_tell(cio) - box.init_pos != box.length) {
  354. opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
  355. return false;
  356. }
  357. return true;
  358. }
  359. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  360. unsigned int j2k_codestream_offset, j2k_codestream_length;
  361. opj_jp2_box_t box;
  362. opj_j2k_t *j2k = jp2->j2k;
  363. box.init_pos = cio_tell(cio);
  364. cio_skip(cio, 4);
  365. cio_write(cio, JP2_JP2C, 4); /* JP2C */
  366. /* J2K encoding */
  367. j2k_codestream_offset = cio_tell(cio);
  368. if(!j2k_encode(j2k, cio, image, cstr_info)) {
  369. opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
  370. return 0;
  371. }
  372. j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
  373. jp2->j2k_codestream_offset = j2k_codestream_offset;
  374. jp2->j2k_codestream_length = j2k_codestream_length;
  375. box.length = 8 + jp2->j2k_codestream_length;
  376. cio_seek(cio, box.init_pos);
  377. cio_write(cio, box.length, 4); /* L */
  378. cio_seek(cio, box.init_pos + box.length);
  379. return box.length;
  380. }
  381. static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
  382. opj_jp2_box_t box;
  383. opj_common_ptr cinfo = jp2->cinfo;
  384. jp2_read_boxhdr(cinfo, cio, &box);
  385. do {
  386. if(JP2_JP2C != box.type) {
  387. cio_skip(cio, box.length - 8);
  388. jp2_read_boxhdr(cinfo, cio, &box);
  389. }
  390. } while(JP2_JP2C != box.type);
  391. *j2k_codestream_offset = cio_tell(cio);
  392. *j2k_codestream_length = box.length - 8;
  393. return true;
  394. }
  395. static void jp2_write_jp(opj_cio_t *cio) {
  396. opj_jp2_box_t box;
  397. box.init_pos = cio_tell(cio);
  398. cio_skip(cio, 4);
  399. cio_write(cio, JP2_JP, 4); /* JP2 signature */
  400. cio_write(cio, 0x0d0a870a, 4);
  401. box.length = cio_tell(cio) - box.init_pos;
  402. cio_seek(cio, box.init_pos);
  403. cio_write(cio, box.length, 4); /* L */
  404. cio_seek(cio, box.init_pos + box.length);
  405. }
  406. static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
  407. opj_jp2_box_t box;
  408. opj_common_ptr cinfo = jp2->cinfo;
  409. jp2_read_boxhdr(cinfo, cio, &box);
  410. if (JP2_JP != box.type) {
  411. opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
  412. return false;
  413. }
  414. if (0x0d0a870a != cio_read(cio, 4)) {
  415. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
  416. return false;
  417. }
  418. if (cio_tell(cio) - box.init_pos != box.length) {
  419. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
  420. return false;
  421. }
  422. return true;
  423. }
  424. static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
  425. if (!jp2_read_jp(jp2, cio))
  426. return false;
  427. if (!jp2_read_ftyp(jp2, cio))
  428. return false;
  429. if (!jp2_read_jp2h(jp2, cio))
  430. return false;
  431. if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
  432. return false;
  433. return true;
  434. }
  435. /* ----------------------------------------------------------------------- */
  436. /* JP2 decoder interface */
  437. /* ----------------------------------------------------------------------- */
  438. opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
  439. opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
  440. if(jp2) {
  441. jp2->cinfo = cinfo;
  442. /* create the J2K codec */
  443. jp2->j2k = j2k_create_decompress(cinfo);
  444. if(jp2->j2k == NULL) {
  445. jp2_destroy_decompress(jp2);
  446. return NULL;
  447. }
  448. }
  449. return jp2;
  450. }
  451. void jp2_destroy_decompress(opj_jp2_t *jp2) {
  452. if(jp2) {
  453. /* destroy the J2K codec */
  454. j2k_destroy_decompress(jp2->j2k);
  455. if(jp2->comps) {
  456. opj_free(jp2->comps);
  457. }
  458. if(jp2->cl) {
  459. opj_free(jp2->cl);
  460. }
  461. opj_free(jp2);
  462. }
  463. }
  464. void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
  465. /* setup the J2K codec */
  466. j2k_setup_decoder(jp2->j2k, parameters);
  467. /* further JP2 initializations go here */
  468. }
  469. opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
  470. opj_common_ptr cinfo;
  471. opj_image_t *image = NULL;
  472. if(!jp2 || !cio) {
  473. return NULL;
  474. }
  475. cinfo = jp2->cinfo;
  476. /* JP2 decoding */
  477. if(!jp2_read_struct(jp2, cio)) {
  478. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
  479. return NULL;
  480. }
  481. /* J2K decoding */
  482. image = j2k_decode(jp2->j2k, cio, cstr_info);
  483. if(!image) {
  484. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
  485. return NULL;
  486. }
  487. /* Set Image Color Space */
  488. if (jp2->enumcs == 16)
  489. image->color_space = CLRSPC_SRGB;
  490. else if (jp2->enumcs == 17)
  491. image->color_space = CLRSPC_GRAY;
  492. else if (jp2->enumcs == 18)
  493. image->color_space = CLRSPC_SYCC;
  494. else
  495. image->color_space = CLRSPC_UNKNOWN;
  496. return image;
  497. }
  498. /* ----------------------------------------------------------------------- */
  499. /* JP2 encoder interface */
  500. /* ----------------------------------------------------------------------- */
  501. opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
  502. opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
  503. if(jp2) {
  504. jp2->cinfo = cinfo;
  505. /* create the J2K codec */
  506. jp2->j2k = j2k_create_compress(cinfo);
  507. if(jp2->j2k == NULL) {
  508. jp2_destroy_compress(jp2);
  509. return NULL;
  510. }
  511. }
  512. return jp2;
  513. }
  514. void jp2_destroy_compress(opj_jp2_t *jp2) {
  515. if(jp2) {
  516. /* destroy the J2K codec */
  517. j2k_destroy_compress(jp2->j2k);
  518. if(jp2->comps) {
  519. opj_free(jp2->comps);
  520. }
  521. if(jp2->cl) {
  522. opj_free(jp2->cl);
  523. }
  524. opj_free(jp2);
  525. }
  526. }
  527. void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
  528. int i;
  529. int depth_0, sign;
  530. if(!jp2 || !parameters || !image)
  531. return;
  532. /* setup the J2K codec */
  533. /* ------------------- */
  534. /* Check if number of components respects standard */
  535. if (image->numcomps < 1 || image->numcomps > 16384) {
  536. opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
  537. return;
  538. }
  539. j2k_setup_encoder(jp2->j2k, parameters, image);
  540. /* setup the JP2 codec */
  541. /* ------------------- */
  542. /* Profile box */
  543. jp2->brand = JP2_JP2; /* BR */
  544. jp2->minversion = 0; /* MinV */
  545. jp2->numcl = 1;
  546. jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
  547. if (!jp2->cl) {
  548. opj_event_msg(jp2->cinfo, EVT_ERROR, "out of memory\n");
  549. return;
  550. }
  551. jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
  552. /* Image Header box */
  553. jp2->numcomps = image->numcomps; /* NC */
  554. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  555. if (!jp2->comps) {
  556. opj_event_msg(jp2->cinfo, EVT_ERROR, "out of memory\n");
  557. return;
  558. }
  559. jp2->h = image->y1 - image->y0; /* HEIGHT */
  560. jp2->w = image->x1 - image->x0; /* WIDTH */
  561. /* BPC */
  562. depth_0 = image->comps[0].prec - 1;
  563. sign = image->comps[0].sgnd;
  564. jp2->bpc = depth_0 + (sign << 7);
  565. for (i = 1; i < image->numcomps; ++i) {
  566. int depth = image->comps[i].prec - 1;
  567. sign = image->comps[i].sgnd;
  568. if (depth_0 != depth)
  569. jp2->bpc = 255;
  570. }
  571. jp2->C = 7; /* C : Always 7 */
  572. jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
  573. jp2->IPR = 0; /* IPR, no intellectual property */
  574. /* BitsPerComponent box */
  575. for (i = 0; i < image->numcomps; ++i) {
  576. jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
  577. }
  578. /* Colour Specification box */
  579. if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
  580. jp2->meth = 1; /* METH: Enumerated colourspace */
  581. } else {
  582. jp2->meth = 2; /* METH: Restricted ICC profile */
  583. }
  584. if (jp2->meth == 1) {
  585. if (image->color_space == 1)
  586. jp2->enumcs = 16; /* sRGB as defined by IEC 6196621 */
  587. else if (image->color_space == 2)
  588. jp2->enumcs = 17; /* greyscale */
  589. else if (image->color_space == 3)
  590. jp2->enumcs = 18; /* YUV */
  591. } else {
  592. jp2->enumcs = 0; /* PROFILE (??) */
  593. }
  594. jp2->precedence = 0; /* PRECEDENCE */
  595. jp2->approx = 0; /* APPROX */
  596. }
  597. bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  598. /* JP2 encoding */
  599. /* JPEG 2000 Signature box */
  600. jp2_write_jp(cio);
  601. /* File Type box */
  602. jp2_write_ftyp(jp2, cio);
  603. /* JP2 Header box */
  604. jp2_write_jp2h(jp2, cio);
  605. /* J2K encoding */
  606. if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) {
  607. opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
  608. return false;
  609. }
  610. return true;
  611. }