Index: gm/image.cpp |
diff --git a/gm/image.cpp b/gm/image.cpp |
index 8fe96d118521a9b92b15c66ea8a424a30dc593d5..3181c9caaf6dd940c7ddac73a7b1e4fcf6db06b5 100644 |
--- a/gm/image.cpp |
+++ b/gm/image.cpp |
@@ -196,3 +196,99 @@ private: |
}; |
DEF_GM( return new ImageGM; ) |
+class ImageResizeGM : public skiagm::GM { |
+ enum { |
+ W = 100, |
+ H = 100, |
+ }; |
+public: |
+ ImageResizeGM() {} |
+ |
+protected: |
robertphillips
2015/01/22 15:50:02
one line ?
reed1
2015/01/22 18:25:51
Done.
|
+ SkString onShortName() SK_OVERRIDE { |
+ return SkString("image-resize"); |
+ } |
+ |
robertphillips
2015/01/22 15:50:02
one line ?
reed1
2015/01/22 18:25:51
Done.
|
+ SkISize onISize() SK_OVERRIDE { |
+ return SkISize::Make(510, 480); |
+ } |
+ |
robertphillips
2015/01/22 15:50:02
drawIntoImage or make static (and then DrawIntoIma
reed1
2015/01/22 18:25:51
Done.
|
+ void draw_into_image(SkCanvas* canvas) { |
+ SkPaint paint; |
+ paint.setAntiAlias(true); |
+ paint.setStyle(SkPaint::kStroke_Style); |
+ paint.setStrokeWidth(3); |
+ SkRandom rand; |
+ for (int i = 0; i < 60; ++i) { |
+ paint.setColor(rand.nextU()); |
+ SkScalar x = rand.nextUScalar1() * W; |
+ SkScalar y = rand.nextUScalar1() * H; |
+ SkScalar r = rand.nextUScalar1() * W / 2; |
+ canvas->drawCircle(x, y, r, paint); |
+ } |
+ } |
+ |
robertphillips
2015/01/22 15:50:02
makeImage or make static ?
reed1
2015/01/22 18:25:51
Done.
|
+ SkImage* make_image(SkCanvas* canvas) { |
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); |
+ SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); |
+ if (!surface) { |
+ surface.reset(SkSurface::NewRaster(info)); |
+ } |
+ draw_into_image(surface->getCanvas()); |
+ return surface->newImageSnapshot(); |
+ } |
+ |
robertphillips
2015/01/22 15:50:02
same
reed1
2015/01/22 18:25:51
Done.
|
+ void draw_resized(SkCanvas* canvas, SkImage* image, int newW, int newH, const SkIRect* subset, |
+ SkFilterQuality fq) { |
+ // canvas method |
+ SkPaint paint; |
+ paint.setFilterQuality(fq); |
+ SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH)); |
+ SkRect srcR; |
+ if (subset) { |
+ srcR.set(*subset); |
+ } |
+ canvas->drawImageRect(image, subset ? &srcR : NULL, dstR, &paint); |
+ canvas->translate(newW + 20.0f, 0); |
+ |
+ // image method |
+ SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq)); |
+ canvas->drawImage(image2, 0, 0, NULL); |
+ canvas->translate(image2->width() + 20.0f, 0); |
+ } |
+ |
robertphillips
2015/01/22 15:50:02
same
reed1
2015/01/22 18:25:51
Done.
|
+ void draw_image(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) { |
+ |
+ canvas->drawImage(image, 0, 0, NULL); |
+ canvas->translate(image->width() + 20.0f, 0); |
+ this->draw_resized(canvas, image, image->width() * 0.4f, image->height() * 0.4f, NULL, fq); |
+ |
+ SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2); |
robertphillips
2015/01/22 15:50:02
this-> ?
reed1
2015/01/22 18:25:51
Done.
|
+ this->draw_resized(canvas, image, W, H, &subset, fq); |
+ } |
+ |
+ void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
+ canvas->translate(10, 10); |
+ |
robertphillips
2015/01/22 15:50:02
SkAutoTUnref ?
reed1
2015/01/22 18:25:51
Done.
|
+ SkImage* image = make_image(canvas); |
+ |
+ const SkFilterQuality fq[] = { |
+ kNone_SkFilterQuality, |
+ kLow_SkFilterQuality, |
+ kMedium_SkFilterQuality, |
+ kHigh_SkFilterQuality, |
+ }; |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) { |
+ { |
+ SkAutoCanvasRestore acr(canvas, true); |
+ draw_image(canvas, image, fq[i]); |
+ } |
+ canvas->translate(0, image->height() + 20.0f); |
+ } |
+ } |
+ |
+private: |
+ typedef skiagm::GM INHERITED; |
+}; |
+DEF_GM( return new ImageResizeGM; ) |
+ |