libnotcurses_sys/blitter/
methods.rs

1//! `NcBlitter` methods
2
3pub use crate::{c_api, error, NcBlitter, NcResult, NcVisualOptions};
4#[cfg(not(feature = "std"))]
5use alloc::format;
6use core::ffi::c_void;
7
8impl NcBlitter {
9    /// The number of `height` subdivisions in a cell using the current blitter.
10    ///
11    /// Default & Pixel returns `None`.
12    pub const fn cell_height(&self) -> Option<u8> {
13        // self.cell_size().and_then(|size| Some(size.0) ) // not const
14        if let Some(size) = self.cell_size() {
15            Some(size.0)
16        } else {
17            None
18        }
19    }
20
21    /// The number of `width` subdivisions in a cell using the current blitter.
22    ///
23    /// Default & Pixel returns `None`.
24    pub const fn cell_width(&self) -> Option<u8> {
25        // self.cell_size().and_then(|size| Some(size.1) ) // not const
26        if let Some(size) = self.cell_size() {
27            Some(size.1)
28        } else {
29            None
30        }
31    }
32
33    /// The inner Cell's dimensions `(height, width)` using the current blitter.
34    ///
35    /// Default & Pixel returns `None`.
36    pub const fn cell_size(&self) -> Option<(u8, u8)> {
37        use NcBlitter::*;
38        match self {
39            Ascii => Some((1, 1)),
40            Half => Some((2, 1)),
41            Quadrant => Some((2, 2)),
42            Sextant => Some((3, 2)),
43            Braille => Some((4, 2)),
44            _4x1 => Some((4, 1)),
45            _8x1 => Some((8, 1)),
46            _ => None,
47        }
48    }
49}
50
51impl NcBlitter {
52    /// Blits a flat array `data` of [`NcRgba`] values to the [`NcPlane`] that
53    /// must be configured in `vopts`.
54    ///
55    /// The blit begins at `vopts.y` and `vopts.x` relative to the plane.
56    ///
57    /// Each source row ought occupy `line_size` bytes (this might be greater
58    /// than `vopts.lenx` * 4 due to padding or partial blits).
59    ///
60    /// A subregion of the input can be specified with the `begy`×`begx` and
61    /// `leny`×`lenx` fields from `vopts`.
62    ///
63    /// Returns the number of pixels blitted on success.
64    ///
65    /// *C style function: [ncblit_rgba()][c_api::ncblit_rgba].*
66    ///
67    /// [`NcRgba`]: crate::NcRgba
68    /// [`NcPlane`]: crate::NcPlane
69    pub fn blit_rgba(data: &[u8], line_size: usize, vopts: &NcVisualOptions) -> NcResult<usize> {
70        let data_ptr: *const c_void = data as *const _ as *const c_void;
71        let res = unsafe { c_api::ncblit_rgba(data_ptr, line_size as i32, vopts) };
72        error![
73            res,
74            &format!["NcBlitter::blit_rgba(data, {}, {:?})", line_size, vopts],
75            res as usize
76        ];
77    }
78
79    /// Like [`blit_rgba`][NcBlitter#method.blit_rgba], but for BGRx.
80    ///
81    /// *C style function: [ncblit_bgrx()][c_api::ncblit_bgrx].*
82    pub fn blit_bgrx(data: &[u8], line_size: usize, vopts: &NcVisualOptions) -> NcResult<usize> {
83        let data_ptr: *const c_void = data as *const _ as *const c_void;
84        let res = unsafe { c_api::ncblit_bgrx(data_ptr, line_size as i32, vopts) };
85        error![
86            res,
87            &format!["NcBlitter::blit_bgrx(data, {}, {:?})", line_size, vopts],
88            res as usize
89        ];
90    }
91
92    /// Like [`blit_rgba`][NcBlitter#method.blit_rgba], but for RGB.
93    ///
94    /// `line_size` must be a multiple of 3 for this RGB data.
95    ///
96    /// Supply an `alpha` value to be applied throughout.
97    ///
98    /// *C style function: [ncblit_rgb_packed()][c_api::ncblit_rgb_packed].*
99    pub fn blit_rgb_packed(
100        data: &[u8],
101        line_size: usize,
102        vopts: &NcVisualOptions,
103        alpha: u8,
104    ) -> NcResult<usize> {
105        let data_ptr: *const c_void = data as *const _ as *const c_void;
106        let res =
107            unsafe { c_api::ncblit_rgb_packed(data_ptr, line_size as i32, vopts, alpha as i32) };
108        error![
109            res,
110            &format![
111                "NcBlitter::blit_rgb_packed(data, {}, {:?}, {})",
112                line_size, vopts, alpha
113            ],
114            res as usize
115        ];
116    }
117
118    /// Like [`blit_rgb_packed`][NcBlitter#method.blit_rgb_packed], but `line_size`
119    /// must be a multiple of 4 for this RGBx data.
120    ///
121    /// Supply an `alpha` value to be applied throughout.
122    ///
123    /// *C style function: [ncblit_rgb_loose()][c_api::ncblit_rgb_loose].*
124    pub fn blit_rgb_loose(
125        data: &[u8],
126        line_size: usize,
127        vopts: &NcVisualOptions,
128        alpha: u8,
129    ) -> NcResult<usize> {
130        let data_ptr: *const c_void = data as *const _ as *const c_void;
131        let res =
132            unsafe { c_api::ncblit_rgb_loose(data_ptr, line_size as i32, vopts, alpha as i32) };
133        error![
134            res,
135            &format![
136                "NcBlitter::blit_rgb_loose(data, {}, {:?}, {})",
137                line_size, vopts, alpha
138            ],
139            res as usize
140        ];
141    }
142}