aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/opts/SkColorXform_opts.h
blob: b3da55c1fdd758d2b3b7d74ea41b050b5918eb18 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * 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 SkColorXform_opts_DEFINED
#define SkColorXform_opts_DEFINED

#include "SkNx.h"
#include "SkColorPriv.h"
#include "SkHalf.h"
#include "SkSRGB.h"
#include "SkTemplates.h"

namespace SK_OPTS_NS {

// Strange that we need a wrapper on SkNx_cast to use as a function ptr.
static Sk4i Sk4f_trunc(const Sk4f& x) {
    return SkNx_cast<int>(x);
}

static Sk4f linear_to_2dot2(const Sk4f& x) {
    // x^(29/64) is a very good approximation of the true value, x^(1/2.2).
    auto x2  = x.rsqrt(),                            // x^(-1/2)
         x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(),   // x^(-1/32)
         x64 = x32.rsqrt();                          // x^(+1/64)

    // 29 = 32 - 2 - 1
    return 255.0f * x2.invert() * x32 * x64.invert();
}

enum DstGamma {
    // 8888
    kSRGB_DstGamma,
    k2Dot2_DstGamma,
    kTable_DstGamma,

    // F16
    kLinear_DstGamma,
};

template <DstGamma kDstGamma, bool kSwapRB>
static void color_xform_RGB1(void* dst, const uint32_t* src, int len,
                             const float* const srcTables[3], const float matrix[16],
                             const uint8_t* const dstTables[3]) {
    int kRShift = 0;
    int kGShift = 8;
    int kBShift = 16;
    int kAShift = 24;
    if (kSwapRB) {
        kBShift = 0;
        kRShift = 16;
    }

    Sk4f rXgXbX = Sk4f::Load(matrix +  0),
         rYgYbY = Sk4f::Load(matrix +  4),
         rZgZbZ = Sk4f::Load(matrix +  8),
         rTgTbT = Sk4f::Load(matrix + 12);

    if (len >= 4) {
        Sk4f reds, greens, blues;
        auto load_next_4 = [&reds, &greens, &blues, &src, &len, &srcTables] {
            reds   = Sk4f{srcTables[0][(src[0] >>  0) & 0xFF],
                          srcTables[0][(src[1] >>  0) & 0xFF],
                          srcTables[0][(src[2] >>  0) & 0xFF],
                          srcTables[0][(src[3] >>  0) & 0xFF]};
            greens = Sk4f{srcTables[1][(src[0] >>  8) & 0xFF],
                          srcTables[1][(src[1] >>  8) & 0xFF],
                          srcTables[1][(src[2] >>  8) & 0xFF],
                          srcTables[1][(src[3] >>  8) & 0xFF]};
            blues  = Sk4f{srcTables[2][(src[0] >> 16) & 0xFF],
                          srcTables[2][(src[1] >> 16) & 0xFF],
                          srcTables[2][(src[2] >> 16) & 0xFF],
                          srcTables[2][(src[3] >> 16) & 0xFF]};
            src += 4;
            len -= 4;
        };

        Sk4f dstReds, dstGreens, dstBlues;
        auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBlues, &rXgXbX,
                            &rYgYbY, &rZgZbZ, &rTgTbT] {
            dstReds   = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues + rTgTbT[0];
            dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues + rTgTbT[1];
            dstBlues  = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues + rTgTbT[2];
        };

        auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables, kRShift, kGShift,
                        kBShift, kAShift] {
            if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
                Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ?
                        sk_linear_to_srgb_needs_trunc : linear_to_2dot2;
                Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ?
                        Sk4f_trunc : Sk4f_round;

                dstReds   = linear_to_curve(dstReds);
                dstGreens = linear_to_curve(dstGreens);
                dstBlues  = linear_to_curve(dstBlues);

                dstReds   = sk_clamp_0_255(dstReds);
                dstGreens = sk_clamp_0_255(dstGreens);
                dstBlues  = sk_clamp_0_255(dstBlues);

                auto rgba = (float_to_int(dstReds)   << kRShift)
                          | (float_to_int(dstGreens) << kGShift)
                          | (float_to_int(dstBlues)  << kBShift)
                          | (Sk4i{0xFF}              << kAShift);
                rgba.store((uint32_t*) dst);

                dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
            } else if (kTable_DstGamma == kDstGamma) {
                Sk4f scaledReds   = Sk4f::Min(Sk4f::Max(1023.0f * dstReds,   0.0f), 1023.0f);
                Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0f), 1023.0f);
                Sk4f scaledBlues  = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues,  0.0f), 1023.0f);

                Sk4i indicesReds   = Sk4f_round(scaledReds);
                Sk4i indicesGreens = Sk4f_round(scaledGreens);
                Sk4i indicesBlues  = Sk4f_round(scaledBlues);

                uint32_t* dst32 = (uint32_t*) dst;
                dst32[0] = dstTables[0][indicesReds  [0]] << kRShift
                         | dstTables[1][indicesGreens[0]] << kGShift
                         | dstTables[2][indicesBlues [0]] << kBShift
                         | 0xFF                           << kAShift;
                dst32[1] = dstTables[0][indicesReds  [1]] << kRShift
                         | dstTables[1][indicesGreens[1]] << kGShift
                         | dstTables[2][indicesBlues [1]] << kBShift
                         | 0xFF                           << kAShift;
                dst32[2] = dstTables[0][indicesReds  [2]] << kRShift
                         | dstTables[1][indicesGreens[2]] << kGShift
                         | dstTables[2][indicesBlues [2]] << kBShift
                         | 0xFF                           << kAShift;
                dst32[3] = dstTables[0][indicesReds  [3]] << kRShift
                         | dstTables[1][indicesGreens[3]] << kGShift
                         | dstTables[2][indicesBlues [3]] << kBShift
                         | 0xFF                           << kAShift;

                dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
            } else {
                Sk4h_store4(dst, SkFloatToHalf_finite(dstReds),
                                 SkFloatToHalf_finite(dstGreens),
                                 SkFloatToHalf_finite(dstBlues),
                                 SK_Half1);
                dst = SkTAddOffset<void>(dst, 4 * sizeof(uint64_t));
            }
        };

        load_next_4();

        while (len >= 4) {
            transform_4();
            load_next_4();
            store_4();
        }

        transform_4();
        store_4();
    }

    while (len > 0) {
        // Splat r,g,b across a register each.
        auto r = Sk4f{srcTables[0][(*src >>  0) & 0xFF]},
             g = Sk4f{srcTables[1][(*src >>  8) & 0xFF]},
             b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]};

        auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b + rTgTbT;

        if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
            Sk4f (*linear_to_curve)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ?
                    sk_linear_to_srgb_needs_trunc : linear_to_2dot2;
            Sk4i (*float_to_int)(const Sk4f&) = (kSRGB_DstGamma == kDstGamma) ?
                    Sk4f_trunc : Sk4f_round;

            dstPixel = sk_clamp_0_255(linear_to_curve(dstPixel));

            uint32_t rgba;
            SkNx_cast<uint8_t>(float_to_int(dstPixel)).store(&rgba);
            rgba |= 0xFF000000;
            if (kSwapRB) {
                *((uint32_t*) dst) = SkSwizzle_RB(rgba);
            } else {
                *((uint32_t*) dst) = rgba;
            }
            dst = SkTAddOffset<void>(dst, sizeof(uint32_t));
        } else if (kTable_DstGamma == kDstGamma) {
            Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 1023.0f);

            Sk4i indices = Sk4f_round(scaledPixel);

            *((uint32_t*) dst) = dstTables[0][indices[0]] << kRShift
                               | dstTables[1][indices[1]] << kGShift
                               | dstTables[2][indices[2]] << kBShift
                               | 0xFF                     << kAShift;

            dst = SkTAddOffset<void>(dst, sizeof(uint32_t));
        } else {
            uint64_t rgba;
            SkFloatToHalf_finite(dstPixel).store(&rgba);
            rgba |= static_cast<uint64_t>(SK_Half1) << 48;
            *((uint64_t*) dst) = rgba;
            dst = SkTAddOffset<void>(dst, sizeof(uint64_t));
        }

        src += 1;
        len -= 1;
    }
}

static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int len,
                                      const float* const srcTables[3], const float matrix[16]) {
    color_xform_RGB1<k2Dot2_DstGamma, false>(dst, src, len, srcTables, matrix, nullptr);
}

static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len,
                                     const float* const srcTables[3], const float matrix[16]) {
    color_xform_RGB1<kSRGB_DstGamma, false>(dst, src, len, srcTables, matrix, nullptr);
}

static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int len,
                                      const float* const srcTables[3], const float matrix[16],
                                      const uint8_t* const dstTables[3]) {
    color_xform_RGB1<kTable_DstGamma, false>(dst, src, len, srcTables, matrix, dstTables);
}

static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int len,
                                       const float* const srcTables[3], const float matrix[16]) {
    color_xform_RGB1<kLinear_DstGamma, false>(dst, src, len, srcTables, matrix, nullptr);
}

static void color_xform_RGB1_to_2dot2_swaprb(uint32_t* dst, const uint32_t* src, int len,
                                             const float* const srcTables[3],
                                             const float matrix[16]) {
    color_xform_RGB1<k2Dot2_DstGamma, true>(dst, src, len, srcTables, matrix, nullptr);
}

static void color_xform_RGB1_to_srgb_swaprb(uint32_t* dst, const uint32_t* src, int len,
                                            const float* const srcTables[3],
                                            const float matrix[16]) {
    color_xform_RGB1<kSRGB_DstGamma, true>(dst, src, len, srcTables, matrix, nullptr);
}

static void color_xform_RGB1_to_table_swaprb(uint32_t* dst, const uint32_t* src, int len,
                                             const float* const srcTables[3],
                                             const float matrix[16],
                                             const uint8_t* const dstTables[3]) {
    color_xform_RGB1<kTable_DstGamma, true>(dst, src, len, srcTables, matrix, dstTables);
}

}  // namespace SK_OPTS_NS

#endif // SkColorXform_opts_DEFINED