<commit_before>part of image;


class TiffDecoder extends Decoder {
  TiffInfo info;

  /**
   * Is the given file a valid TIFF image?
   */
  bool isValidFile(List<int> data) {
    return _readHeader(new Buffer(data)) != null;
  }

  /**
   * Validate the file is a Gif image and get information about it.
   * If the file is not a valid Gif image, null is returned.
   */
  TiffInfo startDecode(List<int> bytes) {
    Buffer ptr = new Buffer(new Uint8List.fromList(bytes));
    info = _readHeader(ptr);
    return info;
  }

  Image decodeImage(List<int> data, {int frame: 0}) {
    Buffer ptr = new Buffer(new Uint8List.fromList(data));

    TiffInfo info = _readHeader(ptr);
    if (info == null) {
      return null;
    }

    return info.images[frame].decode(ptr);
  }

  Animation decodeAnimation(List<int> data) {
    Image image = decodeImage(data);
    if (image == null) {
      return null;
    }

    Animation anim = new Animation();
    anim.addFrame(image);

    return anim;
  }

  /**
   * Read the TIFF header and IFD blocks.
   */
  TiffInfo _readHeader(Buffer p) {
    TiffInfo info = new TiffInfo();
    info.byteOrder = p.readUint16();
    if (info.byteOrder != TIFF_LITTLE_ENDIAN &&
        info.byteOrder != TIFF_BIG_ENDIAN) {
      return null;
    }

    if (info.byteOrder == TIFF_BIG_ENDIAN) {
      p.byteOrder = BIG_ENDIAN;
      info.byteOrder = BIG_ENDIAN;
    } else {
      p.byteOrder = LITTLE_ENDIAN;
      info.byteOrder = LITTLE_ENDIAN;
    }

    info.signature = p.readUint16();
    if (info.signature != TIFF_SIGNATURE) {
      return null;
    }

    int offset = p.readUint32();
    info.ifdOffset = offset;

    Buffer p2 = new Buffer.from(p);
    p2.offset = offset;

    while (offset != 0) {
      TiffImage img;
      try {
        img = new TiffImage(p2);
        if (!img.isValid) {
          break;
        }
      } catch (error) {
        break;
      }
      info.images.add(img);
      if (info.images.length == 1) {
        info.width = info.images[0].width;
        info.height = info.images[0].height;
      }

      offset = p2.readUint32();
      if (offset != 0) {
        p2.offset = offset;
      }
    }

    return info.images.length > 0 ? info : null;
  }

  static const int TIFF_SIGNATURE = 42;
  static const int TIFF_LITTLE_ENDIAN = 0x4949;<commit_msg>Add animation support to TiffDecoder.  Animation, in a tiff, indicates multiple pages of a document image.<commit_after>part of image;


class TiffDecoder extends Decoder {
  TiffInfo info;

  /**
   * Is the given file a valid TIFF image?
   */
  bool isValidFile(List<int> data) {
    return _readHeader(new Buffer(data)) != null;
  }

  /**
   * Validate the file is a Gif image and get information about it.
   * If the file is not a valid Gif image, null is returned.
   */
  TiffInfo startDecode(List<int> bytes) {
    _input = new Buffer(new Uint8List.fromList(bytes));
    info = _readHeader(_input);
    return info;
  }

  /**
   * How many frames are available to be decoded.  [startDecode] should have
   * been called first. Non animated image files will have a single frame.
   */
  int numFrames() => info != null ? info.images.length : 0;

  /**
   * Decode a single frame from the data stat was set with [startDecode].
   * If [frame] is out of the range of available frames, null is returned.
   * Non animated image files will only have [frame] 0.  An [AnimationFrame]
   * is returned, which provides the image, and top-left coordinates of the
   * image, as animated frames may only occupy a subset of the canvas.
   */
  Image decodeFrame(int frame) {
    if (info == null) {
      return null;
    }

    return info.images[frame].decode(_input);
  }

  /**
   * Decode the file and extract a single image from it.  If the file is
   * animated, the specified [frame] will be decoded.  If there was a problem
   * decoding the file, null is returned.
   */
  Image decodeImage(List<int> data, {int frame: 0}) {
    Buffer ptr = new Buffer(new Uint8List.fromList(data));

    TiffInfo info = _readHeader(ptr);
    if (info == null) {
      return null;
    }

    return info.images[frame].decode(ptr);
  }

  /**
   * 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.
   */
  Animation decodeAnimation(List<int> data) {
    if (startDecode(data) == null) {
      return null;
    }

    Animation anim = new Animation();
    for (int i = 0, len = numFrames(); i < len; ++i) {
      Image image = decodeFrame(i);
      if (i == null) {
        continue;
      }
      anim.addFrame(image);
    }

    return anim;
  }

  /**
   * Read the TIFF header and IFD blocks.
   */
  TiffInfo _readHeader(Buffer p) {
    TiffInfo info = new TiffInfo();
    info.byteOrder = p.readUint16();
    if (info.byteOrder != TIFF_LITTLE_ENDIAN &&
        info.byteOrder != TIFF_BIG_ENDIAN) {
      return null;
    }

    if (info.byteOrder == TIFF_BIG_ENDIAN) {
      p.byteOrder = BIG_ENDIAN;
      info.byteOrder = BIG_ENDIAN;
    } else {
      p.byteOrder = LITTLE_ENDIAN;
      info.byteOrder = LITTLE_ENDIAN;
    }

    info.signature = p.readUint16();
    if (info.signature != TIFF_SIGNATURE) {
      return null;
    }

    int offset = p.readUint32();
    info.ifdOffset = offset;

    Buffer p2 = new Buffer.from(p);
    p2.offset = offset;

    while (offset != 0) {
      TiffImage img;
      try {
        img = new TiffImage(p2);
        if (!img.isValid) {
          break;
        }
      } catch (error) {
        break;
      }
      info.images.add(img);
      if (info.images.length == 1) {
        info.width = info.images[0].width;
        info.height = info.images[0].height;
      }

      offset = p2.readUint32();
      if (offset != 0) {
        p2.offset = offset;
      }
    }

    return info.images.length > 0 ? info : null;
  }

  Buffer _input;

  static const int TIFF_SIGNATURE = 42;
  static const int TIFF_LITTLE_ENDIAN = 0x4949;
{'fraction_non_alphanumeric': 0.11203181968843222, 'fraction_numerical': 0.010109380178985748, 'mean_word_length': 2.35650723025584, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '4605424', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1963, 'n_words': 584}