|
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 |
|
29 #include "base/basictypes.h" |
|
30 |
|
31 #define _USE_MATH_DEFINES |
|
32 #include <algorithm> |
|
33 #include <cmath> |
|
34 #include <limits> |
|
35 |
|
36 #include "image_operations.h" |
|
37 |
|
38 #include "base/stack_container.h" |
|
39 #include "convolver.h" |
|
40 #include "skia/SkColorPriv.h" |
|
41 #include "skia/SkBitmap.h" |
|
42 #include "skia/SkRect.h" |
|
43 #include "skia/SkFontHost.h" |
|
44 |
|
45 namespace skia { |
|
46 |
|
47 namespace { |
|
48 |
|
49 // Returns the ceiling/floor as an integer. |
|
50 inline int CeilInt(float val) { |
|
51 return static_cast<int>(ceil(val)); |
|
52 } |
|
53 inline int FloorInt(float val) { |
|
54 return static_cast<int>(floor(val)); |
|
55 } |
|
56 |
|
57 // Filter function computation ------------------------------------------------- |
|
58 |
|
59 // Evaluates the box filter, which goes from -0.5 to +0.5. |
|
60 float EvalBox(float x) { |
|
61 return (x >= -0.5f && x < 0.5f) ? 1.0f : 0.0f; |
|
62 } |
|
63 |
|
64 // Evaluates the Lanczos filter of the given filter size window for the given |
|
65 // position. |
|
66 // |
|
67 // |filter_size| is the width of the filter (the "window"), outside of which |
|
68 // the value of the function is 0. Inside of the window, the value is the |
|
69 // normalized sinc function: |
|
70 // lanczos(x) = sinc(x) * sinc(x / filter_size); |
|
71 // where |
|
72 // sinc(x) = sin(pi*x) / (pi*x); |
|
73 float EvalLanczos(int filter_size, float x) { |
|
74 if (x <= -filter_size || x >= filter_size) |
|
75 return 0.0f; // Outside of the window. |
|
76 if (x > -std::numeric_limits<float>::epsilon() && |
|
77 x < std::numeric_limits<float>::epsilon()) |
|
78 return 1.0f; // Special case the discontinuity at the origin. |
|
79 float xpi = x * static_cast<float>(M_PI); |
|
80 return (sin(xpi) / xpi) * // sinc(x) |
|
81 sin(xpi / filter_size) / (xpi / filter_size); // sinc(x/filter_size) |
|
82 } |
|
83 |
|
84 // Evaluates the Hamming filter of the given filter size window for the given |
|
85 // position. |
|
86 // |
|
87 // The filter covers [-filter_size, +filter_size]. Outside of this window |
|
88 // the value of the function is 0. Inside of the window, the value is sinus |
|
89 // cardinal multiplied by a recentered Hamming function. The traditional |
|
90 // Hamming formula for a window of size N and n ranging in [0, N-1] is: |
|
91 // hamming(n) = 0.54 - 0.46 * cos(2 * pi * n / (N-1))) |
|
92 // In our case we want the function centered for x == 0 and at its minimum |
|
93 // on both ends of the window (x == +/- filter_size), hence the adjusted |
|
94 // formula: |
|
95 // hamming(x) = (0.54 - |
|
96 // 0.46 * cos(2 * pi * (x - filter_size)/ (2 * filter_size))) |
|
97 // = 0.54 - 0.46 * cos(pi * x / filter_size - pi) |
|
98 // = 0.54 + 0.46 * cos(pi * x / filter_size) |
|
99 float EvalHamming(int filter_size, float x) { |
|
100 if (x <= -filter_size || x >= filter_size) |
|
101 return 0.0f; // Outside of the window. |
|
102 if (x > -std::numeric_limits<float>::epsilon() && |
|
103 x < std::numeric_limits<float>::epsilon()) |
|
104 return 1.0f; // Special case the sinc discontinuity at the origin. |
|
105 const float xpi = x * static_cast<float>(M_PI); |
|
106 |
|
107 return ((sin(xpi) / xpi) * // sinc(x) |
|
108 (0.54f + 0.46f * cos(xpi / filter_size))); // hamming(x) |
|
109 } |
|
110 |
|
111 // ResizeFilter ---------------------------------------------------------------- |
|
112 |
|
113 // Encapsulates computation and storage of the filters required for one complete |
|
114 // resize operation. |
|
115 class ResizeFilter { |
|
116 public: |
|
117 ResizeFilter(ImageOperations::ResizeMethod method, |
|
118 int src_full_width, int src_full_height, |
|
119 int dest_width, int dest_height, |
|
120 const SkIRect& dest_subset); |
|
121 |
|
122 // Returns the filled filter values. |
|
123 const ConvolutionFilter1D& x_filter() { return x_filter_; } |
|
124 const ConvolutionFilter1D& y_filter() { return y_filter_; } |
|
125 |
|
126 private: |
|
127 // Returns the number of pixels that the filer spans, in filter space (the |
|
128 // destination image). |
|
129 float GetFilterSupport(float scale) { |
|
130 switch (method_) { |
|
131 case ImageOperations::RESIZE_BOX: |
|
132 // The box filter just scales with the image scaling. |
|
133 return 0.5f; // Only want one side of the filter = /2. |
|
134 case ImageOperations::RESIZE_HAMMING1: |
|
135 // The Hamming filter takes as much space in the source image in |
|
136 // each direction as the size of the window = 1 for Hamming1. |
|
137 return 1.0f; |
|
138 case ImageOperations::RESIZE_LANCZOS2: |
|
139 // The Lanczos filter takes as much space in the source image in |
|
140 // each direction as the size of the window = 2 for Lanczos2. |
|
141 return 2.0f; |
|
142 case ImageOperations::RESIZE_LANCZOS3: |
|
143 // The Lanczos filter takes as much space in the source image in |
|
144 // each direction as the size of the window = 3 for Lanczos3. |
|
145 return 3.0f; |
|
146 default: |
|
147 return 1.0f; |
|
148 } |
|
149 } |
|
150 |
|
151 // Computes one set of filters either horizontally or vertically. The caller |
|
152 // will specify the "min" and "max" rather than the bottom/top and |
|
153 // right/bottom so that the same code can be re-used in each dimension. |
|
154 // |
|
155 // |src_depend_lo| and |src_depend_size| gives the range for the source |
|
156 // depend rectangle (horizontally or vertically at the caller's discretion |
|
157 // -- see above for what this means). |
|
158 // |
|
159 // Likewise, the range of destination values to compute and the scale factor |
|
160 // for the transform is also specified. |
|
161 void ComputeFilters(int src_size, |
|
162 int dest_subset_lo, int dest_subset_size, |
|
163 float scale, float src_support, |
|
164 ConvolutionFilter1D* output); |
|
165 |
|
166 // Computes the filter value given the coordinate in filter space. |
|
167 inline float ComputeFilter(float pos) { |
|
168 switch (method_) { |
|
169 case ImageOperations::RESIZE_BOX: |
|
170 return EvalBox(pos); |
|
171 case ImageOperations::RESIZE_HAMMING1: |
|
172 return EvalHamming(1, pos); |
|
173 case ImageOperations::RESIZE_LANCZOS2: |
|
174 return EvalLanczos(2, pos); |
|
175 case ImageOperations::RESIZE_LANCZOS3: |
|
176 return EvalLanczos(3, pos); |
|
177 default: |
|
178 return 0; |
|
179 } |
|
180 } |
|
181 |
|
182 ImageOperations::ResizeMethod method_; |
|
183 |
|
184 // Size of the filter support on one side only in the destination space. |
|
185 // See GetFilterSupport. |
|
186 float x_filter_support_; |
|
187 float y_filter_support_; |
|
188 |
|
189 // Subset of scaled destination bitmap to compute. |
|
190 SkIRect out_bounds_; |
|
191 |
|
192 ConvolutionFilter1D x_filter_; |
|
193 ConvolutionFilter1D y_filter_; |
|
194 |
|
195 DISALLOW_COPY_AND_ASSIGN(ResizeFilter); |
|
196 }; |
|
197 |
|
198 ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, |
|
199 int src_full_width, int src_full_height, |
|
200 int dest_width, int dest_height, |
|
201 const SkIRect& dest_subset) |
|
202 : method_(method), |
|
203 out_bounds_(dest_subset) { |
|
204 // method_ will only ever refer to an "algorithm method". |
|
205 SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
|
206 (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); |
|
207 |
|
208 float scale_x = static_cast<float>(dest_width) / |
|
209 static_cast<float>(src_full_width); |
|
210 float scale_y = static_cast<float>(dest_height) / |
|
211 static_cast<float>(src_full_height); |
|
212 |
|
213 x_filter_support_ = GetFilterSupport(scale_x); |
|
214 y_filter_support_ = GetFilterSupport(scale_y); |
|
215 |
|
216 // Support of the filter in source space. |
|
217 float src_x_support = x_filter_support_ / scale_x; |
|
218 float src_y_support = y_filter_support_ / scale_y; |
|
219 |
|
220 ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(), |
|
221 scale_x, src_x_support, &x_filter_); |
|
222 ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(), |
|
223 scale_y, src_y_support, &y_filter_); |
|
224 } |
|
225 |
|
226 // TODO(egouriou): Take advantage of periods in the convolution. |
|
227 // Practical resizing filters are periodic outside of the border area. |
|
228 // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the |
|
229 // source become p pixels in the destination) will have a period of p. |
|
230 // A nice consequence is a period of 1 when downscaling by an integral |
|
231 // factor. Downscaling from typical display resolutions is also bound |
|
232 // to produce interesting periods as those are chosen to have multiple |
|
233 // small factors. |
|
234 // Small periods reduce computational load and improve cache usage if |
|
235 // the coefficients can be shared. For periods of 1 we can consider |
|
236 // loading the factors only once outside the borders. |
|
237 void ResizeFilter::ComputeFilters(int src_size, |
|
238 int dest_subset_lo, int dest_subset_size, |
|
239 float scale, float src_support, |
|
240 ConvolutionFilter1D* output) { |
|
241 int dest_subset_hi = dest_subset_lo + dest_subset_size; // [lo, hi) |
|
242 |
|
243 // When we're doing a magnification, the scale will be larger than one. This |
|
244 // means the destination pixels are much smaller than the source pixels, and |
|
245 // that the range covered by the filter won't necessarily cover any source |
|
246 // pixel boundaries. Therefore, we use these clamped values (max of 1) for |
|
247 // some computations. |
|
248 float clamped_scale = std::min(1.0f, scale); |
|
249 |
|
250 // Speed up the divisions below by turning them into multiplies. |
|
251 float inv_scale = 1.0f / scale; |
|
252 |
|
253 StackVector<float, 64> filter_values; |
|
254 StackVector<int16_t, 64> fixed_filter_values; |
|
255 |
|
256 // Loop over all pixels in the output range. We will generate one set of |
|
257 // filter values for each one. Those values will tell us how to blend the |
|
258 // source pixels to compute the destination pixel. |
|
259 for (int dest_subset_i = dest_subset_lo; dest_subset_i < dest_subset_hi; |
|
260 dest_subset_i++) { |
|
261 // Reset the arrays. We don't declare them inside so they can re-use the |
|
262 // same malloc-ed buffer. |
|
263 filter_values->clear(); |
|
264 fixed_filter_values->clear(); |
|
265 |
|
266 // This is the pixel in the source directly under the pixel in the dest. |
|
267 // Note that we base computations on the "center" of the pixels. To see |
|
268 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x |
|
269 // downscale should "cover" the pixels around the pixel with *its center* |
|
270 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). |
|
271 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). |
|
272 float src_pixel = (static_cast<float>(dest_subset_i) + 0.5f) * inv_scale; |
|
273 |
|
274 // Compute the (inclusive) range of source pixels the filter covers. |
|
275 int src_begin = std::max(0, FloorInt(src_pixel - src_support)); |
|
276 int src_end = std::min(src_size - 1, CeilInt(src_pixel + src_support)); |
|
277 |
|
278 // Compute the unnormalized filter value at each location of the source |
|
279 // it covers. |
|
280 float filter_sum = 0.0f; // Sub of the filter values for normalizing. |
|
281 for (int cur_filter_pixel = src_begin; cur_filter_pixel <= src_end; |
|
282 cur_filter_pixel++) { |
|
283 // Distance from the center of the filter, this is the filter coordinate |
|
284 // in source space. We also need to consider the center of the pixel |
|
285 // when comparing distance against 'src_pixel'. In the 5x downscale |
|
286 // example used above the distance from the center of the filter to |
|
287 // the pixel with coordinates (2, 2) should be 0, because its center |
|
288 // is at (2.5, 2.5). |
|
289 float src_filter_dist = |
|
290 ((static_cast<float>(cur_filter_pixel) + 0.5f) - src_pixel); |
|
291 |
|
292 // Since the filter really exists in dest space, map it there. |
|
293 float dest_filter_dist = src_filter_dist * clamped_scale; |
|
294 |
|
295 // Compute the filter value at that location. |
|
296 float filter_value = ComputeFilter(dest_filter_dist); |
|
297 filter_values->push_back(filter_value); |
|
298 |
|
299 filter_sum += filter_value; |
|
300 } |
|
301 |
|
302 // The filter must be normalized so that we don't affect the brightness of |
|
303 // the image. Convert to normalized fixed point. |
|
304 int16_t fixed_sum = 0; |
|
305 for (size_t i = 0; i < filter_values->size(); i++) { |
|
306 int16_t cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum); |
|
307 fixed_sum += cur_fixed; |
|
308 fixed_filter_values->push_back(cur_fixed); |
|
309 } |
|
310 |
|
311 // The conversion to fixed point will leave some rounding errors, which |
|
312 // we add back in to avoid affecting the brightness of the image. We |
|
313 // arbitrarily add this to the center of the filter array (this won't always |
|
314 // be the center of the filter function since it could get clipped on the |
|
315 // edges, but it doesn't matter enough to worry about that case). |
|
316 int16_t leftovers = output->FloatToFixed(1.0f) - fixed_sum; |
|
317 fixed_filter_values[fixed_filter_values->size() / 2] += leftovers; |
|
318 |
|
319 // Now it's ready to go. |
|
320 output->AddFilter(src_begin, &fixed_filter_values[0], |
|
321 static_cast<int>(fixed_filter_values->size())); |
|
322 } |
|
323 |
|
324 output->PaddingForSIMD(8); |
|
325 } |
|
326 |
|
327 ImageOperations::ResizeMethod ResizeMethodToAlgorithmMethod( |
|
328 ImageOperations::ResizeMethod method) { |
|
329 // Convert any "Quality Method" into an "Algorithm Method" |
|
330 if (method >= ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD && |
|
331 method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD) { |
|
332 return method; |
|
333 } |
|
334 // The call to ImageOperationsGtv::Resize() above took care of |
|
335 // GPU-acceleration in the cases where it is possible. So now we just |
|
336 // pick the appropriate software method for each resize quality. |
|
337 switch (method) { |
|
338 // Users of RESIZE_GOOD are willing to trade a lot of quality to |
|
339 // get speed, allowing the use of linear resampling to get hardware |
|
340 // acceleration (SRB). Hence any of our "good" software filters |
|
341 // will be acceptable, and we use the fastest one, Hamming-1. |
|
342 case ImageOperations::RESIZE_GOOD: |
|
343 // Users of RESIZE_BETTER are willing to trade some quality in order |
|
344 // to improve performance, but are guaranteed not to devolve to a linear |
|
345 // resampling. In visual tests we see that Hamming-1 is not as good as |
|
346 // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is |
|
347 // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed |
|
348 // an acceptable trade-off between quality and speed. |
|
349 case ImageOperations::RESIZE_BETTER: |
|
350 return ImageOperations::RESIZE_HAMMING1; |
|
351 default: |
|
352 return ImageOperations::RESIZE_LANCZOS3; |
|
353 } |
|
354 } |
|
355 |
|
356 } // namespace |
|
357 |
|
358 // Resize ---------------------------------------------------------------------- |
|
359 |
|
360 // static |
|
361 SkBitmap ImageOperations::Resize(const SkBitmap& source, |
|
362 ResizeMethod method, |
|
363 int dest_width, int dest_height, |
|
364 const SkIRect& dest_subset, |
|
365 void* dest_pixels /* = nullptr */) { |
|
366 if (method == ImageOperations::RESIZE_SUBPIXEL) |
|
367 return ResizeSubpixel(source, dest_width, dest_height, dest_subset); |
|
368 else |
|
369 return ResizeBasic(source, method, dest_width, dest_height, dest_subset, |
|
370 dest_pixels); |
|
371 } |
|
372 |
|
373 // static |
|
374 SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source, |
|
375 int dest_width, int dest_height, |
|
376 const SkIRect& dest_subset) { |
|
377 // Currently only works on Linux/BSD because these are the only platforms |
|
378 // where SkFontHost::GetSubpixelOrder is defined. |
|
379 #if defined(XP_UNIX) |
|
380 // Understand the display. |
|
381 const SkFontHost::LCDOrder order = SkFontHost::GetSubpixelOrder(); |
|
382 const SkFontHost::LCDOrientation orientation = |
|
383 SkFontHost::GetSubpixelOrientation(); |
|
384 |
|
385 // Decide on which dimension, if any, to deploy subpixel rendering. |
|
386 int w = 1; |
|
387 int h = 1; |
|
388 switch (orientation) { |
|
389 case SkFontHost::kHorizontal_LCDOrientation: |
|
390 w = dest_width < source.width() ? 3 : 1; |
|
391 break; |
|
392 case SkFontHost::kVertical_LCDOrientation: |
|
393 h = dest_height < source.height() ? 3 : 1; |
|
394 break; |
|
395 } |
|
396 |
|
397 // Resize the image. |
|
398 const int width = dest_width * w; |
|
399 const int height = dest_height * h; |
|
400 SkIRect subset = { dest_subset.fLeft, dest_subset.fTop, |
|
401 dest_subset.fLeft + dest_subset.width() * w, |
|
402 dest_subset.fTop + dest_subset.height() * h }; |
|
403 SkBitmap img = ResizeBasic(source, ImageOperations::RESIZE_LANCZOS3, width, |
|
404 height, subset); |
|
405 const int row_words = img.rowBytes() / 4; |
|
406 if (w == 1 && h == 1) |
|
407 return img; |
|
408 |
|
409 // Render into subpixels. |
|
410 SkBitmap result; |
|
411 result.setConfig(SkBitmap::kARGB_8888_Config, dest_subset.width(), |
|
412 dest_subset.height()); |
|
413 result.allocPixels(); |
|
414 if (!result.readyToDraw()) |
|
415 return img; |
|
416 |
|
417 SkAutoLockPixels locker(img); |
|
418 if (!img.readyToDraw()) |
|
419 return img; |
|
420 |
|
421 uint32_t* src_row = img.getAddr32(0, 0); |
|
422 uint32_t* dst_row = result.getAddr32(0, 0); |
|
423 for (int y = 0; y < dest_subset.height(); y++) { |
|
424 uint32_t* src = src_row; |
|
425 uint32_t* dst = dst_row; |
|
426 for (int x = 0; x < dest_subset.width(); x++, src += w, dst++) { |
|
427 uint8_t r = 0, g = 0, b = 0, a = 0; |
|
428 switch (order) { |
|
429 case SkFontHost::kRGB_LCDOrder: |
|
430 switch (orientation) { |
|
431 case SkFontHost::kHorizontal_LCDOrientation: |
|
432 r = SkGetPackedR32(src[0]); |
|
433 g = SkGetPackedG32(src[1]); |
|
434 b = SkGetPackedB32(src[2]); |
|
435 a = SkGetPackedA32(src[1]); |
|
436 break; |
|
437 case SkFontHost::kVertical_LCDOrientation: |
|
438 r = SkGetPackedR32(src[0 * row_words]); |
|
439 g = SkGetPackedG32(src[1 * row_words]); |
|
440 b = SkGetPackedB32(src[2 * row_words]); |
|
441 a = SkGetPackedA32(src[1 * row_words]); |
|
442 break; |
|
443 } |
|
444 break; |
|
445 case SkFontHost::kBGR_LCDOrder: |
|
446 switch (orientation) { |
|
447 case SkFontHost::kHorizontal_LCDOrientation: |
|
448 b = SkGetPackedB32(src[0]); |
|
449 g = SkGetPackedG32(src[1]); |
|
450 r = SkGetPackedR32(src[2]); |
|
451 a = SkGetPackedA32(src[1]); |
|
452 break; |
|
453 case SkFontHost::kVertical_LCDOrientation: |
|
454 b = SkGetPackedB32(src[0 * row_words]); |
|
455 g = SkGetPackedG32(src[1 * row_words]); |
|
456 r = SkGetPackedR32(src[2 * row_words]); |
|
457 a = SkGetPackedA32(src[1 * row_words]); |
|
458 break; |
|
459 } |
|
460 break; |
|
461 case SkFontHost::kNONE_LCDOrder: |
|
462 break; |
|
463 } |
|
464 // Premultiplied alpha is very fragile. |
|
465 a = a > r ? a : r; |
|
466 a = a > g ? a : g; |
|
467 a = a > b ? a : b; |
|
468 *dst = SkPackARGB32(a, r, g, b); |
|
469 } |
|
470 src_row += h * row_words; |
|
471 dst_row += result.rowBytes() / 4; |
|
472 } |
|
473 result.setAlphaType(img.alphaType()); |
|
474 return result; |
|
475 #else |
|
476 return SkBitmap(); |
|
477 #endif // OS_POSIX && !OS_MACOSX && !defined(OS_ANDROID) |
|
478 } |
|
479 |
|
480 // static |
|
481 SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, |
|
482 ResizeMethod method, |
|
483 int dest_width, int dest_height, |
|
484 const SkIRect& dest_subset, |
|
485 void* dest_pixels /* = nullptr */) { |
|
486 // Ensure that the ResizeMethod enumeration is sound. |
|
487 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && |
|
488 (method <= RESIZE_LAST_QUALITY_METHOD)) || |
|
489 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
|
490 (method <= RESIZE_LAST_ALGORITHM_METHOD))); |
|
491 |
|
492 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just |
|
493 // return empty. |
|
494 if (source.width() < 1 || source.height() < 1 || |
|
495 dest_width < 1 || dest_height < 1) |
|
496 return SkBitmap(); |
|
497 |
|
498 method = ResizeMethodToAlgorithmMethod(method); |
|
499 // Check that we deal with an "algorithm methods" from this point onward. |
|
500 SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
|
501 (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); |
|
502 |
|
503 SkAutoLockPixels locker(source); |
|
504 if (!source.readyToDraw()) |
|
505 return SkBitmap(); |
|
506 |
|
507 ResizeFilter filter(method, source.width(), source.height(), |
|
508 dest_width, dest_height, dest_subset); |
|
509 |
|
510 // Get a source bitmap encompassing this touched area. We construct the |
|
511 // offsets and row strides such that it looks like a new bitmap, while |
|
512 // referring to the old data. |
|
513 const uint8_t* source_subset = |
|
514 reinterpret_cast<const uint8_t*>(source.getPixels()); |
|
515 |
|
516 // Convolve into the result. |
|
517 SkBitmap result; |
|
518 result.setConfig(SkBitmap::kARGB_8888_Config, |
|
519 dest_subset.width(), dest_subset.height()); |
|
520 |
|
521 if (dest_pixels) { |
|
522 result.setPixels(dest_pixels); |
|
523 } else { |
|
524 result.allocPixels(); |
|
525 } |
|
526 |
|
527 if (!result.readyToDraw()) |
|
528 return SkBitmap(); |
|
529 |
|
530 BGRAConvolve2D(source_subset, static_cast<int>(source.rowBytes()), |
|
531 !source.isOpaque(), filter.x_filter(), filter.y_filter(), |
|
532 static_cast<int>(result.rowBytes()), |
|
533 static_cast<unsigned char*>(result.getPixels()), |
|
534 /* sse = */ false); |
|
535 |
|
536 // Preserve the "opaque" flag for use as an optimization later. |
|
537 result.setAlphaType(source.alphaType()); |
|
538 |
|
539 return result; |
|
540 } |
|
541 |
|
542 // static |
|
543 SkBitmap ImageOperations::Resize(const SkBitmap& source, |
|
544 ResizeMethod method, |
|
545 int dest_width, int dest_height, |
|
546 void* dest_pixels /* = nullptr */) { |
|
547 SkIRect dest_subset = { 0, 0, dest_width, dest_height }; |
|
548 return Resize(source, method, dest_width, dest_height, dest_subset, |
|
549 dest_pixels); |
|
550 } |
|
551 |
|
552 } // namespace skia |