senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkUnPreMultiply.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 18 | #include "effects/GrBicubicEffect.h" |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
| 20 | #include "GrTexture.h" |
| 21 | #endif |
| 22 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 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 | |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 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 | |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 38 | SkBicubicImageFilter* SkBicubicImageFilter::CreateMitchell(const SkSize& scale, |
| 39 | SkImageFilter* input) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 40 | return SkNEW_ARGS(SkBicubicImageFilter, (scale, gMitchellCoefficients, input)); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 41 | } |
| 42 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 43 | SkBicubicImageFilter::SkBicubicImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 44 | : INHERITED(1, buffer) { |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 45 | SkDEBUGCODE(bool success =) buffer.readScalarArray(fCoefficients, 16); |
| 46 | SkASSERT(success); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 47 | fScale.fWidth = buffer.readScalar(); |
| 48 | fScale.fHeight = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 49 | buffer.validate(SkScalarIsFinite(fScale.fWidth) && |
| 50 | SkScalarIsFinite(fScale.fHeight) && |
| 51 | (fScale.fWidth >= 0) && |
| 52 | (fScale.fHeight >= 0)); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 53 | } |
| 54 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 55 | void SkBicubicImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 56 | this->INHERITED::flatten(buffer); |
| 57 | buffer.writeScalarArray(fCoefficients, 16); |
| 58 | buffer.writeScalar(fScale.fWidth); |
| 59 | buffer.writeScalar(fScale.fHeight); |
| 60 | } |
| 61 | |
| 62 | SkBicubicImageFilter::~SkBicubicImageFilter() { |
| 63 | } |
| 64 | |
| 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); |
robertphillips@google.com | a041b95 | 2013-03-12 13:13:07 +0000 | [diff] [blame] | 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); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 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)); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 77 | return SkPackARGB32(SkScalarRoundToInt(a), |
| 78 | SkScalarRoundToInt(SkScalarClampMax(r, a)), |
| 79 | SkScalarRoundToInt(SkScalarClampMax(g, a)), |
robertphillips@google.com | a041b95 | 2013-03-12 13:13:07 +0000 | [diff] [blame] | 80 | SkScalarRoundToInt(SkScalarClampMax(b, a))); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool SkBicubicImageFilter::onFilterImage(Proxy* proxy, |
| 84 | const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 85 | const Context& ctx, |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 86 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 87 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 88 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 89 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 90 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 94 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 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); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 107 | if (dstIRect.isEmpty()) { |
| 108 | return false; |
| 109 | } |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 110 | result->setConfig(src.config(), dstIRect.width(), dstIRect.height()); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 111 | if (!result->allocPixels()) { |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
| 115 | SkRect srcRect; |
| 116 | src.getBounds(&srcRect); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 117 | srcRect.offset(SkPoint::Make(SkIntToScalar(srcOffset.fX), SkIntToScalar(srcOffset.fY))); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 118 | SkMatrix inverse; |
| 119 | inverse.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 120 | inverse.postTranslate(-0.5f, -0.5f); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 121 | |
| 122 | for (int y = dstIRect.fTop; y < dstIRect.fBottom; ++y) { |
| 123 | SkPMColor* dptr = result->getAddr32(dstIRect.fLeft, y); |
| 124 | for (int x = dstIRect.fLeft; x < dstIRect.fRight; ++x) { |
| 125 | SkPoint srcPt, dstPt = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 126 | inverse.mapPoints(&srcPt, &dstPt, 1); |
| 127 | SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); |
| 128 | SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); |
| 129 | int sx = SkScalarFloorToInt(srcPt.fX); |
| 130 | int sy = SkScalarFloorToInt(srcPt.fY); |
| 131 | int x0 = SkClampMax(sx - 1, src.width() - 1); |
| 132 | int x1 = SkClampMax(sx , src.width() - 1); |
| 133 | int x2 = SkClampMax(sx + 1, src.width() - 1); |
| 134 | int x3 = SkClampMax(sx + 2, src.width() - 1); |
| 135 | int y0 = SkClampMax(sy - 1, src.height() - 1); |
| 136 | int y1 = SkClampMax(sy , src.height() - 1); |
| 137 | int y2 = SkClampMax(sy + 1, src.height() - 1); |
| 138 | int y3 = SkClampMax(sy + 2, src.height() - 1); |
| 139 | SkPMColor s00 = *src.getAddr32(x0, y0); |
| 140 | SkPMColor s10 = *src.getAddr32(x1, y0); |
| 141 | SkPMColor s20 = *src.getAddr32(x2, y0); |
| 142 | SkPMColor s30 = *src.getAddr32(x3, y0); |
| 143 | SkPMColor s0 = cubicBlend(fCoefficients, fractx, s00, s10, s20, s30); |
| 144 | SkPMColor s01 = *src.getAddr32(x0, y1); |
| 145 | SkPMColor s11 = *src.getAddr32(x1, y1); |
| 146 | SkPMColor s21 = *src.getAddr32(x2, y1); |
| 147 | SkPMColor s31 = *src.getAddr32(x3, y1); |
| 148 | SkPMColor s1 = cubicBlend(fCoefficients, fractx, s01, s11, s21, s31); |
| 149 | SkPMColor s02 = *src.getAddr32(x0, y2); |
| 150 | SkPMColor s12 = *src.getAddr32(x1, y2); |
| 151 | SkPMColor s22 = *src.getAddr32(x2, y2); |
| 152 | SkPMColor s32 = *src.getAddr32(x3, y2); |
| 153 | SkPMColor s2 = cubicBlend(fCoefficients, fractx, s02, s12, s22, s32); |
| 154 | SkPMColor s03 = *src.getAddr32(x0, y3); |
| 155 | SkPMColor s13 = *src.getAddr32(x1, y3); |
| 156 | SkPMColor s23 = *src.getAddr32(x2, y3); |
| 157 | SkPMColor s33 = *src.getAddr32(x3, y3); |
| 158 | SkPMColor s3 = cubicBlend(fCoefficients, fractx, s03, s13, s23, s33); |
| 159 | *dptr++ = cubicBlend(fCoefficients, fracty, s0, s1, s2, s3); |
| 160 | } |
| 161 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 162 | offset->fX = dstIRect.fLeft; |
| 163 | offset->fY = dstIRect.fTop; |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /////////////////////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 170 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 171 | bool SkBicubicImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 172 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 173 | SkBitmap srcBM = src; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 174 | if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &srcBM, offset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 177 | GrTexture* srcTexture = srcBM.getTexture(); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 178 | GrContext* context = srcTexture->getContext(); |
| 179 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 180 | SkRect dstRect = SkRect::MakeWH(srcBM.width() * fScale.fWidth, |
| 181 | srcBM.height() * fScale.fHeight); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 182 | |
| 183 | GrTextureDesc desc; |
| 184 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 185 | desc.fWidth = SkScalarCeilToInt(dstRect.width()); |
| 186 | desc.fHeight = SkScalarCeilToInt(dstRect.height()); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 187 | desc.fConfig = kSkia8888_GrPixelConfig; |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 188 | |
| 189 | GrAutoScratchTexture ast(context, desc); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 190 | SkAutoTUnref<GrTexture> dst(ast.detach()); |
| 191 | if (!dst) { |
| 192 | return false; |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 193 | } |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 194 | GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 195 | GrPaint paint; |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 196 | paint.addColorEffect(GrBicubicEffect::Create(srcTexture, fCoefficients))->unref(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 197 | SkRect srcRect; |
| 198 | srcBM.getBounds(&srcRect); |
| 199 | context->drawRectToRect(paint, dstRect, srcRect); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 200 | WrapTexture(dst, desc.fWidth, desc.fHeight, result); |
| 201 | return true; |
senorblanco@chromium.org | 3f2d45a | 2013-01-18 20:48:20 +0000 | [diff] [blame] | 202 | } |
| 203 | #endif |
| 204 | |
| 205 | /////////////////////////////////////////////////////////////////////////////// |