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
#![warn(missing_docs)]

//! # adder-codec-core
//!
//! The core types and utilities for encoding and decoding ADΔER events

/// Expose public API for encoding and decoding
pub mod codec;

pub use bitstream_io;
use bitstream_io::{BigEndian, BitReader};
use std::fs::File;
use std::io::BufReader;
use std::ops::Add;

use thiserror::Error;

/// Error type for the `PlaneSize` struct
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum PlaneError {
    #[error(
        "plane dimensions invalid. All must be positive. Found {width:?}, {height:?}, {channels:?}"
    )]
    InvalidPlane {
        width: u16,
        height: u16,
        channels: u8,
    },
}

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub enum SourceCamera {
    #[default]
    FramedU8,
    FramedU16,
    FramedU32,
    FramedU64,
    FramedF32,
    FramedF64,
    Dvs,
    DavisU8,
    Atis,
    Asint,
}

use crate::codec::compressed::stream::CompressedInput;
use crate::codec::decoder::Decoder;
use crate::codec::raw::stream::RawInput;
use crate::codec::{CodecError, ReadCompression};
use serde::{Deserialize, Serialize};

/// The type of time used in the ADΔER representation
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
pub enum TimeMode {
    /// The time is the delta time from the previous event
    #[default]
    DeltaT,

    /// The time is the absolute time from the start of the recording
    AbsoluteT,

    /// TODO
    Mixed,
}

/// The size of the image plane in pixels
#[derive(Clone, Copy)]
pub struct PlaneSize {
    width: u16,
    height: u16,
    channels: u8,
}

impl Default for PlaneSize {
    fn default() -> Self {
        PlaneSize {
            width: 1,
            height: 1,
            channels: 1,
        }
    }
}

impl PlaneSize {
    /// Create a new `PlaneSize` with the given width, height, and channels
    pub fn new(width: u16, height: u16, channels: u8) -> Result<Self, PlaneError> {
        if width == 0 || height == 0 || channels == 0 {
            return Err(PlaneError::InvalidPlane {
                width,
                height,
                channels,
            });
        }
        Ok(Self {
            width,
            height,
            channels,
        })
    }
    /// The width, shorthand for `self.width`
    pub fn w(&self) -> u16 {
        self.width
    }

    /// The height, shorthand for `self.height`
    pub fn w_usize(&self) -> usize {
        self.width as usize
    }

    /// The height, shorthand for `self.height`
    pub fn h(&self) -> u16 {
        self.height
    }

    /// The height, shorthand for `self.height`
    pub fn h_usize(&self) -> usize {
        self.height as usize
    }

    /// The number of channels, shorthand for `self.channels`
    pub fn c(&self) -> u8 {
        self.channels
    }

    /// The number of channels, shorthand for `self.channels`
    pub fn c_usize(&self) -> usize {
        self.channels as usize
    }

    /// The total number of 2D pixels in the image plane, across the height and width dimension
    pub fn area_wh(&self) -> usize {
        self.width as usize * self.height as usize
    }

    /// The total number of 2D pixels in the image plane, across the width and channel dimension
    pub fn area_wc(&self) -> usize {
        self.width as usize * self.channels as usize
    }

    /// The total number of 2D pixels in the image plane, across the height and channel dimension
    pub fn area_hc(&self) -> usize {
        self.height as usize * self.channels as usize
    }

    /// The total number of 3D pixels in the image plane (2D pixels * color depth)
    pub fn volume(&self) -> usize {
        self.area_wh() * self.channels as usize
    }
}

/// Decimation value; a pixel's sensitivity.
pub type D = u8;

/// The maximum possible [`D`] value
pub const D_MAX: D = 127;

/// Special symbol signifying no information (filler dt)
pub const D_EMPTY: D = 255;

/// Special symbol signifying no information (filler dt)
pub const D_ZERO_INTEGRATION: D = 254;

/// Special symbol signifying no [`Event`] exists
pub const D_NO_EVENT: D = 253;

/// Precision for maximum intensity representable with allowed [`D`] values
pub type UDshift = u128;

/// Array for computing the intensity to integrate for a given [`D`] value
pub const D_SHIFT: [UDshift; 128] = [
    1 << 0,
    1 << 1,
    1 << 2,
    1 << 3,
    1 << 4,
    1 << 5,
    1 << 6,
    1 << 7,
    1 << 8,
    1 << 9,
    1 << 10,
    1 << 11,
    1 << 12,
    1 << 13,
    1 << 14,
    1 << 15,
    1 << 16,
    1 << 17,
    1 << 18,
    1 << 19,
    1 << 20,
    1 << 21,
    1 << 22,
    1 << 23,
    1 << 24,
    1 << 25,
    1 << 26,
    1 << 27,
    1 << 28,
    1 << 29,
    1 << 30,
    1 << 31,
    1 << 32,
    1 << 33,
    1 << 34,
    1 << 35,
    1 << 36,
    1 << 37,
    1 << 38,
    1 << 39,
    1 << 40,
    1 << 41,
    1 << 42,
    1 << 43,
    1 << 44,
    1 << 45,
    1 << 46,
    1 << 47,
    1 << 48,
    1 << 49,
    1 << 50,
    1 << 51,
    1 << 52,
    1 << 53,
    1 << 54,
    1 << 55,
    1 << 56,
    1 << 57,
    1 << 58,
    1 << 59,
    1 << 60,
    1 << 61,
    1 << 62,
    1 << 63,
    1 << 64,
    1 << 65,
    1 << 66,
    1 << 67,
    1 << 68,
    1 << 69,
    1 << 70,
    1 << 71,
    1 << 72,
    1 << 73,
    1 << 74,
    1 << 75,
    1 << 76,
    1 << 77,
    1 << 78,
    1 << 79,
    1 << 80,
    1 << 81,
    1 << 82,
    1 << 83,
    1 << 84,
    1 << 85,
    1 << 86,
    1 << 87,
    1 << 88,
    1 << 89,
    1 << 90,
    1 << 91,
    1 << 92,
    1 << 93,
    1 << 94,
    1 << 95,
    1 << 96,
    1 << 97,
    1 << 98,
    1 << 99,
    1 << 100,
    1 << 101,
    1 << 102,
    1 << 103,
    1 << 104,
    1 << 105,
    1 << 106,
    1 << 107,
    1 << 108,
    1 << 109,
    1 << 110,
    1 << 111,
    1 << 112,
    1 << 113,
    1 << 114,
    1 << 115,
    1 << 116,
    1 << 117,
    1 << 118,
    1 << 119,
    1 << 120,
    1 << 121,
    1 << 122,
    1 << 123,
    1 << 124,
    1 << 125,
    1 << 126,
    1 << 127,
];

/// The maximum intensity representation for input data. Currently 255 for 8-bit framed input.
pub const MAX_INTENSITY: f32 = 255.0; // TODO: make variable, dependent on input bit depth

/// The default [`D`] value for every pixel at the beginning of transcode
pub const D_START: D = 7;

/// Number of ticks elapsed since a given pixel last fired an [`Event`]
pub type DeltaT = u32;

/// Large count of ticks (e.g., for tracking the running timestamp of a sequence of [Events](Event)
pub type BigT = u64;

/// Measure of an amount of light intensity
pub type Intensity = f64;

/// Pixel x- or y- coordinate address in the ADΔER model
pub type PixelAddress = u16;

/// Special pixel address when signifying the end of a sequence of [Events](Event)
pub const EOF_PX_ADDRESS: PixelAddress = u16::MAX;

/// Pixel channel address in the ADΔER model
#[repr(packed)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Coord {
    /// Pixel x-coordinate
    pub x: PixelAddress,

    /// Pixel y-coordinate
    pub y: PixelAddress,

    /// Pixel channel, if present
    pub c: Option<u8>,
}

impl Default for Coord {
    fn default() -> Self {
        Self {
            x: 0,
            y: 0,
            c: Some(0),
        }
    }
}

impl Coord {
    /// Creates a new coordinate with the given x, y, and channel
    pub fn new(x: PixelAddress, y: PixelAddress, c: Option<u8>) -> Self {
        Self { x, y, c }
    }

    /// Creates a new 2D coordinate
    pub fn new_2d(x: PixelAddress, y: PixelAddress) -> Self {
        Self { x, y, c: None }
    }

    /// Creates a new 3D coordinate with the given channel
    pub fn new_3d(x: PixelAddress, y: PixelAddress, c: u8) -> Self {
        Self { x, y, c: Some(c) }
    }

    /// Returns the x coordinate as a [`PixelAddress`]
    pub fn x(&self) -> PixelAddress {
        self.x
    }

    /// Returns the y coordinate as a [`PixelAddress`]
    pub fn y(&self) -> PixelAddress {
        self.y
    }

    /// Returns the channel as an `Option<u8>`
    pub fn c(&self) -> Option<u8> {
        self.c
    }

    /// Returns the x coordinate as a `usize`
    pub fn x_usize(&self) -> usize {
        self.x as usize
    }

    /// Returns the y coordinate as a `usize`
    pub fn y_usize(&self) -> usize {
        self.y as usize
    }

    /// Returns the channel as a usize, or 0 if the coordinate is 2D
    pub fn c_usize(&self) -> usize {
        self.c.unwrap_or(0) as usize
    }

    /// Returns true if the coordinate is 2D
    pub fn is_2d(&self) -> bool {
        self.c.is_none()
    }

    /// Returns true if the coordinate is 3D
    pub fn is_3d(&self) -> bool {
        self.c.is_some()
    }

    /// Returns true if the coordinate is valid
    pub fn is_valid(&self) -> bool {
        self.x != EOF_PX_ADDRESS && self.y != EOF_PX_ADDRESS
    }

    /// Returns true if the coordinate is the EOF coordinate
    pub fn is_eof(&self) -> bool {
        self.x == EOF_PX_ADDRESS && self.y == EOF_PX_ADDRESS
    }
}

/// A 2D coordinate representation
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct CoordSingle {
    pub x: PixelAddress,
    pub y: PixelAddress,
}

/// An ADΔER event representation
#[allow(missing_docs)]
#[repr(packed)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Event {
    pub coord: Coord,
    pub d: D,
    pub delta_t: DeltaT,
}

/// An ADΔER event representation, without the channel component
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct EventSingle {
    pub coord: CoordSingle,
    pub d: D,
    pub delta_t: DeltaT,
}

impl From<&Event> for EventSingle {
    fn from(event: &Event) -> Self {
        EventSingle {
            coord: CoordSingle {
                x: event.coord.x,
                y: event.coord.y,
            },
            d: event.d,
            delta_t: event.delta_t,
        }
    }
}

impl From<EventSingle> for Event {
    fn from(event: EventSingle) -> Self {
        Event {
            coord: Coord {
                x: event.coord.x,
                y: event.coord.y,
                c: None,
            },
            d: event.d,
            delta_t: event.delta_t,
        }
    }
}

/// The type of data source representation
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SourceType {
    U8,
    U16,
    U32,
    U64,
    F32,
    F64,
}

const EOF_EVENT: Event = Event {
    coord: Coord {
        x: EOF_PX_ADDRESS,
        y: EOF_PX_ADDRESS,
        c: Some(0),
    },
    d: 0,
    delta_t: 0,
};

/// Helper function for opening a file as a raw or compressed input ADΔER stream
pub fn open_file_decoder(
    file_path: &str,
) -> Result<
    (
        Decoder<BufReader<File>>,
        BitReader<BufReader<File>, BigEndian>,
    ),
    CodecError,
> {
    let mut bufreader = BufReader::new(File::open(file_path)?);
    let compression = <RawInput as ReadCompression<BufReader<File>>>::new();

    let mut bitreader = BitReader::endian(bufreader, BigEndian);

    // First try opening the file as a raw file, then try as a compressed file
    let stream = match Decoder::new(Box::new(compression), &mut bitreader) {
        Ok(reader) => reader,
        Err(CodecError::WrongMagic) => {
            bufreader = BufReader::new(File::open(file_path)?);
            let compression = <CompressedInput as ReadCompression<BufReader<File>>>::new();
            bitreader = BitReader::endian(bufreader, BigEndian);
            Decoder::new(Box::new(compression), &mut bitreader)?
        }
        Err(e) => {
            return Err(e);
        }
    };
    Ok((stream, bitreader))
}

/// An ADΔER event representation
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct EventCoordless {
    pub d: D,

    pub delta_t: DeltaT,
}

impl From<Event> for EventCoordless {
    fn from(event: Event) -> Self {
        Self {
            d: event.d,
            delta_t: event.delta_t,
        }
    }
}

impl Add<EventCoordless> for EventCoordless {
    type Output = EventCoordless;

    fn add(self, _rhs: EventCoordless) -> EventCoordless {
        todo!()
    }
}

impl num_traits::Zero for EventCoordless {
    fn zero() -> Self {
        EventCoordless { d: 0, delta_t: 0 }
    }

    fn is_zero(&self) -> bool {
        self.d.is_zero() && self.delta_t.is_zero()
    }
}