1 /*
2 * Copyright 2013 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.h"
9 #include "SkColor.h"
10 #include "SkBicubicImageFilter.h"
11
12 namespace skiagm {
13
14 class BicubicGM : public GM {
15 public:
BicubicGM()16 BicubicGM() : fInitialized(false) {
17 this->setBGColor(0x00000000);
18 }
19
20 protected:
onGetFlags() const21 virtual uint32_t onGetFlags() const SK_OVERRIDE {
22 return kSkipTiled_Flag;
23 }
24
onShortName()25 virtual SkString onShortName() {
26 return SkString("bicubicfilter");
27 }
28
make_checkerboard(int width,int height)29 void make_checkerboard(int width, int height) {
30 SkASSERT(width % 2 == 0);
31 SkASSERT(height % 2 == 0);
32 fCheckerboard.allocN32Pixels(width, height);
33 for (int y = 0; y < height; y += 2) {
34 SkPMColor* s = fCheckerboard.getAddr32(0, y);
35 for (int x = 0; x < width; x += 2) {
36 *s++ = 0xFFFFFFFF;
37 *s++ = 0xFF000000;
38 }
39 s = fCheckerboard.getAddr32(0, y + 1);
40 for (int x = 0; x < width; x += 2) {
41 *s++ = 0xFF000000;
42 *s++ = 0xFFFFFFFF;
43 }
44 }
45 }
46
onISize()47 virtual SkISize onISize() {
48 return SkISize::Make(400, 300);
49 }
50
onDraw(SkCanvas * canvas)51 virtual void onDraw(SkCanvas* canvas) {
52 if (!fInitialized) {
53 make_checkerboard(4, 4);
54 fInitialized = true;
55 }
56 SkScalar sk32 = SkIntToScalar(32);
57 canvas->clear(0x00000000);
58 SkPaint bilinearPaint, bicubicPaint;
59 SkSize scale = SkSize::Make(sk32, sk32);
60 canvas->save();
61 canvas->scale(sk32, sk32);
62 bilinearPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
63 canvas->drawBitmap(fCheckerboard, 0, 0, &bilinearPaint);
64 canvas->restore();
65 SkAutoTUnref<SkImageFilter> bicubic(SkBicubicImageFilter::CreateMitchell(scale));
66 bicubicPaint.setImageFilter(bicubic);
67 SkRect srcBounds;
68 fCheckerboard.getBounds(&srcBounds);
69 canvas->translate(SkIntToScalar(140), 0);
70 canvas->saveLayer(&srcBounds, &bicubicPaint);
71 canvas->drawBitmap(fCheckerboard, 0, 0);
72 canvas->restore();
73 }
74
75 private:
76 typedef GM INHERITED;
77 SkBitmap fCheckerboard;
78 bool fInitialized;
79 };
80
81 //////////////////////////////////////////////////////////////////////////////
82
MyFactory(void *)83 static GM* MyFactory(void*) { return new BicubicGM; }
84 static GMRegistry reg(MyFactory);
85
86 }
87