jp2_cod.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /*
  8. * Modified by Andrey Kiselev <dron@remotesensing.org> to handle UUID
  9. * box properly.
  10. */
  11. /* __START_OF_JASPER_LICENSE__
  12. *
  13. * JasPer License Version 2.0
  14. *
  15. * Copyright (c) 2001-2006 Michael David Adams
  16. * Copyright (c) 1999-2000 Image Power, Inc.
  17. * Copyright (c) 1999-2000 The University of British Columbia
  18. *
  19. * All rights reserved.
  20. *
  21. * Permission is hereby granted, free of charge, to any person (the
  22. * "User") obtaining a copy of this software and associated documentation
  23. * files (the "Software"), to deal in the Software without restriction,
  24. * including without limitation the rights to use, copy, modify, merge,
  25. * publish, distribute, and/or sell copies of the Software, and to permit
  26. * persons to whom the Software is furnished to do so, subject to the
  27. * following conditions:
  28. *
  29. * 1. The above copyright notices and this permission notice (which
  30. * includes the disclaimer below) shall be included in all copies or
  31. * substantial portions of the Software.
  32. *
  33. * 2. The name of a copyright holder shall not be used to endorse or
  34. * promote products derived from the Software without specific prior
  35. * written permission.
  36. *
  37. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  38. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  39. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  40. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  41. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  42. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  43. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  44. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  45. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  46. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  47. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  48. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  49. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  50. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  51. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  52. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  53. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  54. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  55. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  56. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  57. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  58. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  59. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  60. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  61. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  62. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  63. *
  64. * __END_OF_JASPER_LICENSE__
  65. */
  66. /*
  67. * JP2 Library
  68. *
  69. * $Id: jp2_cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  70. */
  71. /******************************************************************************\
  72. * Includes.
  73. \******************************************************************************/
  74. #include <assert.h>
  75. #include <stdlib.h>
  76. #include "jasper/jas_stream.h"
  77. #include "jasper/jas_malloc.h"
  78. #include "jasper/jas_debug.h"
  79. #include "jp2_cod.h"
  80. /******************************************************************************\
  81. * Function prototypes.
  82. \******************************************************************************/
  83. #define ONES(n) ((1 << (n)) - 1)
  84. jp2_boxinfo_t *jp2_boxinfolookup(int type);
  85. static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val);
  86. static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val);
  87. static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val);
  88. static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val);
  89. static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val);
  90. static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val);
  91. static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val);
  92. static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val);
  93. static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val);
  94. jp2_box_t *jp2_box_get(jas_stream_t *in);
  95. void jp2_box_dump(jp2_box_t *box, FILE *out);
  96. static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in);
  97. static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out);
  98. static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in);
  99. static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out);
  100. static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in);
  101. static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out);
  102. static void jp2_bpcc_destroy(jp2_box_t *box);
  103. static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in);
  104. static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out);
  105. static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in);
  106. static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out);
  107. static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out);
  108. static void jp2_colr_destroy(jp2_box_t *box);
  109. static void jp2_cdef_destroy(jp2_box_t *box);
  110. static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in);
  111. static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out);
  112. static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out);
  113. static void jp2_cmap_destroy(jp2_box_t *box);
  114. static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in);
  115. static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out);
  116. static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out);
  117. static void jp2_pclr_destroy(jp2_box_t *box);
  118. static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in);
  119. static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out);
  120. static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out);
  121. static void jp2_uuid_destroy(jp2_box_t *box);
  122. static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in);
  123. static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out);
  124. /******************************************************************************\
  125. * Local data.
  126. \******************************************************************************/
  127. jp2_boxinfo_t jp2_boxinfos[] = {
  128. {JP2_BOX_JP, "JP", 0,
  129. {0, 0, jp2_jp_getdata, jp2_jp_putdata, 0}},
  130. {JP2_BOX_FTYP, "FTYP", 0,
  131. {0, 0, jp2_ftyp_getdata, jp2_ftyp_putdata, 0}},
  132. {JP2_BOX_JP2H, "JP2H", JP2_BOX_SUPER,
  133. {0, 0, 0, 0, 0}},
  134. {JP2_BOX_IHDR, "IHDR", 0,
  135. {0, 0, jp2_ihdr_getdata, jp2_ihdr_putdata, 0}},
  136. {JP2_BOX_BPCC, "BPCC", 0,
  137. {0, jp2_bpcc_destroy, jp2_bpcc_getdata, jp2_bpcc_putdata, 0}},
  138. {JP2_BOX_COLR, "COLR", 0,
  139. {0, jp2_colr_destroy, jp2_colr_getdata, jp2_colr_putdata, jp2_colr_dumpdata}},
  140. {JP2_BOX_PCLR, "PCLR", 0,
  141. {0, jp2_pclr_destroy, jp2_pclr_getdata, jp2_pclr_putdata, jp2_pclr_dumpdata}},
  142. {JP2_BOX_CMAP, "CMAP", 0,
  143. {0, jp2_cmap_destroy, jp2_cmap_getdata, jp2_cmap_putdata, jp2_cmap_dumpdata}},
  144. {JP2_BOX_CDEF, "CDEF", 0,
  145. {0, jp2_cdef_destroy, jp2_cdef_getdata, jp2_cdef_putdata, jp2_cdef_dumpdata}},
  146. {JP2_BOX_RES, "RES", JP2_BOX_SUPER,
  147. {0, 0, 0, 0, 0}},
  148. {JP2_BOX_RESC, "RESC", 0,
  149. {0, 0, 0, 0, 0}},
  150. {JP2_BOX_RESD, "RESD", 0,
  151. {0, 0, 0, 0, 0}},
  152. {JP2_BOX_JP2C, "JP2C", JP2_BOX_NODATA,
  153. {0, 0, 0, 0, 0}},
  154. {JP2_BOX_JP2I, "JP2I", 0,
  155. {0, 0, 0, 0, 0}},
  156. {JP2_BOX_XML, "XML", 0,
  157. {0, 0, 0, 0, 0}},
  158. {JP2_BOX_UUID, "UUID", 0,
  159. {0, jp2_uuid_destroy, jp2_uuid_getdata, jp2_uuid_putdata, 0}},
  160. {JP2_BOX_UINF, "UINF", JP2_BOX_SUPER,
  161. {0, 0, 0, 0, 0}},
  162. {JP2_BOX_ULST, "ULST", 0,
  163. {0, 0, 0, 0, 0}},
  164. {JP2_BOX_URL, "URL", 0,
  165. {0, 0, 0, 0, 0}},
  166. {0, 0, 0, {0, 0, 0, 0, 0}},
  167. };
  168. jp2_boxinfo_t jp2_boxinfo_unk = {
  169. 0, "Unknown", 0, {0, 0, 0, 0, 0}
  170. };
  171. /******************************************************************************\
  172. * Box constructor.
  173. \******************************************************************************/
  174. jp2_box_t *jp2_box_create(int type)
  175. {
  176. jp2_box_t *box;
  177. jp2_boxinfo_t *boxinfo;
  178. if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
  179. return 0;
  180. }
  181. memset(box, 0, sizeof(jp2_box_t));
  182. box->type = type;
  183. box->len = 0;
  184. if (!(boxinfo = jp2_boxinfolookup(type))) {
  185. return 0;
  186. }
  187. box->info = boxinfo;
  188. box->ops = &boxinfo->ops;
  189. return box;
  190. }
  191. /******************************************************************************\
  192. * Box destructor.
  193. \******************************************************************************/
  194. void jp2_box_destroy(jp2_box_t *box)
  195. {
  196. if (box->ops->destroy) {
  197. (*box->ops->destroy)(box);
  198. }
  199. jas_free(box);
  200. }
  201. static void jp2_bpcc_destroy(jp2_box_t *box)
  202. {
  203. jp2_bpcc_t *bpcc = &box->data.bpcc;
  204. if (bpcc->bpcs) {
  205. jas_free(bpcc->bpcs);
  206. bpcc->bpcs = 0;
  207. }
  208. }
  209. static void jp2_cdef_destroy(jp2_box_t *box)
  210. {
  211. jp2_cdef_t *cdef = &box->data.cdef;
  212. if (cdef->ents) {
  213. jas_free(cdef->ents);
  214. cdef->ents = 0;
  215. }
  216. }
  217. /******************************************************************************\
  218. * Box input.
  219. \******************************************************************************/
  220. jp2_box_t *jp2_box_get(jas_stream_t *in)
  221. {
  222. jp2_box_t *box;
  223. jp2_boxinfo_t *boxinfo;
  224. jas_stream_t *tmpstream;
  225. uint_fast32_t len;
  226. uint_fast64_t extlen;
  227. bool dataflag;
  228. box = 0;
  229. tmpstream = 0;
  230. if (!(box = jas_malloc(sizeof(jp2_box_t)))) {
  231. goto error;
  232. }
  233. box->ops = &jp2_boxinfo_unk.ops;
  234. if (jp2_getuint32(in, &len) || jp2_getuint32(in, &box->type)) {
  235. goto error;
  236. }
  237. boxinfo = jp2_boxinfolookup(box->type);
  238. box->info = boxinfo;
  239. box->ops = &boxinfo->ops;
  240. box->len = len;
  241. if (box->len == 1) {
  242. if (jp2_getuint64(in, &extlen)) {
  243. goto error;
  244. }
  245. if (extlen > 0xffffffffUL) {
  246. jas_eprintf("warning: cannot handle large 64-bit box length\n");
  247. extlen = 0xffffffffUL;
  248. }
  249. box->len = extlen;
  250. box->datalen = extlen - JP2_BOX_HDRLEN(true);
  251. } else {
  252. box->datalen = box->len - JP2_BOX_HDRLEN(false);
  253. }
  254. if (box->len != 0 && box->len < 8) {
  255. goto error;
  256. }
  257. dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
  258. if (dataflag) {
  259. if (!(tmpstream = jas_stream_memopen(0, 0))) {
  260. goto error;
  261. }
  262. if (jas_stream_copy(tmpstream, in, box->datalen)) {
  263. jas_eprintf("cannot copy box data\n");
  264. goto error;
  265. }
  266. jas_stream_rewind(tmpstream);
  267. if (box->ops->getdata) {
  268. if ((*box->ops->getdata)(box, tmpstream)) {
  269. jas_eprintf("cannot parse box data\n");
  270. goto error;
  271. }
  272. }
  273. jas_stream_close(tmpstream);
  274. }
  275. if (jas_getdbglevel() >= 1) {
  276. jp2_box_dump(box, stderr);
  277. }
  278. return box;
  279. abort();
  280. error:
  281. if (box) {
  282. jp2_box_destroy(box);
  283. }
  284. if (tmpstream) {
  285. jas_stream_close(tmpstream);
  286. }
  287. return 0;
  288. }
  289. void jp2_box_dump(jp2_box_t *box, FILE *out)
  290. {
  291. jp2_boxinfo_t *boxinfo;
  292. boxinfo = jp2_boxinfolookup(box->type);
  293. assert(boxinfo);
  294. fprintf(out, "JP2 box: ");
  295. fprintf(out, "type=%c%s%c (0x%08x); length=%d\n", '"', boxinfo->name,
  296. '"', (unsigned)box->type, (int)box->len);
  297. if (box->ops->dumpdata) {
  298. (*box->ops->dumpdata)(box, out);
  299. }
  300. }
  301. static int jp2_jp_getdata(jp2_box_t *box, jas_stream_t *in)
  302. {
  303. jp2_jp_t *jp = &box->data.jp;
  304. if (jp2_getuint32(in, &jp->magic)) {
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. static int jp2_ftyp_getdata(jp2_box_t *box, jas_stream_t *in)
  310. {
  311. jp2_ftyp_t *ftyp = &box->data.ftyp;
  312. unsigned int i;
  313. if (jp2_getuint32(in, &ftyp->majver) || jp2_getuint32(in, &ftyp->minver)) {
  314. return -1;
  315. }
  316. ftyp->numcompatcodes = (box->datalen - 8) / 4;
  317. if (ftyp->numcompatcodes > JP2_FTYP_MAXCOMPATCODES) {
  318. return -1;
  319. }
  320. for (i = 0; i < ftyp->numcompatcodes; ++i) {
  321. if (jp2_getuint32(in, &ftyp->compatcodes[i])) {
  322. return -1;
  323. }
  324. }
  325. return 0;
  326. }
  327. static int jp2_ihdr_getdata(jp2_box_t *box, jas_stream_t *in)
  328. {
  329. jp2_ihdr_t *ihdr = &box->data.ihdr;
  330. if (jp2_getuint32(in, &ihdr->height) || jp2_getuint32(in, &ihdr->width) ||
  331. jp2_getuint16(in, &ihdr->numcmpts) || jp2_getuint8(in, &ihdr->bpc) ||
  332. jp2_getuint8(in, &ihdr->comptype) || jp2_getuint8(in, &ihdr->csunk) ||
  333. jp2_getuint8(in, &ihdr->ipr)) {
  334. return -1;
  335. }
  336. return 0;
  337. }
  338. static int jp2_bpcc_getdata(jp2_box_t *box, jas_stream_t *in)
  339. {
  340. jp2_bpcc_t *bpcc = &box->data.bpcc;
  341. unsigned int i;
  342. bpcc->numcmpts = box->datalen;
  343. if (!(bpcc->bpcs = jas_alloc2(bpcc->numcmpts, sizeof(uint_fast8_t)))) {
  344. return -1;
  345. }
  346. for (i = 0; i < bpcc->numcmpts; ++i) {
  347. if (jp2_getuint8(in, &bpcc->bpcs[i])) {
  348. return -1;
  349. }
  350. }
  351. return 0;
  352. }
  353. static void jp2_colr_dumpdata(jp2_box_t *box, FILE *out)
  354. {
  355. jp2_colr_t *colr = &box->data.colr;
  356. fprintf(out, "method=%d; pri=%d; approx=%d\n", (int)colr->method, (int)colr->pri, (int)colr->approx);
  357. switch (colr->method) {
  358. case JP2_COLR_ENUM:
  359. fprintf(out, "csid=%d\n", (int)colr->csid);
  360. break;
  361. case JP2_COLR_ICC:
  362. jas_memdump(out, colr->iccp, colr->iccplen);
  363. break;
  364. }
  365. }
  366. static int jp2_colr_getdata(jp2_box_t *box, jas_stream_t *in)
  367. {
  368. jp2_colr_t *colr = &box->data.colr;
  369. colr->csid = 0;
  370. colr->iccp = 0;
  371. colr->iccplen = 0;
  372. if (jp2_getuint8(in, &colr->method) || jp2_getuint8(in, &colr->pri) ||
  373. jp2_getuint8(in, &colr->approx)) {
  374. return -1;
  375. }
  376. switch (colr->method) {
  377. case JP2_COLR_ENUM:
  378. if (jp2_getuint32(in, &colr->csid)) {
  379. return -1;
  380. }
  381. break;
  382. case JP2_COLR_ICC:
  383. colr->iccplen = box->datalen - 3;
  384. if (!(colr->iccp = jas_alloc2(colr->iccplen, sizeof(uint_fast8_t)))) {
  385. return -1;
  386. }
  387. if (jas_stream_read(in, colr->iccp, colr->iccplen) != colr->iccplen) {
  388. return -1;
  389. }
  390. break;
  391. }
  392. return 0;
  393. }
  394. static void jp2_cdef_dumpdata(jp2_box_t *box, FILE *out)
  395. {
  396. jp2_cdef_t *cdef = &box->data.cdef;
  397. unsigned int i;
  398. for (i = 0; i < cdef->numchans; ++i) {
  399. fprintf(out, "channo=%d; type=%d; assoc=%d\n",
  400. (int)cdef->ents[i].channo, (int)cdef->ents[i].type, (int)cdef->ents[i].assoc);
  401. }
  402. }
  403. static void jp2_colr_destroy(jp2_box_t *box)
  404. {
  405. jp2_colr_t *colr = &box->data.colr;
  406. if (colr->iccp) {
  407. jas_free(colr->iccp);
  408. }
  409. }
  410. static int jp2_cdef_getdata(jp2_box_t *box, jas_stream_t *in)
  411. {
  412. jp2_cdef_t *cdef = &box->data.cdef;
  413. jp2_cdefchan_t *chan;
  414. unsigned int channo;
  415. if (jp2_getuint16(in, &cdef->numchans)) {
  416. return -1;
  417. }
  418. if (!(cdef->ents = jas_alloc2(cdef->numchans, sizeof(jp2_cdefchan_t)))) {
  419. return -1;
  420. }
  421. for (channo = 0; channo < cdef->numchans; ++channo) {
  422. chan = &cdef->ents[channo];
  423. if (jp2_getuint16(in, &chan->channo) || jp2_getuint16(in, &chan->type) ||
  424. jp2_getuint16(in, &chan->assoc)) {
  425. return -1;
  426. }
  427. }
  428. return 0;
  429. }
  430. /******************************************************************************\
  431. * Box output.
  432. \******************************************************************************/
  433. int jp2_box_put(jp2_box_t *box, jas_stream_t *out)
  434. {
  435. jas_stream_t *tmpstream;
  436. bool extlen;
  437. bool dataflag;
  438. tmpstream = 0;
  439. dataflag = !(box->info->flags & (JP2_BOX_SUPER | JP2_BOX_NODATA));
  440. if (dataflag) {
  441. tmpstream = jas_stream_memopen(0, 0);
  442. if (box->ops->putdata) {
  443. if ((*box->ops->putdata)(box, tmpstream)) {
  444. goto error;
  445. }
  446. }
  447. box->len = jas_stream_tell(tmpstream) + JP2_BOX_HDRLEN(false);
  448. jas_stream_rewind(tmpstream);
  449. }
  450. //extlen = (box->len >= (((uint_fast64_t)1) << 32)) != 0;
  451. if (jp2_putuint32(out, /*extlen ? 1 :*/ box->len)) {
  452. goto error;
  453. }
  454. if (jp2_putuint32(out, box->type)) {
  455. goto error;
  456. }
  457. /*if (extlen) {
  458. if (jp2_putuint64(out, box->len)) {
  459. goto error;
  460. }
  461. }*/
  462. if (dataflag) {
  463. if (jas_stream_copy(out, tmpstream, box->len - JP2_BOX_HDRLEN(false))) {
  464. goto error;
  465. }
  466. jas_stream_close(tmpstream);
  467. }
  468. return 0;
  469. abort();
  470. error:
  471. if (tmpstream) {
  472. jas_stream_close(tmpstream);
  473. }
  474. return -1;
  475. }
  476. static int jp2_jp_putdata(jp2_box_t *box, jas_stream_t *out)
  477. {
  478. jp2_jp_t *jp = &box->data.jp;
  479. if (jp2_putuint32(out, jp->magic)) {
  480. return -1;
  481. }
  482. return 0;
  483. }
  484. static int jp2_ftyp_putdata(jp2_box_t *box, jas_stream_t *out)
  485. {
  486. jp2_ftyp_t *ftyp = &box->data.ftyp;
  487. unsigned int i;
  488. if (jp2_putuint32(out, ftyp->majver) || jp2_putuint32(out, ftyp->minver)) {
  489. return -1;
  490. }
  491. for (i = 0; i < ftyp->numcompatcodes; ++i) {
  492. if (jp2_putuint32(out, ftyp->compatcodes[i])) {
  493. return -1;
  494. }
  495. }
  496. return 0;
  497. }
  498. static int jp2_ihdr_putdata(jp2_box_t *box, jas_stream_t *out)
  499. {
  500. jp2_ihdr_t *ihdr = &box->data.ihdr;
  501. if (jp2_putuint32(out, ihdr->height) || jp2_putuint32(out, ihdr->width) ||
  502. jp2_putuint16(out, ihdr->numcmpts) || jp2_putuint8(out, ihdr->bpc) ||
  503. jp2_putuint8(out, ihdr->comptype) || jp2_putuint8(out, ihdr->csunk) ||
  504. jp2_putuint8(out, ihdr->ipr)) {
  505. return -1;
  506. }
  507. return 0;
  508. }
  509. static int jp2_bpcc_putdata(jp2_box_t *box, jas_stream_t *out)
  510. {
  511. jp2_bpcc_t *bpcc = &box->data.bpcc;
  512. unsigned int i;
  513. for (i = 0; i < bpcc->numcmpts; ++i) {
  514. if (jp2_putuint8(out, bpcc->bpcs[i])) {
  515. return -1;
  516. }
  517. }
  518. return 0;
  519. }
  520. static int jp2_colr_putdata(jp2_box_t *box, jas_stream_t *out)
  521. {
  522. jp2_colr_t *colr = &box->data.colr;
  523. if (jp2_putuint8(out, colr->method) || jp2_putuint8(out, colr->pri) ||
  524. jp2_putuint8(out, colr->approx)) {
  525. return -1;
  526. }
  527. switch (colr->method) {
  528. case JP2_COLR_ENUM:
  529. if (jp2_putuint32(out, colr->csid)) {
  530. return -1;
  531. }
  532. break;
  533. case JP2_COLR_ICC:
  534. if (jas_stream_write(out, colr->iccp,
  535. JAS_CAST(int, colr->iccplen)) != JAS_CAST(int, colr->iccplen))
  536. return -1;
  537. break;
  538. }
  539. return 0;
  540. }
  541. static int jp2_cdef_putdata(jp2_box_t *box, jas_stream_t *out)
  542. {
  543. jp2_cdef_t *cdef = &box->data.cdef;
  544. unsigned int i;
  545. jp2_cdefchan_t *ent;
  546. if (jp2_putuint16(out, cdef->numchans)) {
  547. return -1;
  548. }
  549. for (i = 0; i < cdef->numchans; ++i) {
  550. ent = &cdef->ents[i];
  551. if (jp2_putuint16(out, ent->channo) ||
  552. jp2_putuint16(out, ent->type) ||
  553. jp2_putuint16(out, ent->assoc)) {
  554. return -1;
  555. }
  556. }
  557. return 0;
  558. }
  559. /******************************************************************************\
  560. * Input operations for primitive types.
  561. \******************************************************************************/
  562. static int jp2_getuint8(jas_stream_t *in, uint_fast8_t *val)
  563. {
  564. int c;
  565. if ((c = jas_stream_getc(in)) == EOF) {
  566. return -1;
  567. }
  568. if (val) {
  569. *val = c;
  570. }
  571. return 0;
  572. }
  573. static int jp2_getuint16(jas_stream_t *in, uint_fast16_t *val)
  574. {
  575. uint_fast16_t v;
  576. int c;
  577. if ((c = jas_stream_getc(in)) == EOF) {
  578. return -1;
  579. }
  580. v = c;
  581. if ((c = jas_stream_getc(in)) == EOF) {
  582. return -1;
  583. }
  584. v = (v << 8) | c;
  585. if (val) {
  586. *val = v;
  587. }
  588. return 0;
  589. }
  590. static int jp2_getuint32(jas_stream_t *in, uint_fast32_t *val)
  591. {
  592. uint_fast32_t v;
  593. int c;
  594. if ((c = jas_stream_getc(in)) == EOF) {
  595. return -1;
  596. }
  597. v = c;
  598. if ((c = jas_stream_getc(in)) == EOF) {
  599. return -1;
  600. }
  601. v = (v << 8) | c;
  602. if ((c = jas_stream_getc(in)) == EOF) {
  603. return -1;
  604. }
  605. v = (v << 8) | c;
  606. if ((c = jas_stream_getc(in)) == EOF) {
  607. return -1;
  608. }
  609. v = (v << 8) | c;
  610. if (val) {
  611. *val = v;
  612. }
  613. return 0;
  614. }
  615. static int jp2_getuint64(jas_stream_t *in, uint_fast64_t *val)
  616. {
  617. uint_fast64_t tmpval;
  618. int i;
  619. int c;
  620. tmpval = 0;
  621. for (i = 0; i < 8; ++i) {
  622. tmpval <<= 8;
  623. if ((c = jas_stream_getc(in)) == EOF) {
  624. return -1;
  625. }
  626. tmpval |= (c & 0xff);
  627. }
  628. *val = tmpval;
  629. return 0;
  630. }
  631. /******************************************************************************\
  632. * Output operations for primitive types.
  633. \******************************************************************************/
  634. static int jp2_putuint8(jas_stream_t *out, uint_fast8_t val)
  635. {
  636. if (jas_stream_putc(out, val & 0xff) == EOF) {
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. static int jp2_putuint16(jas_stream_t *out, uint_fast16_t val)
  642. {
  643. if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
  644. jas_stream_putc(out, val & 0xff) == EOF) {
  645. return -1;
  646. }
  647. return 0;
  648. }
  649. static int jp2_putuint32(jas_stream_t *out, uint_fast32_t val)
  650. {
  651. if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
  652. jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
  653. jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
  654. jas_stream_putc(out, val & 0xff) == EOF) {
  655. return -1;
  656. }
  657. return 0;
  658. }
  659. static int jp2_putuint64(jas_stream_t *out, uint_fast64_t val)
  660. {
  661. if (jp2_putuint32(out, (val >> 32) & 0xffffffffUL) ||
  662. jp2_putuint32(out, val & 0xffffffffUL)) {
  663. return -1;
  664. }
  665. return 0;
  666. }
  667. /******************************************************************************\
  668. * Miscellaneous code.
  669. \******************************************************************************/
  670. jp2_boxinfo_t *jp2_boxinfolookup(int type)
  671. {
  672. jp2_boxinfo_t *boxinfo;
  673. for (boxinfo = jp2_boxinfos; boxinfo->name; ++boxinfo) {
  674. if (boxinfo->type == type) {
  675. return boxinfo;
  676. }
  677. }
  678. return &jp2_boxinfo_unk;
  679. }
  680. static void jp2_cmap_destroy(jp2_box_t *box)
  681. {
  682. jp2_cmap_t *cmap = &box->data.cmap;
  683. if (cmap->ents) {
  684. jas_free(cmap->ents);
  685. }
  686. }
  687. static int jp2_cmap_getdata(jp2_box_t *box, jas_stream_t *in)
  688. {
  689. jp2_cmap_t *cmap = &box->data.cmap;
  690. jp2_cmapent_t *ent;
  691. unsigned int i;
  692. cmap->numchans = (box->datalen) / 4;
  693. if (!(cmap->ents = jas_alloc2(cmap->numchans, sizeof(jp2_cmapent_t)))) {
  694. return -1;
  695. }
  696. for (i = 0; i < cmap->numchans; ++i) {
  697. ent = &cmap->ents[i];
  698. if (jp2_getuint16(in, &ent->cmptno) ||
  699. jp2_getuint8(in, &ent->map) ||
  700. jp2_getuint8(in, &ent->pcol)) {
  701. return -1;
  702. }
  703. }
  704. return 0;
  705. }
  706. static int jp2_cmap_putdata(jp2_box_t *box, jas_stream_t *out)
  707. {
  708. /* Eliminate compiler warning about unused variables. */
  709. box = 0;
  710. out = 0;
  711. return -1;
  712. }
  713. static void jp2_cmap_dumpdata(jp2_box_t *box, FILE *out)
  714. {
  715. jp2_cmap_t *cmap = &box->data.cmap;
  716. unsigned int i;
  717. jp2_cmapent_t *ent;
  718. fprintf(out, "numchans = %d\n", (int) cmap->numchans);
  719. for (i = 0; i < cmap->numchans; ++i) {
  720. ent = &cmap->ents[i];
  721. fprintf(out, "cmptno=%d; map=%d; pcol=%d\n",
  722. (int) ent->cmptno, (int) ent->map, (int) ent->pcol);
  723. }
  724. }
  725. static void jp2_pclr_destroy(jp2_box_t *box)
  726. {
  727. jp2_pclr_t *pclr = &box->data.pclr;
  728. if (pclr->lutdata) {
  729. jas_free(pclr->lutdata);
  730. }
  731. if (pclr->bpc)
  732. jas_free(pclr->bpc);
  733. }
  734. static int jp2_pclr_getdata(jp2_box_t *box, jas_stream_t *in)
  735. {
  736. jp2_pclr_t *pclr = &box->data.pclr;
  737. int lutsize;
  738. unsigned int i;
  739. unsigned int j;
  740. int_fast32_t x;
  741. pclr->lutdata = 0;
  742. if (jp2_getuint16(in, &pclr->numlutents) ||
  743. jp2_getuint8(in, &pclr->numchans)) {
  744. return -1;
  745. }
  746. lutsize = pclr->numlutents * pclr->numchans;
  747. if (!(pclr->lutdata = jas_alloc2(lutsize, sizeof(int_fast32_t)))) {
  748. return -1;
  749. }
  750. if (!(pclr->bpc = jas_alloc2(pclr->numchans, sizeof(uint_fast8_t)))) {
  751. return -1;
  752. }
  753. for (i = 0; i < pclr->numchans; ++i) {
  754. if (jp2_getuint8(in, &pclr->bpc[i])) {
  755. return -1;
  756. }
  757. }
  758. for (i = 0; i < pclr->numlutents; ++i) {
  759. for (j = 0; j < pclr->numchans; ++j) {
  760. if (jp2_getint(in, (pclr->bpc[j] & 0x80) != 0,
  761. (pclr->bpc[j] & 0x7f) + 1, &x)) {
  762. return -1;
  763. }
  764. pclr->lutdata[i * pclr->numchans + j] = x;
  765. }
  766. }
  767. return 0;
  768. }
  769. static int jp2_pclr_putdata(jp2_box_t *box, jas_stream_t *out)
  770. {
  771. #if 0
  772. jp2_pclr_t *pclr = &box->data.pclr;
  773. #endif
  774. /* Eliminate warning about unused variable. */
  775. box = 0;
  776. out = 0;
  777. return -1;
  778. }
  779. static void jp2_pclr_dumpdata(jp2_box_t *box, FILE *out)
  780. {
  781. jp2_pclr_t *pclr = &box->data.pclr;
  782. unsigned int i;
  783. int j;
  784. fprintf(out, "numents=%d; numchans=%d\n", (int) pclr->numlutents,
  785. (int) pclr->numchans);
  786. for (i = 0; i < pclr->numlutents; ++i) {
  787. for (j = 0; j < pclr->numchans; ++j) {
  788. fprintf(out, "LUT[%d][%d]=%d\n", i, j, (int)pclr->lutdata[i * pclr->numchans + j]);
  789. }
  790. }
  791. }
  792. static void jp2_uuid_destroy(jp2_box_t *box)
  793. {
  794. jp2_uuid_t *uuid = &box->data.uuid;
  795. if (uuid->data)
  796. {
  797. jas_free(uuid->data);
  798. uuid->data = NULL;
  799. }
  800. }
  801. static int jp2_uuid_getdata(jp2_box_t *box, jas_stream_t *in)
  802. {
  803. jp2_uuid_t *uuid = &box->data.uuid;
  804. int i;
  805. for (i = 0; i < 16; i++)
  806. {
  807. if (jp2_getuint8(in, &uuid->uuid[i]))
  808. return -1;
  809. }
  810. uuid->datalen = box->datalen - 16;
  811. uuid->data = jas_malloc(uuid->datalen * sizeof(uint_fast8_t));
  812. for (i = 0; i < uuid->datalen; i++)
  813. {
  814. if (jp2_getuint8(in, &uuid->data[i]))
  815. return -1;
  816. }
  817. return 0;
  818. }
  819. static int jp2_uuid_putdata(jp2_box_t *box, jas_stream_t *out)
  820. {
  821. jp2_uuid_t *uuid = &box->data.uuid;
  822. int i;
  823. for (i = 0; i < 16; i++)
  824. {
  825. if (jp2_putuint8(out, uuid->uuid[i]))
  826. return -1;
  827. }
  828. for (i = 0; i < uuid->datalen; i++)
  829. {
  830. if (jp2_putuint8(out, uuid->data[i]))
  831. return -1;
  832. }
  833. return 0;
  834. }
  835. static int jp2_getint(jas_stream_t *in, int s, int n, int_fast32_t *val)
  836. {
  837. int c;
  838. int i;
  839. uint_fast32_t v;
  840. int m;
  841. m = (n + 7) / 8;
  842. v = 0;
  843. for (i = 0; i < m; ++i) {
  844. if ((c = jas_stream_getc(in)) == EOF) {
  845. return -1;
  846. }
  847. v = (v << 8) | c;
  848. }
  849. v &= ONES(n);
  850. if (s) {
  851. int sb;
  852. sb = v & (1 << (8 * m - 1));
  853. *val = ((~v) + 1) & ONES(8 * m);
  854. if (sb) {
  855. *val = -*val;
  856. }
  857. } else {
  858. *val = v;
  859. }
  860. return 0;
  861. }
  862. jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo)
  863. {
  864. unsigned int i;
  865. jp2_cdefchan_t *cdefent;
  866. for (i = 0; i < cdef->numchans; ++i) {
  867. cdefent = &cdef->ents[i];
  868. if (cdefent->channo == JAS_CAST(unsigned int, channo)) {
  869. return cdefent;
  870. }
  871. }
  872. return 0;
  873. }