blob: b39891ea2082e929e74eb6322a398e9b89d70740 [file] [log] [blame]
reedfbc1e292016-01-29 05:22:59 -08001/*
2 * Copyright 2016 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
reeddd9ffea2016-02-18 12:39:14 -08008#include "SkPM4f.h"
reedfbc1e292016-01-29 05:22:59 -08009#include "SkColorPriv.h"
10#include "SkNx.h"
11
12static inline float get_alpha(const Sk4f& f4) {
mtklein7c249e52016-02-21 10:54:19 -080013 return f4[SkPM4f::A];
reedfbc1e292016-01-29 05:22:59 -080014}
15
16static inline Sk4f set_alpha(const Sk4f& f4, float alpha) {
17 static_assert(3 == SkPM4f::A, "");
mtklein7c249e52016-02-21 10:54:19 -080018 return Sk4f(f4[0], f4[1], f4[2], alpha);
reedfbc1e292016-01-29 05:22:59 -080019}
20
21static inline uint32_t to_4b(const Sk4f& f4) {
22 uint32_t b4;
23 SkNx_cast<uint8_t>(f4).store((uint8_t*)&b4);
24 return b4;
25}
26
27static inline Sk4f to_4f(uint32_t b4) {
28 return SkNx_cast<float>(Sk4b::Load((const uint8_t*)&b4));
29}
30
reed395eabe2016-01-30 18:52:31 -080031static inline Sk4f srgb_to_linear(const Sk4f& s4) {
reedfbc1e292016-01-29 05:22:59 -080032 return set_alpha(s4 * s4, get_alpha(s4));
33}
34
reed395eabe2016-01-30 18:52:31 -080035static inline Sk4f linear_to_srgb(const Sk4f& l4) {
36 return set_alpha(l4.sqrt(), get_alpha(l4));
reedfbc1e292016-01-29 05:22:59 -080037}
38
39///////////////////////////////////////////////////////////////////////////////////////////////////
40
41static inline Sk4f Sk4f_fromL32(uint32_t src) {
42 return to_4f(src) * Sk4f(1.0f/255);
43}
44
45static inline Sk4f Sk4f_fromS32(uint32_t src) {
reed395eabe2016-01-30 18:52:31 -080046 return srgb_to_linear(to_4f(src) * Sk4f(1.0f/255));
reedfbc1e292016-01-29 05:22:59 -080047}
48
49static inline uint32_t Sk4f_toL32(const Sk4f& x4) {
50 return to_4b(x4 * Sk4f(255) + Sk4f(0.5f));
51}
52
53static inline uint32_t Sk4f_toS32(const Sk4f& x4) {
reed395eabe2016-01-30 18:52:31 -080054 return to_4b(linear_to_srgb(x4) * Sk4f(255) + Sk4f(0.5f));
reedfbc1e292016-01-29 05:22:59 -080055}