Debian Codegraph @

src/ports/SkNDKConversions.cpp

raw
1 /*
2 * Copyright 2020 Google LLC
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 #include "src/ports/SkNDKConversions.h"
9
10 namespace {
11 static const struct {
12 SkColorType colorType;
13 AndroidBitmapFormat format;
14 } gColorTypeTable[] = {
15 { kRGBA_8888_SkColorType, ANDROID_BITMAP_FORMAT_RGBA_8888 },
16 { kRGBA_F16_SkColorType, ANDROID_BITMAP_FORMAT_RGBA_F16 },
17 { kRGB_565_SkColorType, ANDROID_BITMAP_FORMAT_RGB_565 },
18 // Android allows using its alpha 8 format to get 8 bit gray pixels.
19 { kGray_8_SkColorType, ANDROID_BITMAP_FORMAT_A_8 },
20 };
21
22 } // anonymous namespace
23
24 namespace SkNDKConversions {
25 AndroidBitmapFormat toAndroidBitmapFormat(SkColorType colorType) {
26 for (const auto& entry : gColorTypeTable) {
27 if (entry.colorType == colorType) {
28 return entry.format;
29 }
30 }
31 return ANDROID_BITMAP_FORMAT_NONE;
32 }
33
34 uint32_t toAndroidBitmapAlphaFlags(SkAlphaType alphaType) {
35 switch (alphaType) {
36 case kPremul_SkAlphaType:
37 return ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
38 case kOpaque_SkAlphaType:
39 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
40 case kUnpremul_SkAlphaType:
41 return ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL;
42 default:
43 SkDEBUGFAIL("unspecified alphaType");
44 return ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE;
45 }
46 }
47
48 SkColorType toColorType(AndroidBitmapFormat format) {
49 for (const auto& entry : gColorTypeTable) {
50 if (entry.format == format) {
51 return entry.colorType;
52 }
53 }
54 return kUnknown_SkColorType;
55 }
56
57 } // SkNDKConversions
58
59 static constexpr skcms_TransferFunction k2Dot6 = {2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
60
61 static constexpr skcms_Matrix3x3 kDCIP3 = {{
62 {0.486143, 0.323835, 0.154234},
63 {0.226676, 0.710327, 0.0629966},
64 {0.000800549, 0.0432385, 0.78275},
65 }};
66
67 namespace {
68 static const struct {
69 ADataSpace dataSpace;
70 skcms_TransferFunction transferFunction;
71 skcms_Matrix3x3 gamut;
72 } gColorSpaceTable[] = {
73 { ADATASPACE_SRGB, SkNamedTransferFn::kSRGB, SkNamedGamut::kSRGB },
74 { ADATASPACE_SCRGB, SkNamedTransferFn::kSRGB, SkNamedGamut::kSRGB },
75 { ADATASPACE_SCRGB_LINEAR, SkNamedTransferFn::kLinear, SkNamedGamut::kSRGB },
76 { ADATASPACE_SRGB_LINEAR, SkNamedTransferFn::kLinear, SkNamedGamut::kSRGB },
77 { ADATASPACE_ADOBE_RGB, SkNamedTransferFn::k2Dot2, SkNamedGamut::kAdobeRGB },
78 { ADATASPACE_DISPLAY_P3, SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3 },
79 { ADATASPACE_BT2020, SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020 },
80 { ADATASPACE_BT709, SkNamedTransferFn::kRec2020, SkNamedGamut::kSRGB },
81 { ADATASPACE_DCI_P3, k2Dot6, kDCIP3 },
82 };
83
84 } // anonymous namespace
85
86 static bool nearly_equal(float a, float b) {
87 return fabs(a - b) < .002f;
88 }
89
90 static bool nearly_equal(const skcms_TransferFunction& x, const skcms_TransferFunction& y) {
91 return nearly_equal(x.g, y.g)
92 && nearly_equal(x.a, y.a)
93 && nearly_equal(x.b, y.b)
94 && nearly_equal(x.c, y.c)
95 && nearly_equal(x.d, y.d)
96 && nearly_equal(x.e, y.e)
97 && nearly_equal(x.f, y.f);
98 }
99
100 static bool nearly_equal(const skcms_Matrix3x3& a, const skcms_Matrix3x3& b) {
101 for (int i = 0; i < 3; i++)
102 for (int j = 0; j < 3; j++) {
103 if (!nearly_equal(a.vals[i][j], b.vals[i][j])) return false;
104 }
105 return true;
106 }
107
108 namespace SkNDKConversions {
109 ADataSpace toDataSpace(SkColorSpace* cs) {
110 if (!cs) return ADATASPACE_SRGB;
111
112 skcms_TransferFunction fn;
113 skcms_Matrix3x3 gamut;
114 if (cs->isNumericalTransferFn(&fn) && cs->toXYZD50(&gamut)) {
115 for (const auto& entry : gColorSpaceTable) {
116 if (nearly_equal(gamut, entry.gamut) && nearly_equal(fn, entry.transferFunction)) {
117 return entry.dataSpace;
118 }
119 }
120 }
121 return ADATASPACE_UNKNOWN;
122 }
123
124 sk_sp<SkColorSpace> toColorSpace(ADataSpace dataSpace) {
125 for (const auto& entry : gColorSpaceTable) {
126 if (entry.dataSpace == dataSpace) {
127 return SkColorSpace::MakeRGB(entry.transferFunction, entry.gamut);
128 }
129 }
130 return nullptr;
131 }
132 }
133