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
337
338
|
package compressors;
/**
* Pokemon Gen 1 sprite decompressor Source:
* https://github.com/pret/pokemon-reverse-engineering-tools/blob/master/pokemontools/pic.py
* (and gfx.py for flatten())
* Ported to Java by rafa_99
*
*/
public class Gen1Decmp {
private static int bitflip(int x, int n) {
int r = 0;
while (n > 0) {
r = (r << 1) | (x & 1);
x >>= 1;
n -= 1;
}
return r;
}
private static int[] table1, table3;
private static int[][] table2 = { { 0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5, 0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa },
{ 0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa, 0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5 },
{ 0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa, 0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5 },
{ 0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5, 0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa }, };
static {
table1 = new int[16];
table3 = new int[16];
for (int i = 0; i < 16; i++) {
table1[i] = (2 << i) - 1;
table3[i] = bitflip(i, 4);
}
}
private static int tilesize = 8;
private BitStream bs;
private boolean mirror, planar;
private byte[] data;
private int sizex, sizey, size;
private int ramorder;
public Gen1Decmp(byte[] input, int baseOffset) {
this(input, baseOffset, false, true);
}
private Gen1Decmp(byte[] input, int baseOffset, boolean mirror, boolean planar) {
this.bs = new BitStream(input, baseOffset);
this.mirror = mirror;
this.planar = planar;
this.data = null;
}
public void decompress() {
byte[][] rams = new byte[2][];
this.sizex = this.readint(4) * tilesize;
this.sizey = this.readint(4);
this.size = this.sizex * this.sizey;
this.ramorder = this.readbit();
int r1 = this.ramorder;
int r2 = this.ramorder ^ 1;
fillram(rams, r1);
int mode = this.readbit();
if (mode == 1) {
mode += this.readbit();
}
fillram(rams, r2);
rams[0] = this.bitgroups_to_bytes(rams[0]);
rams[1] = this.bitgroups_to_bytes(rams[1]);
if (mode == 0) {
this.decode(rams[0]);
this.decode(rams[1]);
} else if (mode == 1) {
this.decode(rams[r1]);
this.xor(rams[r1], rams[r2]);
} else if (mode == 2) {
this.decode(rams[r2], false);
this.decode(rams[r1]);
this.xor(rams[r1], rams[r2]);
}
if (this.planar) {
data = new byte[this.size * 2];
for (int i = 0; i < rams[0].length; i++) {
data[i * 2] = rams[0][i];
data[i * 2 + 1] = rams[1][i];
}
} else {
byte[] tmpdata = new byte[this.size * 8];
BitStream r0S = new BitStream(rams[0]);
BitStream r1S = new BitStream(rams[1]);
for (int i = 0; i < tmpdata.length; i++) {
tmpdata[i] = (byte) (r0S.next() | (r1S.next() << 1));
}
data = this.bitgroups_to_bytes(tmpdata);
}
}
public void transpose() {
if (data == null) {
return;
}
int tiles = data.length / 16;
int width = this.sizex / tilesize;
int height = this.sizey;
byte[] newData = new byte[data.length];
for (int tile = 0; tile < tiles; tile++) {
int oldTileX = tile % width;
int oldTileY = tile / width;
int newTileNum = oldTileX * height + oldTileY;
System.arraycopy(data, tile * 16, newData, newTileNum * 16, 16);
}
data = newData;
}
public byte[] getData() {
return data;
}
public byte[] getFlattenedData() {
return flatten(data);
}
public int getWidth() {
return this.sizex;
}
public int getHeight() {
return this.sizey * tilesize;
}
private void fillram(byte[][] rams, int rOffset) {
int size = this.size * 4;
rams[rOffset] = new byte[size];
boolean rleMode = this.readbit() == 0;
int written = 0;
while (written < size) {
if (rleMode) {
written += this.read_rle_chunk(rams[rOffset], written);
} else {
written += this.read_data_chunk(rams[rOffset], written, size);
}
rleMode = !rleMode;
}
rams[rOffset] = deinterlace_bitgroups(rams[rOffset]);
}
private byte[] deinterlace_bitgroups(byte[] bits) {
byte[] l = new byte[bits.length];
int offs = 0;
for (int y = 0; y < this.sizey; y++) {
for (int x = 0; x < this.sizex; x++) {
int i = 4 * y * this.sizex + x;
for (int j = 0; j < 4; j++) {
l[offs++] = bits[i];
i += this.sizex;
}
}
}
return l;
}
private int read_rle_chunk(byte[] ram, int baseOffset) {
int i = 0;
while (this.readbit() == 1) {
i++;
}
int n = table1[i];
int a = this.readint(i + 1);
n += a;
for (i = 0; i < n; i++) {
ram[baseOffset + i] = 0;
}
return n;
}
private int read_data_chunk(byte[] ram, int baseOffset, int size) {
int written = 0;
while (true) {
int bitgroup = this.readint(2);
if (bitgroup == 0) {
break;
}
ram[baseOffset + written] = (byte) bitgroup;
written++;
if (baseOffset + written >= size) {
break;
}
}
return written;
}
private int readbit() {
return bs.next();
}
private int readint(int count) {
return readint(bs, count);
}
private int readint(BitStream strm, int count) {
int n = 0;
while (count > 0) {
n <<= 1;
n |= strm.next();
count -= 1;
}
return n;
}
private byte[] bitgroups_to_bytes(byte[] bits) {
int limiter = bits.length - 3;
byte[] ret = new byte[bits.length / 4];
for (int i = 0; i < limiter; i += 4) {
int n = ((bits[i] << 6) | (bits[i + 1] << 4) | (bits[i + 2] << 2) | (bits[i + 3]));
ret[i / 4] = (byte) n;
}
return ret;
}
private void decode(byte[] ram) {
decode(ram, this.mirror);
}
private void decode(byte[] ram, boolean mirror) {
for (int x = 0; x < this.sizex; x++) {
int bit = 0;
for (int y = 0; y < this.sizey; y++) {
int i = y * this.sizex + x;
int a = ((ram[i] & 0xFF) >> 4) & 0x0F;
int b = ram[i] & 0x0F;
a = table2[bit][a];
bit = a & 1;
if (mirror) {
a = table3[a];
}
b = table2[bit][b];
bit = b & 1;
if (mirror) {
b = table3[b];
}
ram[i] = (byte) ((a << 4) | b);
}
}
}
private void xor(byte[] ram1, byte[] ram2) {
xor(ram1, ram2, this.mirror);
}
private void xor(byte[] ram1, byte[] ram2, boolean mirror) {
for (int i = 0; i < ram2.length; i++) {
if (mirror) {
int a = ((ram2[i] & 0xFF) >> 4) & 0x0F;
int b = ram2[i] & 0x0F;
a = table3[a];
b = table3[b];
ram2[i] = (byte) ((a << 4) | b);
}
ram2[i] = (byte) ((ram2[i] & 0xFF) ^ (ram1[i] & 0xFF));
}
}
private static class BitStream {
private byte[] data;
private int offset;
private int bitsLeft;
private int bufVal;
private static final int bmask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff,
0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff,
0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
0xffffffff };
public BitStream(byte[] data) {
this(data, 0);
}
public BitStream(byte[] data, int baseOffset) {
this.data = data;
this.offset = baseOffset - 1;
this.bitsLeft = 0;
this.bufVal = -1;
}
public int next() {
if (bitsLeft == 0) {
offset++;
bufVal = data[offset] & 0xFF;
if (offset >= data.length) {
return -1;
}
bitsLeft = 8;
}
int retval = bufVal >> (bitsLeft - 1);
bufVal &= bmask[bitsLeft - 1];
bitsLeft--;
return retval;
}
}
private static byte[] flatten(byte[] planar) {
byte[] strips = new byte[planar.length * 4];
for (int j = 0; j < planar.length / 2; j++) {
int bottom = planar[j * 2] & 0xFF;
int top = planar[j * 2 + 1] & 0xFF;
byte[] strip = new byte[8];
for (int i = 7; i >= 0; i--) {
strip[7 - i] = (byte) (((bottom >>> i) & 1) + ((top * 2 >>> i) & 2));
}
System.arraycopy(strip, 0, strips, j * 8, 8);
}
return strips;
}
}
|