blob: a0b57e5f37b5213b0d1860c84194cdda6d1a904b [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 Wagner97182cc2018-02-15 10:20:04 -050016#include "SkPaint.h"
17#include "SkPath.h"
18#include "SkPixelRef.h"
19#include "SkPixmap.h"
Robert Phillipsa8cdbd72018-07-17 12:30:40 -040020#include "SkPoint3.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050021#include "SkRRect.h"
Ben Wagner3560b582018-03-27 18:15:53 +000022#include "SkShader.h"
23#include "SkSurface.h"
Ben Wagner3560b582018-03-27 18:15:53 +000024#include "SkTextBlob.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050025#include "sk_tool_utils.h"
26
27#include <cmath>
28#include <cstring>
29#include <memory>
Cary Clark992c7b02014-07-31 08:58:44 -040030
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000031namespace sk_tool_utils {
32
Mike Reedd4746982018-02-07 16:05:29 -050033const char* alphatype_name(SkAlphaType at) {
34 switch (at) {
35 case kUnknown_SkAlphaType: return "Unknown";
36 case kOpaque_SkAlphaType: return "Opaque";
37 case kPremul_SkAlphaType: return "Premul";
38 case kUnpremul_SkAlphaType: return "Unpremul";
39 }
40 SkASSERT(false);
41 return "unexpected alphatype";
42}
43
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000044const char* colortype_name(SkColorType ct) {
45 switch (ct) {
46 case kUnknown_SkColorType: return "Unknown";
47 case kAlpha_8_SkColorType: return "Alpha_8";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000048 case kRGB_565_SkColorType: return "RGB_565";
Mike Reedd4746982018-02-07 16:05:29 -050049 case kARGB_4444_SkColorType: return "ARGB_4444";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000050 case kRGBA_8888_SkColorType: return "RGBA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050051 case kRGB_888x_SkColorType: return "RGB_888x";
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000052 case kBGRA_8888_SkColorType: return "BGRA_8888";
Mike Reedd4746982018-02-07 16:05:29 -050053 case kRGBA_1010102_SkColorType: return "RGBA_1010102";
54 case kRGB_101010x_SkColorType: return "RGB_101010x";
55 case kGray_8_SkColorType: return "Gray_8";
Brian Osmanf750fbc2017-02-08 10:47:28 -050056 case kRGBA_F16_SkColorType: return "RGBA_F16";
Mike Klein37854712018-06-26 11:43:06 -040057 case kRGBA_F32_SkColorType: return "RGBA_F32";
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) {
Mike Kleind46dce32018-08-16 10:17:03 -040064 // Not a good idea to use this function for greyscale colors...
65 // it will add an obvious purple or green tint.
66 SkASSERT(SkColorGetR(color) != SkColorGetG(color) ||
67 SkColorGetR(color) != SkColorGetB(color) ||
68 SkColorGetG(color) != SkColorGetB(color));
69
caryclark65cdba62015-06-15 06:51:08 -070070 SkPMColor pmColor = SkPreMultiplyColor(color);
caryclarkd85093c2015-06-12 11:49:04 -070071 U16CPU color16 = SkPixel32ToPixel16(pmColor);
caryclark65cdba62015-06-15 06:51:08 -070072 return SkPixel16ToColor(color16);
caryclarkd85093c2015-06-12 11:49:04 -070073}
74
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000075void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
76 SkColorType colorType, SkAlphaType alphaType) {
77 SkBitmap tmp(bitmap);
reede5ea5002014-09-03 11:54:58 -070078 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000079
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000080 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
81}
82
Mike Reed4c790bd2018-02-08 14:10:40 -050083void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
84 SkColorType colorType, SkAlphaType alphaType) {
85 const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
86 surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
87}
88
reed8a21c9f2016-03-08 18:50:00 -080089sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanaryb0cce2c2015-01-26 12:49:00 -080090 SkBitmap bm;
brianosman2331a5f2016-09-28 14:02:10 -070091 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
halcanaryb0cce2c2015-01-26 12:49:00 -080092 bm.eraseColor(c1);
93 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
94 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed8a21c9f2016-03-08 18:50:00 -080095 return SkShader::MakeBitmapShader(
halcanaryb0cce2c2015-01-26 12:49:00 -080096 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
97}
98
robertphillips943a4622015-09-03 13:32:33 -070099SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
100 SkBitmap bitmap;
brianosman2331a5f2016-09-28 14:02:10 -0700101 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
robertphillips943a4622015-09-03 13:32:33 -0700102 SkCanvas canvas(bitmap);
103
104 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
105 return bitmap;
106}
107
halcanaryb0cce2c2015-01-26 12:49:00 -0800108void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
109 SkPaint paint;
reed8a21c9f2016-03-08 18:50:00 -0800110 paint.setShader(create_checkerboard_shader(c1, c2, size));
reed374772b2016-10-05 17:33:02 -0700111 paint.setBlendMode(SkBlendMode::kSrc);
halcanaryb0cce2c2015-01-26 12:49:00 -0800112 canvas->drawPaint(paint);
113}
114
robertphillips943a4622015-09-03 13:32:33 -0700115SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
116 int textSize, const char* str) {
117 SkBitmap bitmap;
118 bitmap.allocN32Pixels(w, h);
119 SkCanvas canvas(bitmap);
120
121 SkPaint paint;
122 paint.setAntiAlias(true);
123 sk_tool_utils::set_portable_typeface(&paint);
124 paint.setColor(c);
125 paint.setTextSize(SkIntToScalar(textSize));
126
127 canvas.clear(0x00000000);
Cary Clark2a475ea2017-04-28 15:35:12 -0400128 canvas.drawString(str, SkIntToScalar(x), SkIntToScalar(y), paint);
robertphillips943a4622015-09-03 13:32:33 -0700129
Brian Osman2b25d342016-12-20 11:09:31 -0500130 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
131 // will process this correctly but legacy configs will render as if this returned N32.
132 SkBitmap result;
133 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
134 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
135 return result;
robertphillips943a4622015-09-03 13:32:33 -0700136}
137
Robert Phillips4c72b262017-08-15 13:28:42 -0400138void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
139 const SkPaint& origPaint, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700140 SkPaint paint(origPaint);
141 SkTDArray<uint16_t> glyphs;
142
halcanary96fcdcc2015-08-27 07:41:13 -0700143 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualitt9e36c1a2015-04-14 12:17:27 -0700144 paint.textToGlyphs(text, len, glyphs.begin());
145
146 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
147 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
halcanary96fcdcc2015-08-27 07:41:13 -0700148 nullptr);
joshualitt9e36c1a2015-04-14 12:17:27 -0700149 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
150}
151
Robert Phillips4c72b262017-08-15 13:28:42 -0400152void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
153 const SkPaint& origPaint, SkScalar x, SkScalar y) {
154 add_to_text_blob_w_len(builder, text, strlen(text), origPaint, x, y);
155}
156
Chris Dalton7c02cc72017-11-06 14:10:54 -0700157SkPath make_star(const SkRect& bounds, int numPts, int step) {
158 SkPath path;
159 path.setFillType(SkPath::kEvenOdd_FillType);
160 path.moveTo(0,-1);
161 for (int i = 1; i < numPts; ++i) {
162 int idx = i*step;
163 SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
164 SkScalar x = SkScalarCos(theta);
165 SkScalar y = -SkScalarSin(theta);
166 path.lineTo(x, y);
167 }
168 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
169 return path;
170}
171
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400172static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
173 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
174 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
175 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
176 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
177 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
178}
179
180void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
181 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
182 dst.fTop + (dst.height() / 2.0f));
183 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
184
185 SkVector3 norm;
186
187 for (int y = dst.fTop; y < dst.fBottom; ++y) {
188 for (int x = dst.fLeft; x < dst.fRight; ++x) {
189 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
190 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
191
192 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
193 if (tmp >= 1.0f) {
194 norm.set(0.0f, 0.0f, 1.0f);
195 } else {
196 norm.fZ = sqrtf(1.0f - tmp);
197 }
198
199 norm_to_rgb(bm, x, y, norm);
200 }
201 }
202}
203
204void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
205 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
206 dst.fTop + (dst.height() / 2.0f));
207
208 SkIRect inner = dst;
209 inner.inset(dst.width()/4, dst.height()/4);
210
211 SkPoint3 norm;
212 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
213 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
214 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
215 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
216
217 for (int y = dst.fTop; y < dst.fBottom; ++y) {
218 for (int x = dst.fLeft; x < dst.fRight; ++x) {
219 if (inner.contains(x, y)) {
220 norm.set(0.0f, 0.0f, 1.0f);
221 } else {
222 SkScalar locX = x + 0.5f - center.fX;
223 SkScalar locY = y + 0.5f - center.fY;
224
225 if (locX >= 0.0f) {
226 if (locY > 0.0f) {
227 norm = locX >= locY ? right : down; // LR corner
228 } else {
229 norm = locX > -locY ? right : up; // UR corner
230 }
231 } else {
232 if (locY > 0.0f) {
233 norm = -locX > locY ? left : down; // LL corner
234 } else {
235 norm = locX > locY ? up : left; // UL corner
236 }
237 }
238 }
239
240 norm_to_rgb(bm, x, y, norm);
241 }
242 }
243}
244
245void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
246 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
247 dst.fTop + (dst.height() / 2.0f));
248
249 static const SkScalar k1OverRoot3 = 0.5773502692f;
250
251 SkPoint3 norm;
252 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
253 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
254 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
255
256 for (int y = dst.fTop; y < dst.fBottom; ++y) {
257 for (int x = dst.fLeft; x < dst.fRight; ++x) {
258 SkScalar locX = x + 0.5f - center.fX;
259 SkScalar locY = y + 0.5f - center.fY;
260
261 if (locX >= 0.0f) {
262 if (locY > 0.0f) {
263 norm = locX >= locY ? rightUp : down; // LR corner
264 } else {
265 norm = rightUp;
266 }
267 } else {
268 if (locY > 0.0f) {
269 norm = -locX > locY ? leftUp : down; // LL corner
270 } else {
271 norm = leftUp;
272 }
273 }
274
275 norm_to_rgb(bm, x, y, norm);
276 }
277 }
278}
279
Mike Kleinc722f792017-07-31 11:57:21 -0400280#if !defined(__clang__) && defined(_MSC_VER)
Mike Klein88fa7472016-10-12 17:06:48 -0400281 // MSVC takes ~2 minutes to compile this function with optimization.
282 // We don't really care to wait that long for this function.
283 #pragma optimize("", off)
284#endif
joshualitt98d2e2f2015-10-05 07:23:30 -0700285void make_big_path(SkPath& path) {
Ben Wagner97182cc2018-02-15 10:20:04 -0500286 #include "BigPathBench.inc" // IWYU pragma: keep
joshualitt98d2e2f2015-10-05 07:23:30 -0700287}
288
robertphillips9c4909b2015-10-19 06:39:17 -0700289static float gaussian2d_value(int x, int y, float sigma) {
290 // don't bother with the scale term since we're just going to normalize the
291 // kernel anyways
bsalomon2f8ac352015-10-19 08:29:16 -0700292 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
robertphillips9c4909b2015-10-19 06:39:17 -0700293 return temp;
294}
295
296static float* create_2d_kernel(float sigma, int* filterSize) {
297 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
298 // sizes are always odd)
299 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
300 int wh = *filterSize = 2*halfFilterSize + 1;
301
302 float* temp = new float[wh*wh];
303
304 float filterTot = 0.0f;
305 for (int yOff = 0; yOff < wh; ++yOff) {
306 for (int xOff = 0; xOff < wh; ++xOff) {
307 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
308
309 filterTot += temp[yOff*wh+xOff];
310 }
311 }
312
313 // normalize the kernel
314 for (int yOff = 0; yOff < wh; ++yOff) {
315 for (int xOff = 0; xOff < wh; ++xOff) {
316 temp[yOff*wh+xOff] /= filterTot;
317 }
318 }
319
320 return temp;
321}
322
323static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
324 SkASSERT(wh & 0x1);
325
326 int halfFilterSize = (wh-1)/2;
327
328 float r = 0.0f, g = 0.0f, b = 0.0f;
329 for (int yOff = 0; yOff < wh; ++yOff) {
330 int ySamp = y + yOff - halfFilterSize;
331
332 if (ySamp < 0) {
333 ySamp = 0;
334 } else if (ySamp > bm.height()-1) {
335 ySamp = bm.height()-1;
336 }
337
338 for (int xOff = 0; xOff < wh; ++xOff) {
339 int xSamp = x + xOff - halfFilterSize;
340
341 if (xSamp < 0) {
342 xSamp = 0;
343 } else if (xSamp > bm.width()-1) {
344 xSamp = bm.width()-1;
345 }
346
347 float filter = kernel[yOff*wh + xOff];
348
349 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
350
351 r += SkGetPackedR32(c) * filter;
352 g += SkGetPackedG32(c) * filter;
353 b += SkGetPackedB32(c) * filter;
354 }
355 }
356
357 U8CPU r8, g8, b8;
358
359 r8 = (U8CPU) (r+0.5f);
360 g8 = (U8CPU) (g+0.5f);
361 b8 = (U8CPU) (b+0.5f);
362
363 return SkPackARGB32(255, r8, g8, b8);
364}
365
366SkBitmap slow_blur(const SkBitmap& src, float sigma) {
367 SkBitmap dst;
368
369 dst.allocN32Pixels(src.width(), src.height(), true);
370
371 int wh;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400372 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
robertphillips9c4909b2015-10-19 06:39:17 -0700373
374 for (int y = 0; y < src.height(); ++y) {
375 for (int x = 0; x < src.width(); ++x) {
376 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
377 }
378 }
halcanary9d524f22016-03-29 09:03:52 -0700379
robertphillips9c4909b2015-10-19 06:39:17 -0700380 return dst;
381}
382
robertphillips276d3282016-08-04 09:03:19 -0700383// compute the intersection point between the diagonal and the ellipse in the
384// lower right corner
385static SkPoint intersection(SkScalar w, SkScalar h) {
386 SkASSERT(w > 0.0f || h > 0.0f);
387
388 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
389}
390
391// Use the intersection of the corners' diagonals with their ellipses to shrink
392// the bounding rect
393SkRect compute_central_occluder(const SkRRect& rr) {
394 const SkRect r = rr.getBounds();
395
396 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
397
398 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
399 if (!radii.isZero()) {
400 SkPoint p = intersection(radii.fX, radii.fY);
401
402 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
403 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
404 }
405
406 radii = rr.radii(SkRRect::kUpperRight_Corner);
407 if (!radii.isZero()) {
408 SkPoint p = intersection(radii.fX, radii.fY);
409
410 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
411 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
412 }
413
414 radii = rr.radii(SkRRect::kLowerRight_Corner);
415 if (!radii.isZero()) {
416 SkPoint p = intersection(radii.fX, radii.fY);
417
418 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
419 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
420 }
421
422 radii = rr.radii(SkRRect::kLowerLeft_Corner);
423 if (!radii.isZero()) {
424 SkPoint p = intersection(radii.fX, radii.fY);
425
426 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
427 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
428 }
429
430 return SkRect::MakeLTRB(newL, newT, newR, newB);
431}
432
robertphillips401c1962016-08-04 12:35:46 -0700433// The widest inset rect
434SkRect compute_widest_occluder(const SkRRect& rr) {
435 const SkRect& r = rr.getBounds();
436
437 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
438 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
439 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
440 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
441
442 SkScalar maxT = SkTMax(ul.fY, ur.fY);
443 SkScalar maxB = SkTMax(ll.fY, lr.fY);
444
445 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
446
447}
448
449// The tallest inset rect
450SkRect compute_tallest_occluder(const SkRRect& rr) {
451 const SkRect& r = rr.getBounds();
452
453 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
454 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
455 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
456 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
457
458 SkScalar maxL = SkTMax(ul.fX, ll.fX);
459 SkScalar maxR = SkTMax(ur.fX, lr.fX);
460
461 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
462}
463
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400464bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
465 SkPixmap srcPM;
466 if (!src.peekPixels(&srcPM)) {
467 return false;
468 }
469
470 SkBitmap tmpDst;
471 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
472 if (!tmpDst.setInfo(dstInfo)) {
473 return false;
474 }
475
Mike Reed304a07c2017-07-12 15:10:28 -0400476 if (!tmpDst.tryAllocPixels()) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400477 return false;
478 }
479
480 SkPixmap dstPM;
481 if (!tmpDst.peekPixels(&dstPM)) {
482 return false;
483 }
484
485 if (!srcPM.readPixels(dstPM)) {
486 return false;
487 }
488
489 dst->swap(tmpDst);
490 return true;
491}
492
Matt Sarett4897fb82017-01-18 11:49:33 -0500493void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
494 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
495 kRGBA_8888_SkColorType == src.colorType());
496
497 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
498 dst->allocPixels(grayInfo);
499 uint8_t* dst8 = (uint8_t*)dst->getPixels();
500 const uint32_t* src32 = (const uint32_t*)src.getPixels();
501
502 const int w = src.width();
503 const int h = src.height();
504 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
505 for (int y = 0; y < h; ++y) {
506 if (isBGRA) {
507 // BGRA
508 for (int x = 0; x < w; ++x) {
509 uint32_t s = src32[x];
510 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
511 }
512 } else {
513 // RGBA
514 for (int x = 0; x < w; ++x) {
515 uint32_t s = src32[x];
516 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
517 }
518 }
519 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
520 dst8 += dst->rowBytes();
521 }
522}
robertphillips401c1962016-08-04 12:35:46 -0700523
Mike Reed5a625e02017-08-08 15:48:54 -0400524 //////////////////////////////////////////////////////////////////////////////////////////////
525
Brian Osman1b151982018-09-20 12:47:39 -0400526 bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
Mike Reed5a625e02017-08-08 15:48:54 -0400527 if (a.width() != b.width() ||
528 a.height() != b.height() ||
Brian Osman1b151982018-09-20 12:47:39 -0400529 a.colorType() != b.colorType())
Mike Reed5a625e02017-08-08 15:48:54 -0400530 {
531 return false;
532 }
533
534 for (int y = 0; y < a.height(); ++y) {
535 const char* aptr = (const char*)a.addr(0, y);
536 const char* bptr = (const char*)b.addr(0, y);
537 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
Brian Osman1b151982018-09-20 12:47:39 -0400538 return false;
Mike Reed5a625e02017-08-08 15:48:54 -0400539 }
540 aptr += a.rowBytes();
541 bptr += b.rowBytes();
542 }
543 return true;
544 }
545
Brian Osman1b151982018-09-20 12:47:39 -0400546 bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
Mike Reed5a625e02017-08-08 15:48:54 -0400547 SkPixmap pm0, pm1;
Brian Osman1b151982018-09-20 12:47:39 -0400548 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
Mike Reed24846602017-12-04 16:06:03 -0500549 }
550
Brian Osman1b151982018-09-20 12:47:39 -0400551 bool equal_pixels(const SkImage* a, const SkImage* b) {
Mike Reed24846602017-12-04 16:06:03 -0500552 // ensure that peekPixels will succeed
553 auto imga = a->makeRasterImage();
554 auto imgb = b->makeRasterImage();
Mike Reed24846602017-12-04 16:06:03 -0500555
556 SkPixmap pm0, pm1;
Brian Osman1b151982018-09-20 12:47:39 -0400557 return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
Mike Reed5a625e02017-08-08 15:48:54 -0400558 }
Mike Reed46596ae2018-01-02 15:40:29 -0500559
Cary Clarka24712e2018-09-05 18:41:40 +0000560 sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
561 const SkSurfaceProps* props) {
562 auto surf = canvas->makeSurface(info, props);
563 if (!surf) {
564 surf = SkSurface::MakeRaster(info, props);
565 }
566 return surf;
567 }
tfarina20108912014-06-21 10:54:17 -0700568} // namespace sk_tool_utils