Difference between revisions of "Compression"
Jump to navigation
Jump to search
(added TD download) |
(described compression in detail) |
||
Line 1: | Line 1: | ||
Terranigma uses a compression for most of the data. | Terranigma uses a compression for most of the data. | ||
== Format == | |||
Compressed data always starts with a 4 byte header: | |||
{| class="wikitable" style="font-family:Lucida Console;" | |||
|- | |||
! Offset !! Length !! Description | |||
|- | |||
| 0 || 1 || Chunk Counter ??, always 0 (not used in the game) | |||
|- | |||
| 1 || 2 || Uncompressed data size (LE) | |||
|- | |||
| 3 || 1 || First byte of uncompressed data | |||
|} | |||
After that follows the compressed data. | |||
It consists of data bytes intersparsed with a control code bitstream, both use MSB first (BE) encoding. | |||
Data bytes are always aligned at byte boundary. | |||
The control code stream is fit in between the data bytes so that a new cc byte is only placed | |||
when a bit needs to be used and the previous cc byte is full. | |||
{| class="wikitable" style="font-family:Lucida Console;" | |||
|+ Terranigma Compression Format | |||
|- | |||
! style="background:#F2F299;" | cc | |||
! style="background:#F2F299;" | cc | |||
! style="background:#F2F299;" | cc | |||
! style="background:#F2F299;" | cc | |||
! 8 bit | |||
! 5 bit | |||
! 3 bit | |||
! 8 bit | |||
! Description | |||
|- | |||
| style="background:#FFFFCC;" | 1 | |||
| colspan="3" style="background:#FFFFCC;" | - | |||
| Literal | |||
| colspan="3" | - | |||
| '''Literal Byte''' | |||
|- | |||
| rowspan="4" style="background:#FFFFCC;" | 0 | |||
| rowspan="3" style="background:#FFFFCC;" | 1 | |||
| rowspan="3" colspan="2" style="background:#FFFFCC;" | - | |||
| rowspan="3" colspan="2" | Offset | |||
| Length != 0 | |||
| - | |||
| Copy '''Length+2''' bytes from '''next_output_pos-0x2000+offset''' to the output | |||
|- | |||
| rowspan="2" | 0 | |||
| Length != 0 | |||
| Copy '''Length+1''' bytes from '''next_output_pos-0x2000+offset''' to the output | |||
|- | |||
| 0 | |||
| '''End of Data''' (usually in this case '''Offset''' is also 0) | |||
|- | |||
| style="background:#FFFFCC;" | 0 | |||
| colspan="2" style="background:#FFFFCC;" | Length | |||
| Offset | |||
| colspan="3" | - | |||
| Copy '''Length+2''' bytes from '''next_output_pos-0x100+offset''' to the output | |||
|} | |||
In the table, each '''cc''' stands for a control code bit. The second data byte is divided into 5+3 bits for mode '''01'''. | |||
== Tool == | |||
Crediar re'ed the decompression from the code and translated it to C code. | |||
You can download a tool with the source to decompress data from any given offset [http://crediar.no-ip.com/td-v0.1-cred.rar here]. | You can download a tool with the source to decompress data from any given offset [http://crediar.no-ip.com/td-v0.1-cred.rar here]. |
Revision as of 22:35, 18 February 2016
Terranigma uses a compression for most of the data.
Format
Compressed data always starts with a 4 byte header:
Offset | Length | Description |
---|---|---|
0 | 1 | Chunk Counter ??, always 0 (not used in the game) |
1 | 2 | Uncompressed data size (LE) |
3 | 1 | First byte of uncompressed data |
After that follows the compressed data. It consists of data bytes intersparsed with a control code bitstream, both use MSB first (BE) encoding. Data bytes are always aligned at byte boundary. The control code stream is fit in between the data bytes so that a new cc byte is only placed when a bit needs to be used and the previous cc byte is full.
cc | cc | cc | cc | 8 bit | 5 bit | 3 bit | 8 bit | Description |
---|---|---|---|---|---|---|---|---|
1 | - | Literal | - | Literal Byte | ||||
0 | 1 | - | Offset | Length != 0 | - | Copy Length+2 bytes from next_output_pos-0x2000+offset to the output | ||
0 | Length != 0 | Copy Length+1 bytes from next_output_pos-0x2000+offset to the output | ||||||
0 | End of Data (usually in this case Offset is also 0) | |||||||
0 | Length | Offset | - | Copy Length+2 bytes from next_output_pos-0x100+offset to the output |
In the table, each cc stands for a control code bit. The second data byte is divided into 5+3 bits for mode 01.
Tool
Crediar re'ed the decompression from the code and translated it to C code.
You can download a tool with the source to decompress data from any given offset here.