OLD | NEW |
(Empty) | |
| 1 #include "SkBitmapScaler.h" |
| 2 #include "SkBitmapFilter.h" |
| 3 #include "SkRect.h" |
| 4 #include "SkTArray.h" |
| 5 #include "SkErrorInternals.h" |
| 6 |
| 7 // SkResizeFilter --------------------------------------------------------------
-- |
| 8 |
| 9 // Encapsulates computation and storage of the filters required for one complete |
| 10 // resize operation. |
| 11 class SkResizeFilter { |
| 12 public: |
| 13 SkResizeFilter(SkBitmapScaler::ResizeMethod method, |
| 14 int srcFullWidth, int srcFullHeight, |
| 15 int destWidth, int destHeight, |
| 16 const SkIRect& destSubset); |
| 17 ~SkResizeFilter() { |
| 18 SkDELETE( fBitmapFilter ); |
| 19 } |
| 20 |
| 21 // Returns the filled filter values. |
| 22 const SkConvolutionFilter1D& xFilter() { return fXFilter; } |
| 23 const SkConvolutionFilter1D& yFilter() { return fYFilter; } |
| 24 |
| 25 private: |
| 26 |
| 27 SkBitmapFilter* fBitmapFilter; |
| 28 |
| 29 // Computes one set of filters either horizontally or vertically. The caller |
| 30 // will specify the "min" and "max" rather than the bottom/top and |
| 31 // right/bottom so that the same code can be re-used in each dimension. |
| 32 // |
| 33 // |srcDependLo| and |srcDependSize| gives the range for the source |
| 34 // depend rectangle (horizontally or vertically at the caller's discretion |
| 35 // -- see above for what this means). |
| 36 // |
| 37 // Likewise, the range of destination values to compute and the scale factor |
| 38 // for the transform is also specified. |
| 39 |
| 40 void computeFilters(int srcSize, |
| 41 int destSubsetLo, int destSubsetSize, |
| 42 float scale, |
| 43 SkConvolutionFilter1D* output); |
| 44 |
| 45 // Subset of scaled destination bitmap to compute. |
| 46 SkIRect fOutBounds; |
| 47 |
| 48 SkConvolutionFilter1D fXFilter; |
| 49 SkConvolutionFilter1D fYFilter; |
| 50 }; |
| 51 |
| 52 SkResizeFilter::SkResizeFilter(SkBitmapScaler::ResizeMethod method, |
| 53 int srcFullWidth, int srcFullHeight, |
| 54 int destWidth, int destHeight, |
| 55 const SkIRect& destSubset) |
| 56 : fOutBounds(destSubset) { |
| 57 |
| 58 // method will only ever refer to an "algorithm method". |
| 59 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
| 60 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); |
| 61 |
| 62 switch(method) { |
| 63 case SkBitmapScaler::RESIZE_BOX: |
| 64 fBitmapFilter = SkNEW(SkBoxFilter); |
| 65 break; |
| 66 case SkBitmapScaler::RESIZE_TRIANGLE: |
| 67 fBitmapFilter = SkNEW(SkTriangleFilter); |
| 68 break; |
| 69 case SkBitmapScaler::RESIZE_MITCHELL: |
| 70 fBitmapFilter = SkNEW_ARGS(SkMitchellFilter, (1.f/3.f, 1.f/3.f)); |
| 71 break; |
| 72 case SkBitmapScaler::RESIZE_HAMMING: |
| 73 fBitmapFilter = SkNEW(SkHammingFilter); |
| 74 break; |
| 75 case SkBitmapScaler::RESIZE_LANCZOS3: |
| 76 fBitmapFilter = SkNEW(SkLanczosFilter); |
| 77 break; |
| 78 default: |
| 79 // NOTREACHED: |
| 80 fBitmapFilter = SkNEW_ARGS(SkMitchellFilter, (1.f/3.f, 1.f/3.f)); |
| 81 break; |
| 82 } |
| 83 |
| 84 |
| 85 float scaleX = static_cast<float>(destWidth) / |
| 86 static_cast<float>(srcFullWidth); |
| 87 float scaleY = static_cast<float>(destHeight) / |
| 88 static_cast<float>(srcFullHeight); |
| 89 |
| 90 this->computeFilters(srcFullWidth, destSubset.fLeft, destSubset.width(), |
| 91 scaleX, &fXFilter); |
| 92 this->computeFilters(srcFullHeight, destSubset.fTop, destSubset.height(), |
| 93 scaleY, &fYFilter); |
| 94 } |
| 95 |
| 96 // TODO(egouriou): Take advantage of periods in the convolution. |
| 97 // Practical resizing filters are periodic outside of the border area. |
| 98 // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the |
| 99 // source become p pixels in the destination) will have a period of p. |
| 100 // A nice consequence is a period of 1 when downscaling by an integral |
| 101 // factor. Downscaling from typical display resolutions is also bound |
| 102 // to produce interesting periods as those are chosen to have multiple |
| 103 // small factors. |
| 104 // Small periods reduce computational load and improve cache usage if |
| 105 // the coefficients can be shared. For periods of 1 we can consider |
| 106 // loading the factors only once outside the borders. |
| 107 void SkResizeFilter::computeFilters(int srcSize, |
| 108 int destSubsetLo, int destSubsetSize, |
| 109 float scale, |
| 110 SkConvolutionFilter1D* output) { |
| 111 int destSubsetHi = destSubsetLo + destSubsetSize; // [lo, hi) |
| 112 |
| 113 // When we're doing a magnification, the scale will be larger than one. This |
| 114 // means the destination pixels are much smaller than the source pixels, and |
| 115 // that the range covered by the filter won't necessarily cover any source |
| 116 // pixel boundaries. Therefore, we use these clamped values (max of 1) for |
| 117 // some computations. |
| 118 float clampedScale = SkTMin(1.0f, scale); |
| 119 |
| 120 // This is how many source pixels from the center we need to count |
| 121 // to support the filtering function. |
| 122 float srcSupport = fBitmapFilter->width() / clampedScale; |
| 123 |
| 124 // Speed up the divisions below by turning them into multiplies. |
| 125 float invScale = 1.0f / scale; |
| 126 |
| 127 SkTArray<float> filterValues(64); |
| 128 SkTArray<short> fixedFilterValues(64); |
| 129 |
| 130 // Loop over all pixels in the output range. We will generate one set of |
| 131 // filter values for each one. Those values will tell us how to blend the |
| 132 // source pixels to compute the destination pixel. |
| 133 for (int destSubsetI = destSubsetLo; destSubsetI < destSubsetHi; |
| 134 destSubsetI++) { |
| 135 // Reset the arrays. We don't declare them inside so they can re-use the |
| 136 // same malloc-ed buffer. |
| 137 filterValues.reset(); |
| 138 fixedFilterValues.reset(); |
| 139 |
| 140 // This is the pixel in the source directly under the pixel in the dest. |
| 141 // Note that we base computations on the "center" of the pixels. To see |
| 142 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x |
| 143 // downscale should "cover" the pixels around the pixel with *its center* |
| 144 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). |
| 145 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). |
| 146 float srcPixel = (static_cast<float>(destSubsetI) + 0.5f) * invScale; |
| 147 |
| 148 // Compute the (inclusive) range of source pixels the filter covers. |
| 149 int srcBegin = SkTMax(0, SkScalarFloorToInt(srcPixel - srcSupport)); |
| 150 int srcEnd = SkTMin(srcSize - 1, SkScalarCeilToInt(srcPixel + srcSupport)); |
| 151 |
| 152 // Compute the unnormalized filter value at each location of the source |
| 153 // it covers. |
| 154 float filterSum = 0.0f; // Sub of the filter values for normalizing. |
| 155 for (int curFilterPixel = srcBegin; curFilterPixel <= srcEnd; |
| 156 curFilterPixel++) { |
| 157 // Distance from the center of the filter, this is the filter coordinate |
| 158 // in source space. We also need to consider the center of the pixel |
| 159 // when comparing distance against 'srcPixel'. In the 5x downscale |
| 160 // example used above the distance from the center of the filter to |
| 161 // the pixel with coordinates (2, 2) should be 0, because its center |
| 162 // is at (2.5, 2.5). |
| 163 float srcFilterDist = |
| 164 ((static_cast<float>(curFilterPixel) + 0.5f) - srcPixel); |
| 165 |
| 166 // Since the filter really exists in dest space, map it there. |
| 167 float destFilterDist = srcFilterDist * clampedScale; |
| 168 |
| 169 // Compute the filter value at that location. |
| 170 float filterValue = fBitmapFilter->evaluate(destFilterDist); |
| 171 filterValues.push_back(filterValue); |
| 172 |
| 173 filterSum += filterValue; |
| 174 } |
| 175 SkASSERT(!filterValues.empty()); |
| 176 |
| 177 // The filter must be normalized so that we don't affect the brightness of |
| 178 // the image. Convert to normalized fixed point. |
| 179 short fixedSum = 0; |
| 180 for (int i = 0; i < filterValues.count(); i++) { |
| 181 short curFixed = output->FloatToFixed(filterValues[i] / filterSum); |
| 182 fixedSum += curFixed; |
| 183 fixedFilterValues.push_back(curFixed); |
| 184 } |
| 185 |
| 186 // The conversion to fixed point will leave some rounding errors, which |
| 187 // we add back in to avoid affecting the brightness of the image. We |
| 188 // arbitrarily add this to the center of the filter array (this won't always |
| 189 // be the center of the filter function since it could get clipped on the |
| 190 // edges, but it doesn't matter enough to worry about that case). |
| 191 short leftovers = output->FloatToFixed(1.0f) - fixedSum; |
| 192 fixedFilterValues[fixedFilterValues.count() / 2] += leftovers; |
| 193 |
| 194 // Now it's ready to go. |
| 195 output->AddFilter(srcBegin, &fixedFilterValues[0], |
| 196 static_cast<int>(fixedFilterValues.count())); |
| 197 } |
| 198 |
| 199 output->PaddingForSIMD(); |
| 200 } |
| 201 |
| 202 static SkBitmapScaler::ResizeMethod ResizeMethodToAlgorithmMethod( |
| 203 SkBitmapScaler::ResizeMethod method) { |
| 204 // Convert any "Quality Method" into an "Algorithm Method" |
| 205 if (method >= SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD && |
| 206 method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD) { |
| 207 return method; |
| 208 } |
| 209 // The call to SkBitmapScalerGtv::Resize() above took care of |
| 210 // GPU-acceleration in the cases where it is possible. So now we just |
| 211 // pick the appropriate software method for each resize quality. |
| 212 switch (method) { |
| 213 // Users of RESIZE_GOOD are willing to trade a lot of quality to |
| 214 // get speed, allowing the use of linear resampling to get hardware |
| 215 // acceleration (SRB). Hence any of our "good" software filters |
| 216 // will be acceptable, so we use a triangle. |
| 217 case SkBitmapScaler::RESIZE_GOOD: |
| 218 return SkBitmapScaler::RESIZE_TRIANGLE; |
| 219 // Users of RESIZE_BETTER are willing to trade some quality in order |
| 220 // to improve performance, but are guaranteed not to devolve to a linear |
| 221 // resampling. In visual tests we see that Hamming-1 is not as good as |
| 222 // Lanczos-2, however it is about 40% faster and Lanczos-2 itself is |
| 223 // about 30% faster than Lanczos-3. The use of Hamming-1 has been deemed |
| 224 // an acceptable trade-off between quality and speed. |
| 225 case SkBitmapScaler::RESIZE_BETTER: |
| 226 return SkBitmapScaler::RESIZE_HAMMING; |
| 227 default: |
| 228 return SkBitmapScaler::RESIZE_MITCHELL; |
| 229 } |
| 230 } |
| 231 |
| 232 // static |
| 233 SkBitmap SkBitmapScaler::Resize(const SkBitmap& source, |
| 234 ResizeMethod method, |
| 235 int destWidth, int destHeight, |
| 236 const SkIRect& destSubset, |
| 237 SkConvolutionProcs *convolveProcs, |
| 238 SkBitmap::Allocator* allocator) { |
| 239 // Ensure that the ResizeMethod enumeration is sound. |
| 240 SkASSERT(((RESIZE_FIRST_QUALITY_METHOD <= method) && |
| 241 (method <= RESIZE_LAST_QUALITY_METHOD)) || |
| 242 ((RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
| 243 (method <= RESIZE_LAST_ALGORITHM_METHOD))); |
| 244 |
| 245 SkIRect dest = { 0, 0, destWidth, destHeight }; |
| 246 if (!dest.contains(destSubset)) { |
| 247 SkErrorInternals::SetError( kInvalidArgument_SkError, |
| 248 "Sorry, you passed me a bitmap resize " |
| 249 " method I have never heard of: %d", |
| 250 method ); |
| 251 } |
| 252 |
| 253 // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just |
| 254 // return empty. |
| 255 if (source.width() < 1 || source.height() < 1 || |
| 256 destWidth < 1 || destHeight < 1) { |
| 257 return SkBitmap(); |
| 258 } |
| 259 |
| 260 method = ResizeMethodToAlgorithmMethod(method); |
| 261 |
| 262 // Check that we deal with an "algorithm methods" from this point onward. |
| 263 SkASSERT((SkBitmapScaler::RESIZE_FIRST_ALGORITHM_METHOD <= method) && |
| 264 (method <= SkBitmapScaler::RESIZE_LAST_ALGORITHM_METHOD)); |
| 265 |
| 266 SkAutoLockPixels locker(source); |
| 267 if (!source.readyToDraw() || source.config() != SkBitmap::kARGB_8888_Config) |
| 268 return SkBitmap(); |
| 269 |
| 270 SkResizeFilter filter(method, source.width(), source.height(), |
| 271 destWidth, destHeight, destSubset); |
| 272 |
| 273 // Get a source bitmap encompassing this touched area. We construct the |
| 274 // offsets and row strides such that it looks like a new bitmap, while |
| 275 // referring to the old data. |
| 276 const unsigned char* sourceSubset = |
| 277 reinterpret_cast<const unsigned char*>(source.getPixels()); |
| 278 |
| 279 // Convolve into the result. |
| 280 SkBitmap result; |
| 281 result.setConfig(SkBitmap::kARGB_8888_Config, |
| 282 destSubset.width(), destSubset.height()); |
| 283 result.allocPixels(allocator, NULL); |
| 284 if (!result.readyToDraw()) |
| 285 return SkBitmap(); |
| 286 |
| 287 BGRAConvolve2D(sourceSubset, static_cast<int>(source.rowBytes()), |
| 288 !source.isOpaque(), filter.xFilter(), filter.yFilter(), |
| 289 static_cast<int>(result.rowBytes()), |
| 290 static_cast<unsigned char*>(result.getPixels()), |
| 291 convolveProcs, true); |
| 292 |
| 293 // Preserve the "opaque" flag for use as an optimization later. |
| 294 result.setIsOpaque(source.isOpaque()); |
| 295 |
| 296 return result; |
| 297 } |
| 298 |
| 299 // static |
| 300 SkBitmap SkBitmapScaler::Resize(const SkBitmap& source, |
| 301 ResizeMethod method, |
| 302 int destWidth, int destHeight, |
| 303 SkConvolutionProcs* convolveProcs, |
| 304 SkBitmap::Allocator* allocator) { |
| 305 SkIRect destSubset = { 0, 0, destWidth, destHeight }; |
| 306 return Resize(source, method, destWidth, destHeight, destSubset, |
| 307 convolveProcs, allocator); |
| 308 } |
OLD | NEW |