| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SKIA_EXT_CONVOLVER_H_ | 5 #ifndef SKIA_EXT_CONVOLVER_H_ |
| 6 #define SKIA_EXT_CONVOLVER_H_ | 6 #define SKIA_EXT_CONVOLVER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cmath> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 // avoid confusion with Mac OS X's math library (Carbon) | 12 // avoid confusion with Mac OS X's math library (Carbon) |
| 12 #if defined(__APPLE__) | 13 #if defined(__APPLE__) |
| 13 #undef FloatToFixed | 14 #undef FloatToFixed |
| 15 #undef FixedToFloat |
| 14 #endif | 16 #endif |
| 15 | 17 |
| 16 namespace skia { | 18 namespace skia { |
| 17 | 19 |
| 18 // Represents a filter in one dimension. Each output pixel has one entry in this | 20 // Represents a filter in one dimension. Each output pixel has one entry in this |
| 19 // object for the filter values contributing to it. You build up the filter | 21 // object for the filter values contributing to it. You build up the filter |
| 20 // list by calling AddFilter for each output pixel (in order). | 22 // list by calling AddFilter for each output pixel (in order). |
| 21 // | 23 // |
| 22 // We do 2-dimensional convolution by first convolving each row by one | 24 // We do 2-dimensional convolution by first convolving each row by one |
| 23 // ConvolutionFilter1D, then convolving each column by another one. | 25 // ConvolutionFilter1D, then convolving each column by another one. |
| 24 // | 26 // |
| 25 // Entries are stored in fixed point, shifted left by kShiftBits. | 27 // Entries are stored in fixed point, shifted left by kShiftBits. |
| 26 class ConvolutionFilter1D { | 28 class ConvolutionFilter1D { |
| 27 public: | 29 public: |
| 28 // The number of bits that fixed point values are shifted by. | 30 // The number of bits that fixed point values are shifted by. |
| 29 enum { kShiftBits = 14 }; | 31 enum { kShiftBits = 14 }; |
| 30 | 32 |
| 31 typedef short Fixed; | 33 typedef short Fixed; |
| 32 | 34 |
| 33 ConvolutionFilter1D(); | 35 ConvolutionFilter1D(); |
| 34 ~ConvolutionFilter1D(); | 36 ~ConvolutionFilter1D(); |
| 35 | 37 |
| 36 // Convert between floating point and our fixed point representation. | 38 // Convert between floating point and our fixed point representation. |
| 37 static Fixed FloatToFixed(float f) { | 39 static Fixed FloatToFixed(float f) { |
| 38 return static_cast<Fixed>(f * (1 << kShiftBits)); | 40 return static_cast<Fixed>(f * (1 << kShiftBits)); |
| 39 } | 41 } |
| 40 static unsigned char FixedToChar(Fixed x) { | 42 static unsigned char FixedToChar(Fixed x) { |
| 41 return static_cast<unsigned char>(x >> kShiftBits); | 43 return static_cast<unsigned char>(x >> kShiftBits); |
| 42 } | 44 } |
| 45 static float FixedToFloat(Fixed x) { |
| 46 // The cast relies on Fixed being a short, implying that on |
| 47 // the platforms we care about all (16) bits will fit into |
| 48 // the mantissa of a (32-bit) float. |
| 49 float raw = static_cast<float>(x); |
| 50 return ldexpf(raw, -kShiftBits); |
| 51 } |
| 43 | 52 |
| 44 // Returns the maximum pixel span of a filter. | 53 // Returns the maximum pixel span of a filter. |
| 45 int max_filter() const { return max_filter_; } | 54 int max_filter() const { return max_filter_; } |
| 46 | 55 |
| 47 // Returns the number of filters in this filter. This is the dimension of the | 56 // Returns the number of filters in this filter. This is the dimension of the |
| 48 // output image. | 57 // output image. |
| 49 int num_values() const { return static_cast<int>(filters_.size()); } | 58 int num_values() const { return static_cast<int>(filters_.size()); } |
| 50 | 59 |
| 51 // Appends the given list of scaling values for generating a given output | 60 // Appends the given list of scaling values for generating a given output |
| 52 // pixel. |filter_offset| is the distance from the edge of the image to where | 61 // pixel. |filter_offset| is the distance from the edge of the image to where |
| (...skipping 20 matching lines...) Expand all Loading... |
| 73 // image in the direction we're convolving. The offset and length of the | 82 // image in the direction we're convolving. The offset and length of the |
| 74 // filter values are put into the corresponding out arguments (see AddFilter | 83 // filter values are put into the corresponding out arguments (see AddFilter |
| 75 // above for what these mean), and a pointer to the first scaling factor is | 84 // above for what these mean), and a pointer to the first scaling factor is |
| 76 // returned. There will be |filter_length| values in this array. | 85 // returned. There will be |filter_length| values in this array. |
| 77 inline const Fixed* FilterForValue(int value_offset, | 86 inline const Fixed* FilterForValue(int value_offset, |
| 78 int* filter_offset, | 87 int* filter_offset, |
| 79 int* filter_length) const { | 88 int* filter_length) const { |
| 80 const FilterInstance& filter = filters_[value_offset]; | 89 const FilterInstance& filter = filters_[value_offset]; |
| 81 *filter_offset = filter.offset; | 90 *filter_offset = filter.offset; |
| 82 *filter_length = filter.length; | 91 *filter_length = filter.length; |
| 83 return &filter_values_[filter.data_location]; | 92 if (filter.length == 0) { |
| 93 return NULL; |
| 94 } else { |
| 95 return &filter_values_[filter.data_location]; |
| 96 } |
| 84 } | 97 } |
| 85 | 98 |
| 86 private: | 99 private: |
| 87 struct FilterInstance { | 100 struct FilterInstance { |
| 88 // Offset within filter_values for this instance of the filter. | 101 // Offset within filter_values for this instance of the filter. |
| 89 int data_location; | 102 int data_location; |
| 90 | 103 |
| 91 // Distance from the left of the filter to the center. IN PIXELS | 104 // Distance from the left of the filter to the center. IN PIXELS |
| 92 int offset; | 105 int offset; |
| 93 | 106 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 123 // true and it will work properly, but setting this to false will be a few | 136 // true and it will work properly, but setting this to false will be a few |
| 124 // percent faster if you know the image is opaque. | 137 // percent faster if you know the image is opaque. |
| 125 // | 138 // |
| 126 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order | 139 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order |
| 127 // (this is ARGB when loaded into 32-bit words on a little-endian machine). | 140 // (this is ARGB when loaded into 32-bit words on a little-endian machine). |
| 128 void BGRAConvolve2D(const unsigned char* source_data, | 141 void BGRAConvolve2D(const unsigned char* source_data, |
| 129 int source_byte_row_stride, | 142 int source_byte_row_stride, |
| 130 bool source_has_alpha, | 143 bool source_has_alpha, |
| 131 const ConvolutionFilter1D& xfilter, | 144 const ConvolutionFilter1D& xfilter, |
| 132 const ConvolutionFilter1D& yfilter, | 145 const ConvolutionFilter1D& yfilter, |
| 146 int output_byte_row_stride, |
| 133 unsigned char* output); | 147 unsigned char* output); |
| 134 | 148 |
| 135 } // namespace skia | 149 } // namespace skia |
| 136 | 150 |
| 137 #endif // SKIA_EXT_CONVOLVER_H_ | 151 #endif // SKIA_EXT_CONVOLVER_H_ |
| 138 | |
| OLD | NEW |