1 /*
2 * Copyright 2013 The Android Open Source Project
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 "SkBicubicImageFilter.h"
9 #include "SkBitmap.h"
10 #include "SkColorPriv.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkMatrix.h"
14 #include "SkRect.h"
15 #include "SkUnPreMultiply.h"
16
17 #if SK_SUPPORT_GPU
18 #include "effects/GrBicubicEffect.h"
19 #include "GrContext.h"
20 #include "GrTexture.h"
21 #endif
22
23 #define DS(x) SkDoubleToScalar(x)
24
25 static const SkScalar gMitchellCoefficients[16] = {
26 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
27 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
28 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
29 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
30 };
31
SkBicubicImageFilter(const SkSize & scale,const SkScalar coefficients[16],SkImageFilter * input)32 SkBicubicImageFilter::SkBicubicImageFilter(const SkSize& scale, const SkScalar coefficients[16], SkImageFilter* input)
33 : INHERITED(input),
34 fScale(scale) {
35 memcpy(fCoefficients, coefficients, sizeof(fCoefficients));
36 }
37
CreateMitchell(const SkSize & scale,SkImageFilter * input)38 SkBicubicImageFilter* SkBicubicImageFilter::CreateMitchell(const SkSize& scale,
39 SkImageFilter* input) {
40 return SkNEW_ARGS(SkBicubicImageFilter, (scale, gMitchellCoefficients, input));
41 }
42
SkBicubicImageFilter(SkReadBuffer & buffer)43 SkBicubicImageFilter::SkBicubicImageFilter(SkReadBuffer& buffer)
44 : INHERITED(1, buffer) {
45 SkDEBUGCODE(bool success =) buffer.readScalarArray(fCoefficients, 16);
46 SkASSERT(success);
47 fScale.fWidth = buffer.readScalar();
48 fScale.fHeight = buffer.readScalar();
49 buffer.validate(SkScalarIsFinite(fScale.fWidth) &&
50 SkScalarIsFinite(fScale.fHeight) &&
51 (fScale.fWidth >= 0) &&
52 (fScale.fHeight >= 0));
53 }
54
flatten(SkWriteBuffer & buffer) const55 void SkBicubicImageFilter::flatten(SkWriteBuffer& buffer) const {
56 this->INHERITED::flatten(buffer);
57 buffer.writeScalarArray(fCoefficients, 16);
58 buffer.writeScalar(fScale.fWidth);
59 buffer.writeScalar(fScale.fHeight);
60 }
61
~SkBicubicImageFilter()62 SkBicubicImageFilter::~SkBicubicImageFilter() {
63 }
64
cubicBlend(const SkScalar c[16],SkScalar t,SkPMColor c0,SkPMColor c1,SkPMColor c2,SkPMColor c3)65 inline SkPMColor cubicBlend(const SkScalar c[16], SkScalar t, SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) {
66 SkScalar t2 = t * t, t3 = t2 * t;
67 SkScalar cc[4];
68 // FIXME: For the fractx case, this should be refactored out of this function.
69 cc[0] = c[0] + SkScalarMul(c[1], t) + SkScalarMul(c[2], t2) + SkScalarMul(c[3], t3);
70 cc[1] = c[4] + SkScalarMul(c[5], t) + SkScalarMul(c[6], t2) + SkScalarMul(c[7], t3);
71 cc[2] = c[8] + SkScalarMul(c[9], t) + SkScalarMul(c[10], t2) + SkScalarMul(c[11], t3);
72 cc[3] = c[12] + SkScalarMul(c[13], t) + SkScalarMul(c[14], t2) + SkScalarMul(c[15], t3);
73 SkScalar a = SkScalarClampMax(SkScalarMul(cc[0], SkGetPackedA32(c0)) + SkScalarMul(cc[1], SkGetPackedA32(c1)) + SkScalarMul(cc[2], SkGetPackedA32(c2)) + SkScalarMul(cc[3], SkGetPackedA32(c3)), 255);
74 SkScalar r = SkScalarMul(cc[0], SkGetPackedR32(c0)) + SkScalarMul(cc[1], SkGetPackedR32(c1)) + SkScalarMul(cc[2], SkGetPackedR32(c2)) + SkScalarMul(cc[3], SkGetPackedR32(c3));
75 SkScalar g = SkScalarMul(cc[0], SkGetPackedG32(c0)) + SkScalarMul(cc[1], SkGetPackedG32(c1)) + SkScalarMul(cc[2], SkGetPackedG32(c2)) + SkScalarMul(cc[3], SkGetPackedG32(c3));
76 SkScalar b = SkScalarMul(cc[0], SkGetPackedB32(c0)) + SkScalarMul(cc[1], SkGetPackedB32(c1)) + SkScalarMul(cc[2], SkGetPackedB32(c2)) + SkScalarMul(cc[3], SkGetPackedB32(c3));
77 return SkPackARGB32(SkScalarRoundToInt(a),
78 SkScalarRoundToInt(SkScalarClampMax(r, a)),
79 SkScalarRoundToInt(SkScalarClampMax(g, a)),
80 SkScalarRoundToInt(SkScalarClampMax(b, a)));
81 }
82
onFilterImage(Proxy * proxy,const SkBitmap & source,const Context & ctx,SkBitmap * result,SkIPoint * offset) const83 bool SkBicubicImageFilter::onFilterImage(Proxy* proxy,
84 const SkBitmap& source,
85 const Context& ctx,
86 SkBitmap* result,
87 SkIPoint* offset) const {
88 SkBitmap src = source;
89 SkIPoint srcOffset = SkIPoint::Make(0, 0);
90 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) {
91 return false;
92 }
93
94 if (src.colorType() != kN32_SkColorType) {
95 return false;
96 }
97
98 SkAutoLockPixels alp(src);
99 if (!src.getPixels()) {
100 return false;
101 }
102
103 SkRect dstRect = SkRect::MakeWH(SkScalarMul(SkIntToScalar(src.width()), fScale.fWidth),
104 SkScalarMul(SkIntToScalar(src.height()), fScale.fHeight));
105 SkIRect dstIRect;
106 dstRect.roundOut(&dstIRect);
107 if (dstIRect.isEmpty()) {
108 return false;
109 }
110 if (!result->allocPixels(src.info().makeWH(dstIRect.width(), dstIRect.height()))) {
111 return false;
112 }
113
114 SkRect srcRect;
115 src.getBounds(&srcRect);
116 srcRect.offset(SkPoint::Make(SkIntToScalar(srcOffset.fX), SkIntToScalar(srcOffset.fY)));
117 SkMatrix inverse;
118 inverse.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit);
119 inverse.postTranslate(-0.5f, -0.5f);
120
121 for (int y = dstIRect.fTop; y < dstIRect.fBottom; ++y) {
122 SkPMColor* dptr = result->getAddr32(dstIRect.fLeft, y);
123 for (int x = dstIRect.fLeft; x < dstIRect.fRight; ++x) {
124 SkPoint srcPt, dstPt = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
125 inverse.mapPoints(&srcPt, &dstPt, 1);
126 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX);
127 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY);
128 int sx = SkScalarFloorToInt(srcPt.fX);
129 int sy = SkScalarFloorToInt(srcPt.fY);
130 int x0 = SkClampMax(sx - 1, src.width() - 1);
131 int x1 = SkClampMax(sx , src.width() - 1);
132 int x2 = SkClampMax(sx + 1, src.width() - 1);
133 int x3 = SkClampMax(sx + 2, src.width() - 1);
134 int y0 = SkClampMax(sy - 1, src.height() - 1);
135 int y1 = SkClampMax(sy , src.height() - 1);
136 int y2 = SkClampMax(sy + 1, src.height() - 1);
137 int y3 = SkClampMax(sy + 2, src.height() - 1);
138 SkPMColor s00 = *src.getAddr32(x0, y0);
139 SkPMColor s10 = *src.getAddr32(x1, y0);
140 SkPMColor s20 = *src.getAddr32(x2, y0);
141 SkPMColor s30 = *src.getAddr32(x3, y0);
142 SkPMColor s0 = cubicBlend(fCoefficients, fractx, s00, s10, s20, s30);
143 SkPMColor s01 = *src.getAddr32(x0, y1);
144 SkPMColor s11 = *src.getAddr32(x1, y1);
145 SkPMColor s21 = *src.getAddr32(x2, y1);
146 SkPMColor s31 = *src.getAddr32(x3, y1);
147 SkPMColor s1 = cubicBlend(fCoefficients, fractx, s01, s11, s21, s31);
148 SkPMColor s02 = *src.getAddr32(x0, y2);
149 SkPMColor s12 = *src.getAddr32(x1, y2);
150 SkPMColor s22 = *src.getAddr32(x2, y2);
151 SkPMColor s32 = *src.getAddr32(x3, y2);
152 SkPMColor s2 = cubicBlend(fCoefficients, fractx, s02, s12, s22, s32);
153 SkPMColor s03 = *src.getAddr32(x0, y3);
154 SkPMColor s13 = *src.getAddr32(x1, y3);
155 SkPMColor s23 = *src.getAddr32(x2, y3);
156 SkPMColor s33 = *src.getAddr32(x3, y3);
157 SkPMColor s3 = cubicBlend(fCoefficients, fractx, s03, s13, s23, s33);
158 *dptr++ = cubicBlend(fCoefficients, fracty, s0, s1, s2, s3);
159 }
160 }
161 offset->fX = dstIRect.fLeft;
162 offset->fY = dstIRect.fTop;
163 return true;
164 }
165
166 ///////////////////////////////////////////////////////////////////////////////
167
168 #if SK_SUPPORT_GPU
169
filterImageGPU(Proxy * proxy,const SkBitmap & src,const Context & ctx,SkBitmap * result,SkIPoint * offset) const170 bool SkBicubicImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
171 SkBitmap* result, SkIPoint* offset) const {
172 SkBitmap srcBM = src;
173 if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &srcBM, offset)) {
174 return false;
175 }
176 GrTexture* srcTexture = srcBM.getTexture();
177 GrContext* context = srcTexture->getContext();
178
179 SkRect dstRect = SkRect::MakeWH(srcBM.width() * fScale.fWidth,
180 srcBM.height() * fScale.fHeight);
181
182 GrTextureDesc desc;
183 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
184 desc.fWidth = SkScalarCeilToInt(dstRect.width());
185 desc.fHeight = SkScalarCeilToInt(dstRect.height());
186 desc.fConfig = kSkia8888_GrPixelConfig;
187
188 GrAutoScratchTexture ast(context, desc);
189 SkAutoTUnref<GrTexture> dst(ast.detach());
190 if (!dst) {
191 return false;
192 }
193 GrContext::AutoRenderTarget art(context, dst->asRenderTarget());
194 GrPaint paint;
195 paint.addColorEffect(GrBicubicEffect::Create(srcTexture, fCoefficients))->unref();
196 SkRect srcRect;
197 srcBM.getBounds(&srcRect);
198 context->drawRectToRect(paint, dstRect, srcRect);
199 WrapTexture(dst, desc.fWidth, desc.fHeight, result);
200 return true;
201 }
202 #endif
203
204 ///////////////////////////////////////////////////////////////////////////////
205