Index: gm/image.cpp |
diff --git a/gm/image.cpp b/gm/image.cpp |
index 8fe96d118521a9b92b15c66ea8a424a30dc593d5..d980cb7562e2f67d9862f6c64c1b552180f44b51 100644 |
--- a/gm/image.cpp |
+++ b/gm/image.cpp |
@@ -6,10 +6,11 @@ |
*/ |
#include "gm.h" |
-#include "SkSurface.h" |
+#include "SkData.h" |
#include "SkCanvas.h" |
+#include "SkRandom.h" |
#include "SkStream.h" |
-#include "SkData.h" |
+#include "SkSurface.h" |
#if SK_SUPPORT_GPU |
#include "GrContext.h" |
@@ -196,3 +197,95 @@ private: |
}; |
DEF_GM( return new ImageGM; ) |
+class ImageResizeGM : public skiagm::GM { |
+ enum { |
+ W = 100, |
+ H = 100, |
+ }; |
+public: |
+ ImageResizeGM() {} |
+ |
+protected: |
+ SkString onShortName() SK_OVERRIDE { return SkString("image-resize"); } |
+ |
+ SkISize onISize() SK_OVERRIDE { return SkISize::Make(510, 480); } |
+ |
+ void drawIntoImage(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); |
+ } |
+ } |
+ |
+ SkImage* makeImage(SkCanvas* canvas) { |
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); |
+ SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); |
+ if (!surface) { |
+ surface.reset(SkSurface::NewRaster(info)); |
+ } |
+ this->drawIntoImage(surface->getCanvas()); |
+ return surface->newImageSnapshot(); |
+ } |
+ |
+ void drawResized(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); |
+ } |
+ |
+ void drawImage(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) { |
+ |
+ canvas->drawImage(image, 0, 0, NULL); |
+ canvas->translate(image->width() + 20.0f, 0); |
+ this->drawResized(canvas, image, image->width()*4/10, image->height()*4/10, NULL, fq); |
+ |
+ SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2); |
+ this->drawResized(canvas, image, W, H, &subset, fq); |
+ } |
+ |
+ void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
+ canvas->translate(10, 10); |
+ |
+ SkAutoTUnref<SkImage> image(this->makeImage(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); |
+ this->drawImage(canvas, image, fq[i]); |
+ } |
+ canvas->translate(0, image->height() + 20.0f); |
+ } |
+ } |
+ |
+private: |
+ typedef skiagm::GM INHERITED; |
+}; |
+DEF_GM( return new ImageResizeGM; ) |
+ |