blob: 99b03a43914fe9a282d9710e38192dface370999 [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
Ben Wagner3560b582018-03-27 18:15:53 +00008#include "SkBitmap.h"
Ben Wagner97182cc2018-02-15 10:20:04 -05009#include "SkBlendMode.h"
Ben Wagner3560b582018-03-27 18:15:53 +000010#include "SkCanvas.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050011#include "SkColorData.h"
12#include "SkColorPriv.h"
13#include "SkFloatingPoint.h"
Ben Wagner3560b582018-03-27 18:15:53 +000014#include "SkImage.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050015#include "SkMatrix.h"
Ben Wagner3560b582018-03-27 18:15:53 +000016#include "SkPM4f.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050017#include "SkPaint.h"
18#include "SkPath.h"
19#include "SkPixelRef.h"
20#include "SkPixmap.h"
21#include "SkPoint.h"
22#include "SkRRect.h"
Ben Wagner3560b582018-03-27 18:15:53 +000023#include "SkShader.h"
24#include "SkSurface.h"
Ben Wagner3560b582018-03-27 18:15:53 +000025#include "SkTextBlob.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050026#include "sk_tool_utils.h"
27
28#include <cmath>
29#include <cstring>
30#include <memory>
Cary Clark992c7b02014-07-31 08:58:44 -040031
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000032namespace sk_tool_utils {
33
Mike Reedd4746982018-02-07 16:05:29 -050034const char* alphatype_name(SkAlphaType at) {
35 switch (at) {
36 case kUnknown_SkAlphaType: return "Unknown";
37 case kOpaque_SkAlphaType: return "Opaque";
38 case kPremul_SkAlphaType: return "Premul";
39 case kUnpremul_SkAlphaType: return "Unpremul";
40 }
41 SkASSERT(false);
42 return "unexpected alphatype";
43}
44
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000045const char* colortype_name(SkColorType ct) {
46 switch (ct) {
47 case kUnknown_SkColorType: return "Unknown";
48 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000049 case kRGB_565_SkColorType: return "RGB_565";
Mike Reedd4746982018-02-07 16:05:29 -050050 case kARGB_4444_SkColorType: return "ARGB_4444";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000051 case kRGBA_8888_SkColorType: return "RGBA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050052 case kRGB_888x_SkColorType: return "RGB_888x";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000053 case kBGRA_8888_SkColorType: return "BGRA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050054 case kRGBA_1010102_SkColorType: return "RGBA_1010102";
55 case kRGB_101010x_SkColorType: return "RGB_101010x";
56 case kGray_8_SkColorType: return "Gray_8";
Brian Osmanf750fbc2017-02-08 10:47:28 -050057 case kRGBA_F16_SkColorType: return "RGBA_F16";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000058 }
Mike Reedd4746982018-02-07 16:05:29 -050059 SkASSERT(false);
60 return "unexpected colortype";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000061}
62
caryclark65cdba62015-06-15 06:51:08 -070063SkColor color_to_565(SkColor color) {
64 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -070065 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -070066 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -070067}
68
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000069void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
70 SkColorType colorType, SkAlphaType alphaType) {
71 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -070072 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000073
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000074 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
75}
76
Mike Reed4c790bd2018-02-08 14:10:40 -050077void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
78 SkColorType colorType, SkAlphaType alphaType) {
79 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
80 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
81}
82
reed8a21c9f2016-03-08 18:50:00 -080083sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -080084 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -070085 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -080086 bm.eraseColor(c1);
87 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
88 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -080089 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -080090 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
91}
92
robertphillips943a4622015-09-03 13:32:33 -070093SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
94 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -070095 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -070096 SkCanvas canvas(bitmap);
97
98 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
99 return bitmap;
100}
101
halcanaryb0cce2c2015-01-26 12:49:00 -0800102void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
103 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800104 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700105 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800106 canvas->drawPaint(paint);
107}
108
robertphillips943a4622015-09-03 13:32:33 -0700109SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
110 int textSize, const char* str) {
111 SkBitmap bitmap;
112 bitmap.allocN32Pixels(w, h);
113 SkCanvas canvas(bitmap);
114
115 SkPaint paint;
116 paint.setAntiAlias(true);
117 sk_tool_utils::set_portable_typeface(&paint);
118 paint.setColor(c);
119 paint.setTextSize(SkIntToScalar(textSize));
120
121 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400122 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700123
Brian Osman2b25d342016-12-20 11:09:31 -0500124 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
125 // will process this correctly but legacy configs will render as if this returned N32.
126 SkBitmap result;
127 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
128 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
129 return result;
robertphillips943a4622015-09-03 13:32:33 -0700130}
131
Robert Phillips4c72b262017-08-15 13:28:42 -0400132void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
133 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700134 SkPaint paint(origPaint);
135 SkTDArray<uint16_t> glyphs;
136
halcanary96fcdcc2015-08-27 07:41:13 -0700137 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700138 paint.textToGlyphs(text, len, glyphs.begin());
139
140 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
141 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700142 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700143 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
144}
145
Robert Phillips4c72b262017-08-15 13:28:42 -0400146void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
147 const SkPaint& origPaint, SkScalar x, SkScalar y) {
148 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
149}
150
Chris Dalton7c02cc72017-11-06 14:10:54 -0700151SkPath make_star(const SkRect& bounds, int numPts, int step) {
152 SkPath path;
153 path.setFillType(SkPath::kEvenOdd_FillType);
154 path.moveTo(0,-1);
155 for (int i = 1; i < numPts; ++i) {
156 int idx = i*step;
157 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
158 SkScalar x = SkScalarCos(theta);
159 SkScalar y = -SkScalarSin(theta);
160 path.lineTo(x, y);
161 }
162 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
163 return path;
164}
165
Mike Kleinc722f792017-07-31 11:57:21 -0400166#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400167 // MSVC takes ~2 minutes to compile this function with optimization.
168 // We don't really care to wait that long for this function.
169 #pragma optimize("", off)
170#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700171void make_big_path(SkPath& path) {
Ben Wagner97182cc2018-02-15 10:20:04 -0500172 #include "BigPathBench.inc" // IWYU pragma: keep
joshualitt98d2e2f2015-10-05 07:23:30 -0700173}
174
robertphillips9c4909b2015-10-19 06:39:17 -0700175static float gaussian2d_value(int x, int y, float sigma) {
176 // don't bother with the scale term since we're just going to normalize the
177 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700178 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700179 return temp;
180}
181
182static float* create_2d_kernel(float sigma, int* filterSize) {
183 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
184 // sizes are always odd)
185 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
186 int wh = *filterSize = 2*halfFilterSize + 1;
187
188 float* temp = new float[wh*wh];
189
190 float filterTot = 0.0f;
191 for (int yOff = 0; yOff < wh; ++yOff) {
192 for (int xOff = 0; xOff < wh; ++xOff) {
193 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
194
195 filterTot += temp[yOff*wh+xOff];
196 }
197 }
198
199 // normalize the kernel
200 for (int yOff = 0; yOff < wh; ++yOff) {
201 for (int xOff = 0; xOff < wh; ++xOff) {
202 temp[yOff*wh+xOff] /= filterTot;
203 }
204 }
205
206 return temp;
207}
208
209static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
210 SkASSERT(wh & 0x1);
211
212 int halfFilterSize = (wh-1)/2;
213
214 float r = 0.0f, g = 0.0f, b = 0.0f;
215 for (int yOff = 0; yOff < wh; ++yOff) {
216 int ySamp = y + yOff - halfFilterSize;
217
218 if (ySamp < 0) {
219 ySamp = 0;
220 } else if (ySamp > bm.height()-1) {
221 ySamp = bm.height()-1;
222 }
223
224 for (int xOff = 0; xOff < wh; ++xOff) {
225 int xSamp = x + xOff - halfFilterSize;
226
227 if (xSamp < 0) {
228 xSamp = 0;
229 } else if (xSamp > bm.width()-1) {
230 xSamp = bm.width()-1;
231 }
232
233 float filter = kernel[yOff*wh + xOff];
234
235 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
236
237 r += SkGetPackedR32(c) * filter;
238 g += SkGetPackedG32(c) * filter;
239 b += SkGetPackedB32(c) * filter;
240 }
241 }
242
243 U8CPU r8, g8, b8;
244
245 r8 = (U8CPU) (r+0.5f);
246 g8 = (U8CPU) (g+0.5f);
247 b8 = (U8CPU) (b+0.5f);
248
249 return SkPackARGB32(255, r8, g8, b8);
250}
251
252SkBitmap slow_blur(const SkBitmap& src, float sigma) {
253 SkBitmap dst;
254
255 dst.allocN32Pixels(src.width(), src.height(), true);
256
257 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400258 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700259
260 for (int y = 0; y < src.height(); ++y) {
261 for (int x = 0; x < src.width(); ++x) {
262 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
263 }
264 }
halcanary9d524f22016-03-29 09:03:52 -0700265
robertphillips9c4909b2015-10-19 06:39:17 -0700266 return dst;
267}
268
robertphillips276d3282016-08-04 09:03:19 -0700269// compute the intersection point between the diagonal and the ellipse in the
270// lower right corner
271static SkPoint intersection(SkScalar w, SkScalar h) {
272 SkASSERT(w > 0.0f || h > 0.0f);
273
274 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
275}
276
277// Use the intersection of the corners' diagonals with their ellipses to shrink
278// the bounding rect
279SkRect compute_central_occluder(const SkRRect& rr) {
280 const SkRect r = rr.getBounds();
281
282 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
283
284 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
285 if (!radii.isZero()) {
286 SkPoint p = intersection(radii.fX, radii.fY);
287
288 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
289 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
290 }
291
292 radii = rr.radii(SkRRect::kUpperRight_Corner);
293 if (!radii.isZero()) {
294 SkPoint p = intersection(radii.fX, radii.fY);
295
296 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
297 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
298 }
299
300 radii = rr.radii(SkRRect::kLowerRight_Corner);
301 if (!radii.isZero()) {
302 SkPoint p = intersection(radii.fX, radii.fY);
303
304 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
305 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
306 }
307
308 radii = rr.radii(SkRRect::kLowerLeft_Corner);
309 if (!radii.isZero()) {
310 SkPoint p = intersection(radii.fX, radii.fY);
311
312 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
313 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
314 }
315
316 return SkRect::MakeLTRB(newL, newT, newR, newB);
317}
318
robertphillips401c1962016-08-04 12:35:46 -0700319// The widest inset rect
320SkRect compute_widest_occluder(const SkRRect& rr) {
321 const SkRect& r = rr.getBounds();
322
323 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
324 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
325 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
326 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
327
328 SkScalar maxT = SkTMax(ul.fY, ur.fY);
329 SkScalar maxB = SkTMax(ll.fY, lr.fY);
330
331 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
332
333}
334
335// The tallest inset rect
336SkRect compute_tallest_occluder(const SkRRect& rr) {
337 const SkRect& r = rr.getBounds();
338
339 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
340 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
341 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
342 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
343
344 SkScalar maxL = SkTMax(ul.fX, ll.fX);
345 SkScalar maxR = SkTMax(ur.fX, lr.fX);
346
347 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
348}
349
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400350bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
351 SkPixmap srcPM;
352 if (!src.peekPixels(&srcPM)) {
353 return false;
354 }
355
356 SkBitmap tmpDst;
357 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
358 if (!tmpDst.setInfo(dstInfo)) {
359 return false;
360 }
361
Mike Reed304a07c2017-07-12 15:10:28 -0400362 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400363 return false;
364 }
365
366 SkPixmap dstPM;
367 if (!tmpDst.peekPixels(&dstPM)) {
368 return false;
369 }
370
371 if (!srcPM.readPixels(dstPM)) {
372 return false;
373 }
374
375 dst->swap(tmpDst);
376 return true;
377}
378
Matt Sarett4897fb82017-01-18 11:49:33 -0500379void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
380 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
381 kRGBA_8888_SkColorType == src.colorType());
382
383 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
384 dst->allocPixels(grayInfo);
385 uint8_t* dst8 = (uint8_t*)dst->getPixels();
386 const uint32_t* src32 = (const uint32_t*)src.getPixels();
387
388 const int w = src.width();
389 const int h = src.height();
390 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
391 for (int y = 0; y < h; ++y) {
392 if (isBGRA) {
393 // BGRA
394 for (int x = 0; x < w; ++x) {
395 uint32_t s = src32[x];
396 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
397 }
398 } else {
399 // RGBA
400 for (int x = 0; x < w; ++x) {
401 uint32_t s = src32[x];
402 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
403 }
404 }
405 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
406 dst8 += dst->rowBytes();
407 }
408}
robertphillips401c1962016-08-04 12:35:46 -0700409
Mike Reed5a625e02017-08-08 15:48:54 -0400410 //////////////////////////////////////////////////////////////////////////////////////////////
411
412 static int scale255(float x) {
413 return sk_float_round2int(x * 255);
414 }
415
416 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
417 int dr = 0,
418 dg = 0,
419 db = 0,
420 da = 0;
421 switch (ct) {
422 case kRGBA_8888_SkColorType:
423 case kBGRA_8888_SkColorType: {
424 SkPMColor c0 = *(const SkPMColor*)a;
425 SkPMColor c1 = *(const SkPMColor*)b;
426 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
427 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
428 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
429 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
430 } break;
431 case kRGB_565_SkColorType: {
432 uint16_t c0 = *(const uint16_t*)a;
433 uint16_t c1 = *(const uint16_t*)b;
434 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
435 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
436 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
437 } break;
438 case kARGB_4444_SkColorType: {
439 uint16_t c0 = *(const uint16_t*)a;
440 uint16_t c1 = *(const uint16_t*)b;
441 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
442 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
443 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
444 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
445 } break;
446 case kAlpha_8_SkColorType:
447 case kGray_8_SkColorType:
448 da = (const uint8_t*)a - (const uint8_t*)b;
449 break;
450 case kRGBA_F16_SkColorType: {
451 const SkPM4f* c0 = (const SkPM4f*)a;
452 const SkPM4f* c1 = (const SkPM4f*)b;
453 dr = scale255(c0->r() - c1->r());
454 dg = scale255(c0->g() - c1->g());
455 db = scale255(c0->b() - c1->b());
456 da = scale255(c0->a() - c1->a());
457 } break;
458 default:
459 return 0;
460 }
461 dr = SkAbs32(dr);
462 dg = SkAbs32(dg);
463 db = SkAbs32(db);
464 da = SkAbs32(da);
465 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
466 }
467
Mike Reed24846602017-12-04 16:06:03 -0500468 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
469 bool respectColorSpace) {
Mike Reed5a625e02017-08-08 15:48:54 -0400470 if (a.width() != b.width() ||
471 a.height() != b.height() ||
472 a.colorType() != b.colorType() ||
Mike Reed24846602017-12-04 16:06:03 -0500473 (respectColorSpace && (a.colorSpace() != b.colorSpace())))
Mike Reed5a625e02017-08-08 15:48:54 -0400474 {
475 return false;
476 }
477
478 for (int y = 0; y < a.height(); ++y) {
479 const char* aptr = (const char*)a.addr(0, y);
480 const char* bptr = (const char*)b.addr(0, y);
481 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
482 for (int x = 0; x < a.width(); ++x) {
483 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
484 return false;
485 }
486 }
487 }
488 aptr += a.rowBytes();
489 bptr += b.rowBytes();
490 }
491 return true;
492 }
493
Mike Reed24846602017-12-04 16:06:03 -0500494 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
495 bool respectColorSpaces) {
Mike Reed5a625e02017-08-08 15:48:54 -0400496 SkPixmap pm0, pm1;
Mike Reed24846602017-12-04 16:06:03 -0500497 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
498 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
499 }
500
501 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
502 bool respectColorSpaces) {
503 // ensure that peekPixels will succeed
504 auto imga = a->makeRasterImage();
505 auto imgb = b->makeRasterImage();
506 a = imga.get();
507 b = imgb.get();
508
509 SkPixmap pm0, pm1;
510 return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
511 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
Mike Reed5a625e02017-08-08 15:48:54 -0400512 }
Mike Reed46596ae2018-01-02 15:40:29 -0500513
514 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
515 const SkSurfaceProps* props) {
516 auto surf = canvas->makeSurface(info, props);
517 if (!surf) {
518 surf = SkSurface::MakeRaster(info, props);
519 }
520 return surf;
521 }
tfarina20108912014-06-21 10:54:17 -0700522} // namespace sk_tool_utils