michael@0: // Copyright (c) 2006-2012 The Chromium Authors. All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions michael@0: // are met: michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above copyright michael@0: // notice, this list of conditions and the following disclaimer in michael@0: // the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google, Inc. nor the names of its contributors michael@0: // may be used to endorse or promote products derived from this michael@0: // software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, michael@0: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS michael@0: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED michael@0: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, michael@0: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT michael@0: // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: // SUCH DAMAGE. michael@0: michael@0: #ifndef SKIA_EXT_CONVOLVER_H_ michael@0: #define SKIA_EXT_CONVOLVER_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/cpu.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "skia/SkTypes.h" michael@0: michael@0: // avoid confusion with Mac OS X's math library (Carbon) michael@0: #if defined(__APPLE__) michael@0: #undef FloatToFixed michael@0: #undef FixedToFloat michael@0: #endif michael@0: michael@0: namespace skia { michael@0: michael@0: // Represents a filter in one dimension. Each output pixel has one entry in this michael@0: // object for the filter values contributing to it. You build up the filter michael@0: // list by calling AddFilter for each output pixel (in order). michael@0: // michael@0: // We do 2-dimensional convolution by first convolving each row by one michael@0: // ConvolutionFilter1D, then convolving each column by another one. michael@0: // michael@0: // Entries are stored in fixed point, shifted left by kShiftBits. michael@0: class ConvolutionFilter1D { michael@0: public: michael@0: typedef short Fixed; michael@0: michael@0: // The number of bits that fixed point values are shifted by. michael@0: enum { kShiftBits = 14 }; michael@0: michael@0: ConvolutionFilter1D(); michael@0: ~ConvolutionFilter1D(); michael@0: michael@0: // Convert between floating point and our fixed point representation. michael@0: static Fixed FloatToFixed(float f) { michael@0: return static_cast(f * (1 << kShiftBits)); michael@0: } michael@0: static unsigned char FixedToChar(Fixed x) { michael@0: return static_cast(x >> kShiftBits); michael@0: } michael@0: static float FixedToFloat(Fixed x) { michael@0: // The cast relies on Fixed being a short, implying that on michael@0: // the platforms we care about all (16) bits will fit into michael@0: // the mantissa of a (32-bit) float. michael@0: static_assert(sizeof(Fixed) == 2, michael@0: "fixed type should fit in float mantissa"); michael@0: float raw = static_cast(x); michael@0: return ldexpf(raw, -kShiftBits); michael@0: } michael@0: michael@0: // Returns the maximum pixel span of a filter. michael@0: int max_filter() const { return max_filter_; } michael@0: michael@0: // Returns the number of filters in this filter. This is the dimension of the michael@0: // output image. michael@0: int num_values() const { return static_cast(filters_.size()); } michael@0: michael@0: // Appends the given list of scaling values for generating a given output michael@0: // pixel. |filter_offset| is the distance from the edge of the image to where michael@0: // the scaling factors start. The scaling factors apply to the source pixels michael@0: // starting from this position, and going for the next |filter_length| pixels. michael@0: // michael@0: // You will probably want to make sure your input is normalized (that is, michael@0: // all entries in |filter_values| sub to one) to prevent affecting the overall michael@0: // brighness of the image. michael@0: // michael@0: // The filter_length must be > 0. michael@0: // michael@0: // This version will automatically convert your input to fixed point. michael@0: void AddFilter(int filter_offset, michael@0: const float* filter_values, michael@0: int filter_length); michael@0: michael@0: // Same as the above version, but the input is already fixed point. michael@0: void AddFilter(int filter_offset, michael@0: const Fixed* filter_values, michael@0: int filter_length); michael@0: michael@0: // Retrieves a filter for the given |value_offset|, a position in the output michael@0: // image in the direction we're convolving. The offset and length of the michael@0: // filter values are put into the corresponding out arguments (see AddFilter michael@0: // above for what these mean), and a pointer to the first scaling factor is michael@0: // returned. There will be |filter_length| values in this array. michael@0: inline const Fixed* FilterForValue(int value_offset, michael@0: int* filter_offset, michael@0: int* filter_length) const { michael@0: const FilterInstance& filter = filters_[value_offset]; michael@0: *filter_offset = filter.offset; michael@0: *filter_length = filter.length; michael@0: if (filter.length == 0) { michael@0: return NULL; michael@0: } michael@0: return &filter_values_[filter.data_location]; michael@0: } michael@0: michael@0: michael@0: inline void PaddingForSIMD(int padding_count) { michael@0: // Padding |padding_count| of more dummy coefficients after the coefficients michael@0: // of last filter to prevent SIMD instructions which load 8 or 16 bytes michael@0: // together to access invalid memory areas. We are not trying to align the michael@0: // coefficients right now due to the opaqueness of implementation. michael@0: // This has to be done after all |AddFilter| calls. michael@0: for (int i = 0; i < padding_count; ++i) michael@0: filter_values_.push_back(static_cast(0)); michael@0: } michael@0: michael@0: private: michael@0: struct FilterInstance { michael@0: // Offset within filter_values for this instance of the filter. michael@0: int data_location; michael@0: michael@0: // Distance from the left of the filter to the center. IN PIXELS michael@0: int offset; michael@0: michael@0: // Number of values in this filter instance. michael@0: int length; michael@0: }; michael@0: michael@0: // Stores the information for each filter added to this class. michael@0: std::vector filters_; michael@0: michael@0: // We store all the filter values in this flat list, indexed by michael@0: // |FilterInstance.data_location| to avoid the mallocs required for storing michael@0: // each one separately. michael@0: std::vector filter_values_; michael@0: michael@0: // The maximum size of any filter we've added. michael@0: int max_filter_; michael@0: }; michael@0: michael@0: // Does a two-dimensional convolution on the given source image. michael@0: // michael@0: // It is assumed the source pixel offsets referenced in the input filters michael@0: // reference only valid pixels, so the source image size is not required. Each michael@0: // row of the source image starts |source_byte_row_stride| after the previous michael@0: // one (this allows you to have rows with some padding at the end). michael@0: // michael@0: // The result will be put into the given output buffer. The destination image michael@0: // size will be xfilter.num_values() * yfilter.num_values() pixels. It will be michael@0: // in rows of exactly xfilter.num_values() * 4 bytes. michael@0: // michael@0: // |source_has_alpha| is a hint that allows us to avoid doing computations on michael@0: // the alpha channel if the image is opaque. If you don't know, set this to michael@0: // true and it will work properly, but setting this to false will be a few michael@0: // percent faster if you know the image is opaque. michael@0: // michael@0: // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order michael@0: // (this is ARGB when loaded into 32-bit words on a little-endian machine). michael@0: void BGRAConvolve2D(const unsigned char* source_data, michael@0: int source_byte_row_stride, michael@0: bool source_has_alpha, michael@0: const ConvolutionFilter1D& xfilter, michael@0: const ConvolutionFilter1D& yfilter, michael@0: int output_byte_row_stride, michael@0: unsigned char* output, michael@0: bool use_sse2); michael@0: } // namespace skia michael@0: michael@0: #endif // SKIA_EXT_CONVOLVER_H_