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 != Jpeg.M_SOI) {
    return false;
  }

  var hasSOF = false;
  var hasSOS = false;

  marker = _nextMarker();
  while (marker != Jpeg.M_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 Jpeg.M_SOF0: // SOF0 (Start of Frame, Baseline DCT)
      case Jpeg.M_SOF1: // SOF1 (Start of Frame, Extended DCT)
      case Jpeg.M_SOF2: // SOF2 (Start of Frame, Progressive DCT)
        hasSOF = true;
        break;
      case Jpeg.M_SOS: // SOS (Start of Scan)
        hasSOS = true;
        break;
      default:
    }

    marker = _nextMarker();
  }

  return hasSOF && hasSOS;
}