|
| 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 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +// skcms.h contains the entire public API for skcms. |
| 11 | + |
| 12 | +#include <stdbool.h> |
| 13 | +#include <stddef.h> |
| 14 | +#include <stdint.h> |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +// A row-major 3x3 matrix (ie vals[row][col]) |
| 21 | +typedef struct { |
| 22 | + float vals[3][3]; |
| 23 | +} skcms_Matrix3x3; |
| 24 | + |
| 25 | +// A row-major 3x4 matrix (ie vals[row][col]) |
| 26 | +typedef struct { |
| 27 | + float vals[3][4]; |
| 28 | +} skcms_Matrix3x4; |
| 29 | + |
| 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 { |
| 38 | + float g, a,b,c,d,e,f; |
| 39 | +} skcms_TransferFunction; |
| 40 | + |
| 41 | +// Unified representation of 'curv' or 'para' tag data, or a 1D table from 'mft1' or 'mft2' |
| 42 | +typedef struct { |
| 43 | + union { |
| 44 | + struct { |
| 45 | + uint32_t alias_of_table_entries; |
| 46 | + skcms_TransferFunction parametric; |
| 47 | + }; |
| 48 | + struct { |
| 49 | + uint32_t table_entries; |
| 50 | + const uint8_t* table_8; |
| 51 | + const uint8_t* table_16; |
| 52 | + }; |
| 53 | + }; |
| 54 | +} skcms_Curve; |
| 55 | + |
| 56 | +typedef struct { |
| 57 | + // Optional: N 1D curves, followed by an N-dimensional CLUT. |
| 58 | + // If input_channels == 0, these curves and CLUT are skipped, |
| 59 | + // Otherwise, input_channels must be in [1, 4]. |
| 60 | + uint32_t input_channels; |
| 61 | + skcms_Curve input_curves[4]; |
| 62 | + uint8_t grid_points[4]; |
| 63 | + const uint8_t* grid_8; |
| 64 | + const uint8_t* grid_16; |
| 65 | + |
| 66 | + // Optional: 3 1D curves, followed by a color matrix. |
| 67 | + // If matrix_channels == 0, these curves and matrix are skipped, |
| 68 | + // Otherwise, matrix_channels must be 3. |
| 69 | + uint32_t matrix_channels; |
| 70 | + skcms_Curve matrix_curves[3]; |
| 71 | + skcms_Matrix3x4 matrix; |
| 72 | + |
| 73 | + // Required: 3 1D curves. Always present, and output_channels must be 3. |
| 74 | + uint32_t output_channels; |
| 75 | + skcms_Curve output_curves[3]; |
| 76 | +} skcms_A2B; |
| 77 | + |
| 78 | +typedef struct { |
| 79 | + const uint8_t* buffer; |
| 80 | + |
| 81 | + uint32_t size; |
| 82 | + uint32_t data_color_space; |
| 83 | + uint32_t pcs; |
| 84 | + uint32_t tag_count; |
| 85 | + |
| 86 | + // skcms_Parse() will set commonly-used fields for you when possible: |
| 87 | + |
| 88 | + // If we can parse red, green and blue transfer curves from the profile, |
| 89 | + // trc will be set to those three curves, and has_trc will be true. |
| 90 | + bool has_trc; |
| 91 | + skcms_Curve trc[3]; |
| 92 | + |
| 93 | + // If this profile's gamut can be represented by a 3x3 transform to XYZD50, |
| 94 | + // skcms_Parse() sets toXYZD50 to that transform and has_toXYZD50 to true. |
| 95 | + bool has_toXYZD50; |
| 96 | + skcms_Matrix3x3 toXYZD50; |
| 97 | + |
| 98 | + // If the profile has a valid A2B0 tag, skcms_Parse() sets A2B to that data, |
| 99 | + // and has_A2B to true. |
| 100 | + bool has_A2B; |
| 101 | + skcms_A2B A2B; |
| 102 | +} skcms_ICCProfile; |
| 103 | + |
| 104 | +// Parse an ICC profile and return true if possible, otherwise return false. |
| 105 | +// The buffer is not copied, it must remain valid as long as the skcms_ICCProfile |
| 106 | +// will be used. |
| 107 | +bool skcms_Parse(const void*, size_t, skcms_ICCProfile*); |
| 108 | + |
| 109 | +bool skcms_ApproximateCurve(const skcms_Curve* curve, skcms_TransferFunction* approx, |
| 110 | + float* max_error); |
| 111 | + |
| 112 | +// A specialized approximation for transfer functions with gamma between 1 and 3. |
| 113 | +// f(x) = Ax^3 + Bx^2 + (1-A-B)x |
| 114 | +typedef struct { |
| 115 | + float A,B; |
| 116 | +} skcms_TF13; |
| 117 | + |
| 118 | +bool skcms_ApproximateCurve13(const skcms_Curve* curve, skcms_TF13* approx, float* max_error); |
| 119 | + |
| 120 | +typedef struct { |
| 121 | + uint32_t signature; |
| 122 | + uint32_t type; |
| 123 | + uint32_t size; |
| 124 | + const uint8_t* buf; |
| 125 | +} skcms_ICCTag; |
| 126 | + |
| 127 | +void skcms_GetTagByIndex (const skcms_ICCProfile*, uint32_t idx, skcms_ICCTag*); |
| 128 | +bool skcms_GetTagBySignature(const skcms_ICCProfile*, uint32_t sig, skcms_ICCTag*); |
| 129 | + |
| 130 | +typedef enum { |
| 131 | + skcms_PixelFormat_RGB_565, |
| 132 | + skcms_PixelFormat_BGR_565, |
| 133 | + |
| 134 | + skcms_PixelFormat_RGB_888, |
| 135 | + skcms_PixelFormat_BGR_888, |
| 136 | + skcms_PixelFormat_RGBA_8888, |
| 137 | + skcms_PixelFormat_BGRA_8888, |
| 138 | + |
| 139 | + skcms_PixelFormat_RGBA_1010102, |
| 140 | + skcms_PixelFormat_BGRA_1010102, |
| 141 | + |
| 142 | + skcms_PixelFormat_RGB_161616, // Big-endian. Pointers must be 16-bit aligned. |
| 143 | + skcms_PixelFormat_BGR_161616, |
| 144 | + skcms_PixelFormat_RGBA_16161616, |
| 145 | + skcms_PixelFormat_BGRA_16161616, |
| 146 | + |
| 147 | + skcms_PixelFormat_RGB_hhh, // 1-5-10 half-precision float. |
| 148 | + skcms_PixelFormat_BGR_hhh, // Pointers must be 16-bit aligned. |
| 149 | + skcms_PixelFormat_RGBA_hhhh, |
| 150 | + skcms_PixelFormat_BGRA_hhhh, |
| 151 | + |
| 152 | + skcms_PixelFormat_RGB_fff, // 1-8-23 single-precision float (the normal kind). |
| 153 | + skcms_PixelFormat_BGR_fff, // Pointers must be 32-bit aligned. |
| 154 | + skcms_PixelFormat_RGBA_ffff, |
| 155 | + skcms_PixelFormat_BGRA_ffff, |
| 156 | +} skcms_PixelFormat; |
| 157 | + |
| 158 | +// We always store any alpha channel linearly. In the chart below, tf-1() is the inverse |
| 159 | +// transfer function for the given color profile (applying the transfer function linearizes). |
| 160 | + |
| 161 | +// We treat opaque as a strong requirement, not just a performance hint: we will ignore |
| 162 | +// any source alpha and treat it as 1.0, and will make sure that any destination alpha |
| 163 | +// channel is filled with the equivalent of 1.0. |
| 164 | + |
| 165 | +// When premultiplying and/or using a non-linear transfer function, it's important |
| 166 | +// that we know the order the operations are applied. If you're used to working |
| 167 | +// with non-color-managed drawing systems, PremulAsEncoded is probably the "premul" |
| 168 | +// you're looking for; if you want linear blending, PremulLinear is the choice for you. |
| 169 | + |
| 170 | +typedef enum { |
| 171 | + skcms_AlphaFormat_Opaque, // alpha is always opaque |
| 172 | + // tf-1(r), tf-1(g), tf-1(b), 1.0 |
| 173 | + skcms_AlphaFormat_Unpremul, // alpha and color are unassociated |
| 174 | + // tf-1(r), tf-1(g), tf-1(b), a |
| 175 | + skcms_AlphaFormat_PremulAsEncoded, // premultiplied while encoded |
| 176 | + // tf-1(r)*a, tf-1(g)*a, tf-1(b)*a, a |
| 177 | + skcms_AlphaFormat_PremulLinear, // premultiplied while linear |
| 178 | + // tf-1(r*a), tf-1(g*a), tf-1(b*a), a |
| 179 | +} skcms_AlphaFormat; |
| 180 | + |
| 181 | +// Convert npixels pixels from src format and color profile to dst format and color profile |
| 182 | +// and return true, otherwise return false. It is safe to alias dst == src if dstFmt == srcFmt. |
| 183 | +bool skcms_Transform(const void* src, |
| 184 | + skcms_PixelFormat srcFmt, |
| 185 | + skcms_AlphaFormat srcAlpha, |
| 186 | + const skcms_ICCProfile* srcProfile, |
| 187 | + void* dst, |
| 188 | + skcms_PixelFormat dstFmt, |
| 189 | + skcms_AlphaFormat dstAlpha, |
| 190 | + const skcms_ICCProfile* dstProfile, |
| 191 | + size_t npixels); |
| 192 | + |
| 193 | +#ifdef __cplusplus |
| 194 | +} |
| 195 | +#endif |
0 commit comments