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