validate method

bool validate(
  1. List<int> bytes
)

Implementation

bool validate(List<int> bytes) {
  input = InputBuffer(bytes, bigEndian: true);

  // Some other formats have embedded jpeg, or jpeg-like data.
  // Only validate if the image starts with the StartOfImage tag.
  final soiCheck = input.peekBytes(2);
  if (soiCheck[0] != 0xff || soiCheck[1] != 0xd8) {
    return false;
  }

  var marker = _nextMarker();
  if (marker != JpegMarker.soi) {
    return false;
  }

  var hasSOF = false;
  var hasSOS = false;

  marker = _nextMarker();
  while (marker != JpegMarker.eoi && !input.isEOS) {
    // EOI (End of image)
    final sectionByteSize = input.readUint16();
    if (sectionByteSize < 2) {
      // jpeg section consists of more than 2 bytes at least
      // return success only when SOF and SOS have already found (as a jpeg
      // without EOF.)
      break;
    }
    input.offset += sectionByteSize - 2;

    switch (marker) {
      case JpegMarker.sof0: // SOF0 (Start of Frame, Baseline DCT)
      case JpegMarker.sof1: // SOF1 (Start of Frame, Extended DCT)
      case JpegMarker.sof2: // SOF2 (Start of Frame, Progressive DCT)
        hasSOF = true;
        break;
      case JpegMarker.sos: // SOS (Start of Scan)
        hasSOS = true;
        break;
      default:
    }

    marker = _nextMarker();
  }

  return hasSOF && hasSOS;
}