1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBitmapScaler.h"
9 #include "SkBitmapFilter.h"
10 #include "SkConvolver.h"
11 #include "SkImageInfo.h"
12 #include "SkPixmap.h"
13 #include "SkRect.h"
14 #include "SkTArray.h"
15
16 // SkResizeFilter ----------------------------------------------------------------
17
18 // Encapsulates computation and storage of the filters required for one complete
19 // resize operation.
20 class SkResizeFilter {
21 public:
22 SkResizeFilter(SkBitmapScaler::ResizeMethod method,
23 int srcFullWidth, int srcFullHeight,
24 float destWidth, float destHeight,
25 const SkRect& destSubset);
~SkResizeFilter()26 ~SkResizeFilter() { delete fBitmapFilter; }
27
28 // Returns the filled filter values.
xFilter()29 const SkConvolutionFilter1D& xFilter() { return fXFilter; }
yFilter()30 const SkConvolutionFilter1D& yFilter() { return fYFilter; }
31
32 private:
33
34 SkBitmapFilter* fBitmapFilter;
35
36 // Computes one set of filters either horizontally or vertically. The caller
37 // will specify the "min" and "max" rather than the bottom/top and
38 // right/bottom so that the same code can be re-used in each dimension.
39 //
40 // |srcDependLo| and |srcDependSize| gives the range for the source
41 // depend rectangle (horizontally or vertically at the caller's discretion
42 // -- see above for what this means).
43 //
44 // Likewise, the range of destination values to compute and the scale factor
45 // for the transform is also specified.
46
47 void computeFilters(int srcSize,
48 float destSubsetLo, float destSubsetSize,
49 float scale,
50 SkConvolutionFilter1D* output);
51
52 SkConvolutionFilter1D fXFilter;
53 SkConvolutionFilter1D fYFilter;
54 };
55
SkResizeFilter(SkBitmapScaler::ResizeMethod method,int srcFullWidth,int srcFullHeight,float destWidth,float destHeight,const SkRect & destSubset)56 SkResizeFilter::SkResizeFilter(SkBitmapScaler::ResizeMethod method,
57 int srcFullWidth, int srcFullHeight,
58 float destWidth, float destHeight,
59 const SkRect& destSubset) {
60
61 SkASSERT(method >= SkBitmapScaler::RESIZE_FirstMethod &&
62 method <= SkBitmapScaler::RESIZE_LastMethod);
63
64 fBitmapFilter = nullptr;
65 switch(method) {
66 case SkBitmapScaler::RESIZE_BOX:
67 fBitmapFilter = new SkBoxFilter;
68 break;
69 case SkBitmapScaler::RESIZE_TRIANGLE:
70 fBitmapFilter = new SkTriangleFilter;
71 break;
72 case SkBitmapScaler::RESIZE_MITCHELL:
73 fBitmapFilter = new SkMitchellFilter;
74 break;
75 case SkBitmapScaler::RESIZE_HAMMING:
76 fBitmapFilter = new SkHammingFilter;
77 break;
78 case SkBitmapScaler::RESIZE_LANCZOS3:
79 fBitmapFilter = new SkLanczosFilter;
80 break;
81 }
82
83
84 float scaleX = destWidth / srcFullWidth;
85 float scaleY = destHeight / srcFullHeight;
86
87 this->computeFilters(srcFullWidth, destSubset.fLeft, destSubset.width(),
88 scaleX, &fXFilter);
89 if (srcFullWidth == srcFullHeight &&
90 destSubset.fLeft == destSubset.fTop &&
91 destSubset.width() == destSubset.height()&&
92 scaleX == scaleY) {
93 fYFilter = fXFilter;
94 } else {
95 this->computeFilters(srcFullHeight, destSubset.fTop, destSubset.height(),
96 scaleY, &fYFilter);
97 }
98 }
99
100 // TODO(egouriou): Take advantage of periods in the convolution.
101 // Practical resizing filters are periodic outside of the border area.
102 // For Lanczos, a scaling by a (reduced) factor of p/q (q pixels in the
103 // source become p pixels in the destination) will have a period of p.
104 // A nice consequence is a period of 1 when downscaling by an integral
105 // factor. Downscaling from typical display resolutions is also bound
106 // to produce interesting periods as those are chosen to have multiple
107 // small factors.
108 // Small periods reduce computational load and improve cache usage if
109 // the coefficients can be shared. For periods of 1 we can consider
110 // loading the factors only once outside the borders.
computeFilters(int srcSize,float destSubsetLo,float destSubsetSize,float scale,SkConvolutionFilter1D * output)111 void SkResizeFilter::computeFilters(int srcSize,
112 float destSubsetLo, float destSubsetSize,
113 float scale,
114 SkConvolutionFilter1D* output) {
115 float destSubsetHi = destSubsetLo + destSubsetSize; // [lo, hi)
116
117 // When we're doing a magnification, the scale will be larger than one. This
118 // means the destination pixels are much smaller than the source pixels, and
119 // that the range covered by the filter won't necessarily cover any source
120 // pixel boundaries. Therefore, we use these clamped values (max of 1) for
121 // some computations.
122 float clampedScale = SkTMin(1.0f, scale);
123
124 // This is how many source pixels from the center we need to count
125 // to support the filtering function.
126 float srcSupport = fBitmapFilter->width() / clampedScale;
127
128 float invScale = 1.0f / scale;
129
130 SkSTArray<64, float, true> filterValuesArray;
131 SkSTArray<64, SkConvolutionFilter1D::ConvolutionFixed, true> fixedFilterValuesArray;
132
133 // Loop over all pixels in the output range. We will generate one set of
134 // filter values for each one. Those values will tell us how to blend the
135 // source pixels to compute the destination pixel.
136
137 // This is the pixel in the source directly under the pixel in the dest.
138 // Note that we base computations on the "center" of the pixels. To see
139 // why, observe that the destination pixel at coordinates (0, 0) in a 5.0x
140 // downscale should "cover" the pixels around the pixel with *its center*
141 // at coordinates (2.5, 2.5) in the source, not those around (0, 0).
142 // Hence we need to scale coordinates (0.5, 0.5), not (0, 0).
143 destSubsetLo = SkScalarFloorToScalar(destSubsetLo);
144 destSubsetHi = SkScalarCeilToScalar(destSubsetHi);
145 float srcPixel = (destSubsetLo + 0.5f) * invScale;
146 int destLimit = SkScalarTruncToInt(destSubsetHi - destSubsetLo);
147 output->reserveAdditional(destLimit, SkScalarCeilToInt(destLimit * srcSupport * 2));
148 for (int destI = 0; destI < destLimit; srcPixel += invScale, destI++) {
149 // Compute the (inclusive) range of source pixels the filter covers.
150 float srcBegin = SkTMax(0.f, SkScalarFloorToScalar(srcPixel - srcSupport));
151 float srcEnd = SkTMin(srcSize - 1.f, SkScalarCeilToScalar(srcPixel + srcSupport));
152
153 // Compute the unnormalized filter value at each location of the source
154 // it covers.
155
156 // Sum of the filter values for normalizing.
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 destFilterDist = (srcBegin + 0.5f - srcPixel) * clampedScale;
164 int filterCount = SkScalarTruncToInt(srcEnd - srcBegin) + 1;
165 if (filterCount <= 0) {
166 // true when srcSize is equal to srcPixel - srcSupport; this may be a bug
167 return;
168 }
169 filterValuesArray.reset(filterCount);
170 float filterSum = fBitmapFilter->evaluate_n(destFilterDist, clampedScale, filterCount,
171 filterValuesArray.begin());
172
173 // The filter must be normalized so that we don't affect the brightness of
174 // the image. Convert to normalized fixed point.
175 int fixedSum = 0;
176 fixedFilterValuesArray.reset(filterCount);
177 const float* filterValues = filterValuesArray.begin();
178 SkConvolutionFilter1D::ConvolutionFixed* fixedFilterValues = fixedFilterValuesArray.begin();
179 float invFilterSum = 1 / filterSum;
180 for (int fixedI = 0; fixedI < filterCount; fixedI++) {
181 int curFixed = SkConvolutionFilter1D::FloatToFixed(filterValues[fixedI] * invFilterSum);
182 fixedSum += curFixed;
183 fixedFilterValues[fixedI] = SkToS16(curFixed);
184 }
185 SkASSERT(fixedSum <= 0x7FFF);
186
187 // The conversion to fixed point will leave some rounding errors, which
188 // we add back in to avoid affecting the brightness of the image. We
189 // arbitrarily add this to the center of the filter array (this won't always
190 // be the center of the filter function since it could get clipped on the
191 // edges, but it doesn't matter enough to worry about that case).
192 int leftovers = SkConvolutionFilter1D::FloatToFixed(1) - fixedSum;
193 fixedFilterValues[filterCount / 2] += leftovers;
194
195 // Now it's ready to go.
196 output->AddFilter(SkScalarFloorToInt(srcBegin), fixedFilterValues, filterCount);
197 }
198 }
199
200 ///////////////////////////////////////////////////////////////////////////////////////////////////
201
valid_for_resize(const SkPixmap & source,int dstW,int dstH)202 static bool valid_for_resize(const SkPixmap& source, int dstW, int dstH) {
203 // TODO: Seems like we shouldn't care about the swizzle of source, just that it's 8888
204 return source.addr() && source.colorType() == kN32_SkColorType &&
205 source.width() >= 1 && source.height() >= 1 && dstW >= 1 && dstH >= 1;
206 }
207
Resize(const SkPixmap & result,const SkPixmap & source,ResizeMethod method)208 bool SkBitmapScaler::Resize(const SkPixmap& result, const SkPixmap& source, ResizeMethod method) {
209 if (!valid_for_resize(source, result.width(), result.height())) {
210 return false;
211 }
212 if (!result.addr() || result.colorType() != source.colorType()) {
213 return false;
214 }
215
216 SkRect destSubset = SkRect::MakeIWH(result.width(), result.height());
217
218 SkResizeFilter filter(method, source.width(), source.height(),
219 result.width(), result.height(), destSubset);
220
221 // Get a subset encompassing this touched area. We construct the
222 // offsets and row strides such that it looks like a new bitmap, while
223 // referring to the old data.
224 const uint8_t* sourceSubset = reinterpret_cast<const uint8_t*>(source.addr());
225
226 return BGRAConvolve2D(sourceSubset, static_cast<int>(source.rowBytes()),
227 !source.isOpaque(), filter.xFilter(), filter.yFilter(),
228 static_cast<int>(result.rowBytes()),
229 static_cast<unsigned char*>(result.writable_addr()));
230 }
231
Resize(SkBitmap * resultPtr,const SkPixmap & source,ResizeMethod method,int destWidth,int destHeight,SkBitmap::Allocator * allocator)232 bool SkBitmapScaler::Resize(SkBitmap* resultPtr, const SkPixmap& source, ResizeMethod method,
233 int destWidth, int destHeight, SkBitmap::Allocator* allocator) {
234 // Preflight some of the checks, to avoid allocating the result if we don't need it.
235 if (!valid_for_resize(source, destWidth, destHeight)) {
236 return false;
237 }
238
239 SkBitmap result;
240 // Note: pass along the profile information even thought this is no the right answer because
241 // this could be scaling in sRGB.
242 result.setInfo(SkImageInfo::MakeN32(destWidth, destHeight, source.alphaType(),
243 sk_ref_sp(source.info().colorSpace())));
244 result.allocPixels(allocator, nullptr);
245
246 SkPixmap resultPM;
247 if (!result.peekPixels(&resultPM) || !Resize(resultPM, source, method)) {
248 return false;
249 }
250
251 *resultPtr = result;
252 resultPtr->lockPixels();
253 SkASSERT(resultPtr->getPixels());
254 return true;
255 }
256