2012-08-13 14:22:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPixelRef.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2012-08-13 14:22:17 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2012-08-13 14:22:17 +00:00
|
|
|
#define WIDTH 500
|
|
|
|
#define HEIGHT 500
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
|
2014-10-23 22:00:10 +00:00
|
|
|
SkPaint filterPaint;
|
|
|
|
filterPaint.setImageFilter(
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::Magnifier(
|
2013-10-21 15:59:26 +00:00
|
|
|
SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
|
2012-08-13 14:22:17 +00:00
|
|
|
SkIntToScalar(WIDTH / 2),
|
|
|
|
SkIntToScalar(HEIGHT / 2)),
|
2016-04-07 14:34:15 +00:00
|
|
|
100, nullptr));
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &filterPaint);
|
2012-08-13 14:22:17 +00:00
|
|
|
const char* str = "The quick brown fox jumped over the lazy dog.";
|
2013-09-16 18:19:30 +00:00
|
|
|
SkRandom rand;
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2012-08-13 14:22:17 +00:00
|
|
|
for (int i = 0; i < 25; ++i) {
|
2013-09-16 18:19:30 +00:00
|
|
|
int x = rand.nextULessThan(WIDTH);
|
|
|
|
int y = rand.nextULessThan(HEIGHT);
|
2014-10-23 22:00:10 +00:00
|
|
|
SkPaint paint;
|
2019-03-20 16:12:10 +00:00
|
|
|
paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
|
2019-01-07 21:01:22 +00:00
|
|
|
font.setSize(rand.nextRangeScalar(0, 300));
|
|
|
|
canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
|
2012-08-13 14:22:17 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define WIDTH_HEIGHT 256
|
|
|
|
|
|
|
|
static sk_sp<SkImage> make_img() {
|
2016-12-20 20:54:11 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
|
|
|
|
SkCanvas canvas(bitmap);
|
2016-04-14 14:54:04 +00:00
|
|
|
|
2016-12-20 20:54:11 +00:00
|
|
|
canvas.clear(0x0);
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
|
2016-12-20 20:54:11 +00:00
|
|
|
canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
|
|
|
|
canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
|
2016-04-14 14:54:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 20:54:11 +00:00
|
|
|
SkBitmap result;
|
|
|
|
result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
|
|
|
|
result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
|
|
|
|
|
2020-12-23 15:11:33 +00:00
|
|
|
return result.asImage();
|
2016-04-14 14:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
|
|
|
|
|
|
|
|
sk_sp<SkImage> image(make_img());
|
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image)));
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
|
|
|
|
SkIntToScalar(WIDTH_HEIGHT-32));
|
|
|
|
srcRect.inset(64.0f, 64.0f);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kInset = 64.0f;
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
// Crop out a 16 pixel ring around the result
|
2019-08-02 19:21:23 +00:00
|
|
|
const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
SkPaint filterPaint;
|
2019-08-02 19:21:23 +00:00
|
|
|
filterPaint.setImageFilter(SkImageFilters::Magnifier(
|
|
|
|
srcRect, kInset, std::move(imageSource), &cropRect));
|
2016-04-14 14:54:04 +00:00
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, &filterPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|