humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 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 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 8 | #include "SkErrorInternals.h" |
| 9 | #include "SkConvolver.h" |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 10 | #include "SkBitmapProcState.h" |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkColor.h" |
| 13 | #include "SkColorPriv.h" |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 14 | #include "SkConvolver.h" |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 15 | #include "SkUnPreMultiply.h" |
| 16 | #include "SkShader.h" |
| 17 | #include "SkRTConf.h" |
| 18 | #include "SkMath.h" |
| 19 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 20 | // These are the per-scanline callbacks that are used when we must resort to |
| 21 | // resampling an image as it is blitted. Typically these are used only when |
| 22 | // the image is rotated or has some other complex transformation applied. |
| 23 | // Scaled images will usually be rescaled directly before rasterization. |
| 24 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 25 | namespace { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 26 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 27 | template <typename Color, typename ColorPacker> |
| 28 | void highQualityFilter(ColorPacker pack, const SkBitmapProcState& s, int x, int y, Color* SK_RESTRICT colors, int count) { |
commit-bot@chromium.org | 7af78e0 | 2013-12-18 19:47:57 +0000 | [diff] [blame] | 29 | const int maxX = s.fBitmap->width(); |
| 30 | const int maxY = s.fBitmap->height(); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 31 | |
| 32 | while (count-- > 0) { |
| 33 | SkPoint srcPt; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 34 | s.fInvProc(s.fInvMatrix, x + 0.5f, |
| 35 | y + 0.5f, &srcPt); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 36 | srcPt.fX -= SK_ScalarHalf; |
| 37 | srcPt.fY -= SK_ScalarHalf; |
| 38 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 39 | SkScalar weight = 0; |
| 40 | SkScalar fr = 0, fg = 0, fb = 0, fa = 0; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 41 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 42 | int y0 = SkClampMax(SkScalarCeilToInt(srcPt.fY-s.getBitmapFilter()->width()), maxY); |
commit-bot@chromium.org | 785f2e1 | 2013-12-11 15:35:41 +0000 | [diff] [blame] | 43 | int y1 = SkClampMax(SkScalarFloorToInt(srcPt.fY+s.getBitmapFilter()->width()+1), maxY); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 44 | int x0 = SkClampMax(SkScalarCeilToInt(srcPt.fX-s.getBitmapFilter()->width()), maxX); |
commit-bot@chromium.org | 785f2e1 | 2013-12-11 15:35:41 +0000 | [diff] [blame] | 45 | int x1 = SkClampMax(SkScalarFloorToInt(srcPt.fX+s.getBitmapFilter()->width())+1, maxX); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | 785f2e1 | 2013-12-11 15:35:41 +0000 | [diff] [blame] | 47 | for (int srcY = y0; srcY < y1; srcY++) { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 48 | SkScalar yWeight = s.getBitmapFilter()->lookupScalar((srcPt.fY - srcY)); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 785f2e1 | 2013-12-11 15:35:41 +0000 | [diff] [blame] | 50 | for (int srcX = x0; srcX < x1 ; srcX++) { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 51 | SkScalar xWeight = s.getBitmapFilter()->lookupScalar((srcPt.fX - srcX)); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 52 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 53 | SkScalar combined_weight = SkScalarMul(xWeight, yWeight); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 54 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 55 | SkPMColor c = *s.fBitmap->getAddr32(srcX, srcY); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 56 | fr += combined_weight * SkGetPackedR32(c); |
| 57 | fg += combined_weight * SkGetPackedG32(c); |
| 58 | fb += combined_weight * SkGetPackedB32(c); |
| 59 | fa += combined_weight * SkGetPackedA32(c); |
| 60 | weight += combined_weight; |
| 61 | } |
| 62 | } |
| 63 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 64 | fr = SkScalarDiv(fr, weight); |
| 65 | fg = SkScalarDiv(fg, weight); |
| 66 | fb = SkScalarDiv(fb, weight); |
| 67 | fa = SkScalarDiv(fa, weight); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 68 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 69 | int a = SkClampMax(SkScalarRoundToInt(fa), 255); |
| 70 | int r = SkClampMax(SkScalarRoundToInt(fr), a); |
| 71 | int g = SkClampMax(SkScalarRoundToInt(fg), a); |
| 72 | int b = SkClampMax(SkScalarRoundToInt(fb), a); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 73 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 74 | *colors++ = pack(a, r, g, b); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 75 | |
| 76 | x++; |
| 77 | } |
| 78 | } |
| 79 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 80 | uint16_t PackTo565(int /*a*/, int r, int g, int b) { |
| 81 | return SkPack888ToRGB16(r, g, b); |
| 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | void highQualityFilter32(const SkBitmapProcState& s, int x, int y, SkPMColor* SK_RESTRICT colors, int count) { |
| 87 | highQualityFilter(&SkPackARGB32, s, x, y, colors, count); |
| 88 | } |
| 89 | |
| 90 | void highQualityFilter16(const SkBitmapProcState& s, int x, int y, uint16_t* SK_RESTRICT colors, int count) { |
| 91 | highQualityFilter(&PackTo565, s, x, y, colors, count); |
| 92 | } |
| 93 | |
| 94 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 95 | SK_CONF_DECLARE(const char *, c_bitmapFilter, "bitmap.filter", "mitchell", "Which scanline bitmap filter to use [mitchell, lanczos, hamming, gaussian, triangle, box]"); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 96 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 97 | SkBitmapFilter *SkBitmapFilter::Allocate() { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 98 | if (!strcmp(c_bitmapFilter, "mitchell")) { |
| 99 | return SkNEW_ARGS(SkMitchellFilter,(1.f/3.f,1.f/3.f)); |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 100 | } else if (!strcmp(c_bitmapFilter, "lanczos")) { |
| 101 | return SkNEW(SkLanczosFilter); |
| 102 | } else if (!strcmp(c_bitmapFilter, "hamming")) { |
| 103 | return SkNEW(SkHammingFilter); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 104 | } else if (!strcmp(c_bitmapFilter, "gaussian")) { |
| 105 | return SkNEW_ARGS(SkGaussianFilter,(2)); |
| 106 | } else if (!strcmp(c_bitmapFilter, "triangle")) { |
| 107 | return SkNEW(SkTriangleFilter); |
| 108 | } else if (!strcmp(c_bitmapFilter, "box")) { |
| 109 | return SkNEW(SkBoxFilter); |
| 110 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 111 | SkDEBUGFAIL("Unknown filter type"); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 112 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 113 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
| 116 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 117 | bool SkBitmapProcState::setBitmapFilterProcs() { |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 118 | if (fFilterLevel != SkPaint::kHigh_FilterLevel) { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 119 | return false; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (fAlphaScale != 256) { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 123 | return false; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 124 | } |
| 125 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 126 | // TODO: consider supporting other colortypes (e.g. 565, A8) |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 127 | if (fBitmap->colorType() != kN32_SkColorType) { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 128 | return false; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // TODO: consider supporting repeat and mirror |
| 132 | if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 133 | return false; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 136 | // TODO: is this right? do we want fBitmapFilter allocated even if we can't set shader procs? |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 137 | if (fInvType & (SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask)) { |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 138 | fBitmapFilter = SkBitmapFilter::Allocate(); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 139 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 140 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 141 | if (fInvType & SkMatrix::kScale_Mask) { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 142 | fShaderProc32 = highQualityFilter32; |
| 143 | fShaderProc16 = highQualityFilter16; |
| 144 | return true; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 145 | } else { |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 146 | return false; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 147 | } |
| 148 | } |