CorTeX Core ABI 7
ToolTech Software · C++ library reference
Loading...
Searching...
No Matches
ctxImageResampling.h
Go to the documentation of this file.
1//*****************************************************************************
2// File : ctxImageResampling.h
3// Description : Shared antialiased byte-plane resizing for SDK and standalone
4// Author : Anders Moden
5// Product : CorTeX 2.0.1
6// Copyright (C) 2026 Anders Moden, Sweden
7// Revision History...
8// AMO 261112 Add Pillow-compatible box filter for area-averaged normalization (2.0.2)
9// AMO 261022 Add contained fractional regions and explicit integer byte reduction (2.0.2)
10// AMO 261022 Add explicit byte-quantized linear and Lanczos3 resize kernels (2.0.2)
11//*****************************************************************************
12#ifndef __CTX_IMAGE_RESAMPLING_H__
13#define __CTX_IMAGE_RESAMPLING_H__
14
15#include "ctxImageAnalysis.h"
16#include <math.h>
17
24
29{
30 gzFloat left, top, right, bottom;
31};
32
48{
49public:
50
51 static gzBool getWorkspaceBytes(gzUInt32 sourceWidth, gzUInt32 sourceHeight,
52 gzUInt32 outputWidth, gzUInt32 outputHeight, ctxImageResamplingFilter filter, gzUInt64 &bytes)
53 {
54 const ctxImageResamplingBox box = { 0, 0, (gzFloat)sourceWidth, (gzFloat)sourceHeight };
55 return getRegionWorkspaceBytes(sourceWidth, sourceHeight, outputWidth, outputHeight, box, filter, bytes);
56 }
57
59 static gzBool getRegionWorkspaceBytes(gzUInt32 sourceWidth, gzUInt32 sourceHeight,
60 gzUInt32 outputWidth, gzUInt32 outputHeight, const ctxImageResamplingBox &box,
61 ctxImageResamplingFilter filter, gzUInt64 &bytes)
62 {
63 Shape shape;
64 if (!describe(sourceWidth, sourceHeight, outputWidth, outputHeight, box, filter, shape))
65 return FALSE;
66 bytes = shape.bytes;
67 return TRUE;
68 }
69
70 static gzBool resize(const ctxImageByteView &source, gzUInt32 outputWidth, gzUInt32 outputHeight,
71 ctxImageResamplingFilter filter, gzUByte *output, gzUInt64 outputBytes,
72 void *workspace, gzUInt64 workspaceBytes)
73 {
74 const ctxImageResamplingBox box = { 0, 0, (gzFloat)source.width, (gzFloat)source.height };
75 return resizeRegion(source, outputWidth, outputHeight, box, filter, output, outputBytes, workspace, workspaceBytes);
76 }
77
82 static gzBool resizeRegion(const ctxImageByteView &source, gzUInt32 outputWidth, gzUInt32 outputHeight,
83 const ctxImageResamplingBox &box, ctxImageResamplingFilter filter, gzUByte *output, gzUInt64 outputBytes,
84 void *workspace, gzUInt64 workspaceBytes)
85 {
86 gzUInt32 sourcePixels;
87 Shape shape;
88 if (!ctxBinaryImageAnalysis::getPixelCount(source, 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))
92 return FALSE;
93 const gzUInt64 extent = (gzUInt64)(source.height - 1) * source.rowStride + source.width;
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))
97 return FALSE;
98
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))
111 return FALSE;
112
113 if (shape.verticalFirst)
114 {
115 filterVertical(source, outputHeight, shape.tapsY, vertical, weightsY, intermediate);
116 const ctxImageByteView view = { intermediate, source.width, outputHeight, source.width,
117 (gzUInt64)source.width * outputHeight };
118 filterHorizontal(view, outputWidth, shape.tapsX, horizontal, weightsX, output);
119 }
120 else
121 {
122 filterHorizontal(source, outputWidth, shape.tapsX, horizontal, weightsX, intermediate);
123 const ctxImageByteView view = { intermediate, outputWidth, source.height, outputWidth,
124 (gzUInt64)outputWidth * source.height };
125 filterVertical(view, outputHeight, shape.tapsY, vertical, weightsY, output);
126 }
127 return TRUE;
128 }
129
136 static gzBool getReducedSize(gzUInt32 width, gzUInt32 height, gzUInt32 factorX, gzUInt32 factorY,
137 gzUInt32 &outputWidth, gzUInt32 &outputHeight)
138 {
139 if (!width || !height || width > 16777216u || height > 16777216u ||
140 (gzUInt64)width * height > 0xffffffffu || !factorX || !factorY ||
141 (gzUInt64)factorX * factorY > 65535u)
142 return FALSE;
143 outputWidth = (gzUInt32)(((gzUInt64)width + factorX - 1) / factorX);
144 outputHeight = (gzUInt32)(((gzUInt64)height + factorY - 1) / factorY);
145 return TRUE;
146 }
147
148 static gzBool reduce(const ctxImageByteView &source, gzUInt32 factorX, gzUInt32 factorY,
149 gzUByte *output, gzUInt64 outputBytes)
150 {
151 gzUInt32 sourcePixels, width, height;
152 if (!ctxBinaryImageAnalysis::getPixelCount(source, sourcePixels) ||
153 !getReducedSize(source.width, source.height, factorX, factorY, width, height) ||
154 outputBytes < (gzUInt64)width * height ||
155 !disjoint(source.pixels, (gzUInt64)(source.height - 1) * source.rowStride + source.width,
156 output, (gzUInt64)width * height))
157 return FALSE;
158 for (gzUInt32 y = 0; y < height; ++y)
159 {
160 const gzUInt32 firstY = y * factorY, rows = gzMin(factorY, source.height - firstY);
161 for (gzUInt32 x = 0; x < width; ++x)
162 {
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);
171 }
172 }
173 return TRUE;
174 }
175
176private:
177
178 struct Span
179 {
180 gzUInt32 first, count;
181 };
182 struct Shape
183 {
184 gzUInt32 tapsX, tapsY, outputPixels;
185 gzBool verticalFirst;
186 gzUInt64 bytes;
187 };
188
189 static gzBool axis(gzUInt32 source, gzUInt32 output, gzFloat first, gzFloat last, double radius, gzUInt32 &taps)
190 {
191 if (!source || !output || source > 16777216u || output > 0x7fffffffu ||
192 !isfinite(first) || !isfinite(last) || first < 0 || last > source || first >= last)
193 return FALSE;
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;
198 }
199
200 static gzBool describe(gzUInt32 width, gzUInt32 height, gzUInt32 outWidth, gzUInt32 outHeight,
201 const ctxImageResamplingBox &box, ctxImageResamplingFilter filter, Shape &shape)
202 {
204 filter != CTX_IMAGE_RESAMPLING_BOX)
205 return FALSE;
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)
210 return FALSE;
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)
214 return FALSE;
215 shape.outputPixels = outWidth * outHeight;
216 shape.bytes = ((gzUInt64)outWidth + outHeight) * sizeof(Span) +
217 ((gzUInt64)outWidth * shape.tapsX + (gzUInt64)outHeight * shape.tapsY) * sizeof(gzInt32) +
218 intermediate;
219 return shape.bytes <= (uintptr_t)~(uintptr_t)0;
220 }
221
222 static double support(ctxImageResamplingFilter filter)
223 {
224 return filter == CTX_IMAGE_RESAMPLING_LINEAR ? 1.0 : filter == CTX_IMAGE_RESAMPLING_BOX ? 0.5 : 3.0;
225 }
226
227 static double kernel(double x, ctxImageResamplingFilter filter)
228 {
229 // The box cell is half-open (-0.5,0.5], so a tap on a cell edge is counted once
230 if (filter == CTX_IMAGE_RESAMPLING_BOX)
231 return x > -0.5 && x <= 0.5 ? 1 : 0;
232 x = fabs(x);
233 if (filter == CTX_IMAGE_RESAMPLING_LINEAR)
234 return x < 1 ? 1 - x : 0;
235 if (x >= 3)
236 return 0;
237 if (!x)
238 return 1;
239 const double angle = x * 3.14159265358979323846;
240 return (sin(angle) / angle) * (sin(angle / 3) / (angle / 3));
241 }
242
243 static gzBool plan(gzUInt32 source, gzUInt32 output, gzFloat firstPixel, gzFloat lastPixel,
244 gzUInt32 taps, ctxImageResamplingFilter filter,
245 Span *spans, gzInt32 *weights)
246 {
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)
251 {
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)
256 return FALSE;
257 spans[pixel].first = (gzUInt32)first;
258 spans[pixel].count = (gzUInt32)(end - first);
259 double total = 0;
260 for (gzInt32 i = first; i < end; ++i)
261 total += kernel((i - center + 0.5) * reciprocal, filter);
262 if (!isfinite(total) || total <= 0)
263 return FALSE;
264 for (gzUInt32 i = 0; i < spans[pixel].count; ++i)
265 {
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)
269 return FALSE;
270 weights[(gzUInt64)pixel * taps + i] = (gzInt32)(scaled < 0 ? scaled - 0.5 : scaled + 0.5);
271 }
272 }
273 return TRUE;
274 }
275
276 static gzVoid filterHorizontal(const ctxImageByteView &source, gzUInt32 width, gzUInt32 taps,
277 const Span *spans, const gzInt32 *coefficients, gzUByte *output)
278 {
279 for (gzUInt32 y = 0; y < source.height; ++y)
280 {
281 const gzUByte *row = source.pixels + (gzUInt64)y * source.rowStride;
282 for (gzUInt32 x = 0; x < width; ++x)
283 {
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);
290 }
291 }
292 }
293
294 static gzVoid filterVertical(const ctxImageByteView &source, gzUInt32 height, gzUInt32 taps,
295 const Span *spans, const gzInt32 *coefficients, gzUByte *output)
296 {
297 for (gzUInt32 y = 0; y < height; ++y)
298 {
299 const Span &span = spans[y];
300 const gzInt32 *weights = coefficients + (gzUInt64)y * taps;
301 for (gzUInt32 x = 0; x < source.width; ++x)
302 {
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);
307 }
308 }
309 }
310
311 static gzUByte quantize(int64_t sum)
312 {
313 if (sum <= 0)
314 return 0;
315 if (sum >= 1069547520)
316 return 255;
317 return (gzUByte)((gzUInt64)sum >> 22);
318 }
319
320 static gzBool span(const void *pointer, gzUInt64 bytes, uintptr_t &first, uintptr_t &last)
321 {
322 const uintptr_t limit = ~(uintptr_t)0;
323 if (!pointer || !bytes || bytes > limit || (uintptr_t)pointer > limit - (uintptr_t)bytes)
324 return FALSE;
325 first = (uintptr_t)pointer;
326 last = first + (uintptr_t)bytes;
327 return TRUE;
328 }
329
330 static gzBool disjoint(const void *a, gzUInt64 aBytes, const void *b, gzUInt64 bBytes)
331 {
332 uintptr_t firstA, lastA, firstB, lastB;
333 return span(a, aBytes, firstA, lastA) && span(b, bBytes, firstB, lastB) &&
334 (lastA <= firstB || lastB <= firstA);
335 }
336};
337
338#endif
static gzBool getPixelCount(const ctxImageByteView &image, gzUInt32 &count)
Resolve the complete required pixel/work extent without changing count on failure.
Separable, antialiased resizing of one unsigned-byte component.
static gzBool getReducedSize(gzUInt32 width, gzUInt32 height, gzUInt32 factorX, gzUInt32 factorY, gzUInt32 &outputWidth, gzUInt32 &outputHeight)
Integer block reduction with clipped right/bottom blocks.
static gzBool resize(const ctxImageByteView &source, gzUInt32 outputWidth, gzUInt32 outputHeight, ctxImageResamplingFilter filter, gzUByte *output, gzUInt64 outputBytes, void *workspace, gzUInt64 workspaceBytes)
static gzBool reduce(const ctxImageByteView &source, gzUInt32 factorX, gzUInt32 factorY, gzUByte *output, gzUInt64 outputBytes)
static gzBool getWorkspaceBytes(gzUInt32 sourceWidth, gzUInt32 sourceHeight, gzUInt32 outputWidth, gzUInt32 outputHeight, ctxImageResamplingFilter filter, gzUInt64 &bytes)
static gzBool getRegionWorkspaceBytes(gzUInt32 sourceWidth, gzUInt32 sourceHeight, gzUInt32 outputWidth, gzUInt32 outputHeight, const ctxImageResamplingBox &box, ctxImageResamplingFilter filter, gzUInt64 &bytes)
Query filtered-region workspace.
static gzBool resizeRegion(const ctxImageByteView &source, gzUInt32 outputWidth, gzUInt32 outputHeight, const ctxImageResamplingBox &box, ctxImageResamplingFilter filter, gzUByte *output, gzUInt64 outputBytes, void *workspace, gzUInt64 workspaceBytes)
Filter a contained, nonempty fractional source region.
ctxImageResamplingFilter
@ CTX_IMAGE_RESAMPLING_BOX
@ CTX_IMAGE_RESAMPLING_LANCZOS3
@ CTX_IMAGE_RESAMPLING_LINEAR
Borrowed one-component byte plane.
const gzUByte * pixels
Half-open source pixel-cell coordinates, with explicit binary32 endpoints.