Skip to content

Commit

Permalink
Fixed GIF Native, added more infos for setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kanimaru committed Mar 11, 2024
1 parent 6c4568c commit e617968
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 74 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ The native image transformer uses a ported version of [vbousquet/godot-gif-impor
I can't guarantee that I ported it correctly from Godot 3 to 4 nor I can guarantee that the original Implementation was correctly.
This is an alterantive that has to be battle tested. But this one doesn't need any external Program to work (or even not) :D.
Also have a look at [jegor377/godot-gif-lzw](https://github.com/jegor377/godot-gif-lzw) that is used for the LZW compression.
The readme has good links for checking this implementation. Couple of parts was broken and I added a "fix"
I marked them with "Probably Buggy" and "Maybe wrong too" in case someone want to check the implementation or run into problems.
The readme has good links for checking this implementation. Couple of parts was broken and I added a "fix".

In case you want to use another way to convert the GIFs. You can easily create a class that has to support:

Expand Down Expand Up @@ -107,6 +106,11 @@ Also set the `Image Transformer` to `Magic Image Transformer` afterwards all emo
</dd>
<dt>Why is the generated folder commited to the repository?</dt>
<dd>This Library targets beginners and the API won't change much anyway. One step less for beginners of this library.</dd>
<dt>I changed scopes but my token is still invalid.</dt>
<dd>
The token can't auto refresh after scope changes always. In case it happens it is possible to remove the token manually or wait until the token runs up.
You can find the token in `user://auth.conf` it's encrypted. Just delete the file and the application will reauthorize next time.
</dd>
</dl>

## See also:
Expand All @@ -121,13 +125,11 @@ Inspired by: [GIFT](https://github.com/issork/gift/)
- Added New Chat and PubSubScopes
- Removed Whisper messages from the log
- fixed a problem when Twitch sends undocumented websocket messages

** TODO **
- add an addons folder to put the lib in
- pull the documentation folder out
- Changed the folder structure to match assetlibs default stuff
- Fixed bugs with native gif parser
- Added more informations to setup everything

**Known Bugs**
- couple of gifs are not parsing with native GIF

**Nice to Have**
- Add continious deployment to Godot AssetLib
Expand Down
37 changes: 21 additions & 16 deletions addons/twitcher/image_transformer/native/GIF2SpriteFramesPlugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@ extends EditorImportPlugin

class_name GifImporterNative

func _get_importer_name():
return "gif.animated.texture.plugin"
enum Presets { DEFAULT }

func _get_priority():
return 0;
func _get_importer_name() -> String:
return "gif.animated.texture.plugin"

func _get_visible_name():
func _get_visible_name() -> String:
return "Sprite Frames (Native)"

func _get_recognized_extensions():
func _get_recognized_extensions() -> PackedStringArray:
return ["gif"]

func _get_save_extension():
return "tres"
func _get_save_extension() -> String:
return "res"

func _get_resource_type():
func _get_resource_type() -> String:
return "SpriteFrames"

func _get_import_order():
return 0;
func _get_priority() -> float:
return 0.0;

func _get_preset_count():
return 1
func _get_preset_count() -> int:
return Presets.size()

func _get_preset_name(i):
func _get_preset_name(preset_index: int) -> String:
return "Default"

func _get_import_options(path: String, preset_index: int):
func _get_import_options(path: String, preset_index: int) -> Array[Dictionary]:
return []

func _import(source_file, save_path, options, platform_variants, gen_files):
func _get_import_order() -> int:
return 0

func _get_option_visibility(path: String, option_name: StringName, options: Dictionary) -> bool:
return true

func _import(source_file: String, save_path: String, options: Dictionary, platform_variants: Array[String], gen_files: Array[String]) -> Error:
var reader = GifReader.new()
var tex = reader.read(source_file)
if tex == null:
Expand Down
5 changes: 0 additions & 5 deletions addons/twitcher/image_transformer/native/GIFReader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func load_gif(data):
var sprite_frame = SpriteFrames.new()

img = Image.create(width, height, false, Image.FORMAT_RGBA8)
var animation_duration: float = 0.0;
while pos < data.size():
if data[pos] == 0x21: # Extension block
var ext_type = data[pos + 1]
Expand Down Expand Up @@ -99,21 +98,17 @@ func load_gif(data):
for y in range(0, img_height):
for x in range(0, img_width):
var c = decompressed[p]
# TODO: Probably Buggy
if transparency == 0 or c != frame_transparent_color:
img.set_pixel(img_left + x, img_top + y, local_lut[c])
p = p + 1
var frame = ImageTexture.create_from_image(img);
# Maybe wrong too
sprite_frame.add_frame(&"default", frame, frame_delay / 100.0);
animation_duration += frame_delay / 1000.0;
frame_anim_packed_info = -1
frame_transparent_color = -1
frame_delay = -1
frame_number = frame_number + 1
elif data[pos] == 0x3B: # Trailer
pos = pos + 1
# Maybe wrong too
sprite_frame.set_animation_speed(&"default", 1);
return sprite_frame

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class LSBLZWBitUnpacker:
return value | (1 << index)

func get_byte():
# TODO: Probably Buggy
if chunk_stream.size() > byte_index:
byte = chunk_stream[byte_index]
else:
byte = 0;
byte = chunk_stream[byte_index]
byte_index += 1
bit_index = 0

Expand All @@ -36,6 +32,9 @@ class LSBLZWBitUnpacker:
result_bit_index += 1
bit_index += 1

if chunk_stream.size() == byte_index && result_bit_index == bits_count:
return result;

if bit_index == 8:
get_byte()

Expand Down
22 changes: 12 additions & 10 deletions addons/twitcher/image_transformer/native/gif-lzw/lzw.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var lsbbitunpacker = preload("./lsbbitunpacker.gd")

class CodeEntry:
var sequence: PackedByteArray
var raw_array: Array
var raw_array: PackedByteArray

func _init(_sequence):
func _init(_sequence : PackedByteArray) -> void:
raw_array = _sequence
sequence = _sequence

func add(other):
func add(other) -> CodeEntry:
return CodeEntry.new(raw_array + other.raw_array)

func to_string():
func to_string() -> String:
var result: String = ""
for element in sequence:
result += str(element) + ", "
Expand All @@ -27,19 +27,19 @@ class CodeTable:
var counter: int = 0
var lookup: Dictionary = {}

func add(entry) -> int:
func add(entry: CodeEntry) -> int:
entries[counter] = entry
lookup[entry.raw_array] = counter
counter += 1
return counter

func find(entry) -> int:
func find(entry: CodeEntry) -> int:
return lookup.get(entry.raw_array, -1)

func has_entry(entry) -> bool:
func has_entry(entry: CodeEntry) -> bool:
return find(entry) != -1

func get_entry(index) -> CodeEntry:
func get_entry(index: int) -> CodeEntry:
return entries.get(index, null)

func to_string() -> String:
Expand All @@ -53,13 +53,11 @@ class CodeTable:
func log2(value: float) -> float:
return log(value) / log(2.0)


func get_bits_number_for(value: int) -> int:
if value == 0:
return 1
return int(ceil(log2(value + 1)))


func initialize_color_code_table(colors: PackedByteArray) -> CodeTable:
var result_code_table: CodeTable = CodeTable.new()
for color_id in colors:
Expand Down Expand Up @@ -176,6 +174,10 @@ func decompress_lzw(code_stream_data: PackedByteArray, min_code_size: int, color
code_table = initialize_color_code_table(colors)
current_code_size = min_code_size + 1
code = binary_code_stream.read_bits(current_code_size)
index_stream.append_array(code_table.get_entry(code).sequence)
prevcode = code
continue
## TODO HOW TO RESET CORRECTLY PREVCODE?!
elif code == clear_code_index + 1: # Stop when detected EOI Code.
break
# is CODE in the code table?
Expand Down
Loading

0 comments on commit e617968

Please sign in to comment.