1 /*
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
8 #ifndef SkColorSpaceXformPriv_DEFINED
9 #define SkColorSpaceXformPriv_DEFINED
10
11 #include "SkColorSpace_Base.h"
12 #include "SkColorSpaceXform.h"
13 #include "SkHalf.h"
14 #include "SkSRGB.h"
15
16 #define SkCSXformPrintfDefined 0
17 #define SkCSXformPrintf(...)
18
19 // Interpolating lookup in a variably sized table.
interp_lut(float input,const float * table,int tableSize)20 static inline float interp_lut(float input, const float* table, int tableSize) {
21 float index = input * (tableSize - 1);
22 float diff = index - sk_float_floor2int(index);
23 return table[(int) sk_float_floor2int(index)] * (1.0f - diff) +
24 table[(int) sk_float_ceil2int(index)] * diff;
25 }
26
27 // Expand range from 0-1 to 0-255, then convert.
clamp_normalized_float_to_byte(float v)28 static inline uint8_t clamp_normalized_float_to_byte(float v) {
29 // The ordering of the logic is a little strange here in order
30 // to make sure we convert NaNs to 0.
31 v = v * 255.0f;
32 if (v >= 254.5f) {
33 return 255;
34 } else if (v >= 0.5f) {
35 return (uint8_t) (v + 0.5f);
36 } else {
37 return 0;
38 }
39 }
40
clamp_0_1(float v)41 static inline float clamp_0_1(float v) {
42 if (v >= 1.0f) {
43 return 1.0f;
44 } else if (v >= 0.0f) {
45 return v;
46 } else {
47 return 0.0f;
48 }
49 }
50
51 /**
52 * Invert table lookup. Ex: what indices corresponds to the input values?
53 * This will have strange results when the table is not increasing.
54 * But any sane gamma function will be increasing.
55 * @param outTableFloat Destination table for float (0-1) results. Can be nullptr if not wanted.
56 * @param outTableByte Destination table for byte (0-255) results. Can be nullptr if not wanted.
57 * @param outTableSize Number of elements in |outTableFloat| or |outTableBytes|
58 * @param inTable The source table to invert
59 * @param inTableSize The number of elements in |inTable|
60 */
invert_table_gamma(float * outTableFloat,uint8_t * outTableByte,int outTableSize,const float * inTable,int inTableSize)61 static inline void invert_table_gamma(float* outTableFloat, uint8_t* outTableByte,
62 int outTableSize, const float* inTable, int inTableSize) {
63 // should never have a gamma table this small anyway, 0/1 are either not allowed
64 // or imply a non-table gamma such as linear/exponential
65 SkASSERT(inTableSize >= 2);
66 int inIndex = 1;
67 for (int outIndex = 0; outIndex < outTableSize; ++outIndex) {
68 const float input = outIndex / (outTableSize - 1.0f);
69 while (inIndex < inTableSize - 1 && inTable[inIndex] < input) {
70 ++inIndex;
71 }
72 const float diff = input - inTable[inIndex - 1];
73 const float distance = inTable[inIndex] - inTable[inIndex - 1];
74 const float normalizedIndex = (inIndex - 1) + diff / distance;
75 const float index = normalizedIndex / (inTableSize - 1);
76 if (outTableByte) {
77 outTableByte[outIndex] = clamp_normalized_float_to_byte(index);
78 }
79 if (outTableFloat) {
80 outTableFloat[outIndex] = clamp_0_1(index);
81 }
82 }
83 }
84
select_xform_format(SkColorType colorType)85 static inline SkColorSpaceXform::ColorFormat select_xform_format(SkColorType colorType) {
86 switch (colorType) {
87 case kRGBA_8888_SkColorType:
88 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
89 case kBGRA_8888_SkColorType:
90 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
91 case kRGBA_F16_SkColorType:
92 return SkColorSpaceXform::kRGBA_F16_ColorFormat;
93 case kRGB_565_SkColorType:
94 return SkColorSpaceXform::kBGR_565_ColorFormat;
95 default:
96 SkASSERT(false);
97 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
98 }
99 }
100
101 #endif
102