Browse Source

Header parsing

Kevin Lee 1 year ago
parent
commit
e3020f644a
5 changed files with 112 additions and 12 deletions
  1. 3 1
      imhex.txt
  2. 20 1
      src/ezcad/basic_field.rs
  3. 75 0
      src/ezcad/file.rs
  4. 1 0
      src/ezcad/mod.rs
  5. 13 10
      src/main.rs

+ 3 - 1
imhex.txt

@@ -192,7 +192,9 @@ struct Rectangle : ObjCommon {
     Field round_bottom_right;
     Field round_top_right;
     Field round_top_left;
-    Field unknown[2];
+    Field unknown;
+    u32 length;
+    u8 bytes[length];
 };
 
 struct Circle : ObjCommon {

+ 20 - 1
src/ezcad/basic_field.rs

@@ -1,6 +1,8 @@
+#![allow(dead_code)]
+
 use binrw::{BinRead, BinWrite};
 
-use crate::field_of::FieldOf;
+use crate::{array_of::ArrayOf, field_of::FieldOf};
 
 /// Wrapper for basic types determined by initial u32 size
 #[derive(BinRead, BinWrite, PartialEq, Debug)]
@@ -9,3 +11,20 @@ pub enum BasicField {
     U32(FieldOf<u32>),
     Double(FieldOf<f64>),
 }
+
+/// Generic field with structure of length + data
+pub type Field = ArrayOf<u8>;
+
+#[derive(BinRead, BinWrite, PartialEq, Debug)]
+pub struct Rgba {
+    red: u8,
+    green: u8,
+    blue: u8,
+    alpha: u8,
+}
+
+#[derive(BinRead, BinWrite, PartialEq, Debug)]
+pub struct Point {
+    x: f64,
+    y: f64,
+}

+ 75 - 0
src/ezcad/file.rs

@@ -0,0 +1,75 @@
+use binrw::{binread, BinRead, BinWrite, FilePtr64};
+
+use crate::{
+    array_of::ArrayOf,
+    basic_field::{Field, Rgba},
+    field_of::FieldOf,
+};
+
+#[derive(BinRead, Debug)]
+pub struct Header {
+    _magic: [u16; 8],
+    _unknown_1: [u8; 0x150],
+    pub thumbnail_offset: FilePtr64<Thumbnail>,
+    pub pens_offset: FilePtr64<PenHeader>,
+    _unknown_offset: FilePtr64<()>,
+    pub layers_offset: FilePtr64<Layers>,
+    _unknown_2: [u8; 0x1A8],
+}
+
+#[derive(BinRead, BinWrite, Debug)]
+pub struct Thumbnail {
+    _zeros_1: u32,
+    dimension_x: u32,
+    dimension_y: u32,
+    _unknown_1: u32, // 0x320?
+    _unknown_2: u32, // 0x200001?
+    _zeros_2: [u32; 3],
+
+    #[br(count = dimension_x * dimension_y)]
+    pixels: Vec<Rgba>,
+}
+
+#[derive(BinRead, Debug)]
+pub struct PenHeader {
+    pen_count: u32,
+    #[br(args {
+        inner: binrw::args! {
+            pen_count
+        }
+    })]
+    start_offset: FilePtr64<Pens>,
+}
+
+#[derive(BinRead, BinWrite, Debug)]
+#[br(import {pen_count: u32})]
+pub struct Pens {
+    #[br(count = pen_count)]
+    pens: Vec<Pen>
+}
+
+#[derive(BinRead, BinWrite, Debug)]
+pub struct Pen {
+
+}
+
+#[derive(BinRead, BinWrite, Debug)]
+pub struct Layers {}
+
+// #[binread]
+// #[derive(Debug)]
+// pub struct File {
+//     header: Header,
+
+//     #[br(count = 40000)] // 200x200?
+//     thumbnail: Vec<u32>,
+//     _padding: u32,
+
+//     // #[br(temp)]
+//     // #[bw(try_calc(u32::try_from(pens.len())))]
+//     pen_count: u32, // Number of pens
+//     pen_offset: u64, // Offset from start of file of pens (should immediately follow this)
+
+//     // #[br(count = pen_count)]
+//     // pens: Vec<ArrayOf<Field>>,
+// }

+ 1 - 0
src/ezcad/mod.rs

@@ -1,3 +1,4 @@
 pub mod array_of;
 pub mod basic_field;
 pub mod field_of;
+pub mod file;

+ 13 - 10
src/main.rs

@@ -1,6 +1,8 @@
-use std::{fs::read, path::PathBuf};
+use std::{fs::{self}, path::PathBuf};
 
+use binrw::BinRead;
 use clap::Parser;
+use ezcad::file::Header;
 
 #[derive(Debug, Parser)]
 struct Cli {
@@ -8,13 +10,13 @@ struct Cli {
     #[arg(short, long)]
     input: PathBuf,
 
-    /// Output file to write to
-    #[arg(short, long)]
-    output: PathBuf,
+    // /// Output file to write to
+    // #[arg(short, long)]
+    // output: PathBuf,
 
-    /// Configuration file
-    #[arg(short, long)]
-    config: PathBuf,
+    // /// Configuration file
+    // #[arg(short, long)]
+    // config: PathBuf,
 
     #[command(flatten)]
     verbose: clap_verbosity_flag::Verbosity,
@@ -28,8 +30,9 @@ fn main() {
 
     let args: Cli = Cli::parse();
 
-    let _input = read(args.input).unwrap();
-    let _confing = read(args.config).unwrap();
+    let mut input = fs::File::open(args.input).unwrap();
+    // let _config = read(args.config).unwrap();
 
-    println!("Hello, world!");
+    let file = Header::read_le(&mut input).unwrap();
+    dbg!(file);
 }