Skip to content

Commit f96e790

Browse files
committed
- added '-sky2tga' && '-bg2tim' to Image tool
1 parent 239d5e5 commit f96e790

5 files changed

Lines changed: 258 additions & 46 deletions

File tree

Driver2CutsceneTool/cutscene_tool.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,20 @@ void PackCutsceneFile(const char* foldername)
317317
char* bufptr = (char*)sheader;
318318
char* pingBufferPtr = bufptr + sizeof(PLAYBACKCAMERA) * MAX_REPLAY_CAMERAS;
319319

320+
// copy all pings to new position and remove deleted car pings too
321+
/*for (int j = 0; j < MAX_REPLAY_PINGS; j++)
322+
{
323+
PING_PACKET packet = *(PING_PACKET*)pingBufferPtr;
324+
325+
if(packet.carId != -1 && packet.frame != 0xffff)
326+
{
327+
*(PING_PACKET*)bufptr = packet;
328+
bufptr += sizeof(PING_PACKET);
329+
}
330+
331+
pingBufferPtr += sizeof(PING_PACKET);
332+
}*/
333+
320334
memmove(bufptr, pingBufferPtr, sizeof(PING_PACKET) * MAX_REPLAY_PINGS);
321335

322336
// shrink size

DriverImageTool/image_tool.cpp

Lines changed: 192 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "util/util.h"
99

1010
#include <string.h>
11+
#include "math/Vector.h"
1112

1213
struct RECT16
1314
{
@@ -42,10 +43,55 @@ void ExportSkyImage(const char* skyFileName, ubyte* data, int x_idx, int y_idx,
4243
CopyTpageImage((ushort*)data, (ushort*)imageClut, clut_x, clut_y, 16, 1);
4344

4445
SaveTIM_4bit(varargs("%s_%d_%d.TIM", skyFileName, sky_id, n),
45-
imageCopy, SKY_SIZE_W * SKY_SIZE_H, 0, 0, SKY_SIZE_W*2, SKY_SIZE_H, (ubyte*)imageClut, 1);
46+
imageCopy, 64 * SKY_SIZE_H, 0, 0, SKY_SIZE_W*2, SKY_SIZE_H, (ubyte*)imageClut, 1);
4647
}
4748

48-
void ConvertSky(const char* skyFileName)
49+
void ConvertIndexedSkyImage(uint* color_data, ubyte* src_indexed, int x_idx, int y_idx, bool outputBGR, bool originalTransparencyKey)
50+
{
51+
ushort imageClut[16];
52+
53+
int clut_x = x_idx * 16;
54+
int clut_y = SKY_CLUT_START_Y + y_idx;
55+
56+
CopyTpageImage((ushort*)src_indexed, (ushort*)imageClut, clut_x, clut_y, 16, 1);
57+
58+
int ox = x_idx * SKY_SIZE_W * 2;
59+
int oy = y_idx * SKY_SIZE_H;
60+
int w = SKY_SIZE_W * 2;
61+
int h = SKY_SIZE_H;
62+
63+
int tp_wx = ox + w;
64+
int tp_hy = oy + h;
65+
66+
for (int y = oy; y < tp_hy; y++)
67+
{
68+
for (int x = ox; x < tp_wx; x++)
69+
{
70+
ubyte clindex = src_indexed[y * 256 + x / 2];
71+
72+
if (0 != (x & 1))
73+
clindex >>= 4;
74+
75+
clindex &= 0xF;
76+
77+
// flip texture by Y
78+
int ypos = (256 - y - 1) * 512;
79+
80+
if (outputBGR)
81+
{
82+
TVec4D<ubyte> color = rgb5a1_ToBGRA8(imageClut[clindex], originalTransparencyKey);
83+
color_data[ypos + x] = *(uint*)(&color);
84+
}
85+
else
86+
{
87+
TVec4D<ubyte> color = rgb5a1_ToRGBA8(imageClut[clindex], originalTransparencyKey);
88+
color_data[ypos + x] = *(uint*)(&color);
89+
}
90+
}
91+
}
92+
}
93+
94+
void ConvertSky(const char* skyFileName, bool saveTGA)
4995
{
5096
FILE* fp = fopen(skyFileName, "rb");
5197
if(!fp)
@@ -54,8 +100,6 @@ void ConvertSky(const char* skyFileName)
54100
return;
55101
}
56102

57-
MsgInfo("Converting '%s' to separate TIM files...", skyFileName);
58-
59103
fseek(fp, 0, SEEK_END);
60104
int size = ftell(fp);
61105
fseek(fp, 0, SEEK_SET);
@@ -67,18 +111,139 @@ void ConvertSky(const char* skyFileName)
67111

68112
const int OFFSET_STEP = 0x10000;
69113

114+
#define SKY_TEX_CHANNELS 4
115+
#define SKY_TEXPAGE_SIZE (512*256)
116+
117+
uint* color_data;
118+
int imgSize = SKY_TEXPAGE_SIZE * SKY_TEX_CHANNELS;
119+
120+
if(saveTGA)
121+
{
122+
color_data = (uint*)malloc(imgSize);
123+
124+
MsgInfo("Converting '%s' to separata TGAs...\n", skyFileName);
125+
}
126+
else
127+
{
128+
MsgInfo("Converting '%s' to separate TIM files...\n", skyFileName);
129+
}
130+
131+
// 5 skies in total
70132
for(int i = 0; i < 5; i++)
71133
{
72134
int n = 0;
73135
ubyte* skyImage = data + i * OFFSET_STEP;
74136

137+
if (saveTGA)
138+
{
139+
memset(color_data, 0, imgSize);
140+
}
141+
142+
// 3x4 images (128x84 makes 256x252 tpage)
75143
for (int y = 0; y < 3; y++)
76144
{
77145
for (int x = 0; x < 4; x++, n++)
78-
ExportSkyImage(skyFileName, skyImage, x, y, i, n);
146+
{
147+
if(saveTGA)
148+
{
149+
ConvertIndexedSkyImage(color_data, skyImage, x, y, true, true);
150+
}
151+
else
152+
{
153+
ExportSkyImage(skyFileName, skyImage, x, y, i, n);
154+
}
155+
}
156+
}
157+
158+
if(saveTGA)
159+
{
160+
SaveTGA(varargs("%s_%d.TGA", skyFileName, i), (ubyte*)color_data, 512, 256, SKY_TEX_CHANNELS);
79161
}
80162
}
81163

164+
if (saveTGA)
165+
{
166+
free(color_data);
167+
}
168+
169+
MsgInfo("Done!");
170+
}
171+
172+
//-----------------------------------------------------
173+
174+
void ExportExtraImage(const char* filename, ubyte* data, ubyte* clut_data, int n)
175+
{
176+
ubyte imageCopy[64 * 219];
177+
ushort imageClut[16];
178+
179+
// size is 64x219. palette goes after it
180+
181+
//CopyTpageImage((ushort*)data, (ushort*)imageCopy, 0, 0, 64, 219);
182+
//CopyTpageImage((ushort*)data, (ushort*)imageClut, clut_x, clut_y, 16, 1);
183+
184+
SaveTIM_4bit(varargs("%s_%d.TIM", filename, n),
185+
data, 64 * 2 * 219, 0, 0, 64 * 4, 219, clut_data, 1);
186+
}
187+
188+
// In frontend, extra poly has a preview image drawn
189+
void ConvertBackgroundRaw(const char* filename, const char* extraFilename)
190+
{
191+
FILE* bgFp = fopen(filename, "rb");
192+
193+
if (!bgFp)
194+
{
195+
MsgError("Unable to open '%s'\n", filename);
196+
return;
197+
}
198+
199+
MsgInfo("Converting background '%s' to separate TIM files...", filename);
200+
201+
// read background file
202+
fseek(bgFp, 0, SEEK_END);
203+
int bgSize = ftell(bgFp);
204+
fseek(bgFp, 0, SEEK_SET);
205+
206+
ubyte* bgData = (ubyte*)malloc(bgSize);
207+
fread(bgData, bgSize, 1, bgFp);
208+
209+
fclose(bgFp);
210+
211+
// load extra file if specified
212+
if(extraFilename)
213+
{
214+
FILE* fp = fopen(extraFilename, "rb");
215+
if (!fp)
216+
{
217+
free(bgData);
218+
MsgError("Unable to open '%s'\n", extraFilename);
219+
return;
220+
}
221+
222+
MsgInfo("Converting '%s' to separate TIM files...", extraFilename);
223+
224+
fseek(fp, 0, SEEK_END);
225+
int size = ftell(fp);
226+
fseek(fp, 0, SEEK_SET);
227+
228+
ubyte* data = (ubyte*)malloc(size);
229+
230+
fread(data, size, 1, fp);
231+
fclose(fp);
232+
233+
const int OFFSET_STEP = 0x8000;
234+
235+
for (int i = 0; i < 10; i++)
236+
{
237+
ubyte* imageAddr = data + i * OFFSET_STEP;
238+
239+
ExportExtraImage(extraFilename, imageAddr, bgData + 11*0x8000, i);
240+
}
241+
242+
free(data);
243+
}
244+
245+
free(bgData);
246+
82247
MsgInfo("Done!");
83248
}
84249

@@ -88,6 +253,8 @@ void PrintCommandLineArguments()
88253
{
89254
MsgInfo("Example usage:\n");
90255
MsgInfo("\tDriverImageTool -sky2tim SKY0.RAW\n");
256+
MsgInfo("\tDriverImageTool -sky2tga SKY1.RAW\n");
257+
MsgInfo("\tDriverImageTool -bg2tim CARBACK.RAW <CCARS.RAW>\n");
91258
}
92259

93260
int main(int argc, char** argv)
@@ -109,10 +276,29 @@ int main(int argc, char** argv)
109276
if (!stricmp(argv[i], "-sky2tim"))
110277
{
111278
if (i + 1 <= argc)
112-
ConvertSky(argv[i + 1]);
279+
ConvertSky(argv[i + 1], false);
113280
else
114281
MsgWarning("-sky2tim must have an argument!");
115282
}
283+
else if (!stricmp(argv[i], "-sky2tga"))
284+
{
285+
if (i + 1 <= argc)
286+
ConvertSky(argv[i + 1], true);
287+
else
288+
MsgWarning("-sky2tga must have an argument!");
289+
}
290+
else if (!stricmp(argv[i], "-bg2tim"))
291+
{
292+
if (i + 1 <= argc)
293+
{
294+
if (i + 2 <= argc)
295+
ConvertBackgroundRaw(argv[i + 1], argv[i + 2]);
296+
else
297+
ConvertBackgroundRaw(argv[i + 1], nullptr);
298+
}
299+
else
300+
MsgWarning("-bg2tim must have an argument!");
301+
}
116302
}
117303

118304
return 0;

DriverLevelTool/driver_routines/textures.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,6 @@
1111

1212
//-------------------------------------------------------------------------------
1313

14-
// 16 bit color to BGRA
15-
// originalTransparencyKey makes it pink
16-
TVec4D<ubyte> rgb5a1_ToBGRA8(ushort color, bool originalTransparencyKey /*= true*/)
17-
{
18-
ubyte r = (color & 0x1F) * 8;
19-
ubyte g = ((color >> 5) & 0x1F) * 8;
20-
ubyte b = ((color >> 10) & 0x1F) * 8;
21-
22-
ubyte a = (color >> 15) * 255;
23-
24-
// restore source transparency key
25-
if (originalTransparencyKey && color == 0)
26-
{
27-
return TVec4D<ubyte>(255, 0, 255, 0);
28-
}
29-
30-
return TVec4D<ubyte>(b, g, r, a);
31-
}
32-
33-
// 16 bit color to RGBA
34-
// originalTransparencyKey makes it pink
35-
TVec4D<ubyte> rgb5a1_ToRGBA8(ushort color, bool originalTransparencyKey /*= true*/)
36-
{
37-
ubyte r = (color & 0x1F) * 8;
38-
ubyte g = ((color >> 5) & 0x1F) * 8;
39-
ubyte b = ((color >> 10) & 0x1F) * 8;
40-
41-
ubyte a = (color >> 15) * 255;
42-
43-
// restore source transparency key
44-
if (originalTransparencyKey && color == 0)
45-
{
46-
return TVec4D<ubyte>(255, 0, 255, 0);
47-
}
48-
49-
return TVec4D<ubyte>(r, g, b, a);
50-
}
51-
52-
//-------------------------------------------------------------------------------
53-
5414
// unpacks texture, returns new source pointer
5515
// there is something like RLE used
5616
char* unpackTexture(char* src, char* dest)

util/image.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,46 @@
22

33
#include <stdio.h>
44

5+
//-------------------------------------------------------------------------------
6+
7+
// 16 bit color to BGRA
8+
// originalTransparencyKey makes it pink
9+
TVec4D<ubyte> rgb5a1_ToBGRA8(ushort color, bool originalTransparencyKey /*= true*/)
10+
{
11+
ubyte r = (color & 0x1F) * 8;
12+
ubyte g = ((color >> 5) & 0x1F) * 8;
13+
ubyte b = ((color >> 10) & 0x1F) * 8;
14+
15+
ubyte a = (color >> 15) * 255;
16+
17+
// restore source transparency key
18+
if (originalTransparencyKey && color == 0)
19+
{
20+
return TVec4D<ubyte>(255, 0, 255, 0);
21+
}
22+
23+
return TVec4D<ubyte>(b, g, r, a);
24+
}
25+
26+
// 16 bit color to RGBA
27+
// originalTransparencyKey makes it pink
28+
TVec4D<ubyte> rgb5a1_ToRGBA8(ushort color, bool originalTransparencyKey /*= true*/)
29+
{
30+
ubyte r = (color & 0x1F) * 8;
31+
ubyte g = ((color >> 5) & 0x1F) * 8;
32+
ubyte b = ((color >> 10) & 0x1F) * 8;
33+
34+
ubyte a = (color >> 15) * 255;
35+
36+
// restore source transparency key
37+
if (originalTransparencyKey && color == 0)
38+
{
39+
return TVec4D<ubyte>(255, 0, 255, 0);
40+
}
41+
42+
return TVec4D<ubyte>(r, g, b, a);
43+
}
44+
545
//-------------------------------------------------------------
646
// Saves TGA file
747
//-------------------------------------------------------------

util/image.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define IMAGE_H
33

44
#include "core/dktypes.h"
5+
#include "math/Vector.h"
56

67
// Define targa header.
78
#pragma pack( push, 1 )
@@ -39,6 +40,17 @@ struct TIMIMAGEHDR
3940

4041
//-------------------------------------------------------------------
4142

43+
// 16 bit color to BGRA
44+
// originalTransparencyKey makes it pink
45+
TVec4D<ubyte> rgb5a1_ToBGRA8(ushort color, bool originalTransparencyKey /*= true*/);
46+
47+
// 16 bit color to RGBA
48+
// originalTransparencyKey makes it pink
49+
TVec4D<ubyte> rgb5a1_ToRGBA8(ushort color, bool originalTransparencyKey /*= true*/);
50+
51+
//-------------------------------------------------------------------
52+
53+
4254
void SaveTGA(const char* filename, ubyte* data, int w, int h, int c);
4355

4456
void SaveTIM_4bit(char* filename,

0 commit comments

Comments
 (0)