getPngDimensions method
Extracts width and height from a PNG file header. Throws an exception if the file is not a valid PNG or if dimensions cannot be read.
Implementation
Size getPngDimensions(Uint8List bytes) {
// Check for PNG signature
if (bytes.length < 24 ||
bytes[0] != 0x89 || bytes[1] != 0x50 || bytes[2] != 0x4E || bytes[3] != 0x47) {
throw Exception('Not a valid PNG file');
}
final width = (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19];
final height = (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23];
return Size(width.toDouble(), height.toDouble());
}