gfx/skia/trunk/src/core/SkMaskGamma.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkTypes.h"
michael@0 9
michael@0 10 #include "SkColor.h"
michael@0 11 #include "SkFloatingPoint.h"
michael@0 12 #include "SkMaskGamma.h"
michael@0 13
michael@0 14 class SkLinearColorSpaceLuminance : public SkColorSpaceLuminance {
michael@0 15 virtual SkScalar toLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luminance) const SK_OVERRIDE {
michael@0 16 SkASSERT(SK_Scalar1 == gamma);
michael@0 17 return luminance;
michael@0 18 }
michael@0 19 virtual SkScalar fromLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luma) const SK_OVERRIDE {
michael@0 20 SkASSERT(SK_Scalar1 == gamma);
michael@0 21 return luma;
michael@0 22 }
michael@0 23 };
michael@0 24
michael@0 25 class SkGammaColorSpaceLuminance : public SkColorSpaceLuminance {
michael@0 26 virtual SkScalar toLuma(SkScalar gamma, SkScalar luminance) const SK_OVERRIDE {
michael@0 27 return SkScalarPow(luminance, gamma);
michael@0 28 }
michael@0 29 virtual SkScalar fromLuma(SkScalar gamma, SkScalar luma) const SK_OVERRIDE {
michael@0 30 return SkScalarPow(luma, SkScalarInvert(gamma));
michael@0 31 }
michael@0 32 };
michael@0 33
michael@0 34 class SkSRGBColorSpaceLuminance : public SkColorSpaceLuminance {
michael@0 35 virtual SkScalar toLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luminance) const SK_OVERRIDE {
michael@0 36 SkASSERT(0 == gamma);
michael@0 37 //The magic numbers are derived from the sRGB specification.
michael@0 38 //See http://www.color.org/chardata/rgb/srgb.xalter .
michael@0 39 if (luminance <= 0.04045f) {
michael@0 40 return luminance / 12.92f;
michael@0 41 }
michael@0 42 return SkScalarPow((luminance + 0.055f) / 1.055f,
michael@0 43 2.4f);
michael@0 44 }
michael@0 45 virtual SkScalar fromLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luma) const SK_OVERRIDE {
michael@0 46 SkASSERT(0 == gamma);
michael@0 47 //The magic numbers are derived from the sRGB specification.
michael@0 48 //See http://www.color.org/chardata/rgb/srgb.xalter .
michael@0 49 if (luma <= 0.0031308f) {
michael@0 50 return luma * 12.92f;
michael@0 51 }
michael@0 52 return 1.055f * SkScalarPow(luma, SkScalarInvert(2.4f))
michael@0 53 - 0.055f;
michael@0 54 }
michael@0 55 };
michael@0 56
michael@0 57 /*static*/ const SkColorSpaceLuminance& SkColorSpaceLuminance::Fetch(SkScalar gamma) {
michael@0 58 static SkLinearColorSpaceLuminance gSkLinearColorSpaceLuminance;
michael@0 59 static SkGammaColorSpaceLuminance gSkGammaColorSpaceLuminance;
michael@0 60 static SkSRGBColorSpaceLuminance gSkSRGBColorSpaceLuminance;
michael@0 61
michael@0 62 if (0 == gamma) {
michael@0 63 return gSkSRGBColorSpaceLuminance;
michael@0 64 } else if (SK_Scalar1 == gamma) {
michael@0 65 return gSkLinearColorSpaceLuminance;
michael@0 66 } else {
michael@0 67 return gSkGammaColorSpaceLuminance;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 static float apply_contrast(float srca, float contrast) {
michael@0 72 return srca + ((1.0f - srca) * contrast * srca);
michael@0 73 }
michael@0 74
michael@0 75 void SkTMaskGamma_build_correcting_lut(uint8_t table[256], U8CPU srcI, SkScalar contrast,
michael@0 76 const SkColorSpaceLuminance& srcConvert, SkScalar srcGamma,
michael@0 77 const SkColorSpaceLuminance& dstConvert, SkScalar dstGamma) {
michael@0 78 const float src = (float)srcI / 255.0f;
michael@0 79 const float linSrc = srcConvert.toLuma(srcGamma, src);
michael@0 80 //Guess at the dst. The perceptual inverse provides smaller visual
michael@0 81 //discontinuities when slight changes to desaturated colors cause a channel
michael@0 82 //to map to a different correcting lut with neighboring srcI.
michael@0 83 //See https://code.google.com/p/chromium/issues/detail?id=141425#c59 .
michael@0 84 const float dst = 1.0f - src;
michael@0 85 const float linDst = dstConvert.toLuma(dstGamma, dst);
michael@0 86
michael@0 87 //Contrast value tapers off to 0 as the src luminance becomes white
michael@0 88 const float adjustedContrast = SkScalarToFloat(contrast) * linDst;
michael@0 89
michael@0 90 //Remove discontinuity and instability when src is close to dst.
michael@0 91 //The value 1/256 is arbitrary and appears to contain the instability.
michael@0 92 if (fabs(src - dst) < (1.0f / 256.0f)) {
michael@0 93 float ii = 0.0f;
michael@0 94 for (int i = 0; i < 256; ++i, ii += 1.0f) {
michael@0 95 float rawSrca = ii / 255.0f;
michael@0 96 float srca = apply_contrast(rawSrca, adjustedContrast);
michael@0 97 table[i] = SkToU8(sk_float_round2int(255.0f * srca));
michael@0 98 }
michael@0 99 } else {
michael@0 100 // Avoid slow int to float conversion.
michael@0 101 float ii = 0.0f;
michael@0 102 for (int i = 0; i < 256; ++i, ii += 1.0f) {
michael@0 103 // 'rawSrca += 1.0f / 255.0f' and even
michael@0 104 // 'rawSrca = i * (1.0f / 255.0f)' can add up to more than 1.0f.
michael@0 105 // When this happens the table[255] == 0x0 instead of 0xff.
michael@0 106 // See http://code.google.com/p/chromium/issues/detail?id=146466
michael@0 107 float rawSrca = ii / 255.0f;
michael@0 108 float srca = apply_contrast(rawSrca, adjustedContrast);
michael@0 109 SkASSERT(srca <= 1.0f);
michael@0 110 float dsta = 1.0f - srca;
michael@0 111
michael@0 112 //Calculate the output we want.
michael@0 113 float linOut = (linSrc * srca + dsta * linDst);
michael@0 114 SkASSERT(linOut <= 1.0f);
michael@0 115 float out = dstConvert.fromLuma(dstGamma, linOut);
michael@0 116
michael@0 117 //Undo what the blit blend will do.
michael@0 118 float result = (out - dst) / (src - dst);
michael@0 119 SkASSERT(sk_float_round2int(255.0f * result) <= 255);
michael@0 120
michael@0 121 table[i] = SkToU8(sk_float_round2int(255.0f * result));
michael@0 122 }
michael@0 123 }
michael@0 124 }

mercurial