1+ #include < math.h>
2+
3+ #include < malloc.h>
4+ #include " core/VirtualStream.h"
5+ #include " core/cmdlib.h"
6+ #include " math/dktypes.h"
7+ #include " util/image.h"
8+ #include " util/util.h"
9+
10+ struct RECT16
11+ {
12+ short x, y, w, h;
13+ };
14+
15+ const int SKY_CLUT_START_Y = 252 ;
16+ const int SKY_SIZE_W = 128 / 4 ;
17+ const int SKY_SIZE_H = 84 ;
18+
19+ void CopyTpageImage (ushort* tp_src, ushort* dst, int x, int y, int dst_w, int dst_h)
20+ {
21+ ushort* src = tp_src + x + 128 * y;
22+
23+ for (int i = 0 ; i < dst_h; i++)
24+ {
25+ memcpy (dst, src, dst_w * sizeof (short ));
26+ dst += dst_w;
27+ src += 128 ;
28+ }
29+ }
30+
31+ void ExportSkyImage (const char * skyFileName, ubyte* data, int x_idx, int y_idx, int sky_id, int n)
32+ {
33+ ubyte imageCopy[SKY_SIZE_W * SKY_SIZE_H];
34+ ushort imageClut[16 ];
35+
36+ CopyTpageImage ((ushort*)data, (ushort*)imageCopy, x_idx * SKY_SIZE_W, y_idx * SKY_SIZE_H, SKY_SIZE_W / 2 , SKY_SIZE_H);
37+ CopyTpageImage ((ushort*)data, (ushort*)imageClut, x_idx * 16 , SKY_CLUT_START_Y + y_idx, 16 , 1 );
38+
39+ SaveTIM_4bit (varargs (" %s_%d_%d.TIM" , skyFileName, sky_id, n),
40+ imageCopy, SKY_SIZE_W * SKY_SIZE_H, 0 , 0 , SKY_SIZE_W*2 , SKY_SIZE_H, (ubyte*)imageClut, 1 );
41+ }
42+
43+ void ConvertSky (const char * skyFileName)
44+ {
45+ FILE* fp = fopen (skyFileName, " rb" );
46+ if (!fp)
47+ {
48+ MsgError (" Unable to open '%s'\n " , skyFileName);
49+ return ;
50+ }
51+
52+ MsgInfo (" Converting '%s' to separate TIM files..." , skyFileName);
53+
54+ fseek (fp, 0 , SEEK_END);
55+ int size = ftell (fp);
56+ fseek (fp, 0 , SEEK_SET);
57+
58+ ubyte* data = (ubyte*)malloc (size);
59+
60+ fread (data, size, 1 , fp);
61+ fclose (fp);
62+
63+ const int OFFSET_STEP = 0x10000 ;
64+
65+ for (int i = 0 ; i < 5 ; i++)
66+ {
67+ int n = 0 ;
68+ ubyte* skyImage = data + i * OFFSET_STEP;
69+
70+ for (int y = 0 ; y < 3 ; y++)
71+ {
72+ for (int x = 0 ; x < 4 ; x++, n++)
73+ ExportSkyImage (skyFileName, skyImage, x, y, i, n);
74+ }
75+ }
76+
77+ MsgInfo (" Done!" );
78+ }
79+
80+ // -----------------------------------------------------
81+
82+ void PrintCommandLineArguments ()
83+ {
84+ MsgInfo (" Example usage:\n " );
85+ MsgInfo (" \t DriverImageTool -sky2tim SKY0.RAW\n " );
86+ }
87+
88+ int main (int argc, char ** argv)
89+ {
90+ #ifdef _WIN32
91+ Install_ConsoleSpewFunction ();
92+ #endif
93+
94+ Msg (" ---------------\n DriverImageTool - PSX Driver image utilities\n ---------------\n\n " );
95+
96+ if (argc <= 1 )
97+ {
98+ PrintCommandLineArguments ();
99+ return -1 ;
100+ }
101+
102+ for (int i = 0 ; i < argc; i++)
103+ {
104+ if (!stricmp (argv[i], " -sky2tim" ))
105+ {
106+ if (i + 1 <= argc)
107+ ConvertSky (argv[i + 1 ]);
108+ else
109+ MsgWarning (" -sky2tim must have an argument!" );
110+ }
111+ }
112+
113+ return 0 ;
114+ }
0 commit comments