oxideav-cli-convert 0.0.8

ImageMagick-style convert engine for the oxideav CLI — translates IM arg syntax into an oxideav-pipeline Job
Documentation
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! End-to-end: build a tiny [`Scene3D`] (one triangle), encode it as
//! STL on disk, run `convert` to translate STL → OBJ and STL → glTF,
//! then assert the output files exist with sensible byte signatures.
//!
//! Cargo-feature-gated on `mesh3d` — the test crate is empty when
//! the feature is off (matches the side-channel module's gating).

#![cfg(feature = "mesh3d")]

use std::fs;
use std::path::PathBuf;

use oxideav_cli_convert::run as convert_run;
use oxideav_core::RuntimeContext;
use oxideav_mesh3d::{Mesh, Mesh3DRegistry, Node, Primitive, Scene3D, Topology, Transform};

fn ctx() -> RuntimeContext {
    // 3D→raster renders encode through the `oxideav-image` gateway, so
    // the raster formats come from the registry.
    let mut ctx = RuntimeContext::new();
    oxideav_png::register(&mut ctx);
    oxideav_bmp::register(&mut ctx);
    oxideav_mjpeg::register(&mut ctx);
    oxideav_webp::register(&mut ctx);
    oxideav_gif::register(&mut ctx);
    oxideav_tiff::register(&mut ctx);
    oxideav_qoi::register(&mut ctx);
    oxideav_ico::register(&mut ctx);
    ctx
}

fn temp_dir(name: &str) -> PathBuf {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.subsec_nanos())
        .unwrap_or(0);
    let mut p = std::env::temp_dir();
    p.push(format!(
        "oxideav-cli-convert-mesh3d-{name}-{}-{}",
        std::process::id(),
        nanos
    ));
    fs::create_dir_all(&p).expect("temp dir");
    p
}

/// Build a single-triangle [`Scene3D`] suitable for round-tripping
/// through every 3D format we wire (STL/OBJ/glTF). Keeps the topology
/// to plain `Triangles` + raw positions so the OBJ encoder (which
/// requires triangulated input) doesn't reject it.
fn make_one_triangle_scene() -> Scene3D {
    let mut prim = Primitive::new(Topology::Triangles);
    prim.positions = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
    let mesh = Mesh::new("triangle".to_string()).with_primitive(prim);

    let mut scene = Scene3D::new();
    scene.meshes.push(mesh);

    let node = Node {
        name: Some("triangle-node".to_string()),
        mesh: Some(oxideav_mesh3d::MeshId(0)),
        transform: Transform::identity(),
        ..Node::default()
    };
    scene.nodes.push(node);
    scene.roots.push(oxideav_mesh3d::NodeId(0));

    scene
}

/// Encode the scene as binary STL and write it to `path`. Goes through
/// the same registry the convert verb uses so the bytes that land on
/// disk match what the CLI sees.
fn write_stl_fixture(path: &std::path::Path, scene: &Scene3D) {
    let mut reg = Mesh3DRegistry::new();
    oxideav_meta::populate_mesh3d_registry(&mut reg);
    let mut enc = reg
        .encoder_for_extension("stl")
        .expect("STL encoder registered");
    let bytes = enc.encode(scene).expect("STL encode succeeds");
    fs::write(path, bytes).expect("write STL fixture");
}

#[test]
fn convert_stl_to_obj_writes_obj_with_v_lines() {
    let dir = temp_dir("stl-to-obj");
    let stl_path = dir.join("input.stl");
    let obj_path = dir.join("output.obj");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        obj_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("convert STL→OBJ succeeds");

    assert!(obj_path.exists(), "OBJ output should exist");
    let body = fs::read_to_string(&obj_path).expect("read OBJ output");
    // Wavefront OBJ vertex lines start with `v `; expect at least one.
    assert!(
        body.lines().any(|l| l.starts_with("v ")),
        "OBJ output should contain `v` (vertex) lines, got:\n{body}"
    );
    // And a face line — STL→OBJ via Scene3D round-trip should
    // preserve the single triangle.
    assert!(
        body.lines().any(|l| l.starts_with("f ")),
        "OBJ output should contain `f` (face) lines, got:\n{body}"
    );
}

#[test]
fn convert_stl_to_gltf_writes_json() {
    let dir = temp_dir("stl-to-gltf");
    let stl_path = dir.join("input.stl");
    let gltf_path = dir.join("output.gltf");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        gltf_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("convert STL→glTF succeeds");

    assert!(gltf_path.exists(), "glTF output should exist");
    let body = fs::read_to_string(&gltf_path).expect("read glTF output");
    // glTF JSON envelope should mention the asset block.
    assert!(
        body.contains("\"asset\""),
        "glTF output should be JSON with an \"asset\" key, got:\n{body}"
    );
}

#[test]
fn convert_obj_to_gltf_works_end_to_end() {
    // Build an OBJ fixture via the registry (so we don't have to
    // hand-author the text format), then convert OBJ → glTF.
    let dir = temp_dir("obj-to-gltf");
    let obj_path = dir.join("input.obj");
    let gltf_path = dir.join("output.gltf");

    let scene = make_one_triangle_scene();
    {
        let mut reg = Mesh3DRegistry::new();
        oxideav_meta::populate_mesh3d_registry(&mut reg);
        let mut enc = reg
            .encoder_for_extension("obj")
            .expect("OBJ encoder registered");
        let bytes = enc.encode(&scene).expect("OBJ encode succeeds");
        fs::write(&obj_path, bytes).expect("write OBJ fixture");
    }

    let args = vec![
        obj_path.to_string_lossy().into_owned(),
        gltf_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("convert OBJ→glTF succeeds");

    let body = fs::read_to_string(&gltf_path).expect("read glTF output");
    assert!(body.contains("\"asset\""), "glTF should mention asset");
}

#[test]
fn convert_stl_to_glb_writes_binary_container() {
    let dir = temp_dir("stl-to-glb");
    let stl_path = dir.join("input.stl");
    let glb_path = dir.join("output.glb");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        glb_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("convert STL→GLB succeeds");

    let bytes = fs::read(&glb_path).expect("read GLB output");
    // GLB binary container starts with the magic `glTF` (0x46546C67 LE).
    assert!(
        bytes.len() >= 4 && &bytes[..4] == b"glTF",
        "GLB output should start with the `glTF` magic, got first 4 bytes: {:?}",
        &bytes[..bytes.len().min(4)]
    );
}

#[test]
fn convert_3d_input_to_raster_renders_png() {
    // STL → PNG now goes through the 3D→raster software renderer
    // instead of being rejected. The output PNG should exist + start
    // with the PNG magic bytes.
    let dir = temp_dir("stl-to-png-renders");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG should render via mesh3d_render");

    let bytes = fs::read(&png_path).expect("read PNG output");
    assert!(
        bytes.len() >= 8 && &bytes[..8] == b"\x89PNG\r\n\x1a\n",
        "PNG output should start with the PNG magic, got: {:?}",
        &bytes[..bytes.len().min(8)]
    );
}

#[test]
fn convert_3d_input_to_unrelated_output_errors() {
    // Anything that's neither a 3D output nor a known raster output
    // (e.g. `.mp4`) is still rejected with the pairing-mismatch error.
    let dir = temp_dir("stl-to-mp4-rejected");
    let stl_path = dir.join("input.stl");
    let mp4_path = dir.join("output.mp4");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        mp4_path.to_string_lossy().into_owned(),
    ];
    let err = convert_run(&args, &ctx()).expect_err("STL→MP4 should fail");
    let msg = format!("{err:?}");
    assert!(
        msg.contains("must pair with a 3D output") || msg.contains("must pair with"),
        "expected pairing-mismatch error, got: {msg}"
    );
}

#[test]
fn convert_stl_to_usdz_round_trips() {
    // USDZ is now a round-trip target — encoder ships in oxideav-usdz
    // and the runner's MESH3D_OUTPUT_EXTS includes "usdz".
    let dir = temp_dir("stl-to-usdz");
    let stl_path = dir.join("input.stl");
    let usdz_path = dir.join("output.usdz");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        usdz_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("convert STL→USDZ succeeds");

    let bytes = fs::read(&usdz_path).expect("read USDZ output");
    // USDZ is a STORED ZIP; first 4 bytes are the local-file-header
    // signature `PK\x03\x04`.
    assert!(
        bytes.len() >= 4 && &bytes[..4] == b"PK\x03\x04",
        "USDZ output should start with PK\\x03\\x04, got: {:?}",
        &bytes[..bytes.len().min(4)]
    );
}

#[test]
fn convert_3d_input_to_raster_renders_with_gouraud_shading() {
    // Round-next: -render gouraud must be honoured end-to-end.
    let dir = temp_dir("stl-gouraud");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-render".into(),
        "gouraud".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -render gouraud should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn convert_3d_input_to_raster_renders_with_phong_shading() {
    // Round-next: -render phong must be honoured end-to-end.
    let dir = temp_dir("stl-phong");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-render".into(),
        "phong".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -render phong should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn convert_3d_input_to_raster_with_camera_and_projection() {
    // Round-next: -camera + -projection + -fov + -bg + -light all
    // pass through to the renderer cleanly.
    let dir = temp_dir("stl-cam-ortho");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-projection".into(),
        "orthographic".into(),
        "-camera".into(),
        "30,45,1.5".into(),
        "-fov".into(),
        "45".into(),
        "-light".into(),
        "60,30,1.0".into(),
        "-bg".into(),
        "#202020".into(),
        "-render".into(),
        "gouraud".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG with all renderer flags should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn fbx_extension_is_recognised_as_mesh3d_input() {
    // Once oxideav-fbx is wired in, the runner should claim `.fbx`
    // as a 3D input. We can't easily author a real binary FBX in a
    // smoke test (the format needs a 27-byte magic + a recursive node
    // tree), so just assert the recognition + that random bytes
    // produce a decoder error (NOT an "unknown extension" error).
    use oxideav_cli_convert::mesh3d_runner::is_mesh3d_input;
    assert!(is_mesh3d_input("foo.fbx"));
    assert!(is_mesh3d_input("FOO.FBX"));

    let dir = temp_dir("fbx-bad-bytes");
    let fbx_path = dir.join("input.fbx");
    let gltf_path = dir.join("output.gltf");
    fs::write(&fbx_path, b"not really an fbx file").expect("write bogus FBX");

    let args = vec![
        fbx_path.to_string_lossy().into_owned(),
        gltf_path.to_string_lossy().into_owned(),
    ];
    let err = convert_run(&args, &ctx()).expect_err("bogus FBX should fail at decode");
    let msg = format!("{err:?}");
    // The decoder owns the message; we just want to be sure the
    // failure didn't come from "no decoder registered".
    assert!(
        !msg.contains("no 3D decoder registered for input extension '.fbx'"),
        "FBX should be wired; decoder must produce its own error, got: {msg}"
    );
}

#[test]
fn convert_3d_input_to_raster_renders_with_normal_debug() {
    // Round 45: -render normal-debug paints normals as RGB.
    let dir = temp_dir("stl-normal-debug");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-render".into(),
        "normal-debug".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -render normal-debug should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn convert_3d_input_to_raster_renders_with_depth_debug() {
    // Round 45: -render depth-debug paints NDC z as grayscale.
    let dir = temp_dir("stl-depth-debug");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-render".into(),
        "depth-debug".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -render depth-debug should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn convert_3d_input_to_raster_with_aa_supersampling() {
    // Round 45: -aa N must be honoured end-to-end and produce the
    // same output dimensions as the no-aa render (only the smoothness
    // changes — the framebuffer footprint is invisible to the user).
    let dir = temp_dir("stl-aa");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-resize".into(),
        "64x64".into(),
        "-aa".into(),
        "4".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -aa 4 should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
    // PNG IHDR chunk starts at byte 8, length 4 bytes (== 13), then
    // chunk type 4 bytes (b"IHDR"), then 4 bytes width, 4 bytes height.
    let width = u32::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]);
    let height = u32::from_be_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]);
    assert_eq!(width, 64, "AA must not change output width");
    assert_eq!(height, 64, "AA must not change output height");
}

#[test]
fn convert_3d_input_to_raster_aa_one_means_no_supersampling() {
    // -aa 1 is documented as "off"; must succeed and produce a valid
    // PNG, even though it's a no-op compared to omitting the flag.
    let dir = temp_dir("stl-aa1");
    let stl_path = dir.join("input.stl");
    let png_path = dir.join("output.png");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        "-aa".into(),
        "1".into(),
        png_path.to_string_lossy().into_owned(),
    ];
    convert_run(&args, &ctx()).expect("STL→PNG -aa 1 should succeed");
    let bytes = fs::read(&png_path).expect("read PNG");
    assert!(&bytes[..8] == b"\x89PNG\r\n\x1a\n");
}

#[test]
fn convert_3d_input_with_printf_template_errors() {
    // 3D scenes are single-document — a `%d` template makes no sense.
    let dir = temp_dir("stl-with-template-rejected");
    let stl_path = dir.join("input.stl");

    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);

    let args = vec![
        stl_path.to_string_lossy().into_owned(),
        dir.join("out-%02d.obj").to_string_lossy().into_owned(),
    ];
    let err = convert_run(&args, &ctx()).expect_err("STL + %d should fail");
    let msg = format!("{err:?}");
    assert!(
        msg.contains("`%d` template"),
        "expected template-rejection error, got: {msg}"
    );
}

/// 3D→raster reaches every still-image format the registry encodes,
/// not only the four registry-free families: the same render as TIFF,
/// GIF, QOI and WebP decodes back to the PNG's samples.
#[test]
fn convert_3d_input_to_registry_raster_formats() {
    let dir = temp_dir("stl-to-registry-formats");
    let stl_path = dir.join("input.stl");
    let scene = make_one_triangle_scene();
    write_stl_fixture(&stl_path, &scene);
    let c = ctx();
    let render = |ext: &str| {
        let out = dir.join(format!("output.{ext}"));
        convert_run(
            &[
                stl_path.to_string_lossy().into_owned(),
                "-resize".into(),
                "64x48".into(),
                "-alpha".into(),
                "remove".into(),
                "-alpha".into(),
                "off".into(),
                out.to_string_lossy().into_owned(),
            ],
            &c,
        )
        .unwrap_or_else(|e| panic!("stl → {ext}: {e}"));
        oxideav_image::open(&c, &out)
            .unwrap_or_else(|e| panic!("{ext} decodes: {e}"))
            .into_primary()
    };
    let png = render("png");
    assert_eq!((png.width(), png.height()), (64, 48));
    let reference = png.to_rgb8().unwrap();
    for ext in ["tif", "qoi", "webp", "bmp"] {
        let img = render(ext);
        assert_eq!((img.width(), img.height()), (64, 48), "{ext}");
        assert_eq!(img.to_rgb8().unwrap(), reference, "{ext} samples");
    }
    // GIF quantises to a palette; the flat-shaded triangle on a
    // background is a handful of colours, so it stays exact too.
    assert_eq!(render("gif").to_rgb8().unwrap(), reference, "gif samples");
}