fillNHWC4DFromBgrBytes function

void fillNHWC4DFromBgrBytes({
  1. required Uint8List bytes,
  2. required List<List<List<List<double>>>> tensor,
  3. required int width,
  4. required int height,
  5. double scale = 1.0 / 255.0,
  6. double offset = 0.0,
})

Fills a 4D NHWC tensor in-place from raw BGR bytes with a BGR-to-RGB channel swap.

The tensor must already be allocated as [1][height][width][3]. Use scale and offset to control normalization:

  • For 0.0..1.0: scale=1/255, offset=0.0 (default)
  • For -1.0..1.0: scale=1/127.5, offset=-1.0

Parameters:

  • bytes: Raw BGR image bytes (length must be width * height * 3)
  • tensor: Pre-allocated 4D tensor [1][height][width][3] to fill
  • width: Image width in pixels
  • height: Image height in pixels
  • scale: Multiplier applied to each byte value before adding offset
  • offset: Value added after scaling

Implementation

void fillNHWC4DFromBgrBytes({
  required Uint8List bytes,
  required List<List<List<List<double>>>> tensor,
  required int width,
  required int height,
  double scale = 1.0 / 255.0,
  double offset = 0.0,
}) {
  int byteIndex = 0;

  for (int y = 0; y < height; y++) {
    final List<List<double>> row = tensor[0][y];
    for (int x = 0; x < width; x++) {
      final List<double> pixel = row[x];
      pixel[0] = bytes[byteIndex + 2] * scale + offset;
      pixel[1] = bytes[byteIndex + 1] * scale + offset;
      pixel[2] = bytes[byteIndex] * scale + offset;
      byteIndex += 3;
    }
  }
}