-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImageToVoxel.cs
49 lines (43 loc) · 1.71 KB
/
ImageToVoxel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Voxels.CommandLine {
internal static class ImageToVoxel {
public static VoxelData Import(string path, Color[] palette) {
using (var bitmap = SKBitmap.Decode(path)) {
if (bitmap == null) return null;
var size = new XYZ(bitmap.Width, 1, bitmap.Height);
var voxelData = new VoxelDataBytes(size, palette);
for (var x = 0; x < bitmap.Width; x++) {
for (var y = 0; y < bitmap.Height; y++) {
var p = bitmap.GetPixel(x, y);
if (p.Alpha != 0) {
var c = new Color(p.Red, p.Green, p.Blue, p.Alpha);
var i = Array.IndexOf(palette, c);
voxelData[new XYZ(x, 0, bitmap.Height-y-1)] = new Voxel((uint)i);
}
}
}
return voxelData;
}
}
public static void ExtractColors(string path, HashSet<Color> colorsUsed) {
using (var bitmap = SKBitmap.Decode(path)) {
if (bitmap == null) return;
for (var x = 0; x < bitmap.Width; x++) {
for (var y = 0; y < bitmap.Height; y++) {
var p = bitmap.GetPixel(x, y);
if (p.Alpha != 0) {
var c = new Color(p.Red, p.Green, p.Blue, p.Alpha);
colorsUsed.Add(c);
}
}
}
}
}
}
}