PdfJpegInfo constructor

PdfJpegInfo(
  1. Uint8List image
)

Load a Jpeg image's metadata

Implementation

factory PdfJpegInfo(Uint8List image) {
  final buffer = image.buffer.asByteData(
    image.offsetInBytes,
    image.lengthInBytes,
  );

  int? width;
  int? height;
  int? color;
  var offset = 0;
  while (offset < buffer.lengthInBytes) {
    while (buffer.getUint8(offset) == 0xff) {
      offset++;
    }

    final mrkr = buffer.getUint8(offset);
    offset++;

    if (mrkr == 0xd8) {
      continue; // SOI
    }

    if (mrkr == 0xd9) {
      break; // EOI
    }

    if (0xd0 <= mrkr && mrkr <= 0xd7) {
      continue;
    }

    if (mrkr == 0x01) {
      continue; // TEM
    }

    final len = buffer.getUint16(offset);
    offset += 2;

    if (mrkr >= 0xc0 && mrkr <= 0xc2) {
      height = buffer.getUint16(offset + 1);
      width = buffer.getUint16(offset + 3);
      color = buffer.getUint8(offset + 5);
      break;
    }
    offset += len - 2;
  }

  if (height == null) {
    throw 'Unable to find a Jpeg image in the file';
  }

  final tags = _findExifInJpeg(buffer);

  return PdfJpegInfo._(width, height, color, tags);
}