瀏覽代碼

Add reencoding check

Kevin Lee 1 年之前
父節點
當前提交
8b631b0718
共有 5 個文件被更改,包括 74 次插入20 次删除
  1. 1 0
      src/config/mod.rs
  2. 7 0
      src/config/object.rs
  3. 2 3
      src/ezcad/file.rs
  4. 1 1
      src/ezcad/pen.rs
  5. 63 16
      src/main.rs

+ 1 - 0
src/config/mod.rs

@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
 
 use self::pen::{ClonePen, PatchPen, PatternPen};
 
+pub mod object;
 pub mod pen;
 
 #[derive(Debug, Serialize, Deserialize)]

+ 7 - 0
src/config/object.rs

@@ -0,0 +1,7 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "PascalCase")]
+pub struct Export {
+    layer: u32,
+}

+ 2 - 3
src/ezcad/file.rs

@@ -2,7 +2,7 @@ use std::io::{Seek, SeekFrom, Write};
 
 use binrw::{BinRead, BinResult, BinWrite, BinWriterExt, Endian, FilePtr64};
 
-use crate::{array_of::ArrayOf, layer::Layer, pen::PenHeader};
+use crate::{array_of::ArrayOf, layer::Layer, pen::PenHeader, types::Rgba};
 
 #[derive(BinRead, Debug)]
 pub struct EzCadHeader {
@@ -78,8 +78,7 @@ pub struct Thumbnail {
     _zeros_2: [u32; 3],
 
     #[br(count = dimension_x * dimension_y)]
-    // pub pixels: Vec<Rgba>, // Takes ~1s longer
-    pub pixels: Vec<u32>,
+    pub pixels: Vec<Rgba>, // Takes a few ms longer than Vec<u32>
 }
 
 #[derive(BinRead, BinWrite, Debug)]

+ 1 - 1
src/ezcad/pen.rs

@@ -5,7 +5,7 @@ use diff::Diff;
 
 use crate::{
     field_of::FieldOf,
-    types::{Field, Rgba, WString, WobbleType, F64, U32, Bool},
+    types::{Bool, Field, Rgba, WString, WobbleType, F64, U32},
 };
 
 #[derive(BinRead, Debug)]

+ 63 - 16
src/main.rs

@@ -1,11 +1,16 @@
-use std::{fs::File, path::PathBuf, time::Instant};
-
-use binrw::{BinRead, BinWriterExt};
+use std::{
+    fs::File,
+    io::{Cursor, Read, Write},
+    path::PathBuf,
+    time::Instant,
+};
+
+use binrw::{BinRead, BinWrite, BinWriterExt};
 use clap::Parser;
 use clap_verbosity_flag::{InfoLevel, Verbosity};
 use diff::Diff;
 use ezcad::file::EzCadHeader;
-use log::{info, trace};
+use log::{info, trace, warn};
 
 use crate::config::{Config, Operations};
 
@@ -42,15 +47,36 @@ fn main() {
         .filter_level(cli.verbose.log_level_filter())
         .init();
 
+    info!("Reading input file {}", cli.input.to_string_lossy());
     let mut input: File = File::open(cli.input).expect("Failed to open input file");
 
+    // Deserialize to memory buffer for perf
+    let mut input_file: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    input
+        .read_to_end(input_file.get_mut())
+        .expect("Failed to read input file");
+
+    // Deserialize as EZCAD format
+    let time: Instant = Instant::now();
+    let mut file: EzCadHeader =
+        EzCadHeader::read_le(&mut input_file).expect("Failed to parse input file as EZCAD format");
+    trace!("Input file decode time: {:?}", time.elapsed());
+
+    // Re-encode input file and check that it matches original
+    info!("Validating re-encoding of input file");
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+
     let time: Instant = Instant::now();
-    let mut input_file: EzCadHeader =
-        EzCadHeader::read_le(&mut input).expect("Failed to parse input file as EZCAD format");
-    info!("Input decode time: {:?}", time.elapsed());
+    file.write_le(&mut buffer)
+        .expect("Failed to reserialize input file as EZCAD format");
+    trace!("Input file re-encode time: {:?}", time.elapsed());
+
+    if buffer.into_inner().as_slice().ne(input_file.get_ref()) {
+        warn!("Re-encoded input file does not match original file!");
+    }
 
     // Print info on pens with non-default settings
-    for pen in input_file
+    for pen in file
         .pens_offset
         .data
         .pens
@@ -61,12 +87,13 @@ fn main() {
     }
 
     // Print all objects
-    for layer in input_file.layers_offset.iter() {
+    for layer in file.layers_offset.iter() {
         for object in layer.objects.iter() {
             trace!("{:#?}", object);
         }
     }
 
+    // Process diff
     if let Some(diff) = cli.diff {
         info!("Processing diff file {}", diff.to_string_lossy());
         let mut diff: File = File::open(diff).expect("Failed to open diff file");
@@ -74,27 +101,47 @@ fn main() {
             EzCadHeader::read_le(&mut diff).expect("Failed to parse diff file as EZCAD format");
 
         // Diff pens
-        info!("{:#?}", input_file.pens_offset.data.pens.diff(&diff_file.pens_offset.data.pens));
+        info!(
+            "{:#?}",
+            file.pens_offset
+                .data
+                .pens
+                .diff(&diff_file.pens_offset.data.pens)
+        );
 
         // Diff objects
-        info!("{:#?}", input_file.layers_offset.value.diff(&diff_file.layers_offset.value));
+        info!(
+            "{:#?}",
+            file.layers_offset
+                .value
+                .diff(&diff_file.layers_offset.value)
+        );
     }
 
+    // Process config
     if let Some(config) = cli.config {
         info!("Processing config file {}", config.to_string_lossy());
         let config: String = std::fs::read_to_string(config).expect("Failed to open config file");
         let config: Config = serde_yaml::from_str(&config).expect("Failed to parse config file");
 
         // Patch pen settings
-        config.ops.apply(&mut input_file.pens_offset.data.pens);
+        config.ops.apply(&mut file.pens_offset.data.pens);
     }
 
+    // Process output
     if let Some(output) = cli.output {
-        let mut output = File::create(output).expect("Failed to open output file");
+        // Serialize to memory buffer for perf
+        let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
         let time: Instant = Instant::now();
+        buffer
+            .write_le(&file)
+            .expect("Failed to serialize contents as EZCAD format");
+        trace!("Output file encode time: {:?}", time.elapsed());
+
+        // Write buffer to output file
+        let mut output = File::create(output).expect("Failed to open output file");
         output
-            .write_le(&input_file)
-            .expect("Failed to serialize contents in EZCAD format");
-        info!("Output encode time: {:?}", time.elapsed());
+            .write_all(buffer.into_inner().as_slice())
+            .expect("Failed to write to output file");
     }
 }