fillNHWC4D function
Fills a 4D NHWC tensor cache from a flat Float32List.
Converts a flat array of pixel data into the nested list structure expected by TensorFlow Lite models in NHWC format:
- N: Batch dimension (assumed to be 1, index 0)
- H: Height dimension (inH)
- W: Width dimension (inW)
- C: Channel dimension (3 for RGB)
flat - Flattened pixel data (length must be inH * inW * 3)
input4dCache - Pre-allocated 4D structure H3
inH - Input tensor height
inW - Input tensor width
Implementation
@pragma('vm:prefer-inline')
void fillNHWC4D(
Float32List flat,
List<List<List<List<double>>>> input4dCache,
int inH,
int inW,
) {
double sanitize(double value) =>
(value * 1e6).roundToDouble() / 1e6; // Reduce float32 noise.
int k = 0;
for (int y = 0; y < inH; y++) {
for (int x = 0; x < inW; x++) {
final List<double> px = input4dCache[0][y][x];
px[0] = sanitize(flat[k++]);
px[1] = sanitize(flat[k++]);
px[2] = sanitize(flat[k++]);
}
}
}