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