blob: 76a0c9367f9b71af2aff042624e81ea85b61c5e1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkColorFilter_DEFINED
11#define SkColorFilter_DEFINED
12
13#include "SkColor.h"
14#include "SkFlattenable.h"
reed@android.com845fdac2009-06-23 03:01:32 +000015#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000017class SK_API SkColorFilter : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000019 SK_DECLARE_INST_COUNT(SkColorFilter)
20
reed@google.com43c50c82011-04-14 15:50:52 +000021 /**
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.orge5ff3ce2011-12-20 20:58:18 +000028 /**
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.com71918402012-01-05 17:24:35 +000035 /**
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.com8a1c16f2008-12-17 15:59:43 +000053 /** 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.com6b7aee32011-04-19 18:36:09 +000088 /**
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.com1447c6f2011-04-27 14:09:52 +000095
reed@android.com845fdac2009-06-23 03:01:32 +000096 /** 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.com8a1c16f2008-12-17 15:59:43 +000098 mode will have no effect on the result).
reed@android.com845fdac2009-06-23 03:01:32 +000099 @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.com8a1c16f2008-12-17 15:59:43 +0000101 the colorfilter's filterSpan[16,32] methods
reed@android.com845fdac2009-06-23 03:01:32 +0000102 @return colorfilter object that applies the src color and mode,
103 or NULL if the mode will have no effect.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 */
reed@android.com845fdac2009-06-23 03:01:32 +0000105 static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 /** 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.comd26147a2011-12-15 14:16:43 +0000113
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000114 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115protected:
116 SkColorFilter() {}
117 SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {}
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119private:
120 typedef SkFlattenable INHERITED;
121};
122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123#endif