bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 1 | /* |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 9 | #include "SkBlendMode.h" |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 11 | #include "SkColorData.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkFloatingPoint.h" |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 14 | #include "SkImage.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 15 | #include "SkMatrix.h" |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 16 | #include "SkPM4f.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 17 | #include "SkPaint.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "SkPixelRef.h" |
| 20 | #include "SkPixmap.h" |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 21 | #include "SkPoint3.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 22 | #include "SkRRect.h" |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 23 | #include "SkShader.h" |
| 24 | #include "SkSurface.h" |
Ben Wagner | 3560b58 | 2018-03-27 18:15:53 +0000 | [diff] [blame] | 25 | #include "SkTextBlob.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 26 | #include "sk_tool_utils.h" |
| 27 | |
| 28 | #include <cmath> |
| 29 | #include <cstring> |
| 30 | #include <memory> |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 32 | namespace sk_tool_utils { |
| 33 | |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 34 | const char* alphatype_name(SkAlphaType at) { |
| 35 | switch (at) { |
| 36 | case kUnknown_SkAlphaType: return "Unknown"; |
| 37 | case kOpaque_SkAlphaType: return "Opaque"; |
| 38 | case kPremul_SkAlphaType: return "Premul"; |
| 39 | case kUnpremul_SkAlphaType: return "Unpremul"; |
| 40 | } |
| 41 | SkASSERT(false); |
| 42 | return "unexpected alphatype"; |
| 43 | } |
| 44 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 45 | const char* colortype_name(SkColorType ct) { |
| 46 | switch (ct) { |
| 47 | case kUnknown_SkColorType: return "Unknown"; |
| 48 | case kAlpha_8_SkColorType: return "Alpha_8"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 49 | case kRGB_565_SkColorType: return "RGB_565"; |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 50 | case kARGB_4444_SkColorType: return "ARGB_4444"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 51 | case kRGBA_8888_SkColorType: return "RGBA_8888"; |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 52 | case kRGB_888x_SkColorType: return "RGB_888x"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 53 | case kBGRA_8888_SkColorType: return "BGRA_8888"; |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 54 | case kRGBA_1010102_SkColorType: return "RGBA_1010102"; |
| 55 | case kRGB_101010x_SkColorType: return "RGB_101010x"; |
| 56 | case kGray_8_SkColorType: return "Gray_8"; |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 57 | case kRGBA_F16_SkColorType: return "RGBA_F16"; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 58 | case kRGBA_F32_SkColorType: return "RGBA_F32"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 59 | } |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 60 | SkASSERT(false); |
| 61 | return "unexpected colortype"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 62 | } |
| 63 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 64 | SkColor color_to_565(SkColor color) { |
| 65 | SkPMColor pmColor = SkPreMultiplyColor(color); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 66 | U16CPU color16 = SkPixel32ToPixel16(pmColor); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 67 | return SkPixel16ToColor(color16); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 70 | void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y, |
| 71 | SkColorType colorType, SkAlphaType alphaType) { |
| 72 | SkBitmap tmp(bitmap); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 73 | const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType); |
skia.committer@gmail.com | e62513f | 2014-03-08 03:02:06 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 75 | canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y); |
| 76 | } |
| 77 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 78 | void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y, |
| 79 | SkColorType colorType, SkAlphaType alphaType) { |
| 80 | const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType); |
| 81 | surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y); |
| 82 | } |
| 83 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 84 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 85 | SkBitmap bm; |
brianosman | 2331a5f | 2016-09-28 14:02:10 -0700 | [diff] [blame] | 86 | bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType)); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 87 | bm.eraseColor(c1); |
| 88 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 89 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 90 | return SkShader::MakeBitmapShader( |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 91 | bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
| 92 | } |
| 93 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 94 | SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 95 | SkBitmap bitmap; |
brianosman | 2331a5f | 2016-09-28 14:02:10 -0700 | [diff] [blame] | 96 | bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 97 | SkCanvas canvas(bitmap); |
| 98 | |
| 99 | sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize); |
| 100 | return bitmap; |
| 101 | } |
| 102 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 103 | void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 104 | SkPaint paint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 105 | paint.setShader(create_checkerboard_shader(c1, c2, size)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 106 | paint.setBlendMode(SkBlendMode::kSrc); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 107 | canvas->drawPaint(paint); |
| 108 | } |
| 109 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 110 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 111 | int textSize, const char* str) { |
| 112 | SkBitmap bitmap; |
| 113 | bitmap.allocN32Pixels(w, h); |
| 114 | SkCanvas canvas(bitmap); |
| 115 | |
| 116 | SkPaint paint; |
| 117 | paint.setAntiAlias(true); |
| 118 | sk_tool_utils::set_portable_typeface(&paint); |
| 119 | paint.setColor(c); |
| 120 | paint.setTextSize(SkIntToScalar(textSize)); |
| 121 | |
| 122 | canvas.clear(0x00000000); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 123 | canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 124 | |
Brian Osman | 2b25d34 | 2016-12-20 11:09:31 -0500 | [diff] [blame] | 125 | // Tag data as sRGB (without doing any color space conversion). Color-space aware configs |
| 126 | // will process this correctly but legacy configs will render as if this returned N32. |
| 127 | SkBitmap result; |
| 128 | result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 129 | result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 130 | return result; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 133 | void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len, |
| 134 | const SkPaint& origPaint, SkScalar x, SkScalar y) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 135 | SkPaint paint(origPaint); |
| 136 | SkTDArray<uint16_t> glyphs; |
| 137 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | glyphs.append(paint.textToGlyphs(text, len, nullptr)); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 139 | paint.textToGlyphs(text, len, glyphs.begin()); |
| 140 | |
| 141 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 142 | const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 143 | nullptr); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 144 | memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); |
| 145 | } |
| 146 | |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 147 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, |
| 148 | const SkPaint& origPaint, SkScalar x, SkScalar y) { |
| 149 | add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y); |
| 150 | } |
| 151 | |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 152 | SkPath make_star(const SkRect& bounds, int numPts, int step) { |
| 153 | SkPath path; |
| 154 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 155 | path.moveTo(0,-1); |
| 156 | for (int i = 1; i < numPts; ++i) { |
| 157 | int idx = i*step; |
| 158 | SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2; |
| 159 | SkScalar x = SkScalarCos(theta); |
| 160 | SkScalar y = -SkScalarSin(theta); |
| 161 | path.lineTo(x, y); |
| 162 | } |
| 163 | path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit)); |
| 164 | return path; |
| 165 | } |
| 166 | |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 167 | static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) { |
| 168 | SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); |
| 169 | unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255); |
| 170 | unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255); |
| 171 | unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255); |
| 172 | *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 173 | } |
| 174 | |
| 175 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 176 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 177 | dst.fTop + (dst.height() / 2.0f)); |
| 178 | const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f); |
| 179 | |
| 180 | SkVector3 norm; |
| 181 | |
| 182 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 183 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 184 | norm.fX = (x + 0.5f - center.fX) / halfSize.fX; |
| 185 | norm.fY = (y + 0.5f - center.fY) / halfSize.fY; |
| 186 | |
| 187 | SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY; |
| 188 | if (tmp >= 1.0f) { |
| 189 | norm.set(0.0f, 0.0f, 1.0f); |
| 190 | } else { |
| 191 | norm.fZ = sqrtf(1.0f - tmp); |
| 192 | } |
| 193 | |
| 194 | norm_to_rgb(bm, x, y, norm); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 200 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 201 | dst.fTop + (dst.height() / 2.0f)); |
| 202 | |
| 203 | SkIRect inner = dst; |
| 204 | inner.inset(dst.width()/4, dst.height()/4); |
| 205 | |
| 206 | SkPoint3 norm; |
| 207 | const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 208 | const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 209 | const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 210 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 211 | |
| 212 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 213 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 214 | if (inner.contains(x, y)) { |
| 215 | norm.set(0.0f, 0.0f, 1.0f); |
| 216 | } else { |
| 217 | SkScalar locX = x + 0.5f - center.fX; |
| 218 | SkScalar locY = y + 0.5f - center.fY; |
| 219 | |
| 220 | if (locX >= 0.0f) { |
| 221 | if (locY > 0.0f) { |
| 222 | norm = locX >= locY ? right : down; // LR corner |
| 223 | } else { |
| 224 | norm = locX > -locY ? right : up; // UR corner |
| 225 | } |
| 226 | } else { |
| 227 | if (locY > 0.0f) { |
| 228 | norm = -locX > locY ? left : down; // LL corner |
| 229 | } else { |
| 230 | norm = locX > locY ? up : left; // UL corner |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | norm_to_rgb(bm, x, y, norm); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 241 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 242 | dst.fTop + (dst.height() / 2.0f)); |
| 243 | |
| 244 | static const SkScalar k1OverRoot3 = 0.5773502692f; |
| 245 | |
| 246 | SkPoint3 norm; |
| 247 | const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 248 | const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 249 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 250 | |
| 251 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 252 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 253 | SkScalar locX = x + 0.5f - center.fX; |
| 254 | SkScalar locY = y + 0.5f - center.fY; |
| 255 | |
| 256 | if (locX >= 0.0f) { |
| 257 | if (locY > 0.0f) { |
| 258 | norm = locX >= locY ? rightUp : down; // LR corner |
| 259 | } else { |
| 260 | norm = rightUp; |
| 261 | } |
| 262 | } else { |
| 263 | if (locY > 0.0f) { |
| 264 | norm = -locX > locY ? leftUp : down; // LL corner |
| 265 | } else { |
| 266 | norm = leftUp; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | norm_to_rgb(bm, x, y, norm); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Mike Klein | c722f79 | 2017-07-31 11:57:21 -0400 | [diff] [blame] | 275 | #if !defined(__clang__) && defined(_MSC_VER) |
Mike Klein | 88fa747 | 2016-10-12 17:06:48 -0400 | [diff] [blame] | 276 | // MSVC takes ~2 minutes to compile this function with optimization. |
| 277 | // We don't really care to wait that long for this function. |
| 278 | #pragma optimize("", off) |
| 279 | #endif |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 280 | void make_big_path(SkPath& path) { |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 281 | #include "BigPathBench.inc" // IWYU pragma: keep |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 282 | } |
| 283 | |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 284 | static float gaussian2d_value(int x, int y, float sigma) { |
| 285 | // don't bother with the scale term since we're just going to normalize the |
| 286 | // kernel anyways |
bsalomon | 2f8ac35 | 2015-10-19 08:29:16 -0700 | [diff] [blame] | 287 | float temp = expf(-(x*x + y*y)/(2*sigma*sigma)); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 288 | return temp; |
| 289 | } |
| 290 | |
| 291 | static float* create_2d_kernel(float sigma, int* filterSize) { |
| 292 | // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel |
| 293 | // sizes are always odd) |
| 294 | int halfFilterSize = SkScalarCeilToInt(6*sigma)/2; |
| 295 | int wh = *filterSize = 2*halfFilterSize + 1; |
| 296 | |
| 297 | float* temp = new float[wh*wh]; |
| 298 | |
| 299 | float filterTot = 0.0f; |
| 300 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 301 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 302 | temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma); |
| 303 | |
| 304 | filterTot += temp[yOff*wh+xOff]; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // normalize the kernel |
| 309 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 310 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 311 | temp[yOff*wh+xOff] /= filterTot; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return temp; |
| 316 | } |
| 317 | |
| 318 | static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) { |
| 319 | SkASSERT(wh & 0x1); |
| 320 | |
| 321 | int halfFilterSize = (wh-1)/2; |
| 322 | |
| 323 | float r = 0.0f, g = 0.0f, b = 0.0f; |
| 324 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 325 | int ySamp = y + yOff - halfFilterSize; |
| 326 | |
| 327 | if (ySamp < 0) { |
| 328 | ySamp = 0; |
| 329 | } else if (ySamp > bm.height()-1) { |
| 330 | ySamp = bm.height()-1; |
| 331 | } |
| 332 | |
| 333 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 334 | int xSamp = x + xOff - halfFilterSize; |
| 335 | |
| 336 | if (xSamp < 0) { |
| 337 | xSamp = 0; |
| 338 | } else if (xSamp > bm.width()-1) { |
| 339 | xSamp = bm.width()-1; |
| 340 | } |
| 341 | |
| 342 | float filter = kernel[yOff*wh + xOff]; |
| 343 | |
| 344 | SkPMColor c = *bm.getAddr32(xSamp, ySamp); |
| 345 | |
| 346 | r += SkGetPackedR32(c) * filter; |
| 347 | g += SkGetPackedG32(c) * filter; |
| 348 | b += SkGetPackedB32(c) * filter; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | U8CPU r8, g8, b8; |
| 353 | |
| 354 | r8 = (U8CPU) (r+0.5f); |
| 355 | g8 = (U8CPU) (g+0.5f); |
| 356 | b8 = (U8CPU) (b+0.5f); |
| 357 | |
| 358 | return SkPackARGB32(255, r8, g8, b8); |
| 359 | } |
| 360 | |
| 361 | SkBitmap slow_blur(const SkBitmap& src, float sigma) { |
| 362 | SkBitmap dst; |
| 363 | |
| 364 | dst.allocN32Pixels(src.width(), src.height(), true); |
| 365 | |
| 366 | int wh; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 367 | std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh)); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 368 | |
| 369 | for (int y = 0; y < src.height(); ++y) { |
| 370 | for (int x = 0; x < src.width(); ++x) { |
| 371 | *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh); |
| 372 | } |
| 373 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 374 | |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 375 | return dst; |
| 376 | } |
| 377 | |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 378 | // compute the intersection point between the diagonal and the ellipse in the |
| 379 | // lower right corner |
| 380 | static SkPoint intersection(SkScalar w, SkScalar h) { |
| 381 | SkASSERT(w > 0.0f || h > 0.0f); |
| 382 | |
| 383 | return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2); |
| 384 | } |
| 385 | |
| 386 | // Use the intersection of the corners' diagonals with their ellipses to shrink |
| 387 | // the bounding rect |
| 388 | SkRect compute_central_occluder(const SkRRect& rr) { |
| 389 | const SkRect r = rr.getBounds(); |
| 390 | |
| 391 | SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom; |
| 392 | |
| 393 | SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner); |
| 394 | if (!radii.isZero()) { |
| 395 | SkPoint p = intersection(radii.fX, radii.fY); |
| 396 | |
| 397 | newL = SkTMax(newL, r.fLeft + radii.fX - p.fX); |
| 398 | newT = SkTMax(newT, r.fTop + radii.fY - p.fY); |
| 399 | } |
| 400 | |
| 401 | radii = rr.radii(SkRRect::kUpperRight_Corner); |
| 402 | if (!radii.isZero()) { |
| 403 | SkPoint p = intersection(radii.fX, radii.fY); |
| 404 | |
| 405 | newR = SkTMin(newR, r.fRight + p.fX - radii.fX); |
| 406 | newT = SkTMax(newT, r.fTop + radii.fY - p.fY); |
| 407 | } |
| 408 | |
| 409 | radii = rr.radii(SkRRect::kLowerRight_Corner); |
| 410 | if (!radii.isZero()) { |
| 411 | SkPoint p = intersection(radii.fX, radii.fY); |
| 412 | |
| 413 | newR = SkTMin(newR, r.fRight + p.fX - radii.fX); |
| 414 | newB = SkTMin(newB, r.fBottom - radii.fY + p.fY); |
| 415 | } |
| 416 | |
| 417 | radii = rr.radii(SkRRect::kLowerLeft_Corner); |
| 418 | if (!radii.isZero()) { |
| 419 | SkPoint p = intersection(radii.fX, radii.fY); |
| 420 | |
| 421 | newL = SkTMax(newL, r.fLeft + radii.fX - p.fX); |
| 422 | newB = SkTMin(newB, r.fBottom - radii.fY + p.fY); |
| 423 | } |
| 424 | |
| 425 | return SkRect::MakeLTRB(newL, newT, newR, newB); |
| 426 | } |
| 427 | |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 428 | // The widest inset rect |
| 429 | SkRect compute_widest_occluder(const SkRRect& rr) { |
| 430 | const SkRect& r = rr.getBounds(); |
| 431 | |
| 432 | const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner); |
| 433 | const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner); |
| 434 | const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner); |
| 435 | const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner); |
| 436 | |
| 437 | SkScalar maxT = SkTMax(ul.fY, ur.fY); |
| 438 | SkScalar maxB = SkTMax(ll.fY, lr.fY); |
| 439 | |
| 440 | return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | // The tallest inset rect |
| 445 | SkRect compute_tallest_occluder(const SkRRect& rr) { |
| 446 | const SkRect& r = rr.getBounds(); |
| 447 | |
| 448 | const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner); |
| 449 | const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner); |
| 450 | const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner); |
| 451 | const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner); |
| 452 | |
| 453 | SkScalar maxL = SkTMax(ul.fX, ll.fX); |
| 454 | SkScalar maxR = SkTMax(ur.fX, lr.fX); |
| 455 | |
| 456 | return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom); |
| 457 | } |
| 458 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 459 | bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 460 | SkPixmap srcPM; |
| 461 | if (!src.peekPixels(&srcPM)) { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | SkBitmap tmpDst; |
| 466 | SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType); |
| 467 | if (!tmpDst.setInfo(dstInfo)) { |
| 468 | return false; |
| 469 | } |
| 470 | |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 471 | if (!tmpDst.tryAllocPixels()) { |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | |
| 475 | SkPixmap dstPM; |
| 476 | if (!tmpDst.peekPixels(&dstPM)) { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | if (!srcPM.readPixels(dstPM)) { |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | dst->swap(tmpDst); |
| 485 | return true; |
| 486 | } |
| 487 | |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 488 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src) { |
| 489 | SkASSERT(kBGRA_8888_SkColorType == src.colorType() || |
| 490 | kRGBA_8888_SkColorType == src.colorType()); |
| 491 | |
| 492 | SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType); |
| 493 | dst->allocPixels(grayInfo); |
| 494 | uint8_t* dst8 = (uint8_t*)dst->getPixels(); |
| 495 | const uint32_t* src32 = (const uint32_t*)src.getPixels(); |
| 496 | |
| 497 | const int w = src.width(); |
| 498 | const int h = src.height(); |
| 499 | const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType()); |
| 500 | for (int y = 0; y < h; ++y) { |
| 501 | if (isBGRA) { |
| 502 | // BGRA |
| 503 | for (int x = 0; x < w; ++x) { |
| 504 | uint32_t s = src32[x]; |
| 505 | dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF); |
| 506 | } |
| 507 | } else { |
| 508 | // RGBA |
| 509 | for (int x = 0; x < w; ++x) { |
| 510 | uint32_t s = src32[x]; |
| 511 | dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF); |
| 512 | } |
| 513 | } |
| 514 | src32 = (const uint32_t*)((const char*)src32 + src.rowBytes()); |
| 515 | dst8 += dst->rowBytes(); |
| 516 | } |
| 517 | } |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 518 | |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 519 | ////////////////////////////////////////////////////////////////////////////////////////////// |
| 520 | |
| 521 | static int scale255(float x) { |
| 522 | return sk_float_round2int(x * 255); |
| 523 | } |
| 524 | |
| 525 | static unsigned diff(const SkColorType ct, const void* a, const void* b) { |
| 526 | int dr = 0, |
| 527 | dg = 0, |
| 528 | db = 0, |
| 529 | da = 0; |
| 530 | switch (ct) { |
| 531 | case kRGBA_8888_SkColorType: |
| 532 | case kBGRA_8888_SkColorType: { |
| 533 | SkPMColor c0 = *(const SkPMColor*)a; |
| 534 | SkPMColor c1 = *(const SkPMColor*)b; |
| 535 | dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 536 | dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 537 | db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 538 | da = SkGetPackedA32(c0) - SkGetPackedA32(c1); |
| 539 | } break; |
| 540 | case kRGB_565_SkColorType: { |
| 541 | uint16_t c0 = *(const uint16_t*)a; |
| 542 | uint16_t c1 = *(const uint16_t*)b; |
| 543 | dr = SkGetPackedR16(c0) - SkGetPackedR16(c1); |
| 544 | dg = SkGetPackedG16(c0) - SkGetPackedG16(c1); |
| 545 | db = SkGetPackedB16(c0) - SkGetPackedB16(c1); |
| 546 | } break; |
| 547 | case kARGB_4444_SkColorType: { |
| 548 | uint16_t c0 = *(const uint16_t*)a; |
| 549 | uint16_t c1 = *(const uint16_t*)b; |
| 550 | dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1); |
| 551 | dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1); |
| 552 | db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1); |
| 553 | da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1); |
| 554 | } break; |
| 555 | case kAlpha_8_SkColorType: |
| 556 | case kGray_8_SkColorType: |
| 557 | da = (const uint8_t*)a - (const uint8_t*)b; |
| 558 | break; |
| 559 | case kRGBA_F16_SkColorType: { |
| 560 | const SkPM4f* c0 = (const SkPM4f*)a; |
| 561 | const SkPM4f* c1 = (const SkPM4f*)b; |
| 562 | dr = scale255(c0->r() - c1->r()); |
| 563 | dg = scale255(c0->g() - c1->g()); |
| 564 | db = scale255(c0->b() - c1->b()); |
| 565 | da = scale255(c0->a() - c1->a()); |
| 566 | } break; |
| 567 | default: |
| 568 | return 0; |
| 569 | } |
| 570 | dr = SkAbs32(dr); |
| 571 | dg = SkAbs32(dg); |
| 572 | db = SkAbs32(db); |
| 573 | da = SkAbs32(da); |
| 574 | return SkMax32(dr, SkMax32(dg, SkMax32(db, da))); |
| 575 | } |
| 576 | |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 577 | bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff, |
| 578 | bool respectColorSpace) { |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 579 | if (a.width() != b.width() || |
| 580 | a.height() != b.height() || |
| 581 | a.colorType() != b.colorType() || |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 582 | (respectColorSpace && (a.colorSpace() != b.colorSpace()))) |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 583 | { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | for (int y = 0; y < a.height(); ++y) { |
| 588 | const char* aptr = (const char*)a.addr(0, y); |
| 589 | const char* bptr = (const char*)b.addr(0, y); |
| 590 | if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) { |
| 591 | for (int x = 0; x < a.width(); ++x) { |
| 592 | if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) { |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | aptr += a.rowBytes(); |
| 598 | bptr += b.rowBytes(); |
| 599 | } |
| 600 | return true; |
| 601 | } |
| 602 | |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 603 | bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff, |
| 604 | bool respectColorSpaces) { |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 605 | SkPixmap pm0, pm1; |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 606 | return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && |
| 607 | equal_pixels(pm0, pm1, maxDiff, respectColorSpaces); |
| 608 | } |
| 609 | |
| 610 | bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff, |
| 611 | bool respectColorSpaces) { |
| 612 | // ensure that peekPixels will succeed |
| 613 | auto imga = a->makeRasterImage(); |
| 614 | auto imgb = b->makeRasterImage(); |
| 615 | a = imga.get(); |
| 616 | b = imgb.get(); |
| 617 | |
| 618 | SkPixmap pm0, pm1; |
| 619 | return a->peekPixels(&pm0) && b->peekPixels(&pm1) && |
| 620 | equal_pixels(pm0, pm1, maxDiff, respectColorSpaces); |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 621 | } |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 622 | |
| 623 | sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info, |
| 624 | const SkSurfaceProps* props) { |
| 625 | auto surf = canvas->makeSurface(info, props); |
| 626 | if (!surf) { |
| 627 | surf = SkSurface::MakeRaster(info, props); |
| 628 | } |
| 629 | return surf; |
| 630 | } |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 631 | } // namespace sk_tool_utils |