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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFilterQuality.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPixmap.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "tools/Resources.h"
25 #include "tools/ToolUtils.h"
26
27 static const char* kFilterQualityNames[] = { "none", "low", "medium", "high" };
28
29 struct DownsampleBitmapGM : public skiagm::GM {
30 SkBitmap (*fMakeBitmap)(SkImageInfo);
31 SkString fName;
32 SkFilterQuality fFilterQuality;
33
DownsampleBitmapGMDownsampleBitmapGM34 DownsampleBitmapGM(SkBitmap (*fn)(SkImageInfo), const char* kind, SkFilterQuality fq)
35 : fMakeBitmap(fn)
36 , fName(SkStringPrintf("downsamplebitmap_%s_%s", kind, kFilterQualityNames[fq]))
37 , fFilterQuality(fq)
38 {
39 this->setBGColor(0xFFDDDDDD);
40 }
41
onShortNameDownsampleBitmapGM42 SkString onShortName() override { return fName; }
43
onISizeDownsampleBitmapGM44 SkISize onISize() override {
45 SkBitmap bm = fMakeBitmap(SkImageInfo::MakeN32Premul(1,1)/*whatever*/);
46 return SkISize::Make(bm.width(), 4 * bm.height());
47 }
48
onDrawDownsampleBitmapGM49 void onDraw(SkCanvas* canvas) override {
50 SkImageInfo info = canvas->imageInfo();
51 if (!info.colorType()) { info = info.makeColorType( kN32_SkColorType); }
52 if (!info.alphaType()) { info = info.makeAlphaType(kPremul_SkAlphaType); }
53
54 SkBitmap bm = fMakeBitmap(info);
55
56 int curY = 0;
57 int curHeight;
58 float curScale = 1;
59 do {
60
61 SkMatrix matrix;
62 matrix.setScale( curScale, curScale );
63
64 SkPaint paint;
65 paint.setFilterQuality(fFilterQuality);
66
67 canvas->save();
68 canvas->translate(0, (SkScalar)curY);
69 canvas->concat(matrix);
70 canvas->drawBitmap(bm, 0, 0, &paint);
71 canvas->restore();
72
73 curHeight = (int) (bm.height() * curScale + 2);
74 curY += curHeight;
75 curScale *= 0.75f;
76 } while (curHeight >= 2 && curY < 4*bm.height());
77 }
78 };
79
convert_bitmap_format(SkBitmap src,SkImageInfo info)80 static SkBitmap convert_bitmap_format(SkBitmap src, SkImageInfo info) {
81 SkBitmap dst;
82 dst.allocPixels(info.makeWH(src.width(), src.height()));
83
84 SkPixmap pm;
85 SkAssertResult(dst.peekPixels(&pm));
86 SkAssertResult(src.readPixels(pm));
87
88 return dst;
89 }
90
91
make_text(SkImageInfo info)92 static SkBitmap make_text(SkImageInfo info) {
93 const SkScalar textSize = 72;
94
95 SkBitmap bm;
96 bm.allocPixels(info.makeWH(int(textSize * 8), int(textSize * 6)));
97 SkCanvas canvas(bm);
98 canvas.drawColor(SK_ColorWHITE);
99
100 SkPaint paint;
101 SkFont font;
102 font.setSubpixel(true);
103 font.setSize(textSize);
104
105 font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
106 canvas.drawString("Hamburgefons", textSize/2, 1.2f*textSize, font, paint);
107 font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold()));
108 canvas.drawString("Hamburgefons", textSize/2, 2.4f*textSize, font, paint);
109 font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()));
110 canvas.drawString("Hamburgefons", textSize/2, 3.6f*textSize, font, paint);
111 font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::BoldItalic()));
112 canvas.drawString("Hamburgefons", textSize/2, 4.8f*textSize, font, paint);
113
114 return bm;
115 }
116 DEF_GM( return new DownsampleBitmapGM(make_text, "text", kHigh_SkFilterQuality); )
117 DEF_GM( return new DownsampleBitmapGM(make_text, "text", kMedium_SkFilterQuality); )
118 DEF_GM( return new DownsampleBitmapGM(make_text, "text", kLow_SkFilterQuality); )
119 DEF_GM( return new DownsampleBitmapGM(make_text, "text", kNone_SkFilterQuality); )
120
121
make_checkerboard(SkImageInfo info)122 static SkBitmap make_checkerboard(SkImageInfo info) {
123 const auto size = 512;
124 const auto numChecks = 256;
125
126 SkBitmap bm;
127 bm.allocN32Pixels(size,size);
128 for (int y = 0; y < size; ++y) {
129 for (int x = 0; x < size; ++x) {
130 SkPMColor* s = bm.getAddr32(x, y);
131 int cx = (x * numChecks) / size;
132 int cy = (y * numChecks) / size;
133 if ((cx+cy)%2) {
134 *s = 0xFFFFFFFF;
135 } else {
136 *s = 0xFF000000;
137 }
138 }
139 }
140 return convert_bitmap_format(bm, info);
141 }
142 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kHigh_SkFilterQuality); )
143 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kMedium_SkFilterQuality); )
144 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kLow_SkFilterQuality); )
145 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kNone_SkFilterQuality); )
146
147
make_image(SkImageInfo info)148 static SkBitmap make_image(SkImageInfo info) {
149 SkBitmap bm;
150 if (!GetResourceAsBitmap("images/mandrill_512.png", &bm)) {
151 bm.allocN32Pixels(1, 1);
152 bm.eraseARGB(255, 255, 0 , 0); // red == bad
153 }
154 return convert_bitmap_format(bm, info);
155 }
156 DEF_GM( return new DownsampleBitmapGM(make_image, "image", kHigh_SkFilterQuality); )
157 DEF_GM( return new DownsampleBitmapGM(make_image, "image", kMedium_SkFilterQuality); )
158 DEF_GM( return new DownsampleBitmapGM(make_image, "image", kLow_SkFilterQuality); )
159 DEF_GM( return new DownsampleBitmapGM(make_image, "image", kNone_SkFilterQuality); )
160