2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-06-17 15:38:31 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2009-06-23 12:19:41 +00:00
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
static void make_bm(SkBitmap* bm) {
|
2011-05-18 18:54:23 +00:00
|
|
|
const SkColor colors[4] = {
|
2009-06-23 12:19:41 +00:00
|
|
|
SK_ColorRED, SK_ColorGREEN,
|
|
|
|
SK_ColorBLUE, SK_ColorWHITE
|
|
|
|
};
|
2011-05-18 18:54:23 +00:00
|
|
|
SkPMColor colorsPM[4];
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
|
|
|
|
colorsPM[i] = SkPreMultiplyColor(colors[i]);
|
|
|
|
}
|
2017-07-12 19:10:28 +00:00
|
|
|
bm->allocN32Pixels(2, 2, true);
|
|
|
|
|
|
|
|
*bm->getAddr32(0, 0) = colorsPM[0];
|
|
|
|
*bm->getAddr32(1, 0) = colorsPM[1];
|
|
|
|
*bm->getAddr32(0, 1) = colorsPM[2];
|
|
|
|
*bm->getAddr32(1, 1) = colorsPM[3];
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
|
|
|
|
SkScalar x, SkScalar y, SkPaint* paint) {
|
|
|
|
canvas->drawBitmap(bm, x, y, paint);
|
|
|
|
return SkIntToScalar(bm.width()) * 5/4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
|
|
|
|
SkPaint* p) {
|
|
|
|
x += draw_bm(c, bm, x, 0, p);
|
2015-03-16 17:08:34 +00:00
|
|
|
p->setFilterQuality(kLow_SkFilterQuality);
|
2009-06-23 12:19:41 +00:00
|
|
|
x += draw_bm(c, bm, x, 0, p);
|
|
|
|
p->setDither(true);
|
|
|
|
return x + draw_bm(c, bm, x, 0, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2019-01-05 21:35:13 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2009-06-23 12:19:41 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
const int scale = 32;
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
|
|
|
const char* name = ToolUtils::colortype_name(bm.colorType());
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
|
2019-01-05 21:35:13 +00:00
|
|
|
font, paint);
|
2009-06-23 12:19:41 +00:00
|
|
|
canvas->translate(SkIntToScalar(48), 0);
|
|
|
|
|
|
|
|
canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
|
2011-05-18 18:54:23 +00:00
|
|
|
|
2009-06-23 12:19:41 +00:00
|
|
|
x += draw_set(canvas, bm, 0, &paint);
|
|
|
|
paint.reset();
|
2019-02-15 21:13:57 +00:00
|
|
|
paint.setAlphaf(0.5f);
|
2009-06-23 12:19:41 +00:00
|
|
|
draw_set(canvas, bm, x, &paint);
|
|
|
|
return x * scale / 3;
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
class FilterGM : public skiagm::GM {
|
|
|
|
void onOnceBeforeDraw() override {
|
2017-07-12 19:10:28 +00:00
|
|
|
make_bm(&fBM32);
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM32);
|
|
|
|
ToolUtils::copy_to(&fBM16, kRGB_565_SkColorType, fBM32);
|
2012-03-08 22:13:04 +00:00
|
|
|
}
|
2015-05-18 19:57:56 +00:00
|
|
|
|
2012-03-08 22:13:04 +00:00
|
|
|
public:
|
2017-07-12 19:10:28 +00:00
|
|
|
SkBitmap fBM4444, fBM16, fBM32;
|
2012-03-08 22:13:04 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
FilterGM() {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-05-18 19:57:56 +00:00
|
|
|
SkString onShortName() override {
|
2009-06-23 12:19:41 +00:00
|
|
|
return SkString("bitmapfilters");
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
SkISize onISize() override {
|
2017-07-12 19:10:28 +00:00
|
|
|
return SkISize::Make(540, 250);
|
2009-06-23 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2009-06-23 12:19:41 +00:00
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
SkScalar y = SkIntToScalar(10);
|
2011-05-18 18:54:23 +00:00
|
|
|
|
2009-06-23 12:19:41 +00:00
|
|
|
canvas->translate(x, y);
|
|
|
|
y = draw_row(canvas, fBM4444);
|
|
|
|
canvas->translate(0, y);
|
|
|
|
y = draw_row(canvas, fBM16);
|
|
|
|
canvas->translate(0, y);
|
|
|
|
draw_row(canvas, fBM32);
|
|
|
|
}
|
2011-05-18 18:54:23 +00:00
|
|
|
|
2009-06-23 12:19:41 +00:00
|
|
|
private:
|
2015-05-18 19:57:56 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2009-06-23 12:19:41 +00:00
|
|
|
};
|
2015-05-18 19:57:56 +00:00
|
|
|
DEF_GM( return new FilterGM; )
|
2009-06-23 12:19:41 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
class TestExtractAlphaGM : public skiagm::GM {
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
// Make a bitmap with per-pixels alpha (stroked circle)
|
|
|
|
fBitmap.allocN32Pixels(100, 100);
|
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
canvas.clear(0);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(20);
|
|
|
|
|
|
|
|
canvas.drawCircle(50, 50, 39, paint);
|
|
|
|
|
|
|
|
fBitmap.extractAlpha(&fAlpha);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
public:
|
|
|
|
SkBitmap fBitmap, fAlpha;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("extractalpha");
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(540, 330);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
canvas->drawBitmap(fBitmap, 10, 10, &paint); // should stay blue (ignore paint's color)
|
|
|
|
canvas->drawBitmap(fAlpha, 120, 10, &paint); // should draw red
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-05-18 19:57:56 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new TestExtractAlphaGM; )
|