openslide.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * OpenSlide, a library for reading whole slide image files
  3. *
  4. * Copyright (c) 2007-2014 Carnegie Mellon University
  5. * Copyright (c) 2021 Benjamin Gilbert
  6. * All rights reserved.
  7. *
  8. * OpenSlide 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, version 2.1.
  11. *
  12. * OpenSlide 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
  18. * License along with OpenSlide. If not, see
  19. * <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * @file openslide.h
  24. * The API for the OpenSlide library.
  25. *
  26. * All functions except openslide_close() are thread-safe.
  27. * See the openslide_close() documentation for its restrictions.
  28. */
  29. #ifndef OPENSLIDE_OPENSLIDE_H_
  30. #define OPENSLIDE_OPENSLIDE_H_
  31. #include "openslide-features.h"
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include <stdbool.h>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * The main OpenSlide type.
  40. *
  41. * An @ref openslide_t object can be used concurrently from multiple threads
  42. * without locking. (But you must lock or otherwise use memory barriers
  43. * when passing the object between threads.)
  44. */
  45. typedef struct _openslide openslide_t;
  46. /**
  47. * An OpenSlide tile cache.
  48. *
  49. * An @ref openslide_cache_t object can be used concurrently from multiple
  50. * threads without locking. (But you must lock or otherwise use memory
  51. * barriers when passing the object between threads.)
  52. */
  53. typedef struct _openslide_cache openslide_cache_t;
  54. /**
  55. * @name Basic Usage
  56. * Opening, reading, and closing whole slide images.
  57. */
  58. //@{
  59. /**
  60. * Quickly determine whether a whole slide image is recognized.
  61. *
  62. * If OpenSlide recognizes the file referenced by @p filename, return a
  63. * string identifying the slide format vendor. This is equivalent to the
  64. * value of the #OPENSLIDE_PROPERTY_NAME_VENDOR property. Calling
  65. * openslide_open() on this file will return a valid OpenSlide object or
  66. * an OpenSlide object in error state.
  67. *
  68. * Otherwise, return NULL. Calling openslide_open() on this file will also
  69. * return NULL.
  70. *
  71. * @param filename The filename to check. On Windows, this must be in UTF-8.
  72. * @return An identification of the format vendor for this file, or NULL.
  73. * @since 3.4.0
  74. */
  75. OPENSLIDE_PUBLIC()
  76. const char *openslide_detect_vendor(const char *filename);
  77. /**
  78. * Open a whole slide image.
  79. *
  80. * This function can be expensive; avoid calling it unnecessarily. For
  81. * example, a tile server should not call openslide_open() on every tile
  82. * request. Instead, it should maintain a cache of OpenSlide objects and
  83. * reuse them when possible.
  84. *
  85. * @param filename The filename to open. On Windows, this must be in UTF-8.
  86. * @return
  87. * On success, a new OpenSlide object.
  88. * If the file is not recognized by OpenSlide, NULL.
  89. * If the file is recognized but an error occurred, an OpenSlide
  90. * object in error state.
  91. */
  92. OPENSLIDE_PUBLIC()
  93. openslide_t *openslide_open(const char *filename);
  94. /**
  95. * Get the number of levels in the whole slide image.
  96. *
  97. * @param osr The OpenSlide object.
  98. * @return The number of levels, or -1 if an error occurred.
  99. * @since 3.3.0
  100. */
  101. OPENSLIDE_PUBLIC()
  102. int32_t openslide_get_level_count(openslide_t *osr);
  103. /**
  104. * Get the dimensions of level 0 (the largest level). Exactly
  105. * equivalent to calling openslide_get_level_dimensions(osr, 0, w, h).
  106. *
  107. * @param osr The OpenSlide object.
  108. * @param[out] w The width of the image, or -1 if an error occurred.
  109. * @param[out] h The height of the image, or -1 if an error occurred.
  110. * @since 3.3.0
  111. */
  112. OPENSLIDE_PUBLIC()
  113. void openslide_get_level0_dimensions(openslide_t *osr, int64_t *w, int64_t *h);
  114. /**
  115. * Get the dimensions of a level.
  116. *
  117. * @param osr The OpenSlide object.
  118. * @param level The desired level.
  119. * @param[out] w The width of the image, or -1 if an error occurred
  120. * or the level was out of range.
  121. * @param[out] h The height of the image, or -1 if an error occurred
  122. * or the level was out of range.
  123. * @since 3.3.0
  124. */
  125. OPENSLIDE_PUBLIC()
  126. void openslide_get_level_dimensions(openslide_t *osr, int32_t level,
  127. int64_t *w, int64_t *h);
  128. /**
  129. * Get the downsampling factor of a given level.
  130. *
  131. * @param osr The OpenSlide object.
  132. * @param level The desired level.
  133. * @return The downsampling factor for this level, or -1.0 if an error occurred
  134. * or the level was out of range.
  135. * @since 3.3.0
  136. */
  137. OPENSLIDE_PUBLIC()
  138. double openslide_get_level_downsample(openslide_t *osr, int32_t level);
  139. /**
  140. * Get the best level to use for displaying the given downsample.
  141. *
  142. * @param osr The OpenSlide object.
  143. * @param downsample The downsample factor.
  144. * @return The level identifier, or -1 if an error occurred.
  145. * @since 3.3.0
  146. */
  147. OPENSLIDE_PUBLIC()
  148. int32_t openslide_get_best_level_for_downsample(openslide_t *osr,
  149. double downsample);
  150. /**
  151. * Copy pre-multiplied ARGB data from a whole slide image.
  152. *
  153. * This function reads and decompresses a region of a whole slide
  154. * image into the specified memory location. @p dest must be a valid
  155. * pointer to enough memory to hold the region, at least (@p w * @p h * 4)
  156. * bytes in length. If an error occurs or has occurred, then the memory
  157. * pointed to by @p dest will be cleared.
  158. *
  159. * The returned pixel data is in device color space. The slide's ICC color
  160. * profile, if available, can be read with openslide_read_icc_profile() and
  161. * used to transform the pixels for display.
  162. *
  163. * For more information about processing pre-multiplied pixel data, see
  164. * the [OpenSlide website](https://openslide.org/docs/premultiplied-argb/).
  165. *
  166. * @param osr The OpenSlide object.
  167. * @param dest The destination buffer for the ARGB data.
  168. * @param x The top left x-coordinate, in the level 0 reference frame.
  169. * @param y The top left y-coordinate, in the level 0 reference frame.
  170. * @param level The desired level.
  171. * @param w The width of the region. Must be non-negative.
  172. * @param h The height of the region. Must be non-negative.
  173. */
  174. OPENSLIDE_PUBLIC()
  175. void openslide_read_region(openslide_t *osr,
  176. uint32_t *dest,
  177. int64_t x, int64_t y,
  178. int32_t level,
  179. int64_t w, int64_t h);
  180. /**
  181. * Get the size in bytes of the ICC color profile for the whole slide image.
  182. *
  183. * @param osr The OpenSlide object.
  184. * @return -1 on error, 0 if no profile is available, otherwise the profile
  185. * size in bytes.
  186. */
  187. OPENSLIDE_PUBLIC()
  188. int64_t openslide_get_icc_profile_size(openslide_t *osr);
  189. /**
  190. * Copy the ICC color profile from a whole slide image.
  191. *
  192. * This function reads the ICC color profile from the slide into the specified
  193. * memory location. @p dest must be a valid pointer to enough memory
  194. * to hold the profile. Get the profile size with
  195. * openslide_get_icc_profile_size().
  196. *
  197. * If an error occurs or has occurred, then the memory pointed to by @p dest
  198. * will be cleared.
  199. *
  200. * @param osr The OpenSlide object.
  201. * @param dest The destination buffer for the ICC color profile.
  202. */
  203. OPENSLIDE_PUBLIC()
  204. void openslide_read_icc_profile(openslide_t *osr, void *dest);
  205. /**
  206. * Close an OpenSlide object.
  207. * No other threads may be using the object.
  208. * After this function returns, the object cannot be used anymore.
  209. *
  210. * @param osr The OpenSlide object.
  211. */
  212. OPENSLIDE_PUBLIC()
  213. void openslide_close(openslide_t *osr);
  214. //@}
  215. /**
  216. * @name Error Handling
  217. * A simple mechanism for detecting errors.
  218. *
  219. * Sometimes an unrecoverable error can occur that will invalidate the
  220. * OpenSlide object. (This is typically something like an I/O error or
  221. * data corruption.) When such an error happens in an OpenSlide
  222. * object, the object will move terminally into an error state.
  223. *
  224. * While an object is in an error state, no OpenSlide functions will
  225. * have any effect on it except for openslide_close(). Functions
  226. * that are expected to return values will instead return an error
  227. * value, typically something like NULL or -1. openslide_read_region()
  228. * will clear its destination buffer instead of painting into
  229. * it. openslide_get_error() will return a non-NULL string containing
  230. * an error message. See the documentation for each function for
  231. * details on what is returned in case of error.
  232. *
  233. * This style of error handling allows programs written in C to check
  234. * for errors only when convenient, because the error state is
  235. * terminal and the OpenSlide functions return harmlessly when there
  236. * has been an error.
  237. *
  238. * If writing wrappers for OpenSlide in languages that support
  239. * exceptions, it is recommended that the error state be checked after
  240. * each call and converted into an exception for that language.
  241. */
  242. //@{
  243. /**
  244. * Get the current error string.
  245. *
  246. * For a given OpenSlide object, once this function returns a non-NULL
  247. * value, the only useful operation on the object is to call
  248. * openslide_close() to free its resources.
  249. *
  250. * @param osr The OpenSlide object.
  251. * @return A string describing the original error that caused
  252. * the problem, or NULL if no error has occurred.
  253. * @since 3.2.0
  254. *
  255. */
  256. OPENSLIDE_PUBLIC()
  257. const char *openslide_get_error(openslide_t *osr);
  258. //@}
  259. /**
  260. * @name Predefined Properties
  261. * Some predefined properties.
  262. */
  263. //@{
  264. /**
  265. * The name of the property containing a slide's background color, if any.
  266. * It is represented as an RGB hex triplet.
  267. *
  268. * @since 3.2.3
  269. */
  270. #define OPENSLIDE_PROPERTY_NAME_BACKGROUND_COLOR "openslide.background-color"
  271. /**
  272. * The name of the property containing the height of the rectangle bounding
  273. * the non-empty region of the slide, if available.
  274. *
  275. * @since 3.4.0
  276. */
  277. #define OPENSLIDE_PROPERTY_NAME_BOUNDS_HEIGHT "openslide.bounds-height"
  278. /**
  279. * The name of the property containing the width of the rectangle bounding
  280. * the non-empty region of the slide, if available.
  281. *
  282. * @since 3.4.0
  283. */
  284. #define OPENSLIDE_PROPERTY_NAME_BOUNDS_WIDTH "openslide.bounds-width"
  285. /**
  286. * The name of the property containing the X coordinate of the rectangle
  287. * bounding the non-empty region of the slide, if available.
  288. *
  289. * @since 3.4.0
  290. */
  291. #define OPENSLIDE_PROPERTY_NAME_BOUNDS_X "openslide.bounds-x"
  292. /**
  293. * The name of the property containing the Y coordinate of the rectangle
  294. * bounding the non-empty region of the slide, if available.
  295. *
  296. * @since 3.4.0
  297. */
  298. #define OPENSLIDE_PROPERTY_NAME_BOUNDS_Y "openslide.bounds-y"
  299. /**
  300. * The name of the property containing a slide's comment, if any.
  301. *
  302. * @since 3.0.0
  303. */
  304. #define OPENSLIDE_PROPERTY_NAME_COMMENT "openslide.comment"
  305. /**
  306. * The name of the property containing the size of a slide's ICC color profile,
  307. * if any.
  308. *
  309. * @since 4.0.0
  310. */
  311. #define OPENSLIDE_PROPERTY_NAME_ICC_SIZE "openslide.icc-size"
  312. /**
  313. * The name of the property containing the number of microns per pixel in
  314. * the X dimension of level 0, if known.
  315. *
  316. * @since 3.3.0
  317. */
  318. #define OPENSLIDE_PROPERTY_NAME_MPP_X "openslide.mpp-x"
  319. /**
  320. * The name of the property containing the number of microns per pixel in
  321. * the Y dimension of level 0, if known.
  322. *
  323. * @since 3.3.0
  324. */
  325. #define OPENSLIDE_PROPERTY_NAME_MPP_Y "openslide.mpp-y"
  326. /**
  327. * The name of the property containing a slide's objective power, if known.
  328. *
  329. * @since 3.3.0
  330. */
  331. #define OPENSLIDE_PROPERTY_NAME_OBJECTIVE_POWER "openslide.objective-power"
  332. /**
  333. * The name of the property containing the "quickhash-1" sum.
  334. *
  335. * @since 3.0.0
  336. */
  337. #define OPENSLIDE_PROPERTY_NAME_QUICKHASH1 "openslide.quickhash-1"
  338. /**
  339. * The name of the property containing an identification of the vendor.
  340. *
  341. * @since 3.0.0
  342. */
  343. #define OPENSLIDE_PROPERTY_NAME_VENDOR "openslide.vendor"
  344. //@}
  345. /**
  346. * @name Properties
  347. * Querying properties.
  348. *
  349. * Properties are string key-value pairs containing metadata about a whole
  350. * slide image. These functions allow listing the available properties and
  351. * obtaining their values.
  352. *
  353. * [Some properties](https://openslide.org/properties/) are officially
  354. * documented and are expected to be stable; others are undocumented but may
  355. * still be useful. Many properties are uninterpreted data gathered
  356. * directly from the slide files. New properties may be added in future
  357. * releases of OpenSlide.
  358. */
  359. //@{
  360. /**
  361. * Get the NULL-terminated array of property names.
  362. *
  363. * This function returns an array of strings naming properties available
  364. * in the whole slide image.
  365. *
  366. * @param osr The OpenSlide object.
  367. * @return A NULL-terminated string array of property names, or
  368. * an empty array if an error occurred.
  369. */
  370. OPENSLIDE_PUBLIC()
  371. const char * const *openslide_get_property_names(openslide_t *osr);
  372. /**
  373. * Get the value of a single property.
  374. *
  375. * This function returns the value of the property given by @p name.
  376. *
  377. * @param osr The OpenSlide object.
  378. * @param name The name of the desired property. Must be
  379. a valid name as given by openslide_get_property_names().
  380. * @return The value of the named property, or NULL if the property
  381. * doesn't exist or an error occurred.
  382. */
  383. OPENSLIDE_PUBLIC()
  384. const char *openslide_get_property_value(openslide_t *osr, const char *name);
  385. //@}
  386. /**
  387. * @name Associated Images
  388. * Reading associated images.
  389. *
  390. * Certain vendor-specific associated images may exist within a whole slide
  391. * image, such as label and thumbnail images. Each associated image has a
  392. * name, dimensions, and pixel data. These functions allow listing the
  393. * available associated images and reading their contents.
  394. */
  395. //@{
  396. /**
  397. * Get the NULL-terminated array of associated image names.
  398. *
  399. * This function returns an array of strings naming associated images
  400. * available in the whole slide image.
  401. *
  402. * @param osr The OpenSlide object.
  403. * @return A NULL-terminated string array of associated image names, or
  404. an empty array if an error occurred.
  405. */
  406. OPENSLIDE_PUBLIC()
  407. const char * const *openslide_get_associated_image_names(openslide_t *osr);
  408. /**
  409. * Get the dimensions of an associated image.
  410. *
  411. * This function returns the width and height of an associated image
  412. * associated with a whole slide image. Once the dimensions are known,
  413. * use openslide_read_associated_image() to read the image.
  414. *
  415. * @param osr The OpenSlide object.
  416. * @param name The name of the desired associated image. Must be
  417. * a valid name as given by openslide_get_associated_image_names().
  418. * @param[out] w The width of the associated image, or -1 if an error occurred.
  419. * @param[out] h The height of the associated image, or -1 if an error occurred.
  420. */
  421. OPENSLIDE_PUBLIC()
  422. void openslide_get_associated_image_dimensions(openslide_t *osr,
  423. const char *name,
  424. int64_t *w, int64_t *h);
  425. /**
  426. * Copy pre-multiplied ARGB data from an associated image.
  427. *
  428. * This function reads and decompresses an associated image associated
  429. * with a whole slide image. @p dest must be a valid pointer to enough
  430. * memory to hold the image, at least (width * height * 4) bytes in
  431. * length. Get the width and height with
  432. * openslide_get_associated_image_dimensions().
  433. *
  434. * If an error occurs or has occurred, then the memory pointed to by @p dest
  435. * will be cleared. In versions prior to 4.0.0, this function did nothing
  436. * if an error occurred.
  437. *
  438. * The returned pixel data is in device color space. The associated image's
  439. * ICC color profile, if available, can be read with
  440. * openslide_read_associated_image_icc_profile() and used to transform the
  441. * pixels for display.
  442. *
  443. * For more information about processing pre-multiplied pixel data, see
  444. * the [OpenSlide website](https://openslide.org/docs/premultiplied-argb/).
  445. *
  446. * @param osr The OpenSlide object.
  447. * @param name The name of the desired associated image. Must be
  448. * a valid name as given by openslide_get_associated_image_names().
  449. * @param dest The destination buffer for the ARGB data.
  450. */
  451. OPENSLIDE_PUBLIC()
  452. void openslide_read_associated_image(openslide_t *osr,
  453. const char *name,
  454. uint32_t *dest);
  455. /**
  456. * Get the size in bytes of the ICC color profile for an associated image.
  457. *
  458. * @param osr The OpenSlide object.
  459. * @param name The name of the desired associated image. Must be
  460. * a valid name as given by openslide_get_associated_image_names().
  461. * @return -1 on error, 0 if no profile is available, otherwise the profile
  462. * size in bytes.
  463. */
  464. OPENSLIDE_PUBLIC()
  465. int64_t openslide_get_associated_image_icc_profile_size(openslide_t *osr,
  466. const char *name);
  467. /**
  468. * Copy the ICC color profile from an associated image.
  469. *
  470. * This function reads the ICC color profile from an associated image into
  471. * the specified memory location. @p dest must be a valid pointer to enough
  472. * memory to hold the profile. Get the profile size with
  473. * openslide_get_associated_image_icc_profile_size().
  474. *
  475. * If an error occurs or has occurred, then the memory pointed to by @p dest
  476. * will be cleared.
  477. *
  478. * @param osr The OpenSlide object.
  479. * @param name The name of the desired associated image. Must be
  480. * a valid name as given by openslide_get_associated_image_names().
  481. * @param dest The destination buffer for the ICC color profile.
  482. */
  483. OPENSLIDE_PUBLIC()
  484. void openslide_read_associated_image_icc_profile(openslide_t *osr,
  485. const char *name,
  486. void *dest);
  487. //@}
  488. /**
  489. * @name Caching
  490. * Managing the in-memory tile cache.
  491. *
  492. * By default, each OpenSlide object has its own internal cache. These
  493. * functions can be used to configure a cache with a custom size, which may
  494. * be shared between multiple OpenSlide objects.
  495. */
  496. //@{
  497. /**
  498. * Create a new tile cache, unconnected to any OpenSlide object. The cache
  499. * can be attached to one or more OpenSlide objects with openslide_set_cache().
  500. * The cache must be released with openslide_cache_release() when done.
  501. *
  502. * @param capacity The capacity of the cache, in bytes.
  503. * @return A new cache.
  504. * @since 4.0.0
  505. */
  506. OPENSLIDE_PUBLIC()
  507. openslide_cache_t *openslide_cache_create(size_t capacity);
  508. /**
  509. * Attach a cache to the specified OpenSlide object, replacing the
  510. * current cache.
  511. *
  512. * @param osr The OpenSlide object.
  513. * @param cache The cache to attach.
  514. * @since 4.0.0
  515. */
  516. OPENSLIDE_PUBLIC()
  517. void openslide_set_cache(openslide_t *osr, openslide_cache_t *cache);
  518. /**
  519. * Release the cache. The cache may be released while it is still attached
  520. * to OpenSlide objects. It will be freed once the last attached OpenSlide
  521. * object is closed.
  522. *
  523. * @param cache The cache to release.
  524. * @since 4.0.0
  525. */
  526. OPENSLIDE_PUBLIC()
  527. void openslide_cache_release(openslide_cache_t *cache);
  528. //@}
  529. /**
  530. * @name Miscellaneous
  531. * Utility functions.
  532. */
  533. //@{
  534. /**
  535. * Get the version of the OpenSlide library.
  536. *
  537. * @return A string describing the OpenSlide version.
  538. * @since 3.3.0
  539. */
  540. OPENSLIDE_PUBLIC()
  541. const char *openslide_get_version(void);
  542. //@}
  543. /**
  544. * @mainpage OpenSlide
  545. *
  546. * OpenSlide is a C library that provides a simple interface to read
  547. * whole-slide images (also known as virtual slides). See the
  548. * [OpenSlide website](https://openslide.org/) for more details.
  549. */
  550. #ifdef __cplusplus
  551. }
  552. #endif
  553. #endif