Skip to content

Commit

Permalink
feat: add bytes function
Browse files Browse the repository at this point in the history
  • Loading branch information
mfauzaan committed Feb 21, 2024
1 parent 5bf72aa commit a181105
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 20 deletions.
148 changes: 131 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ wgpu = "0.18"
bytemuck = { version = "1.12", features = ["derive"] }
# Async runtime
tokio = { version = "1.21", features = ["full"] }
tempfile = "3.10.0"

[profile.release]
strip = true
Expand Down
23 changes: 22 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ impl App {
}
}

pub async fn load_file(
pub async fn load_file_from_bytes(
&self,
file: Vec<u8>,
) -> Result<(ProcreateFile, GpuTexture, CompositorTarget), ProcreateError> {
let (file, gpu_textures) = ProcreateFile::open_from_bytes(file, &self.dev).unwrap();

let mut target = CompositorTarget::new(self.dev.clone());

target
.data
.flip_vertices(file.flipped.horizontally, file.flipped.vertically);
target.set_dimensions(file.size.width, file.size.height);

for _ in 0..file.orientation {
target.data.rotate_vertices(true);
target.set_dimensions(target.dim.height, target.dim.width);
}

Ok((file, gpu_textures, target))
}

pub async fn load_file_from_path(
&self,
path: PathBuf,
) -> Result<(ProcreateFile, GpuTexture, CompositorTarget), ProcreateError> {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() {
let app = App::new(dev);

let (file, gpu_textures, target) = app
.load_file(config_path)
.load_file_from_path(config_path)
.await
.expect("Unable to load file");

Expand Down
29 changes: 28 additions & 1 deletion src/procreate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ mod ir;
use self::ir::{IRData, ProcreateIRHierarchy, ProcreateIRLayer};
use crate::compositor::{dev::GpuHandle, tex::GpuTexture};
use crate::ns_archive::{NsArchiveError, NsKeyedArchive, Size, WrappedArray};
use bytes::Bytes;
use image::EncodableLayout;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::fs::OpenOptions;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::io::Cursor;
use std::io::Read;
use std::path::Path;
use std::sync::atomic::AtomicU32;
use tempfile::{tempdir, tempfile};
use thiserror::Error;
use zip::read::ZipArchive;

Expand Down Expand Up @@ -248,6 +252,29 @@ impl ProcreateFile {
Self::from_ns(archive, nka, dev)
}

pub fn open_from_bytes(
file_content: Vec<u8>,
dev: &GpuHandle,
) -> Result<(Self, GpuTexture), ProcreateError> {
// Specify the desired file path
let mut file = tempfile()?;
file.write_all(file_content.as_bytes())?;

let mapping = unsafe { memmap2::Mmap::map(&file)? };
let mut archive = ZipArchive::new(Cursor::new(&mapping[..]))?;

let nka: NsKeyedArchive = {
let mut document = archive.by_name("Document.archive")?;

let mut buf = Vec::with_capacity(document.size() as usize);
document.read_to_end(&mut buf)?;

NsKeyedArchive::from_reader(Cursor::new(buf))?
};

Self::from_ns(archive, nka, dev)
}

fn from_ns(
archive: ZipArchiveMmap<'_>,
nka: NsKeyedArchive,
Expand Down

0 comments on commit a181105

Please sign in to comment.