aztec 0.1.0

AZTEC compression algorithm
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
#![no_std]

//! # AZTEC
//!
//! A compression algorithm based on: <https://ieeexplore.ieee.org/document/4502549>
//!
//! 3 bytes get used for each line/slope which means that in the worst case the compressed data
//! will be 1.5 times larger than the original.
//!
//!
//! Usage:
//! ```rust
//! const THRESHOLD: u16 = 50;
//! let config = AztecConfigBuilder::new(THRESHOLD)
//!    .max_slope_line_len(4)
//!    .build()
//!    .expect("Invalid Config");
//! ```
//!

struct Buffer<'a> {
    data: &'a [u8],
    current: usize,
}

impl<'a> Buffer<'a> {
    fn new(data: &'a [u8]) -> Buffer<'a> {
        Buffer { data, current: 0 }
    }
}

struct BufferMut<'a> {
    data: &'a mut [u8],
    current: usize,
}

impl<'a> BufferMut<'a> {
    fn new(data: &'a mut [u8]) -> BufferMut<'a> {
        BufferMut { data, current: 0 }
    }
}

struct Packet {
    slope: bool,
    length: u8,
    data: u16,
}

#[derive(Debug)]
pub enum AztecError {
    MaxLineLenTooLong,
    MaxSlopeLenTooLong,
    MaxSlopeLineLenTooLong,
    ProvidedBufferIsNotLongEnough,
}

const NOT_LONG: AztecError = AztecError::ProvidedBufferIsNotLongEnough;

impl Packet {
    /// Create a line packet
    fn line(length: u8, data: u16) -> Self {
        // println!("Created Line. LEN: {} DATA: {}", length, data);
        Self {
            slope: false,
            length,
            data,
        }
    }

    /// Create a slope packet
    fn slope(length: u8, data: u16) -> Self {
        // println!("Created Slope. LEN: {} DATA: {}", length, data);
        Self {
            slope: true,
            length,
            data,
        }
    }

    /// Write the packet to a buffer
    fn write(&self, buf: &mut BufferMut) -> Result<(), AztecError> {
        let i = buf.current;

        let val = ((self.slope as u8) << 7) | self.length;
        let [byte1, byte2] = self.data.to_le_bytes();

        *buf.data.get_mut(i).ok_or(NOT_LONG)? = val;
        *buf.data.get_mut(i + 1).ok_or(NOT_LONG)? = byte1;
        *buf.data.get_mut(i + 2).ok_or(NOT_LONG)? = byte2;

        buf.current += 3;
        Ok(())
    }

    /// Parse the packet from bytes
    fn from_buffer(buf: &mut Buffer) -> Result<Self, AztecError> {
        let i = buf.current;

        let x = buf.data.get(i).ok_or(NOT_LONG)?;
        let byte1 = buf.data.get(i + 1).ok_or(NOT_LONG)?;
        let byte2 = buf.data.get(i + 2).ok_or(NOT_LONG)?;

        let slope = (x >> 7) == 1;
        let length = x & 0b111_1111;
        let data = u16::from_le_bytes([*byte1, *byte2]);

        buf.current += 3;

        Ok(Self {
            slope,
            length,
            data,
        })
    }
}

/// Configuration for the AZTEC compression algorithm. 
/// Use [`AztecConfigBuilder`] in order to create it.
pub struct AztecConfig {
    threshold: u16,
    max_line_len: u8,
    max_slope_len: u8,
    max_slope_line_len: u8,
}

impl AztecConfig {
    /// Get the current threshold
    pub fn threshold(&self) -> u16 {
        self.threshold
    }

    /// Get the current max_line_len
    pub fn max_line_len(&self) -> u8 {
        self.max_line_len
    }

    /// Get the current max_slope_len
    pub fn max_slope_len(&self) -> u8 {
        self.max_slope_len
    }

    /// Get the current max_slope_line_len
    pub fn max_slope_line_len(&self) -> u8 {
        self.max_slope_line_len
    }
}

/// Builder for the AZTEC configuration
pub struct AztecConfigBuilder {
    config: AztecConfig,
}

impl AztecConfigBuilder {
    /// Create a new builder with a given threshold
    pub fn new(threshold: u16) -> Self {
        Self {
            config: AztecConfig {
                threshold,
                max_line_len: 63,
                max_slope_len: 63,
                max_slope_line_len: 4,
            },
        }
    }

    /// The threshold for a series of samples to be represented as a line
    pub fn threshold(mut self, threshold: u16) -> Self {
        self.config.threshold = threshold;
        self
    }

    /// The maximum length of a line (in samples).
    ///
    /// The maximum allowed value is u7 (63)
    pub fn max_line_len(mut self, max_line_len: u8) -> Self {
        self.config.max_line_len = max_line_len;
        self
    }

    /// The maximum length of a slope (in samples). 
    /// Set this value lower if the slopes are too long.
    ///
    /// The maximum allowed value is u7 (63)
    pub fn max_slope_len(mut self, max_slope_len: u8) -> Self {
        self.config.max_slope_len = max_slope_len;
        self
    }

    /// The maximum length of a line for it to be considered a part of the slope.
    ///
    /// The value should be less than max_slope_len
    pub fn max_slope_line_len(mut self, max_slope_line_len: u8) -> Self {
        self.config.max_slope_line_len = max_slope_line_len;
        self
    }

    /// Validates the given parameters and creates a new AztecConfig
    pub fn build(self) -> Result<AztecConfig, AztecError> {
        if self.config.max_line_len > 63 {
            return Err(AztecError::MaxLineLenTooLong);
        }

        if self.config.max_slope_len > 63 {
            return Err(AztecError::MaxSlopeLenTooLong);
        }

        if self.config.max_slope_line_len >= self.config.max_slope_len {
            return Err(AztecError::MaxSlopeLineLenTooLong);
        }

        Ok(self.config)
    }
}

enum SlopeState {
    NoSlope,
    OngoingSlope(Option<bool>),
    SlopeDone,
}

/// Compress the given data
///
/// The result buffer should be at least 3 times the length (1.5 times the size in memory) of the
/// source buffer to account for the worst case scenario where every sample is a line/slope
pub fn compress(
    data: &[u16],
    result: &mut [u8],
    config: &AztecConfig,
) -> Result<usize, AztecError> {
    // Will only reach the capacity if every single sample is a line
    let mut buf = BufferMut::new(result);

    let mut x = data[0];
    let mut min = x;
    let mut max = x;
    let mut old_min = x;
    let mut old_max = x;
    Packet::line(1, x).write(&mut buf)?;

    let mut slope_state = SlopeState::NoSlope;
    let mut slope_length = 0;

    let mut line_done = false;
    let mut line_length = 1;
    let mut line_value;
    let mut previous_line_value = 0;

    let mut i = 1;

    while i < data.len() {
        let mut increment = true;

        x = data[i];

        if x < min {
            min = x;
        }
        if x > max {
            max = x;
        }

        // println!("i-{} x-{} MIN: {} MAX: {}", i, x, old_min, old_max);
        line_value = ((old_max as u32 + old_min as u32) / 2) as u16;

        // Terminate the line if it is too long or the limits exceed the threshold
        if line_length >= config.max_line_len || max - min > config.threshold {
            line_done = true;
        }

        // Terminate the line and the slope on the last sample
        let last_sample = i == data.len() - 1;
        if last_sample {
            line_done = true;
            if let SlopeState::OngoingSlope(_) = slope_state {
                slope_state = SlopeState::SlopeDone;
            }
        }

        if line_done {
            // println!("i-{} LINE LEN: {} VAL: {}", i, line_length, line_value);

            // Check if the line could be a slope
            // Do not change the slope state on the last sample
            if !last_sample {
                if line_length <= config.max_slope_line_len {
                    match slope_state {
                        SlopeState::NoSlope => {
                            // Start a new slope
                            slope_state = SlopeState::OngoingSlope(None);
                            // println!("i-{} SLOPE START", i);
                        }
                        SlopeState::OngoingSlope(slope_dir) => {
                            let diff = line_value as i32 - previous_line_value as i32;
                            let mut sign_changed = false;
                            // println!("DIFF: {}", diff);
                            let current_slope_dir = if diff == 0 { None } else { Some(diff > 0) };
                            if let Some(current_slope_dir) = current_slope_dir {
                                if let Some(slope_dir) = slope_dir {
                                    // Check if the slope direction still matches
                                    if current_slope_dir != slope_dir {
                                        sign_changed = true;
                                    }
                                }

                                if sign_changed {
                                    slope_state = SlopeState::SlopeDone;
                                } else {
                                    slope_state = SlopeState::OngoingSlope(Some(current_slope_dir));
                                }
                            }
                        }
                        SlopeState::SlopeDone => {}
                    }
                } else {
                    // Terminate a slope if there is a line longer than the maximum allowed
                    if let SlopeState::OngoingSlope(_) = slope_state {
                        slope_state = SlopeState::SlopeDone;
                    }
                }
            }

            // Terminate a slope that will be too long
            if let SlopeState::OngoingSlope(_) = slope_state {
                if slope_length as u16 + line_length as u16 >= config.max_slope_len as u16 {
                    slope_state = SlopeState::SlopeDone;
                }
            }

            // println!("OUT {} {:?}", line_length, slope_state);
            match slope_state {
                // Only write out the line
                SlopeState::NoSlope => {
                    Packet::line(line_length, line_value).write(&mut buf)?;
                }
                // This line is a part of the slope
                SlopeState::OngoingSlope(_) => {
                    slope_length += line_length;
                }
                SlopeState::SlopeDone => {
                    // println!("SLOPE END {}", i);
                    Packet::slope(slope_length, previous_line_value).write(&mut buf)?;

                    // Process the current line again because it could potentially be a part of a new slope
                    increment = false;
                    slope_state = SlopeState::NoSlope;
                    slope_length = 0;
                }
            }

            // Reset to initial state after a line
            if increment {
                previous_line_value = line_value;
                min = x;
                max = x;
                line_done = false;
                line_length = 1;
            }
        } else {
            line_length += 1;
        }

        if increment {
            old_min = min;
            old_max = max;
            i += 1;
        }
    }

    Ok(buf.current + 1)
}

/// Decompress the given data
pub fn decompress(data: &[u8], output: &mut [u16]) -> Result<usize, AztecError> {
    let mut buf = Buffer::new(data);

    let mut output_i = 0;
    let mut previous_line_value: u16 = 0;

    // Each packet is 3 bytes
    for _ in 0..data.len() / 3 {
        let packet = Packet::from_buffer(&mut buf)?;
        if packet.slope {
            if packet.length == 1 {
                output[output_i] = packet.data;
                output_i += 1;
            } else {
                for i in 0..packet.length {
                    // Linear interpolation
                    let value = previous_line_value as i32
                        + ((i as i32) * (packet.data as i32 - previous_line_value as i32))
                            / (packet.length as i32 - 1);
                    output[output_i] = value as u16;
                    output_i += 1;
                }
            }
        } else {
            for _ in 0..packet.length {
                output[output_i] = packet.data;
                output_i += 1;
            }
        }

        previous_line_value = packet.data;
    }

    Ok(output_i)
}

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

    const LEN: usize = 128733;
    const RES_LEN: usize = LEN * 3;

    #[test]
    // A flat line will be compressed losslessly
    fn straight_line() {
        let data = [128; LEN];
        let mut decompressed = [0; LEN];

        let mut result = [0; RES_LEN];
        let config = AztecConfigBuilder::new(10)
            .max_slope_line_len(4)
            .build()
            .unwrap();
        let len = compress(&data, &mut result, &config).unwrap();
        let (compressed, _) = result.split_at(len);

        let len = decompress(&compressed, &mut decompressed).unwrap();

        assert_eq!(len, LEN);
        // A straight line will be compressed without any loss
        assert_eq!(data, decompressed);
    }

    #[test]
    fn straight_line_long_slope() {
        let data = [128; LEN];
        let mut decompressed = [0; LEN];

        let mut result = [0; RES_LEN];
        let config = AztecConfigBuilder::new(10)
            .max_slope_line_len(62)
            .build()
            .unwrap();
        let len = compress(&data, &mut result, &config).unwrap();
        let (compressed, _) = result.split_at(len);

        let len = decompress(&compressed, &mut decompressed).unwrap();

        assert_eq!(len, LEN);
        assert_eq!(data, decompressed);
    }

    #[test]
    fn triangle() {
        let mut data = [0; LEN];

        let mut val: u16 = 0;
        for i in 0..LEN {
            data[i] = val;

            if val == u16::MAX {
                val = 0;
            } else {
                val += 1;
            }
        }

        let mut decompressed = [0; LEN * 3];

        let mut result = [0; RES_LEN];
        let config = AztecConfigBuilder::new(10)
            .max_slope_line_len(4)
            .build()
            .unwrap();
        let len = compress(&data, &mut result, &config).unwrap();
        let (compressed, _) = result.split_at(len);

        let len = decompress(&compressed, &mut decompressed).unwrap();

        assert_eq!(len, LEN);
    }

    #[test]
    fn multiple_random() {
        const RANDOM_TESTS: usize = 20;

        let mut data = [0; LEN];

        for _ in 0..RANDOM_TESTS {
            for x in &mut data {
                *x = rand::random();
            }

            let mut decompressed = [0; LEN * 3];

            let mut result = [0; RES_LEN];
            let config = AztecConfigBuilder::new(10)
                .max_slope_line_len(4)
                .build()
                .unwrap();
            let len = compress(&data, &mut result, &config).unwrap();
            let (compressed, _) = result.split_at(len);

            let len = decompress(&compressed, &mut decompressed).unwrap();

            assert_eq!(len, LEN);
        }
    }
}