threers 0.1.0

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
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
//! The block compressors of OpenEXR beyond ZIP: RLE, PIZ, PXR24 and B44/B44A,
//! and through [`exr_dwa`](super::exr_dwa) DWAA and DWAB.
//!
//! Each turns one chunk into the layout an uncompressed file stores: for every
//! scanline, every channel in name order, little-endian. Bad input returns
//! `None`, and nothing here indexes on a value read from the file unchecked.

use super::deflate::inflate_zlib;
use super::exr::exr_reorder;

const HALF: u8 = 1;

/// What a compressor needs to know of a channel.
pub(super) struct Channel<'a> {
    pub name: &'a str,
    pub pixel_type: u8,
    /// Marked perceptually linear in the channel list.
    pub p_linear: bool,
}

/// 16-bit words in one sample: the wavelet and B44 both work on these.
fn words(pixel_type: u8) -> usize {
    if pixel_type == HALF {
        1
    } else {
        2
    }
}

/// Decompress one chunk of `lines` scanlines, `width` samples per channel.
/// `channels` are in name order.
pub(super) fn decompress(
    compression: u8,
    data: &[u8],
    channels: &[Channel],
    width: usize,
    lines: usize,
) -> Option<Vec<u8>> {
    let types: Vec<u8> = channels.iter().map(|c| c.pixel_type).collect();
    let types = &types[..];
    let size = types.iter().map(|&t| words(t) * 2 * width).sum::<usize>() * lines;
    let out = match compression {
        1 => exr_reorder(&rle(data, size)?),
        2 | 3 => exr_reorder(&inflate_zlib(data).ok()?),
        4 => piz(data, types, width, lines, size)?,
        5 => pxr24(data, types, width, lines, size)?,
        6 | 7 => b44(data, channels, width, lines)?,
        8 | 9 => super::exr_dwa::decompress(data, channels, width, lines)?,
        _ => return None,
    };
    Some(out)
}

/// A negative count copies that many literals; any other repeats the next
/// byte count + 1 times.
pub(super) fn rle(data: &[u8], size: usize) -> Option<Vec<u8>> {
    let mut out = Vec::with_capacity(size);
    let mut i = 0;
    while i < data.len() {
        let count = data[i] as i8;
        i += 1;
        if count < 0 {
            let n = -(count as i32) as usize;
            out.extend_from_slice(data.get(i..i + n)?);
            i += n;
        } else {
            out.resize(out.len() + count as usize + 1, *data.get(i)?);
            i += 1;
        }
        if out.len() > size {
            return None;
        }
    }
    Some(out)
}

/// Each channel's samples are stored one byte plane at a time per scanline,
/// delta-coded across the row; FLOAT keeps only its top 24 bits.
fn pxr24(data: &[u8], types: &[u8], width: usize, lines: usize, size: usize) -> Option<Vec<u8>> {
    let packed = inflate_zlib(data).ok()?;
    let mut out = Vec::with_capacity(size);
    let mut pos = 0;
    for _ in 0..lines {
        for &t in types {
            let planes = match t {
                HALF => 2,
                2 => 3,
                _ => 4,
            };
            let row = packed.get(pos..pos + planes * width)?;
            pos += planes * width;
            let mut pixel = 0u32;
            for x in 0..width {
                let mut diff = 0u32;
                for p in 0..planes {
                    diff = diff << 8 | row[p * width + x] as u32;
                }
                if t == 2 {
                    diff <<= 8;
                }
                pixel = pixel.wrapping_add(diff);
                if t == HALF {
                    out.extend_from_slice(&(pixel as u16).to_le_bytes());
                } else {
                    out.extend_from_slice(&pixel.to_le_bytes());
                }
            }
        }
    }
    Some(out)
}

/// Channel planes, one after another, back into scanlines.
fn planes_to_scanlines(tmp: &[u16], types: &[u8], width: usize, lines: usize) -> Vec<u8> {
    let mut starts = Vec::with_capacity(types.len());
    let mut at = 0;
    for &t in types {
        starts.push(at);
        at += words(t) * width * lines;
    }
    let mut out = Vec::with_capacity(tmp.len() * 2);
    for y in 0..lines {
        for (&t, &start) in types.iter().zip(&starts) {
            let n = words(t) * width;
            for w in &tmp[start + y * n..start + (y + 1) * n] {
                out.extend_from_slice(&w.to_le_bytes());
            }
        }
    }
    out
}

fn piz(data: &[u8], types: &[u8], width: usize, lines: usize, size: usize) -> Option<Vec<u8>> {
    const BITMAP_SIZE: usize = 8192;
    let u16_at = |i: usize| Some(u16::from_le_bytes(data.get(i..i + 2)?.try_into().ok()?));
    let (min, max) = (u16_at(0)? as usize, u16_at(2)? as usize);
    let mut pos = 4;
    let mut bitmap = vec![0u8; BITMAP_SIZE];
    if min <= max {
        if max >= BITMAP_SIZE {
            return None;
        }
        let n = max - min + 1;
        bitmap[min..=max].copy_from_slice(data.get(pos..pos + n)?);
        pos += n;
    }

    // The values present, in order: the wavelet ran on their indices.
    let mut lut = vec![0u16; 1 << 16];
    let mut k = 0usize;
    for i in 0..1usize << 16 {
        if i == 0 || bitmap[i >> 3] & (1 << (i & 7)) != 0 {
            lut[k] = i as u16;
            k += 1;
        }
    }
    let max_value = k - 1;

    let length = u32::from_le_bytes(data.get(pos..pos + 4)?.try_into().ok()?) as usize;
    pos += 4;
    let mut tmp = huf_uncompress(data.get(pos..pos.checked_add(length)?)?, size / 2)?;

    let mut start = 0;
    for &t in types {
        let w = words(t);
        for j in 0..w {
            wav2_decode(&mut tmp, start + j, width, w, lines, width * w, max_value);
        }
        start += w * width * lines;
    }
    for v in &mut tmp {
        *v = lut[*v as usize];
    }
    Some(planes_to_scanlines(&tmp, types, width, lines))
}

fn wdec14(l: u16, h: u16) -> (u16, u16) {
    let (ls, hi) = (l as i16 as i32, h as i16 as i32);
    let ai = ls + (hi & 1) + (hi >> 1);
    (ai as u16, (ai - hi) as u16)
}

fn wdec16(l: u16, h: u16) -> (u16, u16) {
    const A_OFFSET: i32 = 1 << 15;
    let (m, d) = (l as i32, h as i32);
    let bb = (m - (d >> 1)) & 0xffff;
    let aa = (d + bb - A_OFFSET) & 0xffff;
    (aa as u16, bb as u16)
}

/// Undo the 2D Haar pyramid over `nx` by `ny` samples `ox` and `oy` apart.
fn wav2_decode(b: &mut [u16], at: usize, nx: usize, ox: usize, ny: usize, oy: usize, mx: usize) {
    if mx < 1 << 14 {
        wav2(b, at, nx, ox, ny, oy, wdec14);
    } else {
        wav2(b, at, nx, ox, ny, oy, wdec16);
    }
}

fn wav2(
    b: &mut [u16],
    at: usize,
    nx: usize,
    ox: usize,
    ny: usize,
    oy: usize,
    wdec: impl Fn(u16, u16) -> (u16, u16),
) {
    let n = nx.min(ny);
    if n == 0 {
        return;
    }
    let mut p = 1;
    while p <= n {
        p <<= 1;
    }
    p >>= 1;
    let mut p2 = p;
    p >>= 1;
    while p >= 1 {
        let (oy1, oy2, ox1, ox2) = (oy * p, oy * p2, ox * p, ox * p2);
        let ey = at + oy * (ny - p2);
        let mut py = at;
        while py <= ey {
            let ex = py + ox * (nx - p2);
            let mut px = py;
            while px <= ex {
                let (p01, p10) = (px + ox1, px + oy1);
                let p11 = p10 + ox1;
                let (i00, i10) = wdec(b[px], b[p10]);
                let (i01, i11) = wdec(b[p01], b[p11]);
                (b[px], b[p01]) = wdec(i00, i01);
                (b[p10], b[p11]) = wdec(i10, i11);
                px += ox2;
            }
            if nx & p != 0 {
                let p10 = px + oy1;
                (b[px], b[p10]) = wdec(b[px], b[p10]);
            }
            py += oy2;
        }
        if ny & p != 0 {
            let ex = py + ox * (nx - p2);
            let mut px = py;
            while px <= ex {
                let p01 = px + ox1;
                (b[px], b[p01]) = wdec(b[px], b[p01]);
                px += ox2;
            }
        }
        p2 = p;
        p >>= 1;
    }
}

const HUF_ENCSIZE: usize = (1 << 16) + 1;
const HUF_DECBITS: u32 = 14;
const HUF_DECSIZE: usize = 1 << HUF_DECBITS;
const SHORT_ZEROCODE_RUN: u64 = 59;
const LONG_ZEROCODE_RUN: u64 = 63;
const SHORTEST_LONG_RUN: usize = 2 + (LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN) as usize;

/// Bits taken from a byte slice, most significant first.
struct Bits<'a> {
    data: &'a [u8],
    pos: usize,
    acc: u64,
    count: u32,
}

impl Bits<'_> {
    fn fill(&mut self) -> Option<()> {
        self.acc = self.acc << 8 | *self.data.get(self.pos)? as u64;
        self.pos += 1;
        self.count += 8;
        Some(())
    }

    fn take(&mut self, n: u32) -> Option<u64> {
        while self.count < n {
            self.fill()?;
        }
        self.count -= n;
        Some(self.acc >> self.count & ((1 << n) - 1))
    }
}

/// A code's bits and length, as `code << 6 | length`.
fn code_bits(code: u64) -> u64 {
    code >> 6
}

fn code_len(code: u64) -> u32 {
    (code & 63) as u32
}

/// Read the run-length-coded code lengths for `min..=max` and give each symbol
/// its canonical code. Returns the table and the bytes it took.
fn huf_table(data: &[u8], min: usize, max: usize) -> Option<(Vec<u64>, usize)> {
    let mut codes = vec![0u64; HUF_ENCSIZE];
    let mut bits = Bits {
        data,
        pos: 0,
        acc: 0,
        count: 0,
    };
    let mut i = min;
    while i <= max {
        let l = bits.take(6)?;
        let run = if l == LONG_ZEROCODE_RUN {
            bits.take(8)? as usize + SHORTEST_LONG_RUN
        } else if l >= SHORT_ZEROCODE_RUN {
            (l - SHORT_ZEROCODE_RUN) as usize + 2
        } else {
            codes[i] = l;
            i += 1;
            continue;
        };
        if i + run > max + 1 {
            return None;
        }
        i += run;
    }

    let mut first = [0u64; 59];
    for &l in &codes {
        first[l as usize] += 1;
    }
    let mut c = 0;
    for l in (1..59).rev() {
        let next = (c + first[l]) >> 1;
        first[l] = c;
        c = next;
    }
    for code in &mut codes {
        let l = *code as usize;
        if l > 0 {
            *code = l as u64 | first[l] << 6;
            first[l] += 1;
        }
    }
    Some((codes, bits.pos))
}

/// A slot for the next 14 bits: a short code and its symbol, or the symbols
/// of the longer codes that begin with those bits.
#[derive(Clone, Default)]
struct DecEntry {
    len: u32,
    symbol: u32,
    long: Vec<u32>,
}

fn huf_decoder(codes: &[u64], min: usize, max: usize) -> Option<Vec<DecEntry>> {
    let mut table = vec![DecEntry::default(); HUF_DECSIZE];
    for (i, &code) in codes.iter().enumerate().take(max + 1).skip(min) {
        let (c, l) = (code_bits(code), code_len(code));
        if c >> l != 0 {
            return None;
        }
        if l > HUF_DECBITS {
            let slot = &mut table[(c >> (l - HUF_DECBITS)) as usize];
            if slot.len != 0 {
                return None;
            }
            slot.long.push(i as u32);
        } else if l > 0 {
            let first = (c << (HUF_DECBITS - l)) as usize;
            for slot in &mut table[first..first + (1 << (HUF_DECBITS - l))] {
                if slot.len != 0 || !slot.long.is_empty() {
                    return None;
                }
                slot.len = l;
                slot.symbol = i as u32;
            }
        }
    }
    Some(table)
}

/// Emit a symbol. The run symbol repeats the last value by a count in the next
/// eight bits.
fn huf_emit(symbol: u32, run: usize, bits: &mut Bits, out: &mut Vec<u16>, n: usize) -> Option<()> {
    if symbol as usize == run {
        if bits.count < 8 {
            bits.fill()?;
        }
        bits.count -= 8;
        let count = (bits.acc >> bits.count) as u8 as usize;
        let last = *out.last()?;
        if out.len() + count > n {
            return None;
        }
        out.resize(out.len() + count, last);
    } else {
        if out.len() >= n {
            return None;
        }
        out.push(symbol as u16);
    }
    Some(())
}

pub(super) fn huf_uncompress(data: &[u8], n: usize) -> Option<Vec<u16>> {
    if data.is_empty() {
        return (n == 0).then(Vec::new);
    }
    let u32_at = |i: usize| Some(u32::from_le_bytes(data.get(i..i + 4)?.try_into().ok()?) as usize);
    let (min, max, n_bits) = (u32_at(0)?, u32_at(4)?, u32_at(12)?);
    if min >= HUF_ENCSIZE || max >= HUF_ENCSIZE {
        return None;
    }
    let body = data.get(20..)?;
    let (codes, used) = huf_table(body, min, max)?;
    let table = huf_decoder(&codes, min, max)?;
    let stream = body.get(used..)?;
    let bytes = n_bits.div_ceil(8);
    if bytes > stream.len() {
        return None;
    }

    let mut bits = Bits {
        data: &stream[..bytes],
        pos: 0,
        acc: 0,
        count: 0,
    };
    let mut out = Vec::with_capacity(n);
    while bits.pos < bytes {
        bits.fill()?;
        while bits.count >= HUF_DECBITS {
            let entry =
                &table[(bits.acc >> (bits.count - HUF_DECBITS)) as usize & (HUF_DECSIZE - 1)];
            if entry.len != 0 {
                bits.count -= entry.len;
                huf_emit(entry.symbol, max, &mut bits, &mut out, n)?;
                continue;
            }
            let mut found = false;
            for &symbol in &entry.long {
                let code = codes[symbol as usize];
                let l = code_len(code);
                while bits.count < l && bits.pos < bytes {
                    bits.fill()?;
                }
                if bits.count >= l
                    && code_bits(code) == (bits.acc >> (bits.count - l)) & ((1 << l) - 1)
                {
                    bits.count -= l;
                    huf_emit(symbol, max, &mut bits, &mut out, n)?;
                    found = true;
                    break;
                }
            }
            if !found {
                return None;
            }
        }
    }

    // The last byte holds padding, and fewer than 14 bits may be left.
    let pad = 8u32.wrapping_sub(n_bits as u32) & 7;
    bits.count = bits.count.checked_sub(pad)?;
    bits.acc >>= pad;
    while bits.count > 0 {
        let entry = &table[(bits.acc << (HUF_DECBITS - bits.count)) as usize & (HUF_DECSIZE - 1)];
        if entry.len == 0 || entry.len > bits.count {
            return None;
        }
        bits.count -= entry.len;
        huf_emit(entry.symbol, max, &mut bits, &mut out, n)?;
    }
    (out.len() == n).then_some(out)
}

/// A 4x4 block of half floats from its 14-byte form: a first sample, then
/// six-bit differences down the first column and along each row.
fn b44_unpack14(b: &[u8]) -> [u16; 16] {
    let b = |i: usize| b[i] as u32;
    let shift = b(2) >> 2;
    let bias = 0x20u32 << shift;
    let d = |v: u32| (v & 0x3f) << shift;
    let mut s = [0u32; 16];
    let step = |from: u32, v: u32| from.wrapping_add(d(v)).wrapping_sub(bias) & 0xffff;
    s[0] = b(0) << 8 | b(1);
    s[4] = step(s[0], b(2) << 4 | b(3) >> 4);
    s[8] = step(s[4], b(3) << 2 | b(4) >> 6);
    s[12] = step(s[8], b(4));
    s[1] = step(s[0], b(5) >> 2);
    s[5] = step(s[4], b(5) << 4 | b(6) >> 4);
    s[9] = step(s[8], b(6) << 2 | b(7) >> 6);
    s[13] = step(s[12], b(7));
    s[2] = step(s[1], b(8) >> 2);
    s[6] = step(s[5], b(8) << 4 | b(9) >> 4);
    s[10] = step(s[9], b(9) << 2 | b(10) >> 6);
    s[14] = step(s[13], b(10));
    s[3] = step(s[2], b(11) >> 2);
    s[7] = step(s[6], b(11) << 4 | b(12) >> 4);
    s[11] = step(s[10], b(12) << 2 | b(13) >> 6);
    s[15] = step(s[14], b(13));
    s.map(|v| b44_order(v as u16))
}

/// Blocks hold halves mapped so that their integer order is their numeric one.
fn b44_order(v: u16) -> u16 {
    if v & 0x8000 != 0 {
        v & 0x7fff
    } else {
        !v
    }
}

fn b44(data: &[u8], channels: &[Channel], width: usize, lines: usize) -> Option<Vec<u8>> {
    let types: Vec<u8> = channels.iter().map(|c| c.pixel_type).collect();
    let types = &types[..];
    let total: usize = types.iter().map(|&t| words(t) * width * lines).sum();
    let mut tmp = vec![0u16; total];
    let mut pos = 0;
    let mut start = 0;
    for channel in channels {
        let t = channel.pixel_type;
        if t != HALF {
            // B44 only compresses HALF: other channels sit there as they are.
            let n = words(t) * width * lines;
            let raw = data.get(pos..pos + n * 2)?;
            for (slot, w) in tmp[start..start + n].iter_mut().zip(raw.as_chunks::<2>().0) {
                *slot = u16::from_le_bytes([w[0], w[1]]);
            }
            pos += n * 2;
            start += n;
            continue;
        }
        for y in (0..lines).step_by(4) {
            for x in (0..width).step_by(4) {
                let flat = *data.get(pos + 2)? >= 13 << 2;
                let mut s = if flat {
                    let first = data.get(pos..pos + 3)?;
                    pos += 3;
                    [b44_order((first[0] as u16) << 8 | first[1] as u16); 16]
                } else {
                    let block = data.get(pos..pos + 14)?;
                    pos += 14;
                    b44_unpack14(block)
                };
                if channel.p_linear {
                    let table = super::exr_dwa::b44_to_linear();
                    s = s.map(|v| table[v as usize]);
                }
                for r in 0..4.min(lines - y) {
                    for c in 0..4.min(width - x) {
                        tmp[start + (y + r) * width + x + c] = s[r * 4 + c];
                    }
                }
            }
        }
        start += width * lines;
    }
    Some(planes_to_scanlines(&tmp, types, width, lines))
}