2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapDevice.h"
10 #include "SkBitmapSource.h"
12 #include "SkMatrixImageFilter.h"
17 class ResizeGM : public GM {
20 this->setBGColor(0x00000000);
24 virtual SkString onShortName() {
25 return SkString("resizeimagefilter");
28 void draw(SkCanvas* canvas,
30 const SkSize& deviceSize,
31 SkPaint::FilterLevel filterLevel,
32 SkImageFilter* input = NULL) {
34 canvas->getTotalMatrix().mapRect(&dstRect, rect);
36 SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width());
37 SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height());
38 canvas->translate(rect.x(), rect.y());
39 canvas->scale(deviceScaleX, deviceScaleY);
40 canvas->translate(-rect.x(), -rect.y());
42 matrix.setScale(SkScalarInvert(deviceScaleX),
43 SkScalarInvert(deviceScaleY));
44 SkAutoTUnref<SkImageFilter> imageFilter(
45 SkMatrixImageFilter::Create(matrix, filterLevel, input));
46 SkPaint filteredPaint;
47 filteredPaint.setImageFilter(imageFilter.get());
48 canvas->saveLayer(&rect, &filteredPaint);
50 paint.setColor(0xFF00FF00);
51 SkRect ovalRect = rect;
52 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
53 canvas->drawOval(ovalRect, paint);
54 canvas->restore(); // for saveLayer
58 virtual SkISize onISize() {
59 return SkISize::Make(520, 100);
62 virtual void onDraw(SkCanvas* canvas) {
63 canvas->clear(0x00000000);
65 SkRect srcRect = SkRect::MakeWH(96, 96);
67 SkSize deviceSize = SkSize::Make(16, 16);
71 SkPaint::kNone_FilterLevel);
73 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
77 SkPaint::kLow_FilterLevel);
79 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
83 SkPaint::kMedium_FilterLevel);
85 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
89 SkPaint::kHigh_FilterLevel);
92 bitmap.allocN32Pixels(16, 16);
93 bitmap.eraseARGB(0x00, 0x00, 0x00, 0x00);
95 SkBitmapDevice bitmapDevice(bitmap);
96 SkCanvas bitmapCanvas(&bitmapDevice);
98 paint.setColor(0xFF00FF00);
99 SkRect ovalRect = SkRect::MakeWH(16, 16);
100 ovalRect.inset(SkScalarDiv(2.0f, 3.0f), SkScalarDiv(2.0f, 3.0f));
101 bitmapCanvas.drawOval(ovalRect, paint);
103 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
104 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
105 SkAutoTUnref<SkBitmapSource> source(SkBitmapSource::Create(bitmap, inRect, outRect));
106 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
110 SkPaint::kHigh_FilterLevel,
115 typedef GM INHERITED;
118 //////////////////////////////////////////////////////////////////////////////
120 static GM* MyFactory(void*) { return new ResizeGM; }
121 static GMRegistry reg(MyFactory);