My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* tim2bmp.cpp -- Convert PlayStation TIM images to BMP's
* Copyright (c) 2010 Matthew Hoops (clone2727)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

// Thanks to http://www.romhacking.net/docs/timgfx.txt for the format information

#include <cstdio>
#include <cstring>

// Standard types
typedef unsigned char byte;
typedef unsigned short uint16;
typedef unsigned int uint32;

// Helper functions for reading integers from the stream (maintaining endianness)
byte readByte(FILE *file) {
byte b = 0;
fread(&b, 1, 1, file);
return b;
}

uint16 readUint16LE(FILE *file) {
uint16 x = readByte(file);
return x | readByte(file) << 8;
}

uint32 readUint32LE(FILE *file) {
uint16 x = readUint16LE(file);
return x | readUint16LE(file) << 16;
}

uint16 readUint16BE(FILE *file) {
uint16 x = readByte(file) << 8;
return x | readByte(file);
}

uint32 readUint32BE(FILE *file) {
uint32 x = readUint16BE(file) << 16;
return x | readUint16BE(file);
}

void writeByte(FILE *file, byte b) {
fwrite(&b, 1, 1, file);
}

void writeUint16LE(FILE *file, uint16 x) {
writeByte(file, x & 0xff);
writeByte(file, x >> 8);
}

void writeUint32LE(FILE *file, uint32 x) {
writeUint16LE(file, x & 0xffff);
writeUint16LE(file, x >> 16);
}

void writeUint16BE(FILE *file, uint16 x) {
writeByte(file, x >> 8);
writeByte(file, x & 0xff);
}

void writeUint32BE(FILE *file, uint32 x) {
writeUint16BE(file, x >> 16);
writeUint16BE(file, x & 0xffff);
}

uint32 getFileSize(FILE *file) {
uint32 pos = ftell(file);
fseek(file, 0, SEEK_END);
uint32 size = ftell(file);
fseek(file, pos, SEEK_SET);
return size;
}

void writeBMPHeader(FILE *output, uint16 width, uint16 height, uint16 bitsPerPixel) {
// Main Header
writeUint16BE(output, 'BM');
writeUint32LE(output, 0); // Size, will fill in later
writeUint16LE(output, 0); // Reserved
writeUint16LE(output, 0); // Reserved
writeUint32LE(output, 0); // Image offset, will fill in later

// Info Header
writeUint32LE(output, 40);
writeUint32LE(output, width);
writeUint32LE(output, height);
writeUint16LE(output, 1);
writeUint16LE(output, bitsPerPixel);
writeUint32LE(output, 0);
writeUint32LE(output, 0); // Image size, will fill in later
writeUint32LE(output, 72); // 72 dpi sounds fine to me
writeUint32LE(output, 72); // as above

// Only write the empty palette if we're not in paletted mode. The
// palette header will be written in writeBMPPalette() otherwise.
if (bitsPerPixel > 8) {
writeUint32LE(output, 0);
writeUint32LE(output, 0);
}
}

void writeBMPPalette(FILE *output, byte *palette) {
writeUint32LE(output, 256);
writeUint32LE(output, 256);
fwrite(palette, 1, 256 * 4, output);
}

void fillBMPHeaderValues(FILE *output, uint32 imageOffset, uint32 imageSize) {
fflush(output);
uint32 fileSize = getFileSize(output);

fseek(output, 2, SEEK_SET);
fflush(output);
writeUint32LE(output, fileSize);
fflush(output);

fseek(output, 10, SEEK_SET);
fflush(output);
writeUint32LE(output, imageOffset);
fflush(output);

fseek(output, 34, SEEK_SET);
fflush(output);
writeUint32LE(output, imageSize);
}

// Functions to isolate the color channels and blow them up to 8-bit values

inline byte isolateRedChannel(uint16 color) {
return (color & 0x1f) << 3;
}

inline byte isolateGreenChannel(uint16 color) {
return (color & 0x3e0) >> 2;
}

inline byte isolateBlueChannel(uint16 color) {
return (color & 0x7c00) >> 7;
}

// 15-bit BGR
byte *readTIMPalette(FILE *input, uint16 maxPaletteSize) {
byte *palette = new byte[256 * 4];
memset(palette, 0, 256 * 4);

/* uint32 clutSize = */ readUint32LE(input);
/* uint16 palOrigX = */ readUint16LE(input);
/* uint16 palOrigY = */ readUint16LE(input);
uint16 colorCount = readUint16LE(input);
uint16 clutCount = readUint16LE(input);

if (clutCount != 1) {
printf("Unsupported CLUT count %d\n", clutCount);
return 0;
}

if (colorCount > maxPaletteSize) {
printf("CLUT color count greater than possible %d > %d\n", colorCount, maxPaletteSize);
return 0;
}

for (uint16 i = 0; i < colorCount; i++) {
uint16 color = readUint16LE(input);
palette[i * 4] = isolateBlueChannel(color);
palette[i * 4 + 1] = isolateGreenChannel(color);
palette[i * 4 + 2] = isolateRedChannel(color);
}

return palette;
}

// 4bpp, paletted
bool convertTIM4ToBMP(FILE *input, FILE *output) {
byte *palette = readTIMPalette(input, 16);

if (!palette)
return false;

/* uint32 fileSize = */ readUint32LE(input);
/* uint16 origX = */ readUint16LE(input);
/* uint16 origY = */ readUint16LE(input);
uint16 width = readUint16LE(input) * 4;
uint16 height = readUint16LE(input);

printf("Width = %d\n", width);
printf("Height = %d\n", height);

byte *pixels = new byte[width * height];

for (uint32 i = 0; i < width * height / 2; i++) {
byte val = readByte(input);
pixels[i * 2] = val >> 4;
pixels[i * 2 + 1] = val & 0xf;
}

writeBMPHeader(output, width, height, 8);
writeBMPPalette(output, palette);

const uint32 pitch = width;
const int extraDataLength = (pitch % 4) ? 4 - (pitch % 4) : 0;

for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x < width; x++)
writeByte(output, pixels[x + width * y]);

for (int i = 0; i < extraDataLength; i++)
writeByte(output, 0);
}

fillBMPHeaderValues(output, 54 + 256 * 4, (pitch + extraDataLength) * height);

delete[] pixels;
delete[] palette;
return true;
}

// 15-bit BGR
bool convertTIM16ToBMP(FILE *input, FILE *output) {
/* uint32 fileSize = */ readUint32LE(input);
/* uint16 origX = */ readUint16LE(input);
/* uint16 origY = */ readUint16LE(input);
uint16 width = readUint16LE(input);
uint16 height = readUint16LE(input);

printf("Width = %d\n", width);
printf("Height = %d\n", height);

uint16 *pixels = new uint16[width * height];
for (uint32 i = 0; i < width * height; i++)
pixels[i] = readUint16LE(input);

writeBMPHeader(output, width, height, 24);

const uint32 pitch = width * 3;
const int extraDataLength = (pitch % 4) ? 4 - (pitch % 4) : 0;

for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
uint16 color = pixels[x + width * y];
writeByte(output, isolateBlueChannel(color));
writeByte(output, isolateGreenChannel(color));
writeByte(output, isolateRedChannel(color));
}

for (int i = 0; i < extraDataLength; i++)
writeByte(output, 0);
}

fillBMPHeaderValues(output, 54, (pitch + extraDataLength) * height);

delete[] pixels;
return true;
}

bool convertTIMToBMP(FILE *input, FILE *output) {
uint32 tag = readUint32LE(input);
uint32 version = readUint32LE(input);

if (tag != 0x10) {
printf("TIM tag not found\n");
return false;
}

switch (version) {
case 8: // 4bpp (with CLUT)
printf("Found 4bpp (with CLUT) image\n");
return convertTIM4ToBMP(input, output);
case 0: // 4bpp (without CLUT)
printf("Unhandled 4bpp (without CLUT) image\n");
return false;
case 9: // 8bpp (with CLUT)
printf("Unhandled 8bpp (with CLUT) image\n");
return false;
case 1: // 8bpp (without CLUT)
printf("Unhandled 8bpp (without CLUT) image\n");
return false;
case 2: // 16bpp
printf("Found 16bpp TIM image\n");
return convertTIM16ToBMP(input, output);
case 3: // 24bpp
printf("Unhandled 24bpp\n");
return false;
}

printf("Unknown TIM type %d\n", version);
return false;
}

int main(int argc, const char **argv) {
printf("\nTIM to BMP Converter\n");
printf("Converts from PlayStation TIM files to BMP\n");
printf("Written by Matthew Hoops (clone2727)\n");
printf("See license.txt for the license\n\n");

if (argc < 3) {
printf("Usage: %s <input> <output>\n", argv[0]);
return 0;
}

FILE *input = fopen(argv[1], "rb");
if (!input) {
printf("Could not open '%s' for reading\n", argv[1]);
return 1;
}

FILE *output = fopen(argv[2], "wb+");
if (!output) {
fclose(input);
printf("Could not open '%s' for writing\n", argv[2]);
return 1;
}

if (!convertTIMToBMP(input, output))
return 1;

fclose(input);
fflush(output);
fclose(output);

printf("\nAll Done!\n");
return 0;
}

Change log

r7 by clone2727 on Jul 20, 2010   Diff
Add my extract_cc4_pix tool and fix a
minor bug in tim2bmp.
Go to: 
Project members, sign in to write a code review

Older revisions

r3 by clone2727 on Jul 18, 2010   Diff
Add my tim2bmp tool.
All revisions of this file

File info

Size: 8696 bytes, 336 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Rev Author URL Id
Powered by Google Project Hosting