Home | History | Annotate | Download | only in ext
      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 SKIA_EXT_CONVOLVER_H_
      6 #define SKIA_EXT_CONVOLVER_H_
      7 
      8 #include <cmath>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "third_party/skia/include/core/SkSize.h"
     13 #include "third_party/skia/include/core/SkTypes.h"
     14 
     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)
     18 #define SIMD_SSE2 1
     19 #define SIMD_PADDING 8  // 8 * int16
     20 #endif
     21 
     22 #if defined (ARCH_CPU_MIPS_FAMILY) && \
     23     defined(__mips_dsp) && (__mips_dsp_rev >= 2)
     24 #define SIMD_MIPS_DSPR2 1
     25 #endif
     26 // avoid confusion with Mac OS X's math library (Carbon)
     27 #if defined(__APPLE__)
     28 #undef FloatToFixed
     29 #undef FixedToFloat
     30 #endif
     31 
     32 namespace skia {
     33 
     34 // Represents a filter in one dimension. Each output pixel has one entry in this
     35 // object for the filter values contributing to it. You build up the filter
     36 // list by calling AddFilter for each output pixel (in order).
     37 //
     38 // We do 2-dimensional convolution by first convolving each row by one
     39 // ConvolutionFilter1D, then convolving each column by another one.
     40 //
     41 // Entries are stored in fixed point, shifted left by kShiftBits.
     42 class ConvolutionFilter1D {
     43  public:
     44   typedef short Fixed;
     45 
     46   // The number of bits that fixed point values are shifted by.
     47   enum { kShiftBits = 14 };
     48 
     49   SK_API ConvolutionFilter1D();
     50   SK_API ~ConvolutionFilter1D();
     51 
     52   // Convert between floating point and our fixed point representation.
     53   static Fixed FloatToFixed(float f) {
     54     return static_cast<Fixed>(f * (1 << kShiftBits));
     55   }
     56   static unsigned char FixedToChar(Fixed x) {
     57     return static_cast<unsigned char>(x >> kShiftBits);
     58   }
     59   static float FixedToFloat(Fixed x) {
     60     // The cast relies on Fixed being a short, implying that on
     61     // the platforms we care about all (16) bits will fit into
     62     // the mantissa of a (32-bit) float.
     63     COMPILE_ASSERT(sizeof(Fixed) == 2, fixed_type_should_fit_in_float_mantissa);
     64     float raw = static_cast<float>(x);
     65     return ldexpf(raw, -kShiftBits);
     66   }
     67 
     68   // Returns the maximum pixel span of a filter.
     69   int max_filter() const { return max_filter_; }
     70 
     71   // Returns the number of filters in this filter. This is the dimension of the
     72   // output image.
     73   int num_values() const { return static_cast<int>(filters_.size()); }
     74 
     75   // Appends the given list of scaling values for generating a given output
     76   // pixel. |filter_offset| is the distance from the edge of the image to where
     77   // the scaling factors start. The scaling factors apply to the source pixels
     78   // starting from this position, and going for the next |filter_length| pixels.
     79   //
     80   // You will probably want to make sure your input is normalized (that is,
     81   // all entries in |filter_values| sub to one) to prevent affecting the overall
     82   // brighness of the image.
     83   //
     84   // The filter_length must be > 0.
     85   //
     86   // This version will automatically convert your input to fixed point.
     87   SK_API void AddFilter(int filter_offset,
     88                         const float* filter_values,
     89                         int filter_length);
     90 
     91   // Same as the above version, but the input is already fixed point.
     92   void AddFilter(int filter_offset,
     93                  const Fixed* filter_values,
     94                  int filter_length);
     95 
     96   // Retrieves a filter for the given |value_offset|, a position in the output
     97   // image in the direction we're convolving. The offset and length of the
     98   // filter values are put into the corresponding out arguments (see AddFilter
     99   // above for what these mean), and a pointer to the first scaling factor is
    100   // returned. There will be |filter_length| values in this array.
    101   inline const Fixed* FilterForValue(int value_offset,
    102                                      int* filter_offset,
    103                                      int* filter_length) const {
    104     const FilterInstance& filter = filters_[value_offset];
    105     *filter_offset = filter.offset;
    106     *filter_length = filter.trimmed_length;
    107     if (filter.trimmed_length == 0) {
    108       return NULL;
    109     }
    110     return &filter_values_[filter.data_location];
    111   }
    112 
    113   // Retrieves the filter for the offset 0, presumed to be the one and only.
    114   // The offset and length of the filter values are put into the corresponding
    115   // out arguments (see AddFilter). Note that |filter_legth| and
    116   // |specified_filter_length| may be different if leading/trailing zeros of the
    117   // original floating point form were clipped.
    118   // There will be |filter_length| values in the return array.
    119   // Returns NULL if the filter is 0-length (for instance when all floating
    120   // point values passed to AddFilter were clipped to 0).
    121   SK_API const Fixed* GetSingleFilter(int* specified_filter_length,
    122                                       int* filter_offset,
    123                                       int* filter_length) const;
    124 
    125   inline void PaddingForSIMD() {
    126     // Padding |padding_count| of more dummy coefficients after the coefficients
    127     // of last filter to prevent SIMD instructions which load 8 or 16 bytes
    128     // together to access invalid memory areas. We are not trying to align the
    129     // coefficients right now due to the opaqueness of <vector> implementation.
    130     // This has to be done after all |AddFilter| calls.
    131 #ifdef SIMD_PADDING
    132     for (int i = 0; i < SIMD_PADDING; ++i)
    133       filter_values_.push_back(static_cast<Fixed>(0));
    134 #endif
    135   }
    136 
    137  private:
    138   struct FilterInstance {
    139     // Offset within filter_values for this instance of the filter.
    140     int data_location;
    141 
    142     // Distance from the left of the filter to the center. IN PIXELS
    143     int offset;
    144 
    145     // Number of values in this filter instance.
    146     int trimmed_length;
    147 
    148     // Filter length as specified. Note that this may be different from
    149     // 'trimmed_length' if leading/trailing zeros of the original floating
    150     // point form were clipped differently on each tail.
    151     int length;
    152   };
    153 
    154   // Stores the information for each filter added to this class.
    155   std::vector<FilterInstance> filters_;
    156 
    157   // We store all the filter values in this flat list, indexed by
    158   // |FilterInstance.data_location| to avoid the mallocs required for storing
    159   // each one separately.
    160   std::vector<Fixed> filter_values_;
    161 
    162   // The maximum size of any filter we've added.
    163   int max_filter_;
    164 };
    165 
    166 // Does a two-dimensional convolution on the given source image.
    167 //
    168 // It is assumed the source pixel offsets referenced in the input filters
    169 // reference only valid pixels, so the source image size is not required. Each
    170 // row of the source image starts |source_byte_row_stride| after the previous
    171 // one (this allows you to have rows with some padding at the end).
    172 //
    173 // The result will be put into the given output buffer. The destination image
    174 // size will be xfilter.num_values() * yfilter.num_values() pixels. It will be
    175 // in rows of exactly xfilter.num_values() * 4 bytes.
    176 //
    177 // |source_has_alpha| is a hint that allows us to avoid doing computations on
    178 // the alpha channel if the image is opaque. If you don't know, set this to
    179 // true and it will work properly, but setting this to false will be a few
    180 // percent faster if you know the image is opaque.
    181 //
    182 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order
    183 // (this is ARGB when loaded into 32-bit words on a little-endian machine).
    184 SK_API void BGRAConvolve2D(const unsigned char* source_data,
    185                            int source_byte_row_stride,
    186                            bool source_has_alpha,
    187                            const ConvolutionFilter1D& xfilter,
    188                            const ConvolutionFilter1D& yfilter,
    189                            int output_byte_row_stride,
    190                            unsigned char* output,
    191                            bool use_simd_if_possible);
    192 
    193 // Does a 1D convolution of the given source image along the X dimension on
    194 // a single channel of the bitmap.
    195 //
    196 // The function uses the same convolution kernel for each pixel. That kernel
    197 // must be added to |filter| at offset 0. This is a most straightforward
    198 // implementation of convolution, intended chiefly for development purposes.
    199 SK_API void SingleChannelConvolveX1D(const unsigned char* source_data,
    200                                      int source_byte_row_stride,
    201                                      int input_channel_index,
    202                                      int input_channel_count,
    203                                      const ConvolutionFilter1D& filter,
    204                                      const SkISize& image_size,
    205                                      unsigned char* output,
    206                                      int output_byte_row_stride,
    207                                      int output_channel_index,
    208                                      int output_channel_count,
    209                                      bool absolute_values);
    210 
    211 // Does a 1D convolution of the given source image along the Y dimension on
    212 // a single channel of the bitmap.
    213 SK_API void SingleChannelConvolveY1D(const unsigned char* source_data,
    214                                      int source_byte_row_stride,
    215                                      int input_channel_index,
    216                                      int input_channel_count,
    217                                      const ConvolutionFilter1D& filter,
    218                                      const SkISize& image_size,
    219                                      unsigned char* output,
    220                                      int output_byte_row_stride,
    221                                      int output_channel_index,
    222                                      int output_channel_count,
    223                                      bool absolute_values);
    224 
    225 // Set up the |filter| instance with a gaussian kernel. |kernel_sigma| is the
    226 // parameter of gaussian. If |derivative| is true, the kernel will be that of
    227 // the first derivative. Intended for use with the two routines above.
    228 SK_API void SetUpGaussianConvolutionKernel(ConvolutionFilter1D* filter,
    229                                            float kernel_sigma,
    230                                            bool derivative);
    231 
    232 }  // namespace skia
    233 
    234 #endif  // SKIA_EXT_CONVOLVER_H_
    235