@@ -82,84 +82,86 @@ void SaveTGA(const char* filename, ubyte* data, int w, int h, int c)
8282
8383void 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