| OLD | NEW |
| 1 // Copyright (c) 2009 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 #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: |
| 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. |
| 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 // TODO(evannier): this code is therefore incorrect and should read: |
| 261 // float src_pixel = (static_cast<float>(dest_subset_i) + 0.5f) * inv_scale; |
| 262 // I leave it incorrect, because changing it would require modifying |
| 263 // the results for the webkit test, which I will do in a subsequent checkin. |
| 204 float src_pixel = dest_subset_i * inv_scale; | 264 float src_pixel = dest_subset_i * inv_scale; |
| 205 | 265 |
| 206 // Compute the (inclusive) range of source pixels the filter covers. | 266 // Compute the (inclusive) range of source pixels the filter covers. |
| 207 int src_begin = std::max(0, FloorInt(src_pixel - src_support)); | 267 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)); | 268 int src_end = std::min(src_size - 1, CeilInt(src_pixel + src_support)); |
| 209 | 269 |
| 210 // Compute the unnormalized filter value at each location of the source | 270 // Compute the unnormalized filter value at each location of the source |
| 211 // it covers. | 271 // it covers. |
| 212 float filter_sum = 0.0f; // Sub of the filter values for normalizing. | 272 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; | 273 for (int cur_filter_pixel = src_begin; cur_filter_pixel <= src_end; |
| 214 cur_filter_pixel++) { | 274 cur_filter_pixel++) { |
| 215 // Distance from the center of the filter, this is the filter coordinate | 275 // Distance from the center of the filter, this is the filter coordinate |
| 216 // in source space. | 276 // in source space. We also need to consider the center of the pixel |
| 217 float src_filter_pos = cur_filter_pixel - src_pixel; | 277 // when comparing distance against 'src_pixel'. In the 5x downscale |
| 278 // example used above the distance from the center of the filter to |
| 279 // the pixel with coordinates (2, 2) should be 0, because its center |
| 280 // is at (2.5, 2.5). |
| 281 // TODO(evannier): as above (in regards to the 0.5 pixel error), |
| 282 // this code is incorrect, but is left it for the same reasons. |
| 283 // float src_filter_dist = |
| 284 // ((static_cast<float>(cur_filter_pixel) + 0.5f) - src_pixel); |
| 285 float src_filter_dist = cur_filter_pixel - src_pixel; |
| 218 | 286 |
| 219 // Since the filter really exists in dest space, map it there. | 287 // Since the filter really exists in dest space, map it there. |
| 220 float dest_filter_pos = src_filter_pos * clamped_scale; | 288 float dest_filter_dist = src_filter_dist * clamped_scale; |
| 221 | 289 |
| 222 // Compute the filter value at that location. | 290 // Compute the filter value at that location. |
| 223 float filter_value = ComputeFilter(dest_filter_pos); | 291 float filter_value = ComputeFilter(dest_filter_dist); |
| 224 filter_values->push_back(filter_value); | 292 filter_values->push_back(filter_value); |
| 225 | 293 |
| 226 filter_sum += filter_value; | 294 filter_sum += filter_value; |
| 227 } | 295 } |
| 228 DCHECK(!filter_values->empty()) << "We should always get a filter!"; | 296 DCHECK(!filter_values->empty()) << "We should always get a filter!"; |
| 229 | 297 |
| 230 // The filter must be normalized so that we don't affect the brightness of | 298 // The filter must be normalized so that we don't affect the brightness of |
| 231 // the image. Convert to normalized fixed point. | 299 // the image. Convert to normalized fixed point. |
| 232 int16 fixed_sum = 0; | 300 int16 fixed_sum = 0; |
| 233 for (size_t i = 0; i < filter_values->size(); i++) { | 301 for (size_t i = 0; i < filter_values->size(); i++) { |
| 234 int16 cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum); | 302 int16 cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum); |
| 235 fixed_sum += cur_fixed; | 303 fixed_sum += cur_fixed; |
| 236 fixed_filter_values->push_back(cur_fixed); | 304 fixed_filter_values->push_back(cur_fixed); |
| 237 } | 305 } |
| 238 | 306 |
| 239 // The conversion to fixed point will leave some rounding errors, which | 307 // The conversion to fixed point will leave some rounding errors, which |
| 240 // we add back in to avoid affecting the brightness of the image. We | 308 // we add back in to avoid affecting the brightness of the image. We |
| 241 // arbitrarily add this to the center of the filter array (this won't always | 309 // arbitrarily add this to the center of the filter array (this won't always |
| 242 // be the center of the filter function since it could get clipped on the | 310 // be the center of the filter function since it could get clipped on the |
| 243 // edges, but it doesn't matter enough to worry about that case). | 311 // edges, but it doesn't matter enough to worry about that case). |
| 244 int16 leftovers = output->FloatToFixed(1.0f) - fixed_sum; | 312 int16 leftovers = output->FloatToFixed(1.0f) - fixed_sum; |
| 245 fixed_filter_values[fixed_filter_values->size() / 2] += leftovers; | 313 fixed_filter_values[fixed_filter_values->size() / 2] += leftovers; |
| 246 | 314 |
| 247 // Now it's ready to go. | 315 // Now it's ready to go. |
| 248 output->AddFilter(src_begin, &fixed_filter_values[0], | 316 output->AddFilter(src_begin, &fixed_filter_values[0], |
| 249 static_cast<int>(fixed_filter_values->size())); | 317 static_cast<int>(fixed_filter_values->size())); |
| 250 } | 318 } |
| 251 } | 319 } |
| 252 | 320 |
| 321 ImageOperations::ResizeMethod ResizeMethodToAlgorithmMethod( |
| 322 ImageOperations::ResizeMethod method) { |
| 323 // Convert any "Quality Method" into an "Algorithm Method" |
| 324 if (method >= ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD && |
| 325 method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD) { |
| 326 return method; |
| 327 } |
| 328 // The call to ImageOperationsGtv::Resize() above took care of |
| 329 // GPU-acceleration in the cases where it is possible. So now we just |
| 330 // pick the appropriate software method for each resize quality. |
| 331 switch (method) { |
| 332 // Users of RESIZE_GOOD are willing to trade a lot of quality to |
| 333 // get speed, allowing the use of linear resampling to get hardware |
| 334 // acceleration (SRB). Hence any of our "good" software filters |
| 335 // will be acceptable, and we use the fastest one, Hamming-1. |
| 336 case ImageOperations::RESIZE_GOOD: |
| 337 // Users of RESIZE_BETTER are willing to trade some quality in order |
| 338 // to improve performance, but are guaranteed not to devolve to a linear |
| 339 // resampling. In visual tests we see that Hamming-1 is not as good as |
| 340 // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is |
| 341 // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed |
| 342 // an acceptable trade-off between quality and speed. |
| 343 case ImageOperations::RESIZE_BETTER: |
| 344 return ImageOperations::RESIZE_HAMMING1; |
| 345 default: |
| 346 return ImageOperations::RESIZE_LANCZOS3; |
| 347 } |
| 348 } |
| 349 |
| 253 } // namespace | 350 } // namespace |
| 254 | 351 |
| 255 // Resize ---------------------------------------------------------------------- | 352 // Resize ---------------------------------------------------------------------- |
| 256 | 353 |
| 257 // static | 354 // static |
| 258 SkBitmap ImageOperations::Resize(const SkBitmap& source, | 355 SkBitmap ImageOperations::Resize(const SkBitmap& source, |
| 259 ResizeMethod method, | 356 ResizeMethod method, |
| 260 int dest_width, int dest_height, | 357 int dest_width, int dest_height, |
| 261 const SkIRect& dest_subset) { | 358 const SkIRect& dest_subset) { |
| 262 if (method == ImageOperations::RESIZE_SUBPIXEL) | 359 if (method == ImageOperations::RESIZE_SUBPIXEL) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 #else | 459 #else |
| 363 return SkBitmap(); | 460 return SkBitmap(); |
| 364 #endif // OS_POSIX && !OS_MACOSX | 461 #endif // OS_POSIX && !OS_MACOSX |
| 365 } | 462 } |
| 366 | 463 |
| 367 // static | 464 // static |
| 368 SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, | 465 SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, |
| 369 ResizeMethod method, | 466 ResizeMethod method, |
| 370 int dest_width, int dest_height, | 467 int dest_width, int dest_height, |
| 371 const SkIRect& dest_subset) { | 468 const SkIRect& dest_subset) { |
| 469 // Ensure that the ResizeMethod enumeration is sound. |
| 470 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && |
| 471 (method <= RESIZE_LAST_QUALITY_METHOD)) || |
| 472 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
| 473 (method <= RESIZE_LAST_ALGORITHM_METHOD))); |
| 474 |
| 372 // Time how long this takes to see if it's a problem for users. | 475 // Time how long this takes to see if it's a problem for users. |
| 373 base::TimeTicks resize_start = base::TimeTicks::Now(); | 476 base::TimeTicks resize_start = base::TimeTicks::Now(); |
| 374 | 477 |
| 375 SkIRect dest = { 0, 0, dest_width, dest_height }; | 478 SkIRect dest = { 0, 0, dest_width, dest_height }; |
| 376 DCHECK(dest.contains(dest_subset)) << | 479 DCHECK(dest.contains(dest_subset)) << |
| 377 "The supplied subset does not fall within the destination image."; | 480 "The supplied subset does not fall within the destination image."; |
| 378 | 481 |
| 379 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just | 482 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just |
| 380 // return empty. | 483 // return empty. |
| 381 if (source.width() < 1 || source.height() < 1 || | 484 if (source.width() < 1 || source.height() < 1 || |
| 382 dest_width < 1 || dest_height < 1) | 485 dest_width < 1 || dest_height < 1) |
| 383 return SkBitmap(); | 486 return SkBitmap(); |
| 384 | 487 |
| 488 method = ResizeMethodToAlgorithmMethod(method); |
| 489 // Check that we deal with an "algorithm methods" from this point onward. |
| 490 SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
| 491 (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); |
| 492 |
| 385 SkAutoLockPixels locker(source); | 493 SkAutoLockPixels locker(source); |
| 386 | 494 |
| 387 ResizeFilter filter(method, source.width(), source.height(), | 495 ResizeFilter filter(method, source.width(), source.height(), |
| 388 dest_width, dest_height, dest_subset); | 496 dest_width, dest_height, dest_subset); |
| 389 | 497 |
| 390 // Get a source bitmap encompassing this touched area. We construct the | 498 // 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 | 499 // offsets and row strides such that it looks like a new bitmap, while |
| 392 // referring to the old data. | 500 // referring to the old data. |
| 393 const uint8* source_subset = | 501 const uint8* source_subset = |
| 394 reinterpret_cast<const uint8*>(source.getPixels()); | 502 reinterpret_cast<const uint8*>(source.getPixels()); |
| 395 | 503 |
| 396 // Convolve into the result. | 504 // Convolve into the result. |
| 397 SkBitmap result; | 505 SkBitmap result; |
| 398 result.setConfig(SkBitmap::kARGB_8888_Config, | 506 result.setConfig(SkBitmap::kARGB_8888_Config, |
| 399 dest_subset.width(), dest_subset.height()); | 507 dest_subset.width(), dest_subset.height()); |
| 400 result.allocPixels(); | 508 result.allocPixels(); |
| 401 BGRAConvolve2D(source_subset, static_cast<int>(source.rowBytes()), | 509 BGRAConvolve2D(source_subset, static_cast<int>(source.rowBytes()), |
| 402 !source.isOpaque(), filter.x_filter(), filter.y_filter(), | 510 !source.isOpaque(), filter.x_filter(), filter.y_filter(), |
| 511 static_cast<int>(result.rowBytes()), |
| 403 static_cast<unsigned char*>(result.getPixels())); | 512 static_cast<unsigned char*>(result.getPixels())); |
| 404 | 513 |
| 405 // Preserve the "opaque" flag for use as an optimization later. | 514 // Preserve the "opaque" flag for use as an optimization later. |
| 406 result.setIsOpaque(source.isOpaque()); | 515 result.setIsOpaque(source.isOpaque()); |
| 407 | 516 |
| 408 base::TimeDelta delta = base::TimeTicks::Now() - resize_start; | 517 base::TimeDelta delta = base::TimeTicks::Now() - resize_start; |
| 409 UMA_HISTOGRAM_TIMES("Image.ResampleMS", delta); | 518 UMA_HISTOGRAM_TIMES("Image.ResampleMS", delta); |
| 410 | 519 |
| 411 return result; | 520 return result; |
| 412 } | 521 } |
| 413 | 522 |
| 414 // static | 523 // static |
| 415 SkBitmap ImageOperations::Resize(const SkBitmap& source, | 524 SkBitmap ImageOperations::Resize(const SkBitmap& source, |
| 416 ResizeMethod method, | 525 ResizeMethod method, |
| 417 int dest_width, int dest_height) { | 526 int dest_width, int dest_height) { |
| 418 SkIRect dest_subset = { 0, 0, dest_width, dest_height }; | 527 SkIRect dest_subset = { 0, 0, dest_width, dest_height }; |
| 419 return Resize(source, method, dest_width, dest_height, dest_subset); | 528 return Resize(source, method, dest_width, dest_height, dest_subset); |
| 420 } | 529 } |
| 421 | 530 |
| 422 } // namespace skia | 531 } // namespace skia |
| OLD | NEW |