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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! Defines the processing block type.

use crate::{
    base::StreamProfileData,
    common::*,
    error::{ErrorChecker, Result as RsResult},
    frame::{marker as frame_marker, ExtendedFrame, Frame, GenericFrame},
    frame_queue::FrameQueue,
    kind::{ColorScheme, Extension, HoleFillingMode, PersistenceControl, Rs2Option, StreamKind},
    options::ToOptions,
};

pub mod marker {
    use super::*;

    pub trait ProcessingBlockKind {}
    pub trait ExtendableProcessingBlockKind
    where
        Self: ProcessingBlockKind,
    {
        const EXTENSION: Extension;
    }

    #[derive(Debug)]
    pub struct Any;
    impl ProcessingBlockKind for Any {}

    #[derive(Debug)]
    pub struct DecimationFilter;
    impl ProcessingBlockKind for DecimationFilter {}
    impl ExtendableProcessingBlockKind for DecimationFilter {
        const EXTENSION: Extension = Extension::DecimationFilter;
    }

    #[derive(Debug)]
    pub struct ThresholdFilter;
    impl ProcessingBlockKind for ThresholdFilter {}
    impl ExtendableProcessingBlockKind for ThresholdFilter {
        const EXTENSION: Extension = Extension::ThresholdFilter;
    }

    #[derive(Debug)]
    pub struct DisparityFilter;
    impl ProcessingBlockKind for DisparityFilter {}
    impl ExtendableProcessingBlockKind for DisparityFilter {
        const EXTENSION: Extension = Extension::DisparityFilter;
    }

    #[derive(Debug)]
    pub struct SpatialFilter;
    impl ProcessingBlockKind for SpatialFilter {}
    impl ExtendableProcessingBlockKind for SpatialFilter {
        const EXTENSION: Extension = Extension::SpatialFilter;
    }

    #[derive(Debug)]
    pub struct TemporalFilter;
    impl ProcessingBlockKind for TemporalFilter {}
    impl ExtendableProcessingBlockKind for TemporalFilter {
        const EXTENSION: Extension = Extension::TemporalFilter;
    }

    #[derive(Debug)]
    pub struct HoleFillingFilter;
    impl ProcessingBlockKind for HoleFillingFilter {}
    impl ExtendableProcessingBlockKind for HoleFillingFilter {
        const EXTENSION: Extension = Extension::HoleFillingFilter;
    }

    #[derive(Debug)]
    pub struct ZeroOrderFilter;
    impl ProcessingBlockKind for ZeroOrderFilter {}
    impl ExtendableProcessingBlockKind for ZeroOrderFilter {
        const EXTENSION: Extension = Extension::ZeroOrderFilter;
    }

    #[derive(Debug)]
    pub struct PointCloud;
    impl ProcessingBlockKind for PointCloud {}

    #[derive(Debug)]
    pub struct YuyDecoder;
    impl ProcessingBlockKind for YuyDecoder {}

    #[derive(Debug)]
    pub struct UnitsTransform;
    impl ProcessingBlockKind for UnitsTransform {}

    #[derive(Debug)]
    pub struct Syncer;
    impl ProcessingBlockKind for Syncer {}

    #[derive(Debug)]
    pub struct Align;
    impl ProcessingBlockKind for Align {}

    #[derive(Debug)]
    pub struct Colorizer;
    impl ProcessingBlockKind for Colorizer {}

    #[derive(Debug)]
    pub struct HuffmanDepthDecompress;
    impl ProcessingBlockKind for HuffmanDepthDecompress {}

    #[derive(Debug)]
    pub struct RatesPrinter;
    impl ProcessingBlockKind for RatesPrinter {}
}

/// The type returned by [ProcessingBlock::<Any>::try_extend](ProcessingBlock::try_extend).
///
/// It enumerates all possible frame extensions. If the frame failed to
/// extend any one of the kind, it falls back to [ExtendedProcessingBlock::Other](ExtendedProcessingBlock::Other) variant.
#[derive(Debug)]
pub enum ExtendedProcessingBlock {
    DecimationFilter(ProcessingBlock<marker::DecimationFilter>),
    ThresholdFilter(ProcessingBlock<marker::ThresholdFilter>),
    DisparityFilter(ProcessingBlock<marker::DisparityFilter>),
    SpatialFilter(ProcessingBlock<marker::SpatialFilter>),
    TemporalFilter(ProcessingBlock<marker::TemporalFilter>),
    HoleFillingFilter(ProcessingBlock<marker::HoleFillingFilter>),
    ZeroOrderFilter(ProcessingBlock<marker::ZeroOrderFilter>),
    Other(ProcessingBlock<marker::Any>),
}

/// The type of data processing unit.
#[derive(Debug)]
pub struct ProcessingBlock<Kind>
where
    Kind: marker::ProcessingBlockKind,
{
    pub(crate) ptr: NonNull<realsense_sys::rs2_processing_block>,
    queue: FrameQueue,
    _phantom: PhantomData<Kind>,
}

impl<Kind> ProcessingBlock<Kind>
where
    Kind: marker::ProcessingBlockKind,
{
    pub fn process<K>(&mut self, input: Frame<K>) -> RsResult<Frame<frame_marker::Any>>
    where
        K: frame_marker::FrameKind,
    {
        unsafe {
            let frame_ptr = input.take();
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_process_frame(
                self.ptr.as_ptr(),
                frame_ptr.as_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
        }
        let output = self.queue.wait(None)?;
        Ok(output)
    }

    pub async fn process_async<K>(&mut self, input: Frame<K>) -> RsResult<Frame<frame_marker::Any>>
    where
        K: frame_marker::FrameKind,
    {
        unsafe {
            let frame_ptr = input.take();
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_process_frame(
                self.ptr.as_ptr(),
                frame_ptr.as_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
        }
        let output = self.queue.wait_async(None).await?;
        Ok(output)
    }

    unsafe fn take(self) -> (NonNull<realsense_sys::rs2_processing_block>, FrameQueue) {
        let ptr = self.ptr;
        let queue = self.queue.unsafe_clone();
        std::mem::forget(self);
        (ptr, queue)
    }

    pub(crate) unsafe fn from_parts(
        ptr: NonNull<realsense_sys::rs2_processing_block>,
        queue: FrameQueue,
    ) -> Self {
        Self {
            ptr,
            queue,
            _phantom: PhantomData,
        }
    }

    pub(crate) unsafe fn new_from_ptr_and_capacity(
        ptr: NonNull<realsense_sys::rs2_processing_block>,
        capacity: usize,
    ) -> RsResult<Self> {
        let queue = FrameQueue::with_capacity(capacity)?;

        // start processing
        {
            let mut checker = ErrorChecker::new();
            realsense_sys::rs2_start_processing_queue(
                ptr.as_ptr(),
                queue.ptr.as_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
        }

        let block = Self {
            ptr,
            queue,
            _phantom: PhantomData,
        };
        Ok(block)
    }

    pub(crate) unsafe fn new_from_ptr(
        ptr: NonNull<realsense_sys::rs2_processing_block>,
    ) -> RsResult<Self> {
        Self::new_from_ptr_and_capacity(ptr, 1)
    }
}

impl ProcessingBlock<marker::Any> {
    pub fn is_extendable_to<Kind>(&self) -> RsResult<bool>
    where
        Kind: marker::ExtendableProcessingBlockKind,
    {
        unsafe {
            let mut checker = ErrorChecker::new();
            let val = realsense_sys::rs2_is_processing_block_extendable_to(
                self.ptr.as_ptr(),
                Kind::EXTENSION as realsense_sys::rs2_extension,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(val != 0)
        }
    }

    pub fn try_extend_to<Kind>(self) -> RsResult<Result<ProcessingBlock<Kind>, Self>>
    where
        Kind: marker::ExtendableProcessingBlockKind,
    {
        unsafe {
            let is_extendable = self.is_extendable_to::<Kind>()?;
            if is_extendable {
                let (ptr, queue) = self.take();
                let block = ProcessingBlock::from_parts(ptr, queue);
                Ok(Ok(block))
            } else {
                Ok(Err(self))
            }
        }
    }

    pub fn try_extend(self) -> RsResult<ExtendedProcessingBlock> {
        let frame_any = self;

        let frame_any = match frame_any.try_extend_to::<marker::DecimationFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::DecimationFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::ThresholdFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::ThresholdFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::DisparityFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::DisparityFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::SpatialFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::SpatialFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::TemporalFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::TemporalFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::HoleFillingFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::HoleFillingFilter(frame)),
            Err(frame) => frame,
        };

        let frame_any = match frame_any.try_extend_to::<marker::ZeroOrderFilter>()? {
            Ok(frame) => return Ok(ExtendedProcessingBlock::ZeroOrderFilter(frame)),
            Err(frame) => frame,
        };

        Ok(ExtendedProcessingBlock::Other(frame_any))
    }
}

impl ProcessingBlock<marker::ThresholdFilter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_threshold(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(min_dist: Option<f32>, max_dist: Option<f32>) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_threshold(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };

        let options = processing_block.to_options()?;
        if let Some(dist) = min_dist {
            options[&Rs2Option::MinDistance].set_value(dist)?;
        }
        if let Some(dist) = max_dist {
            options[&Rs2Option::MaxDistance].set_value(dist)?;
        }

        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::SpatialFilter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_spatial_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(
        smooth_alpha: f32,
        smooth_delta: f32,
        magnitude: f32,
        hole_fill: f32,
    ) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_temporal_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };

        let options = processing_block.to_options()?;
        options[&Rs2Option::FilterSmoothAlpha].set_value(smooth_alpha)?;
        options[&Rs2Option::FilterSmoothDelta].set_value(smooth_delta)?;
        options[&Rs2Option::FilterMagnitude].set_value(magnitude)?;
        options[&Rs2Option::HolesFill].set_value(hole_fill)?;

        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::TemporalFilter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_temporal_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(
        smooth_alpha: f32,
        smooth_delta: f32,
        persistence_control: PersistenceControl,
    ) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_temporal_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };

        let options = processing_block.to_options()?;
        options[&Rs2Option::HolesFill].set_value(persistence_control as usize as f32)?;
        options[&Rs2Option::FilterSmoothAlpha].set_value(smooth_alpha)?;
        options[&Rs2Option::FilterSmoothDelta].set_value(smooth_delta)?;

        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::DecimationFilter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_decimation_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(magnitude: f32) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_decimation_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };

        let options = processing_block.to_options()?;
        options[&Rs2Option::FilterMagnitude].set_value(magnitude)?;

        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::HoleFillingFilter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_hole_filling_filter_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(mode: HoleFillingMode) -> RsResult<Self> {
        let processing_block = Self::create()?;

        let options = processing_block.to_options()?;
        options[&Rs2Option::HolesFill].set_value(mode as usize as f32)?;

        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::DisparityFilter> {
    pub fn create() -> RsResult<Self> {
        Self::with_options(true)
    }

    pub fn with_options(transform_to_disparity: bool) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_disparity_transform_block(
                transform_to_disparity as c_uchar,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::PointCloud> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_pointcloud(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn calculate(
        &mut self,
        depth_frame: Frame<frame_marker::Depth>,
    ) -> RsResult<Frame<frame_marker::Points>> {
        let frame_any = self.process(depth_frame)?;
        match frame_any.try_extend()? {
            ExtendedFrame::Points(points_frame) => Ok(points_frame),
            ExtendedFrame::Composite(composite_frame) => {
                for result in composite_frame.try_into_iter()? {
                    let frame = result?;
                    if let Ok(points_frame) = frame.try_extend_to::<frame_marker::Points>()? {
                        return Ok(points_frame);
                    }
                }
                unreachable!();
            }
            _ => unreachable!(),
        }
    }

    pub fn map_to(&mut self, color_frame: Frame<frame_marker::Video>) -> RsResult<()> {
        let StreamProfileData {
            stream,
            format,
            index,
            ..
        } = color_frame.stream_profile()?.get_data()?;
        let options = self.to_options()?;
        options[&Rs2Option::StreamFilter].set_value(stream as realsense_sys::rs2_stream as f32)?;
        options[&Rs2Option::StreamFormatFilter]
            .set_value(format as realsense_sys::rs2_format as f32)?;
        options[&Rs2Option::StreamIndexFilter].set_value(index as f32)?;

        self.process(color_frame)?;
        Ok(())
    }

    pub async fn calculate_async(
        &mut self,
        depth_frame: Frame<frame_marker::Depth>,
    ) -> RsResult<Frame<frame_marker::Points>> {
        let frame_any = self.process_async(depth_frame).await?;
        match frame_any.try_extend()? {
            ExtendedFrame::Points(points_frame) => Ok(points_frame),
            ExtendedFrame::Composite(composite_frame) => {
                for result in composite_frame.try_into_iter()? {
                    let frame = result?;
                    if let Ok(points_frame) = frame.try_extend_to::<frame_marker::Points>()? {
                        return Ok(points_frame);
                    }
                }
                unreachable!();
            }
            _ => unreachable!(),
        }
    }

    pub async fn map_to_async(&mut self, color_frame: Frame<frame_marker::Video>) -> RsResult<()> {
        let StreamProfileData {
            stream,
            format,
            index,
            ..
        } = color_frame.stream_profile()?.get_data()?;
        let options = self.to_options()?;
        options[&Rs2Option::StreamFilter].set_value(stream as realsense_sys::rs2_stream as f32)?;
        options[&Rs2Option::StreamFormatFilter]
            .set_value(format as realsense_sys::rs2_format as f32)?;
        options[&Rs2Option::StreamIndexFilter].set_value(index as f32)?;

        self.process_async(color_frame).await?;
        Ok(())
    }
}

impl ProcessingBlock<marker::YuyDecoder> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_yuy_decoder(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::UnitsTransform> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_units_transform(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::Syncer> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_sync_processing_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::Align> {
    pub fn create(align_to: StreamKind) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_align(
                align_to as realsense_sys::rs2_stream,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::Colorizer> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_colorizer(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }

    pub fn with_options(color_scheme: ColorScheme) -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_colorizer(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };

        let options = processing_block.to_options()?;
        options[&Rs2Option::ColorScheme].set_value(color_scheme as usize as f32)?;

        Ok(processing_block)
    }

    pub fn colorize(
        &mut self,
        depth_frame: Frame<frame_marker::Depth>,
    ) -> RsResult<Frame<frame_marker::Video>> {
        let frame_any = self.process(depth_frame)?;
        let color_frame = frame_any.try_extend_to::<frame_marker::Video>()?.unwrap();
        Ok(color_frame)
    }
}

impl ProcessingBlock<marker::HuffmanDepthDecompress> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr =
                realsense_sys::rs2_create_huffman_depth_decompress_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl ProcessingBlock<marker::RatesPrinter> {
    pub fn create() -> RsResult<Self> {
        let processing_block = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_create_rates_printer_block(checker.inner_mut_ptr());
            checker.check()?;
            Self::new_from_ptr(NonNull::new(ptr).unwrap())?
        };
        Ok(processing_block)
    }
}

impl<Kind> ToOptions for ProcessingBlock<Kind>
where
    Kind: marker::ProcessingBlockKind,
{
    fn get_options_ptr(&self) -> NonNull<realsense_sys::rs2_options> {
        self.ptr.cast::<realsense_sys::rs2_options>()
    }
}

impl<Kind> Drop for ProcessingBlock<Kind>
where
    Kind: marker::ProcessingBlockKind,
{
    fn drop(&mut self) {
        unsafe {
            realsense_sys::rs2_delete_processing_block(self.ptr.as_ptr());
        }
    }
}

unsafe impl<Kind> Send for ProcessingBlock<Kind> where Kind: marker::ProcessingBlockKind {}