decodeAnimation method

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

Decode all of the frames from an animation. If the file is not an animation, a single frame animation is returned. If there was a problem decoding the file, null is returned.

Implementation

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

  final anim = Animation();
  anim.width = _info!.width;
  anim.height = _info!.height;

  if (!_info!.isAnimated) {
    final image = decodeFrame(0)!;
    anim.addFrame(image);
    return anim;
  }

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

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

    if (image.width == lastImage.width && image.height == lastImage.height &&
        frame.xOffset == 0 && frame.yOffset == 0 &&
        frame.blend == PngFrame.APNG_BLEND_OP_SOURCE) {
      lastImage = image;
      lastImage.duration = (frame.delay * 1000).toInt(); // Convert to MS
      anim.addFrame(lastImage);
      continue;
    }

    final dispose = frame.dispose;
    if (dispose == PngFrame.APNG_DISPOSE_OP_BACKGROUND) {
      lastImage = Image(lastImage.width, lastImage.height);
      lastImage.fill(_info!.backgroundColor);
    } else if (dispose == PngFrame.APNG_DISPOSE_OP_PREVIOUS) {
      lastImage = Image.from(lastImage);
    } else {
      lastImage = Image.from(lastImage);
    }

    lastImage.duration = (frame.delay * 1000).toInt(); // Convert to MS

    copyInto(lastImage, image,
        dstX: frame.xOffset,
        dstY: frame.yOffset,
        blend: frame.blend == PngFrame.APNG_BLEND_OP_OVER);

    anim.addFrame(lastImage);
  }

  return anim;
}