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