blob: 9bff574c933654388dbed988b1fe2d4be50cd849 [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"
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040021#include "SkPoint3.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050022#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";
Mike Klein37854712018-06-26 11:43:06 -040058 case kRGBA_F32_SkColorType: return "RGBA_F32";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000059 }
Mike Reedd4746982018-02-07 16:05:29 -050060 SkASSERT(false);
61 return "unexpected colortype";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000062}
63
caryclark65cdba62015-06-15 06:51:08 -070064SkColor color_to_565(SkColor color) {
Mike Kleind46dce32018-08-16 10:17:03 -040065 // Not a good idea to use this function for greyscale colors...
66 // it will add an obvious purple or green tint.
67 SkASSERT(SkColorGetR(color) != SkColorGetG(color) ||
68 SkColorGetR(color) != SkColorGetB(color) ||
69 SkColorGetG(color) != SkColorGetB(color));
70
caryclark65cdba62015-06-15 06:51:08 -070071 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -070072 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -070073 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -070074}
75
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000076void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
77 SkColorType colorType, SkAlphaType alphaType) {
78 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -070079 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000080
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000081 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
82}
83
Mike Reed4c790bd2018-02-08 14:10:40 -050084void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
85 SkColorType colorType, SkAlphaType alphaType) {
86 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
87 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
88}
89
reed8a21c9f2016-03-08 18:50:00 -080090sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -080091 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -070092 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -080093 bm.eraseColor(c1);
94 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
95 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -080096 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -080097 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
98}
99
robertphillips943a4622015-09-03 13:32:33 -0700100SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
101 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700102 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700103 SkCanvas canvas(bitmap);
104
105 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
106 return bitmap;
107}
108
halcanaryb0cce2c2015-01-26 12:49:00 -0800109void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
110 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800111 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700112 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800113 canvas->drawPaint(paint);
114}
115
robertphillips943a4622015-09-03 13:32:33 -0700116SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
117 int textSize, const char* str) {
118 SkBitmap bitmap;
119 bitmap.allocN32Pixels(w, h);
120 SkCanvas canvas(bitmap);
121
122 SkPaint paint;
123 paint.setAntiAlias(true);
124 sk_tool_utils::set_portable_typeface(&paint);
125 paint.setColor(c);
126 paint.setTextSize(SkIntToScalar(textSize));
127
128 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400129 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700130
Brian Osman2b25d342016-12-20 11:09:31 -0500131 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
132 // will process this correctly but legacy configs will render as if this returned N32.
133 SkBitmap result;
134 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
135 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
136 return result;
robertphillips943a4622015-09-03 13:32:33 -0700137}
138
Robert Phillips4c72b262017-08-15 13:28:42 -0400139void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
140 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700141 SkPaint paint(origPaint);
142 SkTDArray<uint16_t> glyphs;
143
halcanary96fcdcc2015-08-27 07:41:13 -0700144 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700145 paint.textToGlyphs(text, len, glyphs.begin());
146
147 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
148 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700149 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700150 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
151}
152
Robert Phillips4c72b262017-08-15 13:28:42 -0400153void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
154 const SkPaint& origPaint, SkScalar x, SkScalar y) {
155 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
156}
157
Chris Dalton7c02cc72017-11-06 14:10:54 -0700158SkPath make_star(const SkRect& bounds, int numPts, int step) {
159 SkPath path;
160 path.setFillType(SkPath::kEvenOdd_FillType);
161 path.moveTo(0,-1);
162 for (int i = 1; i < numPts; ++i) {
163 int idx = i*step;
164 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
165 SkScalar x = SkScalarCos(theta);
166 SkScalar y = -SkScalarSin(theta);
167 path.lineTo(x, y);
168 }
169 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
170 return path;
171}
172
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400173static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
174 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
175 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
176 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
177 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
178 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
179}
180
181void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
182 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
183 dst.fTop + (dst.height() / 2.0f));
184 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
185
186 SkVector3 norm;
187
188 for (int y = dst.fTop; y < dst.fBottom; ++y) {
189 for (int x = dst.fLeft; x < dst.fRight; ++x) {
190 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
191 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
192
193 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
194 if (tmp >= 1.0f) {
195 norm.set(0.0f, 0.0f, 1.0f);
196 } else {
197 norm.fZ = sqrtf(1.0f - tmp);
198 }
199
200 norm_to_rgb(bm, x, y, norm);
201 }
202 }
203}
204
205void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
206 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
207 dst.fTop + (dst.height() / 2.0f));
208
209 SkIRect inner = dst;
210 inner.inset(dst.width()/4, dst.height()/4);
211
212 SkPoint3 norm;
213 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
214 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
215 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
216 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
217
218 for (int y = dst.fTop; y < dst.fBottom; ++y) {
219 for (int x = dst.fLeft; x < dst.fRight; ++x) {
220 if (inner.contains(x, y)) {
221 norm.set(0.0f, 0.0f, 1.0f);
222 } else {
223 SkScalar locX = x + 0.5f - center.fX;
224 SkScalar locY = y + 0.5f - center.fY;
225
226 if (locX >= 0.0f) {
227 if (locY > 0.0f) {
228 norm = locX >= locY ? right : down; // LR corner
229 } else {
230 norm = locX > -locY ? right : up; // UR corner
231 }
232 } else {
233 if (locY > 0.0f) {
234 norm = -locX > locY ? left : down; // LL corner
235 } else {
236 norm = locX > locY ? up : left; // UL corner
237 }
238 }
239 }
240
241 norm_to_rgb(bm, x, y, norm);
242 }
243 }
244}
245
246void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
247 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
248 dst.fTop + (dst.height() / 2.0f));
249
250 static const SkScalar k1OverRoot3 = 0.5773502692f;
251
252 SkPoint3 norm;
253 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
254 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
255 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
256
257 for (int y = dst.fTop; y < dst.fBottom; ++y) {
258 for (int x = dst.fLeft; x < dst.fRight; ++x) {
259 SkScalar locX = x + 0.5f - center.fX;
260 SkScalar locY = y + 0.5f - center.fY;
261
262 if (locX >= 0.0f) {
263 if (locY > 0.0f) {
264 norm = locX >= locY ? rightUp : down; // LR corner
265 } else {
266 norm = rightUp;
267 }
268 } else {
269 if (locY > 0.0f) {
270 norm = -locX > locY ? leftUp : down; // LL corner
271 } else {
272 norm = leftUp;
273 }
274 }
275
276 norm_to_rgb(bm, x, y, norm);
277 }
278 }
279}
280
Mike Kleinc722f792017-07-31 11:57:21 -0400281#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400282 // MSVC takes ~2 minutes to compile this function with optimization.
283 // We don't really care to wait that long for this function.
284 #pragma optimize("", off)
285#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700286void make_big_path(SkPath& path) {
Ben Wagner97182cc2018-02-15 10:20:04 -0500287 #include "BigPathBench.inc" // IWYU pragma: keep
joshualitt98d2e2f2015-10-05 07:23:30 -0700288}
289
robertphillips9c4909b2015-10-19 06:39:17 -0700290static float gaussian2d_value(int x, int y, float sigma) {
291 // don't bother with the scale term since we're just going to normalize the
292 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700293 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700294 return temp;
295}
296
297static float* create_2d_kernel(float sigma, int* filterSize) {
298 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
299 // sizes are always odd)
300 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
301 int wh = *filterSize = 2*halfFilterSize + 1;
302
303 float* temp = new float[wh*wh];
304
305 float filterTot = 0.0f;
306 for (int yOff = 0; yOff < wh; ++yOff) {
307 for (int xOff = 0; xOff < wh; ++xOff) {
308 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
309
310 filterTot += temp[yOff*wh+xOff];
311 }
312 }
313
314 // normalize the kernel
315 for (int yOff = 0; yOff < wh; ++yOff) {
316 for (int xOff = 0; xOff < wh; ++xOff) {
317 temp[yOff*wh+xOff] /= filterTot;
318 }
319 }
320
321 return temp;
322}
323
324static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
325 SkASSERT(wh & 0x1);
326
327 int halfFilterSize = (wh-1)/2;
328
329 float r = 0.0f, g = 0.0f, b = 0.0f;
330 for (int yOff = 0; yOff < wh; ++yOff) {
331 int ySamp = y + yOff - halfFilterSize;
332
333 if (ySamp < 0) {
334 ySamp = 0;
335 } else if (ySamp > bm.height()-1) {
336 ySamp = bm.height()-1;
337 }
338
339 for (int xOff = 0; xOff < wh; ++xOff) {
340 int xSamp = x + xOff - halfFilterSize;
341
342 if (xSamp < 0) {
343 xSamp = 0;
344 } else if (xSamp > bm.width()-1) {
345 xSamp = bm.width()-1;
346 }
347
348 float filter = kernel[yOff*wh + xOff];
349
350 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
351
352 r += SkGetPackedR32(c) * filter;
353 g += SkGetPackedG32(c) * filter;
354 b += SkGetPackedB32(c) * filter;
355 }
356 }
357
358 U8CPU r8, g8, b8;
359
360 r8 = (U8CPU) (r+0.5f);
361 g8 = (U8CPU) (g+0.5f);
362 b8 = (U8CPU) (b+0.5f);
363
364 return SkPackARGB32(255, r8, g8, b8);
365}
366
367SkBitmap slow_blur(const SkBitmap& src, float sigma) {
368 SkBitmap dst;
369
370 dst.allocN32Pixels(src.width(), src.height(), true);
371
372 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400373 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700374
375 for (int y = 0; y < src.height(); ++y) {
376 for (int x = 0; x < src.width(); ++x) {
377 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
378 }
379 }
halcanary9d524f22016-03-29 09:03:52 -0700380
robertphillips9c4909b2015-10-19 06:39:17 -0700381 return dst;
382}
383
robertphillips276d3282016-08-04 09:03:19 -0700384// compute the intersection point between the diagonal and the ellipse in the
385// lower right corner
386static SkPoint intersection(SkScalar w, SkScalar h) {
387 SkASSERT(w > 0.0f || h > 0.0f);
388
389 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
390}
391
392// Use the intersection of the corners' diagonals with their ellipses to shrink
393// the bounding rect
394SkRect compute_central_occluder(const SkRRect& rr) {
395 const SkRect r = rr.getBounds();
396
397 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
398
399 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
400 if (!radii.isZero()) {
401 SkPoint p = intersection(radii.fX, radii.fY);
402
403 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
404 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
405 }
406
407 radii = rr.radii(SkRRect::kUpperRight_Corner);
408 if (!radii.isZero()) {
409 SkPoint p = intersection(radii.fX, radii.fY);
410
411 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
412 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
413 }
414
415 radii = rr.radii(SkRRect::kLowerRight_Corner);
416 if (!radii.isZero()) {
417 SkPoint p = intersection(radii.fX, radii.fY);
418
419 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
420 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
421 }
422
423 radii = rr.radii(SkRRect::kLowerLeft_Corner);
424 if (!radii.isZero()) {
425 SkPoint p = intersection(radii.fX, radii.fY);
426
427 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
428 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
429 }
430
431 return SkRect::MakeLTRB(newL, newT, newR, newB);
432}
433
robertphillips401c1962016-08-04 12:35:46 -0700434// The widest inset rect
435SkRect compute_widest_occluder(const SkRRect& rr) {
436 const SkRect& r = rr.getBounds();
437
438 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
439 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
440 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
441 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
442
443 SkScalar maxT = SkTMax(ul.fY, ur.fY);
444 SkScalar maxB = SkTMax(ll.fY, lr.fY);
445
446 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
447
448}
449
450// The tallest inset rect
451SkRect compute_tallest_occluder(const SkRRect& rr) {
452 const SkRect& r = rr.getBounds();
453
454 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
455 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
456 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
457 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
458
459 SkScalar maxL = SkTMax(ul.fX, ll.fX);
460 SkScalar maxR = SkTMax(ur.fX, lr.fX);
461
462 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
463}
464
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400465bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
466 SkPixmap srcPM;
467 if (!src.peekPixels(&srcPM)) {
468 return false;
469 }
470
471 SkBitmap tmpDst;
472 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
473 if (!tmpDst.setInfo(dstInfo)) {
474 return false;
475 }
476
Mike Reed304a07c2017-07-12 15:10:28 -0400477 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400478 return false;
479 }
480
481 SkPixmap dstPM;
482 if (!tmpDst.peekPixels(&dstPM)) {
483 return false;
484 }
485
486 if (!srcPM.readPixels(dstPM)) {
487 return false;
488 }
489
490 dst->swap(tmpDst);
491 return true;
492}
493
Matt Sarett4897fb82017-01-18 11:49:33 -0500494void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
495 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
496 kRGBA_8888_SkColorType == src.colorType());
497
498 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
499 dst->allocPixels(grayInfo);
500 uint8_t* dst8 = (uint8_t*)dst->getPixels();
501 const uint32_t* src32 = (const uint32_t*)src.getPixels();
502
503 const int w = src.width();
504 const int h = src.height();
505 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
506 for (int y = 0; y < h; ++y) {
507 if (isBGRA) {
508 // BGRA
509 for (int x = 0; x < w; ++x) {
510 uint32_t s = src32[x];
511 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
512 }
513 } else {
514 // RGBA
515 for (int x = 0; x < w; ++x) {
516 uint32_t s = src32[x];
517 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
518 }
519 }
520 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
521 dst8 += dst->rowBytes();
522 }
523}
robertphillips401c1962016-08-04 12:35:46 -0700524
Mike Reed5a625e02017-08-08 15:48:54 -0400525 //////////////////////////////////////////////////////////////////////////////////////////////
526
527 static int scale255(float x) {
528 return sk_float_round2int(x * 255);
529 }
530
531 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
532 int dr = 0,
533 dg = 0,
534 db = 0,
535 da = 0;
536 switch (ct) {
537 case kRGBA_8888_SkColorType:
538 case kBGRA_8888_SkColorType: {
539 SkPMColor c0 = *(const SkPMColor*)a;
540 SkPMColor c1 = *(const SkPMColor*)b;
541 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
542 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
543 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
544 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
545 } break;
546 case kRGB_565_SkColorType: {
547 uint16_t c0 = *(const uint16_t*)a;
548 uint16_t c1 = *(const uint16_t*)b;
549 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
550 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
551 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
552 } break;
553 case kARGB_4444_SkColorType: {
554 uint16_t c0 = *(const uint16_t*)a;
555 uint16_t c1 = *(const uint16_t*)b;
556 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
557 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
558 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
559 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
560 } break;
561 case kAlpha_8_SkColorType:
562 case kGray_8_SkColorType:
563 da = (const uint8_t*)a - (const uint8_t*)b;
564 break;
565 case kRGBA_F16_SkColorType: {
566 const SkPM4f* c0 = (const SkPM4f*)a;
567 const SkPM4f* c1 = (const SkPM4f*)b;
568 dr = scale255(c0->r() - c1->r());
569 dg = scale255(c0->g() - c1->g());
570 db = scale255(c0->b() - c1->b());
571 da = scale255(c0->a() - c1->a());
572 } break;
573 default:
574 return 0;
575 }
576 dr = SkAbs32(dr);
577 dg = SkAbs32(dg);
578 db = SkAbs32(db);
579 da = SkAbs32(da);
580 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
581 }
582
Mike Reed24846602017-12-04 16:06:03 -0500583 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
584 bool respectColorSpace) {
Mike Reed5a625e02017-08-08 15:48:54 -0400585 if (a.width() != b.width() ||
586 a.height() != b.height() ||
587 a.colorType() != b.colorType() ||
Mike Reed24846602017-12-04 16:06:03 -0500588 (respectColorSpace && (a.colorSpace() != b.colorSpace())))
Mike Reed5a625e02017-08-08 15:48:54 -0400589 {
590 return false;
591 }
592
593 for (int y = 0; y < a.height(); ++y) {
594 const char* aptr = (const char*)a.addr(0, y);
595 const char* bptr = (const char*)b.addr(0, y);
596 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
597 for (int x = 0; x < a.width(); ++x) {
598 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
599 return false;
600 }
601 }
602 }
603 aptr += a.rowBytes();
604 bptr += b.rowBytes();
605 }
606 return true;
607 }
608
Mike Reed24846602017-12-04 16:06:03 -0500609 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
610 bool respectColorSpaces) {
Mike Reed5a625e02017-08-08 15:48:54 -0400611 SkPixmap pm0, pm1;
Mike Reed24846602017-12-04 16:06:03 -0500612 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
613 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
614 }
615
616 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
617 bool respectColorSpaces) {
618 // ensure that peekPixels will succeed
619 auto imga = a->makeRasterImage();
620 auto imgb = b->makeRasterImage();
621 a = imga.get();
622 b = imgb.get();
623
624 SkPixmap pm0, pm1;
625 return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
626 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
Mike Reed5a625e02017-08-08 15:48:54 -0400627 }
Mike Reed46596ae2018-01-02 15:40:29 -0500628
Cary Clarka24712e2018-09-05 18:41:40 +0000629 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
630 const SkSurfaceProps* props) {
631 auto surf = canvas->makeSurface(info, props);
632 if (!surf) {
633 surf = SkSurface::MakeRaster(info, props);
634 }
635 return surf;
636 }
tfarina20108912014-06-21 10:54:17 -0700637} // namespace sk_tool_utils