Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: src/core/SkBitmapProcBicubic.cpp

Issue 15941012: specialize bicubic filtering for scale-only (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "SkBitmapProcState.h" 8 #include "SkBitmapProcState.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkColor.h" 10 #include "SkColor.h"
(...skipping 17 matching lines...) Expand all
28 int g = SkClampMax(SkFixedRoundToInt(fg), a); 28 int g = SkClampMax(SkFixedRoundToInt(fg), a);
29 int b = SkClampMax(SkFixedRoundToInt(fb), a); 29 int b = SkClampMax(SkFixedRoundToInt(fb), a);
30 30
31 return SkPackARGB32(a, r, g, b); 31 return SkPackARGB32(a, r, g, b);
32 } 32 }
33 33
34 static float poly_eval(const float cc[4], float t) { 34 static float poly_eval(const float cc[4], float t) {
35 return cc[0] + t * (cc[1] + t * (cc[2] + t * cc[3])); 35 return cc[0] + t * (cc[1] + t * (cc[2] + t * cc[3]));
36 } 36 }
37 37
38 static void build_coeff4(SkFixed dst[4], const float src[16], float t) { 38 static void build_coeff4(SkFixed dst[4], float t) {
39 dst[0] = SkFloatToFixed(poly_eval(&src[ 0], t));
40 dst[1] = SkFloatToFixed(poly_eval(&src[ 4], t));
41 dst[2] = SkFloatToFixed(poly_eval(&src[ 8], t));
42 dst[3] = SkFloatToFixed(poly_eval(&src[12], t));
43 }
44
45 static void bicubicFilter(const SkBitmapProcState& s, int x, int y,
46 SkPMColor* SK_RESTRICT colors, int count) {
47
48 static const SkScalar coefficients[16] = { 39 static const SkScalar coefficients[16] = {
49 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), 40 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
50 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), 41 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
51 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), 42 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
52 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), 43 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
53 }; 44 };
45
46 dst[0] = SkFloatToFixed(poly_eval(&coefficients[ 0], t));
47 dst[1] = SkFloatToFixed(poly_eval(&coefficients[ 4], t));
48 dst[2] = SkFloatToFixed(poly_eval(&coefficients[ 8], t));
49 dst[3] = SkFloatToFixed(poly_eval(&coefficients[12], t));
50 }
51
52 static SkPMColor doBicubicFilter(const SkBitmap *bm, SkFixed coeffX[4], SkFixed coeffY[4],
53 int x0, int x1, int x2, int x3,
54 int y0, int y1, int y2, int y3 )
55 {
56 SkPMColor s00 = *bm->getAddr32(x0, y0);
57 SkPMColor s10 = *bm->getAddr32(x1, y0);
58 SkPMColor s20 = *bm->getAddr32(x2, y0);
59 SkPMColor s30 = *bm->getAddr32(x3, y0);
60 SkPMColor s0 = cubicBlend(coeffX, s00, s10, s20, s30);
61 SkPMColor s01 = *bm->getAddr32(x0, y1);
62 SkPMColor s11 = *bm->getAddr32(x1, y1);
63 SkPMColor s21 = *bm->getAddr32(x2, y1);
64 SkPMColor s31 = *bm->getAddr32(x3, y1);
65 SkPMColor s1 = cubicBlend(coeffX, s01, s11, s21, s31);
66 SkPMColor s02 = *bm->getAddr32(x0, y2);
67 SkPMColor s12 = *bm->getAddr32(x1, y2);
68 SkPMColor s22 = *bm->getAddr32(x2, y2);
69 SkPMColor s32 = *bm->getAddr32(x3, y2);
70 SkPMColor s2 = cubicBlend(coeffX, s02, s12, s22, s32);
71 SkPMColor s03 = *bm->getAddr32(x0, y3);
72 SkPMColor s13 = *bm->getAddr32(x1, y3);
73 SkPMColor s23 = *bm->getAddr32(x2, y3);
74 SkPMColor s33 = *bm->getAddr32(x3, y3);
75 SkPMColor s3 = cubicBlend(coeffX, s03, s13, s23, s33);
76 return cubicBlend(coeffY, s0, s1, s2, s3);
77 }
78
79 static void bicubicFilter(const SkBitmapProcState& s, int x, int y,
80 SkPMColor* SK_RESTRICT colors, int count) {
54 81
55 const int maxX = s.fBitmap->width() - 1; 82 const int maxX = s.fBitmap->width() - 1;
56 const int maxY = s.fBitmap->height() - 1; 83 const int maxY = s.fBitmap->height() - 1;
57 84
58 while (count-- > 0) { 85 while (count-- > 0) {
59 SkPoint srcPt; 86 SkPoint srcPt;
60 s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), 87 s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
61 SkIntToScalar(y), &srcPt); 88 SkIntToScalar(y), &srcPt);
62 srcPt.fX -= SK_ScalarHalf; 89 srcPt.fX -= SK_ScalarHalf;
63 srcPt.fY -= SK_ScalarHalf; 90 srcPt.fY -= SK_ScalarHalf;
64 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); 91 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
65 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); 92 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
66 93
67 SkFixed coeffX[4], coeffY[4]; 94 SkFixed coeffX[4], coeffY[4];
68 build_coeff4(coeffX, coefficients, fractx); 95 build_coeff4(coeffX, fractx);
69 build_coeff4(coeffY, coefficients, fracty); 96 build_coeff4(coeffY, fracty);
70 97
71 int sx = SkScalarFloorToInt(srcPt.fX); 98 int sx = SkScalarFloorToInt(srcPt.fX);
72 int sy = SkScalarFloorToInt(srcPt.fY); 99 int sy = SkScalarFloorToInt(srcPt.fY);
73 100
74 // Here is where we can support other tile modes (e.g. repeat or mirror) 101 // Here is where we can support other tile modes (e.g. repeat or mirror)
75 int x0 = SkClampMax(sx - 1, maxX); 102 int x0 = SkClampMax(sx - 1, maxX);
76 int x1 = SkClampMax(sx , maxX); 103 int x1 = SkClampMax(sx , maxX);
77 int x2 = SkClampMax(sx + 1, maxX); 104 int x2 = SkClampMax(sx + 1, maxX);
78 int x3 = SkClampMax(sx + 2, maxX); 105 int x3 = SkClampMax(sx + 2, maxX);
79 int y0 = SkClampMax(sy - 1, maxY); 106 int y0 = SkClampMax(sy - 1, maxY);
80 int y1 = SkClampMax(sy , maxY); 107 int y1 = SkClampMax(sy , maxY);
81 int y2 = SkClampMax(sy + 1, maxY); 108 int y2 = SkClampMax(sy + 1, maxY);
82 int y3 = SkClampMax(sy + 2, maxY); 109 int y3 = SkClampMax(sy + 2, maxY);
110
111 *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 );
83 112
84 SkPMColor s00 = *s.fBitmap->getAddr32(x0, y0);
85 SkPMColor s10 = *s.fBitmap->getAddr32(x1, y0);
86 SkPMColor s20 = *s.fBitmap->getAddr32(x2, y0);
87 SkPMColor s30 = *s.fBitmap->getAddr32(x3, y0);
88 SkPMColor s0 = cubicBlend(coeffX, s00, s10, s20, s30);
89 SkPMColor s01 = *s.fBitmap->getAddr32(x0, y1);
90 SkPMColor s11 = *s.fBitmap->getAddr32(x1, y1);
91 SkPMColor s21 = *s.fBitmap->getAddr32(x2, y1);
92 SkPMColor s31 = *s.fBitmap->getAddr32(x3, y1);
93 SkPMColor s1 = cubicBlend(coeffX, s01, s11, s21, s31);
94 SkPMColor s02 = *s.fBitmap->getAddr32(x0, y2);
95 SkPMColor s12 = *s.fBitmap->getAddr32(x1, y2);
96 SkPMColor s22 = *s.fBitmap->getAddr32(x2, y2);
97 SkPMColor s32 = *s.fBitmap->getAddr32(x3, y2);
98 SkPMColor s2 = cubicBlend(coeffX, s02, s12, s22, s32);
99 SkPMColor s03 = *s.fBitmap->getAddr32(x0, y3);
100 SkPMColor s13 = *s.fBitmap->getAddr32(x1, y3);
101 SkPMColor s23 = *s.fBitmap->getAddr32(x2, y3);
102 SkPMColor s33 = *s.fBitmap->getAddr32(x3, y3);
103 SkPMColor s3 = cubicBlend(coeffX, s03, s13, s23, s33);
104 *colors++ = cubicBlend(coeffY, s0, s1, s2, s3);
105 x++; 113 x++;
106 } 114 }
107 } 115 }
116
117 static void bicubicFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y,
118 SkPMColor *SK_RESTRICT colors, int count) {
119 const int maxX = s.fBitmap->width() - 1;
120 const int maxY = s.fBitmap->height() - 1;
121
122 SkPoint srcPt;
123 s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt);
124 srcPt.fY -= SK_ScalarHalf;
125 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
126 SkFixed coeffX[4], coeffY[4];
127 build_coeff4(coeffY, fracty);
128 int sy = SkScalarFloorToInt(srcPt.fY);
129 int y0 = SkClampMax(sy - 1, maxY);
130 int y1 = SkClampMax(sy , maxY);
131 int y2 = SkClampMax(sy + 1, maxY);
132 int y3 = SkClampMax(sy + 2, maxY);
133
134 while (count-- > 0) {
135 s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt);
136 srcPt.fX -= SK_ScalarHalf;
137 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
138
139 build_coeff4(coeffX, fractx);
140
141 int sx = SkScalarFloorToInt(srcPt.fX);
142
143 // Here is where we can support other tile modes (e.g. repeat or mirror)
144 int x0 = SkClampMax(sx - 1, maxX);
145 int x1 = SkClampMax(sx , maxX);
146 int x2 = SkClampMax(sx + 1, maxX);
147 int x3 = SkClampMax(sx + 2, maxX);
148
149 *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 );
150
151 x++;
152 }
153 }
108 154
109 SkBitmapProcState::ShaderProc32 155 SkBitmapProcState::ShaderProc32
110 SkBitmapProcState::chooseBicubicFilterProc(const SkPaint& paint) { 156 SkBitmapProcState::chooseBicubicFilterProc(const SkPaint& paint) {
111 // we need to be requested 157 // we need to be requested
112 uint32_t mask = SkPaint::kFilterBitmap_Flag 158 uint32_t mask = SkPaint::kFilterBitmap_Flag
113 | SkPaint::kBicubicFilterBitmap_Flag 159 | SkPaint::kBicubicFilterBitmap_Flag
114 ; 160 ;
115 if ((paint.getFlags() & mask) != mask) { 161 if ((paint.getFlags() & mask) != mask) {
116 return NULL; 162 return NULL;
117 } 163 }
118 164
119 // TODO: consider supporting other configs (e.g. 565, A8) 165 // TODO: consider supporting other configs (e.g. 565, A8)
120 if (fBitmap->config() != SkBitmap::kARGB_8888_Config) { 166 if (fBitmap->config() != SkBitmap::kARGB_8888_Config) {
121 return NULL; 167 return NULL;
122 } 168 }
123 169
124 // TODO: consider supporting repeat and mirror 170 // TODO: consider supporting repeat and mirror
125 if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) { 171 if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) {
126 return NULL; 172 return NULL;
127 } 173 }
128 174
129 // TODO: support blending inside our procs 175 // TODO: support blending inside our procs
130 if (0xFF != paint.getAlpha()) { 176 if (0xFF != paint.getAlpha()) {
131 return NULL; 177 return NULL;
132 } 178 }
133 179
134 return bicubicFilter; 180 if (fInvType & SkMatrix::kAffine_Mask) {
181 return bicubicFilter;
182 } else if (fInvType & SkMatrix::kScale_Mask) {
183 return bicubicFilter_ScaleOnly;
184 } else {
185 return NULL;
186 }
135 } 187 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698