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 "SkDevice.h" 11 #include "SkReadBuffer.h" 12 #include "SkWriteBuffer.h" 13 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); 19 // SkAutoTUnref<SkBaseDevice> aur(device); 20 // 21 class OwnDeviceCanvas : public SkCanvas { 22 public: 23 OwnDeviceCanvas(SkBaseDevice* device) : SkCanvas(device) { 24 SkSafeUnref(device); 25 } 26 }; 27 28 /////////////////////////////////////////////////////////////////////////////// 29 30 bool SkDownSampleImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src, 31 const Context&, 32 SkBitmap* result, SkIPoint*) const { 33 SkScalar scale = fScale; 34 if (scale > SK_Scalar1 || scale <= 0) { 35 return false; 36 } 37 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 } 46 47 SkBitmap tmp; 48 49 // downsample 50 { 51 SkBaseDevice* dev = proxy->createDevice(dstW, dstH); 52 if (nullptr == dev) { 53 return false; 54 } 55 OwnDeviceCanvas canvas(dev); 56 SkPaint paint; 57 58 paint.setFilterQuality(kLow_SkFilterQuality); 59 canvas.scale(scale, scale); 60 canvas.drawBitmap(src, 0, 0, &paint); 61 tmp = dev->accessBitmap(false); 62 } 63 64 // upscale 65 { 66 SkBaseDevice* dev = proxy->createDevice(src.width(), src.height()); 67 if (nullptr == dev) { 68 return false; 69 } 70 OwnDeviceCanvas canvas(dev); 71 72 canvas.drawBitmapRect(tmp, SkRect::MakeIWH(src.width(), src.height()), nullptr); 73 *result = dev->accessBitmap(false); 74 } 75 return true; 76 } 77 78 SkFlattenable* SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { 79 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); 80 return Create(buffer.readScalar(), common.getInput(0)); 81 } 82 83 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { 84 this->INHERITED::flatten(buffer); 85 buffer.writeScalar(fScale); 86 } 87 88 #ifndef SK_IGNORE_TO_STRING 89 void SkDownSampleImageFilter::toString(SkString* str) const { 90 str->appendf("SkDownSampleImageFilter: ("); 91 str->append(")"); 92 } 93 #endif 94