OLD | NEW |
1 #include "SkBitmapScaler.h" | 1 #include "SkBitmapScaler.h" |
2 #include "SkBitmapFilter.h" | 2 #include "SkBitmapFilter.h" |
3 #include "SkRect.h" | 3 #include "SkRect.h" |
4 #include "SkTArray.h" | 4 #include "SkTArray.h" |
5 #include "SkErrorInternals.h" | 5 #include "SkErrorInternals.h" |
6 #include "SkConvolver.h" | 6 #include "SkConvolver.h" |
7 | 7 |
8 // SkResizeFilter --------------------------------------------------------------
-- | 8 // SkResizeFilter --------------------------------------------------------------
-- |
9 | 9 |
10 // Encapsulates computation and storage of the filters required for one complete | 10 // Encapsulates computation and storage of the filters required for one complete |
11 // resize operation. | 11 // resize operation. |
12 class SkResizeFilter { | 12 class SkResizeFilter { |
13 public: | 13 public: |
14 SkResizeFilter(SkBitmapScaler::ResizeMethod method, | 14 SkResizeFilter(SkBitmapScaler::ResizeMethod method, |
15 int srcFullWidth, int srcFullHeight, | 15 int srcFullWidth, int srcFullHeight, |
16 int destWidth, int destHeight, | 16 int destWidth, int destHeight, |
17 const SkIRect& destSubset, | 17 const SkIRect& destSubset, |
18 SkConvolutionProcs* convolveProcs); | 18 const SkConvolutionProcs& convolveProcs); |
19 ~SkResizeFilter() { | 19 ~SkResizeFilter() { |
20 SkDELETE( fBitmapFilter ); | 20 SkDELETE( fBitmapFilter ); |
21 } | 21 } |
22 | 22 |
23 // Returns the filled filter values. | 23 // Returns the filled filter values. |
24 const SkConvolutionFilter1D& xFilter() { return fXFilter; } | 24 const SkConvolutionFilter1D& xFilter() { return fXFilter; } |
25 const SkConvolutionFilter1D& yFilter() { return fYFilter; } | 25 const SkConvolutionFilter1D& yFilter() { return fYFilter; } |
26 | 26 |
27 private: | 27 private: |
28 | 28 |
29 SkBitmapFilter* fBitmapFilter; | 29 SkBitmapFilter* fBitmapFilter; |
30 | 30 |
31 // Computes one set of filters either horizontally or vertically. The caller | 31 // Computes one set of filters either horizontally or vertically. The caller |
32 // will specify the "min" and "max" rather than the bottom/top and | 32 // will specify the "min" and "max" rather than the bottom/top and |
33 // right/bottom so that the same code can be re-used in each dimension. | 33 // right/bottom so that the same code can be re-used in each dimension. |
34 // | 34 // |
35 // |srcDependLo| and |srcDependSize| gives the range for the source | 35 // |srcDependLo| and |srcDependSize| gives the range for the source |
36 // depend rectangle (horizontally or vertically at the caller's discretion | 36 // depend rectangle (horizontally or vertically at the caller's discretion |
37 // -- see above for what this means). | 37 // -- see above for what this means). |
38 // | 38 // |
39 // Likewise, the range of destination values to compute and the scale factor | 39 // Likewise, the range of destination values to compute and the scale factor |
40 // for the transform is also specified. | 40 // for the transform is also specified. |
41 | 41 |
42 void computeFilters(int srcSize, | 42 void computeFilters(int srcSize, |
43 int destSubsetLo, int destSubsetSize, | 43 int destSubsetLo, int destSubsetSize, |
44 float scale, | 44 float scale, |
45 SkConvolutionFilter1D* output, | 45 SkConvolutionFilter1D* output, |
46 SkConvolutionProcs* convolveProcs); | 46 const SkConvolutionProcs& convolveProcs); |
47 | 47 |
48 SkConvolutionFilter1D fXFilter; | 48 SkConvolutionFilter1D fXFilter; |
49 SkConvolutionFilter1D fYFilter; | 49 SkConvolutionFilter1D fYFilter; |
50 }; | 50 }; |
51 | 51 |
52 SkResizeFilter::SkResizeFilter(SkBitmapScaler::ResizeMethod method, | 52 SkResizeFilter::SkResizeFilter(SkBitmapScaler::ResizeMethod method, |
53 int srcFullWidth, int srcFullHeight, | 53 int srcFullWidth, int srcFullHeight, |
54 int destWidth, int destHeight, | 54 int destWidth, int destHeight, |
55 const SkIRect& destSubset, | 55 const SkIRect& destSubset, |
56 SkConvolutionProcs* convolveProcs) { | 56 const SkConvolutionProcs& convolveProcs) { |
57 | 57 |
58 // method will only ever refer to an "algorithm method". | 58 // method will only ever refer to an "algorithm method". |
59 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && | 59 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
60 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); | 60 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); |
61 | 61 |
62 switch(method) { | 62 switch(method) { |
63 case SkBitmapScaler::RESIZE_BOX: | 63 case SkBitmapScaler::RESIZE_BOX: |
64 fBitmapFilter = SkNEW(SkBoxFilter); | 64 fBitmapFilter = SkNEW(SkBoxFilter); |
65 break; | 65 break; |
66 case SkBitmapScaler::RESIZE_TRIANGLE: | 66 case SkBitmapScaler::RESIZE_TRIANGLE: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // factor. Downscaling from typical display resolutions is also bound | 101 // factor. Downscaling from typical display resolutions is also bound |
102 // to produce interesting periods as those are chosen to have multiple | 102 // to produce interesting periods as those are chosen to have multiple |
103 // small factors. | 103 // small factors. |
104 // Small periods reduce computational load and improve cache usage if | 104 // Small periods reduce computational load and improve cache usage if |
105 // the coefficients can be shared. For periods of 1 we can consider | 105 // the coefficients can be shared. For periods of 1 we can consider |
106 // loading the factors only once outside the borders. | 106 // loading the factors only once outside the borders. |
107 void SkResizeFilter::computeFilters(int srcSize, | 107 void SkResizeFilter::computeFilters(int srcSize, |
108 int destSubsetLo, int destSubsetSize, | 108 int destSubsetLo, int destSubsetSize, |
109 float scale, | 109 float scale, |
110 SkConvolutionFilter1D* output, | 110 SkConvolutionFilter1D* output, |
111 SkConvolutionProcs* convolveProcs) { | 111 const SkConvolutionProcs& convolveProcs) { |
112 int destSubsetHi = destSubsetLo + destSubsetSize; // [lo, hi) | 112 int destSubsetHi = destSubsetLo + destSubsetSize; // [lo, hi) |
113 | 113 |
114 // When we're doing a magnification, the scale will be larger than one. This | 114 // When we're doing a magnification, the scale will be larger than one. This |
115 // means the destination pixels are much smaller than the source pixels, and | 115 // means the destination pixels are much smaller than the source pixels, and |
116 // that the range covered by the filter won't necessarily cover any source | 116 // that the range covered by the filter won't necessarily cover any source |
117 // pixel boundaries. Therefore, we use these clamped values (max of 1) for | 117 // pixel boundaries. Therefore, we use these clamped values (max of 1) for |
118 // some computations. | 118 // some computations. |
119 float clampedScale = SkTMin(1.0f, scale); | 119 float clampedScale = SkTMin(1.0f, scale); |
120 | 120 |
121 // This is how many source pixels from the center we need to count | 121 // This is how many source pixels from the center we need to count |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // be the center of the filter function since it could get clipped on the | 190 // be the center of the filter function since it could get clipped on the |
191 // edges, but it doesn't matter enough to worry about that case). | 191 // edges, but it doesn't matter enough to worry about that case). |
192 short leftovers = output->FloatToFixed(1.0f) - fixedSum; | 192 short leftovers = output->FloatToFixed(1.0f) - fixedSum; |
193 fixedFilterValues[fixedFilterValues.count() / 2] += leftovers; | 193 fixedFilterValues[fixedFilterValues.count() / 2] += leftovers; |
194 | 194 |
195 // Now it's ready to go. | 195 // Now it's ready to go. |
196 output->AddFilter(srcBegin, &fixedFilterValues[0], | 196 output->AddFilter(srcBegin, &fixedFilterValues[0], |
197 static_cast<int>(fixedFilterValues.count())); | 197 static_cast<int>(fixedFilterValues.count())); |
198 } | 198 } |
199 | 199 |
200 if (convolveProcs->fApplySIMDPadding) { | 200 if (convolveProcs.fApplySIMDPadding) { |
201 convolveProcs->fApplySIMDPadding( output ); | 201 convolveProcs.fApplySIMDPadding( output ); |
202 } | 202 } |
203 } | 203 } |
204 | 204 |
205 static SkBitmapScaler::ResizeMethod ResizeMethodToAlgorithmMethod( | 205 static SkBitmapScaler::ResizeMethod ResizeMethodToAlgorithmMethod( |
206 SkBitmapScaler::ResizeMethod method) { | 206 SkBitmapScaler::ResizeMethod method) { |
207 // Convert any "Quality Method" into an "Algorithm Method" | 207 // Convert any "Quality Method" into an "Algorithm Method" |
208 if (method >= SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD && | 208 if (method >= SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD && |
209 method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD) { | 209 method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD) { |
210 return method; | 210 return method; |
211 } | 211 } |
(...skipping 19 matching lines...) Expand all Loading... |
231 return SkBitmapScaler::RESIZE_MITCHELL; | 231 return SkBitmapScaler::RESIZE_MITCHELL; |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 // static | 235 // static |
236 bool SkBitmapScaler::Resize(SkBitmap* resultPtr, | 236 bool SkBitmapScaler::Resize(SkBitmap* resultPtr, |
237 const SkBitmap& source, | 237 const SkBitmap& source, |
238 ResizeMethod method, | 238 ResizeMethod method, |
239 int destWidth, int destHeight, | 239 int destWidth, int destHeight, |
240 const SkIRect& destSubset, | 240 const SkIRect& destSubset, |
241 SkConvolutionProcs* convolveProcs, | 241 const SkConvolutionProcs& convolveProcs, |
242 SkBitmap::Allocator* allocator) { | 242 SkBitmap::Allocator* allocator) { |
243 // Ensure that the ResizeMethod enumeration is sound. | 243 // Ensure that the ResizeMethod enumeration is sound. |
244 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && | 244 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && |
245 (method <= RESIZE_LAST_QUALITY_METHOD)) || | 245 (method <= RESIZE_LAST_QUALITY_METHOD)) || |
246 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && | 246 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
247 (method <= RESIZE_LAST_ALGORITHM_METHOD))); | 247 (method <= RESIZE_LAST_ALGORITHM_METHOD))); |
248 | 248 |
249 SkIRect dest = { 0, 0, destWidth, destHeight }; | 249 SkIRect dest = { 0, 0, destWidth, destHeight }; |
250 if (!dest.contains(destSubset)) { | 250 if (!dest.contains(destSubset)) { |
251 SkErrorInternals::SetError( kInvalidArgument_SkError, | 251 SkErrorInternals::SetError( kInvalidArgument_SkError, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 result.setIsOpaque(source.isOpaque()); | 303 result.setIsOpaque(source.isOpaque()); |
304 *resultPtr = result; | 304 *resultPtr = result; |
305 return true; | 305 return true; |
306 } | 306 } |
307 | 307 |
308 // static | 308 // static |
309 bool SkBitmapScaler::Resize(SkBitmap* resultPtr, | 309 bool SkBitmapScaler::Resize(SkBitmap* resultPtr, |
310 const SkBitmap& source, | 310 const SkBitmap& source, |
311 ResizeMethod method, | 311 ResizeMethod method, |
312 int destWidth, int destHeight, | 312 int destWidth, int destHeight, |
313 SkConvolutionProcs* convolveProcs, | 313 const SkConvolutionProcs& convolveProcs, |
314 SkBitmap::Allocator* allocator) { | 314 SkBitmap::Allocator* allocator) { |
315 SkIRect destSubset = { 0, 0, destWidth, destHeight }; | 315 SkIRect destSubset = { 0, 0, destWidth, destHeight }; |
316 return Resize(resultPtr, source, method, destWidth, destHeight, destSubset, | 316 return Resize(resultPtr, source, method, destWidth, destHeight, destSubset, |
317 convolveProcs, allocator); | 317 convolveProcs, allocator); |
318 } | 318 } |
OLD | NEW |