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

Side by Side Diff: gm/resizeimagefilter.cpp

Issue 136863006: Implement a resize image filter. This is needed for the "filterRes" feature in SVG filter effects, … (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix docs Created 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | gyp/effects.gypi » ('j') | include/effects/SkResizeImageFilter.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 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 #include "SkColor.h"
10 #include "SkResizeImageFilter.h"
11
12 namespace skiagm {
13
14 // Don't use SkBitmapSource here, since it will resize automatically by the CTM.
15 // We want the resize image filter to take care of that, so this one simply retu rns the bitmap.
16
17 class SimpleBitmapSource : public SkImageFilter {
18 public:
19 SimpleBitmapSource(const SkBitmap& bitmap)
20 : INHERITED(0, 0),
21 fBitmap(bitmap) {
22 }
23
24 SimpleBitmapSource(SkFlattenableReadBuffer& buffer)
25 : INHERITED(0, buffer) {
26 fBitmap.unflatten(buffer);
27 }
28
29 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
30 this->INHERITED::flatten(buffer);
31 fBitmap.flatten(buffer);
32 }
33
34 virtual bool onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix& ma trix,
35 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
36 *result = fBitmap;
37 offset->fX = offset->fY = 0;
38 return true;
39 }
40
41 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SimpleBitmapSource)
42
43 private:
44 SkBitmap fBitmap;
45 typedef SkImageFilter INHERITED;
46 };
47
48 class ResizeGM : public GM {
49 public:
50 ResizeGM() : fInitialized(false) {
51 this->setBGColor(0x00000000);
52 }
53
54 protected:
55 virtual SkString onShortName() {
56 return SkString("resizeimagefilter");
57 }
58
59 void make_checkerboard(int width, int height) {
60 SkASSERT(width % 2 == 0);
61 SkASSERT(height % 2 == 0);
62 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, width, height);
63 fCheckerboard.allocPixels();
64 SkAutoLockPixels lock(fCheckerboard);
65 for (int y = 0; y < height; y += 2) {
66 SkPMColor* s = fCheckerboard.getAddr32(0, y);
67 for (int x = 0; x < width; x += 2) {
68 *s++ = 0xFFFFFFFF;
69 *s++ = 0xFF000000;
70 }
71 s = fCheckerboard.getAddr32(0, y + 1);
72 for (int x = 0; x < width; x += 2) {
73 *s++ = 0xFF000000;
74 *s++ = 0xFFFFFFFF;
75 }
76 }
77 }
78
79 virtual SkISize onISize() {
80 return make_isize(400, 300);
81 }
82
83 virtual void onDraw(SkCanvas* canvas) {
84 if (!fInitialized) {
85 make_checkerboard(4, 4);
86 fInitialized = true;
87 }
88 canvas->clear(0x00000000);
89 SkPaint paint;
90 SkSize scale = SkSize::Make(SkIntToScalar(32), SkIntToScalar(32));
91 SkAutoTUnref<SkImageFilter> source(new SimpleBitmapSource(fCheckerboard) );
92 SkAutoTUnref<SkImageFilter> noneResize(new SkResizeImageFilter(scale, Sk Paint::kNone_FilterLevel, source.get()));
93 SkAutoTUnref<SkImageFilter> lowResize(new SkResizeImageFilter(scale, SkP aint::kLow_FilterLevel, source.get()));
94 SkAutoTUnref<SkImageFilter> mediumResize(new SkResizeImageFilter(scale, SkPaint::kMedium_FilterLevel, source.get()));
95 SkAutoTUnref<SkImageFilter> highResize(new SkResizeImageFilter(scale, Sk Paint::kHigh_FilterLevel, source.get()));
96 paint.setFilterLevel(SkPaint::kNone_FilterLevel); // Outer paint does no filtering; leave it all for the filter
97
98 SkRect srcRect;
99 fCheckerboard.getBounds(&srcRect);
100
101 paint.setImageFilter(noneResize);
102 canvas->drawRect(srcRect, paint);
103
104 paint.setImageFilter(lowResize);
105 canvas->translate(SkIntToScalar(140), 0);
106 canvas->drawRect(srcRect, paint);
107
108 paint.setImageFilter(mediumResize);
109 canvas->translate(SkIntToScalar(140), 0);
110 canvas->drawRect(srcRect, paint);
111
112 paint.setImageFilter(highResize);
113 canvas->translate(SkIntToScalar(140), 0);
114 canvas->drawRect(srcRect, paint);
115 }
116
117 private:
118 typedef GM INHERITED;
119 SkBitmap fCheckerboard;
120 bool fInitialized;
121 };
122
123 //////////////////////////////////////////////////////////////////////////////
124
125 static GM* MyFactory(void*) { return new ResizeGM; }
126 static GMRegistry reg(MyFactory);
127
128 }
OLDNEW
« no previous file with comments | « no previous file | gyp/effects.gypi » ('j') | include/effects/SkResizeImageFilter.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698