blob: f0672070f102e5144f29479c2bbb25390aab28a1 [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"
9
caryclarkc3dcb672015-07-21 12:27:36 -070010#include "Resources.h"
tfarina20108912014-06-21 10:54:17 -070011#include "SkBitmap.h"
12#include "SkCanvas.h"
caryclark6531c362015-07-20 13:38:56 -070013#include "SkCommonFlags.h"
kulshinc4b09152016-06-01 08:31:28 -070014#include "SkFontMgr.h"
15#include "SkFontStyle.h"
Mike Reed24846602017-12-04 16:06:03 -050016#include "SkImage.h"
Brian Osman2b25d342016-12-20 11:09:31 -050017#include "SkPixelRef.h"
Mike Reed5a625e02017-08-08 15:48:54 -040018#include "SkPM4f.h"
robertphillipsea4529d2015-08-17 15:04:47 -070019#include "SkPoint3.h"
halcanaryb0cce2c2015-01-26 12:49:00 -080020#include "SkShader.h"
Mike Reed46596ae2018-01-02 15:40:29 -050021#include "SkSurface.h"
Cary Clark992c7b02014-07-31 08:58:44 -040022#include "SkTestScalerContext.h"
joshualitt9e36c1a2015-04-14 12:17:27 -070023#include "SkTextBlob.h"
Cary Clark992c7b02014-07-31 08:58:44 -040024
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000025namespace sk_tool_utils {
26
Mike Kleinb251b722017-11-13 11:03:16 -050027static const char* platform_os_name() {
28 for (int index = 0; index < FLAGS_key.count(); index += 2) {
29 if (!strcmp("os", FLAGS_key[index])) {
30 return FLAGS_key[index + 1];
31 }
32 }
33 return "";
34}
35
bungeman13b9c952016-05-12 10:09:30 -070036sk_sp<SkTypeface> emoji_typeface() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050037#if defined(SK_BUILD_FOR_WIN)
38 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
39 const char *colorEmojiFontName = "Segoe UI Emoji";
40 sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle()));
41 if (typeface) {
42 return typeface;
caryclarkc3dcb672015-07-21 12:27:36 -070043 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050044 sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter(
45 colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */,
46 0x1f4b0 /* character: πŸ’° */));
47 if (fallback) {
48 return fallback;
caryclarkc3dcb672015-07-21 12:27:36 -070049 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050050 // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol.
51 // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji.
52 return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle());
53
54#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
55 return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle());
56
57#else
Hal Canary53e5e7d2017-12-08 14:25:14 -050058 return MakeResourceAsTypeface("fonts/Funkster.ttf");
Mike Kleinf436fbc2017-11-15 10:42:46 -050059
60#endif
caryclarkc3dcb672015-07-21 12:27:36 -070061}
62
63const char* emoji_sample_text() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050064#if defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
65 return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // πŸ’°πŸ‘πŸŽ…
66 "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πŸͺπŸ•πŸš€
67 "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // πŸš»πŸ’©πŸ“·
68 "\xF0\x9F\x93\xA6" // πŸ“¦
69 "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πŸ‡ΊπŸ‡ΈπŸ‡¦
70#else
71 return "Hamburgefons";
72#endif
caryclarkc3dcb672015-07-21 12:27:36 -070073}
74
Mike Kleinb251b722017-11-13 11:03:16 -050075static bool extra_config_contains(const char* substring) {
caryclark6531c362015-07-20 13:38:56 -070076 for (int index = 0; index < FLAGS_key.count(); index += 2) {
Mike Kleinb251b722017-11-13 11:03:16 -050077 if (0 == strcmp("extra_config", FLAGS_key[index])
78 && strstr(FLAGS_key[index + 1], substring)) {
79 return true;
caryclark6531c362015-07-20 13:38:56 -070080 }
81 }
Mike Kleinb251b722017-11-13 11:03:16 -050082 return false;
83}
84
85const char* platform_font_manager() {
86 if (extra_config_contains("GDI")) {
87 return "GDI";
88 }
89 if (extra_config_contains("NativeFonts")){
90 return platform_os_name();
91 }
caryclark6531c362015-07-20 13:38:56 -070092 return "";
93}
94
caryclark6531c362015-07-20 13:38:56 -070095
Mike Reedd4746982018-02-07 16:05:29 -050096const char* alphatype_name(SkAlphaType at) {
97 switch (at) {
98 case kUnknown_SkAlphaType: return "Unknown";
99 case kOpaque_SkAlphaType: return "Opaque";
100 case kPremul_SkAlphaType: return "Premul";
101 case kUnpremul_SkAlphaType: return "Unpremul";
102 }
103 SkASSERT(false);
104 return "unexpected alphatype";
105}
106
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000107const char* colortype_name(SkColorType ct) {
108 switch (ct) {
109 case kUnknown_SkColorType: return "Unknown";
110 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000111 case kRGB_565_SkColorType: return "RGB_565";
Mike Reedd4746982018-02-07 16:05:29 -0500112 case kARGB_4444_SkColorType: return "ARGB_4444";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000113 case kRGBA_8888_SkColorType: return "RGBA_8888";
Mike Reedd4746982018-02-07 16:05:29 -0500114 case kRGB_888x_SkColorType: return "RGB_888x";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000115 case kBGRA_8888_SkColorType: return "BGRA_8888";
Mike Reedd4746982018-02-07 16:05:29 -0500116 case kRGBA_1010102_SkColorType: return "RGBA_1010102";
117 case kRGB_101010x_SkColorType: return "RGB_101010x";
118 case kGray_8_SkColorType: return "Gray_8";
Brian Osmanf750fbc2017-02-08 10:47:28 -0500119 case kRGBA_F16_SkColorType: return "RGBA_F16";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000120 }
Mike Reedd4746982018-02-07 16:05:29 -0500121 SkASSERT(false);
122 return "unexpected colortype";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000123}
124
caryclark65cdba62015-06-15 06:51:08 -0700125SkColor color_to_565(SkColor color) {
126 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -0700127 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -0700128 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -0700129}
130
mbocee6a9912016-05-31 11:42:36 -0700131sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -0700132 return create_font(name, style);
133}
134
mbocee6a9912016-05-31 11:42:36 -0700135void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) {
bungeman13b9c952016-05-12 10:09:30 -0700136 paint->setTypeface(create_font(name, style));
caryclark83ca6282015-06-10 09:31:09 -0700137}
halcanary9d524f22016-03-29 09:03:52 -0700138
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000139void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
140 SkColorType colorType, SkAlphaType alphaType) {
141 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -0700142 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000143
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000144 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
145}
146
Mike Reed4c790bd2018-02-08 14:10:40 -0500147void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
148 SkColorType colorType, SkAlphaType alphaType) {
149 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
150 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
151}
152
reed8a21c9f2016-03-08 18:50:00 -0800153sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -0800154 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -0700155 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -0800156 bm.eraseColor(c1);
157 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
158 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -0800159 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -0800160 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
161}
162
robertphillips943a4622015-09-03 13:32:33 -0700163SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
164 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700165 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700166 SkCanvas canvas(bitmap);
167
168 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
169 return bitmap;
170}
171
halcanaryb0cce2c2015-01-26 12:49:00 -0800172void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
173 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800174 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700175 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800176 canvas->drawPaint(paint);
177}
178
robertphillips943a4622015-09-03 13:32:33 -0700179SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
180 int textSize, const char* str) {
181 SkBitmap bitmap;
182 bitmap.allocN32Pixels(w, h);
183 SkCanvas canvas(bitmap);
184
185 SkPaint paint;
186 paint.setAntiAlias(true);
187 sk_tool_utils::set_portable_typeface(&paint);
188 paint.setColor(c);
189 paint.setTextSize(SkIntToScalar(textSize));
190
191 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400192 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700193
Brian Osman2b25d342016-12-20 11:09:31 -0500194 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
195 // will process this correctly but legacy configs will render as if this returned N32.
196 SkBitmap result;
197 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
198 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
199 return result;
robertphillips943a4622015-09-03 13:32:33 -0700200}
201
Robert Phillips4c72b262017-08-15 13:28:42 -0400202void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
203 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700204 SkPaint paint(origPaint);
205 SkTDArray<uint16_t> glyphs;
206
halcanary96fcdcc2015-08-27 07:41:13 -0700207 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700208 paint.textToGlyphs(text, len, glyphs.begin());
209
210 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
211 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700212 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700213 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
214}
215
Robert Phillips4c72b262017-08-15 13:28:42 -0400216void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
217 const SkPaint& origPaint, SkScalar x, SkScalar y) {
218 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
219}
220
Chris Dalton7c02cc72017-11-06 14:10:54 -0700221SkPath make_star(const SkRect& bounds, int numPts, int step) {
222 SkPath path;
223 path.setFillType(SkPath::kEvenOdd_FillType);
224 path.moveTo(0,-1);
225 for (int i = 1; i < numPts; ++i) {
226 int idx = i*step;
227 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
228 SkScalar x = SkScalarCos(theta);
229 SkScalar y = -SkScalarSin(theta);
230 path.lineTo(x, y);
231 }
232 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
233 return path;
234}
235
Mike Kleinc722f792017-07-31 11:57:21 -0400236#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400237 // MSVC takes ~2 minutes to compile this function with optimization.
238 // We don't really care to wait that long for this function.
239 #pragma optimize("", off)
240#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700241void make_big_path(SkPath& path) {
242 #include "BigPathBench.inc"
243}
244
robertphillips9c4909b2015-10-19 06:39:17 -0700245static float gaussian2d_value(int x, int y, float sigma) {
246 // don't bother with the scale term since we're just going to normalize the
247 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700248 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700249 return temp;
250}
251
252static float* create_2d_kernel(float sigma, int* filterSize) {
253 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
254 // sizes are always odd)
255 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
256 int wh = *filterSize = 2*halfFilterSize + 1;
257
258 float* temp = new float[wh*wh];
259
260 float filterTot = 0.0f;
261 for (int yOff = 0; yOff < wh; ++yOff) {
262 for (int xOff = 0; xOff < wh; ++xOff) {
263 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
264
265 filterTot += temp[yOff*wh+xOff];
266 }
267 }
268
269 // normalize the kernel
270 for (int yOff = 0; yOff < wh; ++yOff) {
271 for (int xOff = 0; xOff < wh; ++xOff) {
272 temp[yOff*wh+xOff] /= filterTot;
273 }
274 }
275
276 return temp;
277}
278
279static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
280 SkASSERT(wh & 0x1);
281
282 int halfFilterSize = (wh-1)/2;
283
284 float r = 0.0f, g = 0.0f, b = 0.0f;
285 for (int yOff = 0; yOff < wh; ++yOff) {
286 int ySamp = y + yOff - halfFilterSize;
287
288 if (ySamp < 0) {
289 ySamp = 0;
290 } else if (ySamp > bm.height()-1) {
291 ySamp = bm.height()-1;
292 }
293
294 for (int xOff = 0; xOff < wh; ++xOff) {
295 int xSamp = x + xOff - halfFilterSize;
296
297 if (xSamp < 0) {
298 xSamp = 0;
299 } else if (xSamp > bm.width()-1) {
300 xSamp = bm.width()-1;
301 }
302
303 float filter = kernel[yOff*wh + xOff];
304
305 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
306
307 r += SkGetPackedR32(c) * filter;
308 g += SkGetPackedG32(c) * filter;
309 b += SkGetPackedB32(c) * filter;
310 }
311 }
312
313 U8CPU r8, g8, b8;
314
315 r8 = (U8CPU) (r+0.5f);
316 g8 = (U8CPU) (g+0.5f);
317 b8 = (U8CPU) (b+0.5f);
318
319 return SkPackARGB32(255, r8, g8, b8);
320}
321
322SkBitmap slow_blur(const SkBitmap& src, float sigma) {
323 SkBitmap dst;
324
325 dst.allocN32Pixels(src.width(), src.height(), true);
326
327 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400328 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700329
330 for (int y = 0; y < src.height(); ++y) {
331 for (int x = 0; x < src.width(); ++x) {
332 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
333 }
334 }
halcanary9d524f22016-03-29 09:03:52 -0700335
robertphillips9c4909b2015-10-19 06:39:17 -0700336 return dst;
337}
338
robertphillips276d3282016-08-04 09:03:19 -0700339// compute the intersection point between the diagonal and the ellipse in the
340// lower right corner
341static SkPoint intersection(SkScalar w, SkScalar h) {
342 SkASSERT(w > 0.0f || h > 0.0f);
343
344 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
345}
346
347// Use the intersection of the corners' diagonals with their ellipses to shrink
348// the bounding rect
349SkRect compute_central_occluder(const SkRRect& rr) {
350 const SkRect r = rr.getBounds();
351
352 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
353
354 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
355 if (!radii.isZero()) {
356 SkPoint p = intersection(radii.fX, radii.fY);
357
358 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
359 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
360 }
361
362 radii = rr.radii(SkRRect::kUpperRight_Corner);
363 if (!radii.isZero()) {
364 SkPoint p = intersection(radii.fX, radii.fY);
365
366 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
367 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
368 }
369
370 radii = rr.radii(SkRRect::kLowerRight_Corner);
371 if (!radii.isZero()) {
372 SkPoint p = intersection(radii.fX, radii.fY);
373
374 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
375 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
376 }
377
378 radii = rr.radii(SkRRect::kLowerLeft_Corner);
379 if (!radii.isZero()) {
380 SkPoint p = intersection(radii.fX, radii.fY);
381
382 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
383 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
384 }
385
386 return SkRect::MakeLTRB(newL, newT, newR, newB);
387}
388
robertphillips401c1962016-08-04 12:35:46 -0700389// The widest inset rect
390SkRect compute_widest_occluder(const SkRRect& rr) {
391 const SkRect& r = rr.getBounds();
392
393 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
394 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
395 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
396 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
397
398 SkScalar maxT = SkTMax(ul.fY, ur.fY);
399 SkScalar maxB = SkTMax(ll.fY, lr.fY);
400
401 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
402
403}
404
405// The tallest inset rect
406SkRect compute_tallest_occluder(const SkRRect& rr) {
407 const SkRect& r = rr.getBounds();
408
409 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
410 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
411 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
412 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
413
414 SkScalar maxL = SkTMax(ul.fX, ll.fX);
415 SkScalar maxR = SkTMax(ur.fX, lr.fX);
416
417 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
418}
419
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400420bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
421 SkPixmap srcPM;
422 if (!src.peekPixels(&srcPM)) {
423 return false;
424 }
425
426 SkBitmap tmpDst;
427 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
428 if (!tmpDst.setInfo(dstInfo)) {
429 return false;
430 }
431
Mike Reed304a07c2017-07-12 15:10:28 -0400432 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400433 return false;
434 }
435
436 SkPixmap dstPM;
437 if (!tmpDst.peekPixels(&dstPM)) {
438 return false;
439 }
440
441 if (!srcPM.readPixels(dstPM)) {
442 return false;
443 }
444
445 dst->swap(tmpDst);
446 return true;
447}
448
Matt Sarett4897fb82017-01-18 11:49:33 -0500449void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
450 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
451 kRGBA_8888_SkColorType == src.colorType());
452
453 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
454 dst->allocPixels(grayInfo);
455 uint8_t* dst8 = (uint8_t*)dst->getPixels();
456 const uint32_t* src32 = (const uint32_t*)src.getPixels();
457
458 const int w = src.width();
459 const int h = src.height();
460 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
461 for (int y = 0; y < h; ++y) {
462 if (isBGRA) {
463 // BGRA
464 for (int x = 0; x < w; ++x) {
465 uint32_t s = src32[x];
466 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
467 }
468 } else {
469 // RGBA
470 for (int x = 0; x < w; ++x) {
471 uint32_t s = src32[x];
472 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
473 }
474 }
475 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
476 dst8 += dst->rowBytes();
477 }
478}
robertphillips401c1962016-08-04 12:35:46 -0700479
Mike Reed5a625e02017-08-08 15:48:54 -0400480 //////////////////////////////////////////////////////////////////////////////////////////////
481
482 static int scale255(float x) {
483 return sk_float_round2int(x * 255);
484 }
485
486 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
487 int dr = 0,
488 dg = 0,
489 db = 0,
490 da = 0;
491 switch (ct) {
492 case kRGBA_8888_SkColorType:
493 case kBGRA_8888_SkColorType: {
494 SkPMColor c0 = *(const SkPMColor*)a;
495 SkPMColor c1 = *(const SkPMColor*)b;
496 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
497 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
498 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
499 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
500 } break;
501 case kRGB_565_SkColorType: {
502 uint16_t c0 = *(const uint16_t*)a;
503 uint16_t c1 = *(const uint16_t*)b;
504 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
505 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
506 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
507 } break;
508 case kARGB_4444_SkColorType: {
509 uint16_t c0 = *(const uint16_t*)a;
510 uint16_t c1 = *(const uint16_t*)b;
511 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
512 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
513 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
514 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
515 } break;
516 case kAlpha_8_SkColorType:
517 case kGray_8_SkColorType:
518 da = (const uint8_t*)a - (const uint8_t*)b;
519 break;
520 case kRGBA_F16_SkColorType: {
521 const SkPM4f* c0 = (const SkPM4f*)a;
522 const SkPM4f* c1 = (const SkPM4f*)b;
523 dr = scale255(c0->r() - c1->r());
524 dg = scale255(c0->g() - c1->g());
525 db = scale255(c0->b() - c1->b());
526 da = scale255(c0->a() - c1->a());
527 } break;
528 default:
529 return 0;
530 }
531 dr = SkAbs32(dr);
532 dg = SkAbs32(dg);
533 db = SkAbs32(db);
534 da = SkAbs32(da);
535 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
536 }
537
Mike Reed24846602017-12-04 16:06:03 -0500538 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
539 bool respectColorSpace) {
Mike Reed5a625e02017-08-08 15:48:54 -0400540 if (a.width() != b.width() ||
541 a.height() != b.height() ||
542 a.colorType() != b.colorType() ||
Mike Reed24846602017-12-04 16:06:03 -0500543 (respectColorSpace && (a.colorSpace() != b.colorSpace())))
Mike Reed5a625e02017-08-08 15:48:54 -0400544 {
545 return false;
546 }
547
548 for (int y = 0; y < a.height(); ++y) {
549 const char* aptr = (const char*)a.addr(0, y);
550 const char* bptr = (const char*)b.addr(0, y);
551 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
552 for (int x = 0; x < a.width(); ++x) {
553 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
554 return false;
555 }
556 }
557 }
558 aptr += a.rowBytes();
559 bptr += b.rowBytes();
560 }
561 return true;
562 }
563
Mike Reed24846602017-12-04 16:06:03 -0500564 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
565 bool respectColorSpaces) {
Mike Reed5a625e02017-08-08 15:48:54 -0400566 SkPixmap pm0, pm1;
Mike Reed24846602017-12-04 16:06:03 -0500567 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
568 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
569 }
570
571 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
572 bool respectColorSpaces) {
573 // ensure that peekPixels will succeed
574 auto imga = a->makeRasterImage();
575 auto imgb = b->makeRasterImage();
576 a = imga.get();
577 b = imgb.get();
578
579 SkPixmap pm0, pm1;
580 return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
581 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
Mike Reed5a625e02017-08-08 15:48:54 -0400582 }
Mike Reed46596ae2018-01-02 15:40:29 -0500583
584 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
585 const SkSurfaceProps* props) {
586 auto surf = canvas->makeSurface(info, props);
587 if (!surf) {
588 surf = SkSurface::MakeRaster(info, props);
589 }
590 return surf;
591 }
tfarina20108912014-06-21 10:54:17 -0700592} // namespace sk_tool_utils