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