Skip to content

jegor377/godot-gif-lzw

Repository files navigation

godot-gif-lzw

GIF's LZW compression/decompression done in Godot. Whole code is based on GIF specification and this website.

Usage

Firstly, use code from realese only. Secondly, you should have put "gif-lzw" directory somewhere in your project modules directory and preload "lzw.gd" file. Here is how to do it:

Compression

If you want to compress image, use compress_lzw(image: PoolByteArray, colors: PoolByteArray) -> Array.

extends Node2D


var lzw_module = load("res://gif-lzw/lzw.gd")
var lzw = lzw_module.new()


func _ready():
    var image: PoolByteArray = [
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 0, 0, 0, 0, 2, 2, 2,
        1, 1, 1, 0, 0, 0, 0, 2, 2, 2,
        2, 2, 2, 0, 0, 0, 0, 1, 1, 1,
        2, 2, 2, 0, 0, 0, 0, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
    ]

    var color_table: PoolByteArray = PoolByteArray([0, 1, 2, 3])

    var compressed_res: Array = lzw.compress_lzw(image, color_table)
    var compressed_data: PoolByteArray = compressed_res[0]
    var min_code_size: int = compressed_res[1]

Decompression

If you want to decompress image, use decompress_lzw(code_stream_data: PoolByteArray, min_code_size: int, colors: PoolByteArray) -> PoolByteArray.

extends Node2D


var lzw_module = load("res://gif-lzw/lzw.gd")
var lzw = lzw_module.new()


func _ready():
    var image: PoolByteArray = [
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
        1, 1, 1, 0, 0, 0, 0, 2, 2, 2,
        1, 1, 1, 0, 0, 0, 0, 2, 2, 2,
        2, 2, 2, 0, 0, 0, 0, 1, 1, 1,
        2, 2, 2, 0, 0, 0, 0, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
        2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
    ]

    var color_table: PoolByteArray = PoolByteArray([0, 1, 2, 3])

    var compressed_res: Array = lzw.compress_lzw(image, color_table)
    var compressed_data: PoolByteArray = compressed_res[0]
    var min_code_size: int = compressed_res[1]

    var decompressed_index_stream: Array = lzw.decompress_lzw(
            compressed_data,
            min_code_size,
            color_table)

Usefull docs