Function histogramInto
Counts numeric samples into N equally spaced value intervals.
RasterHistogramResult histogramInto(T)(
scope RasterView!T source,
size_t planeIndex,
T lower,
T upper,
scope ulong[] bins
) nothrow @nogc @safe
if (isNumericRasterSample!T);
The requested domain is the CLOSED interval [lower, upper], with lower strictly less than upper. For N = bins.length > 0, every in-range sample v is assigned to:
min(N - 1, floor((v - lower) * N / (upper - lower)))
Thus each interior bin is left-inclusive and right-exclusive, and the final bin also includes upper. Values below lower, above upper and NaNs are counted separately in the returned result, never silently dropped. Both range endpoints must be finite for float/double. Floating bin placement uses double arithmetic, with a scaled calculation when the range's direct double difference would overflow. Float/double boundaries may round near an interval edge; integer placement is always exact.
Integral arithmetic does not convert to floating point. The common case uses checked-width ulong multiplication. For large 64-bit ranges where the product would overflow, an exact quotient/remainder fallback preserves bin assignment across the full long/ulong domain.
Supported samples: byte, ubyte, short, ushort, int, uint, long, ulong, float and double (the existing isNumericRasterSample contract).
Destination ownership: - bins is a caller-owned, writable slice. All entries are OVERWRITTEN; previous counts are never accumulated. - No heap allocation, hidden raster materialization, retained view or worker thread is created. - Any error zeros the complete bins slice and all result counters. On success even a valid empty plane returns all zero counts. - Count overflow is detected before traversal by validating that logical width * height is representable in ulong. All counters start at zero, so no individual counter can then exceed this total.
Plane and layout semantics match the rest of raster-d: planeIndex is a logical plane; source may be a validated ROI and use any validated signed row/sample strides, planar or interleaved storage.
For tiled datasets, only combine histograms with IDENTICAL range endpoints, bin count and sample type. Addition is componentwise and must be checked for ulong overflow by the consumer.
Parameters
| Name | Description |
|---|---|
| source | Borrowed validated numeric raster view. |
| planeIndex | Zero-based logical plane index. |
| lower | Inclusive lower numeric bound. |
| upper | Inclusive upper numeric bound, included in the last bin. |
| bins | Writable caller-owned output counts; length defines N. |
Returns
Explicit success/error and counts below/above range and NaNs. Bin counts are returned through the caller-owned bins slice.
Example
Example calculating a signed elevation histogram using the public importer.
import core .stdc .stdlib : malloc;
import raster;
void* memory = malloc(4 * short .sizeof);
assert(memory !is null);
(cast(short*) memory)[0 .. 4] = [-100, 0, 50, 100];
OwnedByteResource owner;
assert(tryAdoptMallocResource(memory, 4 * short .sizeof, owner));
const PlaneByteLayout[1] layout = [
PlaneByteLayout(0, 2 * short .sizeof, short .sizeof)
];
RasterLease!short lease;
assert(tryImportOwnedRaster!short(
owner, layout[], Region2D(0, 0, 2, 2), lease
) .ok);
scope auto view = lease .view();
ulong[4] counts;
const result = view .histogramInto(0, cast(short) -100, cast(short) 100, counts[]);
assert(result .ok);
assert(counts == [1UL, 0UL, 1UL, 2UL]);
assert(result .belowRange == 0 && result .aboveRange == 0);