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.h"
9
10 #include "Resources.h"
11 #include "SkBitmapProcState.h"
12 #include "SkBitmapScaler.h"
13 #include "SkGradientShader.h"
14 #include "SkImageDecoder.h"
15 #include "SkImageEncoder.h"
16 #include "SkStream.h"
17 #include "SkTypeface.h"
18
computeSize(const SkBitmap & bm,const SkMatrix & mat)19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
20 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
21 SkIntToScalar(bm.height()));
22 mat.mapRect(&bounds);
23 return SkSize::Make(bounds.width(), bounds.height());
24 }
25
draw_cell(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx,SkFilterQuality lvl)26 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
27 SkFilterQuality lvl) {
28 SkPaint paint;
29 paint.setFilterQuality(lvl);
30
31 SkAutoCanvasRestore acr(canvas, true);
32
33 canvas->translate(dx, 0);
34 canvas->concat(mat);
35 canvas->drawBitmap(bm, 0, 0, &paint);
36 }
37
draw_row(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx)38 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
39 draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality);
40 draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality);
41 draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality);
42 draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality);
43 }
44
45 class FilterIndiaBoxGM : public skiagm::GM {
onOnceBeforeDraw()46 void onOnceBeforeDraw() override {
47 this->makeBitmap();
48
49 SkScalar cx = SkScalarHalf(fBM.width());
50 SkScalar cy = SkScalarHalf(fBM.height());
51
52 float vertScale = 30.0f/55.0f;
53 float horizScale = 150.0f/200.0f;
54
55 fMatrix[0].setScale(horizScale, vertScale);
56 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
57 }
58
59 public:
60 SkBitmap fBM;
61 SkMatrix fMatrix[2];
62 SkString fName;
63
FilterIndiaBoxGM()64 FilterIndiaBoxGM() {
65 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
66 }
67
FilterIndiaBoxGM(const char filename[])68 FilterIndiaBoxGM(const char filename[]) : fFilename(filename) {
69 fName.printf("filterindiabox");
70 }
71
72 protected:
onShortName()73 SkString onShortName() override {
74 return fName;
75 }
76
onISize()77 SkISize onISize() override {
78 return SkISize::Make(1024, 768);
79 }
80
onDraw(SkCanvas * canvas)81 void onDraw(SkCanvas* canvas) override {
82 canvas->translate(10, 10);
83 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
84 SkSize size = computeSize(fBM, fMatrix[i]);
85 size.fWidth += 20;
86 size.fHeight += 20;
87
88 draw_row(canvas, fBM, fMatrix[i], size.fWidth);
89 canvas->translate(0, size.fHeight);
90 }
91 }
92
93 protected:
94 SkString fFilename;
95 int fSize;
96
getScale()97 SkScalar getScale() {
98 return 192.f/fSize;
99 }
100
makeBitmap()101 void makeBitmap() {
102 SkImageDecoder* codec = nullptr;
103 SkString resourcePath = GetResourcePath(fFilename.c_str());
104 SkFILEStream stream(resourcePath.c_str());
105 if (stream.isValid()) {
106 codec = SkImageDecoder::Factory(&stream);
107 }
108 if (codec) {
109 stream.rewind();
110 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
111 delete codec;
112 } else {
113 fBM.allocN32Pixels(1, 1);
114 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
115 }
116 fSize = fBM.height();
117 }
118 private:
119 typedef skiagm::GM INHERITED;
120 };
121
122 //////////////////////////////////////////////////////////////////////////////
123
124
125 DEF_GM( return new FilterIndiaBoxGM("box.gif"); )
126