From 4720f23b123562ee38c8bd4943907a12a83e1bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Thu, 13 Aug 2020 00:07:50 +0200 Subject: [PATCH 1/2] rust: improve some channel functions & comments Functions `channel_set_alpha()`, `channels_set_fg_alpha()` & `channels_set_bg_alpha()` don't return an error anymore. Instead of failing when the alpha value has others bits set, it gets cleaned of other possible bits with bitmasking. And in case of trying to use high contrast for the background alpha gets reset to a default opaque. --- rust/src/channel.rs | 55 ++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/rust/src/channel.rs b/rust/src/channel.rs index 486908165..9a3c7d312 100644 --- a/rust/src/channel.rs +++ b/rust/src/channel.rs @@ -1,24 +1,14 @@ // --------------------------------------------------------------------------------------- -// - NOTE: The channel components are u8 instead of u32. +// - The channel components are u8 instead of u32. // Because of type enforcing, some runtime checks are now unnecessary. // -// - NOTE: These functions now can't fail and don't have to return an error: -// - `channel_set_rgb()` -// - `channels_set_fg_rgb()` -// - `channels_set_bg_rgb()` -// - `channel_set()` -// - `channels_set_fg()` -// - `channels_set_bg()` +// - None of the functions can't fail now. The original checks for dirty bits +// have been substitued by mask cleaning (bitwise and) // -// - NOTE: These functions were therefore deemed unnecessary to implement: +// - These functions were deemed unnecessary to implement: // - `channel_set_rgb_clipped()` // - `channels_set_fg_rgb_clipped()` // - `channels_set_bg_rgb_clipped()` -// -// - These functions still return an integer error result: -// - `channel_set_alpha()` -// - `channels_set_fg_alpha()` -// - `channels_set_bg_alpha()` // --------------------------------------------------------------------------------------- // // functions already exported by bindgen : 0 @@ -31,7 +21,7 @@ // --------------- (+) implemented (#) + unit test (x) wont implement //#channel_alpha //#channel_b -//#channel_default_p // FIXME TEST +//#channel_default_p //#channel_g //+channel_palindex_p //#channel_r @@ -42,7 +32,7 @@ //+channels_bg_default_p //+channels_bg_palindex_p //+channels_bg_rgb -// channels_blend // TODO +// channels_blend // TODO //#channels_combine //+channel_set //#channel_set_alpha @@ -67,12 +57,12 @@ //+channels_set_fg_default //+channels_set_fg_rgb //xchannels_set_fg_rgb_clipped -// + #![allow(dead_code)] use crate as ffi; -use crate::types::{Alpha, Channel, ChannelPair, Color, IntResult, Rgb}; +use crate::types::{Alpha, Channel, ChannelPair, Color, Rgb}; /// Extract the 8-bit red component from a 32-bit channel. #[inline] @@ -124,16 +114,14 @@ pub fn channel_alpha(channel: Channel) -> Alpha { /// Set the 2-bit alpha component of the 32-bit channel. #[inline] -pub fn channel_set_alpha(channel: &mut Channel, alpha: Alpha) -> IntResult { - if (alpha & !ffi::NCCHANNEL_ALPHA_MASK) != 0 { - return -1; - } - *channel = alpha | (*channel & !ffi::NCCHANNEL_ALPHA_MASK); +pub fn channel_set_alpha(channel: &mut Channel, alpha: Alpha) { + let mut alpha_clean = alpha & ffi::NCCHANNEL_ALPHA_MASK; + *channel = alpha_clean | (*channel & !ffi::NCCHANNEL_ALPHA_MASK); + if alpha != ffi::CELL_ALPHA_OPAQUE { // indicate that we are *not* using the default background color *channel |= ffi::CELL_BGDEFAULT_MASK; } - 0 } /// Is this channel using the "default color" rather than RGB/palette-indexed? @@ -285,29 +273,24 @@ pub fn channels_set_bg(channels: &mut ChannelPair, rgb: Rgb) { /// Set the 2-bit alpha component of the foreground channel. // TODO: TEST #[inline] -pub fn channels_set_fg_alpha(channels: &mut ChannelPair, alpha: Alpha) -> IntResult { +pub fn channels_set_fg_alpha(channels: &mut ChannelPair, alpha: Alpha) { let mut channel = channels_fchannel(*channels); - if channel_set_alpha(&mut channel, alpha) < 0 { - return -1; - } + channel_set_alpha(&mut channel, alpha); *channels = (channel as ChannelPair) << 32 | *channels & 0xffffffff_u64; - 0 } /// Set the 2-bit alpha component of the background channel. // TODO: TEST #[inline] -pub fn channels_set_bg_alpha(channels: &mut ChannelPair, alpha: Alpha) -> IntResult { +pub fn channels_set_bg_alpha(channels: &mut ChannelPair, alpha: Alpha) { + let mut _alpha_clean = alpha; if alpha == ffi::CELL_ALPHA_HIGHCONTRAST { - // forbidden for background alpha - return -1; + // forbidden for background alpha, so makes it opaque + _alpha_clean = ffi::CELL_ALPHA_OPAQUE; } let mut channel = channels_bchannel(*channels); - if channel_set_alpha(&mut channel, alpha) < 0 { - return -1; - } + channel_set_alpha(&mut channel, _alpha_clean); channels_set_bchannel(channels, channel); - 0 } /// Is the foreground using the "default foreground color"? From d0f691af5f7b226a4ff9741b84d227004717f3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Thu, 13 Aug 2020 00:46:52 +0200 Subject: [PATCH 2/2] rust: +8 plane functions; fix headers; minor fixes --- rust/src/cells.rs | 14 ++---- rust/src/channel.rs | 18 +++---- rust/src/key.rs | 8 +-- rust/src/keycodes.rs | 2 +- rust/src/nc.rs | 8 +-- rust/src/palette.rs | 14 +++--- rust/src/pixel.rs | 8 +-- rust/src/plane.rs | 115 +++++++++++++++++++++++-------------------- 8 files changed, 96 insertions(+), 91 deletions(-) diff --git a/rust/src/cells.rs b/rust/src/cells.rs index 0ed8984f5..ac308a4d6 100644 --- a/rust/src/cells.rs +++ b/rust/src/cells.rs @@ -8,10 +8,10 @@ // - cells_rounded_box // // static inline functions to reimplement: 45 -// ------------------------------------------ (done / wont / remaining) -// - implement : 2 / 0 / 43 -// - unit tests: 0 / 0 / 45 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ------------------------------------------ (done / (x) wont / remaining) +// (+) implement : 2 / 0 / 43 +// (#) unit tests: 0 / 0 / 45 +// ------------------------------------------ // cell_bchannel // cell_bg // cell_bg_alpha @@ -49,7 +49,7 @@ // cell_set_fg_rgb // cell_set_fg_rgb_clipped // cell_simple_p -//+cells_load_box // FIXME +//+cells_load_box // FIXME // cell_strdup // cell_styles // cell_styles_off @@ -57,10 +57,6 @@ // cell_styles_set // cell_wide_left_p // cell_wide_right_p -// -// NOTE: -// - ? gcluster (&str) > type alias? -// - ? attr (u32) > type alias? use cstr_core::CString; diff --git a/rust/src/channel.rs b/rust/src/channel.rs index 9a3c7d312..9f04db14c 100644 --- a/rust/src/channel.rs +++ b/rust/src/channel.rs @@ -15,10 +15,10 @@ // ------------------------------------------ // // static inline functions to reimplement: 38 -// ------------------------------------------ (done / wont / remaining) -// - implement : 34 / 3 / 1 -// - unit tests: 14 / 0 / 21 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ------------------------------------------ (done / (x) wont / remaining) +// (+) implement : 34 / 3 / 1 +// (#) unit tests: 14 / 0 / 21 +// ------------------------------------------ //#channel_alpha //#channel_b //#channel_default_p @@ -115,7 +115,7 @@ pub fn channel_alpha(channel: Channel) -> Alpha { /// Set the 2-bit alpha component of the 32-bit channel. #[inline] pub fn channel_set_alpha(channel: &mut Channel, alpha: Alpha) { - let mut alpha_clean = alpha & ffi::NCCHANNEL_ALPHA_MASK; + let alpha_clean = alpha & ffi::NCCHANNEL_ALPHA_MASK; *channel = alpha_clean | (*channel & !ffi::NCCHANNEL_ALPHA_MASK); if alpha != ffi::CELL_ALPHA_OPAQUE { @@ -283,13 +283,13 @@ pub fn channels_set_fg_alpha(channels: &mut ChannelPair, alpha: Alpha) { // TODO: TEST #[inline] pub fn channels_set_bg_alpha(channels: &mut ChannelPair, alpha: Alpha) { - let mut _alpha_clean = alpha; + let mut alpha_clean = alpha; if alpha == ffi::CELL_ALPHA_HIGHCONTRAST { // forbidden for background alpha, so makes it opaque - _alpha_clean = ffi::CELL_ALPHA_OPAQUE; + alpha_clean = ffi::CELL_ALPHA_OPAQUE; } let mut channel = channels_bchannel(*channels); - channel_set_alpha(&mut channel, _alpha_clean); + channel_set_alpha(&mut channel, alpha_clean); channels_set_bchannel(channels, channel); } @@ -473,7 +473,7 @@ mod test { let mut c: Channel = 0x112233; assert_eq!(true, super::channel_default_p(c)); - // TODO FIXME: succesfully test for the false result + // TODO FIXME: test for the false result // let _ = super::channel_set_alpha(&mut c, ffi::CELL_ALPHA_TRANSPARENT); // assert_eq!(false, super::channel_default_p(c)); diff --git a/rust/src/key.rs b/rust/src/key.rs index 4e931daaf..d3f147a8b 100644 --- a/rust/src/key.rs +++ b/rust/src/key.rs @@ -2,10 +2,10 @@ // ------------------------------------------ // // static inline functions to reimplement: 2 -// ------------------------------------------ (done / wont / remaining) -// - implement : 2 / 0 / 0 -// - unit tests: 0 / 0 / 2 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ------------------------------------------ (done / (x) wont / remaining) +// (+) implement : 2 / 0 / 0 +// (#) unit tests: 0 / 0 / 2 +// ------------------------------------------ //+nckey_mouse_p //+nckey_supppuab_p diff --git a/rust/src/keycodes.rs b/rust/src/keycodes.rs index 35aa03c20..8aa79dde6 100644 --- a/rust/src/keycodes.rs +++ b/rust/src/keycodes.rs @@ -106,7 +106,7 @@ pub const NCKEY_COPY: u32 = suppuabize(132); pub const NCKEY_EXIT: u32 = suppuabize(133); pub const NCKEY_PRINT: u32 = suppuabize(134); pub const NCKEY_REFRESH: u32 = suppuabize(135); -// + // Mouse events. We try to encode some details into the char32_t (i.e. which // button was pressed);, but some is embedded in the ncinput event. The release // event is generic across buttons; callers must maintain state, if they care. diff --git a/rust/src/nc.rs b/rust/src/nc.rs index 079ceb0bf..9986f294b 100644 --- a/rust/src/nc.rs +++ b/rust/src/nc.rs @@ -37,10 +37,10 @@ // notcurses_version_components // // static inline functions to reimplement: 4 -// ----------------------------------------- (done / wont / remaining) -// - implement : 0 / 0 / 4 -// - unit tests: 0 / 0 / 4 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ----------------------------------------- (done / (x) wont / remaining) +// (+) implement : 0 / 0 / 4 +// (#) unit tests: 0 / 0 / 4 +// ----------------------------------------- // notcurses_getc_blocking // notcurses_getc_nblock // notcurses_stddim_yx diff --git a/rust/src/palette.rs b/rust/src/palette.rs index 6b7f75416..a5d7e97b6 100644 --- a/rust/src/palette.rs +++ b/rust/src/palette.rs @@ -1,6 +1,6 @@ -// --------------------------------------------------------------------------------------- -// NOTE: Now none of these functions can't fail and therefore don't return errors. -// --------------------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// Now none of these functions can't fail and therefore don't return errors. +// ----------------------------------------------------------------------------- // // functions already exported by bindgen : 3 // ----------------------------------------- @@ -9,10 +9,10 @@ // palette256_use // // static inline functions to reimplement: 3 -// ----------------------------------------- (done / wont / remaining) -// - implement : 1 / 0 / 2 -// - unit tests: 0 / 0 / 3 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ----------------------------------------- (done / (x) wont / remaining) +// (+) implement : 1 / 0 / 2 +// (#) unit tests: 0 / 0 / 3 +// ----------------------------------------- //+palette256_get_rgb //+palette256_set //+palette256_set_rgb diff --git a/rust/src/pixel.rs b/rust/src/pixel.rs index d4e3db24e..31dbdd636 100644 --- a/rust/src/pixel.rs +++ b/rust/src/pixel.rs @@ -12,10 +12,10 @@ // ----------------------------------------- // // static inline functions to reimplement: 10 -// ------------------------------------------ (done / wont / remaining) -// - implement : 10 / 0 / 0 -// - unit tests: 0 / 0 / 10 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ------------------------------------------ (done / (x) wont / remaining) +// (+) implement : 10 / 0 / 0 +// (#) unit tests: 0 / 0 / 10 +// ------------------------------------------ //+ncpixel //+ncpixel_a //+ncpixel_b diff --git a/rust/src/plane.rs b/rust/src/plane.rs index 358dc847d..a03beea26 100644 --- a/rust/src/plane.rs +++ b/rust/src/plane.rs @@ -88,27 +88,27 @@ // ncplane_yx // // static inline functions to reimplement: 42 -// ------------------------------------------ (done / wont / remaining) -// - implement : 4 / 0 / 38 -// - unit tests: 0 / 0 / 42 -// --------------- (+) implemented (#) + unit test (x) wont implement +// ------------------------------------------ (done / (x) wont / remaining) +// (+) implement : 10 / … / 32 +// (#) unit tests: 0 / … / 42 +// ------------------------------------------ // ncplane_align // ncplane_at_cursor_cell // ncplane_at_yx_cell -// ncplane_bchannel -// ncplane_bg -// ncplane_bg_alpha -// ncplane_bg_default_p +//+ncplane_bchannel +//+ncplane_bg +//+ncplane_bg_alpha +//+ncplane_bg_default_p //+ncplane_bg_rgb // ncplane_box_sized //+ncplane_dim_x //+ncplane_dim_y // ncplane_double_box // ncplane_double_box_sized -// ncplane_fchannel -// ncplane_fg -// ncplane_fg_alpha -// ncplane_fg_default_p +//+ncplane_fchannel +//+ncplane_fg +//+ncplane_fg_alpha +//+ncplane_fg_default_p //+ncplane_fg_rgb // ncplane_gradient_sized // ncplane_highgradient_sized @@ -139,7 +139,7 @@ use core::ptr::null_mut; use cstr_core::CString; use crate as ffi; -use ffi::types::{Channel, Color, IntResult}; +use ffi::types::{Alpha, Channel, Color, IntResult}; pub fn ncplane_putstr(plane: *mut ffi::ncplane, _str: &str) -> i32 { unsafe { @@ -486,53 +486,62 @@ pub fn ncplane_perimeter( // return ncplane_highgradient(n, ul, ur, ll, lr, y + ylen - 1, x + xlen - 1); // } -// // Extract the 32-bit working background channel from an ncplane. -// static inline unsigned -// ncplane_bchannel(const struct ncplane* nc){ -// return channels_bchannel(ncplane_channels(nc)); -// } +/// Extract the 32-bit working foreground channel from an ncplane. +// TODO: TEST +#[inline] +pub fn ncplane_fchannel(plane: &ffi::ncplane) -> Channel { + ffi::channels_fchannel(unsafe { ffi::ncplane_channels(plane)}) +} -// // Extract the 32-bit working foreground channel from an ncplane. -// static inline unsigned -// ncplane_fchannel(const struct ncplane* nc){ -// return channels_fchannel(ncplane_channels(nc)); -// } +/// Extract the 32-bit working background channel from an ncplane. +// TODO: TEST +#[inline] +pub fn ncplane_bchannel(plane: &ffi::ncplane) -> Channel { + ffi::channels_bchannel(unsafe { ffi::ncplane_channels(plane)}) +} -// // Extract 24 bits of working foreground RGB from an ncplane, shifted to LSBs. -// static inline unsigned -// ncplane_fg(const struct ncplane* nc){ -// return channels_fg(ncplane_channels(nc)); -// } +/// Extract 24 bits of working foreground RGB from an ncplane, shifted to LSBs. +// TODO: TEST +#[inline] +pub fn ncplane_fg(plane: &ffi::ncplane) -> Channel { + ffi::channels_fg(unsafe { ffi::ncplane_channels(plane)}) +} -// // Extract 24 bits of working background RGB from an ncplane, shifted to LSBs. -// static inline unsigned -// ncplane_bg(const struct ncplane* nc){ -// return channels_bg(ncplane_channels(nc)); -// } -// // Extract 2 bits of foreground alpha from 'struct ncplane', shifted to LSBs. -// static inline unsigned -// ncplane_fg_alpha(const struct ncplane* nc){ -// return channels_fg_alpha(ncplane_channels(nc)); -// } +/// Extract 24 bits of working background RGB from an ncplane, shifted to LSBs. +// TODO: TEST +#[inline] +pub fn ncplane_bg(plane: &ffi::ncplane) -> Channel { + ffi::channels_bg(unsafe { ffi::ncplane_channels(plane)}) +} -// // Is the plane's foreground using the "default foreground color"? -// static inline bool -// ncplane_fg_default_p(const struct ncplane* nc){ -// return channels_fg_default_p(ncplane_channels(nc)); -// } +/// Extract 2 bits of foreground alpha from 'struct ncplane', shifted to LSBs. +// TODO: TEST +#[inline] +pub fn ncplane_fg_alpha(plane: &ffi::ncplane) -> Alpha { + ffi::channels_fg_alpha(unsafe { ffi::ncplane_channels(plane)}) +} -// // Extract 2 bits of background alpha from 'struct ncplane', shifted to LSBs. -// static inline unsigned -// ncplane_bg_alpha(const struct ncplane* nc){ -// return channels_bg_alpha(ncplane_channels(nc)); -// } +/// Extract 2 bits of background alpha from 'struct ncplane', shifted to LSBs. +// TODO: TEST +#[inline] +pub fn ncplane_bg_alpha(plane: &ffi::ncplane) -> Alpha { + ffi::channels_bg_alpha(unsafe { ffi::ncplane_channels(plane)}) +} -// // Is the plane's background using the "default background color"? -// static inline bool -// ncplane_bg_default_p(const struct ncplane* nc){ -// return channels_bg_default_p(ncplane_channels(nc)); -// } +/// Is the plane's foreground using the "default foreground color"? +// TODO: TEST +#[inline] +pub fn ncplane_fg_default_p(plane: &ffi::ncplane) -> bool { + ffi::channels_fg_default_p(unsafe { ffi::ncplane_channels(plane)}) +} + +/// Is the plane's background using the "default background color"? +// TODO: TEST +#[inline] +pub fn ncplane_bg_default_p(plane: &ffi::ncplane) -> bool { + ffi::channels_bg_default_p(unsafe { ffi::ncplane_channels(plane)}) +} /// Extract 24 bits of foreground RGB from a plane, split into components. // TODO: TEST