Home | History | Annotate | Download | only in tools
      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 "SkBitmap.h"
      9 #include "SkBlendMode.h"
     10 #include "SkCanvas.h"
     11 #include "SkColorData.h"
     12 #include "SkColorPriv.h"
     13 #include "SkFontPriv.h"
     14 #include "SkFloatingPoint.h"
     15 #include "SkImage.h"
     16 #include "SkMatrix.h"
     17 #include "SkPaint.h"
     18 #include "SkPath.h"
     19 #include "SkPixelRef.h"
     20 #include "SkPixmap.h"
     21 #include "SkPoint3.h"
     22 #include "SkRRect.h"
     23 #include "SkShader.h"
     24 #include "SkSurface.h"
     25 #include "SkTextBlob.h"
     26 #include "sk_tool_utils.h"
     27 
     28 #include <cmath>
     29 #include <cstring>
     30 #include <memory>
     31 
     32 namespace sk_tool_utils {
     33 
     34 const 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 
     45 const char* colortype_name(SkColorType ct) {
     46     switch (ct) {
     47         case kUnknown_SkColorType:      return "Unknown";
     48         case kAlpha_8_SkColorType:      return "Alpha_8";
     49         case kRGB_565_SkColorType:      return "RGB_565";
     50         case kARGB_4444_SkColorType:    return "ARGB_4444";
     51         case kRGBA_8888_SkColorType:    return "RGBA_8888";
     52         case kRGB_888x_SkColorType:     return "RGB_888x";
     53         case kBGRA_8888_SkColorType:    return "BGRA_8888";
     54         case kRGBA_1010102_SkColorType: return "RGBA_1010102";
     55         case kRGB_101010x_SkColorType:  return "RGB_101010x";
     56         case kGray_8_SkColorType:       return "Gray_8";
     57         case kRGBA_F16Norm_SkColorType: return "RGBA_F16Norm";
     58         case kRGBA_F16_SkColorType:     return "RGBA_F16";
     59         case kRGBA_F32_SkColorType:     return "RGBA_F32";
     60     }
     61     SkASSERT(false);
     62     return "unexpected colortype";
     63 }
     64 
     65 SkColor color_to_565(SkColor color) {
     66     // Not a good idea to use this function for greyscale colors...
     67     // it will add an obvious purple or green tint.
     68     SkASSERT(SkColorGetR(color) != SkColorGetG(color) ||
     69              SkColorGetR(color) != SkColorGetB(color) ||
     70              SkColorGetG(color) != SkColorGetB(color));
     71 
     72     SkPMColor pmColor = SkPreMultiplyColor(color);
     73     U16CPU color16 = SkPixel32ToPixel16(pmColor);
     74     return SkPixel16ToColor(color16);
     75 }
     76 
     77 void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
     78                   SkColorType colorType, SkAlphaType alphaType) {
     79     SkBitmap tmp(bitmap);
     80     const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
     81 
     82     canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
     83 }
     84 
     85 void write_pixels(SkSurface* surface, const SkBitmap& src, int x, int y,
     86                   SkColorType colorType, SkAlphaType alphaType) {
     87     const SkImageInfo info = SkImageInfo::Make(src.width(), src.height(), colorType, alphaType);
     88     surface->writePixels({info, src.getPixels(), src.rowBytes()}, x, y);
     89 }
     90 
     91 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
     92     SkBitmap bm;
     93     bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
     94     bm.eraseColor(c1);
     95     bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
     96     bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
     97     return SkShader::MakeBitmapShader(
     98             bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
     99 }
    100 
    101 SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
    102     SkBitmap bitmap;
    103     bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
    104     SkCanvas canvas(bitmap);
    105 
    106     sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
    107     return bitmap;
    108 }
    109 
    110 void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
    111     SkPaint paint;
    112     paint.setShader(create_checkerboard_shader(c1, c2, size));
    113     paint.setBlendMode(SkBlendMode::kSrc);
    114     canvas->drawPaint(paint);
    115 }
    116 
    117 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
    118                               int textSize, const char* str) {
    119     SkBitmap bitmap;
    120     bitmap.allocN32Pixels(w, h);
    121     SkCanvas canvas(bitmap);
    122 
    123     SkPaint paint;
    124     paint.setColor(c);
    125 
    126     SkFont font(sk_tool_utils::create_portable_typeface(), textSize);
    127 
    128     canvas.clear(0x00000000);
    129     canvas.drawSimpleText(str, strlen(str), kUTF8_SkTextEncoding,
    130                           SkIntToScalar(x), SkIntToScalar(y), font, paint);
    131 
    132     // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
    133     // will process this correctly but legacy configs will render as if this returned N32.
    134     SkBitmap result;
    135     result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
    136     result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
    137     return result;
    138 }
    139 
    140 void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
    141                             SkTextEncoding encoding, const SkFont& font, SkScalar x, SkScalar y) {
    142     int count = font.countText(text, len, encoding);
    143     auto run = builder->allocRun(font, count, x, y);
    144     font.textToGlyphs(text, len, encoding, run.glyphs, count);
    145 }
    146 
    147 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkFont& font,
    148                       SkScalar x, SkScalar y) {
    149     add_to_text_blob_w_len(builder, text, strlen(text), kUTF8_SkTextEncoding, font, x, y);
    150 }
    151 
    152 void get_text_path(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding,
    153                    SkPath* dst, const SkPoint pos[]) {
    154     SkAutoToGlyphs atg(font, text, length, encoding);
    155     const int count = atg.count();
    156     SkAutoTArray<SkPoint> computedPos;
    157     if (pos == nullptr) {
    158         computedPos.reset(count);
    159         font.getPos(atg.glyphs(), count, &computedPos[0]);
    160         pos = computedPos.get();
    161     }
    162 
    163     struct Rec {
    164         SkPath* fDst;
    165         const SkPoint* fPos;
    166     } rec = { dst, pos };
    167     font.getPaths(atg.glyphs(), atg.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
    168         Rec* rec = (Rec*)ctx;
    169         if (src) {
    170             SkMatrix tmp(mx);
    171             tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
    172             rec->fDst->addPath(*src, tmp);
    173         }
    174         rec->fPos += 1;
    175     }, &rec);
    176 }
    177 
    178 SkPath make_star(const SkRect& bounds, int numPts, int step) {
    179     SkASSERT(numPts != step);
    180     SkPath path;
    181     path.setFillType(SkPath::kEvenOdd_FillType);
    182     path.moveTo(0,-1);
    183     for (int i = 1; i < numPts; ++i) {
    184         int idx = i*step % numPts;
    185         SkScalar theta = idx * 2*SK_ScalarPI/numPts + SK_ScalarPI/2;
    186         SkScalar x = SkScalarCos(theta);
    187         SkScalar y = -SkScalarSin(theta);
    188         path.lineTo(x, y);
    189     }
    190     path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
    191     return path;
    192 }
    193 
    194 static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
    195     SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
    196     unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
    197     unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
    198     unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
    199     *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
    200 }
    201 
    202 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
    203     const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
    204                                          dst.fTop + (dst.height() / 2.0f));
    205     const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
    206 
    207     SkVector3 norm;
    208 
    209     for (int y = dst.fTop; y < dst.fBottom; ++y) {
    210         for (int x = dst.fLeft; x < dst.fRight; ++x) {
    211             norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
    212             norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
    213 
    214             SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
    215             if (tmp >= 1.0f) {
    216                 norm.set(0.0f, 0.0f, 1.0f);
    217             } else {
    218                 norm.fZ = sqrtf(1.0f - tmp);
    219             }
    220 
    221             norm_to_rgb(bm, x, y, norm);
    222         }
    223     }
    224 }
    225 
    226 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
    227     const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
    228                                          dst.fTop + (dst.height() / 2.0f));
    229 
    230     SkIRect inner = dst;
    231     inner.inset(dst.width()/4, dst.height()/4);
    232 
    233     SkPoint3 norm;
    234     const SkPoint3 left =  SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
    235     const SkPoint3 up =    SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
    236     const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2,  0.0f, SK_ScalarRoot2Over2);
    237     const SkPoint3 down =  SkPoint3::Make(0.0f,  SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
    238 
    239     for (int y = dst.fTop; y < dst.fBottom; ++y) {
    240         for (int x = dst.fLeft; x < dst.fRight; ++x) {
    241             if (inner.contains(x, y)) {
    242                 norm.set(0.0f, 0.0f, 1.0f);
    243             } else {
    244                 SkScalar locX = x + 0.5f - center.fX;
    245                 SkScalar locY = y + 0.5f - center.fY;
    246 
    247                 if (locX >= 0.0f) {
    248                     if (locY > 0.0f) {
    249                         norm = locX >= locY ? right : down;   // LR corner
    250                     } else {
    251                         norm = locX > -locY ? right : up;     // UR corner
    252                     }
    253                 } else {
    254                     if (locY > 0.0f) {
    255                         norm = -locX > locY ? left : down;    // LL corner
    256                     } else {
    257                         norm = locX > locY ? up : left;       // UL corner
    258                     }
    259                 }
    260             }
    261 
    262             norm_to_rgb(bm, x, y, norm);
    263         }
    264     }
    265 }
    266 
    267 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
    268     const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
    269                                          dst.fTop + (dst.height() / 2.0f));
    270 
    271     static const SkScalar k1OverRoot3 = 0.5773502692f;
    272 
    273     SkPoint3 norm;
    274     const SkPoint3 leftUp =  SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
    275     const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3,  -k1OverRoot3, k1OverRoot3);
    276     const SkPoint3 down =  SkPoint3::Make(0.0f,  SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
    277 
    278     for (int y = dst.fTop; y < dst.fBottom; ++y) {
    279         for (int x = dst.fLeft; x < dst.fRight; ++x) {
    280             SkScalar locX = x + 0.5f - center.fX;
    281             SkScalar locY = y + 0.5f - center.fY;
    282 
    283             if (locX >= 0.0f) {
    284                 if (locY > 0.0f) {
    285                     norm = locX >= locY ? rightUp : down;   // LR corner
    286                 } else {
    287                     norm = rightUp;
    288                 }
    289             } else {
    290                 if (locY > 0.0f) {
    291                     norm = -locX > locY ? leftUp : down;    // LL corner
    292                 } else {
    293                     norm = leftUp;
    294                 }
    295             }
    296 
    297             norm_to_rgb(bm, x, y, norm);
    298         }
    299     }
    300 }
    301 
    302 #if !defined(__clang__) && defined(_MSC_VER)
    303     // MSVC takes ~2 minutes to compile this function with optimization.
    304     // We don't really care to wait that long for this function.
    305     #pragma optimize("", off)
    306 #endif
    307 void make_big_path(SkPath& path) {
    308     #include "BigPathBench.inc" // IWYU pragma: keep
    309 }
    310 
    311 bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
    312     SkPixmap srcPM;
    313     if (!src.peekPixels(&srcPM)) {
    314         return false;
    315     }
    316 
    317     SkBitmap tmpDst;
    318     SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
    319     if (!tmpDst.setInfo(dstInfo)) {
    320         return false;
    321     }
    322 
    323     if (!tmpDst.tryAllocPixels()) {
    324         return false;
    325     }
    326 
    327     SkPixmap dstPM;
    328     if (!tmpDst.peekPixels(&dstPM)) {
    329         return false;
    330     }
    331 
    332     if (!srcPM.readPixels(dstPM)) {
    333         return false;
    334     }
    335 
    336     dst->swap(tmpDst);
    337     return true;
    338 }
    339 
    340 void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
    341     SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
    342              kRGBA_8888_SkColorType == src.colorType());
    343 
    344     SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
    345     dst->allocPixels(grayInfo);
    346     uint8_t* dst8 = (uint8_t*)dst->getPixels();
    347     const uint32_t* src32 = (const uint32_t*)src.getPixels();
    348 
    349     const int w = src.width();
    350     const int h = src.height();
    351     const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
    352     for (int y = 0; y < h; ++y) {
    353         if (isBGRA) {
    354             // BGRA
    355             for (int x = 0; x < w; ++x) {
    356                 uint32_t s = src32[x];
    357                 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
    358             }
    359         } else {
    360             // RGBA
    361             for (int x = 0; x < w; ++x) {
    362                 uint32_t s = src32[x];
    363                 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
    364             }
    365         }
    366         src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
    367         dst8 += dst->rowBytes();
    368     }
    369 }
    370 
    371     //////////////////////////////////////////////////////////////////////////////////////////////
    372 
    373     bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
    374         if (a.width() != b.width() ||
    375             a.height() != b.height() ||
    376             a.colorType() != b.colorType())
    377         {
    378             return false;
    379         }
    380 
    381         for (int y = 0; y < a.height(); ++y) {
    382             const char* aptr = (const char*)a.addr(0, y);
    383             const char* bptr = (const char*)b.addr(0, y);
    384             if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
    385                 return false;
    386             }
    387             aptr += a.rowBytes();
    388             bptr += b.rowBytes();
    389         }
    390         return true;
    391     }
    392 
    393     bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
    394         SkPixmap pm0, pm1;
    395         return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
    396     }
    397 
    398     bool equal_pixels(const SkImage* a, const SkImage* b) {
    399         // ensure that peekPixels will succeed
    400         auto imga = a->makeRasterImage();
    401         auto imgb = b->makeRasterImage();
    402 
    403         SkPixmap pm0, pm1;
    404         return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
    405     }
    406 
    407     sk_sp<SkSurface> makeSurface(SkCanvas* canvas, const SkImageInfo& info,
    408                                  const SkSurfaceProps* props) {
    409         auto surf = canvas->makeSurface(info, props);
    410         if (!surf) {
    411             surf = SkSurface::MakeRaster(info, props);
    412         }
    413         return surf;
    414     }
    415 }  // namespace sk_tool_utils
    416