refimage 1.0.0-pre6

Imaging library. Provides image storage using CoW-like structures to avoid re-allocation in image-aquisition scenarios. Supports rich metadata and serdes.
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
//! Rice compression for the `RICE_1` tile-compression type.
//!
//! Ported from the encode and decode paths of cfitsio's public-domain `ricecomp.c`
//! (R. White, STScI): 8-bit (`fits_rcomp_byte`), 16-bit (`fits_rcomp_short`) and
//! 32-bit (`fits_rcomp`), the last used for quantized floating-point tiles. The block
//! size is fixed at 32 (`ZVAL1 = BLOCKSIZE = 32`).
//!
//! The decoder is retained alongside the encoder so unit tests can round-trip a tile
//! without a reference reader; it is otherwise unused until a `FitsRead` exists.
#![allow(dead_code)] // `decode_*` are exercised only by the round-trip tests for now.
#![allow(clippy::int_plus_one)] // kept 1:1 with cfitsio `ricecomp.c`

const NBLOCK: usize = 32;

const NONZERO_COUNT: [i32; 256] = {
    let mut t = [0i32; 256];
    let mut i = 1usize;
    while i < 256 {
        let mut v = i;
        let mut n = 0i32;
        while v != 0 {
            v >>= 1;
            n += 1;
        }
        t[i] = n;
        i += 1;
    }
    t
};

/// Parameters that vary with pixel width.
struct Params {
    fsbits: i32,
    fsmax: i32,
    /// bits for the raw first pixel and for direct (high-entropy) coding.
    nbits: i32,
    /// `& mask` applied to the block sum before choosing the split (`u16`/`u8`/`u32`
    /// cast in C).
    psum_mask: u32,
    /// truncate a difference to the pixel width (`d as i16 as i32`, etc.).
    trunc_bits: u32,
}

const BYTE: Params = Params {
    fsbits: 3,
    fsmax: 6,
    nbits: 8,
    psum_mask: 0xff,
    trunc_bits: 8,
};
const SHORT: Params = Params {
    fsbits: 4,
    fsmax: 14,
    nbits: 16,
    psum_mask: 0xffff,
    trunc_bits: 16,
};
const INT: Params = Params {
    fsbits: 5,
    fsmax: 25,
    nbits: 32,
    psum_mask: u32::MAX,
    trunc_bits: 32,
};

/// Rice-encode 8-bit (FITS-native) pixel values.
pub(super) fn encode_byte(a: &[i8]) -> Vec<u8> {
    encode(&a.iter().map(|&x| x as i32).collect::<Vec<_>>(), &BYTE)
}

/// Rice-encode 16-bit (FITS-native) pixel values.
pub(super) fn encode_short(a: &[i16]) -> Vec<u8> {
    encode(&a.iter().map(|&x| x as i32).collect::<Vec<_>>(), &SHORT)
}

/// Rice-encode 32-bit values (quantized floating-point tiles).
pub(super) fn encode_int(a: &[i32]) -> Vec<u8> {
    encode(a, &INT)
}

/// Rice-decode into 8-bit values.
pub(super) fn decode_byte(c: &[u8], nx: usize) -> Vec<u8> {
    decode(c, nx, &BYTE).into_iter().map(|v| v as u8).collect()
}

/// Rice-decode into 16-bit values.
pub(super) fn decode_short(c: &[u8], nx: usize) -> Vec<u16> {
    decode(c, nx, &SHORT)
        .into_iter()
        .map(|v| v as u16)
        .collect()
}

/// Rice-decode into 32-bit values.
pub(super) fn decode_int(c: &[u8], nx: usize) -> Vec<i32> {
    decode(c, nx, &INT).into_iter().map(|v| v as i32).collect()
}

/// Truncate `d` to `bits` low bits, sign-extended back to `i32` (C's `(short) d` etc.).
fn trunc(d: i32, bits: u32) -> i32 {
    if bits >= 32 {
        d
    } else {
        let sh = 32 - bits;
        (d << sh) >> sh
    }
}

fn mask(n: i32) -> u32 {
    if n <= 0 {
        0
    } else if n >= 32 {
        u32::MAX
    } else {
        (1u32 << n) - 1
    }
}

/// `x << k`, yielding 0 when `k >= 32` (C's `int` shift wraps; the callers only care
/// about the low 8 bits they subsequently mask).
fn shl(x: u32, k: i32) -> u32 {
    if !(0..32).contains(&k) {
        0
    } else {
        x << k
    }
}

struct BitWriter {
    buf: Vec<u8>,
    acc: u32,
    free: i32,
}

impl BitWriter {
    fn new() -> Self {
        Self {
            buf: Vec::new(),
            acc: 0,
            free: 8,
        }
    }

    /// `output_nbits` from cfitsio (`n <= 32`).
    fn put(&mut self, bits: u32, mut n: i32) {
        if self.free + n > 32 {
            // Large n: flush the top `free` bits first (0 < free <= 8).
            self.acc = shl(self.acc, self.free);
            self.acc |= (bits >> (n - self.free)) & mask(self.free);
            self.buf.push((self.acc & 0xff) as u8);
            n -= self.free;
            self.free = 8;
        }
        self.acc = shl(self.acc, n);
        self.acc |= bits & mask(n);
        self.free -= n;
        while self.free <= 0 {
            self.buf
                .push((self.acc.wrapping_shr((-self.free) as u32) & 0xff) as u8);
            self.free += 8;
        }
    }

    fn done(&mut self) {
        if self.free < 8 {
            self.buf.push((shl(self.acc, self.free) & 0xff) as u8);
        }
    }
}

fn encode(a: &[i32], p: &Params) -> Vec<u8> {
    let nx = a.len();
    let mut w = BitWriter::new();
    if nx == 0 {
        return w.buf;
    }

    w.put(a[0] as u32 & mask(p.nbits), p.nbits);
    let mut lastpix = a[0];

    let bbits = 1i32 << p.fsbits;
    let mut diff = [0u32; NBLOCK];

    let mut i = 0;
    while i < nx {
        let thisblock = (nx - i).min(NBLOCK);
        let mut pixelsum = 0.0f64;
        for j in 0..thisblock {
            let nextpix = a[i + j];
            let pdiff = trunc(nextpix.wrapping_sub(lastpix), p.trunc_bits);
            let shifted = pdiff.wrapping_shl(1);
            let mapped = if pdiff < 0 { !shifted } else { shifted } as u32;
            diff[j] = mapped;
            pixelsum += mapped as f64;
            lastpix = nextpix;
        }

        let mut dpsum = (pixelsum - (thisblock / 2) as f64 - 1.0) / thisblock as f64;
        if dpsum < 0.0 {
            dpsum = 0.0;
        }
        let mut psum = (dpsum as u32 & p.psum_mask) >> 1;
        let mut fs = 0i32;
        while psum > 0 {
            fs += 1;
            psum >>= 1;
        }

        if fs >= p.fsmax {
            w.put((p.fsmax + 1) as u32, p.fsbits);
            for &d in &diff[..thisblock] {
                w.put(d, bbits);
            }
        } else if fs == 0 && pixelsum == 0.0 {
            w.put(0, p.fsbits);
        } else {
            w.put((fs + 1) as u32, p.fsbits);
            let fsmask = mask(fs);
            for &v in &diff[..thisblock] {
                let mut top = (v >> fs) as i32;
                if w.free >= top + 1 {
                    w.acc = shl(w.acc, top + 1) | 1;
                    w.free -= top + 1;
                } else {
                    w.acc = shl(w.acc, w.free);
                    w.buf.push((w.acc & 0xff) as u8);
                    top -= w.free;
                    while top >= 8 {
                        w.buf.push(0);
                        top -= 8;
                    }
                    w.acc = 1;
                    w.free = 7 - top;
                }
                if fs > 0 {
                    w.acc = shl(w.acc, fs) | (v & fsmask);
                    w.free -= fs;
                    while w.free <= 0 {
                        w.buf
                            .push((w.acc.wrapping_shr((-w.free) as u32) & 0xff) as u8);
                        w.free += 8;
                    }
                }
            }
        }
        i += NBLOCK;
    }
    w.done();
    w.buf
}

fn decode(c: &[u8], nx: usize, p: &Params) -> Vec<u32> {
    let mut out = vec![0u32; nx];
    if nx == 0 {
        return out;
    }
    let bbits = 1i32 << p.fsbits;

    let head = (p.nbits / 8) as usize;
    let mut lastpix: u32 = 0;
    for &byte in &c[..head] {
        lastpix = (lastpix << 8) | byte as u32;
    }
    let mut pos = head;

    let mut b = c[pos] as u32;
    pos += 1;
    let mut nbits = 8i32;

    let mut i = 0usize;
    while i < nx {
        nbits -= p.fsbits;
        while nbits < 0 {
            b = (b << 8) | c[pos] as u32;
            pos += 1;
            nbits += 8;
        }
        let fs = (b >> nbits) as i32 - 1;
        b &= mask(nbits);

        let imax = (i + NBLOCK).min(nx);
        if fs < 0 {
            for slot in out.iter_mut().take(imax).skip(i) {
                *slot = lastpix;
            }
            i = imax;
        } else if fs == p.fsmax {
            while i < imax {
                let mut k = bbits - nbits;
                let mut d = shl(b, k);
                k -= 8;
                while k >= 0 {
                    b = c[pos] as u32;
                    pos += 1;
                    d |= shl(b, k);
                    k -= 8;
                }
                if nbits > 0 {
                    b = c[pos] as u32;
                    pos += 1;
                    d |= b >> (-k);
                    b &= mask(nbits);
                } else {
                    b = 0;
                }
                let d = if d & 1 == 0 { d >> 1 } else { !(d >> 1) };
                lastpix = d.wrapping_add(lastpix);
                out[i] = lastpix;
                i += 1;
            }
        } else {
            while i < imax {
                while b == 0 {
                    nbits += 8;
                    b = c[pos] as u32;
                    pos += 1;
                }
                let nzero = nbits - NONZERO_COUNT[b as usize];
                nbits -= nzero + 1;
                b ^= 1u32 << nbits;
                nbits -= fs;
                while nbits < 0 {
                    b = (b << 8) | c[pos] as u32;
                    pos += 1;
                    nbits += 8;
                }
                let d = (shl(nzero as u32, fs)) | (b >> nbits);
                b &= mask(nbits);
                let d = if d & 1 == 0 { d >> 1 } else { !(d >> 1) };
                lastpix = d.wrapping_add(lastpix);
                out[i] = lastpix;
                i += 1;
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rt_byte(v: &[i8]) {
        let dec = decode_byte(&encode_byte(v), v.len());
        let want: Vec<u8> = v.iter().map(|&x| x as u8).collect();
        assert_eq!(dec, want, "input {v:?}");
    }

    fn rt_short(v: &[i16]) {
        let dec = decode_short(&encode_short(v), v.len());
        let want: Vec<u16> = v.iter().map(|&x| x as u16).collect();
        assert_eq!(dec, want, "input {v:?}");
    }

    fn rt_int(v: &[i32]) {
        let dec = decode_int(&encode_int(v), v.len());
        assert_eq!(dec, v, "input {v:?}");
    }

    #[test]
    fn byte_roundtrips() {
        rt_byte(&[]);
        rt_byte(&[42]);
        rt_byte(&[0i8; 80]);
        rt_byte(&(-128..127i8).collect::<Vec<_>>());
        rt_byte(&(0..300i32).map(|i| (i * 97) as i8).collect::<Vec<_>>());
    }

    #[test]
    fn short_roundtrips() {
        rt_short(&[]);
        rt_short(&[0]);
        rt_short(&[7; 1]);
        rt_short(&[0i16; 100]);
        rt_short(&(0..200i16).collect::<Vec<_>>());
        rt_short(&(0..500i32).map(|i| (i * 277) as i16).collect::<Vec<_>>());
        rt_short(&[i16::MIN, i16::MAX, 0, -1, 1, i16::MIN, i16::MAX]);
    }

    #[test]
    fn int_roundtrips() {
        rt_int(&[]);
        rt_int(&[123_456]);
        rt_int(&[0i32; 100]);
        rt_int(&(0..200i32).collect::<Vec<_>>());
        rt_int(
            &(0..500i32)
                .map(|i| i.wrapping_mul(1_234_577))
                .collect::<Vec<_>>(),
        );
        rt_int(&[
            i32::MIN,
            i32::MAX,
            0,
            -1,
            1,
            i32::MIN,
            i32::MAX,
            -12345,
            67890,
        ]);
    }
}