gfx/2d/image_operations.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial