jp2_cod.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  70. */
  71. #ifndef JP2_COD_H
  72. #define JP2_COD_H
  73. /******************************************************************************\
  74. * Includes.
  75. \******************************************************************************/
  76. #include "jasper/jas_types.h"
  77. /******************************************************************************\
  78. * Macros.
  79. \******************************************************************************/
  80. #define JP2_SPTOBPC(s, p) \
  81. ((((p) - 1) & 0x7f) | (((s) & 1) << 7))
  82. /******************************************************************************\
  83. * Box class.
  84. \******************************************************************************/
  85. #define JP2_BOX_HDRLEN(ext) ((ext) ? 16 : 8)
  86. /* Box types. */
  87. #define JP2_BOX_JP 0x6a502020 /* Signature */
  88. #define JP2_BOX_FTYP 0x66747970 /* File Type */
  89. #define JP2_BOX_JP2H 0x6a703268 /* JP2 Header */
  90. #define JP2_BOX_IHDR 0x69686472 /* Image Header */
  91. #define JP2_BOX_BPCC 0x62706363 /* Bits Per Component */
  92. #define JP2_BOX_COLR 0x636f6c72 /* Color Specification */
  93. #define JP2_BOX_PCLR 0x70636c72 /* Palette */
  94. #define JP2_BOX_CMAP 0x636d6170 /* Component Mapping */
  95. #define JP2_BOX_CDEF 0x63646566 /* Channel Definition */
  96. #define JP2_BOX_RES 0x72657320 /* Resolution */
  97. #define JP2_BOX_RESC 0x72657363 /* Capture Resolution */
  98. #define JP2_BOX_RESD 0x72657364 /* Default Display Resolution */
  99. #define JP2_BOX_JP2C 0x6a703263 /* Contiguous Code Stream */
  100. #define JP2_BOX_JP2I 0x6a703269 /* Intellectual Property */
  101. #define JP2_BOX_XML 0x786d6c20 /* XML */
  102. #define JP2_BOX_UUID 0x75756964 /* UUID */
  103. #define JP2_BOX_UINF 0x75696e66 /* UUID Info */
  104. #define JP2_BOX_ULST 0x75637374 /* UUID List */
  105. #define JP2_BOX_URL 0x75726c20 /* URL */
  106. #define JP2_BOX_SUPER 0x01
  107. #define JP2_BOX_NODATA 0x02
  108. /* JP box data. */
  109. #define JP2_JP_MAGIC 0x0d0a870a
  110. #define JP2_JP_LEN 12
  111. typedef struct {
  112. uint_fast32_t magic;
  113. } jp2_jp_t;
  114. /* FTYP box data. */
  115. #define JP2_FTYP_MAXCOMPATCODES 32
  116. #define JP2_FTYP_MAJVER 0x6a703220
  117. #define JP2_FTYP_MINVER 0
  118. #define JP2_FTYP_COMPATCODE JP2_FTYP_MAJVER
  119. typedef struct {
  120. uint_fast32_t majver;
  121. uint_fast32_t minver;
  122. uint_fast32_t numcompatcodes;
  123. uint_fast32_t compatcodes[JP2_FTYP_MAXCOMPATCODES];
  124. } jp2_ftyp_t;
  125. /* IHDR box data. */
  126. #define JP2_IHDR_COMPTYPE 7
  127. #define JP2_IHDR_BPCNULL 255
  128. typedef struct {
  129. uint_fast32_t width;
  130. uint_fast32_t height;
  131. uint_fast16_t numcmpts;
  132. uint_fast8_t bpc;
  133. uint_fast8_t comptype;
  134. uint_fast8_t csunk;
  135. uint_fast8_t ipr;
  136. } jp2_ihdr_t;
  137. /* BPCC box data. */
  138. typedef struct {
  139. uint_fast16_t numcmpts;
  140. uint_fast8_t *bpcs;
  141. } jp2_bpcc_t;
  142. /* COLR box data. */
  143. #define JP2_COLR_ENUM 1
  144. #define JP2_COLR_ICC 2
  145. #define JP2_COLR_PRI 0
  146. #define JP2_COLR_SRGB 16
  147. #define JP2_COLR_SGRAY 17
  148. #define JP2_COLR_SYCC 18
  149. typedef struct {
  150. uint_fast8_t method;
  151. uint_fast8_t pri;
  152. uint_fast8_t approx;
  153. uint_fast32_t csid;
  154. uint_fast8_t *iccp;
  155. int iccplen;
  156. /* XXX - Someday we ought to add ICC profile data here. */
  157. } jp2_colr_t;
  158. /* PCLR box data. */
  159. typedef struct {
  160. uint_fast16_t numlutents;
  161. uint_fast8_t numchans;
  162. int_fast32_t *lutdata;
  163. uint_fast8_t *bpc;
  164. } jp2_pclr_t;
  165. /* CDEF box per-channel data. */
  166. #define JP2_CDEF_RGB_R 1
  167. #define JP2_CDEF_RGB_G 2
  168. #define JP2_CDEF_RGB_B 3
  169. #define JP2_CDEF_YCBCR_Y 1
  170. #define JP2_CDEF_YCBCR_CB 2
  171. #define JP2_CDEF_YCBCR_CR 3
  172. #define JP2_CDEF_GRAY_Y 1
  173. #define JP2_CDEF_TYPE_COLOR 0
  174. #define JP2_CDEF_TYPE_OPACITY 1
  175. #define JP2_CDEF_TYPE_UNSPEC 65535
  176. #define JP2_CDEF_ASOC_ALL 0
  177. #define JP2_CDEF_ASOC_NONE 65535
  178. typedef struct {
  179. uint_fast16_t channo;
  180. uint_fast16_t type;
  181. uint_fast16_t assoc;
  182. } jp2_cdefchan_t;
  183. /* CDEF box data. */
  184. typedef struct {
  185. uint_fast16_t numchans;
  186. jp2_cdefchan_t *ents;
  187. } jp2_cdef_t;
  188. typedef struct {
  189. uint_fast16_t cmptno;
  190. uint_fast8_t map;
  191. uint_fast8_t pcol;
  192. } jp2_cmapent_t;
  193. typedef struct {
  194. uint_fast16_t numchans;
  195. jp2_cmapent_t *ents;
  196. } jp2_cmap_t;
  197. typedef struct {
  198. uint_fast32_t datalen;
  199. uint_fast8_t uuid[16];
  200. uint_fast8_t *data;
  201. } jp2_uuid_t;
  202. #define JP2_CMAP_DIRECT 0
  203. #define JP2_CMAP_PALETTE 1
  204. /* Generic box. */
  205. struct jp2_boxops_s;
  206. typedef struct {
  207. struct jp2_boxops_s *ops;
  208. struct jp2_boxinfo_s *info;
  209. uint_fast32_t type;
  210. /* The length of the box including the (variable-length) header. */
  211. uint_fast32_t len;
  212. /* The length of the box data. */
  213. uint_fast32_t datalen;
  214. union {
  215. jp2_jp_t jp;
  216. jp2_ftyp_t ftyp;
  217. jp2_ihdr_t ihdr;
  218. jp2_bpcc_t bpcc;
  219. jp2_colr_t colr;
  220. jp2_pclr_t pclr;
  221. jp2_cdef_t cdef;
  222. jp2_cmap_t cmap;
  223. jp2_uuid_t uuid;
  224. } data;
  225. } jp2_box_t;
  226. typedef struct jp2_boxops_s {
  227. void (*init)(jp2_box_t *box);
  228. void (*destroy)(jp2_box_t *box);
  229. int (*getdata)(jp2_box_t *box, jas_stream_t *in);
  230. int (*putdata)(jp2_box_t *box, jas_stream_t *out);
  231. void (*dumpdata)(jp2_box_t *box, FILE *out);
  232. } jp2_boxops_t;
  233. /******************************************************************************\
  234. *
  235. \******************************************************************************/
  236. typedef struct jp2_boxinfo_s {
  237. int type;
  238. char *name;
  239. int flags;
  240. jp2_boxops_t ops;
  241. } jp2_boxinfo_t;
  242. /******************************************************************************\
  243. * Box class.
  244. \******************************************************************************/
  245. jp2_box_t *jp2_box_create(int type);
  246. void jp2_box_destroy(jp2_box_t *box);
  247. jp2_box_t *jp2_box_get(jas_stream_t *in);
  248. int jp2_box_put(jp2_box_t *box, jas_stream_t *out);
  249. #define JP2_DTYPETOBPC(dtype) \
  250. ((JAS_IMAGE_CDT_GETSGND(dtype) << 7) | (JAS_IMAGE_CDT_GETPREC(dtype) - 1))
  251. #define JP2_BPCTODTYPE(bpc) \
  252. (JAS_IMAGE_CDT_SETSGND(bpc >> 7) | JAS_IMAGE_CDT_SETPREC((bpc & 0x7f) + 1))
  253. #define ICC_CS_RGB 0x52474220
  254. #define ICC_CS_YCBCR 0x59436272
  255. #define ICC_CS_GRAY 0x47524159
  256. jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo);
  257. #endif