decodeAnimation method

  1. @override
Animation? decodeAnimation(
  1. List<int> bytes
)
override

Decode all of the frames of an animated gif. For single image gifs, this will return an animation with a single frame.

Implementation

@override
Animation? decodeAnimation(List<int> bytes) {
  if (startDecode(bytes) == null) {
    return null;
  }

  final anim = Animation();
  anim.width = info!.width;
  anim.height = info!.height;
  anim.loopCount = _repeat;

  Image? lastImage = null;
  for (var i = 0; i < info!.numFrames; ++i) {
    final frame = info!.frames[i];
    final image = decodeFrame(i);
    if (image == null) {
      return null;
    }

    if (lastImage == null) {
      lastImage = image;
      lastImage.duration = frame.duration * 10; // Convert to MS
      anim.addFrame(lastImage);
      continue;
    }

    if (image.width == lastImage.width && image.height == lastImage.height &&
        frame.x == 0 && frame.y == 0 && frame.clearFrame) {
      lastImage = image;
      lastImage.duration = frame.duration * 10; // Convert to MS
      anim.addFrame(lastImage);
      continue;
    }

    if (frame.clearFrame) {
      lastImage = Image(lastImage.width, lastImage.height);
      final colorMap =
        (frame.colorMap != null) ? frame.colorMap : info!.globalColorMap;
      lastImage.fill(colorMap!.color(info!.backgroundColor));
    } else {
      lastImage = new Image.from(lastImage);
    }

    copyInto(lastImage, image, dstX: frame.x, dstY: frame.y);

    lastImage.duration = frame.duration * 10; // Convert 1/100 sec to ms.
    anim.addFrame(lastImage);
  }

  return anim;
}