blob: 4b57a2cead462554ebe87b052a9e876e22db7a5f [file] [log] [blame]
bungeman13b9c952016-05-12 10:09:30 -07001/*
tfarina20108912014-06-21 10:54:17 -07002 * 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
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +00008#include "sk_tool_utils.h"
Cary Clark992c7b02014-07-31 08:58:44 -04009#include "sk_tool_utils_flags.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000010
caryclarkc3dcb672015-07-21 12:27:36 -070011#include "Resources.h"
tfarina20108912014-06-21 10:54:17 -070012#include "SkBitmap.h"
13#include "SkCanvas.h"
caryclark6531c362015-07-20 13:38:56 -070014#include "SkCommonFlags.h"
kulshinc4b09152016-06-01 08:31:28 -070015#include "SkFontMgr.h"
16#include "SkFontStyle.h"
Brian Osman2b25d342016-12-20 11:09:31 -050017#include "SkPixelRef.h"
robertphillipsea4529d2015-08-17 15:04:47 -070018#include "SkPoint3.h"
halcanaryb0cce2c2015-01-26 12:49:00 -080019#include "SkShader.h"
Cary Clark992c7b02014-07-31 08:58:44 -040020#include "SkTestScalerContext.h"
joshualitt9e36c1a2015-04-14 12:17:27 -070021#include "SkTextBlob.h"
Cary Clark992c7b02014-07-31 08:58:44 -040022
23DEFINE_bool(portableFonts, false, "Use portable fonts");
tfarina20108912014-06-21 10:54:17 -070024
Brian Osman964dec32017-01-26 09:32:33 -050025#if SK_SUPPORT_GPU
26#include "effects/GrSRGBEffect.h"
27#include "SkColorFilter.h"
28
29// Color filter that just wraps GrSRGBEffect
30class SkSRGBColorFilter : public SkColorFilter {
31public:
32 static sk_sp<SkColorFilter> Make(GrSRGBEffect::Mode mode) {
33 return sk_sp<SkColorFilter>(new SkSRGBColorFilter(mode));
34 }
35
36 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override {
37 return GrSRGBEffect::Make(fMode);
38 }
39
40 void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override {
41 SK_ABORT("SkSRGBColorFilter is only implemented for GPU");
42 }
Mike Reedb9641bd2017-05-04 10:57:40 -040043 void filterSpan4f(const SkPM4f src[], int count, SkPM4f dst[]) const override {
44 SK_ABORT("SkSRGBColorFilter is only implemented for GPU");
45 }
Brian Osman964dec32017-01-26 09:32:33 -050046 Factory getFactory() const override { return nullptr; }
47
48#ifndef SK_IGNORE_TO_STRING
49 void toString(SkString* str) const override {}
50#endif
51
52private:
53 SkSRGBColorFilter(GrSRGBEffect::Mode mode) : fMode(mode) {}
54
55 GrSRGBEffect::Mode fMode;
56 typedef SkColorFilter INHERITED;
57};
58#endif
59
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000060namespace sk_tool_utils {
61
caryclark37213552015-07-24 11:08:01 -070062/* these are the default fonts chosen by Chrome for serif, sans-serif, and monospace */
63static const char* gStandardFontNames[][3] = {
64 { "Times", "Helvetica", "Courier" }, // Mac
65 { "Times New Roman", "Helvetica", "Courier" }, // iOS
66 { "Times New Roman", "Arial", "Courier New" }, // Win
67 { "Times New Roman", "Arial", "Monospace" }, // Ubuntu
caryclark1818acb2015-07-24 12:09:25 -070068 { "serif", "sans-serif", "monospace" }, // Android
caryclark37213552015-07-24 11:08:01 -070069 { "Tinos", "Arimo", "Cousine" } // ChromeOS
70};
71
72const char* platform_font_name(const char* name) {
73 SkString platform = major_platform_os_name();
74 int index;
75 if (!strcmp(name, "serif")) {
76 index = 0;
77 } else if (!strcmp(name, "san-serif")) {
78 index = 1;
79 } else if (!strcmp(name, "monospace")) {
80 index = 2;
81 } else {
82 return name;
83 }
84 if (platform.equals("Mac")) {
85 return gStandardFontNames[0][index];
86 }
87 if (platform.equals("iOS")) {
88 return gStandardFontNames[1][index];
89 }
90 if (platform.equals("Win")) {
91 return gStandardFontNames[2][index];
92 }
93 if (platform.equals("Ubuntu")) {
94 return gStandardFontNames[3][index];
95 }
96 if (platform.equals("Android")) {
97 return gStandardFontNames[4][index];
98 }
99 if (platform.equals("ChromeOS")) {
100 return gStandardFontNames[5][index];
101 }
102 return name;
103}
104
caryclarkc3dcb672015-07-21 12:27:36 -0700105const char* platform_os_emoji() {
106 const char* osName = platform_os_name();
caryclarke6633412015-07-23 09:11:58 -0700107 if (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu")) {
caryclarkc3dcb672015-07-21 12:27:36 -0700108 return "CBDT";
109 }
bungemanfbbf01a2016-07-20 07:15:16 -0700110 if (!strncmp(osName, "Mac", 3) || !strncmp(osName, "iOS", 3)) {
caryclarkc3dcb672015-07-21 12:27:36 -0700111 return "SBIX";
112 }
kulshinc4b09152016-06-01 08:31:28 -0700113 if (!strncmp(osName, "Win", 3)) {
114 return "COLR";
115 }
caryclarkc3dcb672015-07-21 12:27:36 -0700116 return "";
117}
118
bungeman13b9c952016-05-12 10:09:30 -0700119sk_sp<SkTypeface> emoji_typeface() {
caryclarkc3dcb672015-07-21 12:27:36 -0700120 if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) {
bungeman13b9c952016-05-12 10:09:30 -0700121 return MakeResourceAsTypeface("/fonts/Funkster.ttf");
caryclarkc3dcb672015-07-21 12:27:36 -0700122 }
123 if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) {
mbocee6a9912016-05-31 11:42:36 -0700124 return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle());
caryclarkc3dcb672015-07-21 12:27:36 -0700125 }
kulshinc4b09152016-06-01 08:31:28 -0700126 if (!strcmp(sk_tool_utils::platform_os_emoji(), "COLR")) {
127 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
128 const char *colorEmojiFontName = "Segoe UI Emoji";
129 sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle()));
130 if (typeface) {
131 return typeface;
132 }
133 sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter(
134 colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */,
135 0x1f4b0 /* character: 💰 */));
136 if (fallback) {
137 return fallback;
138 }
139 // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol.
140 // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji.
141 return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle());
142 }
bungeman13b9c952016-05-12 10:09:30 -0700143 return nullptr;
caryclarkc3dcb672015-07-21 12:27:36 -0700144}
145
146const char* emoji_sample_text() {
147 if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) {
148 return "Hamburgefons";
halcanary9d524f22016-03-29 09:03:52 -0700149 }
kulshinc4b09152016-06-01 08:31:28 -0700150 if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX") ||
151 !strcmp(sk_tool_utils::platform_os_emoji(), "COLR"))
152 {
caryclarkc3dcb672015-07-21 12:27:36 -0700153 return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // 💰🏡🎅
154 "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // 🍪🍕🚀
155 "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // 🚻💩📷
156 "\xF0\x9F\x93\xA6" // 📦
157 "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // 🇺🇸🇦
158 }
159 return "";
160}
161
caryclark6531c362015-07-20 13:38:56 -0700162const char* platform_os_name() {
163 for (int index = 0; index < FLAGS_key.count(); index += 2) {
164 if (!strcmp("os", FLAGS_key[index])) {
165 return FLAGS_key[index + 1];
166 }
167 }
168 // when running SampleApp or dm without a --key pair, omit the platform name
169 return "";
170}
171
caryclark37213552015-07-24 11:08:01 -0700172// omit version number in returned value
173SkString major_platform_os_name() {
174 SkString name;
175 for (int index = 0; index < FLAGS_key.count(); index += 2) {
176 if (!strcmp("os", FLAGS_key[index])) {
177 const char* platform = FLAGS_key[index + 1];
178 const char* end = platform;
179 while (*end && (*end < '0' || *end > '9')) {
180 ++end;
181 }
182 name.append(platform, end - platform);
183 break;
184 }
185 }
186 return name;
187}
188
caryclark6531c362015-07-20 13:38:56 -0700189const char* platform_extra_config(const char* config) {
190 for (int index = 0; index < FLAGS_key.count(); index += 2) {
191 if (!strcmp("extra_config", FLAGS_key[index]) && !strcmp(config, FLAGS_key[index + 1])) {
192 return config;
193 }
194 }
195 return "";
196}
197
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000198const char* colortype_name(SkColorType ct) {
199 switch (ct) {
200 case kUnknown_SkColorType: return "Unknown";
201 case kAlpha_8_SkColorType: return "Alpha_8";
202 case kIndex_8_SkColorType: return "Index_8";
203 case kARGB_4444_SkColorType: return "ARGB_4444";
204 case kRGB_565_SkColorType: return "RGB_565";
205 case kRGBA_8888_SkColorType: return "RGBA_8888";
206 case kBGRA_8888_SkColorType: return "BGRA_8888";
Brian Osmanf750fbc2017-02-08 10:47:28 -0500207 case kRGBA_F16_SkColorType: return "RGBA_F16";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000208 default:
209 SkASSERT(false);
210 return "unexpected colortype";
211 }
212}
213
caryclark65cdba62015-06-15 06:51:08 -0700214SkColor color_to_565(SkColor color) {
215 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -0700216 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -0700217 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -0700218}
219
mbocee6a9912016-05-31 11:42:36 -0700220sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -0700221 return create_font(name, style);
222}
223
mbocee6a9912016-05-31 11:42:36 -0700224void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) {
bungeman13b9c952016-05-12 10:09:30 -0700225 paint->setTypeface(create_font(name, style));
caryclark83ca6282015-06-10 09:31:09 -0700226}
halcanary9d524f22016-03-29 09:03:52 -0700227
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000228void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
229 SkColorType colorType, SkAlphaType alphaType) {
230 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -0700231 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000232
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000233 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
234}
235
reed8a21c9f2016-03-08 18:50:00 -0800236sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -0800237 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -0700238 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -0800239 bm.eraseColor(c1);
240 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
241 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -0800242 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -0800243 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
244}
245
robertphillips943a4622015-09-03 13:32:33 -0700246SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
247 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700248 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700249 SkCanvas canvas(bitmap);
250
251 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
252 return bitmap;
253}
254
halcanaryb0cce2c2015-01-26 12:49:00 -0800255void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
256 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800257 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700258 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800259 canvas->drawPaint(paint);
260}
261
robertphillips943a4622015-09-03 13:32:33 -0700262SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
263 int textSize, const char* str) {
264 SkBitmap bitmap;
265 bitmap.allocN32Pixels(w, h);
266 SkCanvas canvas(bitmap);
267
268 SkPaint paint;
269 paint.setAntiAlias(true);
270 sk_tool_utils::set_portable_typeface(&paint);
271 paint.setColor(c);
272 paint.setTextSize(SkIntToScalar(textSize));
273
274 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400275 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700276
Brian Osman2b25d342016-12-20 11:09:31 -0500277 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
278 // will process this correctly but legacy configs will render as if this returned N32.
279 SkBitmap result;
280 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
281 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
282 return result;
robertphillips943a4622015-09-03 13:32:33 -0700283}
284
joshualitt9e36c1a2015-04-14 12:17:27 -0700285void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
286 SkScalar x, SkScalar y) {
287 SkPaint paint(origPaint);
288 SkTDArray<uint16_t> glyphs;
289
290 size_t len = strlen(text);
halcanary96fcdcc2015-08-27 07:41:13 -0700291 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700292 paint.textToGlyphs(text, len, glyphs.begin());
293
294 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
295 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700296 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700297 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
298}
299
robertphillipsea4529d2015-08-17 15:04:47 -0700300static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
301 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
302 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
303 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
304 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
305 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
306}
307
308void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
309 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
310 dst.fTop + (dst.height() / 2.0f));
311 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
312
313 SkVector3 norm;
314
315 for (int y = dst.fTop; y < dst.fBottom; ++y) {
316 for (int x = dst.fLeft; x < dst.fRight; ++x) {
317 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
318 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
halcanary9d524f22016-03-29 09:03:52 -0700319
robertphillipsea4529d2015-08-17 15:04:47 -0700320 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
321 if (tmp >= 1.0f) {
322 norm.set(0.0f, 0.0f, 1.0f);
323 } else {
bsalomonf3bf8922015-08-19 06:12:36 -0700324 norm.fZ = sqrtf(1.0f - tmp);
robertphillipsea4529d2015-08-17 15:04:47 -0700325 }
326
327 norm_to_rgb(bm, x, y, norm);
328 }
329 }
330}
331
332void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
333 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
334 dst.fTop + (dst.height() / 2.0f));
335
336 SkIRect inner = dst;
337 inner.inset(dst.width()/4, dst.height()/4);
338
339 SkPoint3 norm;
340 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
341 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
342 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
343 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
344
345 for (int y = dst.fTop; y < dst.fBottom; ++y) {
346 for (int x = dst.fLeft; x < dst.fRight; ++x) {
347 if (inner.contains(x, y)) {
348 norm.set(0.0f, 0.0f, 1.0f);
349 } else {
350 SkScalar locX = x + 0.5f - center.fX;
351 SkScalar locY = y + 0.5f - center.fY;
352
353 if (locX >= 0.0f) {
354 if (locY > 0.0f) {
355 norm = locX >= locY ? right : down; // LR corner
356 } else {
357 norm = locX > -locY ? right : up; // UR corner
halcanary9d524f22016-03-29 09:03:52 -0700358 }
robertphillipsea4529d2015-08-17 15:04:47 -0700359 } else {
360 if (locY > 0.0f) {
361 norm = -locX > locY ? left : down; // LL corner
362 } else {
363 norm = locX > locY ? up : left; // UL corner
halcanary9d524f22016-03-29 09:03:52 -0700364 }
robertphillipsea4529d2015-08-17 15:04:47 -0700365 }
366 }
367
368 norm_to_rgb(bm, x, y, norm);
369 }
370 }
371}
372
373void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
374 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
375 dst.fTop + (dst.height() / 2.0f));
376
377 static const SkScalar k1OverRoot3 = 0.5773502692f;
378
379 SkPoint3 norm;
380 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
381 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
382 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
383
384 for (int y = dst.fTop; y < dst.fBottom; ++y) {
385 for (int x = dst.fLeft; x < dst.fRight; ++x) {
386 SkScalar locX = x + 0.5f - center.fX;
387 SkScalar locY = y + 0.5f - center.fY;
388
389 if (locX >= 0.0f) {
390 if (locY > 0.0f) {
391 norm = locX >= locY ? rightUp : down; // LR corner
392 } else {
393 norm = rightUp;
halcanary9d524f22016-03-29 09:03:52 -0700394 }
robertphillipsea4529d2015-08-17 15:04:47 -0700395 } else {
396 if (locY > 0.0f) {
397 norm = -locX > locY ? leftUp : down; // LL corner
398 } else {
399 norm = leftUp;
halcanary9d524f22016-03-29 09:03:52 -0700400 }
robertphillipsea4529d2015-08-17 15:04:47 -0700401 }
402
403 norm_to_rgb(bm, x, y, norm);
404 }
405 }
406}
joshualitt9e36c1a2015-04-14 12:17:27 -0700407
Mike Klein88fa7472016-10-12 17:06:48 -0400408#if defined(_MSC_VER)
409 // MSVC takes ~2 minutes to compile this function with optimization.
410 // We don't really care to wait that long for this function.
411 #pragma optimize("", off)
412#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700413void make_big_path(SkPath& path) {
414 #include "BigPathBench.inc"
415}
416
robertphillips9c4909b2015-10-19 06:39:17 -0700417static float gaussian2d_value(int x, int y, float sigma) {
418 // don't bother with the scale term since we're just going to normalize the
419 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700420 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700421 return temp;
422}
423
424static float* create_2d_kernel(float sigma, int* filterSize) {
425 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
426 // sizes are always odd)
427 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
428 int wh = *filterSize = 2*halfFilterSize + 1;
429
430 float* temp = new float[wh*wh];
431
432 float filterTot = 0.0f;
433 for (int yOff = 0; yOff < wh; ++yOff) {
434 for (int xOff = 0; xOff < wh; ++xOff) {
435 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
436
437 filterTot += temp[yOff*wh+xOff];
438 }
439 }
440
441 // normalize the kernel
442 for (int yOff = 0; yOff < wh; ++yOff) {
443 for (int xOff = 0; xOff < wh; ++xOff) {
444 temp[yOff*wh+xOff] /= filterTot;
445 }
446 }
447
448 return temp;
449}
450
451static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
452 SkASSERT(wh & 0x1);
453
454 int halfFilterSize = (wh-1)/2;
455
456 float r = 0.0f, g = 0.0f, b = 0.0f;
457 for (int yOff = 0; yOff < wh; ++yOff) {
458 int ySamp = y + yOff - halfFilterSize;
459
460 if (ySamp < 0) {
461 ySamp = 0;
462 } else if (ySamp > bm.height()-1) {
463 ySamp = bm.height()-1;
464 }
465
466 for (int xOff = 0; xOff < wh; ++xOff) {
467 int xSamp = x + xOff - halfFilterSize;
468
469 if (xSamp < 0) {
470 xSamp = 0;
471 } else if (xSamp > bm.width()-1) {
472 xSamp = bm.width()-1;
473 }
474
475 float filter = kernel[yOff*wh + xOff];
476
477 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
478
479 r += SkGetPackedR32(c) * filter;
480 g += SkGetPackedG32(c) * filter;
481 b += SkGetPackedB32(c) * filter;
482 }
483 }
484
485 U8CPU r8, g8, b8;
486
487 r8 = (U8CPU) (r+0.5f);
488 g8 = (U8CPU) (g+0.5f);
489 b8 = (U8CPU) (b+0.5f);
490
491 return SkPackARGB32(255, r8, g8, b8);
492}
493
494SkBitmap slow_blur(const SkBitmap& src, float sigma) {
495 SkBitmap dst;
496
497 dst.allocN32Pixels(src.width(), src.height(), true);
498
499 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400500 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700501
502 for (int y = 0; y < src.height(); ++y) {
503 for (int x = 0; x < src.width(); ++x) {
504 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
505 }
506 }
halcanary9d524f22016-03-29 09:03:52 -0700507
robertphillips9c4909b2015-10-19 06:39:17 -0700508 return dst;
509}
510
robertphillips276d3282016-08-04 09:03:19 -0700511// compute the intersection point between the diagonal and the ellipse in the
512// lower right corner
513static SkPoint intersection(SkScalar w, SkScalar h) {
514 SkASSERT(w > 0.0f || h > 0.0f);
515
516 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
517}
518
519// Use the intersection of the corners' diagonals with their ellipses to shrink
520// the bounding rect
521SkRect compute_central_occluder(const SkRRect& rr) {
522 const SkRect r = rr.getBounds();
523
524 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
525
526 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
527 if (!radii.isZero()) {
528 SkPoint p = intersection(radii.fX, radii.fY);
529
530 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
531 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
532 }
533
534 radii = rr.radii(SkRRect::kUpperRight_Corner);
535 if (!radii.isZero()) {
536 SkPoint p = intersection(radii.fX, radii.fY);
537
538 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
539 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
540 }
541
542 radii = rr.radii(SkRRect::kLowerRight_Corner);
543 if (!radii.isZero()) {
544 SkPoint p = intersection(radii.fX, radii.fY);
545
546 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
547 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
548 }
549
550 radii = rr.radii(SkRRect::kLowerLeft_Corner);
551 if (!radii.isZero()) {
552 SkPoint p = intersection(radii.fX, radii.fY);
553
554 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
555 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
556 }
557
558 return SkRect::MakeLTRB(newL, newT, newR, newB);
559}
560
robertphillips401c1962016-08-04 12:35:46 -0700561// The widest inset rect
562SkRect compute_widest_occluder(const SkRRect& rr) {
563 const SkRect& r = rr.getBounds();
564
565 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
566 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
567 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
568 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
569
570 SkScalar maxT = SkTMax(ul.fY, ur.fY);
571 SkScalar maxB = SkTMax(ll.fY, lr.fY);
572
573 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
574
575}
576
577// The tallest inset rect
578SkRect compute_tallest_occluder(const SkRRect& rr) {
579 const SkRect& r = rr.getBounds();
580
581 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
582 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
583 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
584 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
585
586 SkScalar maxL = SkTMax(ul.fX, ll.fX);
587 SkScalar maxR = SkTMax(ur.fX, lr.fX);
588
589 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
590}
591
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400592bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
593 SkPixmap srcPM;
594 if (!src.peekPixels(&srcPM)) {
595 return false;
596 }
597
598 SkBitmap tmpDst;
599 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
600 if (!tmpDst.setInfo(dstInfo)) {
601 return false;
602 }
603
604 // allocate colortable if srcConfig == kIndex8_Config
605 sk_sp<SkColorTable> ctable = nullptr;
606 if (dstColorType == kIndex_8_SkColorType) {
607 if (src.colorType() != kIndex_8_SkColorType) {
608 return false;
609 }
610
611 ctable = sk_ref_sp(srcPM.ctable());
612 }
613 if (!tmpDst.tryAllocPixels(ctable.get())) {
614 return false;
615 }
616
617 SkPixmap dstPM;
618 if (!tmpDst.peekPixels(&dstPM)) {
619 return false;
620 }
621
622 if (!srcPM.readPixels(dstPM)) {
623 return false;
624 }
625
626 dst->swap(tmpDst);
627 return true;
628}
629
Matt Sarett4897fb82017-01-18 11:49:33 -0500630void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
631 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
632 kRGBA_8888_SkColorType == src.colorType());
633
634 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
635 dst->allocPixels(grayInfo);
636 uint8_t* dst8 = (uint8_t*)dst->getPixels();
637 const uint32_t* src32 = (const uint32_t*)src.getPixels();
638
639 const int w = src.width();
640 const int h = src.height();
641 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
642 for (int y = 0; y < h; ++y) {
643 if (isBGRA) {
644 // BGRA
645 for (int x = 0; x < w; ++x) {
646 uint32_t s = src32[x];
647 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
648 }
649 } else {
650 // RGBA
651 for (int x = 0; x < w; ++x) {
652 uint32_t s = src32[x];
653 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
654 }
655 }
656 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
657 dst8 += dst->rowBytes();
658 }
659}
robertphillips401c1962016-08-04 12:35:46 -0700660
Brian Osman964dec32017-01-26 09:32:33 -0500661#if SK_SUPPORT_GPU
662sk_sp<SkColorFilter> MakeLinearToSRGBColorFilter() {
663 return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kLinearToSRGB);
664}
665
666sk_sp<SkColorFilter> MakeSRGBToLinearColorFilter() {
667 return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kSRGBToLinear);
668}
669#endif
670
tfarina20108912014-06-21 10:54:17 -0700671} // namespace sk_tool_utils