1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 09:09:47 +00:00

Update gdgifexporter (#299)

This commit is contained in:
Martin Novák 2020-08-08 23:16:03 +02:00 committed by GitHub
parent da656df5b7
commit e171e2ee65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 24 deletions

View file

@ -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:

View file

@ -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()

View file

@ -3,28 +3,21 @@ 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:
value &= (1 << bits_count) - 1
value <<= bit_index
stream |= value
bit_index += bits_count
while bit_index >= 8:
self.put_byte()
func pack() -> PoolByteArray:
@ -34,5 +27,5 @@ class LSB_LZWBitPacker:
func reset() -> void:
bit_index = 0
byte = 0
stream = 0
chunks = PoolByteArray([])

View file

@ -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);
}

View file

@ -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]