Crate sega_cmp [] [src]

This crate provides support for compressing files using Sega's CMP run-length encoding scheme, which was used in games for the Sega Saturn game console.

Run-length encoding is a simple kind of compression which works by analyzing data for repetitive sequences of data (or "runs"); any repeating sequences can be replaced by commands to repeat X data N times.

For example, imagine the text AAAAAAAABBBBAABA. If you described it in terms of patterns, you could think of it as being 8A4B2A1B1A - which takes up 10 bytes instead of 16.

Sega included CMP in the official Saturn SDK; they provided a closed-source commandline tool to compress data and source code to do compression from within games. As a result, many games used it for data with lots of repetitive content such as text and graphics. This crate's CMP implementation aims to be 100% compatible with the original decoder; it produces identical compressed data to the original encoder. Its output has been tested to work in commercial Saturn games which use CMP.

This crate provides two basic functions: the header-generating create_header, and the data-creating compress. Most Saturn games store both in the same place, with the header followed immedaitely by the compressed data.





Used to denote the width of data to compress. Because CMP compression was created to be used on the SH-2 CPU, the size names come from the three sizes of data used on the SH-2.



Given a slice containing u8s, this function compresses the data in increments of size. On success, returns a slice containing the compressed data.


Writes a CMP header; this header is expected to come at the beginning of a compressed CMP stream.