OLD | NEW |
---|---|
(Empty) | |
1 #include "SkBitmapScaler.h" | |
2 #include "SkBitmapFilter.h" | |
3 #include "SkRect.h" | |
4 #include "SkTArray.h" | |
5 #include "SkErrorInternals.h" | |
6 #include "SkConvolver.h" | |
7 | |
8 // SkResizeFilter -------------------------------------------------------------- -- | |
9 | |
10 // Encapsulates computation and storage of the filters required for one complete | |
11 // resize operation. | |
12 class SkResizeFilter { | |
13 public: | |
14 SkResizeFilter(SkBitmapScaler::ResizeMethod method, | |
15 int srcFullWidth, int srcFullHeight, | |
16 int destWidth, int destHeight, | |
17 const SkIRect& destSubset, | |
18 SkConvolutionProcs *convolveProcs); | |
reed1
2013/07/19 17:47:23
double nit:
1. if you're passing 1 value, Skia li
| |
19 ~SkResizeFilter() { | |
20 SkDELETE( fBitmapFilter ); | |
21 } | |
22 | |
23 // Returns the filled filter values. | |
24 const SkConvolutionFilter1D& xFilter() { return fXFilter; } | |
25 const SkConvolutionFilter1D& yFilter() { return fYFilter; } | |
26 | |
27 private: | |
28 | |
29 SkBitmapFilter* fBitmapFilter; | |
30 | |
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 | |
33 // right/bottom so that the same code can be re-used in each dimension. | |
34 // | |
35 // |srcDependLo| and |srcDependSize| gives the range for the source | |
36 // depend rectangle (horizontally or vertically at the caller's discretion | |
37 // -- see above for what this means). | |
38 // | |
39 // Likewise, the range of destination values to compute and the scale factor | |
40 // for the transform is also specified. | |
41 | |
42 void computeFilters(int srcSize, | |
43 int destSubsetLo, int destSubsetSize, | |
44 float scale, | |
45 SkConvolutionFilter1D* output, | |
46 SkConvolutionProcs *convolveProcs); | |
47 | |
48 // Subset of scaled destination bitmap to compute. | |
49 SkIRect fOutBounds; | |
50 | |
51 SkConvolutionFilter1D fXFilter; | |
52 SkConvolutionFilter1D fYFilter; | |
53 }; | |
54 | |
55 SkResizeFilter::SkResizeFilter(SkBitmapScaler::ResizeMethod method, | |
56 int srcFullWidth, int srcFullHeight, | |
57 int destWidth, int destHeight, | |
58 const SkIRect& destSubset, | |
59 SkConvolutionProcs *convolveProcs) | |
60 : fOutBounds(destSubset) { | |
61 | |
62 // method will only ever refer to an "algorithm method". | |
63 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && | |
64 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); | |
65 | |
66 switch(method) { | |
67 case SkBitmapScaler::RESIZE_BOX: | |
68 fBitmapFilter = SkNEW(SkBoxFilter); | |
69 break; | |
70 case SkBitmapScaler::RESIZE_TRIANGLE: | |
71 fBitmapFilter = SkNEW(SkTriangleFilter); | |
72 break; | |
73 case SkBitmapScaler::RESIZE_MITCHELL: | |
74 fBitmapFilter = SkNEW_ARGS(SkMitchellFilter, (1.f/3.f, 1.f/3.f)); | |
75 break; | |
76 case SkBitmapScaler::RESIZE_HAMMING: | |
77 fBitmapFilter = SkNEW(SkHammingFilter); | |
78 break; | |
79 case SkBitmapScaler::RESIZE_LANCZOS3: | |
80 fBitmapFilter = SkNEW(SkLanczosFilter); | |
81 break; | |
82 default: | |
83 // NOTREACHED: | |
84 fBitmapFilter = SkNEW_ARGS(SkMitchellFilter, (1.f/3.f, 1.f/3.f)); | |
85 break; | |
86 } | |
87 | |
88 | |
89 float scaleX = static_cast<float>(destWidth) / | |
90 static_cast<float>(srcFullWidth); | |
91 float scaleY = static_cast<float>(destHeight) / | |
92 static_cast<float>(srcFullHeight); | |
93 | |
94 this->computeFilters(srcFullWidth, destSubset.fLeft, destSubset.width(), | |
95 scaleX, &fXFilter, convolveProcs); | |
96 this->computeFilters(srcFullHeight, destSubset.fTop, destSubset.height(), | |
97 scaleY, &fYFilter, convolveProcs); | |
98 } | |
99 | |
100 // TODO(egouriou): Take advantage of periods in the convolution. | |
101 // Practical resizing filters are periodic outside of the border area. | |
102 // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the | |
103 // source become p pixels in the destination) will have a period of p. | |
104 // A nice consequence is a period of 1 when downscaling by an integral | |
105 // factor. Downscaling from typical display resolutions is also bound | |
106 // to produce interesting periods as those are chosen to have multiple | |
107 // small factors. | |
108 // Small periods reduce computational load and improve cache usage if | |
109 // the coefficients can be shared. For periods of 1 we can consider | |
110 // loading the factors only once outside the borders. | |
111 void SkResizeFilter::computeFilters(int srcSize, | |
112 int destSubsetLo, int destSubsetSize, | |
113 float scale, | |
114 SkConvolutionFilter1D* output, | |
115 SkConvolutionProcs *convolveProcs) { | |
116 int destSubsetHi = destSubsetLo + destSubsetSize; // [lo, hi) | |
117 | |
118 // When we're doing a magnification, the scale will be larger than one. This | |
119 // means the destination pixels are much smaller than the source pixels, and | |
120 // that the range covered by the filter won't necessarily cover any source | |
121 // pixel boundaries. Therefore, we use these clamped values (max of 1) for | |
122 // some computations. | |
123 float clampedScale = SkTMin(1.0f, scale); | |
124 | |
125 // This is how many source pixels from the center we need to count | |
126 // to support the filtering function. | |
127 float srcSupport = fBitmapFilter->width() / clampedScale; | |
128 | |
129 // Speed up the divisions below by turning them into multiplies. | |
130 float invScale = 1.0f / scale; | |
131 | |
132 SkTArray<float> filterValues(64); | |
133 SkTArray<short> fixedFilterValues(64); | |
134 | |
135 // Loop over all pixels in the output range. We will generate one set of | |
136 // filter values for each one. Those values will tell us how to blend the | |
137 // source pixels to compute the destination pixel. | |
138 for (int destSubsetI = destSubsetLo; destSubsetI < destSubsetHi; | |
139 destSubsetI++) { | |
140 // Reset the arrays. We don't declare them inside so they can re-use the | |
141 // same malloc-ed buffer. | |
142 filterValues.reset(); | |
143 fixedFilterValues.reset(); | |
144 | |
145 // This is the pixel in the source directly under the pixel in the dest. | |
146 // Note that we base computations on the "center" of the pixels. To see | |
147 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x | |
148 // downscale should "cover" the pixels around the pixel with *its center* | |
149 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). | |
150 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). | |
151 float srcPixel = (static_cast<float>(destSubsetI) + 0.5f) * invScale; | |
152 | |
153 // Compute the (inclusive) range of source pixels the filter covers. | |
154 int srcBegin = SkTMax(0, SkScalarFloorToInt(srcPixel - srcSupport)); | |
155 int srcEnd = SkTMin(srcSize - 1, SkScalarCeilToInt(srcPixel + srcSupport)); | |
156 | |
157 // Compute the unnormalized filter value at each location of the source | |
158 // it covers. | |
159 float filterSum = 0.0f; // Sub of the filter values for normalizing. | |
160 for (int curFilterPixel = srcBegin; curFilterPixel <= srcEnd; | |
161 curFilterPixel++) { | |
162 // Distance from the center of the filter, this is the filter coordinate | |
163 // in source space. We also need to consider the center of the pixel | |
164 // when comparing distance against 'srcPixel'. In the 5x downscale | |
165 // example used above the distance from the center of the filter to | |
166 // the pixel with coordinates (2, 2) should be 0, because its center | |
167 // is at (2.5, 2.5). | |
168 float srcFilterDist = | |
169 ((static_cast<float>(curFilterPixel) + 0.5f) - srcPixel); | |
170 | |
171 // Since the filter really exists in dest space, map it there. | |
172 float destFilterDist = srcFilterDist * clampedScale; | |
173 | |
174 // Compute the filter value at that location. | |
175 float filterValue = fBitmapFilter->evaluate(destFilterDist); | |
176 filterValues.push_back(filterValue); | |
177 | |
178 filterSum += filterValue; | |
179 } | |
180 SkASSERT(!filterValues.empty()); | |
181 | |
182 // The filter must be normalized so that we don't affect the brightness of | |
183 // the image. Convert to normalized fixed point. | |
184 short fixedSum = 0; | |
185 for (int i = 0; i < filterValues.count(); i++) { | |
186 short curFixed = output->FloatToFixed(filterValues[i] / filterSum); | |
187 fixedSum += curFixed; | |
188 fixedFilterValues.push_back(curFixed); | |
189 } | |
190 | |
191 // The conversion to fixed point will leave some rounding errors, which | |
192 // we add back in to avoid affecting the brightness of the image. We | |
193 // arbitrarily add this to the center of the filter array (this won't always | |
194 // be the center of the filter function since it could get clipped on the | |
195 // edges, but it doesn't matter enough to worry about that case). | |
196 short leftovers = output->FloatToFixed(1.0f) - fixedSum; | |
197 fixedFilterValues[fixedFilterValues.count() / 2] += leftovers; | |
198 | |
199 // Now it's ready to go. | |
200 output->AddFilter(srcBegin, &fixedFilterValues[0], | |
201 static_cast<int>(fixedFilterValues.count())); | |
202 } | |
203 | |
204 if (convolveProcs->fApplySIMDPadding) { | |
205 convolveProcs->fApplySIMDPadding( output ); | |
206 } | |
207 } | |
208 | |
209 static SkBitmapScaler::ResizeMethod ResizeMethodToAlgorithmMethod( | |
210 SkBitmapScaler::ResizeMethod method) { | |
211 // Convert any "Quality Method" into an "Algorithm Method" | |
212 if (method >= SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD && | |
213 method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD) { | |
214 return method; | |
215 } | |
216 // The call to SkBitmapScalerGtv::Resize() above took care of | |
217 // GPU-acceleration in the cases where it is possible. So now we just | |
218 // pick the appropriate software method for each resize quality. | |
219 switch (method) { | |
220 // Users of RESIZE_GOOD are willing to trade a lot of quality to | |
221 // get speed, allowing the use of linear resampling to get hardware | |
222 // acceleration (SRB). Hence any of our "good" software filters | |
223 // will be acceptable, so we use a triangle. | |
224 case SkBitmapScaler::RESIZE_GOOD: | |
225 return SkBitmapScaler::RESIZE_TRIANGLE; | |
226 // Users of RESIZE_BETTER are willing to trade some quality in order | |
227 // to improve performance, but are guaranteed not to devolve to a linear | |
228 // resampling. In visual tests we see that Hamming-1 is not as good as | |
229 // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is | |
230 // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed | |
231 // an acceptable trade-off between quality and speed. | |
232 case SkBitmapScaler::RESIZE_BETTER: | |
233 return SkBitmapScaler::RESIZE_HAMMING; | |
234 default: | |
235 return SkBitmapScaler::RESIZE_MITCHELL; | |
236 } | |
237 } | |
238 | |
239 // static | |
240 SkBitmap SkBitmapScaler::Resize(const SkBitmap& source, | |
241 ResizeMethod method, | |
242 int destWidth, int destHeight, | |
243 const SkIRect& destSubset, | |
244 SkConvolutionProcs *convolveProcs, | |
245 SkBitmap::Allocator* allocator) { | |
246 // Ensure that the ResizeMethod enumeration is sound. | |
247 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && | |
248 (method <= RESIZE_LAST_QUALITY_METHOD)) || | |
249 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && | |
250 (method <= RESIZE_LAST_ALGORITHM_METHOD))); | |
251 | |
252 SkIRect dest = { 0, 0, destWidth, destHeight }; | |
253 if (!dest.contains(destSubset)) { | |
254 SkErrorInternals::SetError( kInvalidArgument_SkError, | |
255 "Sorry, you passed me a bitmap resize " | |
256 " method I have never heard of: %d", | |
257 method ); | |
258 } | |
259 | |
260 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just | |
261 // return empty. | |
262 if (source.width() < 1 || source.height() < 1 || | |
263 destWidth < 1 || destHeight < 1) { | |
264 return SkBitmap(); | |
265 } | |
266 | |
267 method = ResizeMethodToAlgorithmMethod(method); | |
268 | |
269 // Check that we deal with an "algorithm methods" from this point onward. | |
270 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && | |
271 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); | |
272 | |
273 SkAutoLockPixels locker(source); | |
274 if (!source.readyToDraw() || source.config() != SkBitmap::kARGB_8888_Config) | |
275 return SkBitmap(); | |
276 | |
277 SkResizeFilter filter(method, source.width(), source.height(), | |
278 destWidth, destHeight, destSubset, convolveProcs); | |
279 | |
280 // Get a source bitmap encompassing this touched area. We construct the | |
281 // offsets and row strides such that it looks like a new bitmap, while | |
282 // referring to the old data. | |
283 const unsigned char* sourceSubset = | |
284 reinterpret_cast<const unsigned char*>(source.getPixels()); | |
285 | |
286 // Convolve into the result. | |
287 SkBitmap result; | |
288 result.setConfig(SkBitmap::kARGB_8888_Config, | |
289 destSubset.width(), destSubset.height()); | |
290 result.allocPixels(allocator, NULL); | |
291 if (!result.readyToDraw()) | |
292 return SkBitmap(); | |
293 | |
294 BGRAConvolve2D(sourceSubset, static_cast<int>(source.rowBytes()), | |
295 !source.isOpaque(), filter.xFilter(), filter.yFilter(), | |
296 static_cast<int>(result.rowBytes()), | |
297 static_cast<unsigned char*>(result.getPixels()), | |
298 convolveProcs, true); | |
299 | |
300 // Preserve the "opaque" flag for use as an optimization later. | |
301 result.setIsOpaque(source.isOpaque()); | |
302 | |
303 return result; | |
304 } | |
305 | |
306 // static | |
307 SkBitmap SkBitmapScaler::Resize(const SkBitmap& source, | |
308 ResizeMethod method, | |
309 int destWidth, int destHeight, | |
310 SkConvolutionProcs* convolveProcs, | |
311 SkBitmap::Allocator* allocator) { | |
312 SkIRect destSubset = { 0, 0, destWidth, destHeight }; | |
313 return Resize(source, method, destWidth, destHeight, destSubset, | |
314 convolveProcs, allocator); | |
315 } | |
OLD | NEW |