forked from JayFoxRox/pba-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format008D.c
79 lines (57 loc) · 1.77 KB
/
format008D.c
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
// Converts 008D files to jpg
//FIXME: Find length of JFIF [by parsing the markers and searching EOI]
//FIXME: Merge with 0022 [but keep the format checks seperated until it is more clear what actually happens]
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "tga.h"
#include "surface.h"
int main(int argc, char* argv[]) {
FILE* in = fopen(argv[1], "rb");
const char* outPath = argv[2];
FILE* out = fopen(outPath, "wb");
SurfaceHeader header;
fread(&header, sizeof(header), 1, in);
printSurfaceHeader(&header);
//FIXME: Read and print unknown value
fseek(in, 4, SEEK_CUR);
//FIXME: Obviously not correct for JFIF / JPEG..
size_t bufferSize = header.stride * header.height_a * 4;
uint8_t* buffer = malloc(bufferSize);
switch(header.format) {
case 0x000E: {
// JFIF / JPEG
//FIXME: Needs to read more data!
fread(buffer, bufferSize, 1, in);
fwrite(buffer, bufferSize, 1, out);
break;
}
case 0x0010: {
// A8 with a JFIF afterwards
char alphaPath[1024];
sprintf(alphaPath, "%s-alpha.tga", outPath);
FILE* alpha = fopen(alphaPath, "wb");
TgaHeader tgaHeader;
setupTgaHeader(&tgaHeader, UncompressedGrayscale, header.width_a, header.height_a);
fwrite(&tgaHeader, sizeof(tgaHeader), 1, alpha);
fread(buffer, bufferSize / 4, 1, in);
fwrite(buffer, bufferSize / 4, 1, alpha);
fclose(alpha);
//FIXME: if unk is 0x1004506C there might not be a color image?!
fread(buffer, bufferSize * 10, 1, in);
fwrite(buffer, bufferSize * 10, 1, out);
break;
}
default:
printf("Unknown format 0x%X\n", header.format);
assert(false);
break;
}
free(buffer);
fclose(out);
fclose(in);
return 0;
}