Created
October 20, 2014 20:49
-
-
Save sethhall/ce197a8783aa97581207 to your computer and use it in GitHub Desktop.
very early gif parser for binpac++. probably doesn't even work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GIF; | |
import BinPAC; | |
type Header = unit { | |
%byteorder = BinPAC::ByteOrder::Little; | |
signature : bytes &length=3; #This needs to either be a literal b"GIF" or have a &check attribute when that exists | |
version : bytes &length=3; | |
}; | |
type LOGICALSCREENDESCRIPTOR = unit { | |
%byteorder = BinPAC::ByteOrder::Little; | |
width : uint16; | |
height : uint16; | |
packed_fields : bitfield(8) { | |
global_color_table_flag : 0; | |
color_resolution : 1..3; | |
sort_flag : 4; | |
size_of_global_color_table : 5..7; | |
}; | |
BackgroundColorIndex : uint8; | |
PixelAspectRatio : uint8; | |
}; | |
type RGB = unit { | |
%byteorder = BinPAC::ByteOrder::Little; | |
R : uint8; | |
G : uint8; | |
B : uint8; | |
}; | |
type DataSubBlock = unit { | |
%byteorder = BinPAC::ByteOrder::Little; | |
size : uint8; | |
data : bytes &length=self.size; | |
}; | |
uint64 pow(val: uint64, power: uint64) | |
{ | |
if (power > 1) | |
return val * pow(val, power - 1); | |
return val; | |
} | |
export type File = unit { | |
%byteorder = BinPAC::ByteOrder::Little; | |
header: Header; | |
logical_screen_descriptor: LOGICALSCREENDESCRIPTOR | |
{ | |
local ct_size = cast<uint64>(self.logical_screen_descriptor.packed_fields.size_of_global_color_table) + 1; | |
self.gct_size = pow(2, ct_size); | |
} | |
var gct_size: uint64; | |
global_color_table: list<RGB> &length=self.gct_size; | |
# data: bytes; | |
# trailer: 0x3b; | |
on %done { | |
print self; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment