| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapScaler.h" | 8 #include "SkBitmapScaler.h" |
| 9 #include "SkBitmapFilter.h" | 9 #include "SkBitmapFilter.h" |
| 10 #include "SkConvolver.h" | 10 #include "SkConvolver.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // Loop over all pixels in the output range. We will generate one set of | 137 // Loop over all pixels in the output range. We will generate one set of |
| 138 // filter values for each one. Those values will tell us how to blend the | 138 // filter values for each one. Those values will tell us how to blend the |
| 139 // source pixels to compute the destination pixel. | 139 // source pixels to compute the destination pixel. |
| 140 | 140 |
| 141 // This is the pixel in the source directly under the pixel in the dest. | 141 // This is the pixel in the source directly under the pixel in the dest. |
| 142 // Note that we base computations on the "center" of the pixels. To see | 142 // Note that we base computations on the "center" of the pixels. To see |
| 143 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x | 143 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x |
| 144 // downscale should "cover" the pixels around the pixel with *its center* | 144 // downscale should "cover" the pixels around the pixel with *its center* |
| 145 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). | 145 // at coordinates (2.5, 2.5) in the source, not those around (0, 0). |
| 146 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). | 146 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0). |
| 147 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 148 int destLimit = SkScalarTruncToInt(SkScalarCeilToScalar(destSubsetHi) | |
| 149 - SkScalarFloorToScalar(destSubsetLo)); | |
| 150 #else | |
| 151 destSubsetLo = SkScalarFloorToScalar(destSubsetLo); | 147 destSubsetLo = SkScalarFloorToScalar(destSubsetLo); |
| 152 destSubsetHi = SkScalarCeilToScalar(destSubsetHi); | 148 destSubsetHi = SkScalarCeilToScalar(destSubsetHi); |
| 153 float srcPixel = (destSubsetLo + 0.5f) * invScale; | 149 float srcPixel = (destSubsetLo + 0.5f) * invScale; |
| 154 int destLimit = SkScalarTruncToInt(destSubsetHi - destSubsetLo); | 150 int destLimit = SkScalarTruncToInt(destSubsetHi - destSubsetLo); |
| 155 #endif | |
| 156 output->reserveAdditional(destLimit, SkScalarCeilToInt(destLimit * srcSupport
* 2)); | 151 output->reserveAdditional(destLimit, SkScalarCeilToInt(destLimit * srcSupport
* 2)); |
| 157 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 158 for (int destSubsetI = SkScalarFloorToInt(destSubsetLo); destSubsetI < SkScala
rCeilToInt(destSubsetHi); | |
| 159 destSubsetI++) | |
| 160 #else | |
| 161 for (int destI = 0; destI < destLimit; srcPixel += invScale, destI++) | 152 for (int destI = 0; destI < destLimit; srcPixel += invScale, destI++) |
| 162 #endif | |
| 163 { | 153 { |
| 164 // Compute the (inclusive) range of source pixels the filter covers. | 154 // Compute the (inclusive) range of source pixels the filter covers. |
| 165 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 166 float srcPixel = (static_cast<float>(destSubsetI) + 0.5f) * invScale; | |
| 167 int srcBegin = SkTMax(0, SkScalarFloorToInt(srcPixel - srcSupport)); | |
| 168 int srcEnd = SkTMin(srcSize - 1, SkScalarCeilToInt(srcPixel + srcSupport)); | |
| 169 #else | |
| 170 float srcBegin = SkTMax(0.f, SkScalarFloorToScalar(srcPixel - srcSupport)); | 155 float srcBegin = SkTMax(0.f, SkScalarFloorToScalar(srcPixel - srcSupport)); |
| 171 float srcEnd = SkTMin(srcSize - 1.f, SkScalarCeilToScalar(srcPixel + srcSupp
ort)); | 156 float srcEnd = SkTMin(srcSize - 1.f, SkScalarCeilToScalar(srcPixel + srcSupp
ort)); |
| 172 #endif | |
| 173 | 157 |
| 174 // Compute the unnormalized filter value at each location of the source | 158 // Compute the unnormalized filter value at each location of the source |
| 175 // it covers. | 159 // it covers. |
| 176 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | 160 |
| 177 float filterSum = 0.0f; // Sub of the filter values for normalizing. | |
| 178 int filterCount = srcEnd - srcBegin + 1; | |
| 179 filterValuesArray.reset(filterCount); | |
| 180 for (int curFilterPixel = srcBegin; curFilterPixel <= srcEnd; | |
| 181 curFilterPixel++) { | |
| 182 #endif | |
| 183 // Sum of the filter values for normalizing. | 161 // Sum of the filter values for normalizing. |
| 184 // Distance from the center of the filter, this is the filter coordinate | 162 // Distance from the center of the filter, this is the filter coordinate |
| 185 // in source space. We also need to consider the center of the pixel | 163 // in source space. We also need to consider the center of the pixel |
| 186 // when comparing distance against 'srcPixel'. In the 5x downscale | 164 // when comparing distance against 'srcPixel'. In the 5x downscale |
| 187 // example used above the distance from the center of the filter to | 165 // example used above the distance from the center of the filter to |
| 188 // the pixel with coordinates (2, 2) should be 0, because its center | 166 // the pixel with coordinates (2, 2) should be 0, because its center |
| 189 // is at (2.5, 2.5). | 167 // is at (2.5, 2.5). |
| 190 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 191 float srcFilterDist = | |
| 192 ((static_cast<float>(curFilterPixel) + 0.5f) - srcPixel); | |
| 193 | |
| 194 // Since the filter really exists in dest space, map it there. | |
| 195 float destFilterDist = srcFilterDist * clampedScale; | |
| 196 | |
| 197 // Compute the filter value at that location. | |
| 198 float filterValue = fBitmapFilter->evaluate(destFilterDist); | |
| 199 filterValuesArray[curFilterPixel - srcBegin] = filterValue; | |
| 200 | |
| 201 filterSum += filterValue; | |
| 202 } | |
| 203 #else | |
| 204 float destFilterDist = (srcBegin + 0.5f - srcPixel) * clampedScale; | 168 float destFilterDist = (srcBegin + 0.5f - srcPixel) * clampedScale; |
| 205 int filterCount = SkScalarTruncToInt(srcEnd - srcBegin) + 1; | 169 int filterCount = SkScalarTruncToInt(srcEnd - srcBegin) + 1; |
| 206 SkASSERT(filterCount > 0); | 170 SkASSERT(filterCount > 0); |
| 207 filterValuesArray.reset(filterCount); | 171 filterValuesArray.reset(filterCount); |
| 208 float filterSum = fBitmapFilter->evaluate_n(destFilterDist, clampedScale, fi
lterCount, | 172 float filterSum = fBitmapFilter->evaluate_n(destFilterDist, clampedScale, fi
lterCount, |
| 209 filterValuesArray.begin()); | 173 filterValuesArray.begin()); |
| 210 #endif | 174 |
| 211 // The filter must be normalized so that we don't affect the brightness of | 175 // The filter must be normalized so that we don't affect the brightness of |
| 212 // the image. Convert to normalized fixed point. | 176 // the image. Convert to normalized fixed point. |
| 213 int fixedSum = 0; | 177 int fixedSum = 0; |
| 214 fixedFilterValuesArray.reset(filterCount); | 178 fixedFilterValuesArray.reset(filterCount); |
| 215 const float* filterValues = filterValuesArray.begin(); | 179 const float* filterValues = filterValuesArray.begin(); |
| 216 SkConvolutionFilter1D::ConvolutionFixed* fixedFilterValues = fixedFilterValu
esArray.begin(); | 180 SkConvolutionFilter1D::ConvolutionFixed* fixedFilterValues = fixedFilterValu
esArray.begin(); |
| 217 #ifndef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 218 float invFilterSum = 1 / filterSum; | 181 float invFilterSum = 1 / filterSum; |
| 219 #endif | |
| 220 for (int fixedI = 0; fixedI < filterCount; fixedI++) { | 182 for (int fixedI = 0; fixedI < filterCount; fixedI++) { |
| 221 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 222 int curFixed = SkConvolutionFilter1D::FloatToFixed(filterValues[fixedI] /
filterSum); | |
| 223 #else | |
| 224 int curFixed = SkConvolutionFilter1D::FloatToFixed(filterValues[fixedI] *
invFilterSum); | 183 int curFixed = SkConvolutionFilter1D::FloatToFixed(filterValues[fixedI] *
invFilterSum); |
| 225 #endif | |
| 226 fixedSum += curFixed; | 184 fixedSum += curFixed; |
| 227 fixedFilterValues[fixedI] = SkToS16(curFixed); | 185 fixedFilterValues[fixedI] = SkToS16(curFixed); |
| 228 } | 186 } |
| 229 SkASSERT(fixedSum <= 0x7FFF); | 187 SkASSERT(fixedSum <= 0x7FFF); |
| 230 | 188 |
| 231 // The conversion to fixed point will leave some rounding errors, which | 189 // The conversion to fixed point will leave some rounding errors, which |
| 232 // we add back in to avoid affecting the brightness of the image. We | 190 // we add back in to avoid affecting the brightness of the image. We |
| 233 // arbitrarily add this to the center of the filter array (this won't always | 191 // arbitrarily add this to the center of the filter array (this won't always |
| 234 // be the center of the filter function since it could get clipped on the | 192 // be the center of the filter function since it could get clipped on the |
| 235 // edges, but it doesn't matter enough to worry about that case). | 193 // edges, but it doesn't matter enough to worry about that case). |
| 236 int leftovers = SkConvolutionFilter1D::FloatToFixed(1) - fixedSum; | 194 int leftovers = SkConvolutionFilter1D::FloatToFixed(1) - fixedSum; |
| 237 fixedFilterValues[filterCount / 2] += leftovers; | 195 fixedFilterValues[filterCount / 2] += leftovers; |
| 238 | 196 |
| 239 // Now it's ready to go. | 197 // Now it's ready to go. |
| 240 #ifdef SK_SUPPORT_LEGACY_BITMAP_FILTER | |
| 241 output->AddFilter(srcBegin, fixedFilterValues, filterCount); | |
| 242 #else | |
| 243 output->AddFilter(SkScalarFloorToInt(srcBegin), fixedFilterValues, filterCou
nt); | 198 output->AddFilter(SkScalarFloorToInt(srcBegin), fixedFilterValues, filterCou
nt); |
| 244 #endif | |
| 245 } | 199 } |
| 246 | 200 |
| 247 if (convolveProcs.fApplySIMDPadding) { | 201 if (convolveProcs.fApplySIMDPadding) { |
| 248 convolveProcs.fApplySIMDPadding(output); | 202 convolveProcs.fApplySIMDPadding(output); |
| 249 } | 203 } |
| 250 } | 204 } |
| 251 | 205 |
| 252 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 206 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 253 | 207 |
| 254 static bool valid_for_resize(const SkPixmap& source, int dstW, int dstH) { | 208 static bool valid_for_resize(const SkPixmap& source, int dstW, int dstH) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (!result.peekPixels(&resultPM) || !Resize(resultPM, source, method)) { | 254 if (!result.peekPixels(&resultPM) || !Resize(resultPM, source, method)) { |
| 301 return false; | 255 return false; |
| 302 } | 256 } |
| 303 | 257 |
| 304 *resultPtr = result; | 258 *resultPtr = result; |
| 305 resultPtr->lockPixels(); | 259 resultPtr->lockPixels(); |
| 306 SkASSERT(resultPtr->getPixels()); | 260 SkASSERT(resultPtr->getPixels()); |
| 307 return true; | 261 return true; |
| 308 } | 262 } |
| 309 | 263 |
| OLD | NEW |