<commit_before>import 'dart:typed_data';

import '../animation.dart';
import '../image.dart';
import '../transform/copy_into.dart';
import '../util/input_buffer.dart';
import 'decoder.dart';
import 'gif/gif_color_map.dart';
import 'gif/gif_image_desc.dart';
import 'gif/gif_info.dart';

/// A decoder for the GIF image format. This supports both single frame and
/// animated GIF files, and transparency.
class GifDecoder extends Decoder {
  GifInfo info;

  GifDecoder([List<int> bytes]) {
    if (bytes != null) {
      startDecode(bytes);
    }
  }

  /// Is the given file a valid Gif image?
  @override
  bool isValidFile(List<int> bytes) {
    _input = InputBuffer(bytes);
    info = GifInfo();
    return _getInfo();
  }

  /// How many frames are available to decode?
  ///
  /// You should have prepared the decoder by either passing the file bytes
  /// to the constructor, or calling getInfo.
  @override
  int numFrames() => (info != null) ? info.numFrames : 0;

  /// Validate the file is a Gif image and get information about it.
  /// If the file is not a valid Gif image, null is returned.
  @override
  GifInfo startDecode(List<int> bytes) {
    _input = InputBuffer(bytes);

    info = GifInfo();
    if (!_getInfo()) {
      return null;
    }

    try {
      while (!_input.isEOS) {
        var recordType = _input.readByte();
        switch (recordType) {
          case IMAGE_DESC_RECORD_TYPE:
            var gifImage = _skipImage();
            if (gifImage == null) {
              return info;
            }
            info.frames.add(gifImage);
            break;
          case EXTENSION_RECORD_TYPE:
            var extCode = _input.readByte();
            if (extCode == APPLICATION_EXT) {
              _readApplicationExt(_input);
            } else if (extCode == GRAPHIC_CONTROL_EXT) {
              _readGraphicsControlExt(_input);
            } else {
              _skipRemainder();
            }
            break;
          case TERMINATE_RECORD_TYPE:
            //_numFrames = info.numFrames;
            return info;
          default:
            break;
        }
      }
    } catch (error) {
      print(error);
    }

    //_numFrames = info.numFrames;
    return info;
  }

  void _readApplicationExt(InputBuffer _input) {
    var blockSize = _input.readByte();
    var tag = _input.readString(blockSize);
    if (tag == 'NETSCAPE2.0') {
      var b1 = _input.readByte();
      var b2 = _input.readByte();
      if (b1 == 0x03 && b2 == 0x01) {
        _repeat = _input.readUint16();
      }
    } else {
      _skipRemainder();
    }
  }

  void _readGraphicsControlExt(InputBuffer _input) {
    /*int blockSize =*/ _input.readByte();
    var b = _input.readByte();
    var duration = _input.readUint16();
    var transparent = _input.readByte();
    /*int endBlock =*/ _input.readByte();
    var disposalMethod = (b >> 3) & 0x7;
    //int userInput = (b >> 1) & 0x1;
    var transparentFlag = b & 0x1;

    var recordType = _input.peekBytes(1)[0];
    if (recordType == IMAGE_DESC_RECORD_TYPE) {
      _input.skip(1);
      var gifImage = _skipImage();
      if (gifImage == null) {<commit_msg>Fixed GIF Disposal Method parsing<commit_after>import 'dart:typed_data';

import '../animation.dart';
import '../image.dart';
import '../transform/copy_into.dart';
import '../util/input_buffer.dart';
import 'decoder.dart';
import 'gif/gif_color_map.dart';
import 'gif/gif_image_desc.dart';
import 'gif/gif_info.dart';

/// A decoder for the GIF image format. This supports both single frame and
/// animated GIF files, and transparency.
class GifDecoder extends Decoder {
  GifInfo info;

  GifDecoder([List<int> bytes]) {
    if (bytes != null) {
      startDecode(bytes);
    }
  }

  /// Is the given file a valid Gif image?
  @override
  bool isValidFile(List<int> bytes) {
    _input = InputBuffer(bytes);
    info = GifInfo();
    return _getInfo();
  }

  /// How many frames are available to decode?
  ///
  /// You should have prepared the decoder by either passing the file bytes
  /// to the constructor, or calling getInfo.
  @override
  int numFrames() => (info != null) ? info.numFrames : 0;

  /// Validate the file is a Gif image and get information about it.
  /// If the file is not a valid Gif image, null is returned.
  @override
  GifInfo startDecode(List<int> bytes) {
    _input = InputBuffer(bytes);

    info = GifInfo();
    if (!_getInfo()) {
      return null;
    }

    try {
      while (!_input.isEOS) {
        var recordType = _input.readByte();
        switch (recordType) {
          case IMAGE_DESC_RECORD_TYPE:
            var gifImage = _skipImage();
            if (gifImage == null) {
              return info;
            }
            info.frames.add(gifImage);
            break;
          case EXTENSION_RECORD_TYPE:
            var extCode = _input.readByte();
            if (extCode == APPLICATION_EXT) {
              _readApplicationExt(_input);
            } else if (extCode == GRAPHIC_CONTROL_EXT) {
              _readGraphicsControlExt(_input);
            } else {
              _skipRemainder();
            }
            break;
          case TERMINATE_RECORD_TYPE:
            //_numFrames = info.numFrames;
            return info;
          default:
            break;
        }
      }
    } catch (error) {
      print(error);
    }

    //_numFrames = info.numFrames;
    return info;
  }

  void _readApplicationExt(InputBuffer _input) {
    var blockSize = _input.readByte();
    var tag = _input.readString(blockSize);
    if (tag == 'NETSCAPE2.0') {
      var b1 = _input.readByte();
      var b2 = _input.readByte();
      if (b1 == 0x03 && b2 == 0x01) {
        _repeat = _input.readUint16();
      }
    } else {
      _skipRemainder();
    }
  }

  void _readGraphicsControlExt(InputBuffer _input) {
    /*int blockSize =*/ _input.readByte();
    var b = _input.readByte();
    var duration = _input.readUint16();
    var transparent = _input.readByte();
    /*int endBlock =*/ _input.readByte();
    var disposalMethod = (b >> 2) & 0x7;
    //int userInput = (b >> 1) & 0x1;
    var transparentFlag = b & 0x1;

    var recordType = _input.peekBytes(1)[0];
    if (recordType == IMAGE_DESC_RECORD_TYPE) {
      _input.skip(1);
      var gifImage = _skipImage();
      if (gifImage == null) {
{'fraction_non_alphanumeric': 0.1223842739378567, 'fraction_numerical': 0.008877615726062143, 'mean_word_length': 2.2139582272032605, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '341961', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1980, 'n_words': 520}