blob: 5321513a171c9506d62bf843efe30eeb9fab7159 [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"
Brian Osman2b25d342016-12-20 11:09:31 -050016#include "SkPixelRef.h"
Mike Reed5a625e02017-08-08 15:48:54 -040017#include "SkPM4f.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
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000023namespace sk_tool_utils {
24
Mike Kleinb251b722017-11-13 11:03:16 -050025static const char* platform_os_name() {
26 for (int index = 0; index < FLAGS_key.count(); index += 2) {
27 if (!strcmp("os", FLAGS_key[index])) {
28 return FLAGS_key[index + 1];
29 }
30 }
31 return "";
32}
33
bungeman13b9c952016-05-12 10:09:30 -070034sk_sp<SkTypeface> emoji_typeface() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050035#if defined(SK_BUILD_FOR_WIN)
36 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
37 const char *colorEmojiFontName = "Segoe UI Emoji";
38 sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle()));
39 if (typeface) {
40 return typeface;
caryclarkc3dcb672015-07-21 12:27:36 -070041 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050042 sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter(
43 colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */,
44 0x1f4b0 /* character: πŸ’° */));
45 if (fallback) {
46 return fallback;
caryclarkc3dcb672015-07-21 12:27:36 -070047 }
Mike Kleinf436fbc2017-11-15 10:42:46 -050048 // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol.
49 // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji.
50 return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle());
51
52#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
53 return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle());
54
55#else
56 return MakeResourceAsTypeface("/fonts/Funkster.ttf");
57
58#endif
caryclarkc3dcb672015-07-21 12:27:36 -070059}
60
61const char* emoji_sample_text() {
Mike Kleinf436fbc2017-11-15 10:42:46 -050062#if defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
63 return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // πŸ’°πŸ‘πŸŽ…
64 "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // πŸͺπŸ•πŸš€
65 "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // πŸš»πŸ’©πŸ“·
66 "\xF0\x9F\x93\xA6" // πŸ“¦
67 "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // πŸ‡ΊπŸ‡ΈπŸ‡¦
68#else
69 return "Hamburgefons";
70#endif
caryclarkc3dcb672015-07-21 12:27:36 -070071}
72
Mike Kleinb251b722017-11-13 11:03:16 -050073static bool extra_config_contains(const char* substring) {
caryclark6531c362015-07-20 13:38:56 -070074 for (int index = 0; index < FLAGS_key.count(); index += 2) {
Mike Kleinb251b722017-11-13 11:03:16 -050075 if (0 == strcmp("extra_config", FLAGS_key[index])
76 && strstr(FLAGS_key[index + 1], substring)) {
77 return true;
caryclark6531c362015-07-20 13:38:56 -070078 }
79 }
Mike Kleinb251b722017-11-13 11:03:16 -050080 return false;
81}
82
83const char* platform_font_manager() {
84 if (extra_config_contains("GDI")) {
85 return "GDI";
86 }
87 if (extra_config_contains("NativeFonts")){
88 return platform_os_name();
89 }
caryclark6531c362015-07-20 13:38:56 -070090 return "";
91}
92
caryclark6531c362015-07-20 13:38:56 -070093
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000094const char* colortype_name(SkColorType ct) {
95 switch (ct) {
96 case kUnknown_SkColorType: return "Unknown";
97 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000098 case kARGB_4444_SkColorType: return "ARGB_4444";
99 case kRGB_565_SkColorType: return "RGB_565";
100 case kRGBA_8888_SkColorType: return "RGBA_8888";
101 case kBGRA_8888_SkColorType: return "BGRA_8888";
Brian Osmanf750fbc2017-02-08 10:47:28 -0500102 case kRGBA_F16_SkColorType: return "RGBA_F16";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000103 default:
104 SkASSERT(false);
105 return "unexpected colortype";
106 }
107}
108
caryclark65cdba62015-06-15 06:51:08 -0700109SkColor color_to_565(SkColor color) {
110 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -0700111 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -0700112 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -0700113}
114
mbocee6a9912016-05-31 11:42:36 -0700115sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -0700116 return create_font(name, style);
117}
118
mbocee6a9912016-05-31 11:42:36 -0700119void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) {
bungeman13b9c952016-05-12 10:09:30 -0700120 paint->setTypeface(create_font(name, style));
caryclark83ca6282015-06-10 09:31:09 -0700121}
halcanary9d524f22016-03-29 09:03:52 -0700122
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000123void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
124 SkColorType colorType, SkAlphaType alphaType) {
125 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -0700126 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000127
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000128 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
129}
130
reed8a21c9f2016-03-08 18:50:00 -0800131sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -0800132 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -0700133 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -0800134 bm.eraseColor(c1);
135 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
136 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -0800137 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -0800138 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
139}
140
robertphillips943a4622015-09-03 13:32:33 -0700141SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
142 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700143 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700144 SkCanvas canvas(bitmap);
145
146 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
147 return bitmap;
148}
149
halcanaryb0cce2c2015-01-26 12:49:00 -0800150void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
151 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800152 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700153 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800154 canvas->drawPaint(paint);
155}
156
robertphillips943a4622015-09-03 13:32:33 -0700157SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
158 int textSize, const char* str) {
159 SkBitmap bitmap;
160 bitmap.allocN32Pixels(w, h);
161 SkCanvas canvas(bitmap);
162
163 SkPaint paint;
164 paint.setAntiAlias(true);
165 sk_tool_utils::set_portable_typeface(&paint);
166 paint.setColor(c);
167 paint.setTextSize(SkIntToScalar(textSize));
168
169 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400170 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700171
Brian Osman2b25d342016-12-20 11:09:31 -0500172 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
173 // will process this correctly but legacy configs will render as if this returned N32.
174 SkBitmap result;
175 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
176 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
177 return result;
robertphillips943a4622015-09-03 13:32:33 -0700178}
179
Robert Phillips4c72b262017-08-15 13:28:42 -0400180void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
181 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700182 SkPaint paint(origPaint);
183 SkTDArray<uint16_t> glyphs;
184
halcanary96fcdcc2015-08-27 07:41:13 -0700185 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700186 paint.textToGlyphs(text, len, glyphs.begin());
187
188 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
189 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700190 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700191 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
192}
193
Robert Phillips4c72b262017-08-15 13:28:42 -0400194void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
195 const SkPaint& origPaint, SkScalar x, SkScalar y) {
196 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
197}
198
Chris Dalton7c02cc72017-11-06 14:10:54 -0700199SkPath make_star(const SkRect& bounds, int numPts, int step) {
200 SkPath path;
201 path.setFillType(SkPath::kEvenOdd_FillType);
202 path.moveTo(0,-1);
203 for (int i = 1; i < numPts; ++i) {
204 int idx = i*step;
205 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
206 SkScalar x = SkScalarCos(theta);
207 SkScalar y = -SkScalarSin(theta);
208 path.lineTo(x, y);
209 }
210 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
211 return path;
212}
213
Mike Kleinc722f792017-07-31 11:57:21 -0400214#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400215 // MSVC takes ~2 minutes to compile this function with optimization.
216 // We don't really care to wait that long for this function.
217 #pragma optimize("", off)
218#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700219void make_big_path(SkPath& path) {
220 #include "BigPathBench.inc"
221}
222
robertphillips9c4909b2015-10-19 06:39:17 -0700223static float gaussian2d_value(int x, int y, float sigma) {
224 // don't bother with the scale term since we're just going to normalize the
225 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700226 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700227 return temp;
228}
229
230static float* create_2d_kernel(float sigma, int* filterSize) {
231 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
232 // sizes are always odd)
233 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
234 int wh = *filterSize = 2*halfFilterSize + 1;
235
236 float* temp = new float[wh*wh];
237
238 float filterTot = 0.0f;
239 for (int yOff = 0; yOff < wh; ++yOff) {
240 for (int xOff = 0; xOff < wh; ++xOff) {
241 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
242
243 filterTot += temp[yOff*wh+xOff];
244 }
245 }
246
247 // normalize the kernel
248 for (int yOff = 0; yOff < wh; ++yOff) {
249 for (int xOff = 0; xOff < wh; ++xOff) {
250 temp[yOff*wh+xOff] /= filterTot;
251 }
252 }
253
254 return temp;
255}
256
257static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
258 SkASSERT(wh & 0x1);
259
260 int halfFilterSize = (wh-1)/2;
261
262 float r = 0.0f, g = 0.0f, b = 0.0f;
263 for (int yOff = 0; yOff < wh; ++yOff) {
264 int ySamp = y + yOff - halfFilterSize;
265
266 if (ySamp < 0) {
267 ySamp = 0;
268 } else if (ySamp > bm.height()-1) {
269 ySamp = bm.height()-1;
270 }
271
272 for (int xOff = 0; xOff < wh; ++xOff) {
273 int xSamp = x + xOff - halfFilterSize;
274
275 if (xSamp < 0) {
276 xSamp = 0;
277 } else if (xSamp > bm.width()-1) {
278 xSamp = bm.width()-1;
279 }
280
281 float filter = kernel[yOff*wh + xOff];
282
283 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
284
285 r += SkGetPackedR32(c) * filter;
286 g += SkGetPackedG32(c) * filter;
287 b += SkGetPackedB32(c) * filter;
288 }
289 }
290
291 U8CPU r8, g8, b8;
292
293 r8 = (U8CPU) (r+0.5f);
294 g8 = (U8CPU) (g+0.5f);
295 b8 = (U8CPU) (b+0.5f);
296
297 return SkPackARGB32(255, r8, g8, b8);
298}
299
300SkBitmap slow_blur(const SkBitmap& src, float sigma) {
301 SkBitmap dst;
302
303 dst.allocN32Pixels(src.width(), src.height(), true);
304
305 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400306 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700307
308 for (int y = 0; y < src.height(); ++y) {
309 for (int x = 0; x < src.width(); ++x) {
310 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
311 }
312 }
halcanary9d524f22016-03-29 09:03:52 -0700313
robertphillips9c4909b2015-10-19 06:39:17 -0700314 return dst;
315}
316
robertphillips276d3282016-08-04 09:03:19 -0700317// compute the intersection point between the diagonal and the ellipse in the
318// lower right corner
319static SkPoint intersection(SkScalar w, SkScalar h) {
320 SkASSERT(w > 0.0f || h > 0.0f);
321
322 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
323}
324
325// Use the intersection of the corners' diagonals with their ellipses to shrink
326// the bounding rect
327SkRect compute_central_occluder(const SkRRect& rr) {
328 const SkRect r = rr.getBounds();
329
330 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
331
332 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
333 if (!radii.isZero()) {
334 SkPoint p = intersection(radii.fX, radii.fY);
335
336 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
337 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
338 }
339
340 radii = rr.radii(SkRRect::kUpperRight_Corner);
341 if (!radii.isZero()) {
342 SkPoint p = intersection(radii.fX, radii.fY);
343
344 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
345 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
346 }
347
348 radii = rr.radii(SkRRect::kLowerRight_Corner);
349 if (!radii.isZero()) {
350 SkPoint p = intersection(radii.fX, radii.fY);
351
352 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
353 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
354 }
355
356 radii = rr.radii(SkRRect::kLowerLeft_Corner);
357 if (!radii.isZero()) {
358 SkPoint p = intersection(radii.fX, radii.fY);
359
360 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
361 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
362 }
363
364 return SkRect::MakeLTRB(newL, newT, newR, newB);
365}
366
robertphillips401c1962016-08-04 12:35:46 -0700367// The widest inset rect
368SkRect compute_widest_occluder(const SkRRect& rr) {
369 const SkRect& r = rr.getBounds();
370
371 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
372 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
373 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
374 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
375
376 SkScalar maxT = SkTMax(ul.fY, ur.fY);
377 SkScalar maxB = SkTMax(ll.fY, lr.fY);
378
379 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
380
381}
382
383// The tallest inset rect
384SkRect compute_tallest_occluder(const SkRRect& rr) {
385 const SkRect& r = rr.getBounds();
386
387 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
388 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
389 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
390 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
391
392 SkScalar maxL = SkTMax(ul.fX, ll.fX);
393 SkScalar maxR = SkTMax(ur.fX, lr.fX);
394
395 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
396}
397
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400398bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
399 SkPixmap srcPM;
400 if (!src.peekPixels(&srcPM)) {
401 return false;
402 }
403
404 SkBitmap tmpDst;
405 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
406 if (!tmpDst.setInfo(dstInfo)) {
407 return false;
408 }
409
Mike Reed304a07c2017-07-12 15:10:28 -0400410 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400411 return false;
412 }
413
414 SkPixmap dstPM;
415 if (!tmpDst.peekPixels(&dstPM)) {
416 return false;
417 }
418
419 if (!srcPM.readPixels(dstPM)) {
420 return false;
421 }
422
423 dst->swap(tmpDst);
424 return true;
425}
426
Matt Sarett4897fb82017-01-18 11:49:33 -0500427void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
428 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
429 kRGBA_8888_SkColorType == src.colorType());
430
431 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
432 dst->allocPixels(grayInfo);
433 uint8_t* dst8 = (uint8_t*)dst->getPixels();
434 const uint32_t* src32 = (const uint32_t*)src.getPixels();
435
436 const int w = src.width();
437 const int h = src.height();
438 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
439 for (int y = 0; y < h; ++y) {
440 if (isBGRA) {
441 // BGRA
442 for (int x = 0; x < w; ++x) {
443 uint32_t s = src32[x];
444 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
445 }
446 } else {
447 // RGBA
448 for (int x = 0; x < w; ++x) {
449 uint32_t s = src32[x];
450 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
451 }
452 }
453 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
454 dst8 += dst->rowBytes();
455 }
456}
robertphillips401c1962016-08-04 12:35:46 -0700457
Mike Reed5a625e02017-08-08 15:48:54 -0400458 //////////////////////////////////////////////////////////////////////////////////////////////
459
460 static int scale255(float x) {
461 return sk_float_round2int(x * 255);
462 }
463
464 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
465 int dr = 0,
466 dg = 0,
467 db = 0,
468 da = 0;
469 switch (ct) {
470 case kRGBA_8888_SkColorType:
471 case kBGRA_8888_SkColorType: {
472 SkPMColor c0 = *(const SkPMColor*)a;
473 SkPMColor c1 = *(const SkPMColor*)b;
474 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
475 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
476 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
477 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
478 } break;
479 case kRGB_565_SkColorType: {
480 uint16_t c0 = *(const uint16_t*)a;
481 uint16_t c1 = *(const uint16_t*)b;
482 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
483 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
484 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
485 } break;
486 case kARGB_4444_SkColorType: {
487 uint16_t c0 = *(const uint16_t*)a;
488 uint16_t c1 = *(const uint16_t*)b;
489 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
490 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
491 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
492 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
493 } break;
494 case kAlpha_8_SkColorType:
495 case kGray_8_SkColorType:
496 da = (const uint8_t*)a - (const uint8_t*)b;
497 break;
498 case kRGBA_F16_SkColorType: {
499 const SkPM4f* c0 = (const SkPM4f*)a;
500 const SkPM4f* c1 = (const SkPM4f*)b;
501 dr = scale255(c0->r() - c1->r());
502 dg = scale255(c0->g() - c1->g());
503 db = scale255(c0->b() - c1->b());
504 da = scale255(c0->a() - c1->a());
505 } break;
506 default:
507 return 0;
508 }
509 dr = SkAbs32(dr);
510 dg = SkAbs32(dg);
511 db = SkAbs32(db);
512 da = SkAbs32(da);
513 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
514 }
515
516 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff) {
517 if (a.width() != b.width() ||
518 a.height() != b.height() ||
519 a.colorType() != b.colorType() ||
520 a.colorSpace() != b.colorSpace())
521 {
522 return false;
523 }
524
525 for (int y = 0; y < a.height(); ++y) {
526 const char* aptr = (const char*)a.addr(0, y);
527 const char* bptr = (const char*)b.addr(0, y);
528 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
529 for (int x = 0; x < a.width(); ++x) {
530 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
531 return false;
532 }
533 }
534 }
535 aptr += a.rowBytes();
536 bptr += b.rowBytes();
537 }
538 return true;
539 }
540
541 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff) {
542 SkPixmap pm0, pm1;
543 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1, maxDiff);
544 }
tfarina20108912014-06-21 10:54:17 -0700545} // namespace sk_tool_utils