-
Notifications
You must be signed in to change notification settings - Fork 9
/
TFtoTGA.1sc
216 lines (185 loc) · 3.94 KB
/
TFtoTGA.1sc
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
205
206
207
208
209
210
211
212
213
214
215
216
//------------------------------------------------
//--- 010 Editor v8.0.1 Script File
//
// File:
// Authors:
// Version:
// Purpose:
// Category:
// History:
//------------------------------------------------
local uchar Buffer[10485760]; // file buffer
local uint width, height;
local float size;
local int FileIndex;
local string FileName;
local ubyte MapCount, MaskCount, FileType;
string FileNameRemoveExt(string FileName)
{
return SubStr(FileName, 0, Strchr(FileName, '.'));
};
void CreateBMPFile(string Name, uchar Buffer[], uint type, uint width, uint height)
{
local uchar a, b, RGBA[4], Empty[50], Line[4092];
local float w, alignment, depth;
local int pos, i;
local float size;
local char bfType[2];
local WORD bfReserved1, bfReserved2, biPlanes, biBitCount;
local DWORD bfSize, bfOffBits, biSize, biWidth, biHeight, biCompression, biSizeImage, biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant, biRMask, biGMask, biBMask, biAMask;
FileNew("Hex");
depth = 16;
if (type != 3)
{
size = width*height*(depth/8);
w = width*(depth/8);
}
else
{
size = width*height;
w = width;
};
if (w % 4 > 0)
alignment = 4 - w % 4;
// BMP Header
bfType = "BM";
WriteBytes(bfType, 0, 2);
FSkip(2);
if (type == 3)
bfSize = size + 1078;
else
if (type >= 4)
bfSize = size + 54;
else
bfSize = size + 70;
WriteInt(FTell(), bfSize);
FSkip(4);
bfReserved1 = 0;
WriteShort(FTell(), bfReserved1 );
FSkip(2);
bfReserved2 = 0;
WriteShort(FTell(), bfReserved2 );
FSkip(2);
if (type == 3)
bfOffBits = 1078;
else
if (type >= 4)
bfOffBits = 54;
else
bfOffBits = 70;
WriteInt(FTell(), bfOffBits);
FSkip(4);
if (type >= 3)
biSize = 40;
else
biSize = 56;
WriteInt(FTell(), biSize);
FSkip(4);
biWidth = width;
WriteInt(FTell(), biWidth);
FSkip(4);
biHeight = -height;
WriteInt(FTell(), biHeight);
FSkip(4);
biPlanes = 1;
WriteShort(FTell(), biPlanes );
FSkip(2);
if (type == 3)
biBitCount = 8;
else
biBitCount = depth;
WriteInt(FTell(), biBitCount);
FSkip(2);
if (type >= 4)
biCompression = 0;
else
biCompression = 3;
WriteInt(FTell(), biCompression);
FSkip(4);
biSizeImage = size;
WriteInt(FTell(), biSizeImage);
FSkip(4);
biXPelsPerMeter = 3780;
WriteInt(FTell(), biXPelsPerMeter );
FSkip(4);
biYPelsPerMeter = 3780;
WriteInt(FTell(), biYPelsPerMeter);
FSkip(4);
biClrUsed = 0;
WriteInt(FTell(), biClrUsed);
FSkip(4);
biClrImportant = 0;
WriteInt(FTell(), biClrImportant);
FSkip(4);
if (type == 1) // 565
{
biRMask = 63488;
biGMask = 2016;
biBMask = 31;
biAMask = 0;
}
else // 4444
{
biRMask = 3840;
biGMask = 240;
biBMask = 15;
biAMask = 0;
};
if (type < 3) // 8 bit
{
WriteInt(FTell(), biRMask);
FSkip(4);
WriteInt(FTell(), biGMask);
FSkip(4);
WriteInt(FTell(), biBMask);
FSkip(4);
WriteInt(FTell(), biAMask);
FSkip(4);
}
else
if (type == 3)
{
WriteBytes(Palette, FTell(), 1024);
FSkip(1024);
};
if (alignment > 0)
for ( i = 0; i < height; ++i)
{
Memcpy(Line, Buffer, w, 0, pos);
WriteBytes(Line, FTell(), w);
FSkip(w);
WriteBytes(Empty, FTell(), alignment);
FSkip(alignment);
pos += w;
}
WriteBytes(Buffer, FTell(), size);
FileSave(Name + ".bmp");
FileClose();
};
void ReadTFFile()
{
FSkip(5);
FileType = ReadByte();
FSkip(1);
MapCount = ReadByte();
FSkip(1);
MaskCount = ReadByte();
FSkip(1);
width = ReadInt();
FSkip(4);
height = ReadInt();
FSkip(4);
size = height*width*2;
ReadBytes(Buffer, FTell(), size);
};
void TFtoBMP()
{
FileIndex = GetFileNum();
FileName = GetFileName();
ReadTFFile();
if (FileType)
CreateBMPFile(FileNameRemoveExt(FileName), Buffer, 4, width, height);
else
CreateBMPFile(FileNameRemoveExt(FileName), Buffer, 1, width, height);
}
TFtoBMP();