aboutsummaryrefslogtreecommitdiff
path: root/vgcodec/src/main.rs
blob: 9450894e15593c66bb6ca02fae35965bc37da61b (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
pub mod app;
pub mod diff;
pub mod export;
pub mod paint;

use app::App;
use diff::Differ;
use image::EncodableLayout;
use wgpu::{Extent3d, Queue, Texture, TextureUsages};

use crate::export::Exporter;

fn main() {
    env_logger::init_from_env("LOG");
    pollster::block_on(run());
}

async fn run() {
    let app = app::App::new().await;

    let App { device, queue, .. } = app.as_ref();

    let img_target = image::open("a/a.png").unwrap().into_rgba8();
    let img_initial = image::open("a/initial.png").unwrap().into_rgba8();

    let size = wgpu::Extent3d {
        width: img_initial.width(),
        height: img_initial.height(),
        depth_or_array_layers: 1,
    };

    let create_texture = || {
        device.create_texture(&wgpu::TextureDescriptor {
            size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8Unorm,
            usage: TextureUsages::COPY_DST
                | TextureUsages::TEXTURE_BINDING
                | TextureUsages::COPY_SRC,
            label: None,
        })
    };

    let tex_target = create_texture();
    let tex_approx = create_texture();

    write_texture(queue, &tex_target, img_target.as_bytes(), size);
    write_texture(queue, &tex_approx, img_initial.as_bytes(), size);

    let mut differ = Differ::new(&app, size, &tex_approx, &tex_target);
    let exporter = Exporter::new(&app, size);

    println!("{}", differ.run().await);

    exporter.run(&tex_approx, "a/approx.png").await;
}

pub fn write_texture(queue: &Queue, target: &Texture, data: &[u8], size: Extent3d) {
    queue.write_texture(
        wgpu::ImageCopyTexture {
            texture: &target,
            mip_level: 0,
            origin: wgpu::Origin3d::ZERO,
            aspect: wgpu::TextureAspect::All,
        },
        &data,
        wgpu::ImageDataLayout {
            offset: 0,
            bytes_per_row: Some(std::num::NonZeroU32::try_from((size.width * 4) as u32).unwrap()),
            rows_per_image: None,
        },
        size,
    );
}