Skip to content

Commit c78c25e

Browse files
committed
Add SaveRAW_TIM methods
1 parent a95bbe2 commit c78c25e

5 files changed

Lines changed: 69 additions & 99 deletions

File tree

Driver2CutsceneTool/Driver2CutsceneTool.args.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

Driver2MissionTool/Driver2MissionTool.args.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

DriverImageTool/DriverImageTool.args.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

DriverImageTool/image_tool.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ void ConvertBackgroundRaw(const char* filename, const char* extraFilename)
291291
{
292292
for (int x = 0; x < rect_w * 2; x++)
293293
{
294-
timData[(rect_y + y) * 64 * 6 + rect_x + x] = bgImagePiece[y * 128 + x];
294+
size_t timDataIndex = (rect_y + y) * 64 * 6 + rect_x + x;
295+
size_t bgImagePieceIndex = y * 128 + x;
296+
timData[timDataIndex] = bgImagePiece[bgImagePieceIndex];
295297
}
296298
}
297299
}
@@ -402,7 +404,7 @@ void PrintCommandLineArguments()
402404
MsgInfo("\tDriverImageTool -sky2tim SKY0.RAW\n");
403405
MsgInfo("\tDriverImageTool -sky2tga SKY1.RAW\n");
404406
MsgInfo("\tDriverImageTool -bg2tim CARBACK.RAW <CCARS.RAW>\n");
405-
MsgInfo("\tDriverImageTool -tim2bg BG.RAW FILE.TIM FILE2.TIM\n");
407+
MsgInfo("\tDriverImageTool -tim2raw BG.RAW <FILE.TIM FILE2.TIM ..>\n");
406408
}
407409

408410
int main(int argc, char** argv)
@@ -454,21 +456,21 @@ int main(int argc, char** argv)
454456
else
455457
MsgWarning("-bg2tim must have an argument!");
456458
}
457-
else if (!stricmp(argv[i], "-tim2bg"))
459+
else if (!stricmp(argv[i], "-tim2raw"))
458460
{
459-
int nbFiles = argc - i - 1;
460-
if (nbFiles > 1)
461+
int nbFiles = argc - i - 2;
462+
if (argc > 3)
461463
{
462464
char** filenames = (char**)malloc(sizeof(char*)*nbFiles);
463465
for (int j = 0; j < nbFiles; j++)
464466
{
465-
filenames[j] = argv[i + j + 1];
467+
filenames[j] = argv[i + j + 2];
466468
}
467-
468-
SaveRAW_TIM("TEST.RAW", filenames, nbFiles);
469+
470+
SaveRAW_TIM(argv[i + 1], filenames, nbFiles);
469471
}
470472
else
471-
MsgWarning("-tim2bg must have at least an argument!");
473+
MsgWarning("-tim2raw must have at least three arguments!");
472474
}
473475
}
474476

util/image.cpp

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -82,84 +82,86 @@ void SaveTGA(const char* filename, ubyte* data, int w, int h, int c)
8282

8383
void SaveRAW_TIM(char* out, char** filenames, size_t nbFiles)
8484
{
85-
ubyte** image_data = (ubyte**) malloc(sizeof(ubyte*) * nbFiles);
86-
ubyte** clut_data = (ubyte**) malloc(sizeof(ubyte*) * nbFiles);
85+
ubyte** clut_data = static_cast<ubyte**>(malloc(sizeof(ubyte*) * nbFiles));
86+
size_t* size_cd = static_cast<size_t*>(malloc(sizeof(size_t) * nbFiles)); // Sizes of clut_data
8787

88-
size_t* size_id = (size_t*)malloc(sizeof(size_t) * nbFiles);
89-
size_t* size_cd = (size_t*)malloc(sizeof(size_t) * nbFiles);
90-
91-
/*
92-
* TIM FILE
93-
* TIMHDR(header)
94-
* TIMIMAGEHDR(cluthdr)
95-
* binary data clut_data
96-
* TIMIMAGEHDR(datahdr)
97-
* binary data clut_data
98-
*/
99-
100-
/* GFX.RAW FILE
101-
0x00000 - 0x32000 - GFX.RAW.TIM image_data[0]
102-
0x32000 - 0x52000 - GFX_REST.RAW.TIM image_data[1]
103-
0x52000 - 0x58000 - CLUT DATA clut_data
104-
0x58000 - 0x60000 - NULL BYTES
105-
*/
88+
FILE* fp = fopen(out, "wb");
10689

90+
if (!fp)
91+
{
92+
fprintf(stderr, "Unable to open '%s' file\n", out);
93+
return;
94+
}
95+
10796
for (size_t i = 0; i < nbFiles; i++)
10897
{
109-
FILE* fp = fopen(filenames[i], "rb");
110-
if (!fp)
98+
FILE* inFp = fopen(filenames[i], "rb");
99+
if (!inFp)
111100
{
112101
fprintf(stderr, "Unable to open '%s' file\n", filenames[i]);
113102
exit(EXIT_FAILURE);
114103
}
115-
// Here parse TIM files
116104

117-
TIMHDR hdr;
105+
MsgInfo("Parsing '%s' file\n", filenames[i]);
106+
118107
TIMIMAGEHDR cluthdr;
119-
TIMIMAGEHDR datahdr;
108+
TIMIMAGEHDR header;
109+
ubyte* image_data;
120110

121-
fread(&hdr, 1, sizeof(TIMHDR), fp);
122-
123-
fread(&cluthdr, 1, sizeof(TIMIMAGEHDR), fp);
124-
clut_data[i] = (ubyte*) malloc(sizeof(ubyte)*cluthdr.len);
125-
fread(clut_data[i], 1, cluthdr.len - sizeof(TIMIMAGEHDR), fp);
126-
127-
fread(&datahdr, 1, sizeof(TIMIMAGEHDR), fp);
128-
image_data[i] = (ubyte*) malloc(sizeof(ubyte) * datahdr.len);
129-
fread(image_data[i], 1, datahdr.len - sizeof(TIMIMAGEHDR), fp);
111+
// Skip header
112+
fseek(inFp, 8, SEEK_SET);
130113

131-
size_id[i] = datahdr.len - sizeof(TIMIMAGEHDR);
132-
size_cd[i] = cluthdr.len - sizeof(TIMIMAGEHDR);
133-
134-
fclose(fp);
135-
}
114+
// Parsing CLUT Header
115+
fread(&cluthdr, 1, sizeof(TIMIMAGEHDR), inFp);
116+
clut_data[i] = static_cast<ubyte*>(malloc(cluthdr.len - sizeof(TIMIMAGEHDR)));
117+
fread(clut_data[i], 1, cluthdr.len - sizeof(TIMIMAGEHDR), inFp);
136118

137-
FILE* fp = fopen(out, "wb");
119+
// Parsing image data header
120+
fread(&header, 1, sizeof(TIMIMAGEHDR), inFp);
121+
image_data = static_cast<ubyte*>(malloc(header.len - sizeof(TIMIMAGEHDR)));
122+
fread(image_data, 1, header.len - sizeof(TIMIMAGEHDR), inFp);
138123

139-
if (!fp)
140-
{
141-
fprintf(stderr, "Unable to open '%s' file\n", out);
142-
return;
143-
}
144-
145-
// Here re-order tim raw image and clut data as well to psx raw data
124+
size_cd[i] = cluthdr.len - sizeof(TIMIMAGEHDR);
125+
126+
fclose(inFp);
127+
MsgInfo("Writing image data of file '%s' to '%s'\n", filenames[i], out);
128+
129+
int rect_w = 64;
130+
int rect_h = 256;
146131

147-
for (int i = 0; i < nbFiles; i++)
148-
{
149-
fwrite(image_data[i], 1, size_id[i], fp);
132+
for (int j = 0; j < 6; j++)
133+
{
134+
ubyte* bgImagePiece = (ubyte*)malloc(0x8000);
135+
136+
int rect_y = j / 3;
137+
int rect_x = (j - (rect_y & 1) * 3) * 128;
138+
rect_y *= 256;
139+
140+
for (int y = 0; y < rect_h; y++)
141+
{
142+
for (int x = 0; x < rect_w * 2; x++)
143+
{
144+
size_t timDataIndex = (rect_y + y) * 64 * 6 + rect_x + x;
145+
size_t bgImagePieceIndex = y * 128 + x;
146+
bgImagePiece[bgImagePieceIndex] = image_data[timDataIndex];
147+
}
148+
}
149+
fwrite(bgImagePiece, 1, 0x8000, fp);
150+
}
151+
MsgAccept("Image data of '%s' wrote with success\n\n", filenames[i]);
150152
}
151153

152-
for (int i = 0; i < nbFiles; i++)
154+
// Set offset to start of CLUT data
155+
fseek(fp, 0x58000, SEEK_SET);
156+
for (size_t i = 0; i < nbFiles; i++)
153157
{
158+
MsgInfo("Writting CLUT data to '%s'\n", filenames[i]);
154159
fwrite(clut_data[i], 1, size_cd[i], fp);
160+
MsgAccept("CLUT data of '%s' wrote with success\n\n", filenames[i]);
155161
}
156162

157-
fseek(fp, 0, SEEK_END);
158-
int bgSize = ftell(fp);
159-
int zero = 0;
160-
fwrite(&zero, 1, 0x60000 - bgSize, fp);
161-
162163
fclose(fp);
164+
MsgAccept("'%s' compiled with success !\n", out);
163165
}
164166

165167
//-------------------------------------------------------------

0 commit comments

Comments
 (0)