readImageRgb function
Reads a local file with a bounded stream, then performs synchronous decoding. No URL fetching or filename-extension detection. A file that grows between the length check and the read cannot bypass the encoded-byte limit.
Implementation
Future<DecodedRgbImage> readImageRgb(
String path, {
int maxEncodedBytes = defaultMaxEncodedImageBytes,
int maxPixels = defaultMaxDecodedImagePixels,
}) async {
_checkLimits(maxEncodedBytes, maxPixels);
final file = File(path);
_checkSize(await file.length(), maxEncodedBytes);
final bytes = BytesBuilder(copy: false);
await for (final chunk in file.openRead(0, maxEncodedBytes + 1)) {
if (bytes.length + chunk.length > maxEncodedBytes) {
throw FormatException('encoded image exceeds $maxEncodedBytes bytes');
}
bytes.add(chunk);
}
return decodeImageRgb(
bytes.takeBytes(),
maxEncodedBytes: maxEncodedBytes,
maxPixels: maxPixels,
);
}