-
Notifications
You must be signed in to change notification settings - Fork 31
/
gif-orig.bt
204 lines (197 loc) · 6 KB
/
gif-orig.bt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//------------------------------------------------
//--- 010 Editor v2.0 Binary Template
//
// File: GIF.bt
// Authors: Berend-Jan "SkyLined" Wever
// Version: 1.3
// Purpose: Defines a template for
// parsing GIF image files.
// Category: Image
// File Mask: *.gif
// ID Bytes: 47 49 46 //GIF
// History:
// 1.3 2016-01-28 SweetScape: Updated header for repository submission.
// 1.2 2007-09-25 SweetScape: Added bug fixes for reading local color table and
// changed ReadShort to ReadUShort.
// 1.1 2007-05-02 SweetScape: Added bug fixes for reading global color table.
// 1.0 BJ Wever: Initial release.
//------------------------------------------------
// From GIF89a Specification:
//<GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
//<Logical Screen> ::= Logical Screen Descriptor [Global Color Table]
//<Data> ::= <Graphic Block> | <Special-Purpose Block>
//<Graphic Block> ::= [Graphic Control Extension] <Graphic-Rendering Block>
//<Graphic-Rendering Block> ::= <Table-Based Image> | Plain Text Extension
//<Table-Based Image> ::= Image Descriptor [Local Color Table] Image Data
//<Special-Purpose Block> ::= Application Extension | Comment Extension
LittleEndian();
typedef struct {
UBYTE R;
UBYTE G;
UBYTE B;
} RGB <read=ReadRGB>;
string ReadRGB( RGB &color )
{
string s;
SPrintf( s, "#%02X%02X%02X", color.R, color.G, color.B );
return s;
}
typedef struct {
local UBYTE size = ReadUByte(FTell());
while (size != 0) {
struct DATASUBBLOCK {
UBYTE Size;
char Data[size];
} DataSubBlock;
size = ReadUByte(FTell());
}
UBYTE BlockTerminator;
} DATASUBBLOCKS;
typedef struct {
char Signature[3];
char Version[3];
} GIFHEADER;
typedef struct {
ushort Width;
ushort Height;
BitfieldLeftToRight();
struct LOGICALSCREENDESCRIPTOR_PACKEDFIELDS {
UBYTE GlobalColorTableFlag : 1;
UBYTE ColorResolution : 3;
UBYTE SortFlag : 1;
UBYTE SizeOfGlobalColorTable : 3;
} PackedFields;
UBYTE BackgroundColorIndex;
UBYTE PixelAspectRatio;
} LOGICALSCREENDESCRIPTOR;
//<GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
// Header
SetBackColor( 0xFFFFFF );
GIFHEADER GifHeader;
if( GifHeader.Signature != "GIF" )
{
Warning( "File is not a valid GIF. Template stopped." );
return -1;
}
//<Logical Screen> ::= Logical Screen Descriptor [Global Color Table]
// Logical Screen Descriptor
SetBackColor( 0xE0E0E0 );
LOGICALSCREENDESCRIPTOR LogicalScreenDescriptor;
// [Global Color Table]
if (LogicalScreenDescriptor.PackedFields.GlobalColorTableFlag == 1) {
SetBackColor( 0xC0C0C0 );
struct GLOBALCOLORTABLE {
local int i;
local int size = 1;
for (i = 0; i <= LogicalScreenDescriptor.PackedFields.SizeOfGlobalColorTable; i++) {
size *= 2;
}
RGB rgb[size];
} GlobalColorTable <optimize=false>;;
}
//<Data> ::= <Graphic Block> | <Special-Purpose Block>
SetBackColor( 0xFFFFFF );
struct DATA {
while (ReadUByte(FTell()) != 0x3B) {
// <Graphic Block> ::= [Graphic Control Extension] <Graphic-Rendering Block>
if (ReadUByte(FTell()) == 0x2C) {
SetBackColor( 0xE0FFE0 );
struct IMAGEDESCRIPTOR {
UBYTE ImageSeperator;
ushort ImageLeftPosition;
ushort ImageTopPosition;
ushort ImageWidth;
ushort ImageHeight;
struct IMAGEDESCRIPTOR_PACKEDFIELDS {
UBYTE LocalColorTableFlag : 1;
UBYTE InterlaceFlag : 1;
UBYTE SortFlag : 1;
UBYTE Reserved : 2;
UBYTE SizeOfLocalColorTable : 3;
} PackedFields;
} ImageDescriptor;
if (ImageDescriptor.PackedFields.LocalColorTableFlag == 1) {
SetBackColor( 0xC0FFC0 );
struct LOCALCOLORTABLE {
local int i;
local int size = 1;
for (i = 0; i <= ImageDescriptor.PackedFields.SizeOfLocalColorTable; i++) {
size *= 2;
}
RGB rgb[size];
} LocalColorTable <optimize=false>;;
}
SetBackColor( 0xA0FFA0 );
struct IMAGEDATA {
UBYTE LZWMinimumCodeSize;
DATASUBBLOCKS DataSubBlocks;
} ImageData;
} else if (ReadUShort(FTell()) == 0xF921) {
SetBackColor( 0xC0FFFF );
struct GRAPHICCONTROLEXTENSION {
UBYTE ExtensionIntroducer; // 0x21
UBYTE GraphicControlLabel; // 0xF9
struct GRAPHICCONTROLSUBBLOCK {
UBYTE BlockSize;
struct GRAPHICCONTROLEXTENSION_DATASUBBLOCK_PACKEDFIELDS {
UBYTE Reserved : 3;
UBYTE DisposalMethod : 3;
UBYTE UserInputFlag : 1;
UBYTE TransparentColorFlag : 1;
} PackedFields;
ushort DelayTime;
UBYTE TransparentColorIndex;
} GraphicControlSubBlock;
UBYTE BlockTerminator;
} GraphicControlExtension;
} else if (ReadUShort(FTell()) == 0xFE21) {
SetBackColor( 0xFFFFC0 );
struct COMMENTEXTENSION {
UBYTE ExtensionIntroducer; // 0x21
UBYTE CommentLabel; // 0xFE
DATASUBBLOCKS CommentData;
} CommentExtension;
} else if (ReadUShort(FTell()) == 0x0121) {
SetBackColor( 0xC0C0C0 );
struct PLAINTEXTEXTENTION {
UBYTE ExtensionIntroducer; // 0x21
UBYTE PlainTextLabel; // 0x01
struct PLAINTEXTSUBBLOCK {
UBYTE BlockSize;
ushort TextGridLeftPosition;
ushort TextGridTopPosition;
ushort TextGridWidth;
ushort TextGridHeight;
UBYTE CharacterCellWidth;
UBYTE CharacterCellHeight;
UBYTE TextForegroundColorIndex;
UBYTE TextBackgroundColorIndex;
} PlainTextSubBlock;
DATASUBBLOCKS PlainTextData;
} PlainTextExtension;
} else if (ReadUShort(FTell()) == 0xFF21) {
SetBackColor( 0xC0C0FF );
struct APPLICATIONEXTENTION {
UBYTE ExtensionIntroducer; // 0x21
UBYTE ApplicationLabel; // 0xFF
struct APPLICATIONSUBBLOCK {
UBYTE BlockSize;
char ApplicationIdentifier[8];
char ApplicationAuthenticationCode[3];
} ApplicationSubBlock;
DATASUBBLOCKS ApplicationData;
} ApplicationExtension;
} else {
SetBackColor( 0xFF8080 );
struct UNDEFINEDDATA {
UBYTE ExtensionIntroducer; // 21
UBYTE Label; // F9
DATASUBBLOCKS DataSubBlocks;
} UndefinedData;
}
}
} Data;
SetBackColor( 0xFFFFFF );
struct TRAILER {
UBYTE GIFTrailer;
} Trailer;