Post Reply 
LZW compression tool
09-03-2023, 09:01 PM (This post was last modified: 09-04-2023 12:53 AM by komame.)
Post: #1
LZW compression tool
I've written a tool for compressing and decompressing files using the LZW algorithm for HP Prime.

For now, this should be treated as a Proof of Concept, but I plan to rewrite it in Python - hopefully, we'll have a stable version soon Wink

How to use this?

LZW(input, output, mode)
input - file name to compress
output - file name for saving compressed data
mode - dictionary behavior mode after reaching 10000 entries

allowed values for mode:
0 - reset the dictionary and create it anew based on subsequent data;
1 - maintain the dictionary unchanged until the end of the compression process.

UNLZW(input, output)
input - name of the file for decompression
output - name of the decompressed file

LZW("myFile", "myFile_lzw", 0) // compression
UNLZW("myFile_lzw", "myFile_orig") // decompression

During file compression, a dictionary is created in memory, which grows as the compression progresses. The dictionary uses a PPL list, so when it reaches 10000 entries, you have the choice to either reset the dictionary or prevent further growth without resetting what is already in the dictionary. You control this through the "mode" parameter. In most cases, setting the "mode" to 0 yields better compression results, but there are instances where the data type changes significantly in subsequent parts of the compressed file, and in such cases, option 1 might yield better results.

For small files where the dictionary size doesn't exceed 10000 entries, the choice of "mode" will have no impact on compression efficiency
The "mode" parameter is not required for decompression because the decompression routine can recognize and apply the appropriate decompression method.

If a file is not compressible, the result of compression may be a larger file than the original file.

What types of files are best suited for this compression mechanism?
1. Files with a minimum size of 500 bytes (sometimes smaller ones can be compressed, but it depends on the file's compressibility).
2. Text files / program source code / data files for game levels, etc.
3. Objects like matrices or lists.

What types of files should not be compressed?
1. Graphic files: png, jpg, G0-G9.
2. Other compressed files.

Unfortunately, I've been disappointed with PPL because it's very slow for iterative algorithms, making this solution not very useful for large files (> 100kb) on a real HP Prime.

The second problem is the limitation of the PPL list size to 10000 entries, which limits the maximum dictionary size and, as a result, negatively impacts compression quality.
Nevertheless, compression and decompression are working correctly, and it compresses files reasonably well.

You can save program source code files for compression by using the AFiles function to retrieve them from the Program Catalog:
AFiles("new_filename") := Programs("Program_name_from_Catalog")

Have fun!

Attached File(s)
.hpprgm  LZW.hpprgm (Size: 7.02 KB / Downloads: 6)

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
Post Reply 

User(s) browsing this thread: 1 Guest(s)