gain_map.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (c) the JPEG XL Project Authors. All rights reserved.
  2. *
  3. * Use of this source code is governed by a BSD-style
  4. * license that can be found in the LICENSE file.
  5. */
  6. /** @addtogroup libjxl_metadata
  7. * @{
  8. * @file gain_map.h
  9. * @brief Utility functions to manipulate jhgm (gain map) boxes.
  10. */
  11. #ifndef JXL_GAIN_MAP_H_
  12. #define JXL_GAIN_MAP_H_
  13. #include <jxl/color_encoding.h>
  14. #include <jxl/jxl_export.h>
  15. #include <jxl/types.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * Gain map bundle
  21. *
  22. * This structure is used to serialize gain map data to and from an input
  23. * buffer. It holds pointers to sections within the buffer, and different parts
  24. * of the gain map data such as metadata, ICC profile data, and the gain map
  25. * itself.
  26. *
  27. * The pointers in this structure do not take ownership of the memory they point
  28. * to. Instead, they reference specific locations within the provided buffer. It
  29. * is the caller's responsibility to ensure that the buffer remains valid and is
  30. * not deallocated as long as these pointers are in use. The structure should be
  31. * considered as providing a view into the buffer, not as an owner of the data.
  32. */
  33. typedef struct {
  34. /** Version number of the gain map bundle. */
  35. uint8_t jhgm_version;
  36. /** Size of the gain map metadata in bytes. */
  37. uint16_t gain_map_metadata_size;
  38. /** Pointer to the gain map metadata, which is a binary
  39. * blob following ISO 21496-1. This pointer references data within the input
  40. * buffer. */
  41. const uint8_t* gain_map_metadata;
  42. /** Indicates whether a color encoding is present. */
  43. JXL_BOOL has_color_encoding;
  44. /** If has_color_encoding is true, this field contains the
  45. * uncompressed color encoding data. */
  46. JxlColorEncoding color_encoding;
  47. /** Size of the alternative ICC profile in bytes (compressed
  48. * size). */
  49. uint32_t alt_icc_size;
  50. /** Pointer to the compressed ICC profile. This pointer references
  51. * data within the input buffer. */
  52. const uint8_t* alt_icc;
  53. /** Size of the gain map in bytes. */
  54. uint32_t gain_map_size;
  55. /** Pointer to the gain map data, which is a JPEG XL naked
  56. * codestream. This pointer references data within the input buffer.*/
  57. const uint8_t* gain_map;
  58. } JxlGainMapBundle;
  59. /**
  60. * Calculates the total size required to serialize the gain map bundle into a
  61. * binary buffer. This function accounts for all the necessary space to
  62. * serialize fields such as gain map metadata, color encoding, compressed ICC
  63. * profile data, and the gain map itself.
  64. *
  65. * @param[in] map_bundle Pointer to the JxlGainMapBundle containing all
  66. * necessary data to compute the size.
  67. * @param[out] bundle_size The size in bytes required to serialize the bundle.
  68. * @return Whether setting the size was successful.
  69. */
  70. JXL_EXPORT JXL_BOOL JxlGainMapGetBundleSize(const JxlGainMapBundle* map_bundle,
  71. size_t* bundle_size);
  72. /**
  73. * Serializes the gain map bundle into a preallocated buffer. The function
  74. * ensures that all parts of the bundle such as metadata, color encoding,
  75. * compressed ICC profile, and the gain map are correctly encoded into the
  76. * buffer. First call `JxlGainMapGetBundleSize` to get the size needed for
  77. * the buffer.
  78. *
  79. * @param[in] map_bundle Pointer to the `JxlGainMapBundle` to serialize.
  80. * @param[out] output_buffer Pointer to the buffer where the serialized data
  81. * will be written.
  82. * @param[in] output_buffer_size The size of the output buffer in bytes. Must be
  83. * large enough to hold the entire serialized data.
  84. * @param[out] bytes_written The number of bytes written to the output buffer.
  85. * @return Whether writing the bundle was successful.
  86. */
  87. JXL_EXPORT JXL_BOOL JxlGainMapWriteBundle(const JxlGainMapBundle* map_bundle,
  88. uint8_t* output_buffer,
  89. size_t output_buffer_size,
  90. size_t* bytes_written);
  91. /**
  92. * Deserializes a gain map bundle from a provided buffer and populates a
  93. * `JxlGainMapBundle` structure with the data extracted. This function assumes
  94. * the buffer contains a valid serialized gain map bundle. After successful
  95. * execution, the `JxlGainMapBundle` structure will reference three different
  96. * sections within the buffer:
  97. * - gain_map_metadata
  98. * - alt_icc
  99. * - gain_map
  100. * These sections will be accompanied by their respective sizes. Users must
  101. * ensure that the buffer remains valid as long as these pointers are in use.
  102. * @param[in,out] map_bundle Pointer to a preallocated `JxlGainMapBundle` where
  103. * the deserialized data will be stored.
  104. * @param[in] input_buffer Pointer to the buffer containing the serialized gain
  105. * map bundle data.
  106. * @param[in] input_buffer_size The size of the input buffer in bytes.
  107. * @param[out] bytes_read The number of bytes read from the input buffer.
  108. * @return Whether reading the bundle was successful.
  109. */
  110. JXL_EXPORT JXL_BOOL JxlGainMapReadBundle(JxlGainMapBundle* map_bundle,
  111. const uint8_t* input_buffer,
  112. size_t input_buffer_size,
  113. size_t* bytes_read);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* JXL_GAIN_MAP_H_ */
  118. /** @} */