Getty Ritter vor 10 Monaten
Ursprung
Commit
1fb3d71b6b
1 geänderte Dateien mit 93 neuen und 0 gelöschten Zeilen
  1. 93 0
      src/file.rs

+ 93 - 0
src/file.rs

@@ -0,0 +1,93 @@
+use crate::data::Mapping;
+use crate::image::{Image, Pixel};
+
+use std::collections::HashMap;
+
+#[derive(Debug, Clone)]
+pub enum StitchType {
+    Normal,
+    HalfUp,
+    HalfDown,
+}
+
+#[derive(Debug, Clone)]
+pub struct Stitch {
+    pub color: ColorIdx,
+    pub typ: StitchType,
+}
+
+#[derive(Debug)]
+pub struct Color {
+    pub name: String,
+    pub color: Pixel,
+}
+
+#[derive(Debug, Clone, Copy)]
+pub struct ColorIdx {
+    pub idx: usize,
+}
+
+#[derive(Debug)]
+pub struct ThymeFile {
+    pub width: u32,
+    pub height: u32,
+    pub palette: Vec<Color>,
+    pub payload: Vec<Option<Stitch>>,
+}
+
+impl ThymeFile {
+    pub fn from_image_and_config(image: &Image, Mapping(mapping): &Mapping) -> ThymeFile {
+        let width = image.width;
+        let height = image.height;
+
+        let lookup = mapping
+            .iter()
+            .enumerate()
+            .map(|(idx, (pixel, _))| (*pixel, ColorIdx { idx }))
+            .collect::<HashMap<Pixel, ColorIdx>>();
+
+        let payload = image
+            .iter()
+            .map(|(_, pixel)| {
+                if let Some(color) = lookup.get(&pixel) {
+                    Some(Stitch {
+                        color: *color,
+                        typ: StitchType::Normal,
+                    })
+                } else {
+                    None
+                }
+            })
+            .collect();
+
+        let palette = mapping
+            .iter()
+            .map(|(color, name)| Color {
+                color: *color,
+                name: name.clone(),
+            })
+            .collect();
+
+        ThymeFile {
+            width,
+            height,
+            palette,
+            payload,
+        }
+    }
+
+    pub fn blank(width: u32, height: u32) -> ThymeFile {
+        let palette = Vec::new();
+        let mut payload = Vec::with_capacity((width * height) as usize);
+        for _ in 0..(width * height) {
+            payload.push(None);
+        }
+
+        ThymeFile {
+            width,
+            height,
+            palette,
+            payload,
+        }
+    }
+}