Skip to content

Commit

Permalink
Format code and add static checks (Orama-Interactive#599)
Browse files Browse the repository at this point in the history
* gdformat .

* Lint code - Part 1

* Format code - Part 2

* Lint code - Part 2

Trying to fix the max allowed line length errors

* Add normal_map_invert_y to the image .import files

Because of Godot 3.4

* Do not call private methods outside of the script's scope

Lint code - Part 3

* Format code - Part 3

* Fixed more line length exceeded errors - Lint code Part 3

* Export array of licenses - Lint code part 4

* Clean hint_tooltip code from Global

Removes a lot of lines of code

* Create static-checks.yml

* Fix FreeType's license
  • Loading branch information
OverloadedOrama authored Nov 25, 2021
1 parent 41b5db4 commit c6b9a1f
Show file tree
Hide file tree
Showing 201 changed files with 4,790 additions and 3,119 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/static-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Static Checks 📊
on:
push:
branches-ignore:
- gh-pages
- l10n_master
pull_request:
paths:
- "addons/*"
- "src/*"

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: pip3 install gdtoolkit
- run: gdformat --diff .
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: pip3 install gdtoolkit
- run: gdlint .
5 changes: 3 additions & 2 deletions addons/gdgifexporter/converter.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extends Reference


var _shader: Shader


Expand Down Expand Up @@ -29,7 +28,9 @@ func _convert(image: Image, colors: Array) -> PoolByteArray:
VisualServer.canvas_item_set_parent(ci_rid, canvas)
var texture = ImageTexture.new()
texture.create_from_image(image)
VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(Vector2(0, 0), image.get_size()), texture)
VisualServer.canvas_item_add_texture_rect(
ci_rid, Rect2(Vector2(0, 0), image.get_size()), texture
)

var mat_rid = VisualServer.material_create()
VisualServer.material_set_shader(mat_rid, _shader.get_rid())
Expand Down
107 changes: 53 additions & 54 deletions addons/gdgifexporter/exporter.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
extends Reference

enum Error { OK = 0, EMPTY_IMAGE = 1, BAD_IMAGE_FORMAT = 2 }

enum Error {
OK = 0,
EMPTY_IMAGE = 1,
BAD_IMAGE_FORMAT = 2
}


var little_endian = preload('./little_endian.gd').new()
var lzw = preload('./gif-lzw/lzw.gd').new()
var little_endian = preload("./little_endian.gd").new()
var lzw = preload("./gif-lzw/lzw.gd").new()
var converter = preload("./converter.gd")

var last_color_table := []
Expand All @@ -24,11 +18,14 @@ func _init(_width: int, _height: int):
add_logical_screen_descriptor(_width, _height)
add_application_ext("NETSCAPE", "2.0", [1, 0, 0])


func export_file_data() -> PoolByteArray:
return data + PoolByteArray([0x3b])


func add_header() -> void:
data += 'GIF'.to_ascii() + '89a'.to_ascii()
data += "GIF".to_ascii() + "89a".to_ascii()


func add_logical_screen_descriptor(width: int, height: int) -> void:
# not Global Color Table Flag
Expand All @@ -46,6 +43,7 @@ func add_logical_screen_descriptor(width: int, height: int) -> void:
data.append(background_color_index)
data.append(pixel_aspect_ratio)


func add_application_ext(app_iden: String, app_auth_code: String, _data: Array) -> void:
var extension_introducer := 0x21
var extension_label := 0xff
Expand All @@ -61,6 +59,7 @@ func add_application_ext(app_iden: String, app_auth_code: String, _data: Array)
data += PoolByteArray(_data)
data.append(0)


# finds the image color table. Stops if the size gets larger than 256.
func find_color_table(image: Image) -> Dictionary:
image.lock()
Expand All @@ -72,7 +71,8 @@ func find_color_table(image: Image) -> Dictionary:
int(image_data[i]),
int(image_data[i + 1]),
int(image_data[i + 2]),
int(image_data[i + 3])]
int(image_data[i + 3])
]
if not color in result:
result[color] = result.size()
if result.size() > 256:
Expand All @@ -81,25 +81,21 @@ func find_color_table(image: Image) -> Dictionary:
image.unlock()
return result


func find_transparency_color_index(color_table: Dictionary) -> int:
for color in color_table:
if color[3] == 0:
return color_table[color]
return -1

func change_colors_to_codes(image: Image,
color_palette: Dictionary,
transparency_color_index: int) -> PoolByteArray:

func change_colors_to_codes(image: Image, color_palette: Dictionary, transparency_color_index: int) -> PoolByteArray:
image.lock()
var image_data: PoolByteArray = image.get_data()
var result: PoolByteArray = PoolByteArray([])

for i in range(0, image_data.size(), 4):
var color: Array = [
image_data[i],
image_data[i + 1],
image_data[i + 2],
image_data[i + 3]]
var color: Array = [image_data[i], image_data[i + 1], image_data[i + 2], image_data[i + 3]]

if color in color_palette:
if color[3] == 0 and transparency_color_index != -1:
Expand All @@ -108,11 +104,12 @@ func change_colors_to_codes(image: Image,
result.append(color_palette[color])
else:
result.append(0)
push_warning('change_colors_to_codes: color not found! [%d, %d, %d, %d]' % color)
push_warning("change_colors_to_codes: color not found! [%d, %d, %d, %d]" % color)

image.unlock()
return result


# makes sure that the color table is at least size 4.
func make_proper_size(color_table: Array) -> Array:
var result := [] + color_table
Expand All @@ -121,15 +118,18 @@ func make_proper_size(color_table: Array) -> Array:
result.append([0, 0, 0, 0])
return result


func calc_delay_time(frame_delay: float) -> int:
return int(ceil(frame_delay / 0.01))


func color_table_to_indexes(colors: Array) -> PoolByteArray:
var result: PoolByteArray = PoolByteArray([])
for i in range(colors.size()):
result.append(i)
return result


func add_frame(image: Image, frame_delay: float, quantizator: Script) -> int:
# check if image is of good format
if image.get_format() != Image.FORMAT_RGBA8:
Expand All @@ -144,7 +144,7 @@ func add_frame(image: Image, frame_delay: float, quantizator: Script) -> int:
var image_converted_to_codes: PoolByteArray
var transparency_color_index: int = -1
var color_table: Array
if found_color_table.size() <= 256: # we don't need to quantize the image.
if found_color_table.size() <= 256: # we don't need to quantize the image.
# try to find transparency color index.
transparency_color_index = find_transparency_color_index(found_color_table)
# if didn't found transparency color index but there is atleast one
Expand All @@ -153,37 +153,36 @@ func add_frame(image: Image, frame_delay: float, quantizator: Script) -> int:
found_color_table[[0, 0, 0, 0]] = found_color_table.size()
transparency_color_index = found_color_table.size() - 1
image_converted_to_codes = change_colors_to_codes(
image, found_color_table, transparency_color_index)
image, found_color_table, transparency_color_index
)
color_table = make_proper_size(found_color_table.keys())
else: # we have to quantize the image.
else: # we have to quantize the image.
var quantization_result: Array = quantizator.new().quantize(image)
image_converted_to_codes = quantization_result[0]
color_table = quantization_result[1]
# transparency index should always be as the first element of color table.
transparency_color_index = 0 if quantization_result[2] else -1

last_color_table = color_table
last_transparency_index = transparency_color_index

var delay_time := calc_delay_time(frame_delay)

var color_table_indexes := color_table_to_indexes(color_table)
var compressed_image_result: Array = lzw.compress_lzw(
image_converted_to_codes,
color_table_indexes)
image_converted_to_codes, color_table_indexes
)
var compressed_image_data: PoolByteArray = compressed_image_result[0]
var lzw_min_code_size: int = compressed_image_result[1]

add_graphic_constrol_ext(delay_time, transparency_color_index)
add_image_descriptor(
Vector2.ZERO,
image.get_size(),
color_table_bit_size(color_table))
add_image_descriptor(Vector2.ZERO, image.get_size(), color_table_bit_size(color_table))
add_local_color_table(color_table)
add_image_data_block(lzw_min_code_size, compressed_image_data)

return Error.OK


# adds frame with last color informations
func add_frame_with_lci(image: Image, frame_delay: float) -> int:
# check if image is of good format
Expand All @@ -194,27 +193,27 @@ func add_frame_with_lci(image: Image, frame_delay: float) -> int:
if image.is_empty():
return Error.EMPTY_IMAGE

var image_converted_to_codes: PoolByteArray = converter.new().get_similar_indexed_datas(image, last_color_table)

var image_converted_to_codes: PoolByteArray = converter.new().get_similar_indexed_datas(
image, last_color_table
)

var color_table_indexes := color_table_to_indexes(last_color_table)
var compressed_image_result: Array = lzw.compress_lzw(
image_converted_to_codes,
color_table_indexes)
image_converted_to_codes, color_table_indexes
)
var compressed_image_data: PoolByteArray = compressed_image_result[0]
var lzw_min_code_size: int = compressed_image_result[1]

var delay_time := calc_delay_time(frame_delay)

add_graphic_constrol_ext(delay_time, last_transparency_index)
add_image_descriptor(
Vector2.ZERO,
image.get_size(),
color_table_bit_size(last_color_table))
add_image_descriptor(Vector2.ZERO, image.get_size(), color_table_bit_size(last_color_table))
add_local_color_table(last_color_table)
add_image_data_block(lzw_min_code_size, compressed_image_data)

return Error.OK


func add_graphic_constrol_ext(_delay_time: float, tci: int = -1) -> void:
var extension_introducer: int = 0x21
var graphic_control_label: int = 0xf9
Expand All @@ -226,7 +225,7 @@ func add_graphic_constrol_ext(_delay_time: float, tci: int = -1) -> void:

var delay_time: int = _delay_time
var transparent_color_index: int = tci if tci != -1 else 0

data.append(extension_introducer)
data.append(graphic_control_label)

Expand All @@ -237,27 +236,26 @@ func add_graphic_constrol_ext(_delay_time: float, tci: int = -1) -> void:

data.append(0)

func add_image_descriptor(pos: Vector2,
size: Vector2,
l_color_table_size: int) -> void:

func add_image_descriptor(pos: Vector2, size: Vector2, l_color_table_size: int) -> void:
var image_separator: int = 0x2c
var packed_fields: int = 0b10000000 | (0b111 & l_color_table_size)

var little_endian = preload('./little_endian.gd').new()

data.append(image_separator)
data += little_endian.int_to_2bytes(int(pos.x)) # left pos
data += little_endian.int_to_2bytes(int(pos.y)) # top pos
data += little_endian.int_to_2bytes(int(size.x)) # width
data += little_endian.int_to_2bytes(int(size.y)) # height
data += little_endian.int_to_2bytes(int(pos.x)) # left pos
data += little_endian.int_to_2bytes(int(pos.y)) # top pos
data += little_endian.int_to_2bytes(int(size.x)) # width
data += little_endian.int_to_2bytes(int(size.y)) # height
data.append(packed_fields)


func color_table_bit_size(color_table: Array) -> int:
if color_table.size() <= 1:
return 0
var bit_size := int(ceil(log(color_table.size()) / log(2.0)))
return bit_size - 1


func add_local_color_table(color_table: Array) -> void:
for color in color_table:
data.append(color[0])
Expand All @@ -271,9 +269,10 @@ func add_local_color_table(color_table: Array) -> void:
for i in range(proper_size - color_table.size()):
data += PoolByteArray([0, 0, 0])


func add_image_data_block(lzw_min_code_size: int, _data: PoolByteArray) -> void:
data.append(lzw_min_code_size)

var block_size_index: int = 0
var i: int = 0
var data_index: int = 0
Expand Down
2 changes: 1 addition & 1 deletion addons/gdgifexporter/gif-lzw/lsbbitpacker.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Reference


class LSB_LZWBitPacker:
class LSBLZWBitPacker:
var bit_index: int = 0
var stream: int = 0

Expand Down
2 changes: 1 addition & 1 deletion addons/gdgifexporter/gif-lzw/lsbbitunpacker.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Reference


class LSB_LZWBitUnpacker:
class LSBLZWBitUnpacker:
var chunk_stream: PoolByteArray
var bit_index: int = 0
var byte: int
Expand Down
Loading

0 comments on commit c6b9a1f

Please sign in to comment.