summaryrefslogtreecommitdiff
path: root/src/compressors/Gen2Decmp.java
blob: 6f59ffbe04f2c414c877358388e4e25426517ad9 (plain)
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
package compressors;

/**
 * Pokemon Gen 2 sprite decompressor Source:
 * https://github.com/pret/pokemon-reverse-engineering-tools/blob/master/pokemontools/lz.py 
 * (and gfx.py for flatten())
 * Ported to Java by sneed
 *
 */
public class Gen2Decmp {

    private static final int LZ_END = 0xFF;
    private static final int INITIAL_BUF_SIZE = 0x1000;

    public byte[] data;
    private int address;
    private byte[] output;
    private int out_idx;
    private int cmd;
    private int len;
    private int offset;

    private static int[] bit_flipped;

    static {
        bit_flipped = new int[0x100];
        for (int b = 0; b < 0x100; b++) {
            for (int i = 0; i < 8; i++) {
                bit_flipped[b] += ((b >> i) & 1) << (7 - i);
            }
        }
    }

    public Gen2Decmp(byte[] input, int baseOffset, int tilesWide, int tilesHigh) {
        this.data = input;
        this.address = baseOffset;
        decompress();
        cutAndTranspose(tilesWide, tilesHigh);
    }

    public byte[] getData() {
        return output;
    }

    public byte[] getFlattenedData() {
        return flatten(output);
    }

    private void cutAndTranspose(int width, int height) {
        if (output == null) {
            return;
        }
        int tiles = width * height;

        byte[] newData = new byte[width * height * 16];
        for (int tile = 0; tile < tiles; tile++) {
            int oldTileX = tile % width;
            int oldTileY = tile / width;
            int newTileNum = oldTileX * height + oldTileY;
            System.arraycopy(output, tile * 16, newData, newTileNum * 16, 16);
        }
        output = newData;

    }

    private void decompress() {
        output = new byte[INITIAL_BUF_SIZE];
        while (true) {
            if (this.peek() == LZ_END) {
                this.next();
                break;
            }

            cmd = (this.peek() & 0xE0) >> 5;
            if (cmd == 7) {
                // LONG command
                cmd = (this.peek() & 0x1C) >> 2;
                len = (this.next() & 0x03) * 0x100 + this.next() + 1;
            } else {
                // Normal length
                len = (this.next() & 0x1F) + 1;
            }

            while (out_idx + len > output.length) {
                resizeOutput();
            }

            switch (cmd) {
            case 0:
                // Literal
                System.arraycopy(data, address, output, out_idx, len);
                out_idx += len;
                address += len;
                break;
            case 1:
                // Iterate
                byte repe = (byte) next();
                for (int i = 0; i < len; i++) {
                    output[out_idx++] = repe;
                }
                break;
            case 2:
                // Alternate
                byte[] alts = { (byte) next(), (byte) next() };
                for (int i = 0; i < len; i++) {
                    output[out_idx++] = alts[i & 1];
                }
                break;
            case 3:
                // Zero-fill
                // Easy, since Java arrays are initialized to 0.
                out_idx += len;
                break;
            case 4:
                // Default repeat
                repeat();
                break;
            case 5:
                repeat(1, bit_flipped);
                break;
            case 6:
                repeat(-1, null);
                break;
            }
        }

        byte[] finalOutput = new byte[out_idx];
        System.arraycopy(output, 0, finalOutput, 0, out_idx);
        output = finalOutput;

    }

    private void repeat() {
        repeat(1, null);
    }

    private void repeat(int direction, int[] table) {
        get_offset();
        for (int i = 0; i < len; i++) {
            int value = output[offset + i * direction] & 0xFF;
            output[out_idx++] = (byte) ((table == null) ? value : table[value]);
        }
    }

    private void get_offset() {
        if (this.peek() >= 0x80) {
            // Negative
            offset = this.next() & 0x7F;
            offset = out_idx - offset - 1;
        } else {
            // Positive, extended
            offset = this.next() * 0x100 + this.next();
        }
    }

    private void resizeOutput() {
        byte[] newOut = new byte[output.length * 2];
        System.arraycopy(output, 0, newOut, 0, out_idx);
        output = newOut;
    }

    private int peek() {
        return data[address] & 0xFF;
    }

    public int next() {
        return data[address++] & 0xFF;
    }

    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;
    }

}