decode method
Decode the file and extract a single image from it. If the file is
animated, and frame
is specified, that particular frame will be decoded.
Otherwise if the image is animated and frame
is null, the returned
Image will include all frames. If there was a problem decoding the
Image, null will be returned.
Implementation
@override
Image? decode(Uint8List bytes, {int? frame}) {
if (startDecode(bytes) == null) {
return null;
}
if (!_info.isAnimated || frame != null) {
return decodeFrame(frame ?? 0)!;
}
Image? firstImage;
Image? lastImage;
for (var i = 0; i < _info.numFrames; ++i) {
final frame = _info.frames[i];
final image = decodeFrame(i);
if (image == null) {
continue;
}
if (firstImage == null || lastImage == null) {
firstImage = image;
lastImage = image
// Convert to MS
..frameDuration = (frame.delay * 1000).toInt();
continue;
}
if (image.width == lastImage.width &&
image.height == lastImage.height &&
frame.xOffset == 0 &&
frame.yOffset == 0 &&
frame.blend == PngBlendMode.source) {
lastImage = image
// Convert to MS
..frameDuration = (frame.delay * 1000).toInt();
firstImage.addFrame(lastImage);
continue;
}
final dispose = frame.dispose;
if (dispose == PngDisposeMode.background) {
lastImage = Image.from(lastImage)..clear(_info.backgroundColor);
} else if (dispose == PngDisposeMode.previous) {
lastImage = Image.from(lastImage);
} else {
lastImage = Image.from(lastImage);
}
// Convert to MS
lastImage.frameDuration = (frame.delay * 1000).toInt();
compositeImage(lastImage, image,
dstX: frame.xOffset,
dstY: frame.yOffset,
blend: frame.blend == PngBlendMode.over
? BlendMode.alpha
: BlendMode.direct);
firstImage.addFrame(lastImage);
}
return firstImage;
}