epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkColorFilter_DEFINED |
| 11 | #define SkColorFilter_DEFINED |
| 12 | |
| 13 | #include "SkColor.h" |
| 14 | #include "SkFlattenable.h" |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 15 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 17 | class SK_API SkColorFilter : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 19 | SK_DECLARE_INST_COUNT(SkColorFilter) |
| 20 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 21 | /** |
| 22 | * If the filter can be represented by a source color plus Mode, this |
| 23 | * returns true, and sets (if not NULL) the color and mode appropriately. |
| 24 | * If not, this returns false and ignores the parameters. |
| 25 | */ |
| 26 | virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode); |
| 27 | |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 28 | /** |
| 29 | * If the filter can be represented by a 5x4 matrix, this |
| 30 | * returns true, and sets the matrix appropriately. |
| 31 | * If not, this returns false and ignores the parameter. |
| 32 | */ |
| 33 | virtual bool asColorMatrix(SkScalar matrix[20]); |
| 34 | |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 35 | /** |
| 36 | * If the filter can be represented by per-component table, return true, |
| 37 | * and if table is not null, copy the bitmap containing the table into it. |
| 38 | * |
| 39 | * The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding |
| 40 | * to each component in ARGB order. e.g. row[0] == alpha, row[1] == red, |
| 41 | * etc. To transform a color, you (logically) perform the following: |
| 42 | * |
| 43 | * a' = *table.getAddr8(a, 0); |
| 44 | * r' = *table.getAddr8(r, 1); |
| 45 | * g' = *table.getAddr8(g, 2); |
| 46 | * b' = *table.getAddr8(b, 3); |
| 47 | * |
| 48 | * The original component value is the horizontal index for a given row, |
| 49 | * and the stored value at that index is the new value for that component. |
| 50 | */ |
| 51 | virtual bool asComponentTable(SkBitmap* table); |
| 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | /** Called with a scanline of colors, as if there was a shader installed. |
| 54 | The implementation writes out its filtered version into result[]. |
| 55 | Note: shader and result may be the same buffer. |
| 56 | @param src array of colors, possibly generated by a shader |
| 57 | @param count the number of entries in the src[] and result[] arrays |
| 58 | @param result written by the filter |
| 59 | */ |
| 60 | virtual void filterSpan(const SkPMColor src[], int count, |
| 61 | SkPMColor result[]) = 0; |
| 62 | /** Called with a scanline of colors, as if there was a shader installed. |
| 63 | The implementation writes out its filtered version into result[]. |
| 64 | Note: shader and result may be the same buffer. |
| 65 | @param src array of colors, possibly generated by a shader |
| 66 | @param count the number of entries in the src[] and result[] arrays |
| 67 | @param result written by the filter |
| 68 | */ |
| 69 | virtual void filterSpan16(const uint16_t shader[], int count, |
| 70 | uint16_t result[]); |
| 71 | |
| 72 | enum Flags { |
| 73 | /** If set the filter methods will not change the alpha channel of the |
| 74 | colors. |
| 75 | */ |
| 76 | kAlphaUnchanged_Flag = 0x01, |
| 77 | /** If set, this subclass implements filterSpan16(). If this flag is |
| 78 | set, then kAlphaUnchanged_Flag must also be set. |
| 79 | */ |
| 80 | kHasFilter16_Flag = 0x02 |
| 81 | }; |
| 82 | |
| 83 | /** Returns the flags for this filter. Override in subclasses to return |
| 84 | custom flags. |
| 85 | */ |
| 86 | virtual uint32_t getFlags() { return 0; } |
| 87 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Apply this colorfilter to the specified SkColor. This routine handles |
| 90 | * converting to SkPMColor, calling the filter, and then converting back |
| 91 | * to SkColor. This method is not virtual, but will call filterSpan() |
| 92 | * which is virtual. |
| 93 | */ |
| 94 | SkColor filterColor(SkColor); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 95 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 96 | /** Create a colorfilter that uses the specified color and mode. |
| 97 | If the Mode is DST, this function will return NULL (since that |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | mode will have no effect on the result). |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 99 | @param c The source color used with the specified mode |
| 100 | @param mode The xfermode mode that is applied to each color in |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | the colorfilter's filterSpan[16,32] methods |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 102 | @return colorfilter object that applies the src color and mode, |
| 103 | or NULL if the mode will have no effect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | */ |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 105 | static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | /** Create a colorfilter that multiplies the RGB channels by one color, and |
| 108 | then adds a second color, pinning the result for each component to |
| 109 | [0..255]. The alpha components of the mul and add arguments |
| 110 | are ignored. |
| 111 | */ |
| 112 | static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add); |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 113 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 114 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | protected: |
| 116 | SkColorFilter() {} |
| 117 | SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {} |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | private: |
| 120 | typedef SkFlattenable INHERITED; |
| 121 | }; |
| 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | #endif |