jp2.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. * Copyright (c) 2010-2011, Kaori Hagihara
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. #include <assert.h>
  34. /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
  35. /*@{*/
  36. /** @name Local static functions */
  37. /*@{*/
  38. /**
  39. Read box headers
  40. @param cinfo Codec context info
  41. @param cio Input stream
  42. @param box
  43. @return Returns true if successful, returns false otherwise
  44. */
  45. static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
  46. /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
  47. /**
  48. Read the IHDR box - Image Header box
  49. @param jp2 JP2 handle
  50. @param cio Input buffer stream
  51. @return Returns true if successful, returns false otherwise
  52. */
  53. static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  54. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  55. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  56. static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  57. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
  58. /**
  59. Write the FTYP box - File type box
  60. @param jp2 JP2 handle
  61. @param cio Output buffer stream
  62. */
  63. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  64. /**
  65. Read the FTYP box - File type box
  66. @param jp2 JP2 handle
  67. @param cio Input buffer stream
  68. @return Returns true if successful, returns false otherwise
  69. */
  70. static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  71. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
  72. static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
  73. static void jp2_write_jp(opj_cio_t *cio);
  74. /**
  75. Read the JP box - JPEG 2000 signature
  76. @param jp2 JP2 handle
  77. @param cio Input buffer stream
  78. @return Returns true if successful, returns false otherwise
  79. */
  80. static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
  81. /**
  82. Decode the structure of a JP2 file
  83. @param jp2 JP2 handle
  84. @param cio Input buffer stream
  85. @param color Collector for profile, cdef and pclr data
  86. @return Returns true if successful, returns false otherwise
  87. */
  88. static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
  89. opj_jp2_color_t *color);
  90. /**
  91. Apply collected palette data
  92. @param color Collector for profile, cdef and pclr data
  93. @param image
  94. */
  95. static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo);
  96. /**
  97. Collect palette data
  98. @param jp2 JP2 handle
  99. @param cio Input buffer stream
  100. @param box
  101. @param color Collector for profile, cdef and pclr data
  102. @return Returns true if successful, returns false otherwise
  103. */
  104. static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
  105. opj_jp2_box_t *box, opj_jp2_color_t *color);
  106. /**
  107. Collect component mapping data
  108. @param jp2 JP2 handle
  109. @param cio Input buffer stream
  110. @param box
  111. @param color Collector for profile, cdef and pclr data
  112. @return Returns true if successful, returns false otherwise
  113. */
  114. static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
  115. opj_jp2_box_t *box, opj_jp2_color_t *color);
  116. /**
  117. Collect colour specification data
  118. @param jp2 JP2 handle
  119. @param cio Input buffer stream
  120. @param box
  121. @param color Collector for profile, cdef and pclr data
  122. @return Returns true if successful, returns false otherwise
  123. */
  124. static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
  125. opj_jp2_box_t *box, opj_jp2_color_t *color);
  126. /**
  127. Write file Index (superbox)
  128. @param[in] offset_jp2c offset of jp2c box
  129. @param[in] length_jp2c length of jp2c box
  130. @param[in] offset_idx offset of cidx box
  131. @param[in] length_idx length of cidx box
  132. @param[in] cio file output handle
  133. @return length of fidx box
  134. */
  135. static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
  136. /**
  137. Write index Finder box
  138. @param[in] offset offset of fidx box
  139. @param[in] length length of fidx box
  140. @param[in] cio file output handle
  141. */
  142. static void write_iptr( int offset, int length, opj_cio_t *cio);
  143. /**
  144. Write proxy box
  145. @param[in] offset_jp2c offset of jp2c box
  146. @param[in] length_jp2c length of jp2c box
  147. @param[in] offset_idx offset of cidx box
  148. @param[in] length_idx length of cidx box
  149. @param[in] cio file output handle
  150. */
  151. static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
  152. /*@}*/
  153. /*@}*/
  154. /* ----------------------------------------------------------------------- */
  155. static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
  156. box->init_pos = cio_tell(cio);
  157. box->length = cio_read(cio, 4);
  158. box->type = cio_read(cio, 4);
  159. if (box->length == 1) {
  160. if (cio_read(cio, 4) != 0) {
  161. opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
  162. return OPJ_FALSE;
  163. }
  164. box->length = cio_read(cio, 4);
  165. if (box->length == 0)
  166. box->length = cio_numbytesleft(cio) + 12;
  167. }
  168. else if (box->length == 0) {
  169. box->length = cio_numbytesleft(cio) + 8;
  170. } else if (box->length < 0) {
  171. opj_event_msg(cinfo, EVT_ERROR, "Integer overflow in box->length\n");
  172. return OPJ_FALSE; /* TODO: actually check jp2_read_boxhdr's return value */
  173. }
  174. return OPJ_TRUE;
  175. }
  176. #if 0
  177. static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
  178. unsigned int i;
  179. opj_jp2_box_t box;
  180. box.init_pos = cio_tell(cio);
  181. cio_skip(cio, 4);
  182. cio_write(cio, JP2_URL, 4); /* DBTL */
  183. cio_write(cio, 0, 1); /* VERS */
  184. cio_write(cio, 0, 3); /* FLAG */
  185. if(Idx_file) {
  186. for (i = 0; i < strlen(Idx_file); i++) {
  187. cio_write(cio, Idx_file[i], 1);
  188. }
  189. }
  190. box.length = cio_tell(cio) - box.init_pos;
  191. cio_seek(cio, box.init_pos);
  192. cio_write(cio, box.length, 4); /* L */
  193. cio_seek(cio, box.init_pos + box.length);
  194. }
  195. #endif
  196. static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  197. opj_jp2_box_t box;
  198. opj_common_ptr cinfo = jp2->cinfo;
  199. if(jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
  200. opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
  201. return OPJ_FALSE;
  202. }
  203. if (JP2_IHDR != box.type) {
  204. opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
  205. return OPJ_FALSE;
  206. }
  207. jp2->h = cio_read(cio, 4); /* HEIGHT */
  208. jp2->w = cio_read(cio, 4); /* WIDTH */
  209. jp2->numcomps = cio_read(cio, 2); /* NC */
  210. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  211. jp2->bpc = cio_read(cio, 1); /* BPC */
  212. jp2->C = cio_read(cio, 1); /* C */
  213. jp2->UnkC = cio_read(cio, 1); /* UnkC */
  214. jp2->IPR = cio_read(cio, 1); /* IPR */
  215. if (cio_tell(cio) - box.init_pos != box.length) {
  216. opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
  217. return OPJ_FALSE;
  218. }
  219. return OPJ_TRUE;
  220. }
  221. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  222. opj_jp2_box_t box;
  223. box.init_pos = cio_tell(cio);
  224. cio_skip(cio, 4);
  225. cio_write(cio, JP2_IHDR, 4); /* IHDR */
  226. cio_write(cio, jp2->h, 4); /* HEIGHT */
  227. cio_write(cio, jp2->w, 4); /* WIDTH */
  228. cio_write(cio, jp2->numcomps, 2); /* NC */
  229. cio_write(cio, jp2->bpc, 1); /* BPC */
  230. cio_write(cio, jp2->C, 1); /* C : Always 7 */
  231. cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
  232. cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
  233. box.length = cio_tell(cio) - box.init_pos;
  234. cio_seek(cio, box.init_pos);
  235. cio_write(cio, box.length, 4); /* L */
  236. cio_seek(cio, box.init_pos + box.length);
  237. }
  238. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  239. unsigned int i;
  240. opj_jp2_box_t box;
  241. box.init_pos = cio_tell(cio);
  242. cio_skip(cio, 4);
  243. cio_write(cio, JP2_BPCC, 4); /* BPCC */
  244. for (i = 0; i < jp2->numcomps; i++) {
  245. cio_write(cio, jp2->comps[i].bpcc, 1);
  246. }
  247. box.length = cio_tell(cio) - box.init_pos;
  248. cio_seek(cio, box.init_pos);
  249. cio_write(cio, box.length, 4); /* L */
  250. cio_seek(cio, box.init_pos + box.length);
  251. }
  252. static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  253. unsigned int i;
  254. opj_jp2_box_t box;
  255. opj_common_ptr cinfo = jp2->cinfo;
  256. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
  257. opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
  258. return OPJ_FALSE;
  259. }
  260. if (JP2_BPCC != box.type) {
  261. opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
  262. return OPJ_FALSE;
  263. }
  264. for (i = 0; i < jp2->numcomps; i++) {
  265. jp2->comps[i].bpcc = cio_read(cio, 1);
  266. }
  267. if (cio_tell(cio) - box.init_pos != box.length) {
  268. opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
  269. return OPJ_FALSE;
  270. }
  271. return OPJ_TRUE;
  272. }
  273. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
  274. opj_jp2_box_t box;
  275. box.init_pos = cio_tell(cio);
  276. cio_skip(cio, 4);
  277. cio_write(cio, JP2_COLR, 4); /* COLR */
  278. cio_write(cio, jp2->meth, 1); /* METH */
  279. cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
  280. cio_write(cio, jp2->approx, 1); /* APPROX */
  281. if(jp2->meth == 2)
  282. jp2->enumcs = 0;
  283. cio_write(cio, jp2->enumcs, 4); /* EnumCS */
  284. box.length = cio_tell(cio) - box.init_pos;
  285. cio_seek(cio, box.init_pos);
  286. cio_write(cio, box.length, 4); /* L */
  287. cio_seek(cio, box.init_pos + box.length);
  288. }
  289. static void jp2_free_pclr(opj_jp2_color_t *color)
  290. {
  291. opj_free(color->jp2_pclr->channel_sign);
  292. opj_free(color->jp2_pclr->channel_size);
  293. opj_free(color->jp2_pclr->entries);
  294. if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
  295. opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
  296. }
  297. static void free_color_data(opj_jp2_color_t *color)
  298. {
  299. if(color->jp2_pclr)
  300. {
  301. jp2_free_pclr(color);
  302. }
  303. if(color->jp2_cdef)
  304. {
  305. if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
  306. opj_free(color->jp2_cdef);
  307. }
  308. if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
  309. }
  310. static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo)
  311. {
  312. opj_image_comp_t *old_comps, *new_comps;
  313. unsigned char *channel_size, *channel_sign;
  314. unsigned int *entries;
  315. opj_jp2_cmap_comp_t *cmap;
  316. int *src, *dst;
  317. unsigned int j, max;
  318. unsigned short i, nr_channels, cmp, pcol;
  319. int k, top_k;
  320. channel_size = color->jp2_pclr->channel_size;
  321. channel_sign = color->jp2_pclr->channel_sign;
  322. entries = color->jp2_pclr->entries;
  323. cmap = color->jp2_pclr->cmap;
  324. nr_channels = color->jp2_pclr->nr_channels;
  325. old_comps = image->comps;
  326. new_comps = (opj_image_comp_t*)
  327. opj_malloc(nr_channels * sizeof(opj_image_comp_t));
  328. for(i = 0; i < nr_channels; ++i)
  329. {
  330. pcol = cmap[i].pcol; cmp = cmap[i].cmp;
  331. if( pcol < nr_channels )
  332. new_comps[pcol] = old_comps[cmp];
  333. else
  334. {
  335. opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value %d (max: %d). skipping\n", pcol, nr_channels);
  336. continue;
  337. }
  338. if(cmap[i].mtyp == 0) /* Direct use */
  339. {
  340. old_comps[cmp].data = NULL; continue;
  341. }
  342. /* Palette mapping: */
  343. new_comps[pcol].data = (int*)
  344. opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
  345. new_comps[pcol].prec = channel_size[i];
  346. new_comps[pcol].sgnd = channel_sign[i];
  347. }
  348. top_k = color->jp2_pclr->nr_entries - 1;
  349. for(i = 0; i < nr_channels; ++i)
  350. {
  351. /* Direct use: */
  352. if(cmap[i].mtyp == 0) continue;
  353. /* Palette mapping: */
  354. cmp = cmap[i].cmp; pcol = cmap[i].pcol;
  355. src = old_comps[cmp].data;
  356. dst = new_comps[pcol].data;
  357. max = new_comps[pcol].w * new_comps[pcol].h;
  358. for(j = 0; j < max; ++j)
  359. {
  360. /* The index */
  361. if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
  362. /* The colour */
  363. dst[j] = entries[k * nr_channels + pcol];
  364. }
  365. }
  366. max = image->numcomps;
  367. for(i = 0; i < max; ++i)
  368. {
  369. if(old_comps[i].data) opj_free(old_comps[i].data);
  370. }
  371. opj_free(old_comps);
  372. image->comps = new_comps;
  373. image->numcomps = nr_channels;
  374. jp2_free_pclr(color);
  375. }/* apply_pclr() */
  376. static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
  377. opj_jp2_box_t *box, opj_jp2_color_t *color)
  378. {
  379. opj_jp2_pclr_t *jp2_pclr;
  380. unsigned char *channel_size, *channel_sign;
  381. unsigned int *entries;
  382. unsigned short nr_entries, nr_channels;
  383. unsigned short i, j;
  384. unsigned char uc;
  385. OPJ_ARG_NOT_USED(box);
  386. OPJ_ARG_NOT_USED(jp2);
  387. /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
  388. * a JP2 Header box' :
  389. */
  390. if(color->jp2_pclr) return OPJ_FALSE;
  391. nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
  392. nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
  393. entries = (unsigned int*)
  394. opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
  395. channel_size = (unsigned char*)opj_malloc(nr_channels);
  396. channel_sign = (unsigned char*)opj_malloc(nr_channels);
  397. jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
  398. jp2_pclr->channel_sign = channel_sign;
  399. jp2_pclr->channel_size = channel_size;
  400. jp2_pclr->entries = entries;
  401. jp2_pclr->nr_entries = nr_entries;
  402. jp2_pclr->nr_channels = nr_channels;
  403. jp2_pclr->cmap = NULL;
  404. color->jp2_pclr = jp2_pclr;
  405. for(i = 0; i < nr_channels; ++i)
  406. {
  407. uc = cio_read(cio, 1); /* Bi */
  408. channel_size[i] = (uc & 0x7f) + 1;
  409. channel_sign[i] = (uc & 0x80)?1:0;
  410. }
  411. for(j = 0; j < nr_entries; ++j)
  412. {
  413. for(i = 0; i < nr_channels; ++i)
  414. {
  415. /* Cji */
  416. *entries++ = cio_read(cio, (channel_size[i]+7)>>3);
  417. }
  418. }
  419. return OPJ_TRUE;
  420. }/* jp2_read_pclr() */
  421. static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
  422. opj_jp2_box_t *box, opj_jp2_color_t *color)
  423. {
  424. opj_jp2_cmap_comp_t *cmap;
  425. unsigned short i, nr_channels;
  426. OPJ_ARG_NOT_USED(box);
  427. OPJ_ARG_NOT_USED(jp2);
  428. /* Need nr_channels: */
  429. if(color->jp2_pclr == NULL) return OPJ_FALSE;
  430. /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
  431. * inside a JP2 Header box' :
  432. */
  433. if(color->jp2_pclr->cmap) return OPJ_FALSE;
  434. nr_channels = color->jp2_pclr->nr_channels;
  435. cmap = (opj_jp2_cmap_comp_t*)
  436. opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
  437. for(i = 0; i < nr_channels; ++i)
  438. {
  439. cmap[i].cmp = (unsigned short)cio_read(cio, 2);
  440. cmap[i].mtyp = cio_read(cio, 1);
  441. cmap[i].pcol = cio_read(cio, 1);
  442. }
  443. color->jp2_pclr->cmap = cmap;
  444. return OPJ_TRUE;
  445. }/* jp2_read_cmap() */
  446. static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
  447. {
  448. opj_jp2_cdef_info_t *info;
  449. unsigned short i, n, cn, typ, asoc, acn;
  450. info = color->jp2_cdef->info;
  451. n = color->jp2_cdef->n;
  452. for(i = 0; i < n; ++i)
  453. {
  454. /* WATCH: acn = asoc - 1 ! */
  455. if((asoc = info[i].asoc) == 0) continue;
  456. cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
  457. (void)typ;
  458. if(cn != acn)
  459. {
  460. opj_image_comp_t saved;
  461. memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
  462. memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
  463. memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
  464. info[i].asoc = cn + 1;
  465. info[acn].asoc = info[acn].cn + 1;
  466. }
  467. }
  468. if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
  469. opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
  470. }/* jp2_apply_cdef() */
  471. static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
  472. opj_jp2_box_t *box, opj_jp2_color_t *color)
  473. {
  474. opj_jp2_cdef_info_t *info;
  475. unsigned short i, n;
  476. OPJ_ARG_NOT_USED(box);
  477. OPJ_ARG_NOT_USED(jp2);
  478. /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
  479. * inside a JP2 Header box.'
  480. */
  481. if(color->jp2_cdef) return OPJ_FALSE;
  482. if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
  483. info = (opj_jp2_cdef_info_t*)
  484. opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
  485. color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
  486. color->jp2_cdef->info = info;
  487. color->jp2_cdef->n = n;
  488. for(i = 0; i < n; ++i)
  489. {
  490. info[i].cn = (unsigned short)cio_read(cio, 2);
  491. info[i].typ = (unsigned short)cio_read(cio, 2);
  492. info[i].asoc = (unsigned short)cio_read(cio, 2);
  493. }
  494. return OPJ_TRUE;
  495. }/* jp2_read_cdef() */
  496. static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
  497. opj_jp2_box_t *box, opj_jp2_color_t *color)
  498. {
  499. int skip_len;
  500. opj_common_ptr cinfo;
  501. /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
  502. * Specification boxes after the first.'
  503. */
  504. if(color->jp2_has_colr) return OPJ_FALSE;
  505. cinfo = jp2->cinfo;
  506. jp2->meth = cio_read(cio, 1); /* METH */
  507. jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
  508. jp2->approx = cio_read(cio, 1); /* APPROX */
  509. if (jp2->meth == 1)
  510. {
  511. jp2->enumcs = cio_read(cio, 4); /* EnumCS */
  512. }
  513. else
  514. if (jp2->meth == 2)
  515. {
  516. /* skip PROFILE */
  517. skip_len = box->init_pos + box->length - cio_tell(cio);
  518. if (skip_len < 0)
  519. {
  520. opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
  521. return OPJ_FALSE;
  522. }
  523. if(skip_len > 0)
  524. {
  525. unsigned char *start;
  526. start = cio_getbp(cio);
  527. color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
  528. color->icc_profile_len = skip_len;
  529. cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
  530. memcpy(color->icc_profile_buf, start, skip_len);
  531. }
  532. }
  533. if (cio_tell(cio) - box->init_pos != box->length)
  534. {
  535. opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
  536. return OPJ_FALSE;
  537. }
  538. color->jp2_has_colr = 1;
  539. return OPJ_TRUE;
  540. }/* jp2_read_colr() */
  541. opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
  542. {
  543. opj_jp2_box_t box;
  544. int jp2h_end;
  545. opj_common_ptr cinfo = jp2->cinfo;
  546. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  547. do {
  548. if (JP2_JP2H != box.type)
  549. {
  550. if (box.type == JP2_JP2C)
  551. {
  552. opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
  553. return OPJ_FALSE;
  554. }
  555. if (box.length <= 8) return OPJ_FALSE;
  556. cio_skip(cio, box.length - 8);
  557. if(cio->bp >= cio->end) return OPJ_FALSE;
  558. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  559. }
  560. } while(JP2_JP2H != box.type);
  561. if (!jp2_read_ihdr(jp2, cio))
  562. return OPJ_FALSE;
  563. jp2h_end = box.init_pos + box.length;
  564. if (jp2->bpc == 255)
  565. {
  566. if (!jp2_read_bpcc(jp2, cio))
  567. return OPJ_FALSE;
  568. }
  569. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  570. while(cio_tell(cio) < jp2h_end)
  571. {
  572. if(box.type == JP2_COLR)
  573. {
  574. if( !jp2_read_colr(jp2, cio, &box, color))
  575. {
  576. if (box.length <= 8) return OPJ_FALSE;
  577. cio_seek(cio, box.init_pos + 8);
  578. cio_skip(cio, box.length - 8);
  579. }
  580. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  581. continue;
  582. }
  583. if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
  584. {
  585. if( !jp2_read_cdef(jp2, cio, &box, color))
  586. {
  587. if (box.length <= 8) return OPJ_FALSE;
  588. cio_seek(cio, box.init_pos + 8);
  589. cio_skip(cio, box.length - 8);
  590. }
  591. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  592. continue;
  593. }
  594. if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
  595. {
  596. if( !jp2_read_pclr(jp2, cio, &box, color))
  597. {
  598. if (box.length <= 8) return OPJ_FALSE;
  599. cio_seek(cio, box.init_pos + 8);
  600. cio_skip(cio, box.length - 8);
  601. }
  602. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  603. continue;
  604. }
  605. if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
  606. {
  607. if( !jp2_read_cmap(jp2, cio, &box, color))
  608. {
  609. if (box.length <= 8) return OPJ_FALSE;
  610. cio_seek(cio, box.init_pos + 8);
  611. cio_skip(cio, box.length - 8);
  612. }
  613. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  614. continue;
  615. }
  616. if (box.length <= 8) return OPJ_FALSE;
  617. cio_seek(cio, box.init_pos + 8);
  618. cio_skip(cio, box.length - 8);
  619. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  620. }/* while(cio_tell(cio) < box_end) */
  621. cio_seek(cio, jp2h_end);
  622. /* Part 1, I.5.3.3 : 'must contain at least one' */
  623. return (color->jp2_has_colr == 1);
  624. }/* jp2_read_jp2h() */
  625. static opj_bool opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_common_ptr cinfo)
  626. {
  627. int i;
  628. /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
  629. if (color->jp2_cdef) {
  630. opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
  631. int n = color->jp2_cdef->n;
  632. for (i = 0; i < n; i++) {
  633. if (info[i].cn >= image->numcomps) {
  634. opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, image->numcomps);
  635. return OPJ_FALSE;
  636. }
  637. if (info[i].asoc > 0 && (info[i].asoc - 1) >= image->numcomps) {
  638. opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, image->numcomps);
  639. return OPJ_FALSE;
  640. }
  641. }
  642. }
  643. /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
  644. 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
  645. if (color->jp2_pclr && color->jp2_pclr->cmap) {
  646. int nr_channels = color->jp2_pclr->nr_channels;
  647. opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
  648. opj_bool *pcol_usage, is_sane = OPJ_TRUE;
  649. /* verify that all original components match an existing one */
  650. for (i = 0; i < nr_channels; i++) {
  651. if (cmap[i].cmp >= image->numcomps) {
  652. opj_event_msg(cinfo, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
  653. is_sane = OPJ_FALSE;
  654. }
  655. }
  656. pcol_usage = opj_calloc(nr_channels, sizeof(opj_bool));
  657. if (!pcol_usage) {
  658. opj_event_msg(cinfo, EVT_ERROR, "Unexpected OOM.\n");
  659. return OPJ_FALSE;
  660. }
  661. /* verify that no component is targeted more than once */
  662. for (i = 0; i < nr_channels; i++) {
  663. int pcol = cmap[i].pcol;
  664. assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
  665. if (pcol >= nr_channels) {
  666. opj_event_msg(cinfo, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
  667. is_sane = OPJ_FALSE;
  668. }
  669. else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
  670. opj_event_msg(cinfo, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
  671. is_sane = OPJ_FALSE;
  672. }
  673. else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
  674. /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
  675. * the value of this field shall be 0. */
  676. opj_event_msg(cinfo, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
  677. is_sane = OPJ_FALSE;
  678. }
  679. else
  680. pcol_usage[pcol] = OPJ_TRUE;
  681. }
  682. /* verify that all components are targeted at least once */
  683. for (i = 0; i < nr_channels; i++) {
  684. if (!pcol_usage[i] && cmap[i].mtyp != 0) {
  685. opj_event_msg(cinfo, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
  686. is_sane = OPJ_FALSE;
  687. }
  688. }
  689. opj_free(pcol_usage);
  690. if (!is_sane) {
  691. return OPJ_FALSE;
  692. }
  693. }
  694. return OPJ_TRUE;
  695. }
  696. opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
  697. opj_codestream_info_t *cstr_info)
  698. {
  699. opj_common_ptr cinfo;
  700. opj_image_t *image = NULL;
  701. opj_jp2_color_t color;
  702. if(!jp2 || !cio)
  703. {
  704. return NULL;
  705. }
  706. memset(&color, 0, sizeof(opj_jp2_color_t));
  707. cinfo = jp2->cinfo;
  708. /* JP2 decoding */
  709. if(!jp2_read_struct(jp2, cio, &color))
  710. {
  711. free_color_data(&color);
  712. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
  713. return NULL;
  714. }
  715. /* J2K decoding */
  716. image = j2k_decode(jp2->j2k, cio, cstr_info);
  717. if(!image)
  718. {
  719. free_color_data(&color);
  720. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
  721. return NULL;
  722. }
  723. if (!jp2->ignore_pclr_cmap_cdef){
  724. if (!opj_jp2_check_color(image, &color, cinfo)) {
  725. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode PCRL box\n");
  726. return NULL;
  727. }
  728. /* Set Image Color Space */
  729. if (jp2->enumcs == 16)
  730. image->color_space = CLRSPC_SRGB;
  731. else if (jp2->enumcs == 17)
  732. image->color_space = CLRSPC_GRAY;
  733. else if (jp2->enumcs == 18)
  734. image->color_space = CLRSPC_SYCC;
  735. else
  736. image->color_space = CLRSPC_UNKNOWN;
  737. if(color.jp2_cdef)
  738. {
  739. jp2_apply_cdef(image, &color);
  740. }
  741. if(color.jp2_pclr)
  742. {
  743. /* Part 1, I.5.3.4: Either both or none : */
  744. if( !color.jp2_pclr->cmap)
  745. jp2_free_pclr(&color);
  746. else
  747. jp2_apply_pclr(&color, image, cinfo);
  748. }
  749. if(color.icc_profile_buf)
  750. {
  751. image->icc_profile_buf = color.icc_profile_buf;
  752. color.icc_profile_buf = NULL;
  753. image->icc_profile_len = color.icc_profile_len;
  754. }
  755. }
  756. return image;
  757. }/* opj_jp2_decode() */
  758. void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
  759. opj_jp2_box_t box;
  760. box.init_pos = cio_tell(cio);
  761. cio_skip(cio, 4);
  762. cio_write(cio, JP2_JP2H, 4); /* JP2H */
  763. jp2_write_ihdr(jp2, cio);
  764. if (jp2->bpc == 255) {
  765. jp2_write_bpcc(jp2, cio);
  766. }
  767. jp2_write_colr(jp2, cio);
  768. box.length = cio_tell(cio) - box.init_pos;
  769. cio_seek(cio, box.init_pos);
  770. cio_write(cio, box.length, 4); /* L */
  771. cio_seek(cio, box.init_pos + box.length);
  772. }
  773. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  774. unsigned int i;
  775. opj_jp2_box_t box;
  776. box.init_pos = cio_tell(cio);
  777. cio_skip(cio, 4);
  778. cio_write(cio, JP2_FTYP, 4); /* FTYP */
  779. cio_write(cio, jp2->brand, 4); /* BR */
  780. cio_write(cio, jp2->minversion, 4); /* MinV */
  781. for (i = 0; i < jp2->numcl; i++) {
  782. cio_write(cio, jp2->cl[i], 4); /* CL */
  783. }
  784. box.length = cio_tell(cio) - box.init_pos;
  785. cio_seek(cio, box.init_pos);
  786. cio_write(cio, box.length, 4); /* L */
  787. cio_seek(cio, box.init_pos + box.length);
  788. }
  789. static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  790. int i;
  791. opj_jp2_box_t box;
  792. opj_common_ptr cinfo = jp2->cinfo;
  793. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
  794. opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
  795. return OPJ_FALSE;
  796. }
  797. if (JP2_FTYP != box.type) {
  798. opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
  799. return OPJ_FALSE;
  800. }
  801. jp2->brand = cio_read(cio, 4); /* BR */
  802. jp2->minversion = cio_read(cio, 4); /* MinV */
  803. jp2->numcl = (box.length - 16) / 4;
  804. /* edf_c2_1673169.jp2 */
  805. if (cio_numbytesleft(cio) < ((int)jp2->numcl * 4)) {
  806. opj_event_msg(cinfo, EVT_ERROR, "Not enough bytes in FTYP Box "
  807. "(expected %d, but only %d left)\n",
  808. ((int)jp2->numcl * 4), cio_numbytesleft(cio));
  809. return OPJ_FALSE;
  810. }
  811. jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
  812. for (i = 0; i < (int)jp2->numcl; i++) {
  813. jp2->cl[i] = cio_read(cio, 4); /* CLi */
  814. }
  815. if (cio_tell(cio) - box.init_pos != box.length) {
  816. opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
  817. return OPJ_FALSE;
  818. }
  819. return OPJ_TRUE;
  820. }
  821. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  822. unsigned int j2k_codestream_offset, j2k_codestream_length;
  823. opj_jp2_box_t box;
  824. opj_j2k_t *j2k = jp2->j2k;
  825. box.init_pos = cio_tell(cio);
  826. cio_skip(cio, 4);
  827. cio_write(cio, JP2_JP2C, 4); /* JP2C */
  828. /* J2K encoding */
  829. j2k_codestream_offset = cio_tell(cio);
  830. if(!j2k_encode(j2k, cio, image, cstr_info)) {
  831. opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
  832. return 0;
  833. }
  834. j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
  835. jp2->j2k_codestream_offset = j2k_codestream_offset;
  836. jp2->j2k_codestream_length = j2k_codestream_length;
  837. box.length = 8 + jp2->j2k_codestream_length;
  838. cio_seek(cio, box.init_pos);
  839. cio_write(cio, box.length, 4); /* L */
  840. cio_seek(cio, box.init_pos + box.length);
  841. return box.length;
  842. }
  843. static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
  844. opj_jp2_box_t box;
  845. opj_common_ptr cinfo = jp2->cinfo;
  846. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
  847. opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
  848. return OPJ_FALSE;
  849. }
  850. do {
  851. if(JP2_JP2C != box.type) {
  852. if (box.length <= 8) return OPJ_FALSE;
  853. cio_skip(cio, box.length - 8);
  854. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
  855. }
  856. } while(JP2_JP2C != box.type);
  857. *j2k_codestream_offset = cio_tell(cio);
  858. if (box.length <= 8) return OPJ_FALSE;
  859. *j2k_codestream_length = box.length - 8;
  860. return OPJ_TRUE;
  861. }
  862. static void jp2_write_jp(opj_cio_t *cio) {
  863. opj_jp2_box_t box;
  864. box.init_pos = cio_tell(cio);
  865. cio_skip(cio, 4);
  866. cio_write(cio, JP2_JP, 4); /* JP2 signature */
  867. cio_write(cio, 0x0d0a870a, 4);
  868. box.length = cio_tell(cio) - box.init_pos;
  869. cio_seek(cio, box.init_pos);
  870. cio_write(cio, box.length, 4); /* L */
  871. cio_seek(cio, box.init_pos + box.length);
  872. }
  873. static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
  874. opj_jp2_box_t box;
  875. opj_common_ptr cinfo = jp2->cinfo;
  876. if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
  877. opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
  878. return OPJ_FALSE;
  879. }
  880. if (JP2_JP != box.type) {
  881. opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
  882. return OPJ_FALSE;
  883. }
  884. if (0x0d0a870a != cio_read(cio, 4)) {
  885. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
  886. return OPJ_FALSE;
  887. }
  888. if (cio_tell(cio) - box.init_pos != box.length) {
  889. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
  890. return OPJ_FALSE;
  891. }
  892. return OPJ_TRUE;
  893. }
  894. static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
  895. opj_jp2_color_t *color) {
  896. if (!jp2_read_jp(jp2, cio))
  897. return OPJ_FALSE;
  898. if (!jp2_read_ftyp(jp2, cio))
  899. return OPJ_FALSE;
  900. if (!jp2_read_jp2h(jp2, cio, color))
  901. return OPJ_FALSE;
  902. if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
  903. return OPJ_FALSE;
  904. return OPJ_TRUE;
  905. }
  906. static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
  907. {
  908. int len, lenp;
  909. lenp = cio_tell( cio);
  910. cio_skip( cio, 4); /* L [at the end] */
  911. cio_write( cio, JPIP_FIDX, 4); /* IPTR */
  912. write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
  913. len = cio_tell( cio)-lenp;
  914. cio_seek( cio, lenp);
  915. cio_write( cio, len, 4); /* L */
  916. cio_seek( cio, lenp+len);
  917. return len;
  918. }
  919. static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
  920. {
  921. int len, lenp;
  922. lenp = cio_tell( cio);
  923. cio_skip( cio, 4); /* L [at the end] */
  924. cio_write( cio, JPIP_PRXY, 4); /* IPTR */
  925. cio_write( cio, offset_jp2c, 8); /* OOFF */
  926. cio_write( cio, length_jp2c, 4); /* OBH part 1 */
  927. cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
  928. cio_write( cio, 1,1); /* NI */
  929. cio_write( cio, offset_idx, 8); /* IOFF */
  930. cio_write( cio, length_idx, 4); /* IBH part 1 */
  931. cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
  932. len = cio_tell( cio)-lenp;
  933. cio_seek( cio, lenp);
  934. cio_write( cio, len, 4); /* L */
  935. cio_seek( cio, lenp+len);
  936. }
  937. static void write_iptr( int offset, int length, opj_cio_t *cio)
  938. {
  939. int len, lenp;
  940. lenp = cio_tell( cio);
  941. cio_skip( cio, 4); /* L [at the end] */
  942. cio_write( cio, JPIP_IPTR, 4); /* IPTR */
  943. cio_write( cio, offset, 8);
  944. cio_write( cio, length, 8);
  945. len = cio_tell( cio)-lenp;
  946. cio_seek( cio, lenp);
  947. cio_write( cio, len, 4); /* L */
  948. cio_seek( cio, lenp+len);
  949. }
  950. /* ----------------------------------------------------------------------- */
  951. /* JP2 decoder interface */
  952. /* ----------------------------------------------------------------------- */
  953. opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
  954. opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
  955. if(jp2) {
  956. jp2->cinfo = cinfo;
  957. /* create the J2K codec */
  958. jp2->j2k = j2k_create_decompress(cinfo);
  959. if(jp2->j2k == NULL) {
  960. jp2_destroy_decompress(jp2);
  961. return NULL;
  962. }
  963. }
  964. return jp2;
  965. }
  966. void jp2_destroy_decompress(opj_jp2_t *jp2) {
  967. if(jp2) {
  968. /* destroy the J2K codec */
  969. j2k_destroy_decompress(jp2->j2k);
  970. if(jp2->comps) {
  971. opj_free(jp2->comps);
  972. }
  973. if(jp2->cl) {
  974. opj_free(jp2->cl);
  975. }
  976. opj_free(jp2);
  977. }
  978. }
  979. void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
  980. /* setup the J2K codec */
  981. j2k_setup_decoder(jp2->j2k, parameters);
  982. /* further JP2 initializations go here */
  983. jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
  984. }
  985. /* ----------------------------------------------------------------------- */
  986. /* JP2 encoder interface */
  987. /* ----------------------------------------------------------------------- */
  988. opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
  989. opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
  990. if(jp2) {
  991. memset(jp2, 0, sizeof(opj_jp2_t));
  992. jp2->cinfo = cinfo;
  993. /* create the J2K codec */
  994. jp2->j2k = j2k_create_compress(cinfo);
  995. if(jp2->j2k == NULL) {
  996. jp2_destroy_compress(jp2);
  997. return NULL;
  998. }
  999. }
  1000. return jp2;
  1001. }
  1002. void jp2_destroy_compress(opj_jp2_t *jp2) {
  1003. if(jp2) {
  1004. /* destroy the J2K codec */
  1005. j2k_destroy_compress(jp2->j2k);
  1006. if(jp2->comps) {
  1007. opj_free(jp2->comps);
  1008. }
  1009. if(jp2->cl) {
  1010. opj_free(jp2->cl);
  1011. }
  1012. opj_free(jp2);
  1013. }
  1014. }
  1015. void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
  1016. int i;
  1017. int depth_0, sign;
  1018. if(!jp2 || !parameters || !image)
  1019. return;
  1020. /* setup the J2K codec */
  1021. /* ------------------- */
  1022. /* Check if number of components respects standard */
  1023. if (image->numcomps < 1 || image->numcomps > 16384) {
  1024. opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
  1025. return;
  1026. }
  1027. j2k_setup_encoder(jp2->j2k, parameters, image);
  1028. /* setup the JP2 codec */
  1029. /* ------------------- */
  1030. /* Profile box */
  1031. jp2->brand = JP2_JP2; /* BR */
  1032. jp2->minversion = 0; /* MinV */
  1033. jp2->numcl = 1;
  1034. jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
  1035. jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
  1036. /* Image Header box */
  1037. jp2->numcomps = image->numcomps; /* NC */
  1038. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  1039. jp2->h = image->y1 - image->y0; /* HEIGHT */
  1040. jp2->w = image->x1 - image->x0; /* WIDTH */
  1041. /* BPC */
  1042. depth_0 = image->comps[0].prec - 1;
  1043. sign = image->comps[0].sgnd;
  1044. jp2->bpc = depth_0 + (sign << 7);
  1045. for (i = 1; i < image->numcomps; i++) {
  1046. int depth = image->comps[i].prec - 1;
  1047. sign = image->comps[i].sgnd;
  1048. if (depth_0 != depth)
  1049. jp2->bpc = 255;
  1050. }
  1051. jp2->C = 7; /* C : Always 7 */
  1052. jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
  1053. jp2->IPR = 0; /* IPR, no intellectual property */
  1054. /* BitsPerComponent box */
  1055. for (i = 0; i < image->numcomps; i++) {
  1056. jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
  1057. }
  1058. jp2->meth = 1;
  1059. if (image->color_space == 1)
  1060. jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
  1061. else if (image->color_space == 2)
  1062. jp2->enumcs = 17; /* greyscale */
  1063. else if (image->color_space == 3)
  1064. jp2->enumcs = 18; /* YUV */
  1065. jp2->precedence = 0; /* PRECEDENCE */
  1066. jp2->approx = 0; /* APPROX */
  1067. jp2->jpip_on = parameters->jpip_on;
  1068. }
  1069. opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  1070. int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
  1071. pos_jp2c = pos_iptr = -1; /* remove a warning */
  1072. /* JP2 encoding */
  1073. /* JPEG 2000 Signature box */
  1074. jp2_write_jp(cio);
  1075. /* File Type box */
  1076. jp2_write_ftyp(jp2, cio);
  1077. /* JP2 Header box */
  1078. jp2_write_jp2h(jp2, cio);
  1079. if( jp2->jpip_on){
  1080. pos_iptr = cio_tell( cio);
  1081. cio_skip( cio, 24); /* IPTR further ! */
  1082. pos_jp2c = cio_tell( cio);
  1083. }
  1084. /* J2K encoding */
  1085. if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
  1086. opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
  1087. return OPJ_FALSE;
  1088. }
  1089. if( jp2->jpip_on){
  1090. pos_cidx = cio_tell( cio);
  1091. len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
  1092. pos_fidx = cio_tell( cio);
  1093. len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
  1094. end_pos = cio_tell( cio);
  1095. cio_seek( cio, pos_iptr);
  1096. write_iptr( pos_fidx, len_fidx, cio);
  1097. cio_seek( cio, end_pos);
  1098. }
  1099. return OPJ_TRUE;
  1100. }