Chromium Code Reviews| 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 #define _USE_MATH_DEFINES | 5 #define _USE_MATH_DEFINES |
| 6 #include <algorithm> | |
| 6 #include <cmath> | 7 #include <cmath> |
| 7 #include <limits> | 8 #include <limits> |
| 8 | 9 |
| 9 #include "skia/ext/image_operations.h" | 10 #include "skia/ext/image_operations.h" |
| 10 | 11 |
| 11 // TODO(pkasting): skia/ext should not depend on base/! | 12 // TODO(pkasting): skia/ext should not depend on base/! |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 14 #include "base/stack_container.h" | 15 #include "base/stack_container.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 if (x <= -filter_size || x >= filter_size) | 53 if (x <= -filter_size || x >= filter_size) |
| 53 return 0.0f; // Outside of the window. | 54 return 0.0f; // Outside of the window. |
| 54 if (x > -std::numeric_limits<float>::epsilon() && | 55 if (x > -std::numeric_limits<float>::epsilon() && |
| 55 x < std::numeric_limits<float>::epsilon()) | 56 x < std::numeric_limits<float>::epsilon()) |
| 56 return 1.0f; // Special case the discontinuity at the origin. | 57 return 1.0f; // Special case the discontinuity at the origin. |
| 57 float xpi = x * static_cast<float>(M_PI); | 58 float xpi = x * static_cast<float>(M_PI); |
| 58 return (sin(xpi) / xpi) * // sinc(x) | 59 return (sin(xpi) / xpi) * // sinc(x) |
| 59 sin(xpi / filter_size) / (xpi / filter_size); // sinc(x/filter_size) | 60 sin(xpi / filter_size) / (xpi / filter_size); // sinc(x/filter_size) |
| 60 } | 61 } |
| 61 | 62 |
| 63 // Evaluates the Hamming filter of the given filter size window for the given | |
| 64 // position. | |
| 65 // | |
| 66 // The filter covers [-filter_size, +filter_size]. Outside of this window | |
| 67 // the value of the function is 0. Inside of the window, the value is sinus | |
| 68 // cardinal multiplied by a recentered Hamming function. The traditional | |
| 69 // Hamming formula for a window of size N and n ranging in [0, N-1] is: | |
| 70 // hamming(n) = 0.54 - 0.46 * cos(2 * pi * n / (N-1))) | |
| 71 // In our case we want the function centered for x == 0 and at its minimum | |
| 72 // on both ends of the window (x == +/- filter_size), hence the adjusted | |
| 73 // formula: | |
| 74 // hamming(x) = (0.54 - | |
| 75 // 0.46 * cos(2 * pi * (x - filter_size)/ (2 * filter_size))) | |
| 76 // = 0.54 - 0.46 * cos(pi * x / filter_size - pi) | |
| 77 // = 0.54 + 0.46 * cos(pi * x / filter_size) | |
| 78 float EvalHamming(int filter_size, float x) { | |
| 79 if (x <= -filter_size || x >= filter_size) | |
| 80 return 0.0f; // Outside of the window. | |
| 81 if (x > -std::numeric_limits<float>::epsilon() && | |
| 82 x < std::numeric_limits<float>::epsilon()) | |
| 83 return 1.0f; // Special case the sinc discontinuity at the origin. | |
| 84 const float xpi = x * static_cast<float>(M_PI); | |
| 85 | |
| 86 return ((sin(xpi) / xpi) * // sinc(x) | |
| 87 (0.54f + 0.46f * cos(xpi / filter_size))); // hamming(x) | |
| 88 } | |
| 89 | |
| 62 // ResizeFilter ---------------------------------------------------------------- | 90 // ResizeFilter ---------------------------------------------------------------- |
| 63 | 91 |
| 64 // Encapsulates computation and storage of the filters required for one complete | 92 // Encapsulates computation and storage of the filters required for one complete |
| 65 // resize operation. | 93 // resize operation. |
| 66 class ResizeFilter { | 94 class ResizeFilter { |
| 67 public: | 95 public: |
| 68 ResizeFilter(ImageOperations::ResizeMethod method, | 96 ResizeFilter(ImageOperations::ResizeMethod method, |
| 69 int src_full_width, int src_full_height, | 97 int src_full_width, int src_full_height, |
| 70 int dest_width, int dest_height, | 98 int dest_width, int dest_height, |
| 71 const SkIRect& dest_subset); | 99 const SkIRect& dest_subset); |
| 72 | 100 |
| 73 // Returns the bounds in the input bitmap of data that is used in the output. | 101 // Returns the bounds in the input bitmap of data that is used in the output. |
| 74 // The filter offsets are within this rectangle. | 102 // The filter offsets are within this rectangle. |
| 75 const SkIRect& src_depend() { return src_depend_; } | 103 const SkIRect& src_depend() { return src_depend_; } |
| 76 | 104 |
| 77 // Returns the filled filter values. | 105 // Returns the filled filter values. |
| 78 const ConvolutionFilter1D& x_filter() { return x_filter_; } | 106 const ConvolutionFilter1D& x_filter() { return x_filter_; } |
| 79 const ConvolutionFilter1D& y_filter() { return y_filter_; } | 107 const ConvolutionFilter1D& y_filter() { return y_filter_; } |
| 80 | 108 |
| 81 private: | 109 private: |
| 82 // Returns the number of pixels that the filer spans, in filter space (the | 110 // Returns the number of pixels that the filer spans, in filter space (the |
| 83 // destination image). | 111 // destination image). |
| 84 float GetFilterSupport(float scale) { | 112 float GetFilterSupport(float scale) { |
| 85 switch (method_) { | 113 switch (method_) { |
| 86 case ImageOperations::RESIZE_BOX: | 114 case ImageOperations::RESIZE_BOX: |
| 87 // The box filter just scales with the image scaling. | 115 // The box filter just scales with the image scaling. |
| 88 return 0.5f; // Only want one side of the filter = /2. | 116 return 0.5f; // Only want one side of the filter = /2. |
| 117 case ImageOperations::RESIZE_HAMMING1: | |
| 118 // The Hamming filter takes as much space in the source image in | |
| 119 // each direction as the size of the window = 1 for Hamming1. | |
| 120 return 1.0f; | |
| 121 case ImageOperations::RESIZE_LANCZOS2: | |
|
brettw
2010/12/17 20:07:55
Where is LANCZOS1 handled? Same below.
evannier
2010/12/19 00:15:03
LANCZOS1 should never have been in the checkin (it
| |
| 122 // The Lanczos filter takes as much space in the source image in | |
| 123 // each direction as the size of the window = 2 for Lanczos2. | |
| 124 return 2.0f; | |
| 89 case ImageOperations::RESIZE_LANCZOS3: | 125 case ImageOperations::RESIZE_LANCZOS3: |
| 90 // The lanczos filter takes as much space in the source image in | 126 // The Lanczos filter takes as much space in the source image in |
| 91 // each direction as the size of the window = 3 for Lanczos3. | 127 // each direction as the size of the window = 3 for Lanczos3. |
| 92 return 3.0f; | 128 return 3.0f; |
| 93 default: | 129 default: |
| 94 NOTREACHED(); | 130 NOTREACHED(); |
| 95 return 1.0f; | 131 return 1.0f; |
| 96 } | 132 } |
| 97 } | 133 } |
| 98 | 134 |
| 99 // Computes one set of filters either horizontally or vertically. The caller | 135 // Computes one set of filters either horizontally or vertically. The caller |
| 100 // will specify the "min" and "max" rather than the bottom/top and | 136 // will specify the "min" and "max" rather than the bottom/top and |
| 101 // right/bottom so that the same code can be re-used in each dimension. | 137 // right/bottom so that the same code can be re-used in each dimension. |
| 102 // | 138 // |
| 103 // |src_depend_lo| and |src_depend_size| gives the range for the source | 139 // |src_depend_lo| and |src_depend_size| gives the range for the source |
| 104 // depend rectangle (horizontally or vertically at the caller's discretion | 140 // depend rectangle (horizontally or vertically at the caller's discretion |
| 105 // -- see above for what this means). | 141 // -- see above for what this means). |
| 106 // | 142 // |
| 107 // Likewise, the range of destination values to compute and the scale factor | 143 // Likewise, the range of destination values to compute and the scale factor |
| 108 // for the transform is also specified. | 144 // for the transform is also specified. |
| 109 void ComputeFilters(int src_size, | 145 void ComputeFilters(int src_size, |
| 110 int dest_subset_lo, int dest_subset_size, | 146 int dest_subset_lo, int dest_subset_size, |
| 111 float scale, float src_support, | 147 float scale, float src_support, |
| 112 ConvolutionFilter1D* output); | 148 ConvolutionFilter1D* output); |
| 113 | 149 |
| 114 // Computes the filter value given the coordinate in filter space. | 150 // Computes the filter value given the coordinate in filter space. |
| 115 inline float ComputeFilter(float pos) { | 151 inline float ComputeFilter(float pos) { |
| 116 switch (method_) { | 152 switch (method_) { |
| 117 case ImageOperations::RESIZE_BOX: | 153 case ImageOperations::RESIZE_BOX: |
| 118 return EvalBox(pos); | 154 return EvalBox(pos); |
| 155 case ImageOperations::RESIZE_HAMMING1: | |
| 156 return EvalHamming(1, pos); | |
| 157 case ImageOperations::RESIZE_LANCZOS2: | |
| 158 return EvalLanczos(2, pos); | |
| 119 case ImageOperations::RESIZE_LANCZOS3: | 159 case ImageOperations::RESIZE_LANCZOS3: |
| 120 return EvalLanczos(3, pos); | 160 return EvalLanczos(3, pos); |
| 121 default: | 161 default: |
| 122 NOTREACHED(); | 162 NOTREACHED(); |
| 123 return 0; | 163 return 0; |
| 124 } | 164 } |
| 125 } | 165 } |
| 126 | 166 |
| 127 ImageOperations::ResizeMethod method_; | 167 ImageOperations::ResizeMethod method_; |
| 128 | 168 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 142 | 182 |
| 143 DISALLOW_COPY_AND_ASSIGN(ResizeFilter); | 183 DISALLOW_COPY_AND_ASSIGN(ResizeFilter); |
| 144 }; | 184 }; |
| 145 | 185 |
| 146 ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, | 186 ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, |
| 147 int src_full_width, int src_full_height, | 187 int src_full_width, int src_full_height, |
| 148 int dest_width, int dest_height, | 188 int dest_width, int dest_height, |
| 149 const SkIRect& dest_subset) | 189 const SkIRect& dest_subset) |
| 150 : method_(method), | 190 : method_(method), |
| 151 out_bounds_(dest_subset) { | 191 out_bounds_(dest_subset) { |
| 192 // method_ will only ever refer to an "algorithm method". | |
| 193 SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) | |
| 194 && (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); | |
| 195 | |
| 152 float scale_x = static_cast<float>(dest_width) / | 196 float scale_x = static_cast<float>(dest_width) / |
| 153 static_cast<float>(src_full_width); | 197 static_cast<float>(src_full_width); |
| 154 float scale_y = static_cast<float>(dest_height) / | 198 float scale_y = static_cast<float>(dest_height) / |
| 155 static_cast<float>(src_full_height); | 199 static_cast<float>(src_full_height); |
| 156 | 200 |
| 157 x_filter_support_ = GetFilterSupport(scale_x); | 201 x_filter_support_ = GetFilterSupport(scale_x); |
| 158 y_filter_support_ = GetFilterSupport(scale_y); | 202 y_filter_support_ = GetFilterSupport(scale_y); |
| 159 | 203 |
| 160 SkIRect src_full = { 0, 0, src_full_width, src_full_height }; | |
| 161 SkIRect dest_full = { 0, 0, static_cast<int>(src_full_width * scale_x + 0.5), | |
| 162 static_cast<int>(src_full_height * scale_y + 0.5) }; | |
| 163 | |
| 164 // Support of the filter in source space. | 204 // Support of the filter in source space. |
| 165 float src_x_support = x_filter_support_ / scale_x; | 205 float src_x_support = x_filter_support_ / scale_x; |
| 166 float src_y_support = y_filter_support_ / scale_y; | 206 float src_y_support = y_filter_support_ / scale_y; |
| 167 | 207 |
| 168 ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(), | 208 ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(), |
| 169 scale_x, src_x_support, &x_filter_); | 209 scale_x, src_x_support, &x_filter_); |
| 170 ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(), | 210 ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(), |
| 171 scale_y, src_y_support, &y_filter_); | 211 scale_y, src_y_support, &y_filter_); |
| 172 } | 212 } |
| 173 | 213 |
| 214 // TODO(egouriou): Take advantage of periods in the convolution. | |
| 215 // Practical resizing filters are periodic outside of the border area. | |
| 216 // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the | |
| 217 // source become p pixels in the destination) will have a period of p. | |
| 218 // A nice consequence is a period of 1 when downscaling by an integral | |
| 219 // factor. Downscaling from typical display resolutions is also bound | |
| 220 // to produce interesting periods as those are chosen to have multiple | |
| 221 // small factors. | |
| 222 // Small periods reduce computational load and improve cache usage if | |
| 223 // the coefficients can be shared. For periods of 1 we can consider | |
| 224 // loading the factors only once outside the borders. | |
| 174 void ResizeFilter::ComputeFilters(int src_size, | 225 void ResizeFilter::ComputeFilters(int src_size, |
| 175 int dest_subset_lo, int dest_subset_size, | 226 int dest_subset_lo, int dest_subset_size, |
| 176 float scale, float src_support, | 227 float scale, float src_support, |
| 177 ConvolutionFilter1D* output) { | 228 ConvolutionFilter1D* output) { |
| 178 int dest_subset_hi = dest_subset_lo + dest_subset_size; // [lo, hi) | 229 int dest_subset_hi = dest_subset_lo + dest_subset_size; // [lo, hi) |
| 179 | 230 |
| 180 // When we're doing a magnification, the scale will be larger than one. This | 231 // When we're doing a magnification, the scale will be larger than one. This |
| 181 // means the destination pixels are much smaller than the source pixels, and | 232 // means the destination pixels are much smaller than the source pixels, and |
| 182 // that the range covered by the filter won't necessarily cover any source | 233 // that the range covered by the filter won't necessarily cover any source |
| 183 // pixel boundaries. Therefore, we use these clamped values (max of 1) for | 234 // pixel boundaries. Therefore, we use these clamped values (max of 1) for |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 194 // filter values for each one. Those values will tell us how to blend the | 245 // filter values for each one. Those values will tell us how to blend the |
| 195 // source pixels to compute the destination pixel. | 246 // source pixels to compute the destination pixel. |
| 196 for (int dest_subset_i = dest_subset_lo; dest_subset_i < dest_subset_hi; | 247 for (int dest_subset_i = dest_subset_lo; dest_subset_i < dest_subset_hi; |
| 197 dest_subset_i++) { | 248 dest_subset_i++) { |
| 198 // Reset the arrays. We don't declare them inside so they can re-use the | 249 // Reset the arrays. We don't declare them inside so they can re-use the |
| 199 // same malloc-ed buffer. | 250 // same malloc-ed buffer. |
| 200 filter_values->clear(); | 251 filter_values->clear(); |
| 201 fixed_filter_values->clear(); | 252 fixed_filter_values->clear(); |
| 202 | 253 |
| 203 // This is the pixel in the source directly under the pixel in the dest. | 254 // This is the pixel in the source directly under the pixel in the dest. |
| 204 float src_pixel = dest_subset_i * inv_scale; | 255 // Note that we base computations on the "center" of the pixels. To see |
| 256 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x | |
| 257 // downscale should "cover" the pixels around the pixel with *its center* | |
| 258 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). | |
| 259 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). | |
| 260 float src_pixel = (static_cast<float>(dest_subset_i) + 0.5f) * inv_scale; | |
|
brettw
2010/12/17 20:07:55
This change you made should shift everything by 0.
evannier
2010/12/19 00:15:03
This is correct.
Basically, if you did the simple
brettw
2010/12/20 18:18:45
That's great, thanks for this fix.
Unfortunately,
evannier
2011/01/18 21:51:53
I have the fix in the code, but commented out for
| |
| 205 | 261 |
| 206 // Compute the (inclusive) range of source pixels the filter covers. | 262 // Compute the (inclusive) range of source pixels the filter covers. |
| 207 int src_begin = std::max(0, FloorInt(src_pixel - src_support)); | 263 int src_begin = std::max(0, FloorInt(src_pixel - src_support)); |
| 208 int src_end = std::min(src_size - 1, CeilInt(src_pixel + src_support)); | 264 int src_end = std::min(src_size - 1, CeilInt(src_pixel + src_support)); |
| 209 | 265 |
| 210 // Compute the unnormalized filter value at each location of the source | 266 // Compute the unnormalized filter value at each location of the source |
| 211 // it covers. | 267 // it covers. |
| 212 float filter_sum = 0.0f; // Sub of the filter values for normalizing. | 268 float filter_sum = 0.0f; // Sub of the filter values for normalizing. |
| 213 for (int cur_filter_pixel = src_begin; cur_filter_pixel <= src_end; | 269 for (int cur_filter_pixel = src_begin; cur_filter_pixel <= src_end; |
| 214 cur_filter_pixel++) { | 270 cur_filter_pixel++) { |
| 215 // Distance from the center of the filter, this is the filter coordinate | 271 // Distance from the center of the filter, this is the filter coordinate |
| 216 // in source space. | 272 // in source space. We also need to consider the center of the pixel |
| 217 float src_filter_pos = cur_filter_pixel - src_pixel; | 273 // when comparing distance against 'src_pixel'. In the 5x downscale |
| 274 // example used above the distance from the center of the filter to | |
| 275 // the pixel with coordinates (2, 2) should be 0, because its center | |
| 276 // is at (2.5, 2.5). | |
| 277 float src_filter_dist = ((static_cast<float>(cur_filter_pixel) + 0.5f) - | |
| 278 src_pixel); | |
| 218 | 279 |
| 219 // Since the filter really exists in dest space, map it there. | 280 // Since the filter really exists in dest space, map it there. |
| 220 float dest_filter_pos = src_filter_pos * clamped_scale; | 281 float dest_filter_dist = src_filter_dist * clamped_scale; |
| 221 | 282 |
| 222 // Compute the filter value at that location. | 283 // Compute the filter value at that location. |
| 223 float filter_value = ComputeFilter(dest_filter_pos); | 284 float filter_value = ComputeFilter(dest_filter_dist); |
| 224 filter_values->push_back(filter_value); | 285 filter_values->push_back(filter_value); |
| 225 | 286 |
| 226 filter_sum += filter_value; | 287 filter_sum += filter_value; |
| 227 } | 288 } |
| 228 DCHECK(!filter_values->empty()) << "We should always get a filter!"; | 289 DCHECK(!filter_values->empty()) << "We should always get a filter!"; |
| 229 | 290 |
| 230 // The filter must be normalized so that we don't affect the brightness of | 291 // The filter must be normalized so that we don't affect the brightness of |
| 231 // the image. Convert to normalized fixed point. | 292 // the image. Convert to normalized fixed point. |
| 232 int16 fixed_sum = 0; | 293 int16 fixed_sum = 0; |
| 233 for (size_t i = 0; i < filter_values->size(); i++) { | 294 for (size_t i = 0; i < filter_values->size(); i++) { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 #else | 423 #else |
| 363 return SkBitmap(); | 424 return SkBitmap(); |
| 364 #endif // OS_POSIX && !OS_MACOSX | 425 #endif // OS_POSIX && !OS_MACOSX |
| 365 } | 426 } |
| 366 | 427 |
| 367 // static | 428 // static |
| 368 SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, | 429 SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, |
| 369 ResizeMethod method, | 430 ResizeMethod method, |
| 370 int dest_width, int dest_height, | 431 int dest_width, int dest_height, |
| 371 const SkIRect& dest_subset) { | 432 const SkIRect& dest_subset) { |
| 433 // Ensure that the ResizeMethod enumeration is sound. | |
| 434 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) | |
| 435 && (method <= RESIZE_LAST_QUALITY_METHOD)) | |
| 436 || ((RESIZE_FIRST_ALGORITHM_METHOD <= method) | |
| 437 && (method <= RESIZE_LAST_ALGORITHM_METHOD))); | |
| 438 | |
| 372 // Time how long this takes to see if it's a problem for users. | 439 // Time how long this takes to see if it's a problem for users. |
| 373 base::TimeTicks resize_start = base::TimeTicks::Now(); | 440 base::TimeTicks resize_start = base::TimeTicks::Now(); |
| 374 | 441 |
| 375 SkIRect dest = { 0, 0, dest_width, dest_height }; | 442 SkIRect dest = { 0, 0, dest_width, dest_height }; |
| 376 DCHECK(dest.contains(dest_subset)) << | 443 DCHECK(dest.contains(dest_subset)) << |
| 377 "The supplied subset does not fall within the destination image."; | 444 "The supplied subset does not fall within the destination image."; |
| 378 | 445 |
| 379 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just | 446 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just |
| 380 // return empty. | 447 // return empty. |
| 381 if (source.width() < 1 || source.height() < 1 || | 448 if (source.width() < 1 || source.height() < 1 || |
| 382 dest_width < 1 || dest_height < 1) | 449 dest_width < 1 || dest_height < 1) |
| 383 return SkBitmap(); | 450 return SkBitmap(); |
| 384 | 451 |
| 452 // Convert any "Quality Method" into an "Algorithm Method" | |
| 453 if ( (RESIZE_FIRST_QUALITY_METHOD <= method) | |
|
brettw
2010/12/17 20:07:55
We'd normally put the && at the end of this first
evannier
2010/12/19 00:15:03
Done.
| |
| 454 && (method <= RESIZE_LAST_QUALITY_METHOD)) { | |
| 455 #if defined(GTV) | |
|
brettw
2010/12/17 20:07:55
I don't think you need this define, it seems like
evannier
2010/12/19 00:15:03
That is correct. But as you can imagine, there wil
brettw
2010/12/20 18:18:45
I agree we may need an ifdef for some platforms. B
evannier
2011/01/18 21:51:53
Done. Fair enough. For now, I have removed the ifd
| |
| 456 // The call to ImageOperationsGtv::Resize() above took care of | |
| 457 // GPU-acceleration in the cases where it is possible. So now we just | |
| 458 // pick the appropriate software method for each resize quality. | |
| 459 switch (method) { | |
| 460 // Users of RESIZE_GOOD are willing to trade a lot of quality to | |
| 461 // get speed, allowing the use of linear resampling to get hardware | |
| 462 // acceleration (SRB). Hence any of our "good" software filters | |
| 463 // will be acceptable, and we use the fastest one, Hamming-1. | |
| 464 case RESIZE_GOOD: | |
| 465 // Users of RESIZE_BETTER are willing to trade some quality in order | |
| 466 // to improve performance, but are guaranteed not to devolve to a linear | |
| 467 // resampling. In visual tests we see that Hamming-1 is not as good as | |
| 468 // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is | |
| 469 // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed | |
| 470 // an acceptable trade-off between quality and speed. | |
| 471 case RESIZE_BETTER: | |
| 472 method = RESIZE_HAMMING1; | |
| 473 break; | |
| 474 default: | |
| 475 method = RESIZE_LANCZOS3; | |
| 476 break; | |
| 477 } | |
| 478 #else | |
| 479 method = RESIZE_LANCZOS3; | |
| 480 #endif | |
| 481 } | |
| 482 | |
| 483 // Check that we deal with an "algorithm methods" from this point onward. | |
| 484 SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) | |
| 485 && (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); | |
| 486 | |
| 385 SkAutoLockPixels locker(source); | 487 SkAutoLockPixels locker(source); |
| 386 | 488 |
| 387 ResizeFilter filter(method, source.width(), source.height(), | 489 ResizeFilter filter(method, source.width(), source.height(), |
| 388 dest_width, dest_height, dest_subset); | 490 dest_width, dest_height, dest_subset); |
| 389 | 491 |
| 390 // Get a source bitmap encompassing this touched area. We construct the | 492 // Get a source bitmap encompassing this touched area. We construct the |
| 391 // offsets and row strides such that it looks like a new bitmap, while | 493 // offsets and row strides such that it looks like a new bitmap, while |
| 392 // referring to the old data. | 494 // referring to the old data. |
| 393 const uint8* source_subset = | 495 const uint8* source_subset = |
| 394 reinterpret_cast<const uint8*>(source.getPixels()); | 496 reinterpret_cast<const uint8*>(source.getPixels()); |
| 395 | 497 |
| 396 // Convolve into the result. | 498 // Convolve into the result. |
| 397 SkBitmap result; | 499 SkBitmap result; |
| 398 result.setConfig(SkBitmap::kARGB_8888_Config, | 500 result.setConfig(SkBitmap::kARGB_8888_Config, |
| 399 dest_subset.width(), dest_subset.height()); | 501 dest_subset.width(), dest_subset.height()); |
| 400 result.allocPixels(); | 502 result.allocPixels(); |
| 401 BGRAConvolve2D(source_subset, static_cast<int>(source.rowBytes()), | 503 BGRAConvolve2D(source_subset, static_cast<int>(source.rowBytes()), |
| 402 !source.isOpaque(), filter.x_filter(), filter.y_filter(), | 504 !source.isOpaque(), filter.x_filter(), filter.y_filter(), |
| 505 static_cast<int>(result.rowBytes()), | |
| 403 static_cast<unsigned char*>(result.getPixels())); | 506 static_cast<unsigned char*>(result.getPixels())); |
| 404 | 507 |
| 405 // Preserve the "opaque" flag for use as an optimization later. | 508 // Preserve the "opaque" flag for use as an optimization later. |
| 406 result.setIsOpaque(source.isOpaque()); | 509 result.setIsOpaque(source.isOpaque()); |
| 407 | 510 |
| 408 base::TimeDelta delta = base::TimeTicks::Now() - resize_start; | 511 base::TimeDelta delta = base::TimeTicks::Now() - resize_start; |
| 409 UMA_HISTOGRAM_TIMES("Image.ResampleMS", delta); | 512 UMA_HISTOGRAM_TIMES("Image.ResampleMS", delta); |
| 410 | 513 |
| 411 return result; | 514 return result; |
| 412 } | 515 } |
| 413 | 516 |
| 414 // static | 517 // static |
| 415 SkBitmap ImageOperations::Resize(const SkBitmap& source, | 518 SkBitmap ImageOperations::Resize(const SkBitmap& source, |
| 416 ResizeMethod method, | 519 ResizeMethod method, |
| 417 int dest_width, int dest_height) { | 520 int dest_width, int dest_height) { |
| 418 SkIRect dest_subset = { 0, 0, dest_width, dest_height }; | 521 SkIRect dest_subset = { 0, 0, dest_width, dest_height }; |
| 419 return Resize(source, method, dest_width, dest_height, dest_subset); | 522 return Resize(source, method, dest_width, dest_height, dest_subset); |
| 420 } | 523 } |
| 421 | 524 |
| 422 } // namespace skia | 525 } // namespace skia |
| OLD | NEW |