Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: gm/showmiplevels.cpp

Issue 1574233003: expand gm to exercise miplevels and various scalers (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gm/downsamplebitmap.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 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 "SkBitmapScaler.h"
12 #include "SkGradientShader.h"
13 #include "SkTypeface.h"
14 #include "SkImageDecoder.h"
15 #include "SkStream.h"
16 #include "SkPaint.h"
17 #include "SkMipMap.h"
18 #include "Resources.h"
19 #include "sk_tool_utils.h"
20
21 static SkBitmap make_bitmap(int size) {
22 SkBitmap bm;
23 bm.allocN32Pixels(size, size);
24 SkCanvas canvas(bm);
25 canvas.clear(0xFFFFFFFF);
26 SkPaint paint;
27 paint.setStyle(SkPaint::kStroke_Style);
28 paint.setStrokeWidth(size / 16.0f);
29 canvas.drawCircle(size/2.0f, size/2.0f, size/3.0f, paint);
30 return bm;
31 }
32
33 static SkBitmap make_bitmap2(int size) {
34 SkBitmap bm;
35 bm.allocN32Pixels(size, size);
36 SkCanvas canvas(bm);
37 canvas.clear(0xFFFFFFFF);
38 SkPaint paint;
39 paint.setStyle(SkPaint::kStroke_Style);
40
41 SkScalar inset = 2;
42 SkRect r = SkRect::MakeIWH(size, size).makeInset(0.5f, 0.5f);
43 while (r.width() > 4) {
44 canvas.drawRect(r, paint);
45 r.inset(inset, inset);
46 inset += 1;
47 }
48 return bm;
49 }
50
51 #include "SkNx.h"
52 static SkBitmap make_bitmap3(int size) {
53 SkBitmap bm;
54 bm.allocN32Pixels(size, size);
55 SkCanvas canvas(bm);
56 canvas.clear(0xFFFFFFFF);
57 SkPaint paint;
58 paint.setStyle(SkPaint::kStroke_Style);
59 paint.setStrokeWidth(2.1f);
60
61 SkScalar s = SkIntToScalar(size);
62 Sk4f p(s, -s, -s, s);
63 Sk4f d(5);
64 while (p.kth<1>() < s) {
65 canvas.drawLine(p.kth<0>(),p.kth<1>(), p.kth<2>(), p.kth<3>(), paint);
66 p = p + d;
67 }
68 return bm;
69 }
70
71 class ShowMipLevels : public skiagm::GM {
72 const int fN;
73 SkBitmap fBM[4];
74
75 public:
76 static unsigned gamma(unsigned n) {
77 float x = n / 255.0f;
78 #if 0
79 x = sqrtf(x);
80 #else
81 if (x > 0.0031308f) {
82 x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f;
83 } else {
84 x = 12.92f * x;
85 }
86 #endif
87 return (int)(x * 255);
88 }
89
90 static void apply_gamma(const SkBitmap& bm) {
91 return; // below is our experiment for sRGB correction
92 bm.lockPixels();
93 for (int y = 0; y < bm.height(); ++y) {
94 for (int x = 0; x < bm.width(); ++x) {
95 SkPMColor c = *bm.getAddr32(x, y);
96 unsigned r = gamma(SkGetPackedR32(c));
97 unsigned g = gamma(SkGetPackedG32(c));
98 unsigned b = gamma(SkGetPackedB32(c));
99 *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
100 }
101 }
102 }
103
104 ShowMipLevels(int N) : fN(N) {
105 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(N, N, SK_ColorBLACK, SK_ColorWHITE, 2);
106 fBM[1] = make_bitmap(N);
107 fBM[2] = make_bitmap2(N);
108 fBM[3] = make_bitmap3(N);
109 }
110
111 protected:
112
113 SkString onShortName() override {
114 SkString str;
115 str.printf("showmiplevels_%d", fN);
116 return str;
117 }
118
119 SkISize onISize() override {
120 return { 824, 862 };
121 }
122
123 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
124 SkBitmap bm;
125 orig.copyTo(&bm);
126 apply_gamma(bm);
127
128 canvas->drawBitmap(bm, x, y, nullptr);
129 SkPaint paint;
130 paint.setStyle(SkPaint::kStroke_Style);
131 paint.setColor(0xFFFFCCCC);
132 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
133 }
134
135 template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& base BM, F func) {
136 SkScalar x = 4;
137 SkScalar y = 4;
138
139 SkPixmap prevPM;
140 baseBM.lockPixels();
141 baseBM.peekPixels(&prevPM);
142
143 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
144
145 int index = 0;
146 SkMipMap::Level level;
147 SkScalar scale = 0.5f;
148 while (mm->extractLevel(scale, &level)) {
149 SkImageInfo info = SkImageInfo::MakeN32Premul(level.fWidth, level.fH eight);
150 SkPixmap levelPM{ info, level.fPixels, level.fRowBytes };
151
152 SkBitmap bm = func(prevPM, levelPM);
153 DrawAndFrame(canvas, bm, x, y);
154
155 if (info.width() <= 2 || info.height() <= 2) {
156 break;
157 }
158 if (index & 1) {
159 x += info.width() + 4;
160 } else {
161 y += info.height() + 4;
162 }
163 scale /= 2;
164 prevPM = levelPM;
165 index += 1;
166 }
167 }
168
169 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
170 SkAutoCanvasRestore acr(canvas, true);
171
172 drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
173 SkBitmap bm;
174 bm.installPixels(curr);
175 return bm;
176 });
177
178 const SkBitmapScaler::ResizeMethod methods[] = {
179 SkBitmapScaler::RESIZE_BOX,
180 SkBitmapScaler::RESIZE_TRIANGLE,
181 SkBitmapScaler::RESIZE_LANCZOS3,
182 SkBitmapScaler::RESIZE_HAMMING,
183 SkBitmapScaler::RESIZE_MITCHELL,
184 };
185
186 SkPixmap basePM;
187 orig.lockPixels();
188 orig.peekPixels(&basePM);
189 for (auto method : methods) {
190 canvas->translate(orig.width()/2 + 8.0f, 0);
191 drawLevels(canvas, orig, [basePM, method](const SkPixmap& prev, cons t SkPixmap& curr) {
192 SkBitmap bm;
193 SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.hei ght());
194 return bm;
195 });
196 }
197 }
198
199 void onDraw(SkCanvas* canvas) override {
200 canvas->translate(4, 4);
201 for (const auto& bm : fBM) {
202 this->drawSet(canvas, bm);
203 canvas->translate(0, bm.height() * 0.85f);
204 }
205 }
206
207 private:
208 typedef skiagm::GM INHERITED;
209 };
210 DEF_GM( return new ShowMipLevels(255); )
211 DEF_GM( return new ShowMipLevels(256); )
212
OLDNEW
« no previous file with comments | « gm/downsamplebitmap.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698