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.
5 #ifndef SKIA_EXT_CONVOLVER_H_
6 #define SKIA_EXT_CONVOLVER_H_
11 #include "base/basictypes.h"
13 #include "third_party/skia/include/core/SkTypes.h"
15 // We can build SSE2 optimized versions for all x86 CPUs
16 // except when building for the IOS emulator.
17 #if defined(ARCH_CPU_X86_FAMILY) && !defined(OS_IOS)
19 #define SIMD_PADDING 8 // 8 * int16
22 // avoid confusion with Mac OS X's math library (Carbon)
23 #if defined(__APPLE__)
30 // Represents a filter in one dimension. Each output pixel has one entry in this
31 // object for the filter values contributing to it. You build up the filter
32 // list by calling AddFilter for each output pixel (in order).
34 // We do 2-dimensional convolution by first convolving each row by one
35 // ConvolutionFilter1D, then convolving each column by another one.
37 // Entries are stored in fixed point, shifted left by kShiftBits.
38 class ConvolutionFilter1D
{
42 // The number of bits that fixed point values are shifted by.
43 enum { kShiftBits
= 14 };
45 SK_API
ConvolutionFilter1D();
46 SK_API
~ConvolutionFilter1D();
48 // Convert between floating point and our fixed point representation.
49 static Fixed
FloatToFixed(float f
) {
50 return static_cast<Fixed
>(f
* (1 << kShiftBits
));
52 static unsigned char FixedToChar(Fixed x
) {
53 return static_cast<unsigned char>(x
>> kShiftBits
);
55 static float FixedToFloat(Fixed x
) {
56 // The cast relies on Fixed being a short, implying that on
57 // the platforms we care about all (16) bits will fit into
58 // the mantissa of a (32-bit) float.
59 COMPILE_ASSERT(sizeof(Fixed
) == 2, fixed_type_should_fit_in_float_mantissa
);
60 float raw
= static_cast<float>(x
);
61 return ldexpf(raw
, -kShiftBits
);
64 // Returns the maximum pixel span of a filter.
65 int max_filter() const { return max_filter_
; }
67 // Returns the number of filters in this filter. This is the dimension of the
69 int num_values() const { return static_cast<int>(filters_
.size()); }
71 // Appends the given list of scaling values for generating a given output
72 // pixel. |filter_offset| is the distance from the edge of the image to where
73 // the scaling factors start. The scaling factors apply to the source pixels
74 // starting from this position, and going for the next |filter_length| pixels.
76 // You will probably want to make sure your input is normalized (that is,
77 // all entries in |filter_values| sub to one) to prevent affecting the overall
78 // brighness of the image.
80 // The filter_length must be > 0.
82 // This version will automatically convert your input to fixed point.
83 SK_API
void AddFilter(int filter_offset
,
84 const float* filter_values
,
87 // Same as the above version, but the input is already fixed point.
88 void AddFilter(int filter_offset
,
89 const Fixed
* filter_values
,
92 // Retrieves a filter for the given |value_offset|, a position in the output
93 // image in the direction we're convolving. The offset and length of the
94 // filter values are put into the corresponding out arguments (see AddFilter
95 // above for what these mean), and a pointer to the first scaling factor is
96 // returned. There will be |filter_length| values in this array.
97 inline const Fixed
* FilterForValue(int value_offset
,
99 int* filter_length
) const {
100 const FilterInstance
& filter
= filters_
[value_offset
];
101 *filter_offset
= filter
.offset
;
102 *filter_length
= filter
.length
;
103 if (filter
.length
== 0) {
106 return &filter_values_
[filter
.data_location
];
110 inline void PaddingForSIMD() {
111 // Padding |padding_count| of more dummy coefficients after the coefficients
112 // of last filter to prevent SIMD instructions which load 8 or 16 bytes
113 // together to access invalid memory areas. We are not trying to align the
114 // coefficients right now due to the opaqueness of <vector> implementation.
115 // This has to be done after all |AddFilter| calls.
117 for (int i
= 0; i
< SIMD_PADDING
; ++i
)
118 filter_values_
.push_back(static_cast<Fixed
>(0));
123 struct FilterInstance
{
124 // Offset within filter_values for this instance of the filter.
127 // Distance from the left of the filter to the center. IN PIXELS
130 // Number of values in this filter instance.
134 // Stores the information for each filter added to this class.
135 std::vector
<FilterInstance
> filters_
;
137 // We store all the filter values in this flat list, indexed by
138 // |FilterInstance.data_location| to avoid the mallocs required for storing
139 // each one separately.
140 std::vector
<Fixed
> filter_values_
;
142 // The maximum size of any filter we've added.
146 // Does a two-dimensional convolution on the given source image.
148 // It is assumed the source pixel offsets referenced in the input filters
149 // reference only valid pixels, so the source image size is not required. Each
150 // row of the source image starts |source_byte_row_stride| after the previous
151 // one (this allows you to have rows with some padding at the end).
153 // The result will be put into the given output buffer. The destination image
154 // size will be xfilter.num_values() * yfilter.num_values() pixels. It will be
155 // in rows of exactly xfilter.num_values() * 4 bytes.
157 // |source_has_alpha| is a hint that allows us to avoid doing computations on
158 // the alpha channel if the image is opaque. If you don't know, set this to
159 // true and it will work properly, but setting this to false will be a few
160 // percent faster if you know the image is opaque.
162 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order
163 // (this is ARGB when loaded into 32-bit words on a little-endian machine).
164 SK_API
void BGRAConvolve2D(const unsigned char* source_data
,
165 int source_byte_row_stride
,
166 bool source_has_alpha
,
167 const ConvolutionFilter1D
& xfilter
,
168 const ConvolutionFilter1D
& yfilter
,
169 int output_byte_row_stride
,
170 unsigned char* output
,
171 bool use_simd_if_possible
);
174 #endif // SKIA_EXT_CONVOLVER_H_