Get the PNG image sizes from IHDR

Imagine to load a PNG file through an URLStream intance named stream, on the Event.COMPLETE event this script extract the IHDR information releated to the width and height of the image, more info about IHDR here.
var _width:Number = 0;
var _height:Number = 0;

var ba:ByteArray = new ByteArray();
stream.readBytes(fileData, 0, stream.bytesAvailable);

ba.position = 16
_width += this.readUnsignedByte() << 24;

ba.position = 17
_width += this.readUnsignedByte() << 16;

ba.position = 18
_width += this.readUnsignedByte() << 8;

ba.position = 19
_width += this.readUnsignedByte();

ba.position = 20
_height += this.readUnsignedByte() << 24;

ba.position = 21
_height += this.readUnsignedByte() << 16;

ba.position = 22
_height += this.readUnsignedByte() << 8;

ba.position = 23
_height += this.readUnsignedByte();

ba.position = 0;

return new Rectangle(0, 0, _width, _height);

Comments on this post

Post a comment

Embed any code between [code] [/code] tags
captcha image   Reload