Flutter Engine
The Flutter Engine
Classes | Functions
SkMaskGamma.cpp File Reference
#include "src/core/SkMaskGamma.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkFloatingPoint.h"
#include "include/private/base/SkTo.h"
#include <cmath>

Go to the source code of this file.

Classes

class  SkLinearColorSpaceLuminance
 
class  SkGammaColorSpaceLuminance
 
class  SkSRGBColorSpaceLuminance
 

Functions

static float apply_contrast (float srca, float contrast)
 
void SkTMaskGamma_build_correcting_lut (uint8_t table[256], U8CPU srcI, SkScalar contrast, const SkColorSpaceLuminance &dstConvert, SkScalar dstGamma)
 

Function Documentation

◆ apply_contrast()

static float apply_contrast ( float  srca,
float  contrast 
)
static

Definition at line 74 of file SkMaskGamma.cpp.

74 {
75 return srca + ((1.0f - srca) * contrast * srca);
76}

◆ SkTMaskGamma_build_correcting_lut()

void SkTMaskGamma_build_correcting_lut ( uint8_t  table[256],
U8CPU  srcI,
SkScalar  contrast,
const SkColorSpaceLuminance dstConvert,
SkScalar  dstGamma 
)

Definition at line 78 of file SkMaskGamma.cpp.

79 {
80 const SkColorSpaceLuminance& srcConvert = dstConvert;
81 const SkScalar srcGamma = dstGamma;
82 const float src = (float)srcI / 255.0f;
83 const float linSrc = srcConvert.toLuma(srcGamma, src);
84 //Guess at the dst. The perceptual inverse provides smaller visual
85 //discontinuities when slight changes to desaturated colors cause a channel
86 //to map to a different correcting lut with neighboring srcI.
87 //See https://code.google.com/p/chromium/issues/detail?id=141425#c59 .
88 const float dst = 1.0f - src;
89 const float linDst = dstConvert.toLuma(dstGamma, dst);
90
91 //Contrast value tapers off to 0 as the src luminance becomes white
92 const float adjustedContrast = contrast * linDst;
93
94 //Remove discontinuity and instability when src is close to dst.
95 //The value 1/256 is arbitrary and appears to contain the instability.
96 if (fabs(src - dst) < (1.0f / 256.0f)) {
97 float ii = 0.0f;
98 for (int i = 0; i < 256; ++i, ii += 1.0f) {
99 float rawSrca = ii / 255.0f;
100 float srca = apply_contrast(rawSrca, adjustedContrast);
101 table[i] = SkToU8(sk_float_round2int(255.0f * srca));
102 }
103 } else {
104 // Avoid slow int to float conversion.
105 float ii = 0.0f;
106 for (int i = 0; i < 256; ++i, ii += 1.0f) {
107 // 'rawSrca += 1.0f / 255.0f' and even
108 // 'rawSrca = i * (1.0f / 255.0f)' can add up to more than 1.0f.
109 // When this happens the table[255] == 0x0 instead of 0xff.
110 // See http://code.google.com/p/chromium/issues/detail?id=146466
111 float rawSrca = ii / 255.0f;
112 float srca = apply_contrast(rawSrca, adjustedContrast);
113 SkASSERT(srca <= 1.0f);
114 float dsta = 1.0f - srca;
115
116 //Calculate the output we want.
117 float linOut = (linSrc * srca + dsta * linDst);
118 SkASSERT(linOut <= 1.0f);
119 float out = dstConvert.fromLuma(dstGamma, linOut);
120
121 //Undo what the blit blend will do.
122 float result = (out - dst) / (src - dst);
123 SkASSERT(sk_float_round2int(255.0f * result) <= 255);
124
125 table[i] = SkToU8(sk_float_round2int(255.0f * result));
126 }
127 }
128}
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define sk_float_round2int(x)
static float apply_contrast(float srca, float contrast)
Definition: SkMaskGamma.cpp:74
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
SI F table(const skcms_Curve *curve, F v)
virtual SkScalar fromLuma(SkScalar gamma, SkScalar luma) const =0
virtual SkScalar toLuma(SkScalar gamma, SkScalar luminance) const =0
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
dst
Definition: cp.py:12