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

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

Issue 17381008: More general version of image filtering; reworked to be robust and easier to SSE (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: make temp. scale function private; disable GM and bench for that function Created 7 years, 5 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
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBitmapProcState.h"
9 #include "SkBitmap.h"
10 #include "SkColor.h"
11 #include "SkColorPriv.h"
12 #include "SkUnPreMultiply.h"
13 #include "SkRTConf.h"
14 #include "SkShader.h"
15
16 #define DS(x) SkDoubleToScalar(x)
17
18 #define MUL(a, b) ((a) * (b))
19
20 static inline SkPMColor cubicBlend(const SkFixed cc[4], SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) {
21 SkFixed fa = MUL(cc[0], SkGetPackedA32(c0)) + MUL(cc[1], SkGetPackedA32(c1)) + MUL(cc[2], SkGetPackedA32(c2)) + MUL(cc[3], SkGetPackedA32(c3));
22 SkFixed fr = MUL(cc[0], SkGetPackedR32(c0)) + MUL(cc[1], SkGetPackedR32(c1)) + MUL(cc[2], SkGetPackedR32(c2)) + MUL(cc[3], SkGetPackedR32(c3));
23 SkFixed fg = MUL(cc[0], SkGetPackedG32(c0)) + MUL(cc[1], SkGetPackedG32(c1)) + MUL(cc[2], SkGetPackedG32(c2)) + MUL(cc[3], SkGetPackedG32(c3));
24 SkFixed fb = MUL(cc[0], SkGetPackedB32(c0)) + MUL(cc[1], SkGetPackedB32(c1)) + MUL(cc[2], SkGetPackedB32(c2)) + MUL(cc[3], SkGetPackedB32(c3));
25
26 int a = SkClampMax(SkFixedRoundToInt(fa), 255);
27 int r = SkClampMax(SkFixedRoundToInt(fr), a);
28 int g = SkClampMax(SkFixedRoundToInt(fg), a);
29 int b = SkClampMax(SkFixedRoundToInt(fb), a);
30
31 return SkPackARGB32(a, r, g, b);
32 }
33
34 static float poly_eval(const float cc[4], float t) {
35 return cc[0] + t * (cc[1] + t * (cc[2] + t * cc[3]));
36 }
37
38 static void build_coeff4(SkFixed dst[4], float t) {
39 static const SkScalar coefficients[16] = {
40 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
41 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.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),
43 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
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) {
81
82 const int maxX = s.fBitmap->width() - 1;
83 const int maxY = s.fBitmap->height() - 1;
84
85 while (count-- > 0) {
86 SkPoint srcPt;
87 s.fInvProc(*s.fInvMatrix, SkIntToScalar(x),
88 SkIntToScalar(y), &srcPt);
89 srcPt.fX -= SK_ScalarHalf;
90 srcPt.fY -= SK_ScalarHalf;
91 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
92 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
93
94 SkFixed coeffX[4], coeffY[4];
95 build_coeff4(coeffX, fractx);
96 build_coeff4(coeffY, fracty);
97
98 int sx = SkScalarFloorToInt(srcPt.fX);
99 int sy = SkScalarFloorToInt(srcPt.fY);
100
101 // Here is where we can support other tile modes (e.g. repeat or mirror)
102 int x0 = SkClampMax(sx - 1, maxX);
103 int x1 = SkClampMax(sx , maxX);
104 int x2 = SkClampMax(sx + 1, maxX);
105 int x3 = SkClampMax(sx + 2, maxX);
106 int y0 = SkClampMax(sy - 1, maxY);
107 int y1 = SkClampMax(sy , maxY);
108 int y2 = SkClampMax(sy + 1, maxY);
109 int y3 = SkClampMax(sy + 2, maxY);
110
111 *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 );
112
113 x++;
114 }
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 }
154
155 SkBitmapProcState::ShaderProc32
156 SkBitmapProcState::chooseBicubicFilterProc(const SkPaint& paint) {
157 // we need to be requested
158 uint32_t mask = SkPaint::kFilterBitmap_Flag
159 | SkPaint::kBicubicFilterBitmap_Flag
160 ;
161 if ((paint.getFlags() & mask) != mask) {
162 return NULL;
163 }
164
165 // TODO: consider supporting other configs (e.g. 565, A8)
166 if (fBitmap->config() != SkBitmap::kARGB_8888_Config) {
167 return NULL;
168 }
169
170 // TODO: consider supporting repeat and mirror
171 if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) {
172 return NULL;
173 }
174
175 // TODO: support blending inside our procs
176 if (0xFF != paint.getAlpha()) {
177 return NULL;
178 }
179
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 }
187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698