CorTeX Core ABI 7
ToolTech Software · C++ library reference
Loading...
Searching...
No Matches
ctxImageAnalysis.h
Go to the documentation of this file.
1//*****************************************************************************
2// File : ctxImageAnalysis.h
3// Description : Shared CPU binary-image analysis for SDK and standalone callers
4// Author : Anders Moden
5// Product : CorTeX 2.0.1
6// Copyright (C) 2026 Anders Moden, Sweden
7// Revision History...
8// AMO 261024 Share bounded original-foreground corridor-run support (2.0.2)
9// AMO 261022 Add bounded run filtering, projections and component labels (2.0.2)
10//*****************************************************************************
11#ifndef __CTX_IMAGE_ANALYSIS_H__
12#define __CTX_IMAGE_ANALYSIS_H__
13
14#ifdef CTX_CPP_STANDALONE
15#include <ctxVirtualMachine.h>
16#ifndef CTX_CPP_STANDALONE_RUNTIME
17#error Standalone image analysis requires the small standalone header first
18#endif
19#else
20#include "ctxBase.h"
21#include <stdint.h>
22#endif
23
30{
31 const gzUByte *pixels;
32 gzUInt32 width, height;
34};
35
41
47
53{
54 gzUInt32 label, x, y, width, height, area;
55 gzUInt64 sumX, sumY;
56};
57
63{
64public:
65
67 virtual gzBool onComponent(const ctxBinaryImageComponent &component) = 0;
68};
69
76{
77public:
78
83 static gzBool getPixelCount(const ctxImageByteView &image, gzUInt32 &count)
84 {
85 gzUInt32 pixels;
86 gzUInt64 bytes;
87 if (!validateImage(image, pixels, bytes))
88 return FALSE;
89
90 count = pixels;
91 return TRUE;
92 }
93
99 static gzBool retainRuns(const ctxImageByteView &image, ctxBinaryImageAxis axis,
100 gzUInt32 minimumLength, gzUByte *output, gzUInt32 outputCount)
101 {
102 gzUInt32 pixels;
103 gzUInt64 bytes;
104 if (!validateImage(image, pixels, bytes) || !validAxis(axis) || !minimumLength ||
105 outputCount != pixels || !separate(image.pixels, bytes, output, outputCount))
106 return FALSE;
107
108 memset(output, 0, outputCount);
109 const gzUInt32 extent = axis == CTX_BINARY_IMAGE_HORIZONTAL ? image.width : image.height;
110 const gzUInt32 lines = axis == CTX_BINARY_IMAGE_HORIZONTAL ? image.height : image.width;
111 for (gzUInt32 line = 0; line < lines; ++line)
112 {
113 gzUInt32 position = 0;
114 while (position < extent)
115 {
116 if (!at(image, axis, line, position))
117 {
118 ++position;
119 continue;
120 }
121
122 const gzUInt32 first = position;
123 while (position < extent && at(image, axis, line, position))
124 ++position;
125 if (position - first >= minimumLength)
126 {
127 for (gzUInt32 i = first; i < position; ++i)
128 {
129 const gzUInt32 index = axis == CTX_BINARY_IMAGE_HORIZONTAL ?
130 line * image.width + i : i * image.width + line;
131 output[index] = 255;
132 }
133 }
134 }
135 }
136 return TRUE;
137 }
138
150 gzUInt32 minimumLength, gzUInt32 orthogonalRadius, gzUByte *output, gzUByte *scratch, gzUInt32 outputCount)
151 {
152 gzUInt32 pixels;
153 gzUInt64 bytes;
154 if (!validateImage(image, pixels, bytes) || !validAxis(axis) || !minimumLength || outputCount != pixels ||
155 !separate(image.pixels, bytes, output, outputCount) || !separate(image.pixels, bytes, scratch, outputCount) ||
156 !separate(output, outputCount, scratch, outputCount))
157 return FALSE;
158
160 const gzUInt32 extent = axis == CTX_BINARY_IMAGE_HORIZONTAL ? image.height : image.width;
161 const gzUInt32 lines = axis == CTX_BINARY_IMAGE_HORIZONTAL ? image.width : image.height;
162 const gzUInt32 radius = gzMin(orthogonalRadius, extent - 1);
163 for (gzUInt32 line = 0; line < lines; ++line)
164 {
165 gzUInt32 support = 0;
166 for (gzUInt32 position = 0; position <= radius; ++position)
167 support += at(image, across, line, position) != 0;
168 for (gzUInt32 position = 0; position < extent; ++position)
169 {
170 const gzUInt32 index = across == CTX_BINARY_IMAGE_HORIZONTAL ?
171 line * image.width + position : position * image.width + line;
172 scratch[index] = support ? 255 : 0;
173 if (position >= radius)
174 support -= at(image, across, line, position - radius) != 0;
175 if (radius < extent - position - 1)
176 support += at(image, across, line, position + radius + 1) != 0;
177 }
178 }
179 const ctxImageByteView corridor = { scratch, image.width, image.height, image.width, pixels };
180 if (!retainRuns(corridor, axis, minimumLength, output, outputCount))
181 return FALSE;
182 for (gzUInt32 y = 0; y < image.height; ++y)
183 for (gzUInt32 x = 0; x < image.width; ++x)
184 if (!image.pixels[(gzUInt64)y * image.rowStride + x]) output[y * image.width + x] = 0;
185 return TRUE;
186 }
187
192 static gzBool project(const ctxImageByteView &image, ctxBinaryImageAxis axis,
193 gzUInt32 *output, gzUInt32 outputCount)
194 {
195 gzUInt32 pixels;
196 gzUInt64 bytes;
197 if (!validateImage(image, pixels, bytes) || !validAxis(axis) ||
198 outputCount != (axis == CTX_BINARY_IMAGE_HORIZONTAL ? image.height : image.width) ||
199 !aligned(output) || !separate(image.pixels, bytes, output, (gzUInt64)outputCount * sizeof(*output)))
200 return FALSE;
201
202 memset(output, 0, (size_t)outputCount * sizeof(*output));
203 for (gzUInt32 y = 0; y < image.height; ++y)
204 {
205 for (gzUInt32 x = 0; x < image.width; ++x)
206 {
207 if (image.pixels[(gzUInt64)y * image.rowStride + x])
208 ++output[axis == CTX_BINARY_IMAGE_HORIZONTAL ? y : x];
209 }
210 }
211 return TRUE;
212 }
213
223 static gzBool labelComponents(const ctxImageByteView &image, ctxBinaryImageConnectivity connectivity,
224 gzUInt32 *labels, gzUInt32 labelCount, gzUInt32 *queue, gzUInt32 queueCount,
225 ctxBinaryImageComponentSink *sink, gzUInt32 &componentCount)
226 {
227 gzUInt32 pixels;
228 gzUInt64 bytes;
229 if (!validateImage(image, pixels, bytes) ||
230 (connectivity != CTX_BINARY_IMAGE_FOUR_CONNECTED && connectivity != CTX_BINARY_IMAGE_EIGHT_CONNECTED) ||
231 labelCount != pixels || queueCount < pixels || !aligned(labels) || !aligned(queue) ||
232 !separate(image.pixels, bytes, labels, (gzUInt64)pixels * sizeof(*labels)) ||
233 !separate(image.pixels, bytes, queue, (gzUInt64)pixels * sizeof(*queue)) ||
234 !separate(labels, (gzUInt64)pixels * sizeof(*labels), queue, (gzUInt64)pixels * sizeof(*queue)) ||
235 !separate(image.pixels, bytes, &componentCount, sizeof(componentCount)) ||
236 !separate(labels, (gzUInt64)pixels * sizeof(*labels), &componentCount, sizeof(componentCount)) ||
237 !separate(queue, (gzUInt64)pixels * sizeof(*queue), &componentCount, sizeof(componentCount)))
238 return FALSE;
239
240 memset(labels, 0, (size_t)pixels * sizeof(*labels));
241 gzUInt32 count = 0;
242 for (gzUInt32 seed = 0; seed < pixels; ++seed)
243 {
244 const gzUInt32 sx = seed % image.width, sy = seed / image.width;
245 if (labels[seed] || !image.pixels[(gzUInt64)sy * image.rowStride + sx])
246 continue;
247
248 ctxBinaryImageComponent component = { ++count, sx, sy, 1, 1, 0, 0, 0 };
249 gzUInt32 head = 0, tail = 1, maxX = sx, maxY = sy;
250 queue[0] = seed;
251 labels[seed] = count;
252 while (head < tail)
253 {
254 const gzUInt32 current = queue[head++];
255 const gzUInt32 x = current % image.width, y = current / image.width;
256 component.x = gzMin(component.x, x);
257 component.y = gzMin(component.y, y);
258 maxX = gzMax(maxX, x);
259 maxY = gzMax(maxY, y);
260 ++component.area;
261 component.sumX += x;
262 component.sumY += y;
263 for (gzInt32 dy = -1; dy <= 1; ++dy)
264 {
265 for (gzInt32 dx = -1; dx <= 1; ++dx)
266 {
267 if ((!dx && !dy) ||
268 (connectivity == CTX_BINARY_IMAGE_FOUR_CONNECTED && dx && dy))
269 continue;
270 const gzInt32 nx = (gzInt32)x + dx, ny = (gzInt32)y + dy;
271 if (nx < 0 || ny < 0 || (gzUInt32)nx >= image.width || (gzUInt32)ny >= image.height)
272 continue;
273 const gzUInt32 next = (gzUInt32)ny * image.width + (gzUInt32)nx;
274 if (!labels[next] && image.pixels[(gzUInt64)ny * image.rowStride + (gzUInt32)nx])
275 {
276 labels[next] = count;
277 queue[tail++] = next;
278 }
279 }
280 }
281 }
282 component.width = maxX - component.x + 1;
283 component.height = maxY - component.y + 1;
284 if (sink && !sink->onComponent(component))
285 return FALSE;
286 }
287 componentCount = count;
288 return TRUE;
289 }
290
291private:
292
293 static gzBool validAxis(ctxBinaryImageAxis axis)
294 {
296 }
297
298 static gzBool aligned(const gzUInt32 *values)
299 {
300 return values && (uintptr_t)values % alignof(gzUInt32) == 0;
301 }
302
303 static gzBool span(const void *pointer, gzUInt64 bytes, uintptr_t &first, uintptr_t &last)
304 {
305 const uintptr_t limit = ~(uintptr_t)0;
306 if (!pointer || !bytes || bytes > limit || (uintptr_t)pointer > limit - (uintptr_t)bytes)
307 return FALSE;
308 first = (uintptr_t)pointer;
309 last = first + (uintptr_t)bytes;
310 return TRUE;
311 }
312
313 static gzBool separate(const void *left, gzUInt64 leftBytes, const void *right, gzUInt64 rightBytes)
314 {
315 uintptr_t a, endA, b, endB;
316 return span(left, leftBytes, a, endA) && span(right, rightBytes, b, endB) &&
317 (endA <= b || endB <= a);
318 }
319
320 static gzBool validateImage(const ctxImageByteView &image, gzUInt32 &pixels, gzUInt64 &bytes)
321 {
322 const gzUInt64 limit = ~(gzUInt64)0;
323 if (!image.pixels || !image.width || !image.height || image.width > 0x7fffffffu ||
324 image.height > 0x7fffffffu || image.rowStride < image.width ||
325 (gzUInt64)(image.height - 1) > (limit - image.width) / image.rowStride)
326 return FALSE;
327 const gzUInt64 extent = (gzUInt64)(image.height - 1) * image.rowStride + image.width;
328 const gzUInt64 count = (gzUInt64)image.width * image.height;
329 uintptr_t first, last;
330 if (extent > image.byteCount || count > 0xffffffffu || !span(image.pixels, extent, first, last))
331 return FALSE;
332 pixels = (gzUInt32)count;
333 bytes = extent;
334 return TRUE;
335 }
336
337 static gzUByte at(const ctxImageByteView &image, ctxBinaryImageAxis axis,
338 gzUInt32 line, gzUInt32 position)
339 {
340 return axis == CTX_BINARY_IMAGE_HORIZONTAL ?
341 image.pixels[(gzUInt64)line * image.rowStride + position] :
342 image.pixels[(gzUInt64)position * image.rowStride + line];
343 }
344};
345
346#endif
Allocation-free CPU image primitives, shared by SDK and standalone code.
static gzBool labelComponents(const ctxImageByteView &image, ctxBinaryImageConnectivity connectivity, gzUInt32 *labels, gzUInt32 labelCount, gzUInt32 *queue, gzUInt32 queueCount, ctxBinaryImageComponentSink *sink, gzUInt32 &componentCount)
Label every foreground pixel with 4/8 connectivity; background is zero.
static gzBool project(const ctxImageByteView &image, ctxBinaryImageAxis axis, gzUInt32 *output, gzUInt32 outputCount)
Count foreground along the named axis: HORIZONTAL returns height row counts, VERTICAL returns width c...
static gzBool retainRuns(const ctxImageByteView &image, ctxBinaryImageAxis axis, gzUInt32 minimumLength, gzUByte *output, gzUInt32 outputCount)
Retain complete maximal horizontal/vertical foreground runs having at least minimumLength pixels.
static gzBool getPixelCount(const ctxImageByteView &image, gzUInt32 &count)
Resolve the complete required pixel/work extent without changing count on failure.
static gzBool retainCorridorRuns(const ctxImageByteView &image, ctxBinaryImageAxis axis, gzUInt32 minimumLength, gzUInt32 orthogonalRadius, gzUByte *output, gzUByte *scratch, gzUInt32 outputCount)
Retain original foreground supported by maximal runs in an orthogonal corridor.
Borrowed streaming sink.
virtual gzBool onComponent(const ctxBinaryImageComponent &component)=0
ctxBinaryImageAxis
@ CTX_BINARY_IMAGE_VERTICAL
@ CTX_BINARY_IMAGE_HORIZONTAL
ctxBinaryImageConnectivity
@ CTX_BINARY_IMAGE_EIGHT_CONNECTED
@ CTX_BINARY_IMAGE_FOUR_CONNECTED
Integer pixel bounds and moments.
Borrowed one-component byte plane.
const gzUByte * pixels