From e171e2ee655591f0e8c3b1cc57e06f6f2c698f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Nov=C3=A1k?= <42614907+novhack@users.noreply.github.com> Date: Sat, 8 Aug 2020 23:16:03 +0200 Subject: [PATCH] Update gdgifexporter (#299) --- addons/README.md | 2 +- addons/gdgifexporter/converter.gd | 27 ++++++++++++++--- addons/gdgifexporter/gif-lzw/lsbbitpacker.gd | 29 +++++++------------ addons/gdgifexporter/lookup_color.shader | 19 ++++++++++++ .../gdgifexporter/quantization/median_cut.gd | 2 +- 5 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 addons/gdgifexporter/lookup_color.shader diff --git a/addons/README.md b/addons/README.md index f30b9eaf8..0d97815ee 100644 --- a/addons/README.md +++ b/addons/README.md @@ -3,7 +3,7 @@ ## gdgifexporter - Upstream: https://github.com/jegor377/godot-gdgifexporter -- Version: git (9cdc448922717f069dd12e0377c1d9fc09d30f9f, 2020) +- Version: git (201123154acfb9a1e00149fa708b4f13645d88dc, 2020) - License: MIT Files extracted from source: diff --git a/addons/gdgifexporter/converter.gd b/addons/gdgifexporter/converter.gd index 54c7580c8..03598a301 100644 --- a/addons/gdgifexporter/converter.gd +++ b/addons/gdgifexporter/converter.gd @@ -1,7 +1,20 @@ extends Node -func setup(image: Image, colors: Array) -> PoolByteArray: +var _shader: Shader + + +func get_indexed_datas(image: Image, colors: Array) -> PoolByteArray: + _shader = preload("./lookup_color.shader") + return _convert(image, colors) + + +func get_similar_indexed_datas(image: Image, colors: Array) -> PoolByteArray: + _shader = preload("./lookup_similar.shader") + return _convert(image, colors) + + +func _convert(image: Image, colors: Array) -> PoolByteArray: var vp = VisualServer.viewport_create() var canvas = VisualServer.canvas_create() VisualServer.viewport_attach_canvas(vp, canvas) @@ -18,13 +31,13 @@ func setup(image: Image, colors: Array) -> PoolByteArray: texture.create_from_image(image) VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(Vector2(0, 0), image.get_size()), texture) - var shader = preload("./lookup_similar.shader") var mat_rid = VisualServer.material_create() - VisualServer.material_set_shader(mat_rid, shader.get_rid()) + VisualServer.material_set_shader(mat_rid, _shader.get_rid()) var lut = Image.new() lut.create(256, 1, false, Image.FORMAT_RGB8) + lut.fill(Color8(colors[0][0], colors[0][1], colors[0][2])) lut.lock() - for i in 256: + for i in colors.size(): lut.set_pixel(i, 0, Color8(colors[i][0], colors[i][1], colors[i][2])) var lut_tex = ImageTexture.new() lut_tex.create_from_image(lut) @@ -35,5 +48,11 @@ func setup(image: Image, colors: Array) -> PoolByteArray: VisualServer.viewport_set_vflip(vp, true) VisualServer.force_draw(false) image = VisualServer.texture_get_data(VisualServer.viewport_get_texture(vp)) + + VisualServer.free_rid(vp) + VisualServer.free_rid(canvas) + VisualServer.free_rid(ci_rid) + VisualServer.free_rid(mat_rid) + image.convert(Image.FORMAT_R8) return image.get_data() diff --git a/addons/gdgifexporter/gif-lzw/lsbbitpacker.gd b/addons/gdgifexporter/gif-lzw/lsbbitpacker.gd index a3434d61f97..c40b70901 100644 --- a/addons/gdgifexporter/gif-lzw/lsbbitpacker.gd +++ b/addons/gdgifexporter/gif-lzw/lsbbitpacker.gd @@ -3,29 +3,22 @@ extends Node class LSB_LZWBitPacker: var bit_index: int = 0 - var byte: int = 0 + var stream: int = 0 var chunks: PoolByteArray = PoolByteArray([]) - func get_bit(value: int, index: int) -> int: - return (value >> index) & 1 - - func set_bit(value: int, index: int) -> int: - return value | (1 << index) - func put_byte(): - chunks.append(byte) - bit_index = 0 - byte = 0 + chunks.append(stream & 0xff) + bit_index -= 8 + stream >>= 8 func write_bits(value: int, bits_count: int) -> void: - for i in range(bits_count): - if self.get_bit(value, i) == 1: - byte = self.set_bit(byte, bit_index) - - bit_index += 1 - if bit_index == 8: - self.put_byte() + value &= (1 << bits_count) - 1 + value <<= bit_index + stream |= value + bit_index += bits_count + while bit_index >= 8: + self.put_byte() func pack() -> PoolByteArray: if bit_index != 0: @@ -34,5 +27,5 @@ class LSB_LZWBitPacker: func reset() -> void: bit_index = 0 - byte = 0 + stream = 0 chunks = PoolByteArray([]) diff --git a/addons/gdgifexporter/lookup_color.shader b/addons/gdgifexporter/lookup_color.shader new file mode 100644 index 000000000..837910868 --- /dev/null +++ b/addons/gdgifexporter/lookup_color.shader @@ -0,0 +1,19 @@ +shader_type canvas_item; +render_mode unshaded; + +uniform sampler2D lut; + +void fragment() { + vec4 color = texture(TEXTURE, UV); + float index = 0.0; + if (color.a > 0.0) { + for (int i = 0; i < 256; i++) { + vec4 c = texture(lut, vec2((float(i) + 0.5) / 256.0, 0.5)); + if (c.rgb == color.rgb) { + index = float(i) / 255.0; + break; + } + } + } + COLOR = vec4(vec3(index), 1.0); +} \ No newline at end of file diff --git a/addons/gdgifexporter/quantization/median_cut.gd b/addons/gdgifexporter/quantization/median_cut.gd index e53d90292..823e9bf9a 100644 --- a/addons/gdgifexporter/quantization/median_cut.gd +++ b/addons/gdgifexporter/quantization/median_cut.gd @@ -144,5 +144,5 @@ func quantize_and_convert_to_codes(image: Image) -> Array: if transparency: color_array.push_front([0, 0, 0]) - var data: PoolByteArray = converter.setup(image, color_array) + var data: PoolByteArray = converter.get_similar_indexed_datas(image, color_array) return [data, color_array, transparency]