-
Notifications
You must be signed in to change notification settings - Fork 2
/
gfxcodec.c
161 lines (144 loc) · 4.79 KB
/
gfxcodec.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
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
// Furrtek 2018
// NeoGeo graphics codec to/from Genesis format
// To be used with Tile Layer Pro, YY-CHR, Tile Molester...
// TODO: Test CD sprites encode/decode
// TODO: Cart sprites encode/decode
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void print_usage() {
puts("Usage: gfxcodec (-d|e) (-f|s|c) (input file) (output file)");
puts(" -d|e: decode/encode");
puts(" -f|s|c: fix/cart sprites/CD sprites");
exit(EXIT_FAILURE);
}
int main(int argc, char * argv[]) {
int opt;
enum { NO_MODE, ENCODE, DECODE } mode = NO_MODE;
enum { NO_TYPE, FIX, SPR, SPRCD } type = NO_TYPE;
unsigned long int bitplanes, indexes, mask;
const unsigned char fix_column_order[4] = { 2, 3, 0, 1 };
const unsigned char bitplane_masks[4] = { 8, 4, 2, 1 };
unsigned char byte, color_index, bitplane;
unsigned char * buffer_in;
unsigned char * buffer_out;
FILE * file;
long int size, i, p, bp;
puts("NeoGeo gfxcodec");
opterr = 0;
while ((opt = getopt(argc, argv, "defs")) != -1) {
switch (opt) {
case 'd': mode = DECODE; break;
case 'e': mode = ENCODE; break;
case 'f': type = FIX; break;
case 's': type = SPR; break;
case 'c': type = SPRCD; break;
default:
print_usage();
}
}
if ((mode == NO_MODE) || (type == NO_TYPE) || (optind != 3))
print_usage();
// Load file
file = fopen(argv[optind], "rb");
if (!file) {
printf("Can't open input file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
buffer_in = malloc(size); // 4bpp
if (buffer_in == NULL) {
printf("Malloc failed.\n");
return 1;
}
buffer_out = malloc(size); // 4bpp
if (buffer_out == NULL) {
printf("Malloc failed.\n");
return 1;
}
fread(buffer_in, 1, size, file);
fclose(file);
if (type == FIX) {
if (mode == DECODE) {
// Decode fix tiles
for (i = 0; i < size; i++) {
// Remove ^ 1 if input isn't byteswapped
byte = buffer_in[(i & 0xFFFFFFE0UL) + (fix_column_order[i & 3] << 3) + (((i >> 2) & 7) ^ 1)];
buffer_out[i] = (byte >> 4) | ((byte & 15) << 4); // Nibble swap
}
} else {
// Encode fix tiles
// Input: AB CD EF GH
// IJ KL MN OP
// Output: EF MN .. .. .. .. .. .. GH OP .. .. .. .. .. .. AB IJ .. .. .. .. .. .. CD KL .. .. .. .. .. ..
for (i = 0; i < size; i++) {
byte = buffer_in[(i & 0xFFFFFFE0UL) + fix_column_order[(i >> 3) & 3] + ((i & 7) << 2)];
buffer_out[i ^ 1] = (byte >> 4) | ((byte & 15) << 4); // Nibble swap
}
}
} else {
if (mode == DECODE) {
// Decode CD sprite tiles
for (i = 0; i < size; i+=4) {
// Bitplanes: 33333333 22222222 11111111 00000000
// Indexes: A A A A
// B B B B
// C C C D
// D D D D
// E E E E
// F F F F
// G G G G
// H H H H
bitplanes = (buffer_in[i + 2] << 24) + (buffer_in[i + 3] << 16) + (buffer_in[i + 0] << 8) + buffer_in[i + 1];
for (p = 0; p < 8; p++) {
color_index = ((bitplanes & 0x01000000UL) >> 21) |
((bitplanes & 0x00010000UL) >> 14) |
((bitplanes & 0x00000100UL) >> 7) |
(bitplanes & 0x00000001UL);
if (!(p & 1)) {
byte = color_index << 4;
} else {
buffer_out[i + (p >> 1)] = byte | color_index;
}
bitplanes >>= 1;
}
}
} else {
// Encode CD sprite tiles
for (i = 0; i < size; i++) {
// Indexes: AAAABBBB CCCCDDDD EEEEFFFF GGGGHHHH
// Bitplanes: 1 1 1 1 1 1 1 1
// 0 0 0 0 0 0 0 0
// 3 3 3 3 3 3 3 3
// 2 2 2 2 2 2 2 2
indexes <<= 8;
indexes |= buffer_in[i];
if ((i & 3) == 3) {
// Accumulated an 8-pixels row
for (bp = 0; bp < 4; bp++) {
mask = bitplane_masks[bp];
bitplane = 0;
for (p = 0; p < 8; p++) {
bitplane <<= 1;
if (indexes & mask)
bitplane |= 1;
mask <<= 4;
}
buffer_out[((i & 0xFFFC) ^ 0x40) + bp] = bitplane;
}
}
}
}
}
file = fopen(argv[optind + 1], "wb");
if (!file) {
printf("Can't open output file.\n");
return 1;
}
fwrite(buffer_out, 1, size, file);
fclose(file);
puts("Done.");
return 0;
}