OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 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 "SkTestImageFilters.h" | |
9 #include "SkCanvas.h" | |
10 #include "SkReadBuffer.h" | |
11 #include "SkSpecialImage.h" | |
12 #include "SkSpecialSurface.h" | |
13 #include "SkWriteBuffer.h" | |
14 | |
15 /////////////////////////////////////////////////////////////////////////////// | |
16 | |
17 sk_sp<SkImageFilter> SkDownSampleImageFilter::Make(SkScalar scale, sk_sp<SkImage
Filter> input) { | |
18 if (!SkScalarIsFinite(scale)) { | |
19 return nullptr; | |
20 } | |
21 // we don't support scale in this range | |
22 if (scale > SK_Scalar1 || scale <= 0) { | |
23 return nullptr; | |
24 } | |
25 return sk_sp<SkImageFilter>(new SkDownSampleImageFilter(scale, std::move(inp
ut))); | |
26 } | |
27 | |
28 sk_sp<SkSpecialImage> SkDownSampleImageFilter::onFilterImage(SkSpecialImage* sou
rce, | |
29 const Context& ctx, | |
30 SkIPoint* offset) c
onst { | |
31 if (fScale > SK_Scalar1 || fScale <= 0) { | |
32 return nullptr; | |
33 } | |
34 | |
35 int dstW = SkScalarRoundToInt(source->width() * fScale); | |
36 int dstH = SkScalarRoundToInt(source->height() * fScale); | |
37 if (dstW < 1) { | |
38 dstW = 1; | |
39 } | |
40 if (dstH < 1) { | |
41 dstH = 1; | |
42 } | |
43 | |
44 sk_sp<SkSpecialImage> tmp; | |
45 | |
46 // downsample | |
47 { | |
48 const SkImageInfo info = SkImageInfo::MakeN32Premul(dstW, dstH); | |
49 | |
50 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | |
51 if (!surf) { | |
52 return nullptr; | |
53 } | |
54 | |
55 SkCanvas* canvas = surf->getCanvas(); | |
56 SkASSERT(canvas); | |
57 | |
58 canvas->clear(0x0); | |
59 | |
60 SkPaint paint; | |
61 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
62 paint.setFilterQuality(kLow_SkFilterQuality); | |
63 | |
64 canvas->scale(fScale, fScale); | |
65 source->draw(canvas, 0, 0, &paint); | |
66 | |
67 tmp = surf->makeImageSnapshot(); | |
68 } | |
69 | |
70 // upscale | |
71 { | |
72 const SkImageInfo info = SkImageInfo::MakeN32Premul(source->width(), sou
rce->height()); | |
73 | |
74 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | |
75 if (!surf) { | |
76 return nullptr; | |
77 } | |
78 | |
79 SkCanvas* canvas = surf->getCanvas(); | |
80 SkASSERT(canvas); | |
81 | |
82 canvas->clear(0x0); | |
83 | |
84 SkPaint paint; | |
85 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
86 | |
87 canvas->scale(SkScalarInvert(fScale), SkScalarInvert(fScale)); | |
88 tmp->draw(canvas, 0, 0, &paint); | |
89 | |
90 return surf->makeImageSnapshot(); | |
91 } | |
92 } | |
93 | |
94 sk_sp<SkFlattenable> SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { | |
95 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | |
96 return Make(buffer.readScalar(), common.getInput(0)); | |
97 } | |
98 | |
99 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { | |
100 this->INHERITED::flatten(buffer); | |
101 buffer.writeScalar(fScale); | |
102 } | |
103 | |
104 #ifndef SK_IGNORE_TO_STRING | |
105 void SkDownSampleImageFilter::toString(SkString* str) const { | |
106 str->appendf("SkDownSampleImageFilter: ("); | |
107 str->append(")"); | |
108 } | |
109 #endif | |
OLD | NEW |