heif_items.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * HEIF codec.
  3. * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com>
  4. *
  5. * This file is part of libheif.
  6. *
  7. * libheif is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * libheif is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libheif. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef LIBHEIF_HEIF_ITEMS_H
  21. #define LIBHEIF_HEIF_ITEMS_H
  22. #include "libheif/heif.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * Gets the number of items.
  28. *
  29. * This is not the same as the number of images, since there can be other types of items,
  30. * such as metadata.
  31. *
  32. * @param ctx the file context
  33. * @return the number of items
  34. */
  35. LIBHEIF_API
  36. int heif_context_get_number_of_items(const struct heif_context* ctx);
  37. /**
  38. * Get the item identifiers.
  39. *
  40. * Fills in the item IDs into the user-supplied array {@code ID_array}, preallocated with {@code count} entries.
  41. *
  42. * @param ctx the file context
  43. * @param ID_array the output array.
  44. * @param count the number of items allocated within {@code ID_array}.
  45. * @return the total number of IDs filled into the array, which may be less than {@code count}.
  46. */
  47. LIBHEIF_API
  48. int heif_context_get_list_of_item_IDs(const struct heif_context* ctx,
  49. heif_item_id* ID_array,
  50. int count);
  51. /**
  52. * Gets the item type.
  53. *
  54. * Usually, this is a four character code (e.g. `mime` or `uri `), but it can theoretically be
  55. * any 4-byte number. Thus, the type is returned as an integer. You can use {@link heif_fourcc} to map
  56. * between the two representations.
  57. *
  58. * @param ctx the file context
  59. * @param item_id the item identifier for the item
  60. * @return the item type
  61. */
  62. LIBHEIF_API
  63. uint32_t heif_item_get_item_type(const struct heif_context* ctx, heif_item_id item_id);
  64. #define heif_item_type_mime heif_fourcc('m','i','m','e')
  65. #define heif_item_type_uri heif_fourcc('u','r','i',' ')
  66. LIBHEIF_API
  67. int heif_item_is_item_hidden(const struct heif_context* ctx, heif_item_id item_id);
  68. /**
  69. * Gets the MIME content_type for an item.
  70. *
  71. * Only valid if the item type is `mime`.
  72. * If the item does not exist, or if it is not a `mime` item, NULL is returned.
  73. *
  74. * @param ctx the file context
  75. * @param item_id the item identifier for the item
  76. * @return the item content_type
  77. */
  78. LIBHEIF_API
  79. const char* heif_item_get_mime_item_content_type(const struct heif_context* ctx, heif_item_id item_id);
  80. /**
  81. * Gets the content_encoding for a MIME item.
  82. *
  83. * Only valid if the item type is `mime`.
  84. * If the item does not exist, or if it is not a `mime` item, NULL is returned.
  85. *
  86. * If the item is not encoded, the returned value will be an empty string (not null).
  87. *
  88. * @param ctx the file context
  89. * @param item_id the item identifier for the item
  90. * @return the item content_type
  91. */
  92. LIBHEIF_API
  93. const char* heif_item_get_mime_item_content_encoding(const struct heif_context* ctx, heif_item_id item_id);
  94. /**
  95. * Gets the item_uri_type for an item.
  96. *
  97. * Only valid if the item type is `uri `.
  98. * If the item does not exist, or if it is not a `uri ` item, NULL is returned.
  99. *
  100. * @param ctx the file context
  101. * @param item_id the item identifier for the item
  102. * @return the item item_uri_type
  103. */
  104. LIBHEIF_API
  105. const char* heif_item_get_uri_item_uri_type(const struct heif_context* ctx, heif_item_id item_id);
  106. LIBHEIF_API
  107. const char* heif_item_get_item_name(const struct heif_context* ctx, heif_item_id item_id);
  108. LIBHEIF_API
  109. struct heif_error heif_item_set_item_name(struct heif_context* ctx,
  110. heif_item_id item,
  111. const char* item_name);
  112. /**
  113. * Gets the raw metadata, as stored in the HEIF file.
  114. *
  115. * Data in a "mime" item with "content_encoding" can be compressed.
  116. * When `out_compression_format` is NULL, the decompressed data will be returned.
  117. * Otherwise, the compressed data is returned and `out_compression_format` will be filled with the
  118. * compression format.
  119. * If the compression method is not supported, an error will be returned.
  120. *
  121. * It is valid to set `out_data` to NULL. In that case, only the `out_data_size` is filled.
  122. * Note that it is inefficient to use `out_data=NULL` just to get the size of compressed data.
  123. * In general, this should be avoided.
  124. *
  125. * If there is no data assigned to the item or there is an error, `out_data_size` is set to zero.
  126. *
  127. * @param ctx the file context
  128. * @param item_id the item identifier for the item
  129. * @param out_compression_format how the data is compressed. If the pointer is NULL, the decompressed data will be returned.
  130. * @param out_data the corresponding raw metadata
  131. * @param out_data_size the size of the metadata in bytes
  132. * @return whether the call succeeded, or there was an error
  133. */
  134. LIBHEIF_API
  135. struct heif_error heif_item_get_item_data(const struct heif_context* ctx,
  136. heif_item_id item_id,
  137. heif_metadata_compression* out_compression_format,
  138. uint8_t** out_data, size_t* out_data_size);
  139. /**
  140. * Free the item data.
  141. *
  142. * This is used to free memory associated with the data returned by
  143. * {@link heif_item_get_item_data} in 'out_data' and set the pointer to NULL.
  144. *
  145. * @param ctx the file context
  146. * @param item_data the data to free
  147. */
  148. LIBHEIF_API
  149. void heif_release_item_data(const struct heif_context* ctx, uint8_t** item_data);
  150. // ------------------------- item references -------------------------
  151. /**
  152. * Get the item ids that reference the given item.
  153. *
  154. * @param ctx the file context.
  155. * @param from_item_id the item identifier for the item.
  156. * @param index the index of the reference to get.
  157. * @param out_reference_type_4cc The 4cc of the reference. (e.g dimg, thmb, cdsc, or auxl)
  158. * @param out_references_to the item references. Use {@link heif_release_item_references} to free the memory.
  159. * @return the number of items that reference the given item. Returns 0 if the index exceeds the number of references.
  160. */
  161. LIBHEIF_API
  162. size_t heif_context_get_item_references(const struct heif_context* ctx,
  163. heif_item_id from_item_id,
  164. int index,
  165. uint32_t* out_reference_type_4cc,
  166. heif_item_id** out_references_to);
  167. LIBHEIF_API
  168. void heif_release_item_references(const struct heif_context* ctx, heif_item_id** references);
  169. LIBHEIF_API
  170. struct heif_error heif_context_add_item_reference(struct heif_context* ctx,
  171. uint32_t reference_type,
  172. heif_item_id from_item,
  173. heif_item_id to_item);
  174. LIBHEIF_API
  175. struct heif_error heif_context_add_item_references(struct heif_context* ctx,
  176. uint32_t reference_type,
  177. heif_item_id from_item,
  178. const heif_item_id* to_item,
  179. int num_to_items);
  180. // ------------------------- adding new items -------------------------
  181. LIBHEIF_API
  182. struct heif_error heif_context_add_item(struct heif_context* ctx,
  183. const char* item_type,
  184. const void* data, int size,
  185. heif_item_id* out_item_id);
  186. LIBHEIF_API
  187. struct heif_error heif_context_add_mime_item(struct heif_context* ctx,
  188. const char* content_type,
  189. heif_metadata_compression content_encoding,
  190. const void* data, int size,
  191. heif_item_id* out_item_id);
  192. LIBHEIF_API
  193. struct heif_error heif_context_add_precompressed_mime_item(struct heif_context* ctx,
  194. const char* content_type,
  195. const char* content_encoding,
  196. const void* data, int size,
  197. heif_item_id* out_item_id);
  198. LIBHEIF_API
  199. struct heif_error heif_context_add_uri_item(struct heif_context* ctx,
  200. const char* item_uri_type,
  201. const void* data, int size,
  202. heif_item_id* out_item_id);
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif