2020-08-11 10:27:40 +00:00
|
|
|
//! The ncpixel API facilitates direct management of the pixels within an
|
|
|
|
//! ncvisual (ncvisuals keep a backing store of 32-bit RGBA pixels, and render
|
|
|
|
//! them down to terminal graphics in ncvisual_render()).
|
|
|
|
//
|
|
|
|
// - NOTE: The pixel color & alpha components are u8 instead of u32.
|
|
|
|
// Because of type enforcing, some runtime checks are now unnecessary.
|
|
|
|
//
|
|
|
|
// - NOTE: None of the functions can't fail anymore and don't have to return an error.
|
|
|
|
//
|
2020-12-04 01:25:49 +00:00
|
|
|
// functions manually reimplemented: 10
|
|
|
|
// ------------------------------------------
|
2020-12-21 20:15:10 +00:00
|
|
|
// (+) done: 10 / 0
|
|
|
|
// (#) test: 0
|
|
|
|
// (W) wrap: 10
|
2020-08-12 22:46:52 +00:00
|
|
|
// ------------------------------------------
|
2020-12-21 20:15:10 +00:00
|
|
|
//W+ ncpixel
|
|
|
|
//W+ ncpixel_a
|
|
|
|
//W+ ncpixel_b
|
|
|
|
//W+ ncpixel_g
|
|
|
|
//W+ ncpixel_r
|
|
|
|
//W+ ncpixel_set_a
|
|
|
|
//W+ ncpixel_set_b
|
|
|
|
//W+ ncpixel_set_g
|
|
|
|
//W+ ncpixel_set_r
|
|
|
|
//W+ ncpixel_set_rgb8
|
2020-08-10 21:44:40 +00:00
|
|
|
|
2020-12-04 01:25:49 +00:00
|
|
|
use crate::NcColor;
|
2020-08-10 21:44:40 +00:00
|
|
|
|
2020-12-04 01:25:49 +00:00
|
|
|
// NcPixel (RGBA)
|
2020-12-21 19:34:50 +00:00
|
|
|
/// An ABGR pixel.
|
2020-12-04 01:25:49 +00:00
|
|
|
///
|
|
|
|
/// ## Diagram
|
|
|
|
///
|
|
|
|
/// ```txt
|
|
|
|
/// AAAAAAAA GGGGGGGG BBBBBBBB RRRRRRRR
|
|
|
|
/// ```
|
2020-12-21 19:34:50 +00:00
|
|
|
///
|
2020-12-04 01:25:49 +00:00
|
|
|
/// `type in C: ncpixel (uint32_t)`
|
|
|
|
///
|
2020-12-21 19:34:50 +00:00
|
|
|
/// NcPixel has 8 bits of alpha, more or less linear, contributing
|
|
|
|
/// directly to the usual alpha blending equation.
|
|
|
|
///
|
|
|
|
/// We map the 8 bits of alpha to 2 bits of alpha via a [level
|
|
|
|
/// function](https://nick-black.com/dankwiki/index.php?title=Notcurses#Transparency.2FContrasting)
|
|
|
|
///
|
|
|
|
/// The ncpixel API facilitates direct management of the pixels within an
|
|
|
|
/// ncvisual (ncvisuals keep a backing store of 32-bit RGBA pixels, and render
|
|
|
|
/// them down to terminal graphics in ncvisual_render()).
|
|
|
|
///
|
|
|
|
/// Per libav, we "store as BGRA on little-endian, and ARGB on big-endian".
|
|
|
|
/// This is an RGBA *byte-order* scheme. libav emits bytes, not words. Those
|
|
|
|
/// bytes are R-G-B-A. When read as words, on little endian this will be ABGR,
|
|
|
|
/// and on big-endian this will be RGBA. force everything to LE ABGR.
|
|
|
|
///
|
2020-12-04 01:25:49 +00:00
|
|
|
pub type NcPixel = u32;
|
2020-08-13 15:28:58 +00:00
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Constructs a libav-compatible ABGR pixel from [NcColor] RGB components.
|
|
|
|
#[inline]
|
2020-12-21 20:15:10 +00:00
|
|
|
pub const fn ncpixel(red: NcColor, green: NcColor, blue: NcColor) -> NcPixel {
|
|
|
|
0xff000000 as NcPixel | red as NcPixel | (blue as NcPixel) << 8 | (green as NcPixel) << 16
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
2020-08-10 21:44:40 +00:00
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Extracts the 8-bit alpha component from an ABGR pixel.
|
|
|
|
#[inline]
|
|
|
|
pub const fn ncpixel_a(pixel: NcPixel) -> NcColor {
|
|
|
|
((pixel.to_le() & 0xff000000) >> 24) as NcColor
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
2020-08-10 21:44:40 +00:00
|
|
|
|
2020-12-21 20:15:10 +00:00
|
|
|
/// Extracts the 8 bit blue component from an ABGR pixel.
|
2020-12-21 19:34:50 +00:00
|
|
|
#[inline]
|
2020-12-21 20:15:10 +00:00
|
|
|
pub const fn ncpixel_b(pixel: NcPixel) -> NcColor {
|
2020-12-21 19:34:50 +00:00
|
|
|
((pixel.to_le() & 0x00ff0000) >> 16) as NcColor
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 20:15:10 +00:00
|
|
|
/// Extracts the 8 bit green component from an ABGR pixel.
|
2020-12-21 19:34:50 +00:00
|
|
|
#[inline]
|
2020-12-21 20:15:10 +00:00
|
|
|
pub const fn ncpixel_g(pixel: NcPixel) -> NcColor {
|
2020-12-21 19:34:50 +00:00
|
|
|
((pixel.to_le() & 0x0000ff00) >> 8) as NcColor
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Extracts the 8 bit red component from an ABGR pixel.
|
|
|
|
#[inline]
|
|
|
|
pub const fn ncpixel_r(pixel: NcPixel) -> NcColor {
|
|
|
|
(pixel.to_le() & 0x000000ff) as NcColor
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Sets the 8-bit alpha component of an ABGR pixel.
|
|
|
|
#[inline]
|
2020-11-25 12:27:45 +00:00
|
|
|
pub fn ncpixel_set_a(pixel: &mut NcPixel, alpha: NcColor) {
|
2020-12-21 19:34:50 +00:00
|
|
|
*pixel = (((*pixel).to_le() & 0x00ffffff) | ((alpha as NcPixel) << 24)).to_le();
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Sets the 8-bit blue component of an ABGR pixel.
|
|
|
|
#[inline]
|
2020-11-25 12:27:45 +00:00
|
|
|
pub fn ncpixel_set_b(pixel: &mut NcPixel, blue: NcColor) {
|
2020-12-21 19:34:50 +00:00
|
|
|
*pixel = (((*pixel).to_le() & 0xffff00ff) | ((blue as NcPixel) << 8)).to_le();
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 20:15:10 +00:00
|
|
|
/// Sets the 8-bit green component of an ABGR pixel.
|
|
|
|
#[inline]
|
|
|
|
pub fn ncpixel_set_g(pixel: &mut NcPixel, green: NcColor) {
|
|
|
|
*pixel = (((*pixel).to_le() & 0xff00ffff) | ((green as NcPixel) << 16)).to_le();
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Sets the 8-bit red component of an ABGR pixel.
|
|
|
|
#[inline]
|
2020-11-25 12:27:45 +00:00
|
|
|
pub fn ncpixel_set_r(pixel: &mut NcPixel, red: NcColor) {
|
2020-12-21 19:34:50 +00:00
|
|
|
*pixel = (((*pixel).to_le() & 0xffffff00) | red as NcPixel).to_le();
|
2020-08-11 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:50 +00:00
|
|
|
/// Sets the [NcColor] RGB components of an ABGR pixel.
|
|
|
|
#[inline]
|
|
|
|
pub fn ncpixel_set_rgb8(pixel: &mut NcPixel, red: NcColor, green: NcColor, blue: NcColor) {
|
2020-08-11 10:27:40 +00:00
|
|
|
ncpixel_set_r(pixel, red);
|
|
|
|
ncpixel_set_g(pixel, green);
|
|
|
|
ncpixel_set_b(pixel, blue);
|
|
|
|
}
|
2020-12-21 20:15:10 +00:00
|
|
|
|
|
|
|
/// Enables the [NcPixel] methods.
|
|
|
|
//
|
|
|
|
// NOTE: waiting for: https://github.com/rust-lang/rust/issues/56546
|
|
|
|
// to move doc comments to the trait and appear unhidden at the implementation.
|
|
|
|
pub trait NcPixelMethods {
|
|
|
|
fn new(r: NcColor, g: NcColor, b: NcColor) -> NcPixel;
|
|
|
|
fn a(self) -> NcColor;
|
|
|
|
fn b(self) -> NcColor;
|
|
|
|
fn g(self) -> NcColor;
|
|
|
|
fn r(self) -> NcColor;
|
|
|
|
fn set_a(&mut self, green: NcColor);
|
|
|
|
fn set_b(&mut self, blue: NcColor);
|
|
|
|
fn set_g(&mut self, green: NcColor);
|
|
|
|
fn set_r(&mut self, red: NcColor);
|
|
|
|
fn set_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NcPixelMethods for NcPixel {
|
|
|
|
/// Constructs a libav-compatible ABGR pixel from [NcColor] RGB components.
|
|
|
|
fn new(red: NcColor, green: NcColor, blue: NcColor) -> NcPixel {
|
|
|
|
ncpixel(red, green, blue)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the 8-bit alpha component from an ABGR pixel.
|
|
|
|
fn a(self) -> NcColor {
|
|
|
|
ncpixel_a(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the 8 bit blue component from an ABGR pixel.
|
|
|
|
fn b(self) -> NcColor {
|
|
|
|
ncpixel_b(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the 8 bit green component from an ABGR pixel.
|
|
|
|
fn g(self) -> NcColor {
|
|
|
|
ncpixel_g(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the 8 bit red component from an ABGR pixel.
|
|
|
|
fn r(self) -> NcColor {
|
|
|
|
ncpixel_r(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the 8-bit alpha component of an ABGR pixel.
|
|
|
|
fn set_a(&mut self, alpha: NcColor) {
|
|
|
|
ncpixel_set_a(self, alpha)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the 8-bit green component of an ABGR pixel.
|
|
|
|
fn set_g(&mut self, green: NcColor) {
|
|
|
|
ncpixel_set_b(self, green)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the 8-bit blue component of an ABGR pixel.
|
|
|
|
fn set_b(&mut self, blue: NcColor) {
|
|
|
|
ncpixel_set_b(self, blue)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the 8-bit red component of an ABGR pixel.
|
|
|
|
fn set_r(&mut self, red: NcColor) {
|
|
|
|
ncpixel_set_r(self, red)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the [NcColor] RGB components of an ABGR pixel.
|
|
|
|
fn set_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor) {
|
|
|
|
ncpixel_set_rgb8(self, red, green, blue);
|
|
|
|
}
|
|
|
|
}
|