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
use std::error::Error;
use std::cmp;
use error::InvalidSizeError;
use misc::*;

#[cfg(not(feature = "rlibc"))]
use std::io::Write;

#[cfg(feature = "rlibc")]
use rlibc;


/// A fast "binned" waveform renderer.
///
/// Minimum / maximum amplitude values are binned to reduce
/// calculation and memory usage.
pub struct BinnedWaveformRenderer<T: Sample> {
    pub config: WaveformConfig,
    sample_rate: f64,
    bin_size: usize,
    minmax: MinMaxPairSequence<T>,
}

impl<T: Sample> BinnedWaveformRenderer<T> {
    /// The constructor.
    ///
    /// # Arguments
    ///
    /// * `samples` - The samples that will be used to calculate binned min / max values.
    ///               It must also contain the sample rate that is used by
    ///               `BinnedWaveformRenderer` to render images when given a
    ///               `TimeRange::Seconds`.
    /// * `bin_size` - The size of the bins which the min / max values will be binned
    ///                into.
    /// * `config` - See `WaveformConfig`.
    pub fn new(samples: &SampleSequence<T>, bin_size: usize, config: WaveformConfig) -> Result<BinnedWaveformRenderer<T>, Box<Error>> {
        let mut data: Vec<MinMaxPair<T>> = Vec::new();
        let nb_samples = samples.data.len();

        if bin_size > nb_samples {
            return Err(Box::new(InvalidSizeError {
                var_name: "bin_size".to_string(),
            }));
        }

        let nb_bins = (nb_samples as f64 / bin_size as f64).ceil() as usize;

        for x in 0..nb_bins {
            let mut min = samples.data[x * bin_size + 0];
            let mut max = samples.data[x * bin_size + 0];
            if bin_size > 1 {
                for i in 1..bin_size {
                    let idx = x * bin_size + i;
                    if idx >= nb_samples {
                        break;
                    }
                    let s = samples.data[idx];
                    if s > max {
                        max = s;
                    } else if s < min {
                        min = s;
                    }
                }
            }
            data.push(MinMaxPair { min: min, max: max });
        }
        let minmax = MinMaxPairSequence { data: data };
        Ok(Self {
            config: config,
            bin_size: bin_size,
            minmax: minmax,
            sample_rate: samples.sample_rate,
        })
    }


    /// Renders an image as a `Vec<u8>`.
    ///
    /// `None` will be returned if the area of the specified `shape` is equal to zero.
    ///
    /// # Arguments
    ///
    /// * `range` - The samples within this `TimeRange` will be rendered.
    /// * `shape` - The `(width, height)` of the resulting image in pixels.
    pub fn render_vec(&self, range: TimeRange, shape: (usize, usize)) -> Option<Vec<u8>> {
        let (w, h) = shape;
        if w == 0 || h == 0 {
            return None;
        }


        let mut img = match self.config.get_background() {
            Color::Scalar(_) => vec![0u8; w * h],
            Color::Vector3{..} => vec![0u8; w * h * 3],
            Color::Vector4{..} => vec![0u8; w * h * 4],
        };
        
        self.render_write(range, (0, 0), shape, &mut img[..], shape).unwrap();

        Some(img)
    }

    /// Writes the image into a mutable reference to a slice.
    ///
    /// It will raise an error if
    ///
    /// * the area of the specified `shape` is equal to zero.
    /// * either the width or height of the `shape` exceeds that of the `full_shape`
    ///   of `img`.
    /// * the length of `img` is not long enough to contain the result.
    ///   `(offsets.0 + shape.0) * (offsets.1 + shape.1) * (Bytes per pixel) <= img.len()`
    ///   must be satisfied.
    ///
    /// # Arguments
    ///
    /// * `range` - The samples within this `TimeRange` will be rendered.
    /// * `offsets` - The `(x-offset, y-offset)` of the part of the `img` that is
    ///               going to be overwritten in in pixels.
    ///               Specifies the starting position to write into `img`.
    /// * `shape` - The `(width, height)` of the part of the `img` that is going 
    ///             to be overwritten in pixels.
    /// * `img`   - A mutable reference to the slice to write the result into.
    /// * `full_shape` - The `(width, height)` of the whole `img` in pixels.
    ///
    pub fn render_write(&self, range: TimeRange, offsets: (usize, usize), shape: (usize, usize), img: &mut [u8], full_shape: (usize, usize)) -> Result<(), Box<Error>> {
        let (w, h) = shape;
        if w == 0 || h == 0 {
            return Err(Box::new(InvalidSizeError{var_name: "shape".to_string()}));
        }

        let (fullw, fullh) = full_shape;
        if fullw < w || fullh < h {
            return Err(Box::new(InvalidSizeError{var_name: "shape and/or full_shape".to_string()}));
        }

        let (offx, offy) = offsets;

        // Check if we have enough bytes in `img`
        match self.config.get_background() {
            Color::Scalar(_) => {
                if (offx + w) * (offy + h) > img.len() {
                    return Err(Box::new(InvalidSizeError{var_name: "offsets and/or shape".to_string()}));
                }
            },
            Color::Vector3{..} => {
                if (offx + w) * (offy + h) * 3 > img.len() {
                    return Err(Box::new(InvalidSizeError{var_name: "offsets and/or shape".to_string()}));
                }
            },
            Color::Vector4{..} => {
                if (offx + w) * (offy + h) * 4 > img.len() {
                    return Err(Box::new(InvalidSizeError{var_name: "offsets and/or shape".to_string()}));
                }
            },
        }

        let (begin, end) = match range {
            TimeRange::Seconds(b, e) => (
                (b * self.sample_rate) as usize,
                (e * self.sample_rate) as usize,
            ),
            TimeRange::Samples(b, e) => (b, e),
        };
        let nb_samples = end - begin;
        let samples_per_pixel = (nb_samples as f64) / (w as f64);
        let bins_per_pixel = samples_per_pixel / (self.bin_size as f64);
        let bins_per_pixel_floor = bins_per_pixel.floor() as usize;
        let bins_per_pixel_ceil = bins_per_pixel.ceil() as usize;

        let offset_bin_idx = begin / self.bin_size;
        let mut start_bin_idx = offset_bin_idx;
        for x in 0..w {
            let inc = if ((start_bin_idx - offset_bin_idx) as f64 + 1f64) / (x as f64) < bins_per_pixel {
                bins_per_pixel_ceil
            } else {
                bins_per_pixel_floor
            };

            let mut min: T;
            let mut max: T;
            if start_bin_idx < self.minmax.data.len() - 1 {
                let ref d = self.minmax.data[start_bin_idx];
                min = d.min;
                max = d.max;
                let range_start = start_bin_idx;
                let range_end = if start_bin_idx + inc <= self.minmax.data.len() {
                    start_bin_idx + inc
                } else {
                    self.minmax.data.len()
                };
                for b in self.minmax.data[range_start..range_end].iter() {
                    if b.min < min {
                        min = b.min
                    }
                    if b.max > max {
                        max = b.max
                    }
                }
                start_bin_idx = range_end;
            } else {
                min = T::zero();
                max = T::zero();
            }

            let scale = 1f64 / (self.config.amp_max - self.config.amp_min) * (h as f64);
            let min_translated: usize = h -
                cmp::max(
                    0,
                    cmp::min(
                        h as i32,
                        ((min.into() - self.config.amp_min) * scale).floor() as i32,
                    ),
                ) as usize;
            let max_translated: usize = h -
                cmp::max(
                    0,
                    cmp::min(
                        h as i32,
                        ((max.into() - self.config.amp_min) * scale).floor() as i32,
                    ),
                ) as usize;

            // Putting this `match` outside for loops improved the speed.
            match (self.config.get_background(), self.config.get_foreground()) {
                (Color::Scalar(ba), Color::Scalar(fa)) => {
                    flipping_three_segment_for!{
                                for y in 0, max_translated, min_translated, h, {
                                    pixel!(img[fullw, fullh; offx+x, offy+y]) = ba,
                                    pixel!(img[fullw, fullh; offx+x, offy+y]) = fa
                                }
                            }
                },

                (
                    Color::Vector3 (br, bg, bb),
                    Color::Vector3 (fr, fg, fb),
                ) => {
                    // Order the RGB values so we can directly
                    // copy them into the image.
                    let bg_colors: [u8; 3] = [br, bg, bb];
                    let fg_colors: [u8; 3] = [fr, fg, fb];

                    // Each `flipping_three_segment_for` macro
                    // will be expanded into three for loops below.
                    //
                    // I could have used just one for loop (and I did once)
                    // but this made a significant difference in
                    // the performance.
                    //
                    // The `pixel` macro is used to access pixels.
                    //
                    // See src/macros/*.rs for the defenitions.


                    #[cfg(feature = "rlibc")]
                    unsafe {
                        flipping_three_segment_for!{
                                for y in 0, max_translated, min_translated, h, {
                                        rlibc::memcpy(
                                            &mut pixel!(img[fullw, fullh, 3; offx+x, offy+y, 0]) as _,
                                            &bg_colors[0] as _,
                                            3
                                            ),
                                        rlibc::memcpy(
                                            &mut pixel!(img[fullw, fullh, 3; offx+x, offy+y, 0]) as _,
                                            &fg_colors[0] as _,
                                            3
                                            )
                                }
                            }
                    }

                    // A similar implementation is possible without
                    // the rlibc crate, but it appeared to be
                    // slightly slower.
                    #[cfg(not(feature = "rlibc"))]
                    {
                        flipping_three_segment_for!{
                                for y in 0, max_translated, min_translated, h, {
                                    (&mut pixel!(img[fullw, fullh, 3; offx+x, offy+y, 0 => 4]))
                                        .write(&bg_colors).unwrap(),
                                    (&mut pixel!(img[fullw, fullh, 3; offx+x, offy+y, 0 => 4]))
                                        .write(&fg_colors).unwrap()
                                }
                            }
                    }

                },

                (
                    Color::Vector4 (br, bg, bb, ba),
                    Color::Vector4 (fr, fg, fb, fa),
                ) => {

                    // Order the RGBA values so we can directly
                    // copy them into the image.
                    let bg_colors: [u8; 4] = [br, bg, bb, ba];
                    let fg_colors: [u8; 4] = [fr, fg, fb, fa];

                    // Each `flipping_three_segment_for` macro
                    // will be expanded into three for loops below.
                    //
                    // I could have used just one for loop (and I did once)
                    // but this made a significant difference in
                    // the performance.
                    //
                    // The `pixel` macro is used to access pixels.
                    //
                    // See src/macros/*.rs for the defenitions.


                    #[cfg(feature = "rlibc")]
                    unsafe {
                        flipping_three_segment_for!{
                                for y in 0, max_translated, min_translated, h, {
                                        rlibc::memcpy(
                                            &mut pixel!(img[fullw, fullh, 4; offx+x, offy+y, 0]) as _,
                                            &bg_colors[0] as _,
                                            4
                                            ),
                                        rlibc::memcpy(
                                            &mut pixel!(img[fullw, fullh, 4; offx+x, offy+y, 0]) as _,
                                            &fg_colors[0] as _,
                                            4
                                            )
                                }
                            }
                    }

                    // A similar implementation is possible without
                    // the rlibc crate, but it appeared to be
                    // slightly slower.
                    #[cfg(not(feature = "rlibc"))]
                    {
                        flipping_three_segment_for!{
                                for y in 0, max_translated, min_translated, h, {
                                    (&mut pixel!(img[fullw, fullh, 4; offx+x, offy+y, 0 => 4]))
                                        .write(&bg_colors).unwrap(),
                                    (&mut pixel!(img[fullw, fullh, 4; offx+x, offy+y, 0 => 4]))
                                        .write(&fg_colors).unwrap()
                                }
                            }
                    }
                },

                // This case is unreachable because inconsistent
                // `Color` formats are checked whenever a user
                // creates a `WaveformConfig`.
                (_, _) => unreachable!(), 
            }
        }

        Ok(())
    }

    pub fn get_bin_size(&self) -> usize {
        self.bin_size
    }
    pub fn get_sample_rate(&self) -> f64 {
        self.sample_rate
    }
}

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

    #[test]
    fn render_vec_and_write_eq() {
        let tr = TimeRange::Seconds(0f64, 10f64);
        let (width, height) = (1000, 100);
        let mut samples: Vec<f64> = Vec::new();
        for t in 0u32..44100u32 {
            samples.push(((t as f64) * 0.01f64 * 2f64 * 3.1415f64).sin());
        }
        let config = WaveformConfig::new(
            -1f64,
            1f64,
            Color::Vector4(0, 0, 0, 255),
            Color::Vector4(0, 0, 0, 255)
            ).unwrap();
        let wfr = BinnedWaveformRenderer::new(
            &SampleSequence {
                data: &samples[..],
                sample_rate: 44100f64,
            },
            10,
            config,
        ).unwrap();

        let v1 = wfr.render_vec(tr, (width, height)).unwrap();

        let mut v2: Vec<u8> = vec![0; width*height*4];

        wfr.render_write(tr, (0, 0), (width, height), &mut v2[..], (width, height)).unwrap();

        assert_eq!(v1, v2);
    }

    #[test]
    fn markers() {
        let c = Color::Scalar(0);
        let config = WaveformConfig::new(-1f64, 1f64, c, c).unwrap();
        let wfr = BinnedWaveformRenderer::new(
            &SampleSequence {
                data: &vec![0f64; 10],
                sample_rate: 44100f64,
            },
            10, config
        ).unwrap();
        let _test: &(Sync+Send) = &wfr;
    }
}