aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkColorSpace_Base.h
blob: a188a11dd115255e07125dccb16b96c83d736175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkColorSpace_Base_DEFINED
#define SkColorSpace_Base_DEFINED

#include "SkColorSpace.h"
#include "SkData.h"
#include "SkOnce.h"
#include "SkTemplates.h"

enum SkGammaNamed : uint8_t {
    kLinear_SkGammaNamed,
    kSRGB_SkGammaNamed,
    k2Dot2Curve_SkGammaNamed,
    kNonStandard_SkGammaNamed,
};

struct SkGammas : SkRefCnt {

    // There are four possible representations for gamma curves.  kNone_Type is used
    // as a placeholder until the struct is initialized.  It is not a valid value.
    enum class Type : uint8_t {
        kNone_Type,
        kNamed_Type,
        kValue_Type,
        kTable_Type,
        kParam_Type,
    };

    // Contains information for a gamma table.
    struct Table {
        size_t fOffset;
        int    fSize;

        const float* table(const SkGammas* base) const {
            return SkTAddOffset<const float>(base, sizeof(SkGammas) + fOffset);
        }
    };

    // Contains the actual gamma curve information.  Should be interpreted
    // based on the type of the gamma curve.
    union Data {
        Data()
            : fTable{ 0, 0 }
        {}

        inline bool operator==(const Data& that) const {
            return this->fTable.fOffset == that.fTable.fOffset &&
                   this->fTable.fSize == that.fTable.fSize;
        }

        SkGammaNamed             fNamed;
        float                    fValue;
        Table                    fTable;
        size_t                   fParamOffset;

        const SkColorSpaceTransferFn& params(const SkGammas* base) const {
            return *SkTAddOffset<const SkColorSpaceTransferFn>(
                    base, sizeof(SkGammas) + fParamOffset);
        }
    };

    bool isNamed(int i) const {
        return Type::kNamed_Type == this->type(i);
    }

    bool isValue(int i) const {
        return Type::kValue_Type == this->type(i);
    }

    bool isTable(int i) const {
        return Type::kTable_Type == this->type(i);
    }

    bool isParametric(int i) const {
        return Type::kParam_Type == this->type(i);
    }

    const Data& data(int i) const {
        switch (i) {
            case 0:
                return fRedData;
            case 1:
                return fGreenData;
            case 2:
                return fBlueData;
            default:
                SkASSERT(false);
                return fRedData;
        }
    }

    const float* table(int i) const {
        SkASSERT(isTable(i));
        return this->data(i).fTable.table(this);
    }

    const SkColorSpaceTransferFn& params(int i) const {
        SkASSERT(isParametric(i));
        return this->data(i).params(this);
    }

    Type type(int i) const {
        switch (i) {
            case 0:
                return fRedType;
            case 1:
                return fGreenType;
            case 2:
                return fBlueType;
            default:
                SkASSERT(false);
                return fRedType;
        }
    }

    SkGammas()
        : fRedType(Type::kNone_Type)
        , fGreenType(Type::kNone_Type)
        , fBlueType(Type::kNone_Type)
    {}

    // These fields should only be modified when initializing the struct.
    Data fRedData;
    Data fGreenData;
    Data fBlueData;
    Type fRedType;
    Type fGreenType;
    Type fBlueType;

    // Objects of this type are sometimes created in a custom fashion using
    // sk_malloc_throw and therefore must be sk_freed.  We overload new to
    // also call sk_malloc_throw so that memory can be unconditionally released
    // using sk_free in an overloaded delete. Overloading regular new means we
    // must also overload placement new.
    void* operator new(size_t size) { return sk_malloc_throw(size); }
    void* operator new(size_t, void* p) { return p; }
    void operator delete(void* p) { sk_free(p); }
};

struct SkColorLookUpTable : public SkRefCnt {
    static constexpr uint8_t kOutputChannels = 3;

    uint8_t                  fInputChannels;
    uint8_t                  fGridPoints[3];

    const float* table() const {
        return SkTAddOffset<const float>(this, sizeof(SkColorLookUpTable));
    }

    SkColorLookUpTable(uint8_t inputChannels, uint8_t gridPoints[3])
        : fInputChannels(inputChannels)
    {
        SkASSERT(3 == inputChannels);
        memcpy(fGridPoints, gridPoints, 3 * sizeof(uint8_t));
    }

    // Objects of this type are created in a custom fashion using sk_malloc_throw
    // and therefore must be sk_freed.
    void* operator new(size_t size) = delete;
    void* operator new(size_t, void* p) { return p; }
    void operator delete(void* p) { sk_free(p); }
};

class SkColorSpace_Base : public SkColorSpace {
public:
    SkGammaNamed gammaNamed() const { return fGammaNamed; }
    const SkGammas* gammas() const { return fGammas.get(); }

    const SkColorLookUpTable* colorLUT() const { return fColorLUT.get(); }

    const SkMatrix44& toXYZD50() const { return fToXYZD50; }
    const SkMatrix44& fromXYZD50() const;
    
    void toDstGammaTables(const uint8_t* tables[3], sk_sp<SkData>* storage, int numTables) const;

    /**
     *  Create an SkColorSpace with the same gamut as this color space, but with linear gamma.
     */
    sk_sp<SkColorSpace> makeLinearGamma();

private:

    /**
     *  FIXME (msarett):
     *  Hiding this function until we can determine if we need it.  Known issues include:
     *  Only writes 3x3 matrices
     *  Only writes float gammas
     *  Rejected by some parsers because the "profile description" is empty
     */
    sk_sp<SkData> writeToICC() const;

    static sk_sp<SkColorSpace> NewRGB(SkGammaNamed gammaNamed, const SkMatrix44& toXYZD50);

    SkColorSpace_Base(SkGammaNamed gammaNamed, const SkMatrix44& toXYZ);

    SkColorSpace_Base(sk_sp<SkColorLookUpTable> colorLUT, SkGammaNamed gammaNamed,
                      sk_sp<SkGammas> gammas, const SkMatrix44& toXYZ, sk_sp<SkData> profileData);

    sk_sp<SkColorLookUpTable>      fColorLUT;
    const SkGammaNamed             fGammaNamed;
    sk_sp<SkGammas>                fGammas;
    sk_sp<SkData>                  fProfileData;

    const SkMatrix44               fToXYZD50;
    mutable SkMatrix44             fFromXYZD50;
    mutable SkOnce                 fFromXYZOnce;

    mutable sk_sp<SkData>          fDstStorage;
    mutable const uint8_t*         fToDstGammaTables[3];
    mutable SkOnce                 fToDstGammaOnce;

    friend class SkColorSpace;
    friend class ColorSpaceXformTest;
    friend class ColorSpaceTest;
    typedef SkColorSpace INHERITED;
};

static inline SkColorSpace_Base* as_CSB(SkColorSpace* colorSpace) {
    return static_cast<SkColorSpace_Base*>(colorSpace);
}

static inline const SkColorSpace_Base* as_CSB(const SkColorSpace* colorSpace) {
    return static_cast<const SkColorSpace_Base*>(colorSpace);
}

static inline SkColorSpace_Base* as_CSB(const sk_sp<SkColorSpace>& colorSpace) {
    return static_cast<SkColorSpace_Base*>(colorSpace.get());
}

#endif