openexr_attr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. ** SPDX-License-Identifier: BSD-3-Clause
  3. ** Copyright Contributors to the OpenEXR Project.
  4. */
  5. #ifndef OPENEXR_ATTR_H
  6. #define OPENEXR_ATTR_H
  7. #include "openexr_context.h"
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /** @file */
  14. /**
  15. * @defgroup AttributeTypes Attribute/metadata value types and struct declarations
  16. *
  17. * @brief These are a group of enum values defining valid values for
  18. * some attributes and then associated structs for other types.
  19. *
  20. * Some of these types will be directly representable/storable in
  21. * the file, some not. There is some overlap here with Imath, and they
  22. * should be kept in the same order for compatibility. However do note
  23. * that these are just the raw data, and no useful functions are
  24. * declared at this layer, that is what Imath is for.
  25. *
  26. * @{
  27. */
  28. /** Enum declaring allowed values for \c uint8_t value stored in built-in compression type. */
  29. typedef enum
  30. {
  31. EXR_COMPRESSION_NONE = 0,
  32. EXR_COMPRESSION_RLE = 1,
  33. EXR_COMPRESSION_ZIPS = 2,
  34. EXR_COMPRESSION_ZIP = 3,
  35. EXR_COMPRESSION_PIZ = 4,
  36. EXR_COMPRESSION_PXR24 = 5,
  37. EXR_COMPRESSION_B44 = 6,
  38. EXR_COMPRESSION_B44A = 7,
  39. EXR_COMPRESSION_DWAA = 8,
  40. EXR_COMPRESSION_DWAB = 9,
  41. EXR_COMPRESSION_LAST_TYPE /**< Invalid value, provided for range checking. */
  42. } exr_compression_t;
  43. /** Enum declaring allowed values for \c uint8_t value stored in built-in env map type. */
  44. typedef enum
  45. {
  46. EXR_ENVMAP_LATLONG = 0,
  47. EXR_ENVMAP_CUBE = 1,
  48. EXR_ENVMAP_LAST_TYPE /**< Invalid value, provided for range checking. */
  49. } exr_envmap_t;
  50. /** Enum declaring allowed values for \c uint8_t value stored in \c lineOrder type. */
  51. typedef enum
  52. {
  53. EXR_LINEORDER_INCREASING_Y = 0,
  54. EXR_LINEORDER_DECREASING_Y = 1,
  55. EXR_LINEORDER_RANDOM_Y = 2,
  56. EXR_LINEORDER_LAST_TYPE /**< Invalid value, provided for range checking. */
  57. } exr_lineorder_t;
  58. /** Enum declaring allowed values for part type. */
  59. typedef enum
  60. {
  61. EXR_STORAGE_SCANLINE = 0, /**< Corresponds to type of \c scanlineimage. */
  62. EXR_STORAGE_TILED, /**< Corresponds to type of \c tiledimage. */
  63. EXR_STORAGE_DEEP_SCANLINE, /**< Corresponds to type of \c deepscanline. */
  64. EXR_STORAGE_DEEP_TILED, /**< Corresponds to type of \c deeptile. */
  65. EXR_STORAGE_LAST_TYPE /**< Invalid value, provided for range checking. */
  66. } exr_storage_t;
  67. /** @brief Enum representing what type of tile information is contained. */
  68. typedef enum
  69. {
  70. EXR_TILE_ONE_LEVEL = 0, /**< Single level of image data. */
  71. EXR_TILE_MIPMAP_LEVELS = 1, /**< Mipmapped image data. */
  72. EXR_TILE_RIPMAP_LEVELS = 2, /**< Ripmapped image data. */
  73. EXR_TILE_LAST_TYPE /**< Invalid value, provided for range checking. */
  74. } exr_tile_level_mode_t;
  75. /** @brief Enum representing how to scale positions between levels. */
  76. typedef enum
  77. {
  78. EXR_TILE_ROUND_DOWN = 0,
  79. EXR_TILE_ROUND_UP = 1,
  80. EXR_TILE_ROUND_LAST_TYPE
  81. } exr_tile_round_mode_t;
  82. /** @brief Enum capturing the underlying data type on a channel. */
  83. typedef enum
  84. {
  85. EXR_PIXEL_UINT = 0,
  86. EXR_PIXEL_HALF = 1,
  87. EXR_PIXEL_FLOAT = 2,
  88. EXR_PIXEL_LAST_TYPE
  89. } exr_pixel_type_t;
  90. /* /////////////////////////////////////// */
  91. /* First set of structs are data where we can read directly with no allocation needed... */
  92. /* Most are naturally aligned, but force some of these
  93. * structs to be tightly packed
  94. */
  95. #pragma pack(push, 1)
  96. /** @brief Struct to hold color chromaticities to interpret the tristimulus color values in the image data. */
  97. typedef struct
  98. {
  99. float red_x;
  100. float red_y;
  101. float green_x;
  102. float green_y;
  103. float blue_x;
  104. float blue_y;
  105. float white_x;
  106. float white_y;
  107. } exr_attr_chromaticities_t;
  108. /** @brief Struct to hold keycode information. */
  109. typedef struct
  110. {
  111. int32_t film_mfc_code;
  112. int32_t film_type;
  113. int32_t prefix;
  114. int32_t count;
  115. int32_t perf_offset;
  116. int32_t perfs_per_frame;
  117. int32_t perfs_per_count;
  118. } exr_attr_keycode_t;
  119. /** @brief struct to hold a 32-bit floating-point 3x3 matrix. */
  120. typedef struct
  121. {
  122. float m[9];
  123. } exr_attr_m33f_t;
  124. /** @brief struct to hold a 64-bit floating-point 3x3 matrix. */
  125. typedef struct
  126. {
  127. double m[9];
  128. } exr_attr_m33d_t;
  129. /** @brief Struct to hold a 32-bit floating-point 4x4 matrix. */
  130. typedef struct
  131. {
  132. float m[16];
  133. } exr_attr_m44f_t;
  134. /** @brief Struct to hold a 64-bit floating-point 4x4 matrix. */
  135. typedef struct
  136. {
  137. double m[16];
  138. } exr_attr_m44d_t;
  139. /** @brief Struct to hold an integer ratio value. */
  140. typedef struct
  141. {
  142. int32_t num;
  143. uint32_t denom;
  144. } exr_attr_rational_t;
  145. /** @brief Struct to hold timecode information. */
  146. typedef struct
  147. {
  148. uint32_t time_and_flags;
  149. uint32_t user_data;
  150. } exr_attr_timecode_t;
  151. /** @brief Struct to hold a 2-element integer vector. */
  152. typedef struct
  153. {
  154. union
  155. {
  156. struct
  157. {
  158. int32_t x, y;
  159. };
  160. int32_t arr[2];
  161. };
  162. } exr_attr_v2i_t;
  163. /** @brief Struct to hold a 2-element 32-bit float vector. */
  164. typedef struct
  165. {
  166. union
  167. {
  168. struct
  169. {
  170. float x, y;
  171. };
  172. float arr[2];
  173. };
  174. } exr_attr_v2f_t;
  175. /** @brief Struct to hold a 2-element 64-bit float vector. */
  176. typedef struct
  177. {
  178. union
  179. {
  180. struct
  181. {
  182. double x, y;
  183. };
  184. double arr[2];
  185. };
  186. } exr_attr_v2d_t;
  187. /** @brief Struct to hold a 3-element integer vector. */
  188. typedef struct
  189. {
  190. union
  191. {
  192. struct
  193. {
  194. int32_t x, y, z;
  195. };
  196. int32_t arr[3];
  197. };
  198. } exr_attr_v3i_t;
  199. /** @brief Struct to hold a 3-element 32-bit float vector. */
  200. typedef struct
  201. {
  202. union
  203. {
  204. struct
  205. {
  206. float x, y, z;
  207. };
  208. float arr[3];
  209. };
  210. } exr_attr_v3f_t;
  211. /** @brief Struct to hold a 3-element 64-bit float vector. */
  212. typedef struct
  213. {
  214. union
  215. {
  216. struct
  217. {
  218. double x, y, z;
  219. };
  220. double arr[3];
  221. };
  222. } exr_attr_v3d_t;
  223. /** @brief Struct to hold an integer box/region definition. */
  224. typedef struct
  225. {
  226. exr_attr_v2i_t min;
  227. exr_attr_v2i_t max;
  228. } exr_attr_box2i_t;
  229. /** @brief Struct to hold a floating-point box/region definition. */
  230. typedef struct
  231. {
  232. exr_attr_v2f_t min;
  233. exr_attr_v2f_t max;
  234. } exr_attr_box2f_t;
  235. /** @brief Struct holding base tiledesc attribute type defined in spec
  236. *
  237. * NB: This is in a tightly packed area so it can be read directly, be
  238. * careful it doesn't become padded to the next \c uint32_t boundary.
  239. */
  240. typedef struct
  241. {
  242. uint32_t x_size;
  243. uint32_t y_size;
  244. uint8_t level_and_round;
  245. } exr_attr_tiledesc_t;
  246. /** @brief Macro to access type of tiling from packed structure. */
  247. #define EXR_GET_TILE_LEVEL_MODE(tiledesc) \
  248. ((exr_tile_level_mode_t) (((tiledesc).level_and_round) & 0xF))
  249. /** @brief Macro to access the rounding mode of tiling from packed structure. */
  250. #define EXR_GET_TILE_ROUND_MODE(tiledesc) \
  251. ((exr_tile_round_mode_t) ((((tiledesc).level_and_round) >> 4) & 0xF))
  252. /** @brief Macro to pack the tiling type and rounding mode into packed structure. */
  253. #define EXR_PACK_TILE_LEVEL_ROUND(lvl, mode) \
  254. ((uint8_t) ((((uint8_t) ((mode) &0xF) << 4)) | ((uint8_t) ((lvl) &0xF))))
  255. #pragma pack(pop)
  256. /* /////////////////////////////////////// */
  257. /* Now structs that involve heap allocation to store data. */
  258. /** Storage for a string. */
  259. typedef struct
  260. {
  261. int32_t length;
  262. /** If this is non-zero, the string owns the data, if 0, is a const ref to a static string. */
  263. int32_t alloc_size;
  264. const char* str;
  265. } exr_attr_string_t;
  266. /** Storage for a string vector. */
  267. typedef struct
  268. {
  269. int32_t n_strings;
  270. /** If this is non-zero, the string vector owns the data, if 0, is a const ref. */
  271. int32_t alloc_size;
  272. const exr_attr_string_t* strings;
  273. } exr_attr_string_vector_t;
  274. /** Float vector storage struct. */
  275. typedef struct
  276. {
  277. int32_t length;
  278. /** If this is non-zero, the float vector owns the data, if 0, is a const ref. */
  279. int32_t alloc_size;
  280. const float* arr;
  281. } exr_attr_float_vector_t;
  282. /** Hint for lossy compression methods about how to treat values
  283. * (logarithmic or linear), meaning a human sees values like R, G, B,
  284. * luminance difference between 0.1 and 0.2 as about the same as 1.0
  285. * to 2.0 (logarithmic), where chroma coordinates are closer to linear
  286. * (0.1 and 0.2 is about the same difference as 1.0 and 1.1).
  287. */
  288. typedef enum
  289. {
  290. EXR_PERCEPTUALLY_LOGARITHMIC = 0,
  291. EXR_PERCEPTUALLY_LINEAR = 1
  292. } exr_perceptual_treatment_t;
  293. /** Individual channel information. */
  294. typedef struct
  295. {
  296. exr_attr_string_t name;
  297. /** Data representation for these pixels: uint, half, float. */
  298. exr_pixel_type_t pixel_type;
  299. /** Possible values are 0 and 1 per docs exr_perceptual_treatment_t. */
  300. uint8_t p_linear;
  301. uint8_t reserved[3];
  302. int32_t x_sampling;
  303. int32_t y_sampling;
  304. } exr_attr_chlist_entry_t;
  305. /** List of channel information (sorted alphabetically). */
  306. typedef struct
  307. {
  308. int num_channels;
  309. int num_alloced;
  310. const exr_attr_chlist_entry_t* entries;
  311. } exr_attr_chlist_t;
  312. /** @brief Struct to define attributes of an embedded preview image. */
  313. typedef struct
  314. {
  315. uint32_t width;
  316. uint32_t height;
  317. /** If this is non-zero, the preview owns the data, if 0, is a const ref. */
  318. size_t alloc_size;
  319. const uint8_t* rgba;
  320. } exr_attr_preview_t;
  321. /** Custom storage structure for opaque data.
  322. *
  323. * Handlers for opaque types can be registered, then when a
  324. * non-builtin type is encountered with a registered handler, the
  325. * function pointers to unpack/pack it will be set up.
  326. *
  327. * @sa exr_register_attr_type_handler
  328. */
  329. typedef struct
  330. {
  331. int32_t size;
  332. int32_t unpacked_size;
  333. /** If this is non-zero, the struct owns the data, if 0, is a const ref. */
  334. int32_t packed_alloc_size;
  335. uint8_t pad[4];
  336. void* packed_data;
  337. /** When an application wants to have custom data, they can store
  338. * an unpacked form here which will be requested to be destroyed
  339. * upon destruction of the attribute.
  340. */
  341. void* unpacked_data;
  342. /** An application can register an attribute handler which then
  343. * fills in these function pointers. This allows a user to delay
  344. * the expansion of the custom type until access is desired, and
  345. * similarly, to delay the packing of the data until write time.
  346. */
  347. exr_result_t (*unpack_func_ptr) (
  348. exr_context_t ctxt,
  349. const void* data,
  350. int32_t attrsize,
  351. int32_t* outsize,
  352. void** outbuffer);
  353. exr_result_t (*pack_func_ptr) (
  354. exr_context_t ctxt,
  355. const void* data,
  356. int32_t datasize,
  357. int32_t* outsize,
  358. void* outbuffer);
  359. void (*destroy_unpacked_func_ptr) (
  360. exr_context_t ctxt, void* data, int32_t attrsize);
  361. } exr_attr_opaquedata_t;
  362. /* /////////////////////////////////////// */
  363. /** @brief Built-in/native attribute type enum.
  364. *
  365. * This will enable us to do a tagged type struct to generically store
  366. * attributes.
  367. */
  368. typedef enum
  369. {
  370. EXR_ATTR_UNKNOWN =
  371. 0, /**< Type indicating an error or uninitialized attribute. */
  372. EXR_ATTR_BOX2I, /**< Integer region definition. @see exr_attr_box2i_t. */
  373. EXR_ATTR_BOX2F, /**< Float region definition. @see exr_attr_box2f_t. */
  374. EXR_ATTR_CHLIST, /**< Definition of channels in file @see exr_chlist_entry. */
  375. EXR_ATTR_CHROMATICITIES, /**< Values to specify color space of colors in file @see exr_attr_chromaticities_t. */
  376. EXR_ATTR_COMPRESSION, /**< ``uint8_t`` declaring compression present. */
  377. EXR_ATTR_DOUBLE, /**< Double precision floating point number. */
  378. EXR_ATTR_ENVMAP, /**< ``uint8_t`` declaring environment map type. */
  379. EXR_ATTR_FLOAT, /**< Normal (4 byte) precision floating point number. */
  380. EXR_ATTR_FLOAT_VECTOR, /**< List of normal (4 byte) precision floating point numbers. */
  381. EXR_ATTR_INT, /**< 32-bit signed integer value. */
  382. EXR_ATTR_KEYCODE, /**< Struct recording keycode @see exr_attr_keycode_t. */
  383. EXR_ATTR_LINEORDER, /**< ``uint8_t`` declaring scanline ordering. */
  384. EXR_ATTR_M33F, /**< 9 32-bit floats representing a 3x3 matrix. */
  385. EXR_ATTR_M33D, /**< 9 64-bit floats representing a 3x3 matrix. */
  386. EXR_ATTR_M44F, /**< 16 32-bit floats representing a 4x4 matrix. */
  387. EXR_ATTR_M44D, /**< 16 64-bit floats representing a 4x4 matrix. */
  388. EXR_ATTR_PREVIEW, /**< 2 ``unsigned ints`` followed by 4 x w x h ``uint8_t`` image. */
  389. EXR_ATTR_RATIONAL, /**< \c int followed by ``unsigned int`` */
  390. EXR_ATTR_STRING, /**< ``int`` (length) followed by char string data. */
  391. EXR_ATTR_STRING_VECTOR, /**< 0 or more text strings (int + string). number is based on attribute size. */
  392. EXR_ATTR_TILEDESC, /**< 2 ``unsigned ints`` ``xSize``, ``ySize`` followed by mode. */
  393. EXR_ATTR_TIMECODE, /**< 2 ``unsigned ints`` time and flags, user data. */
  394. EXR_ATTR_V2I, /**< Pair of 32-bit integers. */
  395. EXR_ATTR_V2F, /**< Pair of 32-bit floats. */
  396. EXR_ATTR_V2D, /**< Pair of 64-bit floats. */
  397. EXR_ATTR_V3I, /**< Set of 3 32-bit integers. */
  398. EXR_ATTR_V3F, /**< Set of 3 32-bit floats. */
  399. EXR_ATTR_V3D, /**< Set of 3 64-bit floats. */
  400. EXR_ATTR_OPAQUE, /**< User/unknown provided type. */
  401. EXR_ATTR_LAST_KNOWN_TYPE
  402. } exr_attribute_type_t;
  403. /** @brief Storage, name and type information for an attribute.
  404. *
  405. * Attributes (metadata) for the file cause a surprising amount of
  406. * overhead. It is not uncommon for a production-grade EXR to have
  407. * many attributes. As such, the attribute struct is designed in a
  408. * slightly more complicated manner. It is optimized to have the
  409. * storage for that attribute: the struct itself, the name, the type,
  410. * and the data all allocated as one block. Further, the type and
  411. * standard names may use a static string to avoid allocating space
  412. * for those as necessary with the pointers pointing to static strings
  413. * (not to be freed). Finally, small values are optimized for.
  414. */
  415. typedef struct
  416. {
  417. /** Name of the attribute. */
  418. const char* name;
  419. /** String type name of the attribute. */
  420. const char* type_name;
  421. /** Length of name string (short flag is 31 max, long allows 255). */
  422. uint8_t name_length;
  423. /** Length of type string (short flag is 31 max, long allows 255). */
  424. uint8_t type_name_length;
  425. uint8_t pad[2];
  426. /** Enum of the attribute type. */
  427. exr_attribute_type_t type;
  428. /** Union of pointers of different types that can be used to type
  429. * pun to an appropriate type for builtins. Do note that while
  430. * this looks like a big thing, it is only the size of a single
  431. * pointer. These are all pointers into some other data block
  432. * storing the value you want, with the exception of the pod types
  433. * which are just put in place (i.e. small value optimization).
  434. *
  435. * The attribute type \c type should directly correlate to one
  436. * of these entries.
  437. */
  438. union
  439. {
  440. // NB: not pointers for POD types
  441. uint8_t uc;
  442. double d;
  443. float f;
  444. int32_t i;
  445. exr_attr_box2i_t* box2i;
  446. exr_attr_box2f_t* box2f;
  447. exr_attr_chlist_t* chlist;
  448. exr_attr_chromaticities_t* chromaticities;
  449. exr_attr_keycode_t* keycode;
  450. exr_attr_float_vector_t* floatvector;
  451. exr_attr_m33f_t* m33f;
  452. exr_attr_m33d_t* m33d;
  453. exr_attr_m44f_t* m44f;
  454. exr_attr_m44d_t* m44d;
  455. exr_attr_preview_t* preview;
  456. exr_attr_rational_t* rational;
  457. exr_attr_string_t* string;
  458. exr_attr_string_vector_t* stringvector;
  459. exr_attr_tiledesc_t* tiledesc;
  460. exr_attr_timecode_t* timecode;
  461. exr_attr_v2i_t* v2i;
  462. exr_attr_v2f_t* v2f;
  463. exr_attr_v2d_t* v2d;
  464. exr_attr_v3i_t* v3i;
  465. exr_attr_v3f_t* v3f;
  466. exr_attr_v3d_t* v3d;
  467. exr_attr_opaquedata_t* opaque;
  468. uint8_t* rawptr;
  469. };
  470. } exr_attribute_t;
  471. /** @} */
  472. #ifdef __cplusplus
  473. } /* extern "C" */
  474. #endif
  475. #endif /* OPENEXR_ATTR_H */