michael@0: // Copyright (c) 2006-2012 The Chromium Authors. All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions michael@0: // are met: michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above copyright michael@0: // notice, this list of conditions and the following disclaimer in michael@0: // the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google, Inc. nor the names of its contributors michael@0: // may be used to endorse or promote products derived from this michael@0: // software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, michael@0: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS michael@0: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED michael@0: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, michael@0: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT michael@0: // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: // SUCH DAMAGE. michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #define _USE_MATH_DEFINES michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "image_operations.h" michael@0: michael@0: #include "base/stack_container.h" michael@0: #include "convolver.h" michael@0: #include "skia/SkColorPriv.h" michael@0: #include "skia/SkBitmap.h" michael@0: #include "skia/SkRect.h" michael@0: #include "skia/SkFontHost.h" michael@0: michael@0: namespace skia { michael@0: michael@0: namespace { michael@0: michael@0: // Returns the ceiling/floor as an integer. michael@0: inline int CeilInt(float val) { michael@0: return static_cast(ceil(val)); michael@0: } michael@0: inline int FloorInt(float val) { michael@0: return static_cast(floor(val)); michael@0: } michael@0: michael@0: // Filter function computation ------------------------------------------------- michael@0: michael@0: // Evaluates the box filter, which goes from -0.5 to +0.5. michael@0: float EvalBox(float x) { michael@0: return (x >= -0.5f && x < 0.5f) ? 1.0f : 0.0f; michael@0: } michael@0: michael@0: // Evaluates the Lanczos filter of the given filter size window for the given michael@0: // position. michael@0: // michael@0: // |filter_size| is the width of the filter (the "window"), outside of which michael@0: // the value of the function is 0. Inside of the window, the value is the michael@0: // normalized sinc function: michael@0: // lanczos(x) = sinc(x) * sinc(x / filter_size); michael@0: // where michael@0: // sinc(x) = sin(pi*x) / (pi*x); michael@0: float EvalLanczos(int filter_size, float x) { michael@0: if (x <= -filter_size || x >= filter_size) michael@0: return 0.0f; // Outside of the window. michael@0: if (x > -std::numeric_limits::epsilon() && michael@0: x < std::numeric_limits::epsilon()) michael@0: return 1.0f; // Special case the discontinuity at the origin. michael@0: float xpi = x * static_cast(M_PI); michael@0: return (sin(xpi) / xpi) * // sinc(x) michael@0: sin(xpi / filter_size) / (xpi / filter_size); // sinc(x/filter_size) michael@0: } michael@0: michael@0: // Evaluates the Hamming filter of the given filter size window for the given michael@0: // position. michael@0: // michael@0: // The filter covers [-filter_size, +filter_size]. Outside of this window michael@0: // the value of the function is 0. Inside of the window, the value is sinus michael@0: // cardinal multiplied by a recentered Hamming function. The traditional michael@0: // Hamming formula for a window of size N and n ranging in [0, N-1] is: michael@0: // hamming(n) = 0.54 - 0.46 * cos(2 * pi * n / (N-1))) michael@0: // In our case we want the function centered for x == 0 and at its minimum michael@0: // on both ends of the window (x == +/- filter_size), hence the adjusted michael@0: // formula: michael@0: // hamming(x) = (0.54 - michael@0: // 0.46 * cos(2 * pi * (x - filter_size)/ (2 * filter_size))) michael@0: // = 0.54 - 0.46 * cos(pi * x / filter_size - pi) michael@0: // = 0.54 + 0.46 * cos(pi * x / filter_size) michael@0: float EvalHamming(int filter_size, float x) { michael@0: if (x <= -filter_size || x >= filter_size) michael@0: return 0.0f; // Outside of the window. michael@0: if (x > -std::numeric_limits::epsilon() && michael@0: x < std::numeric_limits::epsilon()) michael@0: return 1.0f; // Special case the sinc discontinuity at the origin. michael@0: const float xpi = x * static_cast(M_PI); michael@0: michael@0: return ((sin(xpi) / xpi) * // sinc(x) michael@0: (0.54f + 0.46f * cos(xpi / filter_size))); // hamming(x) michael@0: } michael@0: michael@0: // ResizeFilter ---------------------------------------------------------------- michael@0: michael@0: // Encapsulates computation and storage of the filters required for one complete michael@0: // resize operation. michael@0: class ResizeFilter { michael@0: public: michael@0: ResizeFilter(ImageOperations::ResizeMethod method, michael@0: int src_full_width, int src_full_height, michael@0: int dest_width, int dest_height, michael@0: const SkIRect& dest_subset); michael@0: michael@0: // Returns the filled filter values. michael@0: const ConvolutionFilter1D& x_filter() { return x_filter_; } michael@0: const ConvolutionFilter1D& y_filter() { return y_filter_; } michael@0: michael@0: private: michael@0: // Returns the number of pixels that the filer spans, in filter space (the michael@0: // destination image). michael@0: float GetFilterSupport(float scale) { michael@0: switch (method_) { michael@0: case ImageOperations::RESIZE_BOX: michael@0: // The box filter just scales with the image scaling. michael@0: return 0.5f; // Only want one side of the filter = /2. michael@0: case ImageOperations::RESIZE_HAMMING1: michael@0: // The Hamming filter takes as much space in the source image in michael@0: // each direction as the size of the window = 1 for Hamming1. michael@0: return 1.0f; michael@0: case ImageOperations::RESIZE_LANCZOS2: michael@0: // The Lanczos filter takes as much space in the source image in michael@0: // each direction as the size of the window = 2 for Lanczos2. michael@0: return 2.0f; michael@0: case ImageOperations::RESIZE_LANCZOS3: michael@0: // The Lanczos filter takes as much space in the source image in michael@0: // each direction as the size of the window = 3 for Lanczos3. michael@0: return 3.0f; michael@0: default: michael@0: return 1.0f; michael@0: } michael@0: } michael@0: michael@0: // Computes one set of filters either horizontally or vertically. The caller michael@0: // will specify the "min" and "max" rather than the bottom/top and michael@0: // right/bottom so that the same code can be re-used in each dimension. michael@0: // michael@0: // |src_depend_lo| and |src_depend_size| gives the range for the source michael@0: // depend rectangle (horizontally or vertically at the caller's discretion michael@0: // -- see above for what this means). michael@0: // michael@0: // Likewise, the range of destination values to compute and the scale factor michael@0: // for the transform is also specified. michael@0: void ComputeFilters(int src_size, michael@0: int dest_subset_lo, int dest_subset_size, michael@0: float scale, float src_support, michael@0: ConvolutionFilter1D* output); michael@0: michael@0: // Computes the filter value given the coordinate in filter space. michael@0: inline float ComputeFilter(float pos) { michael@0: switch (method_) { michael@0: case ImageOperations::RESIZE_BOX: michael@0: return EvalBox(pos); michael@0: case ImageOperations::RESIZE_HAMMING1: michael@0: return EvalHamming(1, pos); michael@0: case ImageOperations::RESIZE_LANCZOS2: michael@0: return EvalLanczos(2, pos); michael@0: case ImageOperations::RESIZE_LANCZOS3: michael@0: return EvalLanczos(3, pos); michael@0: default: michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: ImageOperations::ResizeMethod method_; michael@0: michael@0: // Size of the filter support on one side only in the destination space. michael@0: // See GetFilterSupport. michael@0: float x_filter_support_; michael@0: float y_filter_support_; michael@0: michael@0: // Subset of scaled destination bitmap to compute. michael@0: SkIRect out_bounds_; michael@0: michael@0: ConvolutionFilter1D x_filter_; michael@0: ConvolutionFilter1D y_filter_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ResizeFilter); michael@0: }; michael@0: michael@0: ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, michael@0: int src_full_width, int src_full_height, michael@0: int dest_width, int dest_height, michael@0: const SkIRect& dest_subset) michael@0: : method_(method), michael@0: out_bounds_(dest_subset) { michael@0: // method_ will only ever refer to an "algorithm method". michael@0: SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) && michael@0: (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); michael@0: michael@0: float scale_x = static_cast(dest_width) / michael@0: static_cast(src_full_width); michael@0: float scale_y = static_cast(dest_height) / michael@0: static_cast(src_full_height); michael@0: michael@0: x_filter_support_ = GetFilterSupport(scale_x); michael@0: y_filter_support_ = GetFilterSupport(scale_y); michael@0: michael@0: // Support of the filter in source space. michael@0: float src_x_support = x_filter_support_ / scale_x; michael@0: float src_y_support = y_filter_support_ / scale_y; michael@0: michael@0: ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(), michael@0: scale_x, src_x_support, &x_filter_); michael@0: ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(), michael@0: scale_y, src_y_support, &y_filter_); michael@0: } michael@0: michael@0: // TODO(egouriou): Take advantage of periods in the convolution. michael@0: // Practical resizing filters are periodic outside of the border area. michael@0: // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the michael@0: // source become p pixels in the destination) will have a period of p. michael@0: // A nice consequence is a period of 1 when downscaling by an integral michael@0: // factor. Downscaling from typical display resolutions is also bound michael@0: // to produce interesting periods as those are chosen to have multiple michael@0: // small factors. michael@0: // Small periods reduce computational load and improve cache usage if michael@0: // the coefficients can be shared. For periods of 1 we can consider michael@0: // loading the factors only once outside the borders. michael@0: void ResizeFilter::ComputeFilters(int src_size, michael@0: int dest_subset_lo, int dest_subset_size, michael@0: float scale, float src_support, michael@0: ConvolutionFilter1D* output) { michael@0: int dest_subset_hi = dest_subset_lo + dest_subset_size; // [lo, hi) michael@0: michael@0: // When we're doing a magnification, the scale will be larger than one. This michael@0: // means the destination pixels are much smaller than the source pixels, and michael@0: // that the range covered by the filter won't necessarily cover any source michael@0: // pixel boundaries. Therefore, we use these clamped values (max of 1) for michael@0: // some computations. michael@0: float clamped_scale = std::min(1.0f, scale); michael@0: michael@0: // Speed up the divisions below by turning them into multiplies. michael@0: float inv_scale = 1.0f / scale; michael@0: michael@0: StackVector filter_values; michael@0: StackVector fixed_filter_values; michael@0: michael@0: // Loop over all pixels in the output range. We will generate one set of michael@0: // filter values for each one. Those values will tell us how to blend the michael@0: // source pixels to compute the destination pixel. michael@0: for (int dest_subset_i = dest_subset_lo; dest_subset_i < dest_subset_hi; michael@0: dest_subset_i++) { michael@0: // Reset the arrays. We don't declare them inside so they can re-use the michael@0: // same malloc-ed buffer. michael@0: filter_values->clear(); michael@0: fixed_filter_values->clear(); michael@0: michael@0: // This is the pixel in the source directly under the pixel in the dest. michael@0: // Note that we base computations on the "center" of the pixels. To see michael@0: // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x michael@0: // downscale should "cover" the pixels around the pixel with *its center* michael@0: // at coordinates (2.5, 2.5) in the source, not those around (0, 0). michael@0: // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). michael@0: float src_pixel = (static_cast(dest_subset_i) + 0.5f) * inv_scale; michael@0: michael@0: // Compute the (inclusive) range of source pixels the filter covers. michael@0: int src_begin = std::max(0, FloorInt(src_pixel - src_support)); michael@0: int src_end = std::min(src_size - 1, CeilInt(src_pixel + src_support)); michael@0: michael@0: // Compute the unnormalized filter value at each location of the source michael@0: // it covers. michael@0: float filter_sum = 0.0f; // Sub of the filter values for normalizing. michael@0: for (int cur_filter_pixel = src_begin; cur_filter_pixel <= src_end; michael@0: cur_filter_pixel++) { michael@0: // Distance from the center of the filter, this is the filter coordinate michael@0: // in source space. We also need to consider the center of the pixel michael@0: // when comparing distance against 'src_pixel'. In the 5x downscale michael@0: // example used above the distance from the center of the filter to michael@0: // the pixel with coordinates (2, 2) should be 0, because its center michael@0: // is at (2.5, 2.5). michael@0: float src_filter_dist = michael@0: ((static_cast(cur_filter_pixel) + 0.5f) - src_pixel); michael@0: michael@0: // Since the filter really exists in dest space, map it there. michael@0: float dest_filter_dist = src_filter_dist * clamped_scale; michael@0: michael@0: // Compute the filter value at that location. michael@0: float filter_value = ComputeFilter(dest_filter_dist); michael@0: filter_values->push_back(filter_value); michael@0: michael@0: filter_sum += filter_value; michael@0: } michael@0: michael@0: // The filter must be normalized so that we don't affect the brightness of michael@0: // the image. Convert to normalized fixed point. michael@0: int16_t fixed_sum = 0; michael@0: for (size_t i = 0; i < filter_values->size(); i++) { michael@0: int16_t cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum); michael@0: fixed_sum += cur_fixed; michael@0: fixed_filter_values->push_back(cur_fixed); michael@0: } michael@0: michael@0: // The conversion to fixed point will leave some rounding errors, which michael@0: // we add back in to avoid affecting the brightness of the image. We michael@0: // arbitrarily add this to the center of the filter array (this won't always michael@0: // be the center of the filter function since it could get clipped on the michael@0: // edges, but it doesn't matter enough to worry about that case). michael@0: int16_t leftovers = output->FloatToFixed(1.0f) - fixed_sum; michael@0: fixed_filter_values[fixed_filter_values->size() / 2] += leftovers; michael@0: michael@0: // Now it's ready to go. michael@0: output->AddFilter(src_begin, &fixed_filter_values[0], michael@0: static_cast(fixed_filter_values->size())); michael@0: } michael@0: michael@0: output->PaddingForSIMD(8); michael@0: } michael@0: michael@0: ImageOperations::ResizeMethod ResizeMethodToAlgorithmMethod( michael@0: ImageOperations::ResizeMethod method) { michael@0: // Convert any "Quality Method" into an "Algorithm Method" michael@0: if (method >= ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD && michael@0: method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD) { michael@0: return method; michael@0: } michael@0: // The call to ImageOperationsGtv::Resize() above took care of michael@0: // GPU-acceleration in the cases where it is possible. So now we just michael@0: // pick the appropriate software method for each resize quality. michael@0: switch (method) { michael@0: // Users of RESIZE_GOOD are willing to trade a lot of quality to michael@0: // get speed, allowing the use of linear resampling to get hardware michael@0: // acceleration (SRB). Hence any of our "good" software filters michael@0: // will be acceptable, and we use the fastest one, Hamming-1. michael@0: case ImageOperations::RESIZE_GOOD: michael@0: // Users of RESIZE_BETTER are willing to trade some quality in order michael@0: // to improve performance, but are guaranteed not to devolve to a linear michael@0: // resampling. In visual tests we see that Hamming-1 is not as good as michael@0: // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is michael@0: // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed michael@0: // an acceptable trade-off between quality and speed. michael@0: case ImageOperations::RESIZE_BETTER: michael@0: return ImageOperations::RESIZE_HAMMING1; michael@0: default: michael@0: return ImageOperations::RESIZE_LANCZOS3; michael@0: } michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: // Resize ---------------------------------------------------------------------- michael@0: michael@0: // static michael@0: SkBitmap ImageOperations::Resize(const SkBitmap& source, michael@0: ResizeMethod method, michael@0: int dest_width, int dest_height, michael@0: const SkIRect& dest_subset, michael@0: void* dest_pixels /* = nullptr */) { michael@0: if (method == ImageOperations::RESIZE_SUBPIXEL) michael@0: return ResizeSubpixel(source, dest_width, dest_height, dest_subset); michael@0: else michael@0: return ResizeBasic(source, method, dest_width, dest_height, dest_subset, michael@0: dest_pixels); michael@0: } michael@0: michael@0: // static michael@0: SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source, michael@0: int dest_width, int dest_height, michael@0: const SkIRect& dest_subset) { michael@0: // Currently only works on Linux/BSD because these are the only platforms michael@0: // where SkFontHost::GetSubpixelOrder is defined. michael@0: #if defined(XP_UNIX) michael@0: // Understand the display. michael@0: const SkFontHost::LCDOrder order = SkFontHost::GetSubpixelOrder(); michael@0: const SkFontHost::LCDOrientation orientation = michael@0: SkFontHost::GetSubpixelOrientation(); michael@0: michael@0: // Decide on which dimension, if any, to deploy subpixel rendering. michael@0: int w = 1; michael@0: int h = 1; michael@0: switch (orientation) { michael@0: case SkFontHost::kHorizontal_LCDOrientation: michael@0: w = dest_width < source.width() ? 3 : 1; michael@0: break; michael@0: case SkFontHost::kVertical_LCDOrientation: michael@0: h = dest_height < source.height() ? 3 : 1; michael@0: break; michael@0: } michael@0: michael@0: // Resize the image. michael@0: const int width = dest_width * w; michael@0: const int height = dest_height * h; michael@0: SkIRect subset = { dest_subset.fLeft, dest_subset.fTop, michael@0: dest_subset.fLeft + dest_subset.width() * w, michael@0: dest_subset.fTop + dest_subset.height() * h }; michael@0: SkBitmap img = ResizeBasic(source, ImageOperations::RESIZE_LANCZOS3, width, michael@0: height, subset); michael@0: const int row_words = img.rowBytes() / 4; michael@0: if (w == 1 && h == 1) michael@0: return img; michael@0: michael@0: // Render into subpixels. michael@0: SkBitmap result; michael@0: result.setConfig(SkBitmap::kARGB_8888_Config, dest_subset.width(), michael@0: dest_subset.height()); michael@0: result.allocPixels(); michael@0: if (!result.readyToDraw()) michael@0: return img; michael@0: michael@0: SkAutoLockPixels locker(img); michael@0: if (!img.readyToDraw()) michael@0: return img; michael@0: michael@0: uint32_t* src_row = img.getAddr32(0, 0); michael@0: uint32_t* dst_row = result.getAddr32(0, 0); michael@0: for (int y = 0; y < dest_subset.height(); y++) { michael@0: uint32_t* src = src_row; michael@0: uint32_t* dst = dst_row; michael@0: for (int x = 0; x < dest_subset.width(); x++, src += w, dst++) { michael@0: uint8_t r = 0, g = 0, b = 0, a = 0; michael@0: switch (order) { michael@0: case SkFontHost::kRGB_LCDOrder: michael@0: switch (orientation) { michael@0: case SkFontHost::kHorizontal_LCDOrientation: michael@0: r = SkGetPackedR32(src[0]); michael@0: g = SkGetPackedG32(src[1]); michael@0: b = SkGetPackedB32(src[2]); michael@0: a = SkGetPackedA32(src[1]); michael@0: break; michael@0: case SkFontHost::kVertical_LCDOrientation: michael@0: r = SkGetPackedR32(src[0 * row_words]); michael@0: g = SkGetPackedG32(src[1 * row_words]); michael@0: b = SkGetPackedB32(src[2 * row_words]); michael@0: a = SkGetPackedA32(src[1 * row_words]); michael@0: break; michael@0: } michael@0: break; michael@0: case SkFontHost::kBGR_LCDOrder: michael@0: switch (orientation) { michael@0: case SkFontHost::kHorizontal_LCDOrientation: michael@0: b = SkGetPackedB32(src[0]); michael@0: g = SkGetPackedG32(src[1]); michael@0: r = SkGetPackedR32(src[2]); michael@0: a = SkGetPackedA32(src[1]); michael@0: break; michael@0: case SkFontHost::kVertical_LCDOrientation: michael@0: b = SkGetPackedB32(src[0 * row_words]); michael@0: g = SkGetPackedG32(src[1 * row_words]); michael@0: r = SkGetPackedR32(src[2 * row_words]); michael@0: a = SkGetPackedA32(src[1 * row_words]); michael@0: break; michael@0: } michael@0: break; michael@0: case SkFontHost::kNONE_LCDOrder: michael@0: break; michael@0: } michael@0: // Premultiplied alpha is very fragile. michael@0: a = a > r ? a : r; michael@0: a = a > g ? a : g; michael@0: a = a > b ? a : b; michael@0: *dst = SkPackARGB32(a, r, g, b); michael@0: } michael@0: src_row += h * row_words; michael@0: dst_row += result.rowBytes() / 4; michael@0: } michael@0: result.setAlphaType(img.alphaType()); michael@0: return result; michael@0: #else michael@0: return SkBitmap(); michael@0: #endif // OS_POSIX && !OS_MACOSX && !defined(OS_ANDROID) michael@0: } michael@0: michael@0: // static michael@0: SkBitmap ImageOperations::ResizeBasic(const SkBitmap& source, michael@0: ResizeMethod method, michael@0: int dest_width, int dest_height, michael@0: const SkIRect& dest_subset, michael@0: void* dest_pixels /* = nullptr */) { michael@0: // Ensure that the ResizeMethod enumeration is sound. michael@0: SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && michael@0: (method <= RESIZE_LAST_QUALITY_METHOD)) || michael@0: ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && michael@0: (method <= RESIZE_LAST_ALGORITHM_METHOD))); michael@0: michael@0: // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just michael@0: // return empty. michael@0: if (source.width() < 1 || source.height() < 1 || michael@0: dest_width < 1 || dest_height < 1) michael@0: return SkBitmap(); michael@0: michael@0: method = ResizeMethodToAlgorithmMethod(method); michael@0: // Check that we deal with an "algorithm methods" from this point onward. michael@0: SkASSERT((ImageOperations::RESIZE_FIRST_ALGORITHM_METHOD <= method) && michael@0: (method <= ImageOperations::RESIZE_LAST_ALGORITHM_METHOD)); michael@0: michael@0: SkAutoLockPixels locker(source); michael@0: if (!source.readyToDraw()) michael@0: return SkBitmap(); michael@0: michael@0: ResizeFilter filter(method, source.width(), source.height(), michael@0: dest_width, dest_height, dest_subset); michael@0: michael@0: // Get a source bitmap encompassing this touched area. We construct the michael@0: // offsets and row strides such that it looks like a new bitmap, while michael@0: // referring to the old data. michael@0: const uint8_t* source_subset = michael@0: reinterpret_cast(source.getPixels()); michael@0: michael@0: // Convolve into the result. michael@0: SkBitmap result; michael@0: result.setConfig(SkBitmap::kARGB_8888_Config, michael@0: dest_subset.width(), dest_subset.height()); michael@0: michael@0: if (dest_pixels) { michael@0: result.setPixels(dest_pixels); michael@0: } else { michael@0: result.allocPixels(); michael@0: } michael@0: michael@0: if (!result.readyToDraw()) michael@0: return SkBitmap(); michael@0: michael@0: BGRAConvolve2D(source_subset, static_cast(source.rowBytes()), michael@0: !source.isOpaque(), filter.x_filter(), filter.y_filter(), michael@0: static_cast(result.rowBytes()), michael@0: static_cast(result.getPixels()), michael@0: /* sse = */ false); michael@0: michael@0: // Preserve the "opaque" flag for use as an optimization later. michael@0: result.setAlphaType(source.alphaType()); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: // static michael@0: SkBitmap ImageOperations::Resize(const SkBitmap& source, michael@0: ResizeMethod method, michael@0: int dest_width, int dest_height, michael@0: void* dest_pixels /* = nullptr */) { michael@0: SkIRect dest_subset = { 0, 0, dest_width, dest_height }; michael@0: return Resize(source, method, dest_width, dest_height, dest_subset, michael@0: dest_pixels); michael@0: } michael@0: michael@0: } // namespace skia