| 1 |
/* |
| 2 |
* Copyright 2011 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTypeface.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "tools/ToolUtils.h" |
| 21 |
#include "tools/fonts/FontToolUtils.h" |
| 22 |
|
| 23 |
static void make_bm(SkBitmap* bm) { |
| 24 |
const SkColor colors[4] = { |
| 25 |
SK_ColorRED, SK_ColorGREEN, |
| 26 |
SK_ColorBLUE, SK_ColorWHITE |
| 27 |
}; |
| 28 |
SkPMColor colorsPM[4]; |
| 29 |
for (size_t i = 0; i < std::size(colors); ++i) { |
| 30 |
colorsPM[i] = SkPreMultiplyColor(colors[i]); |
| 31 |
} |
| 32 |
bm->allocN32Pixels(2, 2, true); |
| 33 |
|
| 34 |
*bm->getAddr32(0, 0) = colorsPM[0]; |
| 35 |
*bm->getAddr32(1, 0) = colorsPM[1]; |
| 36 |
*bm->getAddr32(0, 1) = colorsPM[2]; |
| 37 |
*bm->getAddr32(1, 1) = colorsPM[3]; |
| 38 |
} |
| 39 |
|
| 40 |
static SkScalar draw_bm(SkCanvas* canvas, sk_sp<SkImage> img, SkScalar x, SkScalar y, |
| 41 |
const SkSamplingOptions& sampling, SkPaint* paint) { |
| 42 |
canvas->drawImage(img, x, y, sampling, paint); |
| 43 |
return SkIntToScalar(img->width()) * 5/4; |
| 44 |
} |
| 45 |
|
| 46 |
static SkScalar draw_set(SkCanvas* c, sk_sp<SkImage> img, SkScalar x, SkPaint* p) { |
| 47 |
x += draw_bm(c, img, x, 0, SkSamplingOptions(), p); |
| 48 |
x += draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p); |
| 49 |
p->setDither(true); |
| 50 |
return x + draw_bm(c, img, x, 0, SkSamplingOptions(SkFilterMode::kLinear), p); |
| 51 |
} |
| 52 |
|
| 53 |
static SkScalar draw_row(SkCanvas* canvas, sk_sp<SkImage> img) { |
| 54 |
SkAutoCanvasRestore acr(canvas, true); |
| 55 |
|
| 56 |
SkPaint paint; |
| 57 |
paint.setAntiAlias(true); |
| 58 |
|
| 59 |
SkScalar x = 0; |
| 60 |
const int scale = 32; |
| 61 |
|
| 62 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 63 |
const char* name = ToolUtils::colortype_name(img->colorType()); |
| 64 |
canvas->drawString(name, x, SkIntToScalar(img->height())*scale*5/8, font, paint); |
| 65 |
canvas->translate(SkIntToScalar(48), 0); |
| 66 |
|
| 67 |
canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 68 |
|
| 69 |
x += draw_set(canvas, img, 0, &paint); |
| 70 |
paint.reset(); |
| 71 |
paint.setAlphaf(0.5f); |
| 72 |
draw_set(canvas, img, x, &paint); |
| 73 |
return x * scale / 3; |
| 74 |
} |
| 75 |
|
| 76 |
class FilterGM : public skiagm::GM { |
| 77 |
void onOnceBeforeDraw() override { |
| 78 |
SkBitmap bm32, bm4444, bm565; |
| 79 |
make_bm(&bm32); |
| 80 |
ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm32); |
| 81 |
ToolUtils::copy_to(&bm565, kRGB_565_SkColorType, bm32); |
| 82 |
|
| 83 |
fImg32 = bm32.asImage(); |
| 84 |
fImg4444 = bm4444.asImage(); |
| 85 |
fImg565 = bm565.asImage(); |
| 86 |
} |
| 87 |
|
| 88 |
public: |
| 89 |
sk_sp<SkImage> fImg32, fImg4444, fImg565; |
| 90 |
|
| 91 |
FilterGM() { |
| 92 |
this->setBGColor(0xFFDDDDDD); |
| 93 |
} |
| 94 |
|
| 95 |
protected: |
| 96 |
SkString getName() const override { return SkString("bitmapfilters"); } |
| 97 |
|
| 98 |
SkISize getISize() override { return SkISize::Make(540, 250); } |
| 99 |
|
| 100 |
void onDraw(SkCanvas* canvas) override { |
| 101 |
SkScalar x = SkIntToScalar(10); |
| 102 |
SkScalar y = SkIntToScalar(10); |
| 103 |
|
| 104 |
canvas->translate(x, y); |
| 105 |
y = draw_row(canvas, fImg4444); |
| 106 |
canvas->translate(0, y); |
| 107 |
y = draw_row(canvas, fImg565); |
| 108 |
canvas->translate(0, y); |
| 109 |
draw_row(canvas, fImg32); |
| 110 |
} |
| 111 |
|
| 112 |
private: |
| 113 |
using INHERITED = skiagm::GM; |
| 114 |
}; |
| 115 |
DEF_GM( return new FilterGM; ) |
| 116 |
|
| 117 |
////////////////////////////////////////////////////////////////////////////// |
| 118 |
|
| 119 |
class TestExtractAlphaGM : public skiagm::GM { |
| 120 |
void onOnceBeforeDraw() override { |
| 121 |
// Make a bitmap with per-pixels alpha (stroked circle) |
| 122 |
fBitmap.allocN32Pixels(100, 100); |
| 123 |
SkCanvas canvas(fBitmap); |
| 124 |
canvas.clear(0); |
| 125 |
|
| 126 |
SkPaint paint; |
| 127 |
paint.setAntiAlias(true); |
| 128 |
paint.setColor(SK_ColorBLUE); |
| 129 |
paint.setStyle(SkPaint::kStroke_Style); |
| 130 |
paint.setStrokeWidth(20); |
| 131 |
|
| 132 |
canvas.drawCircle(50, 50, 39, paint); |
| 133 |
|
| 134 |
fBitmap.extractAlpha(&fAlpha); |
| 135 |
} |
| 136 |
|
| 137 |
public: |
| 138 |
SkBitmap fBitmap, fAlpha; |
| 139 |
|
| 140 |
protected: |
| 141 |
SkString getName() const override { return SkString("extractalpha"); } |
| 142 |
|
| 143 |
SkISize getISize() override { return SkISize::Make(540, 330); } |
| 144 |
|
| 145 |
void onDraw(SkCanvas* canvas) override { |
| 146 |
SkPaint paint; |
| 147 |
paint.setAntiAlias(true); |
| 148 |
paint.setColor(SK_ColorRED); |
| 149 |
|
| 150 |
SkSamplingOptions sampling(SkFilterMode::kLinear); |
| 151 |
|
| 152 |
// should stay blue (ignore paint's color) |
| 153 |
canvas->drawImage(fBitmap.asImage(), 10, 10, sampling, &paint); |
| 154 |
// should draw red |
| 155 |
canvas->drawImage(fAlpha.asImage(), 120, 10, sampling, &paint); |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
using INHERITED = skiagm::GM; |
| 160 |
}; |
| 161 |
DEF_GM( return new TestExtractAlphaGM; ) |