blob: ae6e90f7cf66e2a8de52c8f48de0aff871fe3cee [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) {
65 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -070066 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -070067 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -070068}
69
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000070void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
71 SkColorType colorType, SkAlphaType alphaType) {
72 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -070073 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000074
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000075 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
76}
77
Mike Reed4c790bd2018-02-08 14:10:40 -050078void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
79 SkColorType colorType, SkAlphaType alphaType) {
80 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
81 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
82}
83
reed8a21c9f2016-03-08 18:50:00 -080084sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -080085 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -070086 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -080087 bm.eraseColor(c1);
88 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
89 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -080090 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -080091 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
92}
93
robertphillips943a4622015-09-03 13:32:33 -070094SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
95 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -070096 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -070097 SkCanvas canvas(bitmap);
98
99 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
100 return bitmap;
101}
102
halcanaryb0cce2c2015-01-26 12:49:00 -0800103void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
104 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800105 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700106 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800107 canvas->drawPaint(paint);
108}
109
robertphillips943a4622015-09-03 13:32:33 -0700110SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
111 int textSize, const char* str) {
112 SkBitmap bitmap;
113 bitmap.allocN32Pixels(w, h);
114 SkCanvas canvas(bitmap);
115
116 SkPaint paint;
117 paint.setAntiAlias(true);
118 sk_tool_utils::set_portable_typeface(&paint);
119 paint.setColor(c);
120 paint.setTextSize(SkIntToScalar(textSize));
121
122 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400123 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700124
Brian Osman2b25d342016-12-20 11:09:31 -0500125 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
126 // will process this correctly but legacy configs will render as if this returned N32.
127 SkBitmap result;
128 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
129 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
130 return result;
robertphillips943a4622015-09-03 13:32:33 -0700131}
132
Robert Phillips4c72b262017-08-15 13:28:42 -0400133void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
134 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700135 SkPaint paint(origPaint);
136 SkTDArray<uint16_t> glyphs;
137
halcanary96fcdcc2015-08-27 07:41:13 -0700138 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700139 paint.textToGlyphs(text, len, glyphs.begin());
140
141 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
142 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700143 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700144 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
145}
146
Robert Phillips4c72b262017-08-15 13:28:42 -0400147void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
148 const SkPaint& origPaint, SkScalar x, SkScalar y) {
149 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
150}
151
Chris Dalton7c02cc72017-11-06 14:10:54 -0700152SkPath make_star(const SkRect& bounds, int numPts, int step) {
153 SkPath path;
154 path.setFillType(SkPath::kEvenOdd_FillType);
155 path.moveTo(0,-1);
156 for (int i = 1; i < numPts; ++i) {
157 int idx = i*step;
158 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
159 SkScalar x = SkScalarCos(theta);
160 SkScalar y = -SkScalarSin(theta);
161 path.lineTo(x, y);
162 }
163 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
164 return path;
165}
166
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400167static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
168 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
169 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
170 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
171 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
172 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
173}
174
175void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
176 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
177 dst.fTop + (dst.height() / 2.0f));
178 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
179
180 SkVector3 norm;
181
182 for (int y = dst.fTop; y < dst.fBottom; ++y) {
183 for (int x = dst.fLeft; x < dst.fRight; ++x) {
184 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
185 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
186
187 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
188 if (tmp >= 1.0f) {
189 norm.set(0.0f, 0.0f, 1.0f);
190 } else {
191 norm.fZ = sqrtf(1.0f - tmp);
192 }
193
194 norm_to_rgb(bm, x, y, norm);
195 }
196 }
197}
198
199void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
200 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
201 dst.fTop + (dst.height() / 2.0f));
202
203 SkIRect inner = dst;
204 inner.inset(dst.width()/4, dst.height()/4);
205
206 SkPoint3 norm;
207 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
208 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
209 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
210 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
211
212 for (int y = dst.fTop; y < dst.fBottom; ++y) {
213 for (int x = dst.fLeft; x < dst.fRight; ++x) {
214 if (inner.contains(x, y)) {
215 norm.set(0.0f, 0.0f, 1.0f);
216 } else {
217 SkScalar locX = x + 0.5f - center.fX;
218 SkScalar locY = y + 0.5f - center.fY;
219
220 if (locX >= 0.0f) {
221 if (locY > 0.0f) {
222 norm = locX >= locY ? right : down; // LR corner
223 } else {
224 norm = locX > -locY ? right : up; // UR corner
225 }
226 } else {
227 if (locY > 0.0f) {
228 norm = -locX > locY ? left : down; // LL corner
229 } else {
230 norm = locX > locY ? up : left; // UL corner
231 }
232 }
233 }
234
235 norm_to_rgb(bm, x, y, norm);
236 }
237 }
238}
239
240void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
241 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
242 dst.fTop + (dst.height() / 2.0f));
243
244 static const SkScalar k1OverRoot3 = 0.5773502692f;
245
246 SkPoint3 norm;
247 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
248 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
249 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
250
251 for (int y = dst.fTop; y < dst.fBottom; ++y) {
252 for (int x = dst.fLeft; x < dst.fRight; ++x) {
253 SkScalar locX = x + 0.5f - center.fX;
254 SkScalar locY = y + 0.5f - center.fY;
255
256 if (locX >= 0.0f) {
257 if (locY > 0.0f) {
258 norm = locX >= locY ? rightUp : down; // LR corner
259 } else {
260 norm = rightUp;
261 }
262 } else {
263 if (locY > 0.0f) {
264 norm = -locX > locY ? leftUp : down; // LL corner
265 } else {
266 norm = leftUp;
267 }
268 }
269
270 norm_to_rgb(bm, x, y, norm);
271 }
272 }
273}
274
Mike Kleinc722f792017-07-31 11:57:21 -0400275#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400276 // MSVC takes ~2 minutes to compile this function with optimization.
277 // We don't really care to wait that long for this function.
278 #pragma optimize("", off)
279#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700280void make_big_path(SkPath& path) {
Ben Wagner97182cc2018-02-15 10:20:04 -0500281 #include "BigPathBench.inc" // IWYU pragma: keep
joshualitt98d2e2f2015-10-05 07:23:30 -0700282}
283
robertphillips9c4909b2015-10-19 06:39:17 -0700284static float gaussian2d_value(int x, int y, float sigma) {
285 // don't bother with the scale term since we're just going to normalize the
286 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700287 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700288 return temp;
289}
290
291static float* create_2d_kernel(float sigma, int* filterSize) {
292 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
293 // sizes are always odd)
294 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
295 int wh = *filterSize = 2*halfFilterSize + 1;
296
297 float* temp = new float[wh*wh];
298
299 float filterTot = 0.0f;
300 for (int yOff = 0; yOff < wh; ++yOff) {
301 for (int xOff = 0; xOff < wh; ++xOff) {
302 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
303
304 filterTot += temp[yOff*wh+xOff];
305 }
306 }
307
308 // normalize the kernel
309 for (int yOff = 0; yOff < wh; ++yOff) {
310 for (int xOff = 0; xOff < wh; ++xOff) {
311 temp[yOff*wh+xOff] /= filterTot;
312 }
313 }
314
315 return temp;
316}
317
318static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
319 SkASSERT(wh & 0x1);
320
321 int halfFilterSize = (wh-1)/2;
322
323 float r = 0.0f, g = 0.0f, b = 0.0f;
324 for (int yOff = 0; yOff < wh; ++yOff) {
325 int ySamp = y + yOff - halfFilterSize;
326
327 if (ySamp < 0) {
328 ySamp = 0;
329 } else if (ySamp > bm.height()-1) {
330 ySamp = bm.height()-1;
331 }
332
333 for (int xOff = 0; xOff < wh; ++xOff) {
334 int xSamp = x + xOff - halfFilterSize;
335
336 if (xSamp < 0) {
337 xSamp = 0;
338 } else if (xSamp > bm.width()-1) {
339 xSamp = bm.width()-1;
340 }
341
342 float filter = kernel[yOff*wh + xOff];
343
344 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
345
346 r += SkGetPackedR32(c) * filter;
347 g += SkGetPackedG32(c) * filter;
348 b += SkGetPackedB32(c) * filter;
349 }
350 }
351
352 U8CPU r8, g8, b8;
353
354 r8 = (U8CPU) (r+0.5f);
355 g8 = (U8CPU) (g+0.5f);
356 b8 = (U8CPU) (b+0.5f);
357
358 return SkPackARGB32(255, r8, g8, b8);
359}
360
361SkBitmap slow_blur(const SkBitmap& src, float sigma) {
362 SkBitmap dst;
363
364 dst.allocN32Pixels(src.width(), src.height(), true);
365
366 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400367 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700368
369 for (int y = 0; y < src.height(); ++y) {
370 for (int x = 0; x < src.width(); ++x) {
371 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
372 }
373 }
halcanary9d524f22016-03-29 09:03:52 -0700374
robertphillips9c4909b2015-10-19 06:39:17 -0700375 return dst;
376}
377
robertphillips276d3282016-08-04 09:03:19 -0700378// compute the intersection point between the diagonal and the ellipse in the
379// lower right corner
380static SkPoint intersection(SkScalar w, SkScalar h) {
381 SkASSERT(w > 0.0f || h > 0.0f);
382
383 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
384}
385
386// Use the intersection of the corners' diagonals with their ellipses to shrink
387// the bounding rect
388SkRect compute_central_occluder(const SkRRect& rr) {
389 const SkRect r = rr.getBounds();
390
391 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
392
393 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
394 if (!radii.isZero()) {
395 SkPoint p = intersection(radii.fX, radii.fY);
396
397 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
398 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
399 }
400
401 radii = rr.radii(SkRRect::kUpperRight_Corner);
402 if (!radii.isZero()) {
403 SkPoint p = intersection(radii.fX, radii.fY);
404
405 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
406 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
407 }
408
409 radii = rr.radii(SkRRect::kLowerRight_Corner);
410 if (!radii.isZero()) {
411 SkPoint p = intersection(radii.fX, radii.fY);
412
413 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
414 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
415 }
416
417 radii = rr.radii(SkRRect::kLowerLeft_Corner);
418 if (!radii.isZero()) {
419 SkPoint p = intersection(radii.fX, radii.fY);
420
421 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
422 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
423 }
424
425 return SkRect::MakeLTRB(newL, newT, newR, newB);
426}
427
robertphillips401c1962016-08-04 12:35:46 -0700428// The widest inset rect
429SkRect compute_widest_occluder(const SkRRect& rr) {
430 const SkRect& r = rr.getBounds();
431
432 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
433 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
434 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
435 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
436
437 SkScalar maxT = SkTMax(ul.fY, ur.fY);
438 SkScalar maxB = SkTMax(ll.fY, lr.fY);
439
440 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
441
442}
443
444// The tallest inset rect
445SkRect compute_tallest_occluder(const SkRRect& rr) {
446 const SkRect& r = rr.getBounds();
447
448 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
449 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
450 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
451 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
452
453 SkScalar maxL = SkTMax(ul.fX, ll.fX);
454 SkScalar maxR = SkTMax(ur.fX, lr.fX);
455
456 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
457}
458
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400459bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
460 SkPixmap srcPM;
461 if (!src.peekPixels(&srcPM)) {
462 return false;
463 }
464
465 SkBitmap tmpDst;
466 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
467 if (!tmpDst.setInfo(dstInfo)) {
468 return false;
469 }
470
Mike Reed304a07c2017-07-12 15:10:28 -0400471 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400472 return false;
473 }
474
475 SkPixmap dstPM;
476 if (!tmpDst.peekPixels(&dstPM)) {
477 return false;
478 }
479
480 if (!srcPM.readPixels(dstPM)) {
481 return false;
482 }
483
484 dst->swap(tmpDst);
485 return true;
486}
487
Matt Sarett4897fb82017-01-18 11:49:33 -0500488void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
489 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
490 kRGBA_8888_SkColorType == src.colorType());
491
492 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
493 dst->allocPixels(grayInfo);
494 uint8_t* dst8 = (uint8_t*)dst->getPixels();
495 const uint32_t* src32 = (const uint32_t*)src.getPixels();
496
497 const int w = src.width();
498 const int h = src.height();
499 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
500 for (int y = 0; y < h; ++y) {
501 if (isBGRA) {
502 // BGRA
503 for (int x = 0; x < w; ++x) {
504 uint32_t s = src32[x];
505 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
506 }
507 } else {
508 // RGBA
509 for (int x = 0; x < w; ++x) {
510 uint32_t s = src32[x];
511 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
512 }
513 }
514 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
515 dst8 += dst->rowBytes();
516 }
517}
robertphillips401c1962016-08-04 12:35:46 -0700518
Mike Reed5a625e02017-08-08 15:48:54 -0400519 //////////////////////////////////////////////////////////////////////////////////////////////
520
521 static int scale255(float x) {
522 return sk_float_round2int(x * 255);
523 }
524
525 static unsigned diff(const SkColorType ct, const void* a, const void* b) {
526 int dr = 0,
527 dg = 0,
528 db = 0,
529 da = 0;
530 switch (ct) {
531 case kRGBA_8888_SkColorType:
532 case kBGRA_8888_SkColorType: {
533 SkPMColor c0 = *(const SkPMColor*)a;
534 SkPMColor c1 = *(const SkPMColor*)b;
535 dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
536 dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
537 db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
538 da = SkGetPackedA32(c0) - SkGetPackedA32(c1);
539 } break;
540 case kRGB_565_SkColorType: {
541 uint16_t c0 = *(const uint16_t*)a;
542 uint16_t c1 = *(const uint16_t*)b;
543 dr = SkGetPackedR16(c0) - SkGetPackedR16(c1);
544 dg = SkGetPackedG16(c0) - SkGetPackedG16(c1);
545 db = SkGetPackedB16(c0) - SkGetPackedB16(c1);
546 } break;
547 case kARGB_4444_SkColorType: {
548 uint16_t c0 = *(const uint16_t*)a;
549 uint16_t c1 = *(const uint16_t*)b;
550 dr = SkGetPackedR4444(c0) - SkGetPackedR4444(c1);
551 dg = SkGetPackedG4444(c0) - SkGetPackedG4444(c1);
552 db = SkGetPackedB4444(c0) - SkGetPackedB4444(c1);
553 da = SkGetPackedA4444(c0) - SkGetPackedA4444(c1);
554 } break;
555 case kAlpha_8_SkColorType:
556 case kGray_8_SkColorType:
557 da = (const uint8_t*)a - (const uint8_t*)b;
558 break;
559 case kRGBA_F16_SkColorType: {
560 const SkPM4f* c0 = (const SkPM4f*)a;
561 const SkPM4f* c1 = (const SkPM4f*)b;
562 dr = scale255(c0->r() - c1->r());
563 dg = scale255(c0->g() - c1->g());
564 db = scale255(c0->b() - c1->b());
565 da = scale255(c0->a() - c1->a());
566 } break;
567 default:
568 return 0;
569 }
570 dr = SkAbs32(dr);
571 dg = SkAbs32(dg);
572 db = SkAbs32(db);
573 da = SkAbs32(da);
574 return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
575 }
576
Mike Reed24846602017-12-04 16:06:03 -0500577 bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
578 bool respectColorSpace) {
Mike Reed5a625e02017-08-08 15:48:54 -0400579 if (a.width() != b.width() ||
580 a.height() != b.height() ||
581 a.colorType() != b.colorType() ||
Mike Reed24846602017-12-04 16:06:03 -0500582 (respectColorSpace && (a.colorSpace() != b.colorSpace())))
Mike Reed5a625e02017-08-08 15:48:54 -0400583 {
584 return false;
585 }
586
587 for (int y = 0; y < a.height(); ++y) {
588 const char* aptr = (const char*)a.addr(0, y);
589 const char* bptr = (const char*)b.addr(0, y);
590 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
591 for (int x = 0; x < a.width(); ++x) {
592 if (diff(a.colorType(), a.addr(x, y), b.addr(x, y)) > maxDiff) {
593 return false;
594 }
595 }
596 }
597 aptr += a.rowBytes();
598 bptr += b.rowBytes();
599 }
600 return true;
601 }
602
Mike Reed24846602017-12-04 16:06:03 -0500603 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
604 bool respectColorSpaces) {
Mike Reed5a625e02017-08-08 15:48:54 -0400605 SkPixmap pm0, pm1;
Mike Reed24846602017-12-04 16:06:03 -0500606 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
607 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
608 }
609
610 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
611 bool respectColorSpaces) {
612 // ensure that peekPixels will succeed
613 auto imga = a->makeRasterImage();
614 auto imgb = b->makeRasterImage();
615 a = imga.get();
616 b = imgb.get();
617
618 SkPixmap pm0, pm1;
619 return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
620 equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
Mike Reed5a625e02017-08-08 15:48:54 -0400621 }
Mike Reed46596ae2018-01-02 15:40:29 -0500622
623 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
624 const SkSurfaceProps* props) {
625 auto surf = canvas->makeSurface(info, props);
626 if (!surf) {
627 surf = SkSurface::MakeRaster(info, props);
628 }
629 return surf;
630 }
tfarina20108912014-06-21 10:54:17 -0700631} // namespace sk_tool_utils