aboutsummaryrefslogtreecommitdiff
path: root/addons/voxel-core/classes/readers/image.gd
diff options
context:
space:
mode:
authorjacopograndi <jacopo.grandi@outlook.it>2021-12-09 00:52:59 +0100
committerjacopograndi <jacopo.grandi@outlook.it>2021-12-09 00:52:59 +0100
commit0f518727c28d3204415db14c7ca0e4f7cb653677 (patch)
treea595121771b69a52f17154d8e5c61b7e4b2b8190 /addons/voxel-core/classes/readers/image.gd
working
Diffstat (limited to 'addons/voxel-core/classes/readers/image.gd')
-rw-r--r--addons/voxel-core/classes/readers/image.gd40
1 files changed, 40 insertions, 0 deletions
diff --git a/addons/voxel-core/classes/readers/image.gd b/addons/voxel-core/classes/readers/image.gd
new file mode 100644
index 0000000..3c19d2f
--- /dev/null
+++ b/addons/voxel-core/classes/readers/image.gd
@@ -0,0 +1,40 @@
+class_name ImageReader, "res://addons/voxel-core/assets/logos/MagicaVoxel.png"
+extends Reference
+# Image file reader
+
+
+
+## Public Methods
+# Reads images pixels, returns voxel content and voxel palette
+static func read(image : Image) -> Dictionary:
+ var result := {
+ "error": OK,
+ "voxels": {},
+ "palette": [],
+ }
+
+ image.lock()
+ for x in range(image.get_width()):
+ for y in range(image.get_height()):
+ if image.get_pixel(x, y).a > 0:
+ var color := image.get_pixel(x, y)
+ color.a = 1.0
+ var index = result["palette"].find(color)
+ if index == -1:
+ index = result["palette"].size()
+ result["palette"].append(color)
+ result["voxels"][Vector3(x, -y, 0).round()] = index
+ image.unlock()
+
+ for index in range(result["palette"].size()):
+ result["palette"][index] = Voxel.colored(result["palette"][index])
+
+ return result
+
+
+static func read_file(image_path : String) -> Dictionary:
+ var image := Image.new()
+ var err = image.load(image_path)
+ if err == OK:
+ return read(image)
+ return { "error": err }