jp2_enc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2003 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_enc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  70. */
  71. /******************************************************************************\
  72. * Includes.
  73. \******************************************************************************/
  74. #include <assert.h>
  75. #include "jasper/jas_malloc.h"
  76. #include "jasper/jas_image.h"
  77. #include "jasper/jas_stream.h"
  78. #include "jasper/jas_cm.h"
  79. #include "jasper/jas_icc.h"
  80. #include "jp2_cod.h"
  81. static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype);
  82. static int clrspctojp2(jas_clrspc_t clrspc);
  83. /******************************************************************************\
  84. * Functions.
  85. \******************************************************************************/
  86. int jp2_write_header(jas_image_t *image, jas_stream_t *out)
  87. {
  88. jp2_box_t *box;
  89. jp2_ftyp_t *ftyp;
  90. jp2_ihdr_t *ihdr;
  91. jas_stream_t *tmpstream;
  92. int allcmptssame;
  93. jp2_bpcc_t *bpcc;
  94. long len;
  95. uint_fast16_t cmptno;
  96. jp2_colr_t *colr;
  97. jp2_cdefchan_t *cdefchanent;
  98. jp2_cdef_t *cdef;
  99. int i;
  100. uint_fast32_t typeasoc;
  101. jas_iccprof_t *iccprof;
  102. jas_stream_t *iccstream;
  103. int pos;
  104. int needcdef;
  105. int prec;
  106. int sgnd;
  107. box = 0;
  108. tmpstream = 0;
  109. allcmptssame = 1;
  110. sgnd = jas_image_cmptsgnd(image, 0);
  111. prec = jas_image_cmptprec(image, 0);
  112. for (i = 1; i < jas_image_numcmpts(image); ++i) {
  113. if (jas_image_cmptsgnd(image, i) != sgnd ||
  114. jas_image_cmptprec(image, i) != prec) {
  115. allcmptssame = 0;
  116. break;
  117. }
  118. }
  119. /* Output the signature box. */
  120. if (!(box = jp2_box_create(JP2_BOX_JP))) {
  121. goto error;
  122. }
  123. box->data.jp.magic = JP2_JP_MAGIC;
  124. if (jp2_box_put(box, out)) {
  125. goto error;
  126. }
  127. jp2_box_destroy(box);
  128. box = 0;
  129. /* Output the file type box. */
  130. if (!(box = jp2_box_create(JP2_BOX_FTYP))) {
  131. goto error;
  132. }
  133. ftyp = &box->data.ftyp;
  134. ftyp->majver = JP2_FTYP_MAJVER;
  135. ftyp->minver = JP2_FTYP_MINVER;
  136. ftyp->numcompatcodes = 1;
  137. ftyp->compatcodes[0] = JP2_FTYP_COMPATCODE;
  138. if (jp2_box_put(box, out)) {
  139. goto error;
  140. }
  141. jp2_box_destroy(box);
  142. box = 0;
  143. /*
  144. * Generate the data portion of the JP2 header box.
  145. * We cannot simply output the header for this box
  146. * since we do not yet know the correct value for the length
  147. * field.
  148. */
  149. if (!(tmpstream = jas_stream_memopen(0, 0))) {
  150. goto error;
  151. }
  152. /* Generate image header box. */
  153. if (!(box = jp2_box_create(JP2_BOX_IHDR))) {
  154. goto error;
  155. }
  156. ihdr = &box->data.ihdr;
  157. ihdr->width = jas_image_width(image);
  158. ihdr->height = jas_image_height(image);
  159. ihdr->numcmpts = jas_image_numcmpts(image);
  160. ihdr->bpc = allcmptssame ? JP2_SPTOBPC(jas_image_cmptsgnd(image, 0),
  161. jas_image_cmptprec(image, 0)) : JP2_IHDR_BPCNULL;
  162. ihdr->comptype = JP2_IHDR_COMPTYPE;
  163. ihdr->csunk = 0;
  164. ihdr->ipr = 0;
  165. if (jp2_box_put(box, tmpstream)) {
  166. goto error;
  167. }
  168. jp2_box_destroy(box);
  169. box = 0;
  170. /* Generate bits per component box. */
  171. if (!allcmptssame) {
  172. if (!(box = jp2_box_create(JP2_BOX_BPCC))) {
  173. goto error;
  174. }
  175. bpcc = &box->data.bpcc;
  176. bpcc->numcmpts = jas_image_numcmpts(image);
  177. if (!(bpcc->bpcs = jas_alloc2(bpcc->numcmpts,
  178. sizeof(uint_fast8_t)))) {
  179. goto error;
  180. }
  181. for (cmptno = 0; cmptno < bpcc->numcmpts; ++cmptno) {
  182. bpcc->bpcs[cmptno] = JP2_SPTOBPC(jas_image_cmptsgnd(image,
  183. cmptno), jas_image_cmptprec(image, cmptno));
  184. }
  185. if (jp2_box_put(box, tmpstream)) {
  186. goto error;
  187. }
  188. jp2_box_destroy(box);
  189. box = 0;
  190. }
  191. /* Generate color specification box. */
  192. if (!(box = jp2_box_create(JP2_BOX_COLR))) {
  193. goto error;
  194. }
  195. colr = &box->data.colr;
  196. switch (jas_image_clrspc(image)) {
  197. case JAS_CLRSPC_SRGB:
  198. case JAS_CLRSPC_SYCBCR:
  199. case JAS_CLRSPC_SGRAY:
  200. colr->method = JP2_COLR_ENUM;
  201. colr->csid = clrspctojp2(jas_image_clrspc(image));
  202. colr->pri = JP2_COLR_PRI;
  203. colr->approx = 0;
  204. break;
  205. default:
  206. colr->method = JP2_COLR_ICC;
  207. colr->pri = JP2_COLR_PRI;
  208. colr->approx = 0;
  209. iccprof = jas_iccprof_createfromcmprof(jas_image_cmprof(image));
  210. assert(iccprof);
  211. iccstream = jas_stream_memopen(0, 0);
  212. assert(iccstream);
  213. if (jas_iccprof_save(iccprof, iccstream))
  214. abort();
  215. if ((pos = jas_stream_tell(iccstream)) < 0)
  216. abort();
  217. colr->iccplen = pos;
  218. colr->iccp = jas_malloc(pos);
  219. assert(colr->iccp);
  220. jas_stream_rewind(iccstream);
  221. if (jas_stream_read(iccstream, colr->iccp, colr->iccplen) != colr->iccplen)
  222. abort();
  223. jas_stream_close(iccstream);
  224. jas_iccprof_destroy(iccprof);
  225. break;
  226. }
  227. if (jp2_box_put(box, tmpstream)) {
  228. goto error;
  229. }
  230. jp2_box_destroy(box);
  231. box = 0;
  232. needcdef = 1;
  233. switch (jas_clrspc_fam(jas_image_clrspc(image))) {
  234. case JAS_CLRSPC_FAM_RGB:
  235. if (jas_image_cmpttype(image, 0) ==
  236. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R) &&
  237. jas_image_cmpttype(image, 1) ==
  238. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G) &&
  239. jas_image_cmpttype(image, 2) ==
  240. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))
  241. needcdef = 0;
  242. break;
  243. case JAS_CLRSPC_FAM_YCBCR:
  244. if (jas_image_cmpttype(image, 0) ==
  245. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_Y) &&
  246. jas_image_cmpttype(image, 1) ==
  247. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CB) &&
  248. jas_image_cmpttype(image, 2) ==
  249. JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CR))
  250. needcdef = 0;
  251. break;
  252. case JAS_CLRSPC_FAM_GRAY:
  253. if (jas_image_cmpttype(image, 0) ==
  254. JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y))
  255. needcdef = 0;
  256. break;
  257. default:
  258. abort();
  259. break;
  260. }
  261. if (needcdef) {
  262. if (!(box = jp2_box_create(JP2_BOX_CDEF))) {
  263. goto error;
  264. }
  265. cdef = &box->data.cdef;
  266. cdef->numchans = jas_image_numcmpts(image);
  267. cdef->ents = jas_alloc2(cdef->numchans, sizeof(jp2_cdefchan_t));
  268. for (i = 0; i < jas_image_numcmpts(image); ++i) {
  269. cdefchanent = &cdef->ents[i];
  270. cdefchanent->channo = i;
  271. typeasoc = jp2_gettypeasoc(jas_image_clrspc(image), jas_image_cmpttype(image, i));
  272. cdefchanent->type = typeasoc >> 16;
  273. cdefchanent->assoc = typeasoc & 0x7fff;
  274. }
  275. if (jp2_box_put(box, tmpstream)) {
  276. goto error;
  277. }
  278. jp2_box_destroy(box);
  279. box = 0;
  280. }
  281. /* Determine the total length of the JP2 header box. */
  282. len = jas_stream_tell(tmpstream);
  283. jas_stream_rewind(tmpstream);
  284. /*
  285. * Output the JP2 header box and all of the boxes which it contains.
  286. */
  287. if (!(box = jp2_box_create(JP2_BOX_JP2H))) {
  288. goto error;
  289. }
  290. box->len = len + JP2_BOX_HDRLEN(false);
  291. if (jp2_box_put(box, out)) {
  292. goto error;
  293. }
  294. jp2_box_destroy(box);
  295. box = 0;
  296. if (jas_stream_copy(out, tmpstream, len)) {
  297. goto error;
  298. }
  299. jas_stream_close(tmpstream);
  300. tmpstream = 0;
  301. return 0;
  302. abort();
  303. error:
  304. if (box) {
  305. jp2_box_destroy(box);
  306. }
  307. if (tmpstream) {
  308. jas_stream_close(tmpstream);
  309. }
  310. return -1;
  311. }
  312. int jp2_write_codestream(jas_image_t *image, jas_stream_t *out, char *optstr)
  313. {
  314. jp2_box_t *box;
  315. char buf[4096];
  316. uint_fast32_t overhead;
  317. /*
  318. * Output the contiguous code stream box.
  319. */
  320. if (!(box = jp2_box_create(JP2_BOX_JP2C))) {
  321. goto error;
  322. }
  323. box->len = 0;
  324. if (jp2_box_put(box, out)) {
  325. goto error;
  326. }
  327. jp2_box_destroy(box);
  328. box = 0;
  329. /* Output the JPEG-2000 code stream. */
  330. overhead = jas_stream_getrwcount(out);
  331. sprintf(buf, "%s\n_jp2overhead=%lu\n", (optstr ? optstr : ""),
  332. (unsigned long) overhead);
  333. if (jpc_encode(image, out, buf)) {
  334. goto error;
  335. }
  336. return 0;
  337. abort();
  338. error:
  339. if (box) {
  340. jp2_box_destroy(box);
  341. }
  342. return -1;
  343. }
  344. int jp2_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
  345. {
  346. if (jp2_write_header(image, out) < 0)
  347. return -1;
  348. if (jp2_write_codestream(image, out, optstr) < 0)
  349. return -1;
  350. return 0;
  351. }
  352. int jp2_encode_uuid(jas_image_t *image, jas_stream_t *out,
  353. char *optstr, jp2_box_t *uuid)
  354. {
  355. if (jp2_write_header(image, out) < 0)
  356. return -1;
  357. if (uuid) {
  358. if (jp2_box_put(uuid, out))
  359. return -1;
  360. }
  361. if (jp2_write_codestream(image, out, optstr) < 0)
  362. return -1;
  363. return 0;
  364. }
  365. static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype)
  366. {
  367. int type;
  368. int asoc;
  369. if (ctype & JAS_IMAGE_CT_OPACITY) {
  370. type = JP2_CDEF_TYPE_OPACITY;
  371. asoc = JP2_CDEF_ASOC_ALL;
  372. goto done;
  373. }
  374. type = JP2_CDEF_TYPE_UNSPEC;
  375. asoc = JP2_CDEF_ASOC_NONE;
  376. switch (jas_clrspc_fam(colorspace)) {
  377. case JAS_CLRSPC_FAM_RGB:
  378. switch (JAS_IMAGE_CT_COLOR(ctype)) {
  379. case JAS_IMAGE_CT_RGB_R:
  380. type = JP2_CDEF_TYPE_COLOR;
  381. asoc = JP2_CDEF_RGB_R;
  382. break;
  383. case JAS_IMAGE_CT_RGB_G:
  384. type = JP2_CDEF_TYPE_COLOR;
  385. asoc = JP2_CDEF_RGB_G;
  386. break;
  387. case JAS_IMAGE_CT_RGB_B:
  388. type = JP2_CDEF_TYPE_COLOR;
  389. asoc = JP2_CDEF_RGB_B;
  390. break;
  391. }
  392. break;
  393. case JAS_CLRSPC_FAM_YCBCR:
  394. switch (JAS_IMAGE_CT_COLOR(ctype)) {
  395. case JAS_IMAGE_CT_YCBCR_Y:
  396. type = JP2_CDEF_TYPE_COLOR;
  397. asoc = JP2_CDEF_YCBCR_Y;
  398. break;
  399. case JAS_IMAGE_CT_YCBCR_CB:
  400. type = JP2_CDEF_TYPE_COLOR;
  401. asoc = JP2_CDEF_YCBCR_CB;
  402. break;
  403. case JAS_IMAGE_CT_YCBCR_CR:
  404. type = JP2_CDEF_TYPE_COLOR;
  405. asoc = JP2_CDEF_YCBCR_CR;
  406. break;
  407. }
  408. break;
  409. case JAS_CLRSPC_FAM_GRAY:
  410. type = JP2_CDEF_TYPE_COLOR;
  411. asoc = JP2_CDEF_GRAY_Y;
  412. break;
  413. }
  414. done:
  415. return (type << 16) | asoc;
  416. }
  417. static int clrspctojp2(jas_clrspc_t clrspc)
  418. {
  419. switch (clrspc) {
  420. case JAS_CLRSPC_SRGB:
  421. return JP2_COLR_SRGB;
  422. case JAS_CLRSPC_SYCBCR:
  423. return JP2_COLR_SYCC;
  424. case JAS_CLRSPC_SGRAY:
  425. return JP2_COLR_SGRAY;
  426. default:
  427. abort();
  428. break;
  429. }
  430. }