• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef USE_PREDEFINED_GAMMA_TABLES
18 
19 #include "sk_predefined_gamma.h"
20 
21 #else   // use writable globals for gamma tables
22 
23 static bool gGammaIsBuilt;
24 static uint8_t gBlackGamma[256], gWhiteGamma[256];
25 
26 #define SK_BLACK_GAMMA     (1.4f)
27 #define SK_WHITE_GAMMA     (1/1.4f)
28 
build_power_table(uint8_t table[],float ee)29 static void build_power_table(uint8_t table[], float ee)
30 {
31     //    printf("------ build_power_table %g\n", ee);
32     for (int i = 0; i < 256; i++)
33     {
34         float x = i / 255.f;
35         //   printf(" %d %g", i, x);
36         x = powf(x, ee);
37         //   printf(" %g", x);
38         int xx = SkScalarRound(SkFloatToScalar(x * 255));
39         //   printf(" %d\n", xx);
40         table[i] = SkToU8(xx);
41     }
42 }
43 
44 #ifdef DUMP_GAMMA_TABLES
45 
46 #include "SkString.h"
47 
dump_a_table(const char name[],const uint8_t table[],float gamma)48 static void dump_a_table(const char name[], const uint8_t table[],
49                          float gamma) {
50     SkDebugf("\n");
51     SkDebugf("\/\/ Gamma table for %g\n", gamma);
52     SkDebugf("static const uint8_t %s[] = {\n", name);
53     for (int y = 0; y < 16; y++) {
54         SkString line, tmp;
55         for (int x = 0; x < 16; x++) {
56             tmp.printf("0x%02X, ", *table++);
57             line.append(tmp);
58         }
59         SkDebugf("    %s\n", line.c_str());
60     }
61     SkDebugf("};\n");
62 }
63 
64 #endif
65 
66 #endif
67 
68 ///////////////////////////////////////////////////////////////////////////////
69 
GetGammaTables(const uint8_t * tables[2])70 void SkFontHost::GetGammaTables(const uint8_t* tables[2])
71 {
72 #ifndef USE_PREDEFINED_GAMMA_TABLES
73     if (!gGammaIsBuilt)
74     {
75         build_power_table(gBlackGamma, SK_BLACK_GAMMA);
76         build_power_table(gWhiteGamma, SK_WHITE_GAMMA);
77         gGammaIsBuilt = true;
78 
79 #ifdef DUMP_GAMMA_TABLES
80         dump_a_table("gBlackGamma", gBlackGamma, SK_BLACK_GAMMA);
81         dump_a_table("gWhiteGamma", gWhiteGamma, SK_WHITE_GAMMA);
82 #endif
83     }
84 #endif
85     tables[0] = gBlackGamma;
86     tables[1] = gWhiteGamma;
87 }
88 
89 // If the luminance is <= this value, then apply the black gamma table
90 #define BLACK_GAMMA_THRESHOLD   0x40
91 
92 // If the luminance is >= this value, then apply the white gamma table
93 #define WHITE_GAMMA_THRESHOLD   0xC0
94 
ComputeGammaFlag(const SkPaint & paint)95 int SkFontHost::ComputeGammaFlag(const SkPaint& paint)
96 {
97     if (paint.getShader() == NULL)
98     {
99         SkColor c = paint.getColor();
100         int r = SkColorGetR(c);
101         int g = SkColorGetG(c);
102         int b = SkColorGetB(c);
103         int luminance = (r * 2 + g * 5 + b) >> 3;
104 
105         if (luminance <= BLACK_GAMMA_THRESHOLD)
106         {
107         //    printf("------ black gamma for [%d %d %d]\n", r, g, b);
108             return SkScalerContext::kGammaForBlack_Flag;
109         }
110         if (luminance >= WHITE_GAMMA_THRESHOLD)
111         {
112         //    printf("------ white gamma for [%d %d %d]\n", r, g, b);
113             return SkScalerContext::kGammaForWhite_Flag;
114         }
115     }
116     return 0;
117 }
118 
119