| 1 |
/* |
| 2 |
* 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 |
|
| 8 |
#include "tools/ToolUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkBlendMode.h" |
| 13 |
#include "include/core/SkCanvas.h" |
| 14 |
#include "include/core/SkColorSpace.h" |
| 15 |
#include "include/core/SkColorType.h" |
| 16 |
#include "include/core/SkFont.h" |
| 17 |
#include "include/core/SkFontTypes.h" |
| 18 |
#include "include/core/SkImage.h" |
| 19 |
#include "include/core/SkImageInfo.h" |
| 20 |
#include "include/core/SkMatrix.h" |
| 21 |
#include "include/core/SkPaint.h" |
| 22 |
#include "include/core/SkPath.h" |
| 23 |
#include "include/core/SkPathBuilder.h" |
| 24 |
#include "include/core/SkPathTypes.h" |
| 25 |
#include "include/core/SkPicture.h" |
| 26 |
#include "include/core/SkPixelRef.h" // IWYU pragma: keep |
| 27 |
#include "include/core/SkPixmap.h" |
| 28 |
#include "include/core/SkPoint3.h" |
| 29 |
#include "include/core/SkRefCnt.h" |
| 30 |
#include "include/core/SkSamplingOptions.h" |
| 31 |
#include "include/core/SkStream.h" |
| 32 |
#include "include/core/SkSurface.h" |
| 33 |
#include "include/core/SkTextBlob.h" |
| 34 |
#include "include/core/SkTileMode.h" |
| 35 |
#include "include/core/SkTypeface.h" |
| 36 |
#include "include/effects/SkGradient.h" |
| 37 |
#include "include/private/base/SkCPUTypes.h" |
| 38 |
#include "include/private/base/SkTemplates.h" |
| 39 |
#include "src/core/SkColorData.h" |
| 40 |
#include "src/core/SkColorPriv.h" |
| 41 |
#include "src/core/SkFontPriv.h" |
| 42 |
#include "tools/SkMetaData.h" |
| 43 |
|
| 44 |
#include <cmath> |
| 45 |
#include <cstring> |
| 46 |
|
| 47 |
using namespace skia_private; |
| 48 |
|
| 49 |
namespace ToolUtils { |
| 50 |
|
| 51 |
const char* alphatype_name(SkAlphaType at) { |
| 52 |
switch (at) { |
| 53 |
case kUnknown_SkAlphaType: return "Unknown"; |
| 54 |
case kOpaque_SkAlphaType: return "Opaque"; |
| 55 |
case kPremul_SkAlphaType: return "Premul"; |
| 56 |
case kUnpremul_SkAlphaType: return "Unpremul"; |
| 57 |
} |
| 58 |
SkUNREACHABLE; |
| 59 |
} |
| 60 |
|
| 61 |
const char* colortype_name(SkColorType ct) { |
| 62 |
switch (ct) { |
| 63 |
case kUnknown_SkColorType: return "Unknown"; |
| 64 |
case kAlpha_8_SkColorType: return "Alpha_8"; |
| 65 |
case kA16_unorm_SkColorType: return "Alpha_16"; |
| 66 |
case kA16_float_SkColorType: return "A16_float"; |
| 67 |
case kRGB_565_SkColorType: return "RGB_565"; |
| 68 |
case kARGB_4444_SkColorType: return "ARGB_4444"; |
| 69 |
case kRGBA_8888_SkColorType: return "RGBA_8888"; |
| 70 |
case kSRGBA_8888_SkColorType: return "SRGBA_8888"; |
| 71 |
case kRGB_888x_SkColorType: return "RGB_888x"; |
| 72 |
case kBGRA_8888_SkColorType: return "BGRA_8888"; |
| 73 |
case kRGBA_1010102_SkColorType: return "RGBA_1010102"; |
| 74 |
case kBGRA_1010102_SkColorType: return "BGRA_1010102"; |
| 75 |
case kRGB_101010x_SkColorType: return "RGB_101010x"; |
| 76 |
case kBGR_101010x_SkColorType: return "BGR_101010x"; |
| 77 |
case kBGR_101010x_XR_SkColorType: return "BGR_101010x_XR"; |
| 78 |
case kRGBA_10x6_SkColorType: return "RGBA_10x6"; |
| 79 |
case kGray_8_SkColorType: return "Gray_8"; |
| 80 |
case kRGBA_F16Norm_SkColorType: return "RGBA_F16Norm"; |
| 81 |
case kRGB_F16F16F16x_SkColorType: return "RGB_F16F16F16x"; |
| 82 |
case kRGBA_F16_SkColorType: return "RGBA_F16"; |
| 83 |
case kRGBA_F32_SkColorType: return "RGBA_F32"; |
| 84 |
case kR8G8_unorm_SkColorType: return "R8G8_unorm"; |
| 85 |
case kR16_unorm_SkColorType: return "R16_unorm"; |
| 86 |
case kR16G16_unorm_SkColorType: return "R16G16_unorm"; |
| 87 |
case kR16G16_float_SkColorType: return "R16G16_float"; |
| 88 |
case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm"; |
| 89 |
case kR8_unorm_SkColorType: return "R8_unorm"; |
| 90 |
case kBGRA_10101010_XR_SkColorType: return "BGRA_10101010_XR"; |
| 91 |
} |
| 92 |
SkUNREACHABLE; |
| 93 |
} |
| 94 |
|
| 95 |
const char* colortype_depth(SkColorType ct) { |
| 96 |
switch (ct) { |
| 97 |
case kUnknown_SkColorType: return "Unknown"; |
| 98 |
case kAlpha_8_SkColorType: return "A8"; |
| 99 |
case kA16_unorm_SkColorType: return "A16"; |
| 100 |
case kA16_float_SkColorType: return "AF16"; |
| 101 |
case kRGB_565_SkColorType: return "565"; |
| 102 |
case kARGB_4444_SkColorType: return "4444"; |
| 103 |
case kRGBA_8888_SkColorType: return "8888"; |
| 104 |
case kSRGBA_8888_SkColorType: return "8888"; |
| 105 |
case kRGB_888x_SkColorType: return "888"; |
| 106 |
case kBGRA_8888_SkColorType: return "8888"; |
| 107 |
case kRGBA_1010102_SkColorType: return "1010102"; |
| 108 |
case kBGRA_1010102_SkColorType: return "1010102"; |
| 109 |
case kRGB_101010x_SkColorType: return "101010"; |
| 110 |
case kBGR_101010x_SkColorType: return "101010"; |
| 111 |
case kBGR_101010x_XR_SkColorType: return "101010"; |
| 112 |
case kBGRA_10101010_XR_SkColorType: return "10101010"; |
| 113 |
case kRGBA_10x6_SkColorType: return "10101010"; |
| 114 |
case kGray_8_SkColorType: return "G8"; |
| 115 |
case kRGBA_F16Norm_SkColorType: return "F16Norm"; |
| 116 |
case kRGB_F16F16F16x_SkColorType: return "F16F16F16x"; |
| 117 |
case kRGBA_F16_SkColorType: return "F16"; |
| 118 |
case kRGBA_F32_SkColorType: return "F32"; |
| 119 |
case kR8G8_unorm_SkColorType: return "88"; |
| 120 |
case kR16_unorm_SkColorType: return "R16"; |
| 121 |
case kR16G16_unorm_SkColorType: return "1616"; |
| 122 |
case kR16G16_float_SkColorType: return "F16F16"; |
| 123 |
case kR16G16B16A16_unorm_SkColorType: return "16161616"; |
| 124 |
case kR8_unorm_SkColorType: return "R8"; |
| 125 |
} |
| 126 |
SkUNREACHABLE; |
| 127 |
} |
| 128 |
|
| 129 |
const char* tilemode_name(SkTileMode mode) { |
| 130 |
switch (mode) { |
| 131 |
case SkTileMode::kClamp: return "clamp"; |
| 132 |
case SkTileMode::kRepeat: return "repeat"; |
| 133 |
case SkTileMode::kMirror: return "mirror"; |
| 134 |
case SkTileMode::kDecal: return "decal"; |
| 135 |
} |
| 136 |
SkUNREACHABLE; |
| 137 |
} |
| 138 |
|
| 139 |
SkColor color_to_565(SkColor color) { |
| 140 |
// Not a good idea to use this function for greyscale colors... |
| 141 |
// it will add an obvious purple or green tint. |
| 142 |
SkASSERT(SkColorGetR(color) != SkColorGetG(color) || SkColorGetR(color) != SkColorGetB(color) || |
| 143 |
SkColorGetG(color) != SkColorGetB(color)); |
| 144 |
|
| 145 |
SkPMColor pmColor = SkPreMultiplyColor(color); |
| 146 |
U16CPU color16 = SkPixel32ToPixel16(pmColor); |
| 147 |
return SkPixel16ToColor(color16); |
| 148 |
} |
| 149 |
|
| 150 |
sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
| 151 |
SkBitmap bm; |
| 152 |
bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType)); |
| 153 |
bm.eraseColor(c1); |
| 154 |
bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 155 |
bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
| 156 |
return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()); |
| 157 |
} |
| 158 |
|
| 159 |
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 160 |
SkBitmap bitmap; |
| 161 |
bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 162 |
SkCanvas canvas(bitmap); |
| 163 |
|
| 164 |
ToolUtils::draw_checkerboard(&canvas, c1, c2, checkSize); |
| 165 |
return bitmap; |
| 166 |
} |
| 167 |
|
| 168 |
sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 169 |
auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(w, h)); |
| 170 |
ToolUtils::draw_checkerboard(surf->getCanvas(), c1, c2, checkSize); |
| 171 |
return surf->makeImageSnapshot(); |
| 172 |
} |
| 173 |
|
| 174 |
void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 175 |
SkPaint paint; |
| 176 |
paint.setShader(create_checkerboard_shader(c1, c2, size)); |
| 177 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 178 |
canvas->drawPaint(paint); |
| 179 |
} |
| 180 |
|
| 181 |
int make_pixmaps(SkColorType ct, |
| 182 |
SkAlphaType at, |
| 183 |
bool withMips, |
| 184 |
const SkColor4f colors[6], |
| 185 |
SkPixmap pixmaps[6], |
| 186 |
std::unique_ptr<char[]>* mem) { |
| 187 |
|
| 188 |
int levelSize = 32; |
| 189 |
int numMipLevels = withMips ? 6 : 1; |
| 190 |
size_t size = 0; |
| 191 |
SkImageInfo ii[6]; |
| 192 |
size_t rowBytes[6]; |
| 193 |
for (int level = 0; level < numMipLevels; ++level) { |
| 194 |
ii[level] = SkImageInfo::Make(levelSize, levelSize, ct, at); |
| 195 |
rowBytes[level] = ii[level].minRowBytes(); |
| 196 |
// Make sure we test row bytes that aren't tight. |
| 197 |
if (!(level % 2)) { |
| 198 |
rowBytes[level] += (level + 1)*SkColorTypeBytesPerPixel(ii[level].colorType()); |
| 199 |
} |
| 200 |
size += rowBytes[level]*ii[level].height(); |
| 201 |
levelSize /= 2; |
| 202 |
} |
| 203 |
mem->reset(new char[size]); |
| 204 |
char* addr = mem->get(); |
| 205 |
for (int level = 0; level < numMipLevels; ++level) { |
| 206 |
pixmaps[level].reset(ii[level], addr, rowBytes[level]); |
| 207 |
addr += rowBytes[level]*ii[level].height(); |
| 208 |
pixmaps[level].erase(colors[level]); |
| 209 |
} |
| 210 |
return numMipLevels; |
| 211 |
} |
| 212 |
|
| 213 |
void add_to_text_blob_w_len(SkTextBlobBuilder* builder, |
| 214 |
const char* text, |
| 215 |
size_t len, |
| 216 |
SkTextEncoding encoding, |
| 217 |
const SkFont& font, |
| 218 |
SkScalar x, |
| 219 |
SkScalar y) { |
| 220 |
size_t count = font.countText(text, len, encoding); |
| 221 |
if (count < 1) { |
| 222 |
return; |
| 223 |
} |
| 224 |
auto run = builder->allocRun(font, count, x, y); |
| 225 |
font.textToGlyphs(text, len, encoding, {run.glyphs, count}); |
| 226 |
} |
| 227 |
|
| 228 |
void add_to_text_blob(SkTextBlobBuilder* builder, |
| 229 |
const char* text, |
| 230 |
const SkFont& font, |
| 231 |
SkScalar x, |
| 232 |
SkScalar y) { |
| 233 |
add_to_text_blob_w_len(builder, text, strlen(text), SkTextEncoding::kUTF8, font, x, y); |
| 234 |
} |
| 235 |
|
| 236 |
SkPath get_text_path(const SkFont& font, |
| 237 |
const void* text, |
| 238 |
size_t length, |
| 239 |
SkTextEncoding encoding, |
| 240 |
const SkPoint pos[]) { |
| 241 |
SkAutoToGlyphs atg(font, text, length, encoding); |
| 242 |
const size_t count = atg.count(); |
| 243 |
AutoTArray<SkPoint> computedPos; |
| 244 |
if (pos == nullptr) { |
| 245 |
computedPos.reset(count); |
| 246 |
font.getPos(atg.glyphs(), computedPos); |
| 247 |
pos = computedPos.get(); |
| 248 |
} |
| 249 |
|
| 250 |
struct Rec { |
| 251 |
SkPathBuilder fBuilder; |
| 252 |
const SkPoint* fPos; |
| 253 |
} rec = {{}, pos}; |
| 254 |
font.getPaths(atg.glyphs(), |
| 255 |
[](const SkPath* src, const SkMatrix& mx, void* ctx) { |
| 256 |
Rec* rec = (Rec*)ctx; |
| 257 |
if (src) { |
| 258 |
SkMatrix tmp(mx); |
| 259 |
tmp.postTranslate(rec->fPos->fX, rec->fPos->fY); |
| 260 |
rec->fBuilder.addPath(*src, tmp); |
| 261 |
} |
| 262 |
rec->fPos += 1; |
| 263 |
}, |
| 264 |
&rec); |
| 265 |
return rec.fBuilder.detach(); |
| 266 |
} |
| 267 |
|
| 268 |
SkPath make_star(const SkRect& bounds, int numPts, int step) { |
| 269 |
SkASSERT(numPts != step); |
| 270 |
SkPathBuilder builder; |
| 271 |
builder.setFillType(SkPathFillType::kEvenOdd); |
| 272 |
builder.moveTo(0, -1); |
| 273 |
for (int i = 1; i < numPts; ++i) { |
| 274 |
int idx = i * step % numPts; |
| 275 |
SkScalar theta = idx * 2 * SK_ScalarPI / numPts + SK_ScalarPI / 2; |
| 276 |
SkScalar x = SkScalarCos(theta); |
| 277 |
SkScalar y = -SkScalarSin(theta); |
| 278 |
builder.lineTo(x, y); |
| 279 |
} |
| 280 |
SkPath path = builder.detach(); |
| 281 |
return path.makeTransform(SkMatrix::RectToRectOrIdentity(path.getBounds(), bounds)); |
| 282 |
} |
| 283 |
|
| 284 |
static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) { |
| 285 |
SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); |
| 286 |
unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255); |
| 287 |
unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255); |
| 288 |
unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255); |
| 289 |
*bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 290 |
} |
| 291 |
|
| 292 |
void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 293 |
const SkPoint center = |
| 294 |
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 295 |
const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f); |
| 296 |
|
| 297 |
SkVector3 norm; |
| 298 |
|
| 299 |
for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 300 |
for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 301 |
norm.fX = (x + 0.5f - center.fX) / halfSize.fX; |
| 302 |
norm.fY = (y + 0.5f - center.fY) / halfSize.fY; |
| 303 |
|
| 304 |
SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY; |
| 305 |
if (tmp >= 1.0f) { |
| 306 |
norm.set(0.0f, 0.0f, 1.0f); |
| 307 |
} else { |
| 308 |
norm.fZ = sqrtf(1.0f - tmp); |
| 309 |
} |
| 310 |
|
| 311 |
norm_to_rgb(bm, x, y, norm); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 317 |
const SkPoint center = |
| 318 |
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 319 |
|
| 320 |
SkIRect inner = dst; |
| 321 |
inner.inset(dst.width() / 4, dst.height() / 4); |
| 322 |
|
| 323 |
SkPoint3 norm; |
| 324 |
const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 325 |
const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 326 |
const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 327 |
const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 328 |
|
| 329 |
for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 330 |
for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 331 |
if (inner.contains(x, y)) { |
| 332 |
norm.set(0.0f, 0.0f, 1.0f); |
| 333 |
} else { |
| 334 |
SkScalar locX = x + 0.5f - center.fX; |
| 335 |
SkScalar locY = y + 0.5f - center.fY; |
| 336 |
|
| 337 |
if (locX >= 0.0f) { |
| 338 |
if (locY > 0.0f) { |
| 339 |
norm = locX >= locY ? right : down; // LR corner |
| 340 |
} else { |
| 341 |
norm = locX > -locY ? right : up; // UR corner |
| 342 |
} |
| 343 |
} else { |
| 344 |
if (locY > 0.0f) { |
| 345 |
norm = -locX > locY ? left : down; // LL corner |
| 346 |
} else { |
| 347 |
norm = locX > locY ? up : left; // UL corner |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
norm_to_rgb(bm, x, y, norm); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 358 |
const SkPoint center = |
| 359 |
SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 360 |
|
| 361 |
static const SkScalar k1OverRoot3 = 0.5773502692f; |
| 362 |
|
| 363 |
SkPoint3 norm; |
| 364 |
const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 365 |
const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 366 |
const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 367 |
|
| 368 |
for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 369 |
for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 370 |
SkScalar locX = x + 0.5f - center.fX; |
| 371 |
SkScalar locY = y + 0.5f - center.fY; |
| 372 |
|
| 373 |
if (locX >= 0.0f) { |
| 374 |
if (locY > 0.0f) { |
| 375 |
norm = locX >= locY ? rightUp : down; // LR corner |
| 376 |
} else { |
| 377 |
norm = rightUp; |
| 378 |
} |
| 379 |
} else { |
| 380 |
if (locY > 0.0f) { |
| 381 |
norm = -locX > locY ? leftUp : down; // LL corner |
| 382 |
} else { |
| 383 |
norm = leftUp; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
norm_to_rgb(bm, x, y, norm); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 393 |
SkPixmap srcPM; |
| 394 |
if (!src.peekPixels(&srcPM)) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
SkBitmap tmpDst; |
| 399 |
SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType); |
| 400 |
if (!tmpDst.setInfo(dstInfo)) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
if (!tmpDst.tryAllocPixels()) { |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
SkPixmap dstPM; |
| 409 |
if (!tmpDst.peekPixels(&dstPM)) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
if (!srcPM.readPixels(dstPM)) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
dst->swap(tmpDst); |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
void copy_to_g8(SkBitmap* dst, const SkBitmap& src) { |
| 422 |
SkASSERT(kBGRA_8888_SkColorType == src.colorType() || |
| 423 |
kRGBA_8888_SkColorType == src.colorType()); |
| 424 |
|
| 425 |
SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType); |
| 426 |
dst->allocPixels(grayInfo); |
| 427 |
uint8_t* dst8 = (uint8_t*)dst->getPixels(); |
| 428 |
const uint32_t* src32 = (const uint32_t*)src.getPixels(); |
| 429 |
|
| 430 |
const int w = src.width(); |
| 431 |
const int h = src.height(); |
| 432 |
const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType()); |
| 433 |
for (int y = 0; y < h; ++y) { |
| 434 |
if (isBGRA) { |
| 435 |
// BGRA |
| 436 |
for (int x = 0; x < w; ++x) { |
| 437 |
uint32_t s = src32[x]; |
| 438 |
dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF); |
| 439 |
} |
| 440 |
} else { |
| 441 |
// RGBA |
| 442 |
for (int x = 0; x < w; ++x) { |
| 443 |
uint32_t s = src32[x]; |
| 444 |
dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF); |
| 445 |
} |
| 446 |
} |
| 447 |
src32 = (const uint32_t*)((const char*)src32 + src.rowBytes()); |
| 448 |
dst8 += dst->rowBytes(); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
////////////////////////////////////////////////////////////////////////////////////////////// |
| 453 |
|
| 454 |
bool equal_pixels(const SkPixmap& a, const SkPixmap& b) { |
| 455 |
if (a.width() != b.width() || a.height() != b.height()) { |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
if (a.colorType() != b.colorType()) { |
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
for (int y = 0; y < a.height(); ++y) { |
| 464 |
const char* aptr = (const char*)a.addr(0, y); |
| 465 |
const char* bptr = (const char*)b.addr(0, y); |
| 466 |
if (0 != memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) { |
| 467 |
return false; |
| 468 |
} |
| 469 |
} |
| 470 |
return true; |
| 471 |
} |
| 472 |
|
| 473 |
bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) { |
| 474 |
SkPixmap pm0, pm1; |
| 475 |
if (!bm0.peekPixels(&pm0)) { |
| 476 |
return false; |
| 477 |
} |
| 478 |
if (!bm1.peekPixels(&pm1)) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
return equal_pixels(pm0, pm1); |
| 482 |
} |
| 483 |
|
| 484 |
bool equal_pixels(const SkImage* a, const SkImage* b) { |
| 485 |
SkASSERT_RELEASE(a); |
| 486 |
SkASSERT_RELEASE(b); |
| 487 |
// ensure that peekPixels will succeed |
| 488 |
auto imga = a->makeRasterImage(nullptr); |
| 489 |
auto imgb = b->makeRasterImage(nullptr); |
| 490 |
|
| 491 |
SkPixmap pm0, pm1; |
| 492 |
if (!imga->peekPixels(&pm0)) { |
| 493 |
return false; |
| 494 |
} |
| 495 |
if (!imgb->peekPixels(&pm1)) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
return equal_pixels(pm0, pm1); |
| 499 |
} |
| 500 |
|
| 501 |
sk_sp<SkSurface> makeSurface(SkCanvas* canvas, |
| 502 |
const SkImageInfo& info, |
| 503 |
const SkSurfaceProps* props) { |
| 504 |
auto surf = canvas->makeSurface(info, props); |
| 505 |
if (!surf) { |
| 506 |
surf = SkSurfaces::Raster(info, props); |
| 507 |
} |
| 508 |
return surf; |
| 509 |
} |
| 510 |
|
| 511 |
VariationSliders::VariationSliders(SkTypeface* typeface, |
| 512 |
SkFontArguments::VariationPosition variationPosition) { |
| 513 |
if (!typeface) { |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
int numAxes = typeface->getVariationDesignParameters({}); |
| 518 |
if (numAxes < 0) { |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
std::unique_ptr<SkFontParameters::Variation::Axis[]> copiedAxes = |
| 523 |
std::make_unique<SkFontParameters::Variation::Axis[]>(numAxes); |
| 524 |
|
| 525 |
numAxes = typeface->getVariationDesignParameters({copiedAxes.get(), (size_t)numAxes}); |
| 526 |
if (numAxes < 0) { |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
auto argVariationPositionOrDefault = [&variationPosition](SkFourByteTag tag, |
| 531 |
SkScalar defaultValue) -> SkScalar { |
| 532 |
for (int i = 0; i < variationPosition.coordinateCount; ++i) { |
| 533 |
if (variationPosition.coordinates[i].axis == tag) { |
| 534 |
return variationPosition.coordinates[i].value; |
| 535 |
} |
| 536 |
} |
| 537 |
return defaultValue; |
| 538 |
}; |
| 539 |
|
| 540 |
fAxisSliders.resize(numAxes); |
| 541 |
fCoords = std::make_unique<SkFontArguments::VariationPosition::Coordinate[]>(numAxes); |
| 542 |
for (int i = 0; i < numAxes; ++i) { |
| 543 |
fAxisSliders[i].axis = copiedAxes[i]; |
| 544 |
fAxisSliders[i].current = |
| 545 |
argVariationPositionOrDefault(copiedAxes[i].tag, copiedAxes[i].def); |
| 546 |
fAxisSliders[i].name = tagToString(fAxisSliders[i].axis.tag); |
| 547 |
fCoords[i] = { fAxisSliders[i].axis.tag, fAxisSliders[i].current }; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/* static */ |
| 552 |
SkString VariationSliders::tagToString(SkFourByteTag tag) { |
| 553 |
char tagAsString[5]; |
| 554 |
tagAsString[4] = 0; |
| 555 |
tagAsString[0] = (char)(uint8_t)(tag >> 24); |
| 556 |
tagAsString[1] = (char)(uint8_t)(tag >> 16); |
| 557 |
tagAsString[2] = (char)(uint8_t)(tag >> 8); |
| 558 |
tagAsString[3] = (char)(uint8_t)(tag >> 0); |
| 559 |
return SkString(tagAsString); |
| 560 |
} |
| 561 |
|
| 562 |
bool VariationSliders::writeControls(SkMetaData* controls) { |
| 563 |
for (size_t i = 0; i < fAxisSliders.size(); ++i) { |
| 564 |
SkScalar axisVars[kAxisVarsSize]; |
| 565 |
|
| 566 |
axisVars[0] = fAxisSliders[i].current; |
| 567 |
axisVars[1] = fAxisSliders[i].axis.min; |
| 568 |
axisVars[2] = fAxisSliders[i].axis.max; |
| 569 |
controls->setScalars(fAxisSliders[i].name.c_str(), kAxisVarsSize, axisVars); |
| 570 |
} |
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
void VariationSliders::readControls(const SkMetaData& controls, bool* changed) { |
| 575 |
for (size_t i = 0; i < fAxisSliders.size(); ++i) { |
| 576 |
SkScalar axisVars[kAxisVarsSize] = {0}; |
| 577 |
int resultAxisVarsSize = 0; |
| 578 |
SkASSERT_RELEASE(controls.findScalars( |
| 579 |
tagToString(fAxisSliders[i].axis.tag).c_str(), &resultAxisVarsSize, axisVars)); |
| 580 |
SkASSERT_RELEASE(resultAxisVarsSize == kAxisVarsSize); |
| 581 |
if (changed) { |
| 582 |
*changed |= fAxisSliders[i].current != axisVars[0]; |
| 583 |
} |
| 584 |
fAxisSliders[i].current = axisVars[0]; |
| 585 |
fCoords[i] = { fAxisSliders[i].axis.tag, fAxisSliders[i].current }; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
SkSpan<const SkFontArguments::VariationPosition::Coordinate> VariationSliders::getCoordinates() { |
| 590 |
return SkSpan<const SkFontArguments::VariationPosition::Coordinate>{fCoords.get(), |
| 591 |
fAxisSliders.size()}; |
| 592 |
} |
| 593 |
|
| 594 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 595 |
HilbertGenerator::HilbertGenerator(float desiredSize, float desiredLineWidth, int desiredDepth) |
| 596 |
: fDesiredSize(desiredSize) |
| 597 |
, fDesiredDepth(desiredDepth) |
| 598 |
, fSegmentLength(fDesiredSize / ((0x1 << fDesiredDepth) - 1.0f)) |
| 599 |
, fDesiredLineWidth(desiredLineWidth) |
| 600 |
, fActualBounds(SkRect::MakeEmpty()) |
| 601 |
, fCurPos(SkPoint::Make(0.0f, 0.0f)) |
| 602 |
, fCurDir(0) |
| 603 |
, fExpectedLen(fSegmentLength * ((0x1 << (2*fDesiredDepth)) - 1.0f)) |
| 604 |
, fCurLen(0.0f) { |
| 605 |
} |
| 606 |
|
| 607 |
void HilbertGenerator::draw(SkCanvas* canvas) { |
| 608 |
this->recursiveDraw(canvas, /* curDepth= */ 0, /* turnLeft= */ true); |
| 609 |
|
| 610 |
SkScalarNearlyEqual(fExpectedLen, fCurLen, 0.01f); |
| 611 |
SkScalarNearlyEqual(fDesiredSize, fActualBounds.width(), 0.01f); |
| 612 |
SkScalarNearlyEqual(fDesiredSize, fActualBounds.height(), 0.01f); |
| 613 |
} |
| 614 |
|
| 615 |
void HilbertGenerator::turn90(bool turnLeft) { |
| 616 |
fCurDir += turnLeft ? 90 : -90; |
| 617 |
if (fCurDir >= 360) { |
| 618 |
fCurDir = 0; |
| 619 |
} else if (fCurDir < 0) { |
| 620 |
fCurDir = 270; |
| 621 |
} |
| 622 |
|
| 623 |
SkASSERT(fCurDir == 0 || fCurDir == 90 || fCurDir == 180 || fCurDir == 270); |
| 624 |
} |
| 625 |
|
| 626 |
void HilbertGenerator::line(SkCanvas* canvas) { |
| 627 |
|
| 628 |
SkPoint before = fCurPos; |
| 629 |
|
| 630 |
SkRect r; |
| 631 |
switch (fCurDir) { |
| 632 |
case 0: |
| 633 |
r.fLeft = fCurPos.fX; |
| 634 |
r.fTop = fCurPos.fY - fDesiredLineWidth / 2.0f; |
| 635 |
r.fRight = fCurPos.fX + fSegmentLength; |
| 636 |
r.fBottom = fCurPos.fY + fDesiredLineWidth / 2.0f; |
| 637 |
fCurPos.fX += fSegmentLength; |
| 638 |
break; |
| 639 |
case 90: |
| 640 |
r.fLeft = fCurPos.fX - fDesiredLineWidth / 2.0f; |
| 641 |
r.fTop = fCurPos.fY - fSegmentLength; |
| 642 |
r.fRight = fCurPos.fX + fDesiredLineWidth / 2.0f; |
| 643 |
r.fBottom = fCurPos.fY; |
| 644 |
fCurPos.fY -= fSegmentLength; |
| 645 |
break; |
| 646 |
case 180: |
| 647 |
r.fLeft = fCurPos.fX - fSegmentLength; |
| 648 |
r.fTop = fCurPos.fY - fDesiredLineWidth / 2.0f; |
| 649 |
r.fRight = fCurPos.fX; |
| 650 |
r.fBottom = fCurPos.fY + fDesiredLineWidth / 2.0f; |
| 651 |
fCurPos.fX -= fSegmentLength; |
| 652 |
break; |
| 653 |
case 270: |
| 654 |
r.fLeft = fCurPos.fX - fDesiredLineWidth / 2.0f; |
| 655 |
r.fTop = fCurPos.fY; |
| 656 |
r.fRight = fCurPos.fX + fDesiredLineWidth / 2.0f; |
| 657 |
r.fBottom = fCurPos.fY + fSegmentLength; |
| 658 |
fCurPos.fY += fSegmentLength; |
| 659 |
break; |
| 660 |
default: |
| 661 |
return; |
| 662 |
} |
| 663 |
|
| 664 |
SkPoint pts[2] = { before, fCurPos }; |
| 665 |
|
| 666 |
SkColor4f colors[2] = { |
| 667 |
this->getColor(fCurLen), |
| 668 |
this->getColor(fCurLen + fSegmentLength), |
| 669 |
}; |
| 670 |
|
| 671 |
fCurLen += fSegmentLength; |
| 672 |
if (fActualBounds.isEmpty()) { |
| 673 |
fActualBounds = r; |
| 674 |
} else { |
| 675 |
fActualBounds.join(r); |
| 676 |
} |
| 677 |
|
| 678 |
SkPaint paint; |
| 679 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 680 |
canvas->drawRect(r, paint); |
| 681 |
} |
| 682 |
|
| 683 |
void HilbertGenerator::recursiveDraw(SkCanvas* canvas, int curDepth, bool turnLeft) { |
| 684 |
if (curDepth >= fDesiredDepth) { |
| 685 |
return; |
| 686 |
} |
| 687 |
|
| 688 |
this->turn90(turnLeft); |
| 689 |
this->recursiveDraw(canvas, curDepth + 1, !turnLeft); |
| 690 |
this->line(canvas); |
| 691 |
this->turn90(!turnLeft); |
| 692 |
this->recursiveDraw(canvas, curDepth + 1, turnLeft); |
| 693 |
this->line(canvas); |
| 694 |
this->recursiveDraw(canvas, curDepth + 1, turnLeft); |
| 695 |
this->turn90(!turnLeft); |
| 696 |
this->line(canvas); |
| 697 |
this->recursiveDraw(canvas, curDepth + 1, !turnLeft); |
| 698 |
this->turn90(turnLeft); |
| 699 |
} |
| 700 |
|
| 701 |
SkColor4f HilbertGenerator::getColor(float curLen) { |
| 702 |
static const SkColor4f kColors[] = { |
| 703 |
SkColors::kBlack, |
| 704 |
SkColors::kBlue, |
| 705 |
SkColors::kCyan, |
| 706 |
SkColors::kGreen, |
| 707 |
SkColors::kYellow, |
| 708 |
SkColors::kRed, |
| 709 |
SkColors::kWhite, |
| 710 |
}; |
| 711 |
|
| 712 |
static const float kStops[] = { |
| 713 |
0.0f, |
| 714 |
1.0f/6.0f, |
| 715 |
2.0f/6.0f, |
| 716 |
0.5f, |
| 717 |
4.0f/6.0f, |
| 718 |
5.0f/6.0f, |
| 719 |
1.0f, |
| 720 |
}; |
| 721 |
static_assert(std::size(kColors) == std::size(kStops)); |
| 722 |
|
| 723 |
float t = curLen / fExpectedLen; |
| 724 |
if (t <= 0.0f) { |
| 725 |
return kColors[0]; |
| 726 |
} else if (t >= 1.0f) { |
| 727 |
return kColors[std::size(kColors)-1]; |
| 728 |
} |
| 729 |
|
| 730 |
for (unsigned int i = 0; i < std::size(kColors)-1; ++i) { |
| 731 |
if (kStops[i] <= t && t <= kStops[i+1]) { |
| 732 |
t = (t - kStops[i]) / (kStops[i+1] - kStops[i]); |
| 733 |
SkASSERT(0.0f <= t && t <= 1.0f); |
| 734 |
return { kColors[i].fR * (1 - t) + kColors[i+1].fR * t, |
| 735 |
kColors[i].fG * (1 - t) + kColors[i+1].fG * t, |
| 736 |
kColors[i].fB * (1 - t) + kColors[i+1].fB * t, |
| 737 |
kColors[i].fA * (1 - t) + kColors[i+1].fA * t }; |
| 738 |
|
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
return SkColors::kBlack; |
| 743 |
} |
| 744 |
|
| 745 |
void ExtractPathsFromSKP(const char filepath[], std::function<PathSniffCallback> callback) { |
| 746 |
SkFILEStream stream(filepath); |
| 747 |
if (!stream.isValid()) { |
| 748 |
SkDebugf("ExtractPaths: invalid input file at \"%s\"\n", filepath); |
| 749 |
return; |
| 750 |
} |
| 751 |
|
| 752 |
class PathSniffer : public SkCanvas { |
| 753 |
public: |
| 754 |
PathSniffer(std::function<PathSniffCallback> callback) |
| 755 |
: SkCanvas(4096, 4096, nullptr) |
| 756 |
, fPathSniffCallback(callback) {} |
| 757 |
private: |
| 758 |
void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 759 |
fPathSniffCallback(this->getTotalMatrix(), path, paint); |
| 760 |
} |
| 761 |
std::function<PathSniffCallback> fPathSniffCallback; |
| 762 |
}; |
| 763 |
|
| 764 |
// We don't need to decode images etc, so we can pass nullptr for the deserial procs. |
| 765 |
sk_sp<SkPicture> skp = SkPicture::MakeFromStream(&stream, nullptr); |
| 766 |
if (!skp) { |
| 767 |
SkDebugf("ExtractPaths: couldn't load skp at \"%s\"\n", filepath); |
| 768 |
return; |
| 769 |
} |
| 770 |
PathSniffer pathSniffer(callback); |
| 771 |
skp->playback(&pathSniffer); |
| 772 |
} |
| 773 |
|
| 774 |
bool A8ComparePaths(const SkPath& a, const SkPath& b, A8CompareProc cmp) { |
| 775 |
const auto ra = a.computeTightBounds(), |
| 776 |
rb = b.computeTightBounds(); |
| 777 |
if (ra.isEmpty() && rb.isEmpty()) { |
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
const auto r = ra.makeOutset(1, 1); |
| 782 |
if (!r.contains(rb)) { |
| 783 |
return false; |
| 784 |
} |
| 785 |
|
| 786 |
const auto ir = r.roundOut(); |
| 787 |
const auto info = SkImageInfo::MakeA8(ir.width(), ir.height()); |
| 788 |
|
| 789 |
auto make_img = [&](const SkPath& path) { |
| 790 |
SkPaint paint; |
| 791 |
paint.setAntiAlias(true); |
| 792 |
auto surf = SkSurfaces::Raster(info); |
| 793 |
auto canvas = surf->getCanvas(); |
| 794 |
canvas->translate(1 - ir.fLeft, 1 - ir.fTop); // keep ~1 pixel margin |
| 795 |
canvas->drawPath(a, paint); |
| 796 |
return surf->makeImageSnapshot(); |
| 797 |
}; |
| 798 |
auto imga = make_img(a), |
| 799 |
imgb = make_img(b); |
| 800 |
|
| 801 |
SkPixmap pma, pmb; |
| 802 |
SkAssertResult(imga->peekPixels(&pma)); |
| 803 |
SkAssertResult(imgb->peekPixels(&pmb)); |
| 804 |
|
| 805 |
for (int y = 0; y < pma.height(); ++y) { |
| 806 |
for (int x = 0; x < pma.width(); ++x) { |
| 807 |
uint8_t pa = *pma.addr8(x, y), |
| 808 |
pb = *pmb.addr8(x, y); |
| 809 |
if (pa != pb) { |
| 810 |
if (!cmp(x, y, pa, pb)) { |
| 811 |
return false; |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
} // namespace ToolUtils |