reed | c15d957 | 2015-06-09 13:13:16 -0700 | [diff] [blame] | 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 | */ |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 7 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 8 | #include "SkTestImageFilters.h" |
| 9 | #include "SkCanvas.h" |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 10 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 13 | |
reed@google.com | e763951 | 2012-08-07 14:25:44 +0000 | [diff] [blame] | 14 | // Simple helper canvas that "takes ownership" of the provided device, so that |
| 15 | // when this canvas goes out of scope, so will its device. Could be replaced |
| 16 | // with the following: |
| 17 | // |
| 18 | // SkCanvas canvas(device); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 19 | // SkAutoTUnref<SkBaseDevice> aur(device); |
reed@google.com | e763951 | 2012-08-07 14:25:44 +0000 | [diff] [blame] | 20 | // |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 21 | class OwnDeviceCanvas : public SkCanvas { |
| 22 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 23 | OwnDeviceCanvas(SkBaseDevice* device) : SkCanvas(device) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 24 | SkSafeUnref(device); |
| 25 | } |
| 26 | }; |
| 27 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 30 | bool SkDownSampleImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 31 | const Context&, |
| 32 | SkBitmap* result, SkIPoint*) const { |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 33 | SkScalar scale = fScale; |
| 34 | if (scale > SK_Scalar1 || scale <= 0) { |
| 35 | return false; |
| 36 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 37 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 38 | int dstW = SkScalarRoundToInt(src.width() * scale); |
| 39 | int dstH = SkScalarRoundToInt(src.height() * scale); |
| 40 | if (dstW < 1) { |
| 41 | dstW = 1; |
| 42 | } |
| 43 | if (dstH < 1) { |
| 44 | dstH = 1; |
| 45 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 46 | |
| 47 | SkBitmap tmp; |
| 48 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 49 | // downsample |
| 50 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 51 | SkBaseDevice* dev = proxy->createDevice(dstW, dstH); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | if (nullptr == dev) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | OwnDeviceCanvas canvas(dev); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 56 | SkPaint paint; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 57 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 58 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 59 | canvas.scale(scale, scale); |
| 60 | canvas.drawBitmap(src, 0, 0, &paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 61 | tmp = dev->accessBitmap(false); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 62 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 63 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 64 | // upscale |
| 65 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 66 | SkBaseDevice* dev = proxy->createDevice(src.width(), src.height()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | if (nullptr == dev) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | OwnDeviceCanvas canvas(dev); |
| 71 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 72 | canvas.drawBitmapRect(tmp, SkRect::MakeIWH(src.width(), src.height()), nullptr); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 73 | *result = dev->accessBitmap(false); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 78 | SkFlattenable* SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 79 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 80 | return Create(buffer.readScalar(), common.getInput(0)); |
| 81 | } |
| 82 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 83 | void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 84 | this->INHERITED::flatten(buffer); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 85 | buffer.writeScalar(fScale); |
| 86 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 87 | |
| 88 | #ifndef SK_IGNORE_TO_STRING |
| 89 | void SkDownSampleImageFilter::toString(SkString* str) const { |
| 90 | str->appendf("SkDownSampleImageFilter: ("); |
| 91 | str->append(")"); |
| 92 | } |
| 93 | #endif |