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

Side by Side Diff: gm/spritebitmap.cpp

Issue 1390913005: add applyFilter() to SkImage (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase to new effect factories, use stroke to show image bounds Created 5 years, 2 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 | « no previous file | include/core/SkDevice.h » ('j') | src/image/SkImage_Gpu.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkBlurImageFilter.h" 10 #include "SkBlurImageFilter.h"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 draw_2_bitmaps(canvas, bm, true, dx, dy); 90 draw_2_bitmaps(canvas, bm, true, dx, dy);
91 dy += bm.height() + 20; 91 dy += bm.height() + 20;
92 draw_2_bitmaps(canvas, bm, true, dx, dy, filter); 92 draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
93 } 93 }
94 94
95 private: 95 private:
96 typedef GM INHERITED; 96 typedef GM INHERITED;
97 }; 97 };
98 DEF_GM( return new SpriteBitmapGM; ) 98 DEF_GM( return new SpriteBitmapGM; )
99 99
100 //////////////////////////////////////////////////////////////////////////////// ///////////////////
101
102 #include "SkColorFilterImageFilter.h"
103 #include "SkModeColorFilter.h"
104 #include "SkMorphologyImageFilter.h"
105 #include "SkOffsetImageFilter.h"
106
107 static SkImage* make_image(SkCanvas* rootCanvas) {
108 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
109 SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info));
110 if (!surface) {
111 surface.reset(SkSurface::NewRaster(info));
112 }
113
114 SkPaint paint;
115 paint.setAntiAlias(true);
116 paint.setColor(SK_ColorRED);
117 surface->getCanvas()->drawCircle(50, 50, 50, paint);
118 return surface->newImageSnapshot();
119 }
120
121 static void show_image(SkCanvas* canvas, SkImage* image, const SkIPoint& offset) {
122 SkScalar x = SkIntToScalar(offset.x());
123 SkScalar y = SkIntToScalar(offset.y());
124
125 SkPaint paint;
126 paint.setStyle(SkPaint::kStroke_Style);
127
128 SkRect r = SkRect::MakeIWH(image->width(), image->height());
129 r.offset(x, y);
130 // get on pixel-centers to make the hairline land on a numerical stable boun dary
131 r.outset(SK_ScalarHalf, SK_ScalarHalf);
132 canvas->drawRect(r, paint);
133
134 canvas->drawImage(image, x, y, nullptr);
135 }
136
137 typedef SkImageFilter* (*ImageFilterFactory)();
138
139 // +[]{...} did not work on windows (VS)
140 // (ImageFilterFactory)[]{...} did not work on linux (gcc)
141 // hence this cast function
142 template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; }
143
144 /**
145 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
146 */
147 class ApplyFilterGM : public skiagm::GM {
148 public:
149 ApplyFilterGM() {}
150
151 protected:
152 SkString onShortName() override {
153 return SkString("apply-filter");
154 }
155
156 SkISize onISize() override {
157 return SkISize::Make(640, 480);
158 }
159
160 void onDraw(SkCanvas* canvas) override {
161 SkAutoTUnref<SkImage> image0(make_image(canvas));
162
163 const ImageFilterFactory factories[] = {
164 IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }),
165 IFCCast([]{ SkAutoTUnref<SkColorFilter> cf(SkModeColorFilter::Create (SK_ColorBLUE,
166 SkXfermode: :kSrcIn_Mode));
167 return SkColorFilterImageFilter::Create(cf);
168 }),
169 IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }),
170 IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }),
171 IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }),
172 };
173
174 const SkScalar spacer = image0->width() * 3.0f / 2;
175
176 for (auto&& factory : factories) {
177 SkAutoTUnref<SkImageFilter> filter(factory());
178
179 SkIPoint offset1, offset2;
180 SkAutoTUnref<SkImage> image1(image0->applyFilter(filter, &offset1, t rue));
181 SkAutoTUnref<SkImage> image2(image0->applyFilter(filter, &offset2, f alse));
182
183 canvas->save();
184 canvas->translate(30, 30);
185 show_image(canvas, image0, SkIPoint::Make(0, 0)); // original
186 canvas->translate(spacer, 0);
187 show_image(canvas, image1, offset1); // snug
188 canvas->translate(spacer, 0);
189 show_image(canvas, image2, offset2); // not snug
190
191 // Try drawing the original w/ the filter, to see that it "draws" th e same as
192 // when we have manually applied the filter (above).
193 {
194 SkPaint paint;
195 paint.setImageFilter(filter);
196
197 SkBitmap bm;
198 image0->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode);
199 SkPoint loc = { 0, 0 };
200 canvas->translate(spacer, 0);
201 canvas->getTotalMatrix().mapPoints(&loc, 1);
202 canvas->drawSprite(bm, (int)loc.x(), (int)loc.y(), &paint); // l ike snug
203
204 canvas->translate(spacer, 0);
205 canvas->drawImage(image0, 0, 0, &paint); // like not snug
206 }
207 canvas->restore();
208
209 canvas->translate(0, spacer);
210 }
211 }
212
213 private:
214 typedef GM INHERITED;
215 };
216 DEF_GM( return new ApplyFilterGM; )
217
OLDNEW
« no previous file with comments | « no previous file | include/core/SkDevice.h » ('j') | src/image/SkImage_Gpu.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698