heif_regions.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * HEIF codec.
  3. * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com>
  4. * Copyright (c) 2023 Brad Hards <bradh@frogmouth.net>
  5. *
  6. * This file is part of libheif.
  7. *
  8. * libheif is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as
  10. * published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * libheif is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with libheif. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef LIBHEIF_HEIF_REGIONS_H
  22. #define LIBHEIF_HEIF_REGIONS_H
  23. #include "heif.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. // --- region items and annotations
  28. // See ISO/IEC 23008-12:2022 Section 6.10 "Region items and region annotations"
  29. struct heif_region_item;
  30. /**
  31. * Region type.
  32. *
  33. * Each region item will contain zero or more regions, which may have different geometry or
  34. * mask representations.
  35. */
  36. enum heif_region_type
  37. {
  38. /**
  39. * Point gemetry.
  40. *
  41. * The region is represented by a single point.
  42. */
  43. heif_region_type_point = 0,
  44. /**
  45. * Rectangle geometry.
  46. *
  47. * The region is represented by a top left position, and a size defined
  48. * by a width and height. All of the interior points and the edge are
  49. * part of the region.
  50. */
  51. heif_region_type_rectangle = 1,
  52. /**
  53. * Ellipse geometry.
  54. *
  55. * The region is represented by a centre point, and radii in the X and
  56. * Y directions. All of the interior points and the edge are part of the
  57. * region.
  58. */
  59. heif_region_type_ellipse = 2,
  60. /**
  61. * Polygon geometry.
  62. *
  63. * The region is represented by a sequence of points, which is considered
  64. * implicitly closed. All of the interior points and the edge are part
  65. * of the region.
  66. */
  67. heif_region_type_polygon = 3,
  68. /**
  69. * Reference mask.
  70. *
  71. * The region geometry is described by the pixels in another image item,
  72. * which has a item reference of type `mask` from the region item to the
  73. * image item containing the mask.
  74. *
  75. * The image item containing the mask is one of:
  76. *
  77. * - a mask item (see ISO/IEC 23008-12:2022 Section 6.10.2), or a derived
  78. * image from a mask item
  79. *
  80. * - an image item in monochrome format (4:0:0 chroma)
  81. *
  82. * - an image item in colour format with luma and chroma planes (e.g. 4:2:0)
  83. *
  84. * If the pixel value is equal to the minimum sample value (e.g. 0 for unsigned
  85. * integer), the pixel is not part of the region. If the pixel value is equal
  86. * to the maximum sample value (e.g. 255 for 8 bit unsigned integer), the pixel
  87. * is part of the region. If the pixel value is between the minimum sample value
  88. * and maximum sample value, the pixel value represents an (application defined)
  89. * probability that the pixel is part of the region, where higher pixel values
  90. * correspond to higher probability values.
  91. */
  92. heif_region_type_referenced_mask = 4,
  93. /**
  94. * Inline mask.
  95. *
  96. * The region geometry is described by a sequence of bits stored in inline
  97. * in the region, one bit per pixel. If the bit value is `1`, the pixel is
  98. * part of the region. If the bit value is `0`, the pixel is not part of the
  99. * region.
  100. */
  101. heif_region_type_inline_mask = 5,
  102. /**
  103. * Polyline geometry.
  104. *
  105. * The region is represented by a sequence of points, which are not
  106. * considered to form a closed surface. Only the edge is part of the region.
  107. */
  108. heif_region_type_polyline = 6
  109. };
  110. struct heif_region;
  111. /**
  112. * Get the number of region items that are attached to an image.
  113. *
  114. * @param image_handle the image handle for the image to query.
  115. * @return the number of region items, which can be zero.
  116. */
  117. LIBHEIF_API
  118. int heif_image_handle_get_number_of_region_items(const struct heif_image_handle* image_handle);
  119. /**
  120. * Get the region item identifiers for the region items attached to an image.
  121. *
  122. * Possible usage (in C++):
  123. * @code
  124. * int numRegionItems = heif_image_handle_get_number_of_region_items(handle);
  125. * if (numRegionItems > 0) {
  126. * std::vector<heif_item_id> region_item_ids(numRegionItems);
  127. * heif_image_handle_get_list_of_region_item_ids(handle, region_item_ids.data(), numRegionItems);
  128. * // use region item ids
  129. * }
  130. * @endcode
  131. *
  132. * @param image_handle the image handle for the parent image to query
  133. * @param region_item_ids_array array to put the item identifiers into
  134. * @param max_count the maximum number of region identifiers
  135. * @return the number of region item identifiers that were returned.
  136. */
  137. LIBHEIF_API
  138. int heif_image_handle_get_list_of_region_item_ids(const struct heif_image_handle* image_handle,
  139. heif_item_id* region_item_ids_array,
  140. int max_count);
  141. /**
  142. * Get the region item.
  143. *
  144. * Caller is responsible for release of the output heif_region_item with heif_region_item_release().
  145. *
  146. * @param context the context to get the region item from, usually from a file operation
  147. * @param region_item_id the identifier for the region item
  148. * @param out pointer to pointer to the resulting region item
  149. * @return heif_error_ok on success, or an error value indicating the problem
  150. */
  151. LIBHEIF_API
  152. struct heif_error heif_context_get_region_item(const struct heif_context* context,
  153. heif_item_id region_item_id,
  154. struct heif_region_item** out);
  155. /**
  156. * Get the item identifier for a region item.
  157. *
  158. * @param region_item the region item to query
  159. * @return the region item identifier (or -1 if the region_item is null)
  160. */
  161. LIBHEIF_API
  162. heif_item_id heif_region_item_get_id(struct heif_region_item* region_item);
  163. /**
  164. * Release a region item.
  165. *
  166. * This should be called on items from heif_context_get_region_item().
  167. *
  168. * @param region_item the item to release.
  169. */
  170. LIBHEIF_API
  171. void heif_region_item_release(struct heif_region_item* region_item);
  172. /**
  173. * Get the reference size for a region item.
  174. *
  175. * The reference size specifies the coordinate space used for the region items.
  176. * When the reference size does not match the image size, the regions need to be
  177. * scaled to correspond.
  178. *
  179. * @param out_width the return value for the reference width (before any transformation)
  180. * @param out_height the return value for the reference height (before any transformation)
  181. */
  182. LIBHEIF_API
  183. void heif_region_item_get_reference_size(struct heif_region_item*, uint32_t* out_width, uint32_t* out_height);
  184. /**
  185. * Get the number of regions within a region item.
  186. *
  187. * @param region_item the region item to query.
  188. * @return the number of regions
  189. */
  190. LIBHEIF_API
  191. int heif_region_item_get_number_of_regions(const struct heif_region_item* region_item);
  192. /**
  193. * Get the regions that are part of a region item.
  194. *
  195. * Caller is responsible for releasing the returned `heif_region` objects, using heif_region_release()
  196. * on each region, or heif_region_release_many() on the returned array.
  197. *
  198. * Possible usage (in C++):
  199. * @code
  200. * int num_regions = heif_image_handle_get_number_of_regions(region_item);
  201. * if (num_regions > 0) {
  202. * std::vector<heif_region*> regions(num_regions);
  203. * int n = heif_region_item_get_list_of_regions(region_item, regions.data(), (int)regions.size());
  204. * // use regions
  205. * heif_region_release_many(regions.data(), n);
  206. * }
  207. * @endcode
  208. *
  209. * @param region_item the region_item to query
  210. * @param out_regions_array array to put the region pointers into
  211. * @param max_count the maximum number of regions, which needs to correspond to the size of the out_regions_array
  212. * @return the number of regions that were returned.
  213. */
  214. LIBHEIF_API
  215. int heif_region_item_get_list_of_regions(const struct heif_region_item* region_item,
  216. struct heif_region** out_regions_array,
  217. int max_count);
  218. /**
  219. * Release a region.
  220. *
  221. * This should be called on regions from heif_region_item_get_list_of_regions().
  222. *
  223. * @param region the region to release.
  224. *
  225. * \sa heif_region_release_many() to release the whole list
  226. */
  227. LIBHEIF_API
  228. void heif_region_release(const struct heif_region* region);
  229. /**
  230. * Release a list of regions.
  231. *
  232. * This should be called on the list of regions from heif_region_item_get_list_of_regions().
  233. *
  234. * @param regions_array the regions to release.
  235. * @param num_items the number of items in the array
  236. *
  237. * \sa heif_region_release() to release a single region
  238. */
  239. LIBHEIF_API
  240. void heif_region_release_many(const struct heif_region* const* regions_array, int num_items);
  241. /**
  242. * Get the region type for a specified region.
  243. *
  244. * @param region the region to query
  245. * @return the corresponding region type as an enumeration value
  246. */
  247. LIBHEIF_API
  248. enum heif_region_type heif_region_get_type(const struct heif_region* region);
  249. // When querying the region geometry, there is a version without and a version with "_transformed" suffix.
  250. // The version without returns the coordinates in the reference coordinate space.
  251. // The version with "_transformed" suffix give the coordinates in pixels after all transformative properties have been applied.
  252. /**
  253. * Get the values for a point region.
  254. *
  255. * This returns the coordinates in the reference coordinate space (from the parent region item).
  256. *
  257. * @param region the region to query, which must be of type #heif_region_type_point.
  258. * @param out_x the X coordinate, where 0 is the left-most column.
  259. * @param out_y the Y coordinate, where 0 is the top-most row.
  260. * @return heif_error_ok on success, or an error value indicating the problem on failure
  261. *
  262. * \sa heif_region_get_point_transformed() for a version in pixels after all transformative properties have been applied.
  263. */
  264. LIBHEIF_API
  265. struct heif_error heif_region_get_point(const struct heif_region* region, int32_t* out_x, int32_t* out_y);
  266. /**
  267. * Get the transformed values for a point region.
  268. *
  269. * This returns the coordinates in pixels after all transformative properties have been applied.
  270. *
  271. * @param region the region to query, which must be of type #heif_region_type_point.
  272. * @param image_id the identifier for the image to transform / scale the region to
  273. * @param out_x the X coordinate, where 0 is the left-most column.
  274. * @param out_y the Y coordinate, where 0 is the top-most row.
  275. * @return heif_error_ok on success, or an error value indicating the problem on failure
  276. *
  277. * \sa heif_region_get_point() for a version that returns the values in the reference coordinate space.
  278. */
  279. LIBHEIF_API
  280. struct heif_error heif_region_get_point_transformed(const struct heif_region* region, heif_item_id image_id, double* out_x, double* out_y);
  281. /**
  282. * Get the values for a rectangle region.
  283. *
  284. * This returns the values in the reference coordinate space (from the parent region item).
  285. * The rectangle is represented by a top left corner position, and a size defined
  286. * by a width and height. All of the interior points and the edge are
  287. * part of the region.
  288. *
  289. * @param region the region to query, which must be of type #heif_region_type_rectangle.
  290. * @param out_x the X coordinate for the top left corner, where 0 is the left-most column.
  291. * @param out_y the Y coordinate for the top left corner, where 0 is the top-most row.
  292. * @param out_width the width of the rectangle
  293. * @param out_height the height of the rectangle
  294. * @return heif_error_ok on success, or an error value indicating the problem on failure
  295. *
  296. * \sa heif_region_get_rectangle_transformed() for a version in pixels after all transformative properties have been applied.
  297. */
  298. LIBHEIF_API
  299. struct heif_error heif_region_get_rectangle(const struct heif_region* region,
  300. int32_t* out_x, int32_t* out_y,
  301. uint32_t* out_width, uint32_t* out_height);
  302. /**
  303. * Get the transformed values for a rectangle region.
  304. *
  305. * This returns the coordinates in pixels after all transformative properties have been applied.
  306. * The rectangle is represented by a top left corner position, and a size defined
  307. * by a width and height. All of the interior points and the edge are
  308. * part of the region.
  309. *
  310. * @param region the region to query, which must be of type #heif_region_type_rectangle.
  311. * @param image_id the identifier for the image to transform / scale the region to
  312. * @param out_x the X coordinate for the top left corner, where 0 is the left-most column.
  313. * @param out_y the Y coordinate for the top left corner, where 0 is the top-most row.
  314. * @param out_width the width of the rectangle
  315. * @param out_height the height of the rectangle
  316. * @return heif_error_ok on success, or an error value indicating the problem on failure
  317. *
  318. * \sa heif_region_get_rectangle() for a version that returns the values in the reference coordinate space.
  319. */
  320. LIBHEIF_API
  321. struct heif_error heif_region_get_rectangle_transformed(const struct heif_region* region,
  322. heif_item_id image_id,
  323. double* out_x, double* out_y,
  324. double* out_width, double* out_height);
  325. /**
  326. * Get the values for an ellipse region.
  327. *
  328. * This returns the values in the reference coordinate space (from the parent region item).
  329. * The ellipse is represented by a centre position, and a size defined
  330. * by radii in the X and Y directions. All of the interior points and the edge are
  331. * part of the region.
  332. *
  333. * @param region the region to query, which must be of type #heif_region_type_ellipse.
  334. * @param out_x the X coordinate for the centre point, where 0 is the left-most column.
  335. * @param out_y the Y coordinate for the centre point, where 0 is the top-most row.
  336. * @param out_radius_x the radius value in the X direction.
  337. * @param out_radius_y the radius value in the Y direction
  338. * @return heif_error_ok on success, or an error value indicating the problem on failure
  339. *
  340. * \sa heif_region_get_ellipse_transformed() for a version in pixels after all transformative properties have been applied.
  341. */
  342. LIBHEIF_API
  343. struct heif_error heif_region_get_ellipse(const struct heif_region* region,
  344. int32_t* out_x, int32_t* out_y,
  345. uint32_t* out_radius_x, uint32_t* out_radius_y);
  346. /**
  347. * Get the transformed values for an ellipse region.
  348. *
  349. * This returns the coordinates in pixels after all transformative properties have been applied.
  350. * The ellipse is represented by a centre position, and a size defined
  351. * by radii in the X and Y directions. All of the interior points and the edge are
  352. * part of the region.
  353. *
  354. * @param region the region to query, which must be of type #heif_region_type_ellipse.
  355. * @param image_id the identifier for the image to transform / scale the region to
  356. * @param out_x the X coordinate for the centre point, where 0 is the left-most column.
  357. * @param out_y the Y coordinate for the centre point, where 0 is the top-most row.
  358. * @param out_radius_x the radius value in the X direction.
  359. * @param out_radius_y the radius value in the Y direction
  360. * @return heif_error_ok on success, or an error value indicating the problem on failure
  361. *
  362. * \sa heif_region_get_ellipse() for a version that returns the values in the reference coordinate space.
  363. */
  364. LIBHEIF_API
  365. struct heif_error heif_region_get_ellipse_transformed(const struct heif_region* region,
  366. heif_item_id image_id,
  367. double* out_x, double* out_y,
  368. double* out_radius_x, double* out_radius_y);
  369. /**
  370. * Get the number of points in a polygon.
  371. *
  372. * @param region the region to query, which must be of type #heif_region_type_polygon
  373. * @return the number of points, or -1 on error.
  374. */
  375. LIBHEIF_API
  376. int heif_region_get_polygon_num_points(const struct heif_region* region);
  377. /**
  378. * Get the points in a polygon region.
  379. *
  380. * This returns the values in the reference coordinate space (from the parent region item).
  381. *
  382. * A polygon is a sequence of points that form a closed shape. The first point does
  383. * not need to be repeated as the last point. All of the interior points and the edge are
  384. * part of the region.
  385. * The points are returned as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  386. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  387. *
  388. * @param region the region to equery, which must be of type #heif_region_type_polygon
  389. * @param out_pts_array the array to return the points in, which must have twice as many entries as there are points
  390. * in the polygon.
  391. * @return heif_error_ok on success, or an error value indicating the problem on failure
  392. *
  393. * \sa heif_region_get_polygon_points_transformed() for a version in pixels after all transformative properties have been applied.
  394. */
  395. LIBHEIF_API
  396. struct heif_error heif_region_get_polygon_points(const struct heif_region* region,
  397. int32_t* out_pts_array);
  398. /**
  399. * Get the transformed points in a polygon region.
  400. *
  401. * This returns the coordinates in pixels after all transformative properties have been applied.
  402. *
  403. * A polygon is a sequence of points that form a closed shape. The first point does
  404. * not need to be repeated as the last point. All of the interior points and the edge are
  405. * part of the region.
  406. * The points are returned as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  407. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  408. *
  409. * @param region the region to equery, which must be of type #heif_region_type_polygon
  410. * @param image_id the identifier for the image to transform / scale the region to
  411. * @param out_pts_array the array to return the points in, which must have twice as many entries as there are points
  412. * in the polygon.
  413. * @return heif_error_ok on success, or an error value indicating the problem on failure
  414. *
  415. * \sa heif_region_get_polygon_points() for a version that returns the values in the reference coordinate space.
  416. */
  417. LIBHEIF_API
  418. struct heif_error heif_region_get_polygon_points_transformed(const struct heif_region* region,
  419. heif_item_id image_id,
  420. double* out_pts_array);
  421. /**
  422. * Get the number of points in a polyline.
  423. *
  424. * @param region the region to query, which must be of type #heif_region_type_polyline
  425. * @return the number of points, or -1 on error.
  426. */
  427. LIBHEIF_API
  428. int heif_region_get_polyline_num_points(const struct heif_region* region);
  429. /**
  430. * Get the points in a polyline region.
  431. *
  432. * This returns the values in the reference coordinate space (from the parent region item).
  433. *
  434. * A polyline is a sequence of points that does not form a closed shape. Even if the
  435. * polyline is closed, the only points that are part of the region are those that
  436. * intersect (even minimally) a one-pixel line drawn along the polyline.
  437. * The points are provided as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  438. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  439. *
  440. * Possible usage (in C++):
  441. * @code
  442. * int num_polyline_points = heif_region_get_polyline_num_points(region);
  443. * if (num_polyline_points > 0) {
  444. * std::vector<int32_t> polyline(num_polyline_points * 2);
  445. * heif_region_get_polyline_points(region, polyline.data());
  446. * // do something with points ...
  447. * }
  448. * @endcode
  449. *
  450. * @param region the region to equery, which must be of type #heif_region_type_polyline
  451. * @param out_pts_array the array to return the points in, which must have twice as many entries as there are points
  452. * in the polyline.
  453. * @return heif_error_ok on success, or an error value indicating the problem on failure
  454. *
  455. * \sa heif_region_get_polyline_points_transformed() for a version in pixels after all transformative properties have been applied.
  456. */
  457. LIBHEIF_API
  458. struct heif_error heif_region_get_polyline_points(const struct heif_region* region,
  459. int32_t* out_pts_array);
  460. /**
  461. * Get the transformed points in a polyline region.
  462. *
  463. * This returns the coordinates in pixels after all transformative properties have been applied.
  464. *
  465. * A polyline is a sequence of points that does not form a closed shape. Even if the
  466. * polyline is closed, the only points that are part of the region are those that
  467. * intersect (even minimally) a one-pixel line drawn along the polyline.
  468. * The points are provided as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  469. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  470. *
  471. * @param region the region to query, which must be of type #heif_region_type_polyline
  472. * @param image_id the identifier for the image to transform / scale the region to
  473. * @param out_pts_array the array to return the points in, which must have twice as many entries as there are points
  474. * in the polyline.
  475. * @return heif_error_ok on success, or an error value indicating the problem on failure
  476. *
  477. * \sa heif_region_get_polyline_points() for a version that returns the values in the reference coordinate space.
  478. */
  479. LIBHEIF_API
  480. struct heif_error heif_region_get_polyline_points_transformed(const struct heif_region* region,
  481. heif_item_id image_id,
  482. double* out_pts_array);
  483. /**
  484. * Get a referenced item mask region.
  485. *
  486. * This returns the values in the reference coordinate space (from the parent region item).
  487. * The mask location is represented by a top left corner position, and a size defined
  488. * by a width and height. The value of each sample in that mask identifies whether the
  489. * corresponding pixel is part of the region.
  490. *
  491. * The mask is provided as an image in another item. The image item containing the mask
  492. * is one of:
  493. *
  494. * - a mask item (see ISO/IEC 23008-12:2022 Section 6.10.2), or a derived
  495. * image from a mask item
  496. *
  497. * - an image item in monochrome format (4:0:0 chroma)
  498. *
  499. * - an image item in colour format with luma and chroma planes (e.g. 4:2:0)
  500. *
  501. * If the pixel value is equal to the minimum sample value (e.g. 0 for unsigned
  502. * integer), the pixel is not part of the region. If the pixel value is equal
  503. * to the maximum sample value (e.g. 255 for 8 bit unsigned integer), the pixel
  504. * is part of the region. If the pixel value is between the minimum sample value
  505. * and maximum sample value, the pixel value represents an (application defined)
  506. * probability that the pixel is part of the region, where higher pixel values
  507. * correspond to higher probability values.
  508. *
  509. * @param region the region to query, which must be of type #heif_region_type_referenced_mask.
  510. * @param out_x the X coordinate for the top left corner, where 0 is the left-most column.
  511. * @param out_y the Y coordinate for the top left corner, where 0 is the top-most row.
  512. * @param out_width the width of the mask region
  513. * @param out_height the height of the mask region
  514. * @param out_mask_item_id the item identifier for the image that provides the mask.
  515. * @return heif_error_ok on success, or an error value indicating the problem on failure
  516. */
  517. LIBHEIF_API
  518. struct heif_error heif_region_get_referenced_mask_ID(const struct heif_region* region,
  519. int32_t* out_x, int32_t* out_y,
  520. uint32_t* out_width, uint32_t* out_height,
  521. heif_item_id *out_mask_item_id);
  522. /**
  523. * Get the length of the data in an inline mask region.
  524. *
  525. * @param region the region to query, which must be of type #heif_region_type_inline_mask.
  526. * @return the number of bytes in the mask data, or 0 on error.
  527. */
  528. LIBHEIF_API
  529. size_t heif_region_get_inline_mask_data_len(const struct heif_region* region);
  530. /**
  531. * Get data for an inline mask region.
  532. *
  533. * This returns the values in the reference coordinate space (from the parent region item).
  534. * The mask location is represented by a top left corner position, and a size defined
  535. * by a width and height.
  536. *
  537. * The mask is held as inline data on the region, one bit per pixel, most significant
  538. * bit first pixel, no padding. If the bit value is `1`, the corresponding pixel is
  539. * part of the region. If the bit value is `0`, the corresponding pixel is not part of the
  540. * region.
  541. *
  542. * Possible usage (in C++):
  543. * @code
  544. * long unsigned int data_len = heif_region_get_inline_mask_data_len(region);
  545. * int32_t x, y;
  546. * uint32_t width, height;
  547. * std::vector<uint8_t> mask_data(data_len);
  548. * err = heif_region_get_inline_mask(region, &x, &y, &width, &height, mask_data.data());
  549. * @endcode
  550. *
  551. * @param region the region to query, which must be of type #heif_region_type_inline_mask.
  552. * @param out_x the X coordinate for the top left corner, where 0 is the left-most column.
  553. * @param out_y the Y coordinate for the top left corner, where 0 is the top-most row.
  554. * @param out_width the width of the mask region
  555. * @param out_height the height of the mask region
  556. * @param out_mask_data the location to return the mask data
  557. * @return heif_error_ok on success, or an error value indicating the problem on failure
  558. */
  559. LIBHEIF_API
  560. struct heif_error heif_region_get_inline_mask_data(const struct heif_region* region,
  561. int32_t* out_x, int32_t* out_y,
  562. uint32_t* out_width, uint32_t* out_height,
  563. uint8_t* out_mask_data);
  564. /**
  565. * Get a mask region image.
  566. *
  567. * This returns the values in the reference coordinate space (from the parent region item).
  568. * The mask location is represented by a top left corner position, and a size defined
  569. * by a width and height.
  570. *
  571. * This function works when the passed region is either a heif_region_type_referenced_mask or
  572. * a heif_region_type_inline_mask.
  573. * The returned image is a monochrome image where each pixel represents the (scaled) probability
  574. * of the pixel being part of the mask.
  575. *
  576. * If the region type is an inline mask, which always holds a binary mask, this function
  577. * converts the binary inline mask to an 8-bit monochrome image with the values '0' and '255'.
  578. * The pixel value is set to `255` where the pixel is part of the region, and `0` where the
  579. * pixel is not part of the region.
  580. *
  581. * @param region the region to query, which must be of type #heif_region_type_inline_mask.
  582. * @param out_x the X coordinate for the top left corner, where 0 is the left-most column.
  583. * @param out_y the Y coordinate for the top left corner, where 0 is the top-most row.
  584. * @param out_width the width of the mask region
  585. * @param out_height the height of the mask region
  586. * @param out_mask_image the returned mask image
  587. * @return heif_error_ok on success, or an error value indicating the problem on failure
  588. *
  589. * \note the caller is responsible for releasing the mask image
  590. */
  591. LIBHEIF_API
  592. struct heif_error heif_region_get_mask_image(const struct heif_region* region,
  593. int32_t* out_x, int32_t* out_y,
  594. uint32_t* out_width, uint32_t* out_height,
  595. struct heif_image** out_mask_image);
  596. // --- adding region items
  597. /**
  598. * Add a region item to an image.
  599. *
  600. * The region item is a collection of regions (point, polyline, polygon, rectangle, ellipse or mask)
  601. * along with a reference size (width and height) that forms the coordinate basis for the regions.
  602. *
  603. * The concept is to add the region item, then add one or more regions to the region item.
  604. *
  605. * @param image_handle the image to attach the region item to.
  606. * @param reference_width the width of the reference size.
  607. * @param reference_height the height of the reference size.
  608. * @param out_region_item the resulting region item
  609. * @return heif_error_ok on success, or an error indicating the problem on failure
  610. */
  611. LIBHEIF_API
  612. struct heif_error heif_image_handle_add_region_item(struct heif_image_handle* image_handle,
  613. uint32_t reference_width, uint32_t reference_height,
  614. struct heif_region_item** out_region_item);
  615. /**
  616. * Add a point region to the region item.
  617. *
  618. * @param region_item the region item that holds this point region
  619. * @param x the x value for the point location
  620. * @param y the y value for the point location
  621. * @param out_region pointer to pointer to the returned region (optional, see below)
  622. * @return heif_error_ok on success, or an error indicating the problem on failure
  623. *
  624. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  625. */
  626. LIBHEIF_API
  627. struct heif_error heif_region_item_add_region_point(struct heif_region_item* region_item,
  628. int32_t x, int32_t y,
  629. struct heif_region** out_region);
  630. /**
  631. * Add a rectangle region to the region item.
  632. *
  633. * @param region_item the region item that holds this rectangle region
  634. * @param x the x value for the top-left corner of this rectangle region
  635. * @param y the y value for the top-left corner of this rectangle region
  636. * @param width the width of this rectangle region
  637. * @param height the height of this rectangle region
  638. * @param out_region pointer to pointer to the returned region (optional, see below)
  639. * @return heif_error_ok on success, or an error indicating the problem on failure
  640. *
  641. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  642. */
  643. LIBHEIF_API
  644. struct heif_error heif_region_item_add_region_rectangle(struct heif_region_item* region_item,
  645. int32_t x, int32_t y,
  646. uint32_t width, uint32_t height,
  647. struct heif_region** out_region);
  648. /**
  649. * Add a ellipse region to the region item.
  650. *
  651. * @param region_item the region item that holds this ellipse region
  652. * @param x the x value for the centre of this ellipse region
  653. * @param y the y value for the centre of this ellipse region
  654. * @param radius_x the radius of the ellipse in the X (horizontal) direction
  655. * @param radius_y the radius of the ellipse in the Y (vertical) direction
  656. * @param out_region pointer to pointer to the returned region (optional, see below)
  657. * @return heif_error_ok on success, or an error indicating the problem on failure
  658. *
  659. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  660. */
  661. LIBHEIF_API
  662. struct heif_error heif_region_item_add_region_ellipse(struct heif_region_item* region_item,
  663. int32_t x, int32_t y,
  664. uint32_t radius_x, uint32_t radius_y,
  665. struct heif_region** out_region);
  666. /**
  667. * Add a polygon region to the region item.
  668. *
  669. * A polygon is a sequence of points that form a closed shape. The first point does
  670. * not need to be repeated as the last point.
  671. * The points are provided as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  672. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  673. *
  674. * @param region_item the region item that holds this polygon region
  675. * @param pts_array the array of points in X,Y order (see above)
  676. * @param nPoints the number of points
  677. * @param out_region pointer to pointer to the returned region (optional, see below)
  678. * @return heif_error_ok on success, or an error indicating the problem on failure
  679. *
  680. * @note `nPoints` is the number of points, not the number of elements in the array
  681. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  682. */
  683. LIBHEIF_API
  684. struct heif_error heif_region_item_add_region_polygon(struct heif_region_item* region_item,
  685. const int32_t* pts_array, int nPoints,
  686. struct heif_region** out_region);
  687. /**
  688. * Add a polyline region to the region item.
  689. *
  690. * A polyline is a sequence of points that does not form a closed shape. Even if the
  691. * polyline is closed, the only points that are part of the region are those that
  692. * intersect (even minimally) a one-pixel line drawn along the polyline.
  693. * The points are provided as pairs of X,Y coordinates, in the order X<sub>1</sub>,
  694. * Y<sub>1</sub>, X<sub>2</sub>, Y<sub>2</sub>, ..., X<sub>n</sub>, Y<sub>n</sub>.
  695. *
  696. * @param region_item the region item that holds this polyline region
  697. * @param pts_array the array of points in X,Y order (see above)
  698. * @param nPoints the number of points
  699. * @param out_region pointer to pointer to the returned region (optional, see below)
  700. * @return heif_error_ok on success, or an error indicating the problem on failure
  701. *
  702. * @note `nPoints` is the number of points, not the number of elements in the array
  703. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  704. */
  705. LIBHEIF_API
  706. struct heif_error heif_region_item_add_region_polyline(struct heif_region_item* region_item,
  707. const int32_t* pts_array, int nPoints,
  708. struct heif_region** out_region);
  709. /**
  710. * Add a referenced mask region to the region item.
  711. *
  712. * The region geometry is described by the pixels in another image item,
  713. * which has a item reference of type `mask` from the region item to the
  714. * image item containing the mask.
  715. *
  716. * The image item containing the mask is one of:
  717. *
  718. * - a mask item (see ISO/IEC 23008-12:2022 Section 6.10.2), or a derived
  719. * image from a mask item
  720. *
  721. * - an image item in monochrome format (4:0:0 chroma)
  722. *
  723. * - an image item in colour format with luma and chroma planes (e.g. 4:2:0)
  724. *
  725. * If the pixel value is equal to the minimum sample value (e.g. 0 for unsigned
  726. * integer), the pixel is not part of the region. If the pixel value is equal
  727. * to the maximum sample value (e.g. 255 for 8 bit unsigned integer), the pixel
  728. * is part of the region. If the pixel value is between the minimum sample value
  729. * and maximum sample value, the pixel value represents an (application defined)
  730. * probability that the pixel is part of the region, where higher pixel values
  731. * correspond to higher probability values.
  732. *
  733. * @param region_item the region item that holds this mask region
  734. * @param x the x value for the top-left corner of this mask region
  735. * @param y the y value for the top-left corner of this mask region
  736. * @param width the width of this mask region
  737. * @param height the height of this mask region
  738. * @param mask_item_id the item identifier for the mask that is referenced
  739. * @param out_region pointer to pointer to the returned region (optional, see below)
  740. * @return heif_error_ok on success, or an error indicating the problem on failure
  741. *
  742. * @note The `out_region` parameter is optional, and can be set to `NULL` if not needed.
  743. */
  744. LIBHEIF_API
  745. struct heif_error heif_region_item_add_region_referenced_mask(struct heif_region_item* region_item,
  746. int32_t x, int32_t y,
  747. uint32_t width, uint32_t height,
  748. heif_item_id mask_item_id,
  749. struct heif_region** out_region);
  750. /**
  751. * Add an inline mask region to the region item.
  752. *
  753. * The region geometry is described by a top left corner position, and a size defined
  754. * by a width and height.
  755. *
  756. * The mask is held as inline data on the region, one bit per pixel, most significant
  757. * bit first pixel, no padding. If the bit value is `1`, the corresponding pixel is
  758. * part of the region. If the bit value is `0`, the corresponding pixel is not part of the
  759. * region.
  760. *
  761. * @param region_item the region item that holds this mask region
  762. * @param x the x value for the top-left corner of this mask region
  763. * @param y the y value for the top-left corner of this mask region
  764. * @param width the width of this mask region
  765. * @param height the height of this mask region
  766. * @param mask_data the location to return the mask data
  767. * @param mask_data_len the length of the mask data, in bytes
  768. * @param out_region pointer to pointer to the returned region (optional, see below)
  769. * @return heif_error_ok on success, or an error value indicating the problem on failure
  770. */
  771. LIBHEIF_API
  772. struct heif_error heif_region_item_add_region_inline_mask_data(struct heif_region_item* region_item,
  773. int32_t x, int32_t y,
  774. uint32_t width, uint32_t height,
  775. const uint8_t* mask_data,
  776. size_t mask_data_len,
  777. struct heif_region** out_region);
  778. /**
  779. * Add an inline mask region image to the region item.
  780. *
  781. * The region geometry is described by a top left corner position, and a size defined
  782. * by a width and height.
  783. *
  784. * The mask data is held as inline data on the region, one bit per pixel. The provided
  785. * image is converted to inline data, where any pixel with a value >= 0x80 becomes part of the
  786. * mask region. If the image width is less that the specified width, it is expanded
  787. * to match the width of the region (zero fill on the right). If the image height is
  788. * less than the specified height, it is expanded to match the height of the region
  789. * (zero fill on the bottom). If the image width or height is greater than the
  790. * width or height (correspondingly) of the region, the image is cropped.
  791. *
  792. * @param region_item the region item that holds this mask region
  793. * @param x the x value for the top-left corner of this mask region
  794. * @param y the y value for the top-left corner of this mask region
  795. * @param width the width of this mask region
  796. * @param height the height of this mask region
  797. * @param image the image to convert to an inline mask
  798. * @param out_region pointer to pointer to the returned region (optional, see below)
  799. * @return heif_error_ok on success, or an error value indicating the problem on failure
  800. */
  801. LIBHEIF_API
  802. struct heif_error heif_region_item_add_region_inline_mask(struct heif_region_item* region_item,
  803. int32_t x, int32_t y,
  804. uint32_t width, uint32_t height,
  805. struct heif_image* image,
  806. struct heif_region** out_region);
  807. #ifdef __cplusplus
  808. }
  809. #endif
  810. #endif