Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created November 15, 2011 22:45
Show Gist options
  • Save dpeek/1368618 to your computer and use it in GitHub Desktop.
Save dpeek/1368618 to your computer and use it in GitHub Desktop.
Read JPG size in Neko
var b = bytes.get;
var s = bytes.readString;
var i = 0;
var l = bytes.length;
if (b(i) == 0xFF && b(i+1) == 0xD8 && b(i+2) == 0xFF && b(i+3) == 0xE0)
{
i += 4;
if (s(i+2,1) == "J" && s(i+3,1) == "F" && s(i+4,1) == "I" && s(i+5,1) == "F" && b(i+6) == 0x00)
{
var block = b(i) * 256 + b(i+1);
while (i < l)
{
// Increase the file index to get to the next block
i += block;
// Check for end of file
if (i > l) throw "EOF";
// Check that we are truly at the start of another block
if (b(i) != 0xFF) throw "Not really a block!";
// 0xFFC0 is the "Start of frame" marker which contains the file size
if (b(i+1) == 0xC0)
{
//The structure of the 0xFFC0 block is quite simple [0xFFC0][ushort length][uchar precision][ushort x][ushort y]
var height = b(i+5) * 256 + b(i+6);
var width = b(i+7) * 256 + b(i+8);
return {width:width, height:height};
}
else
{
// skip to next block marker
i += 2;
//Go to the next block
block = b(i) * 256 + b(i+1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment