skcms.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #pragma once
  8. // skcms.h contains the entire public API for skcms.
  9. #ifndef SKCMS_API
  10. #define SKCMS_API
  11. #endif
  12. #include <stdbool.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // A row-major 3x3 matrix (ie vals[row][col])
  20. typedef struct skcms_Matrix3x3 {
  21. float vals[3][3];
  22. } skcms_Matrix3x3;
  23. // It is _not_ safe to alias the pointers to invert in-place.
  24. SKCMS_API bool skcms_Matrix3x3_invert(const skcms_Matrix3x3*, skcms_Matrix3x3*);
  25. SKCMS_API skcms_Matrix3x3 skcms_Matrix3x3_concat(const skcms_Matrix3x3*, const skcms_Matrix3x3*);
  26. // A row-major 3x4 matrix (ie vals[row][col])
  27. typedef struct skcms_Matrix3x4 {
  28. float vals[3][4];
  29. } skcms_Matrix3x4;
  30. // A transfer function mapping encoded values to linear values,
  31. // represented by this 7-parameter piecewise function:
  32. //
  33. // linear = sign(encoded) * (c*|encoded| + f) , 0 <= |encoded| < d
  34. // = sign(encoded) * ((a*|encoded| + b)^g + e), d <= |encoded|
  35. //
  36. // (A simple gamma transfer function sets g to gamma and a to 1.)
  37. typedef struct skcms_TransferFunction {
  38. float g, a,b,c,d,e,f;
  39. } skcms_TransferFunction;
  40. SKCMS_API float skcms_TransferFunction_eval (const skcms_TransferFunction*, float);
  41. SKCMS_API bool skcms_TransferFunction_invert(const skcms_TransferFunction*,
  42. skcms_TransferFunction*);
  43. // Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2'
  44. typedef union skcms_Curve {
  45. struct {
  46. uint32_t alias_of_table_entries;
  47. skcms_TransferFunction parametric;
  48. };
  49. struct {
  50. uint32_t table_entries;
  51. const uint8_t* table_8;
  52. const uint8_t* table_16;
  53. };
  54. } skcms_Curve;
  55. typedef struct skcms_A2B {
  56. // Optional: N 1D curves, followed by an N-dimensional CLUT.
  57. // If input_channels == 0, these curves and CLUT are skipped,
  58. // Otherwise, input_channels must be in [1, 4].
  59. uint32_t input_channels;
  60. skcms_Curve input_curves[4];
  61. uint8_t grid_points[4];
  62. const uint8_t* grid_8;
  63. const uint8_t* grid_16;
  64. // Optional: 3 1D curves, followed by a color matrix.
  65. // If matrix_channels == 0, these curves and matrix are skipped,
  66. // Otherwise, matrix_channels must be 3.
  67. uint32_t matrix_channels;
  68. skcms_Curve matrix_curves[3];
  69. skcms_Matrix3x4 matrix;
  70. // Required: 3 1D curves. Always present, and output_channels must be 3.
  71. uint32_t output_channels;
  72. skcms_Curve output_curves[3];
  73. } skcms_A2B;
  74. typedef struct skcms_ICCProfile {
  75. const uint8_t* buffer;
  76. uint32_t size;
  77. uint32_t data_color_space;
  78. uint32_t pcs;
  79. uint32_t tag_count;
  80. // skcms_Parse() will set commonly-used fields for you when possible:
  81. // If we can parse red, green and blue transfer curves from the profile,
  82. // trc will be set to those three curves, and has_trc will be true.
  83. bool has_trc;
  84. skcms_Curve trc[3];
  85. // If this profile's gamut can be represented by a 3x3 transform to XYZD50,
  86. // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true.
  87. bool has_toXYZD50;
  88. skcms_Matrix3x3 toXYZD50;
  89. // If the profile has a valid A2B0 tag, skcms_Parse() sets A2B to that data,
  90. // and has_A2B to true.
  91. bool has_A2B;
  92. skcms_A2B A2B;
  93. } skcms_ICCProfile;
  94. // The sRGB color profile is so commonly used that we offer a canonical skcms_ICCProfile for it.
  95. SKCMS_API const skcms_ICCProfile* skcms_sRGB_profile(void);
  96. // Ditto for XYZD50, the most common profile connection space.
  97. SKCMS_API const skcms_ICCProfile* skcms_XYZD50_profile(void);
  98. SKCMS_API const skcms_TransferFunction* skcms_sRGB_TransferFunction(void);
  99. SKCMS_API const skcms_TransferFunction* skcms_sRGB_Inverse_TransferFunction(void);
  100. SKCMS_API const skcms_TransferFunction* skcms_Identity_TransferFunction(void);
  101. // Practical equality test for two skcms_ICCProfiles.
  102. // The implementation is subject to change, but it will always try to answer
  103. // "can I substitute A for B?" and "can I skip transforming from A to B?".
  104. SKCMS_API bool skcms_ApproximatelyEqualProfiles(const skcms_ICCProfile* A,
  105. const skcms_ICCProfile* B);
  106. // Practical test that answers: Is curve roughly the inverse of inv_tf? Typically used by passing
  107. // the inverse of a known parametric transfer function (like sRGB), to determine if a particular
  108. // curve is very close to sRGB.
  109. SKCMS_API bool skcms_AreApproximateInverses(const skcms_Curve* curve,
  110. const skcms_TransferFunction* inv_tf);
  111. // Similar to above, answering the question for all three TRC curves of the given profile. Again,
  112. // passing skcms_sRGB_InverseTransferFunction as inv_tf will answer the question:
  113. // "Does this profile have a transfer function that is very close to sRGB?"
  114. SKCMS_API bool skcms_TRCs_AreApproximateInverse(const skcms_ICCProfile* profile,
  115. const skcms_TransferFunction* inv_tf);
  116. // Parse an ICC profile and return true if possible, otherwise return false.
  117. // The buffer is not copied, it must remain valid as long as the skcms_ICCProfile
  118. // will be used.
  119. SKCMS_API bool skcms_Parse(const void*, size_t, skcms_ICCProfile*);
  120. SKCMS_API bool skcms_ApproximateCurve(const skcms_Curve* curve,
  121. skcms_TransferFunction* approx,
  122. float* max_error);
  123. typedef struct skcms_ICCTag {
  124. uint32_t signature;
  125. uint32_t type;
  126. uint32_t size;
  127. const uint8_t* buf;
  128. } skcms_ICCTag;
  129. SKCMS_API void skcms_GetTagByIndex (const skcms_ICCProfile*, uint32_t idx, skcms_ICCTag*);
  130. SKCMS_API bool skcms_GetTagBySignature(const skcms_ICCProfile*, uint32_t sig, skcms_ICCTag*);
  131. // These are common ICC signature values
  132. enum {
  133. // data_color_space
  134. skcms_Signature_CMYK = 0x434D594B,
  135. skcms_Signature_Gray = 0x47524159,
  136. skcms_Signature_RGB = 0x52474220,
  137. // pcs
  138. skcms_Signature_Lab = 0x4C616220,
  139. skcms_Signature_XYZ = 0x58595A20,
  140. };
  141. typedef enum skcms_PixelFormat {
  142. skcms_PixelFormat_A_8,
  143. skcms_PixelFormat_A_8_,
  144. skcms_PixelFormat_G_8,
  145. skcms_PixelFormat_G_8_,
  146. skcms_PixelFormat_RGBA_8888_Palette8,
  147. skcms_PixelFormat_BGRA_8888_Palette8,
  148. skcms_PixelFormat_RGB_565,
  149. skcms_PixelFormat_BGR_565,
  150. skcms_PixelFormat_ABGR_4444,
  151. skcms_PixelFormat_ARGB_4444,
  152. skcms_PixelFormat_RGB_888,
  153. skcms_PixelFormat_BGR_888,
  154. skcms_PixelFormat_RGBA_8888,
  155. skcms_PixelFormat_BGRA_8888,
  156. skcms_PixelFormat_RGBA_1010102,
  157. skcms_PixelFormat_BGRA_1010102,
  158. skcms_PixelFormat_RGB_161616LE, // Little-endian. Pointers must be 16-bit aligned.
  159. skcms_PixelFormat_BGR_161616LE,
  160. skcms_PixelFormat_RGBA_16161616LE,
  161. skcms_PixelFormat_BGRA_16161616LE,
  162. skcms_PixelFormat_RGB_161616BE, // Big-endian. Pointers must be 16-bit aligned.
  163. skcms_PixelFormat_BGR_161616BE,
  164. skcms_PixelFormat_RGBA_16161616BE,
  165. skcms_PixelFormat_BGRA_16161616BE,
  166. skcms_PixelFormat_RGB_hhh_Norm, // 1-5-10 half-precision float in [0,1]
  167. skcms_PixelFormat_BGR_hhh_Norm, // Pointers must be 16-bit aligned.
  168. skcms_PixelFormat_RGBA_hhhh_Norm,
  169. skcms_PixelFormat_BGRA_hhhh_Norm,
  170. skcms_PixelFormat_RGB_hhh, // 1-5-10 half-precision float.
  171. skcms_PixelFormat_BGR_hhh, // Pointers must be 16-bit aligned.
  172. skcms_PixelFormat_RGBA_hhhh,
  173. skcms_PixelFormat_BGRA_hhhh,
  174. skcms_PixelFormat_RGB_fff, // 1-8-23 single-precision float (the normal kind).
  175. skcms_PixelFormat_BGR_fff, // Pointers must be 32-bit aligned.
  176. skcms_PixelFormat_RGBA_ffff,
  177. skcms_PixelFormat_BGRA_ffff,
  178. } skcms_PixelFormat;
  179. // We always store any alpha channel linearly. In the chart below, tf-1() is the inverse
  180. // transfer function for the given color profile (applying the transfer function linearizes).
  181. // We treat opaque as a strong requirement, not just a performance hint: we will ignore
  182. // any source alpha and treat it as 1.0, and will make sure that any destination alpha
  183. // channel is filled with the equivalent of 1.0.
  184. // We used to offer multiple types of premultiplication, but now just one, PremulAsEncoded.
  185. // This is the premul you're probably used to working with.
  186. typedef enum skcms_AlphaFormat {
  187. skcms_AlphaFormat_Opaque, // alpha is always opaque
  188. // tf-1(r), tf-1(g), tf-1(b), 1.0
  189. skcms_AlphaFormat_Unpremul, // alpha and color are unassociated
  190. // tf-1(r), tf-1(g), tf-1(b), a
  191. skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded
  192. // tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a
  193. } skcms_AlphaFormat;
  194. // Convert npixels pixels from src format and color profile to dst format and color profile
  195. // and return true, otherwise return false. It is safe to alias dst == src if dstFmt == srcFmt.
  196. SKCMS_API bool skcms_Transform(const void* src,
  197. skcms_PixelFormat srcFmt,
  198. skcms_AlphaFormat srcAlpha,
  199. const skcms_ICCProfile* srcProfile,
  200. void* dst,
  201. skcms_PixelFormat dstFmt,
  202. skcms_AlphaFormat dstAlpha,
  203. const skcms_ICCProfile* dstProfile,
  204. size_t npixels);
  205. // As skcms_Transform(), supporting srcFmts with a palette.
  206. SKCMS_API bool skcms_TransformWithPalette(const void* src,
  207. skcms_PixelFormat srcFmt,
  208. skcms_AlphaFormat srcAlpha,
  209. const skcms_ICCProfile* srcProfile,
  210. void* dst,
  211. skcms_PixelFormat dstFmt,
  212. skcms_AlphaFormat dstAlpha,
  213. const skcms_ICCProfile* dstProfile,
  214. size_t npixels,
  215. const void* palette);
  216. // If profile can be used as a destination in skcms_Transform, return true. Otherwise, attempt to
  217. // rewrite it with approximations where reasonable. If successful, return true. If no reasonable
  218. // approximation exists, leave the profile unchanged and return false.
  219. SKCMS_API bool skcms_MakeUsableAsDestination(skcms_ICCProfile* profile);
  220. // If profile can be used as a destination with a single parametric transfer function (ie for
  221. // rasterization), return true. Otherwise, attempt to rewrite it with approximations where
  222. // reasonable. If successful, return true. If no reasonable approximation exists, leave the
  223. // profile unchanged and return false.
  224. SKCMS_API bool skcms_MakeUsableAsDestinationWithSingleCurve(skcms_ICCProfile* profile);
  225. SKCMS_API bool skcms_PrimariesToXYZD50(float rx, float ry,
  226. float gx, float gy,
  227. float bx, float by,
  228. float wx, float wy,
  229. skcms_Matrix3x3* toXYZD50);
  230. // Utilities for programmatically constructing profiles
  231. static inline void skcms_Init(skcms_ICCProfile* p) {
  232. memset(p, 0, sizeof(*p));
  233. p->data_color_space = skcms_Signature_RGB;
  234. p->pcs = skcms_Signature_XYZ;
  235. }
  236. static inline void skcms_SetTransferFunction(skcms_ICCProfile* p,
  237. const skcms_TransferFunction* tf) {
  238. p->has_trc = true;
  239. for (int i = 0; i < 3; ++i) {
  240. p->trc[i].table_entries = 0;
  241. p->trc[i].parametric = *tf;
  242. }
  243. }
  244. static inline void skcms_SetXYZD50(skcms_ICCProfile* p, const skcms_Matrix3x3* m) {
  245. p->has_toXYZD50 = true;
  246. p->toXYZD50 = *m;
  247. }
  248. #ifdef __cplusplus
  249. }
  250. #endif