realContentSize method
Get the real content size of the image.
Implementation
Future<Rect> realContentSize({
Color ignoreColor = const Color(0xffffffff),
double tolerance = 1,
}) async {
Completer<Rect> completer = Completer();
final ByteData? byteData =
await toByteData(format: ImageByteFormat.rawRgba);
if (byteData.isNull) {
completer.complete(rect());
} else {
final Uint32List uint32list = byteData!.buffer.asUint32List();
late double minDx = -1;
late double minDy = -1;
late double maxDx = -1;
late double maxDy = -1;
bool isAssign = false;
uint32list.iFor((index, element) {
final bool isDiff = element.uInt32toColor
.euclideanDistanceColorDifference(ignoreColor,
tolerance: tolerance);
if (isDiff) {
final int dx = index % width;
final int dy = index ~/ width;
if (!isAssign) {
isAssign = true;
minDx = dx * 1;
maxDx = dx * 1;
minDy = dy * 1;
maxDy = dy * 1;
} else {
minDx = min(dx * 1, minDx);
minDy = min(dy * 1, minDy);
maxDx = max(dx * 1, maxDx);
maxDy = max(dy * 1, maxDy);
}
}
});
final Offset start = Offset(minDx, minDy);
final Size realSize = Size((maxDx - minDx), (maxDy - minDy));
completer.complete(start & realSize);
}
return completer.future;
}