OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SK_CONVOLVER_H | |
6 #define SK_CONVOLVER_H | |
7 | |
8 #include <cmath> | |
reed1
2013/07/18 13:42:12
must we bring in these (would be a first for skia)
humper
2013/07/18 17:11:04
cmath can probably be replaced by math.h; the othe
| |
9 #include <vector> | |
10 | |
11 #include "SkSize.h" | |
12 #include "SkTypes.h" | |
13 | |
14 // We can build SSE2 optimized versions for all x86 CPUs | |
15 // except when building for the IOS emulator. | |
16 #if defined(SKIA_SSE) | |
17 #define SIMD_SSE2 1 | |
18 #define SIMD_PADDING 8 // 8 * int16 | |
19 #endif | |
20 | |
21 // avoid confusion with Mac OS X's math library (Carbon) | |
22 #if defined(__APPLE__) | |
23 #undef FloatToFixed | |
24 #undef FixedToFloat | |
25 #endif | |
26 | |
27 // Represents a filter in one dimension. Each output pixel has one entry in this | |
28 // object for the filter values contributing to it. You build up the filter | |
29 // list by calling AddFilter for each output pixel (in order). | |
30 // | |
31 // We do 2-dimensional convolution by first convolving each row by one | |
32 // SkConvolutionFilter1D, then convolving each column by another one. | |
33 // | |
34 // Entries are stored in fixed point, shifted left by kShiftBits. | |
35 class SkConvolutionFilter1D { | |
36 public: | |
37 typedef short Fixed; | |
38 | |
39 // The number of bits that fixed point values are shifted by. | |
40 enum { kShiftBits = 14 }; | |
41 | |
42 SK_API SkConvolutionFilter1D(); | |
43 SK_API ~SkConvolutionFilter1D(); | |
44 | |
45 // Convert between floating point and our fixed point representation. | |
46 static Fixed FloatToFixed(float f) { | |
47 return static_cast<Fixed>(f * (1 << kShiftBits)); | |
48 } | |
49 static unsigned char FixedToChar(Fixed x) { | |
50 return static_cast<unsigned char>(x >> kShiftBits); | |
51 } | |
52 static float FixedToFloat(Fixed x) { | |
53 // The cast relies on Fixed being a short, implying that on | |
54 // the platforms we care about all (16) bits will fit into | |
55 // the mantissa of a (32-bit) float. | |
56 SK_COMPILE_ASSERT(sizeof(Fixed) == 2, fixed_type_should_fit_in_float_man tissa); | |
57 float raw = static_cast<float>(x); | |
58 return ldexpf(raw, -kShiftBits); | |
59 } | |
60 | |
61 // Returns the maximum pixel span of a filter. | |
62 int maxFilter() const { return fMaxFilter; } | |
63 | |
64 // Returns the number of filters in this filter. This is the dimension of th e | |
65 // output image. | |
66 int numValues() const { return static_cast<int>(fFilters.size()); } | |
67 | |
68 // Appends the given list of scaling values for generating a given output | |
69 // pixel. |filterOffset| is the distance from the edge of the image to where | |
70 // the scaling factors start. The scaling factors apply to the source pixels | |
71 // starting from this position, and going for the next |filterLength| pixels . | |
72 // | |
73 // You will probably want to make sure your input is normalized (that is, | |
74 // all entries in |filterValuesg| sub to one) to prevent affecting the overa ll | |
75 // brighness of the image. | |
76 // | |
77 // The filterLength must be > 0. | |
78 // | |
79 // This version will automatically convert your input to fixed point. | |
80 SK_API void AddFilter(int filterOffset, | |
81 const float* filterValues, | |
82 int filterLength); | |
83 | |
84 // Same as the above version, but the input is already fixed point. | |
85 void AddFilter(int filterOffset, | |
86 const Fixed* filterValues, | |
87 int filterLength); | |
88 | |
89 // Retrieves a filter for the given |valueOffset|, a position in the output | |
90 // image in the direction we're convolving. The offset and length of the | |
91 // filter values are put into the corresponding out arguments (see AddFilter | |
92 // above for what these mean), and a pointer to the first scaling factor is | |
93 // returned. There will be |filterLength| values in this array. | |
94 inline const Fixed* FilterForValue(int valueOffset, | |
95 int* filterOffset, | |
96 int* filterLength) const { | |
97 const FilterInstance& filter = fFilters[valueOffset]; | |
98 *filterOffset = filter.fOffset; | |
99 *filterLength = filter.fTrimmedLength; | |
100 if (filter.fTrimmedLength == 0) { | |
101 return NULL; | |
102 } | |
103 return &fFilterValues[filter.fDataLocation]; | |
104 } | |
105 | |
106 // Retrieves the filter for the offset 0, presumed to be the one and only. | |
107 // The offset and length of the filter values are put into the corresponding | |
108 // out arguments (see AddFilter). Note that |filterLegth| and | |
109 // |specifiedFilterLength| may be different if leading/trailing zeros of the | |
110 // original floating point form were clipped. | |
111 // There will be |filterLength| values in the return array. | |
112 // Returns NULL if the filter is 0-length (for instance when all floating | |
113 // point values passed to AddFilter were clipped to 0). | |
114 SK_API const Fixed* GetSingleFilter(int* specifiedFilterLength, | |
115 int* filterOffset, | |
116 int* filterLength) const; | |
117 | |
118 inline void PaddingForSIMD() { | |
119 // Padding |paddingCount| of more dummy coefficients after the coefficients | |
120 // of last filter to prevent SIMD instructions which load 8 or 16 bytes | |
121 // together to access invalid memory areas. We are not trying to align the | |
122 // coefficients right now due to the opaqueness of <vector> implementation. | |
123 // This has to be done after all |AddFilter| calls. | |
124 #ifdef SIMD_PADDING | |
125 for (int i = 0; i < SIMD_PADDING; ++i) | |
126 fFilterValues.push_back(static_cast<Fixed>(0)); | |
127 #endif | |
128 } | |
129 | |
130 private: | |
131 struct FilterInstance { | |
132 // Offset within filterValues for this instance of the filter. | |
133 int fDataLocation; | |
134 | |
135 // Distance from the left of the filter to the center. IN PIXELS | |
136 int fOffset; | |
137 | |
138 // Number of values in this filter instance. | |
139 int fTrimmedLength; | |
140 | |
141 // Filter length as specified. Note that this may be different from | |
142 // 'trimmed_length' if leading/trailing zeros of the original floating | |
143 // point form were clipped differently on each tail. | |
144 int fLength; | |
145 }; | |
146 | |
147 // Stores the information for each filter added to this class. | |
148 std::vector<FilterInstance> fFilters; | |
149 | |
150 // We store all the filter values in this flat list, indexed by | |
151 // |FilterInstance.data_location| to avoid the mallocs required for storing | |
152 // each one separately. | |
153 std::vector<Fixed> fFilterValues; | |
154 | |
155 // The maximum size of any filter we've added. | |
156 int fMaxFilter; | |
157 }; | |
158 | |
159 typedef void (*SkConvolveVertically_pointer)( | |
160 const SkConvolutionFilter1D::Fixed* filterValues, | |
161 int filterLength, | |
162 unsigned char* const* sourceDataRows, | |
163 int pixelWidth, | |
164 unsigned char* outRow, | |
165 bool hasAlpha); | |
166 typedef void (*SkConvolve4RowsHorizontally_pointer)( | |
167 const unsigned char* srcData[4], | |
168 const SkConvolutionFilter1D& filter, | |
169 unsigned char* outRow[4]); | |
170 typedef void (*SkConvolveHorizontally_pointer)( | |
171 const unsigned char* srcData, | |
172 const SkConvolutionFilter1D& filter, | |
173 unsigned char* outRow, | |
174 bool hasAlpha); | |
175 | |
176 struct SkConvolutionProcs { | |
177 // This is how many extra pixels may be read by the | |
178 // conolve*horizontally functions. | |
179 int fExtraHorizontalReads; | |
180 SkConvolveVertically_pointer fConvolveVertically; | |
181 SkConvolve4RowsHorizontally_pointer fConvolve4RowsHorizontally; | |
182 SkConvolveHorizontally_pointer fConvolveHorizontally; | |
183 }; | |
184 | |
185 | |
186 | |
187 // Does a two-dimensional convolution on the given source image. | |
188 // | |
189 // It is assumed the source pixel offsets referenced in the input filters | |
190 // reference only valid pixels, so the source image size is not required. Each | |
191 // row of the source image starts |sourceByteRowStride| after the previous | |
192 // one (this allows you to have rows with some padding at the end). | |
193 // | |
194 // The result will be put into the given output buffer. The destination image | |
195 // size will be xfilter.numValues() * yfilter.numValues() pixels. It will be | |
196 // in rows of exactly xfilter.numValues() * 4 bytes. | |
197 // | |
198 // |sourceHasAlpha| is a hint that allows us to avoid doing computations on | |
199 // the alpha channel if the image is opaque. If you don't know, set this to | |
200 // true and it will work properly, but setting this to false will be a few | |
201 // percent faster if you know the image is opaque. | |
202 // | |
203 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order | |
204 // (this is ARGB when loaded into 32-bit words on a little-endian machine). | |
205 SK_API void BGRAConvolve2D(const unsigned char* sourceData, | |
206 int sourceByteRowStride, | |
207 bool sourceHasAlpha, | |
208 const SkConvolutionFilter1D& xfilter, | |
209 const SkConvolutionFilter1D& yfilter, | |
210 int outputByteRowStride, | |
211 unsigned char* output, | |
212 SkConvolutionProcs *convolveProcs, | |
213 bool useSimdIfPossible); | |
214 | |
215 #endif // SK_CONVOLVER_H | |
OLD | NEW |