OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrDistanceFieldAdjustTable.h" | 8 #include "GrDistanceFieldAdjustTable.h" |
9 | 9 |
10 #include "SkScalerContext.h" | 10 #include "SkScalerContext.h" |
11 | 11 |
12 SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;) | 12 SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;) |
13 | 13 |
14 void GrDistanceFieldAdjustTable::buildDistanceAdjustTable() { | 14 SkScalar* build_distance_adjust_table(SkScalar paintGamma, SkScalar deviceGamma)
{ |
15 // This is used for an approximation of the mask gamma hack, used by raster
and bitmap | 15 // This is used for an approximation of the mask gamma hack, used by raster
and bitmap |
16 // text. The mask gamma hack is based off of guessing what the blend color i
s going to | 16 // text. The mask gamma hack is based off of guessing what the blend color i
s going to |
17 // be, and adjusting the mask so that when run through the linear blend will | 17 // be, and adjusting the mask so that when run through the linear blend will |
18 // produce the value closest to the desired result. However, in practice thi
s means | 18 // produce the value closest to the desired result. However, in practice thi
s means |
19 // that the 'adjusted' mask is just increasing or decreasing the coverage of | 19 // that the 'adjusted' mask is just increasing or decreasing the coverage of |
20 // the mask depending on what it is thought it will blit against. For black
(on | 20 // the mask depending on what it is thought it will blit against. For black
(on |
21 // assumed white) this means that coverages are decreased (on a curve). For
white (on | 21 // assumed white) this means that coverages are decreased (on a curve). For
white (on |
22 // assumed black) this means that coverages are increased (on a a curve). At | 22 // assumed black) this means that coverages are increased (on a a curve). At |
23 // middle (perceptual) gray (which could be blit against anything) the cover
ages | 23 // middle (perceptual) gray (which could be blit against anything) the cover
ages |
24 // remain the same. | 24 // remain the same. |
(...skipping 23 matching lines...) Expand all Loading... |
48 // previously covered at all. | 48 // previously covered at all. |
49 | 49 |
50 int width, height; | 50 int width, height; |
51 size_t size; | 51 size_t size; |
52 | 52 |
53 #ifdef SK_GAMMA_CONTRAST | 53 #ifdef SK_GAMMA_CONTRAST |
54 SkScalar contrast = SK_GAMMA_CONTRAST; | 54 SkScalar contrast = SK_GAMMA_CONTRAST; |
55 #else | 55 #else |
56 SkScalar contrast = 0.5f; | 56 SkScalar contrast = 0.5f; |
57 #endif | 57 #endif |
58 SkScalar paintGamma = SK_GAMMA_EXPONENT; | |
59 SkScalar deviceGamma = SK_GAMMA_EXPONENT; | |
60 | 58 |
61 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma, | 59 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma, |
62 &width, &height); | 60 &width, &height); |
63 | 61 |
64 SkASSERT(kExpectedDistanceAdjustTableSize == height); | 62 SkASSERT(kExpectedDistanceAdjustTableSize == height); |
65 fTable = new SkScalar[height]; | 63 SkScalar* table = new SkScalar[height]; |
66 | 64 |
67 SkAutoTArray<uint8_t> data((int)size); | 65 SkAutoTArray<uint8_t> data((int)size); |
68 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get
()); | 66 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get
()); |
69 | 67 |
70 // find the inverse points where we cross 0.5 | 68 // find the inverse points where we cross 0.5 |
71 // binsearch might be better, but we only need to do this once on creation | 69 // binsearch might be better, but we only need to do this once on creation |
72 for (int row = 0; row < height; ++row) { | 70 for (int row = 0; row < height; ++row) { |
73 uint8_t* rowPtr = data.get() + row*width; | 71 uint8_t* rowPtr = data.get() + row*width; |
74 for (int col = 0; col < width - 1; ++col) { | 72 for (int col = 0; col < width - 1; ++col) { |
75 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) { | 73 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) { |
76 // compute point where a mask value will give us a result of 0.5 | 74 // compute point where a mask value will give us a result of 0.5 |
77 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPt
r[col]); | 75 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPt
r[col]); |
78 float borderAlpha = (col + interp) / 255.f; | 76 float borderAlpha = (col + interp) / 255.f; |
79 | 77 |
80 // compute t value for that alpha | 78 // compute t value for that alpha |
81 // this is an approximate inverse for smoothstep() | 79 // this is an approximate inverse for smoothstep() |
82 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5
.0f) / 3.0f; | 80 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5
.0f) / 3.0f; |
83 | 81 |
84 // compute distance which gives us that t value | 82 // compute distance which gives us that t value |
85 const float kDistanceFieldAAFactor = 0.65f; // should match SK_D
istanceFieldAAFactor | 83 const float kDistanceFieldAAFactor = 0.65f; // should match SK_D
istanceFieldAAFactor |
86 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor
; | 84 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor
; |
87 | 85 |
88 fTable[row] = d; | 86 table[row] = d; |
89 break; | 87 break; |
90 } | 88 } |
91 } | 89 } |
92 } | 90 } |
| 91 |
| 92 return table; |
93 } | 93 } |
| 94 |
| 95 void GrDistanceFieldAdjustTable::buildDistanceAdjustTables() { |
| 96 fTable = build_distance_adjust_table(SK_GAMMA_EXPONENT, SK_GAMMA_EXPONENT); |
| 97 fSRGBTable = build_distance_adjust_table(SK_Scalar1, SK_Scalar1); |
| 98 } |
OLD | NEW |