64 if (!describe(sourceWidth, sourceHeight, outputWidth, outputHeight, box, filter, shape))
72 void *workspace, gzUInt64 workspaceBytes)
75 return resizeRegion(source, outputWidth, outputHeight, box, filter, output, outputBytes, workspace, workspaceBytes);
84 void *workspace, gzUInt64 workspaceBytes)
86 gzUInt32 sourcePixels;
89 !describe(source.
width, source.
height, outputWidth, outputHeight, box, filter, shape) ||
90 outputBytes < shape.outputPixels || workspaceBytes < shape.bytes ||
91 !workspace || (uintptr_t)workspace %
alignof(gzUInt32))
94 if (!disjoint(source.
pixels, extent, output, shape.outputPixels) ||
95 !disjoint(source.
pixels, extent, workspace, shape.bytes) ||
96 !disjoint(output, shape.outputPixels, workspace, shape.bytes))
99 gzUByte *cursor = (gzUByte *)workspace;
100 Span *horizontal = (Span *)cursor;
101 cursor += (gzUInt64)outputWidth *
sizeof(Span);
102 Span *vertical = (Span *)cursor;
103 cursor += (gzUInt64)outputHeight *
sizeof(Span);
104 gzInt32 *weightsX = (gzInt32 *)cursor;
105 cursor += (gzUInt64)outputWidth * shape.tapsX *
sizeof(gzInt32);
106 gzInt32 *weightsY = (gzInt32 *)cursor;
107 cursor += (gzUInt64)outputHeight * shape.tapsY *
sizeof(gzInt32);
108 gzUByte *intermediate = cursor;
109 if (!plan(source.
width, outputWidth, box.
left, box.
right, shape.tapsX, filter, horizontal, weightsX) ||
110 !plan(source.
height, outputHeight, box.
top, box.
bottom, shape.tapsY, filter, vertical, weightsY))
113 if (shape.verticalFirst)
115 filterVertical(source, outputHeight, shape.tapsY, vertical, weightsY, intermediate);
117 (gzUInt64)source.
width * outputHeight };
118 filterHorizontal(view, outputWidth, shape.tapsX, horizontal, weightsX, output);
122 filterHorizontal(source, outputWidth, shape.tapsX, horizontal, weightsX, intermediate);
124 (gzUInt64)outputWidth * source.
height };
125 filterVertical(view, outputHeight, shape.tapsY, vertical, weightsY, output);
136 static gzBool
getReducedSize(gzUInt32 width, gzUInt32 height, gzUInt32 factorX, gzUInt32 factorY,
137 gzUInt32 &outputWidth, gzUInt32 &outputHeight)
139 if (!width || !height || width > 16777216u || height > 16777216u ||
140 (gzUInt64)width * height > 0xffffffffu || !factorX || !factorY ||
141 (gzUInt64)factorX * factorY > 65535u)
143 outputWidth = (gzUInt32)(((gzUInt64)width + factorX - 1) / factorX);
144 outputHeight = (gzUInt32)(((gzUInt64)height + factorY - 1) / factorY);
149 gzUByte *output, gzUInt64 outputBytes)
151 gzUInt32 sourcePixels, width, height;
154 outputBytes < (gzUInt64)width * height ||
156 output, (gzUInt64)width * height))
158 for (gzUInt32 y = 0; y < height; ++y)
160 const gzUInt32 firstY = y * factorY, rows = gzMin(factorY, source.
height - firstY);
161 for (gzUInt32 x = 0; x < width; ++x)
163 const gzUInt32 firstX = x * factorX, columns = gzMin(factorX, source.
width - firstX);
164 const gzUInt32 area = columns * rows;
165 gzUInt64 sum = area / 2;
166 for (gzUInt32 j = 0; j < rows; ++j)
167 for (gzUInt32 i = 0; i < columns; ++i)
168 sum += source.
pixels[(gzUInt64)(firstY + j) * source.
rowStride + firstX + i];
169 const gzUInt32 multiplier = (gzUInt32)(16777216.0f / (gzFloat)area);
170 output[(gzUInt64)y * width + x] = (gzUByte)gzMin((gzUInt64)255, sum * multiplier >> 24);
180 gzUInt32 first, count;
184 gzUInt32 tapsX, tapsY, outputPixels;
185 gzBool verticalFirst;
189 static gzBool axis(gzUInt32 source, gzUInt32 output, gzFloat first, gzFloat last,
double radius, gzUInt32 &taps)
191 if (!source || !output || source > 16777216u || output > 0x7fffffffu ||
192 !isfinite(first) || !isfinite(last) || first < 0 || last > source || first >= last)
194 const double support = radius * gzMax(1.0, (
double)(last - first) / output);
195 const double extent = 2 * ceil(support) + 1;
196 taps = (gzUInt32)gzMin((
double)source, extent);
197 return (gzUInt64)taps * output <= 0xffffffffu;
200 static gzBool describe(gzUInt32 width, gzUInt32 height, gzUInt32 outWidth, gzUInt32 outHeight,
206 const double radius = support(filter);
207 if (!axis(width, outWidth, box.
left, box.
right, radius, shape.tapsX) ||
208 !axis(height, outHeight, box.
top, box.
bottom, radius, shape.tapsY) ||
209 (gzUInt64)width * height > 0xffffffffu || (gzUInt64)outWidth * outHeight > 0xffffffffu)
211 shape.verticalFirst = (gzUInt64)height > (gzUInt64)100 * width && outHeight < height;
212 const gzUInt64 intermediate = shape.verticalFirst ? (gzUInt64)width * outHeight : (gzUInt64)outWidth * height;
213 if (intermediate > 0xffffffffu)
215 shape.outputPixels = outWidth * outHeight;
216 shape.bytes = ((gzUInt64)outWidth + outHeight) *
sizeof(Span) +
217 ((gzUInt64)outWidth * shape.tapsX + (gzUInt64)outHeight * shape.tapsY) *
sizeof(gzInt32) +
219 return shape.bytes <= (uintptr_t)~(uintptr_t)0;
231 return x > -0.5 && x <= 0.5 ? 1 : 0;
234 return x < 1 ? 1 - x : 0;
239 const double angle = x * 3.14159265358979323846;
240 return (sin(angle) / angle) * (sin(angle / 3) / (angle / 3));
243 static gzBool plan(gzUInt32 source, gzUInt32 output, gzFloat firstPixel, gzFloat lastPixel,
245 Span *spans, gzInt32 *weights)
247 const double scale = (double)(lastPixel - firstPixel) / output;
248 const double filterScale = gzMax(1.0, scale), reciprocal = 1 / filterScale;
249 const double reach = support(filter) * filterScale;
250 for (gzUInt32 pixel = 0; pixel < output; ++pixel)
252 const double center = firstPixel + (pixel + 0.5) * scale;
253 const gzInt32 first = gzMax(0, (gzInt32)(center - reach + 0.5));
254 const gzInt32 end = gzMin((gzInt32)source, (gzInt32)(center + reach + 0.5));
255 if (end <= first || (gzUInt32)(end - first) > taps)
257 spans[pixel].first = (gzUInt32)first;
258 spans[pixel].count = (gzUInt32)(end - first);
260 for (gzInt32 i = first; i < end; ++i)
261 total += kernel((i - center + 0.5) * reciprocal, filter);
262 if (!isfinite(total) || total <= 0)
264 for (gzUInt32 i = 0; i < spans[pixel].count; ++i)
266 const double value = kernel((i + first - center + 0.5) * reciprocal, filter) / total;
267 const double scaled = value * 4194304.0;
268 if (!isfinite(scaled) || scaled < -2147483647.0 || scaled > 2147483646.0)
270 weights[(gzUInt64)pixel * taps + i] = (gzInt32)(scaled < 0 ? scaled - 0.5 : scaled + 0.5);
276 static gzVoid filterHorizontal(
const ctxImageByteView &source, gzUInt32 width, gzUInt32 taps,
277 const Span *spans,
const gzInt32 *coefficients, gzUByte *output)
279 for (gzUInt32 y = 0; y < source.
height; ++y)
282 for (gzUInt32 x = 0; x < width; ++x)
284 const Span &span = spans[x];
285 const gzInt32 *weights = coefficients + (gzUInt64)x * taps;
286 int64_t sum = 2097152;
287 for (gzUInt32 i = 0; i < span.count; ++i)
288 sum += (int64_t)row[span.first + i] * weights[i];
289 output[(gzUInt64)y * width + x] = quantize(sum);
294 static gzVoid filterVertical(
const ctxImageByteView &source, gzUInt32 height, gzUInt32 taps,
295 const Span *spans,
const gzInt32 *coefficients, gzUByte *output)
297 for (gzUInt32 y = 0; y < height; ++y)
299 const Span &span = spans[y];
300 const gzInt32 *weights = coefficients + (gzUInt64)y * taps;
301 for (gzUInt32 x = 0; x < source.
width; ++x)
303 int64_t sum = 2097152;
304 for (gzUInt32 i = 0; i < span.count; ++i)
305 sum += (int64_t)source.
pixels[(gzUInt64)(span.first + i) * source.
rowStride + x] * weights[i];
306 output[(gzUInt64)y * source.
width + x] = quantize(sum);
311 static gzUByte quantize(int64_t sum)
315 if (sum >= 1069547520)
317 return (gzUByte)((gzUInt64)sum >> 22);
320 static gzBool span(
const void *pointer, gzUInt64 bytes, uintptr_t &first, uintptr_t &last)
322 const uintptr_t limit = ~(uintptr_t)0;
323 if (!pointer || !bytes || bytes > limit || (uintptr_t)pointer > limit - (uintptr_t)bytes)
325 first = (uintptr_t)pointer;
326 last = first + (uintptr_t)bytes;
330 static gzBool disjoint(
const void *a, gzUInt64 aBytes,
const void *b, gzUInt64 bBytes)
332 uintptr_t firstA, lastA, firstB, lastB;
333 return span(a, aBytes, firstA, lastA) && span(b, bBytes, firstB, lastB) &&
334 (lastA <= firstB || lastB <= firstA);