1 #include "SkFontHost.h"
2 #include <math.h>
3
4 // define this to use pre-compiled tables for gamma. This is slightly faster,
5 // and doesn't create any RW global memory, but means we cannot change the
6 // gamma at runtime.
7 //#define USE_PREDEFINED_GAMMA_TABLES
8
9 #ifndef USE_PREDEFINED_GAMMA_TABLES
10 // define this if you want to spew out the "C" code for the tables, given
11 // the current values for SK_BLACK_GAMMA and SK_WHITE_GAMMA.
12 #define DUMP_GAMMA_TABLESx
13 #endif
14
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include "SkGraphics.h"
18
19 // declared here, so we can link against it elsewhere
20 void skia_set_text_gamma(float blackGamma, float whiteGamma);
21
22 #ifdef USE_PREDEFINED_GAMMA_TABLES
23
24 #include "sk_predefined_gamma.h"
25
skia_set_text_gamma(float blackGamma,float whiteGamma)26 void skia_set_text_gamma(float blackGamma, float whiteGamma) {}
27
28 #else // use writable globals for gamma tables
29
build_power_table(uint8_t table[],float ee)30 static void build_power_table(uint8_t table[], float ee)
31 {
32 SkDebugf("------ build_power_table %g\n", ee);
33 for (int i = 0; i < 256; i++)
34 {
35 float x = i / 255.f;
36 // printf(" %d %g", i, x);
37 x = powf(x, ee);
38 // printf(" %g", x);
39 int xx = SkScalarRound(SkFloatToScalar(x * 255));
40 // printf(" %d\n", xx);
41 table[i] = SkToU8(xx);
42 }
43 }
44
45 static bool gGammaIsBuilt;
46 static uint8_t gBlackGamma[256], gWhiteGamma[256];
47
48 static float gBlackGammaCoeff = 1.4f;
49 static float gWhiteGammaCoeff = 1/1.4f;
50
skia_set_text_gamma(float blackGamma,float whiteGamma)51 void skia_set_text_gamma(float blackGamma, float whiteGamma) {
52 gBlackGammaCoeff = blackGamma;
53 gWhiteGammaCoeff = whiteGamma;
54 gGammaIsBuilt = false;
55 SkGraphics::SetFontCacheUsed(0);
56 build_power_table(gBlackGamma, gBlackGammaCoeff);
57 build_power_table(gWhiteGamma, gWhiteGammaCoeff);
58 }
59
60 #ifdef DUMP_GAMMA_TABLES
61
62 #include "SkString.h"
63
dump_a_table(const char name[],const uint8_t table[],float gamma)64 static void dump_a_table(const char name[], const uint8_t table[],
65 float gamma) {
66 SkDebugf("\n");
67 SkDebugf("\/\/ Gamma table for %g\n", gamma);
68 SkDebugf("static const uint8_t %s[] = {\n", name);
69 for (int y = 0; y < 16; y++) {
70 SkString line, tmp;
71 for (int x = 0; x < 16; x++) {
72 tmp.printf("0x%02X, ", *table++);
73 line.append(tmp);
74 }
75 SkDebugf(" %s\n", line.c_str());
76 }
77 SkDebugf("};\n");
78 }
79
80 #endif
81
82 #endif
83
84 ///////////////////////////////////////////////////////////////////////////////
85
GetGammaTables(const uint8_t * tables[2])86 void SkFontHost::GetGammaTables(const uint8_t* tables[2])
87 {
88 #ifndef USE_PREDEFINED_GAMMA_TABLES
89 if (!gGammaIsBuilt)
90 {
91 build_power_table(gBlackGamma, gBlackGammaCoeff);
92 build_power_table(gWhiteGamma, gWhiteGammaCoeff);
93 gGammaIsBuilt = true;
94
95 #ifdef DUMP_GAMMA_TABLES
96 dump_a_table("gBlackGamma", gBlackGamma, gBlackGammaCoeff);
97 dump_a_table("gWhiteGamma", gWhiteGamma, gWhiteGammaCoeff);
98 #endif
99 }
100 #endif
101 tables[0] = gBlackGamma;
102 tables[1] = gWhiteGamma;
103 }
104
105 // If the luminance is <= this value, then apply the black gamma table
106 #define BLACK_GAMMA_THRESHOLD 0x40
107
108 // If the luminance is >= this value, then apply the white gamma table
109 #define WHITE_GAMMA_THRESHOLD 0xC0
110
ComputeGammaFlag(const SkPaint & paint)111 int SkFontHost::ComputeGammaFlag(const SkPaint& paint)
112 {
113 if (paint.getShader() == NULL)
114 {
115 SkColor c = paint.getColor();
116 int r = SkColorGetR(c);
117 int g = SkColorGetG(c);
118 int b = SkColorGetB(c);
119 int luminance = (r * 2 + g * 5 + b) >> 3;
120
121 if (luminance <= BLACK_GAMMA_THRESHOLD)
122 {
123 // printf("------ black gamma for [%d %d %d]\n", r, g, b);
124 return SkScalerContext::kGammaForBlack_Flag;
125 }
126 if (luminance >= WHITE_GAMMA_THRESHOLD)
127 {
128 // printf("------ white gamma for [%d %d %d]\n", r, g, b);
129 return SkScalerContext::kGammaForWhite_Flag;
130 }
131 }
132 return 0;
133 }
134
135