imageInfo function

({int channels, int height, int width}) imageInfo(
  1. Uint8List bytes
)

Reads an image's dimensions and channel count without decoding its pixels.

This parses only the header, so it is far cheaper than decodeImage when all you need is the size. The channel count is the image's native value and matches what decodeImage returns with no forceChannels.

final info = imageInfo(bytes);
print('${info.width}x${info.height}');

Throws an ArgumentError if bytes is empty and an ImageFfiException if the header cannot be parsed.

Implementation

({int width, int height, int channels}) imageInfo(Uint8List bytes) {
  if (bytes.isEmpty) {
    throw ArgumentError.value(bytes, 'bytes', 'must not be empty');
  }

  final dataPtr = _copyToNative(bytes);
  final outWidth = malloc<Int>();
  final outHeight = malloc<Int>();
  final outChannels = malloc<Int>();
  try {
    final ok = imgffiInfo(
      dataPtr,
      bytes.length,
      outWidth,
      outHeight,
      outChannels,
    );
    if (ok != 1) {
      throw ImageFfiException(_failureReason());
    }
    return (
      width: outWidth.value,
      height: outHeight.value,
      channels: outChannels.value,
    );
  } finally {
    freeBytes(dataPtr);
    malloc.free(outWidth);
    malloc.free(outHeight);
    malloc.free(outChannels);
  }
}