senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 "SkColorFilterImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 11 | #include "SkColorMatrixFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
| 13 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| 15 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 16 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | void mult_color_matrix(SkScalar a[20], SkScalar b[20], SkScalar out[20]) { |
| 20 | for (int j = 0; j < 4; ++j) { |
| 21 | for (int i = 0; i < 5; ++i) { |
| 22 | out[i+j*5] = 4 == i ? a[4+j*5] : 0; |
| 23 | for (int k = 0; k < 4; ++k) |
| 24 | out[i+j*5] += SkScalarMul(a[k+j*5], b[i+k*5]); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // To detect if we need to apply clamping after applying a matrix, we check if |
| 30 | // any output component might go outside of [0, 255] for any combination of |
| 31 | // input components in [0..255]. |
| 32 | // Each output component is an affine transformation of the input component, so |
| 33 | // the minimum and maximum values are for any combination of minimum or maximum |
| 34 | // values of input components (i.e. 0 or 255). |
| 35 | // E.g. if R' = x*R + y*G + z*B + w*A + t |
| 36 | // Then the maximum value will be for R=255 if x>0 or R=0 if x<0, and the |
| 37 | // minimum value will be for R=0 if x>0 or R=255 if x<0. |
| 38 | // Same goes for all components. |
| 39 | bool component_needs_clamping(SkScalar row[5]) { |
| 40 | SkScalar maxValue = row[4] / 255; |
| 41 | SkScalar minValue = row[4] / 255; |
| 42 | for (int i = 0; i < 4; ++i) { |
| 43 | if (row[i] > 0) |
| 44 | maxValue += row[i]; |
| 45 | else |
| 46 | minValue += row[i]; |
| 47 | } |
| 48 | return (maxValue > 1) || (minValue < 0); |
| 49 | } |
| 50 | |
| 51 | bool matrix_needs_clamping(SkScalar matrix[20]) { |
| 52 | return component_needs_clamping(matrix) |
| 53 | || component_needs_clamping(matrix+5) |
| 54 | || component_needs_clamping(matrix+10) |
| 55 | || component_needs_clamping(matrix+15); |
| 56 | } |
| 57 | |
| 58 | }; |
| 59 | |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 60 | SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 61 | SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) { |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 62 | SkASSERT(cf); |
| 63 | SkScalar colorMatrix[20], inputMatrix[20]; |
| 64 | SkColorFilter* inputColorFilter; |
| 65 | if (input && cf->asColorMatrix(colorMatrix) |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 66 | && input->asColorFilter(&inputColorFilter) |
| 67 | && (NULL != inputColorFilter)) { |
| 68 | SkAutoUnref autoUnref(inputColorFilter); |
| 69 | if (inputColorFilter->asColorMatrix(inputMatrix) && !matrix_needs_clamping(inputMatrix)) { |
| 70 | SkScalar combinedMatrix[20]; |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 71 | mult_color_matrix(colorMatrix, inputMatrix, combinedMatrix); |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 72 | SkAutoTUnref<SkColorFilter> newCF(SkColorMatrixFilter::Create(combinedMatrix)); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 73 | return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0), cropRect, 0)); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 74 | } |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 75 | } |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 76 | return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input, cropRect, uniqueID)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 79 | SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 80 | SkImageFilter* input, const CropRect* cropRect, uint32_t uniqueID) |
| 81 | : INHERITED(1, &input, cropRect, uniqueID), fColorFilter(cf) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 82 | SkASSERT(cf); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 83 | SkSafeRef(cf); |
| 84 | } |
| 85 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 86 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 87 | SkColorFilterImageFilter::SkColorFilterImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 88 | : INHERITED(1, buffer) { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 89 | fColorFilter = buffer.readColorFilter(); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 90 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | SkFlattenable* SkColorFilterImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 94 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
reed | f0f6c40 | 2014-08-22 05:08:26 -0700 | [diff] [blame] | 95 | SkAutoTUnref<SkColorFilter> cf(buffer.readColorFilter()); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 96 | return Create(cf, common.getInput(0), &common.cropRect(), common.uniqueID()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 97 | } |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 99 | void SkColorFilterImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 100 | this->INHERITED::flatten(buffer); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 101 | buffer.writeFlattenable(fColorFilter); |
| 102 | } |
| 103 | |
| 104 | SkColorFilterImageFilter::~SkColorFilterImageFilter() { |
| 105 | SkSafeUnref(fColorFilter); |
| 106 | } |
| 107 | |
| 108 | bool SkColorFilterImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 109 | const Context& ctx, |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 110 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 111 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 112 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 113 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 114 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 118 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 119 | if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 123 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 124 | if (NULL == device.get()) { |
| 125 | return false; |
| 126 | } |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 127 | SkCanvas canvas(device.get()); |
| 128 | SkPaint paint; |
| 129 | |
| 130 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 131 | paint.setColorFilter(fColorFilter); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 132 | canvas.drawSprite(src, srcOffset.fX - bounds.fLeft, srcOffset.fY - bounds.fTop, &paint); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 133 | |
| 134 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 135 | offset->fX = bounds.fLeft; |
| 136 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 137 | return true; |
| 138 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 139 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 140 | bool SkColorFilterImageFilter::asColorFilter(SkColorFilter** filter) const { |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 141 | if (!cropRectIsSet()) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 142 | if (filter) { |
| 143 | *filter = fColorFilter; |
| 144 | fColorFilter->ref(); |
| 145 | } |
| 146 | return true; |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 147 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 148 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 149 | } |