blob: bbb00bc12382118f8611e6e67ff469f5e067596f [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"
Cary Clark992c7b02014-07-31 08:58:44 -040021#include "SkTestScalerContext.h"
joshualitt9e36c1a2015-04-14 12:17:27 -070022#include "SkTextBlob.h"
Cary Clark992c7b02014-07-31 08:58:44 -040023
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000024namespace sk_tool_utils {
25
Mike Kleinb251b722017-11-13 11:03:16 -050026static const char* platform_os_name() {
27 for (int index = 0; index < FLAGS_key.count(); index += 2) {
28 if (!strcmp("os", FLAGS_key[index])) {
29 return FLAGS_key[index + 1];
30 }
31 }
32 return "";
33}
34
bungeman13b9c952016-05-12 10:09:30 -070035sk_sp<SkTypeface> emoji_typeface() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050036#if defined(SK_BUILD_FOR_WIN)
37 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
38 const char *colorEmojiFontName = "Segoe UI Emoji";
39 sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle()));
40 if (typeface) {
41 return typeface;
caryclarkc3dcb672015-07-21 12:27:36 -070042 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050043 sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter(
44 colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */,
45 0x1f4b0 /* character: πŸ’° */));
46 if (fallback) {
47 return fallback;
caryclarkc3dcb672015-07-21 12:27:36 -070048 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050049 // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol.
50 // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji.
51 return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle());
52
53#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
54 return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle());
55
56#else
Hal Canary53e5e7d2017-12-08 14:25:14 -050057 return MakeResourceAsTypeface("fonts/Funkster.ttf");
Mike Kleinf436fbc2017-11-15 10:42:46 -050058
59#endif
caryclarkc3dcb672015-07-21 12:27:36 -070060}
61
62const char* emoji_sample_text() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050063#if defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
64 return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // πŸ’°πŸ‘πŸŽ…
65 "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πŸͺπŸ•πŸš€
66 "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // πŸš»πŸ’©πŸ“·
67 "\xF0\x9F\x93\xA6" // πŸ“¦
68 "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πŸ‡ΊπŸ‡ΈπŸ‡¦
69#else
70 return "Hamburgefons";
71#endif
caryclarkc3dcb672015-07-21 12:27:36 -070072}
73
Mike Kleinb251b722017-11-13 11:03:16 -050074static bool extra_config_contains(const char* substring) {
caryclark6531c362015-07-20 13:38:56 -070075 for (int index = 0; index < FLAGS_key.count(); index += 2) {
Mike Kleinb251b722017-11-13 11:03:16 -050076 if (0 == strcmp("extra_config", FLAGS_key[index])
77 && strstr(FLAGS_key[index + 1], substring)) {
78 return true;
caryclark6531c362015-07-20 13:38:56 -070079 }
80 }
Mike Kleinb251b722017-11-13 11:03:16 -050081 return false;
82}
83
84const char* platform_font_manager() {
85 if (extra_config_contains("GDI")) {
86 return "GDI";
87 }
88 if (extra_config_contains("NativeFonts")){
89 return platform_os_name();
90 }
caryclark6531c362015-07-20 13:38:56 -070091 return "";
92}
93
caryclark6531c362015-07-20 13:38:56 -070094
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000095const char* colortype_name(SkColorType ct) {
96 switch (ct) {
97 case kUnknown_SkColorType: return "Unknown";
98 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000099 case kARGB_4444_SkColorType: return "ARGB_4444";
100 case kRGB_565_SkColorType: return "RGB_565";
101 case kRGBA_8888_SkColorType: return "RGBA_8888";
102 case kBGRA_8888_SkColorType: return "BGRA_8888";
Brian Osmanf750fbc2017-02-08 10:47:28 -0500103 case kRGBA_F16_SkColorType: return "RGBA_F16";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000104 default:
105 SkASSERT(false);
106 return "unexpected colortype";
107 }
108}
109
caryclark65cdba62015-06-15 06:51:08 -0700110SkColor color_to_565(SkColor color) {
111 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -0700112 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -0700113 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -0700114}
115
mbocee6a9912016-05-31 11:42:36 -0700116sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -0700117 return create_font(name, style);
118}
119
mbocee6a9912016-05-31 11:42:36 -0700120void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) {
bungeman13b9c952016-05-12 10:09:30 -0700121 paint->setTypeface(create_font(name, style));
caryclark83ca6282015-06-10 09:31:09 -0700122}
halcanary9d524f22016-03-29 09:03:52 -0700123
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000124void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
125 SkColorType colorType, SkAlphaType alphaType) {
126 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -0700127 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000128
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000129 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
130}
131
reed8a21c9f2016-03-08 18:50:00 -0800132sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -0800133 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -0700134 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -0800135 bm.eraseColor(c1);
136 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
137 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -0800138 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -0800139 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
140}
141
robertphillips943a4622015-09-03 13:32:33 -0700142SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
143 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700144 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700145 SkCanvas canvas(bitmap);
146
147 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
148 return bitmap;
149}
150
halcanaryb0cce2c2015-01-26 12:49:00 -0800151void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
152 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800153 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700154 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800155 canvas->drawPaint(paint);
156}
157
robertphillips943a4622015-09-03 13:32:33 -0700158SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
159 int textSize, const char* str) {
160 SkBitmap bitmap;
161 bitmap.allocN32Pixels(w, h);
162 SkCanvas canvas(bitmap);
163
164 SkPaint paint;
165 paint.setAntiAlias(true);
166 sk_tool_utils::set_portable_typeface(&paint);
167 paint.setColor(c);
168 paint.setTextSize(SkIntToScalar(textSize));
169
170 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400171 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700172
Brian Osman2b25d342016-12-20 11:09:31 -0500173 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
174 // will process this correctly but legacy configs will render as if this returned N32.
175 SkBitmap result;
176 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
177 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
178 return result;
robertphillips943a4622015-09-03 13:32:33 -0700179}
180
Robert Phillips4c72b262017-08-15 13:28:42 -0400181void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
182 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700183 SkPaint paint(origPaint);
184 SkTDArray<uint16_t> glyphs;
185
halcanary96fcdcc2015-08-27 07:41:13 -0700186 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700187 paint.textToGlyphs(text, len, glyphs.begin());
188
189 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
190 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700191 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700192 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
193}
194
Robert Phillips4c72b262017-08-15 13:28:42 -0400195void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
196 const SkPaint& origPaint, SkScalar x, SkScalar y) {
197 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
198}
199
Chris Dalton7c02cc72017-11-06 14:10:54 -0700200SkPath make_star(const SkRect& bounds, int numPts, int step) {
201 SkPath path;
202 path.setFillType(SkPath::kEvenOdd_FillType);
203 path.moveTo(0,-1);
204 for (int i = 1; i < numPts; ++i) {
205 int idx = i*step;
206 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
207 SkScalar x = SkScalarCos(theta);
208 SkScalar y = -SkScalarSin(theta);
209 path.lineTo(x, y);
210 }
211 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
212 return path;
213}
214
Mike Kleinc722f792017-07-31 11:57:21 -0400215#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400216 // MSVC takes ~2 minutes to compile this function with optimization.
217 // We don't really care to wait that long for this function.
218 #pragma optimize("", off)
219#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700220void make_big_path(SkPath& path) {
221 #include "BigPathBench.inc"
222}
223
robertphillips9c4909b2015-10-19 06:39:17 -0700224static float gaussian2d_value(int x, int y, float sigma) {
225 // don't bother with the scale term since we're just going to normalize the
226 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700227 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700228 return temp;
229}
230
231static float* create_2d_kernel(float sigma, int* filterSize) {
232 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
233 // sizes are always odd)
234 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
235 int wh = *filterSize = 2*halfFilterSize + 1;
236
237 float* temp = new float[wh*wh];
238
239 float filterTot = 0.0f;
240 for (int yOff = 0; yOff < wh; ++yOff) {
241 for (int xOff = 0; xOff < wh; ++xOff) {
242 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
243
244 filterTot += temp[yOff*wh+xOff];
245 }
246 }
247
248 // normalize the kernel
249 for (int yOff = 0; yOff < wh; ++yOff) {
250 for (int xOff = 0; xOff < wh; ++xOff) {
251 temp[yOff*wh+xOff] /= filterTot;
252 }
253 }
254
255 return temp;
256}
257
258static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
259 SkASSERT(wh & 0x1);
260
261 int halfFilterSize = (wh-1)/2;
262
263 float r = 0.0f, g = 0.0f, b = 0.0f;
264 for (int yOff = 0; yOff < wh; ++yOff) {
265 int ySamp = y + yOff - halfFilterSize;
266
267 if (ySamp < 0) {
268 ySamp = 0;
269 } else if (ySamp > bm.height()-1) {
270 ySamp = bm.height()-1;
271 }
272
273 for (int xOff = 0; xOff < wh; ++xOff) {
274 int xSamp = x + xOff - halfFilterSize;
275
276 if (xSamp < 0) {
277 xSamp = 0;
278 } else if (xSamp > bm.width()-1) {
279 xSamp = bm.width()-1;
280 }
281
282 float filter = kernel[yOff*wh + xOff];
283
284 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
285
286 r += SkGetPackedR32(c) * filter;
287 g += SkGetPackedG32(c) * filter;
288 b += SkGetPackedB32(c) * filter;
289 }
290 }
291
292 U8CPU r8, g8, b8;
293
294 r8 = (U8CPU) (r+0.5f);
295 g8 = (U8CPU) (g+0.5f);
296 b8 = (U8CPU) (b+0.5f);
297
298 return SkPackARGB32(255, r8, g8, b8);
299}
300
301SkBitmap slow_blur(const SkBitmap& src, float sigma) {
302 SkBitmap dst;
303
304 dst.allocN32Pixels(src.width(), src.height(), true);
305
306 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400307 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700308
309 for (int y = 0; y < src.height(); ++y) {
310 for (int x = 0; x < src.width(); ++x) {
311 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
312 }
313 }
halcanary9d524f22016-03-29 09:03:52 -0700314
robertphillips9c4909b2015-10-19 06:39:17 -0700315 return dst;
316}
317
robertphillips276d3282016-08-04 09:03:19 -0700318// compute the intersection point between the diagonal and the ellipse in the
319// lower right corner
320static SkPoint intersection(SkScalar w, SkScalar h) {
321 SkASSERT(w > 0.0f || h > 0.0f);
322
323 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
324}
325
326// Use the intersection of the corners' diagonals with their ellipses to shrink
327// the bounding rect
328SkRect compute_central_occluder(const SkRRect& rr) {
329 const SkRect r = rr.getBounds();
330
331 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
332
333 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
334 if (!radii.isZero()) {
335 SkPoint p = intersection(radii.fX, radii.fY);
336
337 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
338 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
339 }
340
341 radii = rr.radii(SkRRect::kUpperRight_Corner);
342 if (!radii.isZero()) {
343 SkPoint p = intersection(radii.fX, radii.fY);
344
345 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
346 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
347 }
348
349 radii = rr.radii(SkRRect::kLowerRight_Corner);
350 if (!radii.isZero()) {
351 SkPoint p = intersection(radii.fX, radii.fY);
352
353 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
354 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
355 }
356
357 radii = rr.radii(SkRRect::kLowerLeft_Corner);
358 if (!radii.isZero()) {
359 SkPoint p = intersection(radii.fX, radii.fY);
360
361 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
362 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
363 }
364
365 return SkRect::MakeLTRB(newL, newT, newR, newB);
366}
367
robertphillips401c1962016-08-04 12:35:46 -0700368// The widest inset rect
369SkRect compute_widest_occluder(const SkRRect& rr) {
370 const SkRect& r = rr.getBounds();
371
372 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
373 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
374 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
375 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
376
377 SkScalar maxT = SkTMax(ul.fY, ur.fY);
378 SkScalar maxB = SkTMax(ll.fY, lr.fY);
379
380 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
381
382}
383
384// The tallest inset rect
385SkRect compute_tallest_occluder(const SkRRect& rr) {
386 const SkRect& r = rr.getBounds();
387
388 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
389 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
390 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
391 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
392
393 SkScalar maxL = SkTMax(ul.fX, ll.fX);
394 SkScalar maxR = SkTMax(ur.fX, lr.fX);
395
396 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
397}
398
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400399bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
400 SkPixmap srcPM;
401 if (!src.peekPixels(&srcPM)) {
402 return false;
403 }
404
405 SkBitmap tmpDst;
406 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
407 if (!tmpDst.setInfo(dstInfo)) {
408 return false;
409 }
410
Mike Reed304a07c2017-07-12 15:10:28 -0400411 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400412 return false;
413 }
414
415 SkPixmap dstPM;
416 if (!tmpDst.peekPixels(&dstPM)) {
417 return false;
418 }
419
420 if (!srcPM.readPixels(dstPM)) {
421 return false;
422 }
423
424 dst->swap(tmpDst);
425 return true;
426}
427
Matt Sarett4897fb82017-01-18 11:49:33 -0500428void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
429 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
430 kRGBA_8888_SkColorType == src.colorType());
431
432 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
433 dst->allocPixels(grayInfo);
434 uint8_t* dst8 = (uint8_t*)dst->getPixels();
435 const uint32_t* src32 = (const uint32_t*)src.getPixels();
436
437 const int w = src.width();
438 const int h = src.height();
439 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
440 for (int y = 0; y < h; ++y) {
441 if (isBGRA) {
442 // BGRA
443 for (int x = 0; x < w; ++x) {
444 uint32_t s = src32[x];
445 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
446 }
447 } else {
448 // RGBA
449 for (int x = 0; x < w; ++x) {
450 uint32_t s = src32[x];
451 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
452 }
453 }
454 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
455 dst8 += dst->rowBytes();
456 }
457}
robertphillips401c1962016-08-04 12:35:46 -0700458
Mike Reed5a625e02017-08-08 15:48:54 -0400459 //////////////////////////////////////////////////////////////////////////////////////////////
460
461 static int scale255(float x) {
462 return sk_float_round2int(x * 255);
463 }
464
465 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
466 int dr = 0,
467 dg = 0,
468 db = 0,
469 da = 0;
470 switch (ct) {
471 case kRGBA_8888_SkColorType:
472 case kBGRA_8888_SkColorType: {
473 SkPMColor c0 = *(const SkPMColor*)a;
474 SkPMColor c1 = *(const SkPMColor*)b;
475 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
476 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
477 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
478 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
479 } break;
480 case kRGB_565_SkColorType: {
481 uint16_t c0 = *(const uint16_t*)a;
482 uint16_t c1 = *(const uint16_t*)b;
483 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
484 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
485 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
486 } break;
487 case kARGB_4444_SkColorType: {
488 uint16_t c0 = *(const uint16_t*)a;
489 uint16_t c1 = *(const uint16_t*)b;
490 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
491 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
492 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
493 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
494 } break;
495 case kAlpha_8_SkColorType:
496 case kGray_8_SkColorType:
497 da = (const uint8_t*)a - (const uint8_t*)b;
498 break;
499 case kRGBA_F16_SkColorType: {
500 const SkPM4f* c0 = (const SkPM4f*)a;
501 const SkPM4f* c1 = (const SkPM4f*)b;
502 dr = scale255(c0->r() - c1->r());
503 dg = scale255(c0->g() - c1->g());
504 db = scale255(c0->b() - c1->b());
505 da = scale255(c0->a() - c1->a());
506 } break;
507 default:
508 return 0;
509 }
510 dr = SkAbs32(dr);
511 dg = SkAbs32(dg);
512 db = SkAbs32(db);
513 da = SkAbs32(da);
514 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
515 }
516
Mike Reed24846602017-12-04 16:06:03 -0500517 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
518 bool respectColorSpace) {
Mike Reed5a625e02017-08-08 15:48:54 -0400519 if (a.width() != b.width() ||
520 a.height() != b.height() ||
521 a.colorType() != b.colorType() ||
Mike Reed24846602017-12-04 16:06:03 -0500522 (respectColorSpace && (a.colorSpace() != b.colorSpace())))
Mike Reed5a625e02017-08-08 15:48:54 -0400523 {
524 return false;
525 }
526
527 for (int y = 0; y < a.height(); ++y) {
528 const char* aptr = (const char*)a.addr(0, y);
529 const char* bptr = (const char*)b.addr(0, y);
530 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
531 for (int x = 0; x < a.width(); ++x) {
532 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
533 return false;
534 }
535 }
536 }
537 aptr += a.rowBytes();
538 bptr += b.rowBytes();
539 }
540 return true;
541 }
542
Mike Reed24846602017-12-04 16:06:03 -0500543 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
544 bool respectColorSpaces) {
Mike Reed5a625e02017-08-08 15:48:54 -0400545 SkPixmap pm0, pm1;
Mike Reed24846602017-12-04 16:06:03 -0500546 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
547 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
548 }
549
550 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
551 bool respectColorSpaces) {
552 // ensure that peekPixels will succeed
553 auto imga = a->makeRasterImage();
554 auto imgb = b->makeRasterImage();
555 a = imga.get();
556 b = imgb.get();
557
558 SkPixmap pm0, pm1;
559 return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
560 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
Mike Reed5a625e02017-08-08 15:48:54 -0400561 }
tfarina20108912014-06-21 10:54:17 -0700562} // namespace sk_tool_utils