inochi2d-parser 0.2.0

Typed parser and intermediate representation for Inochi2D INP/INX files
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! INR: a flattened, binary runtime format for Inochi2D puppets.
//!
//! INX/INP are authoring/interchange formats: deeply nested JSON trees with
//! UUID cross-references and encoded textures. INR is the engine-facing
//! counterpart (in the spirit of glTF): a small JSON index plus one binary
//! blob with all heavy data laid out for direct GPU upload.
//!
//! # Container layout
//!
//! ```text
//! [0..4)   magic  b"INR1"
//! [4..8)   u32 LE container version (currently 1)
//! [8..12)  u32 LE JSON chunk length (4-aligned, space padded)
//! [12..16) u32 LE BIN chunk length
//! [16..)   JSON chunk, then BIN chunk
//! ```
//!
//! The JSON chunk is an [`InrDoc`]: node tree already flattened in
//! pre-order with parent *indices*, params/animations/masks referencing
//! nodes and params by index, and [`BufferView`]s pointing into the BIN
//! chunk. Textures are raw RGBA8 with their alpha/color-space semantics
//! declared per texture (`premultiplied`, `color_space`).
//!
//! Files written by the exporter (feature `inr-export`) store STRAIGHT
//! alpha sRGB textures with edge dilation, so consumers can sample them
//! through hardware sRGB views and premultiply in-shader (blending in
//! linear space) without fringe artifacts — the convention expected by
//! engines like Bevy, Unity, Unreal or Godot.

mod import;
pub use import::{open_puppet, to_puppet};

#[cfg(feature = "inr-export")]
mod export;
#[cfg(feature = "inr-export")]
pub use export::{export_puppet, export_to_file, write_container};

use serde::{Deserialize, Serialize};

pub const MAGIC: [u8; 4] = *b"INR1";
pub const VERSION: u32 = 1;

#[derive(Debug)]
pub enum InrError {
    Io(std::io::Error),
    Json(serde_json::Error),
    #[cfg(feature = "inr-export")]
    Texture(image::ImageError),
    BadMagic,
    UnsupportedVersion(u32),
    Truncated,
    BadView(u32),
    UnsupportedTexture(usize),
}

impl std::fmt::Display for InrError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "io error: {e}"),
            Self::Json(e) => write!(f, "invalid JSON chunk: {e}"),
            #[cfg(feature = "inr-export")]
            Self::Texture(e) => write!(f, "texture decode error: {e}"),
            Self::BadMagic => write!(f, "not an INR file (bad magic)"),
            Self::UnsupportedVersion(v) => write!(f, "unsupported INR container version {v}"),
            Self::Truncated => write!(f, "truncated INR container"),
            Self::BadView(id) => write!(f, "buffer view {id} out of range"),
            Self::UnsupportedTexture(i) => write!(f, "texture {i}: unsupported pixel format"),
        }
    }
}

impl std::error::Error for InrError {}

impl From<std::io::Error> for InrError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}
impl From<serde_json::Error> for InrError {
    fn from(e: serde_json::Error) -> Self {
        Self::Json(e)
    }
}
#[cfg(feature = "inr-export")]
impl From<image::ImageError> for InrError {
    fn from(e: image::ImageError) -> Self {
        Self::Texture(e)
    }
}

// --- string enums (unknown values fall back to spec defaults on read) ------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrNodeKind {
    Part,
    Composite,
    Mask,
    #[serde(rename = "meshgroup")]
    MeshGroup,
    #[serde(rename = "simplephysics")]
    SimplePhysics,
    Camera,
    #[default]
    #[serde(other)]
    Node,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrBlendMode {
    Multiply,
    Screen,
    Overlay,
    Darken,
    Lighten,
    ColorDodge,
    LinearDodge,
    Add,
    ColorBurn,
    HardLight,
    SoftLight,
    Subtract,
    Difference,
    Exclusion,
    Inverse,
    DestinationIn,
    ClipToLower,
    SliceFromLower,
    #[default]
    #[serde(other)]
    Normal,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrMaskMode {
    Dodge,
    #[default]
    #[serde(other)]
    Mask,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrPhysicsModel {
    SpringPendulum,
    #[default]
    #[serde(other)]
    Pendulum,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrMapMode {
    Xy,
    LengthAngle,
    Yx,
    #[default]
    #[serde(other)]
    AngleLength,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrMergeMode {
    Multiplicative,
    Override,
    Forced,
    #[default]
    #[serde(other)]
    Additive,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrInterpolation {
    Stepped,
    Nearest,
    Cubic,
    #[default]
    #[serde(other)]
    Linear,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrBindingKind {
    #[default]
    Scalar,
    Deform,
    /// Unknown kinds must not be misread as scalar data.
    #[serde(other)]
    Other,
}

/// Binding target. Unknown targets keep their raw string so they survive
/// a read→write round trip.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "String", into = "String")]
pub enum InrBindingTarget {
    TranslateX,
    TranslateY,
    TranslateZ,
    RotateX,
    RotateY,
    RotateZ,
    ScaleX,
    ScaleY,
    Deform,
    Opacity,
    Other(String),
}

impl From<String> for InrBindingTarget {
    fn from(s: String) -> Self {
        match s.as_str() {
            "transform.t.x" => Self::TranslateX,
            "transform.t.y" => Self::TranslateY,
            "transform.t.z" => Self::TranslateZ,
            "transform.r.x" => Self::RotateX,
            "transform.r.y" => Self::RotateY,
            "transform.r.z" => Self::RotateZ,
            "transform.s.x" => Self::ScaleX,
            "transform.s.y" => Self::ScaleY,
            "deform" => Self::Deform,
            "opacity" => Self::Opacity,
            _ => Self::Other(s),
        }
    }
}

impl From<InrBindingTarget> for String {
    fn from(t: InrBindingTarget) -> Self {
        match t {
            InrBindingTarget::TranslateX => "transform.t.x".into(),
            InrBindingTarget::TranslateY => "transform.t.y".into(),
            InrBindingTarget::TranslateZ => "transform.t.z".into(),
            InrBindingTarget::RotateX => "transform.r.x".into(),
            InrBindingTarget::RotateY => "transform.r.y".into(),
            InrBindingTarget::RotateZ => "transform.r.z".into(),
            InrBindingTarget::ScaleX => "transform.s.x".into(),
            InrBindingTarget::ScaleY => "transform.s.y".into(),
            InrBindingTarget::Deform => "deform".into(),
            InrBindingTarget::Opacity => "opacity".into(),
            InrBindingTarget::Other(s) => s,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrTextureFormat {
    #[default]
    Rgba8,
    Bc7,
    #[serde(other)]
    Other,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InrColorSpace {
    Linear,
    #[default]
    #[serde(other)]
    Srgb,
}

/// Parsed container: JSON document + binary blob.
#[derive(Debug)]
pub struct InrModel {
    pub doc: InrDoc,
    pub bin: Vec<u8>,
}

impl InrModel {
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, InrError> {
        if bytes.len() < 16 {
            return Err(InrError::Truncated);
        }
        if bytes[0..4] != MAGIC {
            return Err(InrError::BadMagic);
        }
        let u32_at = |o: usize| u32::from_le_bytes(bytes[o..o + 4].try_into().unwrap());
        let version = u32_at(4);
        if version != VERSION {
            return Err(InrError::UnsupportedVersion(version));
        }
        let json_len = u32_at(8) as usize;
        let bin_len = u32_at(12) as usize;
        let json_end = 16usize.checked_add(json_len).ok_or(InrError::Truncated)?;
        let bin_end = json_end.checked_add(bin_len).ok_or(InrError::Truncated)?;
        if bytes.len() < bin_end {
            return Err(InrError::Truncated);
        }
        let doc: InrDoc = serde_json::from_slice(&bytes[16..json_end])?;
        Ok(Self {
            doc,
            bin: bytes[json_end..bin_end].to_vec(),
        })
    }

    pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, InrError> {
        Self::from_bytes(&std::fs::read(path)?)
    }

    /// Raw bytes of a buffer view.
    pub fn view_bytes(&self, view: u32) -> Result<&[u8], InrError> {
        let v = self
            .doc
            .buffer_views
            .get(view as usize)
            .ok_or(InrError::BadView(view))?;
        let start = v.offset as usize;
        let end = start
            .checked_add(v.length as usize)
            .ok_or(InrError::BadView(view))?;
        self.bin.get(start..end).ok_or(InrError::BadView(view))
    }

    /// Copying read: safe regardless of `bin` alignment.
    pub fn view_f32(&self, view: u32) -> Result<Vec<f32>, InrError> {
        let b = self.view_bytes(view)?;
        if !b.len().is_multiple_of(4) {
            return Err(InrError::BadView(view));
        }
        Ok(bytemuck::pod_collect_to_vec(b))
    }

    pub fn view_u32(&self, view: u32) -> Result<Vec<u32>, InrError> {
        let b = self.view_bytes(view)?;
        if !b.len().is_multiple_of(4) {
            return Err(InrError::BadView(view));
        }
        Ok(bytemuck::pod_collect_to_vec(b))
    }
}

// --- JSON schema (unknown fields are ignored for forward compat) ----------

#[derive(Debug, Serialize, Deserialize)]
pub struct InrDoc {
    #[serde(default)]
    pub asset: Asset,
    #[serde(default)]
    pub meta: Meta,
    pub physics: Physics,
    pub buffer_views: Vec<BufferView>,
    #[serde(default)]
    pub textures: Vec<TextureDesc>,
    /// Flattened pre-order: a parent always precedes its children.
    pub nodes: Vec<InrNode>,
    #[serde(default)]
    pub params: Vec<InrParam>,
    #[serde(default)]
    pub animations: Vec<InrAnimation>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Asset {
    #[serde(default)]
    pub generator: String,
    #[serde(default)]
    pub version: u32,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Meta {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rigger: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artist: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rights: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub copyright: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub license_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contact: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_version: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Physics {
    pub pixels_per_meter: f32,
    pub gravity: f32,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct BufferView {
    pub offset: u32,
    pub length: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TextureDesc {
    pub width: u32,
    pub height: u32,
    /// Pixel layout, currently always `Rgba8`.
    pub format: InrTextureFormat,
    /// Encoding of the RGB channels.
    #[serde(default)]
    pub color_space: InrColorSpace,
    /// RGB premultiplied by alpha (in `color_space`).
    #[serde(default)]
    pub premultiplied: bool,
    pub view: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrNode {
    pub name: String,
    pub uuid: u32,
    /// Index into `nodes`; absent on the root.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<u32>,
    pub kind: InrNodeKind,
    pub enabled: bool,
    pub zsort: f32,
    #[serde(default)]
    pub lock_to_root: bool,
    pub translation: [f32; 3],
    pub rotation: [f32; 3],
    pub scale: [f32; 2],
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mesh: Option<InrMesh>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub part: Option<InrPart>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub composite: Option<InrComposite>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub physics: Option<InrPhysics>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrMesh {
    pub vertex_count: u32,
    /// View: f32 x/y pairs (`vertex_count * 2` floats).
    pub positions: u32,
    /// View: f32 u/v pairs.
    pub uvs: u32,
    /// View: u32 triangle indices.
    pub indices: u32,
    pub origin: [f32; 2],
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrPart {
    /// Texture indices [albedo, emissive, bump]; -1 = none.
    pub textures: [i32; 3],
    pub blend_mode: InrBlendMode,
    pub tint: [f32; 3],
    pub screen_tint: [f32; 3],
    pub opacity: f32,
    #[serde(default)]
    pub emission_strength: f32,
    pub mask_threshold: f32,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub masks: Vec<InrMask>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrComposite {
    pub blend_mode: InrBlendMode,
    pub tint: [f32; 3],
    pub screen_tint: [f32; 3],
    pub opacity: f32,
    pub mask_threshold: f32,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub masks: Vec<InrMask>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrMask {
    /// Index into `nodes`.
    pub node: u32,
    pub mode: InrMaskMode,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrPhysics {
    /// Index into `params`; -1 = unresolved.
    pub param: i32,
    pub model: InrPhysicsModel,
    pub map_mode: InrMapMode,
    pub gravity: f32,
    pub length: f32,
    pub frequency: f32,
    pub angle_damping: f32,
    pub length_damping: f32,
    pub output_scale: [f32; 2],
    #[serde(default)]
    pub local_only: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrParam {
    pub name: String,
    pub uuid: u32,
    pub is_vec2: bool,
    pub min: [f32; 2],
    pub max: [f32; 2],
    pub defaults: [f32; 2],
    pub axis_points: [Vec<f32>; 2],
    pub merge_mode: InrMergeMode,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bindings: Vec<InrBinding>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrBinding {
    /// Index into `nodes`.
    pub node: u32,
    pub target: InrBindingTarget,
    pub interpolation: InrInterpolation,
    pub x_count: u32,
    pub y_count: u32,
    /// Row-major [x][y] authored flags, flattened.
    pub is_set: Vec<bool>,
    pub kind: InrBindingKind,
    pub view: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrAnimation {
    pub name: String,
    pub timestep: f32,
    #[serde(default)]
    pub additive: bool,
    pub length: u32,
    #[serde(default)]
    pub lead_in: u32,
    #[serde(default)]
    pub lead_out: u32,
    #[serde(default = "one")]
    pub weight: f32,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub lanes: Vec<InrLane>,
}

fn one() -> f32 {
    1.0
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InrLane {
    /// Index into `params`; -1 = unresolved.
    pub param: i32,
    /// 0 = X, 1 = Y.
    pub target: u8,
    pub interpolation: InrInterpolation,
    pub merge_mode: InrMergeMode,
    /// [frame, value, tension] per keyframe.
    pub keyframes: Vec<[f32; 3]>,
}