livePngSize function

({int height, int width})? livePngSize(
  1. List<int> png
)

The width and height in the header of png, or null when it isn't a PNG.

Implementation

({int width, int height})? livePngSize(List<int> png) {
  const List<int> signature = [137, 80, 78, 71, 13, 10, 26, 10];
  if (png.length < 24) return null;
  for (int i = 0; i < signature.length; i++) {
    if (png[i] != signature[i]) return null;
  }
  // The IHDR chunk comes first, with the width and height as 4-byte ints.
  int read(int at) =>
      (png[at] << 24) | (png[at + 1] << 16) | (png[at + 2] << 8) | png[at + 3];
  return (width: read(16), height: read(20));
}