OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (surf2) { | 185 if (surf2) { |
186 canvas->translate(80, 0); | 186 canvas->translate(80, 0); |
187 test_surface(canvas, surf2, true); | 187 test_surface(canvas, surf2, true); |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 private: | 191 private: |
192 typedef skiagm::GM INHERITED; | 192 typedef skiagm::GM INHERITED; |
193 }; | 193 }; |
194 DEF_GM( return new ImageGM; ) | 194 DEF_GM( return new ImageGM; ) |
195 | |
196 class ImageResizeGM : public skiagm::GM { | |
197 enum { | |
198 W = 100, | |
199 H = 100, | |
200 }; | |
201 public: | |
202 ImageResizeGM() {} | |
203 | |
204 protected: | |
205 SkString onShortName() override { return SkString("image-resize"); } | |
206 | |
207 SkISize onISize() override { return SkISize::Make(510, 480); } | |
208 | |
209 void drawIntoImage(SkCanvas* canvas) { | |
210 SkPaint paint; | |
211 paint.setAntiAlias(true); | |
212 paint.setStyle(SkPaint::kStroke_Style); | |
213 paint.setStrokeWidth(3); | |
214 SkRandom rand; | |
215 for (int i = 0; i < 60; ++i) { | |
216 paint.setColor(rand.nextU()); | |
217 SkScalar x = rand.nextUScalar1() * W; | |
218 SkScalar y = rand.nextUScalar1() * H; | |
219 SkScalar r = rand.nextUScalar1() * W / 2; | |
220 canvas->drawCircle(x, y, r, paint); | |
221 } | |
222 } | |
223 | |
224 SkImage* makeImage(SkCanvas* canvas) { | |
225 const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); | |
226 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); | |
227 if (!surface) { | |
228 surface.reset(SkSurface::NewRaster(info)); | |
229 } | |
230 this->drawIntoImage(surface->getCanvas()); | |
231 return surface->newImageSnapshot(); | |
232 } | |
233 | |
234 void drawResized(SkCanvas* canvas, SkImage* image, int newW, int newH, const
SkIRect* subset, | |
235 SkFilterQuality fq) { | |
236 // canvas method | |
237 SkPaint paint; | |
238 paint.setFilterQuality(fq); | |
239 SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH)); | |
240 SkRect srcR; | |
241 if (subset) { | |
242 srcR.set(*subset); | |
243 } | |
244 canvas->legacy_drawImageRect(image, subset ? &srcR : nullptr, dstR, &pai
nt); | |
245 canvas->translate(newW + 20.0f, 0); | |
246 | |
247 // image method | |
248 SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq)); | |
249 canvas->drawImage(image2, 0, 0, nullptr); | |
250 canvas->translate(image2->width() + 20.0f, 0); | |
251 } | |
252 | |
253 void drawImage(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) { | |
254 | |
255 canvas->drawImage(image, 0, 0, nullptr); | |
256 canvas->translate(image->width() + 20.0f, 0); | |
257 this->drawResized(canvas, image, image->width()*4/10, image->height()*4/
10, nullptr, fq); | |
258 | |
259 SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2); | |
260 this->drawResized(canvas, image, W, H, &subset, fq); | |
261 } | |
262 | |
263 void onDraw(SkCanvas* canvas) override { | |
264 canvas->translate(10, 10); | |
265 | |
266 SkAutoTUnref<SkImage> image(this->makeImage(canvas)); | |
267 | |
268 const SkFilterQuality fq[] = { | |
269 kNone_SkFilterQuality, | |
270 kLow_SkFilterQuality, | |
271 kMedium_SkFilterQuality, | |
272 kHigh_SkFilterQuality, | |
273 }; | |
274 for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) { | |
275 { | |
276 SkAutoCanvasRestore acr(canvas, true); | |
277 this->drawImage(canvas, image, fq[i]); | |
278 } | |
279 canvas->translate(0, image->height() + 20.0f); | |
280 } | |
281 } | |
282 | |
283 private: | |
284 typedef skiagm::GM INHERITED; | |
285 }; | |
286 DEF_GM( return new ImageResizeGM; ) | |
287 | |
OLD | NEW |