convolver.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2006-2012 The Chromium Authors. All rights reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions
  5. // are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in
  10. // the documentation and/or other materials provided with the
  11. // distribution.
  12. // * Neither the name of Google, Inc. nor the names of its contributors
  13. // may be used to endorse or promote products derived from this
  14. // software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  24. // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  26. // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. // SUCH DAMAGE.
  28. #ifndef SKIA_EXT_CONVOLVER_H_
  29. #define SKIA_EXT_CONVOLVER_H_
  30. #include <cmath>
  31. #include <vector>
  32. #include "base/basictypes.h"
  33. #include "mozilla/Assertions.h"
  34. #include "skia/include/core/SkTypes.h"
  35. // avoid confusion with Mac OS X's math library (Carbon)
  36. #if defined(__APPLE__)
  37. #undef FloatToFixed
  38. #undef FixedToFloat
  39. #endif
  40. namespace skia {
  41. // Represents a filter in one dimension. Each output pixel has one entry in this
  42. // object for the filter values contributing to it. You build up the filter
  43. // list by calling AddFilter for each output pixel (in order).
  44. //
  45. // We do 2-dimensional convolution by first convolving each row by one
  46. // ConvolutionFilter1D, then convolving each column by another one.
  47. //
  48. // Entries are stored in fixed point, shifted left by kShiftBits.
  49. class ConvolutionFilter1D {
  50. public:
  51. typedef short Fixed;
  52. // The number of bits that fixed point values are shifted by.
  53. enum { kShiftBits = 14 };
  54. ConvolutionFilter1D();
  55. ~ConvolutionFilter1D();
  56. // Convert between floating point and our fixed point representation.
  57. static Fixed FloatToFixed(float f) {
  58. return static_cast<Fixed>(f * (1 << kShiftBits));
  59. }
  60. static unsigned char FixedToChar(Fixed x) {
  61. return static_cast<unsigned char>(x >> kShiftBits);
  62. }
  63. static float FixedToFloat(Fixed x) {
  64. // The cast relies on Fixed being a short, implying that on
  65. // the platforms we care about all (16) bits will fit into
  66. // the mantissa of a (32-bit) float.
  67. static_assert(sizeof(Fixed) == 2,
  68. "fixed type should fit in float mantissa");
  69. float raw = static_cast<float>(x);
  70. return ldexpf(raw, -kShiftBits);
  71. }
  72. // Returns the maximum pixel span of a filter.
  73. int max_filter() const { return max_filter_; }
  74. // Returns the number of filters in this filter. This is the dimension of the
  75. // output image.
  76. int num_values() const { return static_cast<int>(filters_.size()); }
  77. // Appends the given list of scaling values for generating a given output
  78. // pixel. |filter_offset| is the distance from the edge of the image to where
  79. // the scaling factors start. The scaling factors apply to the source pixels
  80. // starting from this position, and going for the next |filter_length| pixels.
  81. //
  82. // You will probably want to make sure your input is normalized (that is,
  83. // all entries in |filter_values| sub to one) to prevent affecting the overall
  84. // brighness of the image.
  85. //
  86. // The filter_length must be > 0.
  87. //
  88. // This version will automatically convert your input to fixed point.
  89. void AddFilter(int filter_offset,
  90. const float* filter_values,
  91. int filter_length);
  92. // Same as the above version, but the input is already fixed point.
  93. void AddFilter(int filter_offset,
  94. const Fixed* filter_values,
  95. int filter_length);
  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.length;
  107. if (filter.length == 0) {
  108. return NULL;
  109. }
  110. return &filter_values_[filter.data_location];
  111. }
  112. inline void PaddingForSIMD(int padding_count) {
  113. // Padding |padding_count| of more dummy coefficients after the coefficients
  114. // of last filter to prevent SIMD instructions which load 8 or 16 bytes
  115. // together to access invalid memory areas. We are not trying to align the
  116. // coefficients right now due to the opaqueness of <vector> implementation.
  117. // This has to be done after all |AddFilter| calls.
  118. for (int i = 0; i < padding_count; ++i)
  119. filter_values_.push_back(static_cast<Fixed>(0));
  120. }
  121. private:
  122. struct FilterInstance {
  123. // Offset within filter_values for this instance of the filter.
  124. int data_location;
  125. // Distance from the left of the filter to the center. IN PIXELS
  126. int offset;
  127. // Number of values in this filter instance.
  128. int length;
  129. };
  130. // Stores the information for each filter added to this class.
  131. std::vector<FilterInstance> filters_;
  132. // We store all the filter values in this flat list, indexed by
  133. // |FilterInstance.data_location| to avoid the mallocs required for storing
  134. // each one separately.
  135. std::vector<Fixed> filter_values_;
  136. // The maximum size of any filter we've added.
  137. int max_filter_;
  138. };
  139. // Does a two-dimensional convolution on the given source image.
  140. //
  141. // It is assumed the source pixel offsets referenced in the input filters
  142. // reference only valid pixels, so the source image size is not required. Each
  143. // row of the source image starts |source_byte_row_stride| after the previous
  144. // one (this allows you to have rows with some padding at the end).
  145. //
  146. // The result will be put into the given output buffer. The destination image
  147. // size will be xfilter.num_values() * yfilter.num_values() pixels. It will be
  148. // in rows of exactly xfilter.num_values() * 4 bytes.
  149. //
  150. // |source_has_alpha| is a hint that allows us to avoid doing computations on
  151. // the alpha channel if the image is opaque. If you don't know, set this to
  152. // true and it will work properly, but setting this to false will be a few
  153. // percent faster if you know the image is opaque.
  154. //
  155. // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order
  156. // (this is ARGB when loaded into 32-bit words on a little-endian machine).
  157. void BGRAConvolve2D(const unsigned char* source_data,
  158. int source_byte_row_stride,
  159. bool source_has_alpha,
  160. const ConvolutionFilter1D& xfilter,
  161. const ConvolutionFilter1D& yfilter,
  162. int output_byte_row_stride,
  163. unsigned char* output);
  164. void ConvolveHorizontally(const unsigned char* src_data,
  165. const ConvolutionFilter1D& filter,
  166. unsigned char* out_row,
  167. bool has_alpha, bool use_sse2);
  168. void ConvolveVertically(const ConvolutionFilter1D::Fixed* filter_values,
  169. int filter_length,
  170. unsigned char* const* source_data_rows,
  171. int pixel_width, unsigned char* out_row,
  172. bool has_alpha, bool use_sse2);
  173. } // namespace skia
  174. #endif // SKIA_EXT_CONVOLVER_H_