-
Notifications
You must be signed in to change notification settings - Fork 8
/
ICO.bt
165 lines (140 loc) · 4.4 KB
/
ICO.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
//------------------------------------------------
//--- 010 Editor v2.0 Binary Template
//
// File: ICO.bt
// Authors: Amotz Getzov
// Version: 1.1
// Purpose: Defines a template for
// parsing icon image files.
// Category: Image
// File Mask: *.ico
// ID Bytes: 00 00 01 00
// History:
// 1.1 2016-01-28 SweetScape: Updated header for repository submission.
// 1.0 A Getzov: Initial release
//
// Based on BMP.bt from SweetScape Software
// Does not support PMG icon images
//------------------------------------------------
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved ( must be 0)
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // How many bytes in this resource?
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY <read=ReadIconDirEntry>;
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource Type (1 for icons)
WORD idCount; // How many images?
ICONDIRENTRY idEntries[idCount]; // An entry for each image (idCount of 'em)
} ICONDIR;
typedef struct { // rgbq
UBYTE rgbBlue;
UBYTE rgbGreen;
UBYTE rgbRed;
UBYTE rgbReserved;
} RGBQUAD <read=ReadRGBQUAD>;
typedef struct { // rgbt
UBYTE rgbBlue;
UBYTE rgbGreen;
UBYTE rgbRed;
} RGBTRIPLE <read=ReadRGBTRIPLE>;
typedef struct { // bmih
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct {
// Define the color table
if( (bmiHeader.biBitCount != 24) && (bmiHeader.biBitCount != 32) )
{
if( bmiHeader.biClrUsed > 0 )
RGBQUAD aColors[ bmiHeader.biClrUsed ];
else
RGBQUAD aColors[ 1 << bmiHeader.biBitCount ];
}
// Calculate bytes per line and padding required
local int bytesPerLine = (int)Ceil( bmiHeader.biWidth * bmiHeader.biBitCount / 8.0 );
local int padding = 4 - (bytesPerLine % 4);
if( padding == 4 )
padding = 0;
// Define each line of the image
struct BITMAPLINE {
// Define color data
if( bmiHeader.biBitCount < 8 )
UBYTE imageData[ bytesPerLine ];
else if( bmiHeader.biBitCount == 8 )
UBYTE colorIndex[ bmiHeader.biWidth ];
else if( bmiHeader.biBitCount == 24 )
RGBTRIPLE colors[ bmiHeader.biWidth ];
else if( bmiHeader.biBitCount == 32 )
RGBQUAD colors[ bmiHeader.biWidth ];
// Pad if necessary
if( padding != 0 )
UBYTE padBytes[ padding ];
} lines[ bmiHeader.biHeight/2 ]<optimize=false>;
// Define each line of the mask
struct MASKLINE {
UBYTE line[((bmiHeader.biWidth + 31)/32)*4]<format=hex>;
}mask[bmiHeader.biHeight/2]<optimize=false>;
}IMAGEDATA;
typedef struct {
SetBackColor( cLtAqua );
BITMAPINFOHEADER bmiHeader;
SetBackColor( cLtGray );
IMAGEDATA data;
}ICONIMAGE <read=ReadDIBHeader>;
//---------------------------------------------
// Custom read functions - this allows the data to be displayed without having to open up the structure.
string ReadIconDirEntry( ICONDIRENTRY &dirEntry )
{
string s;
SPrintf( s, "%dx%d %dbit %d colors", dirEntry.bWidth, dirEntry.bHeight, dirEntry.wBitCount, dirEntry.bColorCount );
return s;
}
string ReadDIBHeader( ICONIMAGE &image )
{
string s;
SPrintf( s, "%dx%d %dbit", image.bmiHeader.biWidth, image.bmiHeader.biHeight, image.bmiHeader.biBitCount );
return s;
}
string ReadRGBQUAD( RGBQUAD &a )
{
string s;
SPrintf( s, "#%02X%02X%02X%02X", a.rgbReserved, a.rgbRed, a.rgbGreen, a.rgbBlue );
return s;
}
string ReadRGBTRIPLE( RGBTRIPLE &a )
{
string s;
SPrintf( s, "#%02X%02X%02X", a.rgbRed, a.rgbGreen, a.rgbBlue );
return s;
}
//---------------------------------------------
// Define the headers
LittleEndian();
local short res = ReadShort( FTell() );
local short type = ReadShort(FTell() +2 );
//Check icon dir
if( res != 0 || type != 1)
{
Warning( "File is not an Icon. Template stopped." );
return -1;
}
SetBackColor( cNone );
ICONDIR icondir;
ICONIMAGE images[icondir.idCount] <optimize=false>;