Maraiah/maraiah/image.rs

261 lines
5.4 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
//! Image and color representations.
2019-06-13 18:09:07 -07:00
pub mod pict;
pub mod ppm;
pub mod tga;
/// Creates a RGB8 color from a R5G5B5 format color.
2019-03-04 18:14:09 -08:00
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::image::{r5g5b5_to_rgb8, Color8};
2019-03-04 18:14:09 -08:00
///
/// assert_eq!(r5g5b5_to_rgb8(0x2345), Color8::new(64, 208, 40));
/// ```
2019-07-04 17:31:34 -07:00
#[inline]
pub const fn r5g5b5_to_rgb8(rgb: u16) -> Color8
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
let r = (rgb >> 10) as u8 & 0x1f;
let g = (rgb >> 5) as u8 & 0x1f;
let b = rgb as u8 & 0x1f;
Color8::new(r << 3, g << 3, b << 3)
2018-09-06 09:01:52 -07:00
}
/// Creates a RGB16 color from a R5G5B5 format color.
2019-03-04 18:14:09 -08:00
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::image::{r5g5b5_to_rgb16, Color16};
2019-03-04 18:14:09 -08:00
///
2019-04-01 06:05:06 -07:00
/// assert_eq!(r5g5b5_to_rgb16(0x2345),
/// Color16::new(0x4000, 0xD000, 0x2800));
2019-03-04 18:14:09 -08:00
/// ```
2019-07-04 17:31:34 -07:00
#[inline]
pub const fn r5g5b5_to_rgb16(rgb: u16) -> Color16
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
let r = rgb >> 10 & 0x1f;
let g = rgb >> 5 & 0x1f;
let b = rgb & 0x1f;
Color16::new(r << 11, g << 11, b << 11)
2018-09-06 09:01:52 -07:00
}
/// Creates a RGB16 color from a RGB8 format color.
2019-03-04 18:14:09 -08:00
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::image;
2019-03-04 18:14:09 -08:00
///
/// let inpl = image::r5g5b5_to_rgb8(0x2345);
/// let inph = image::r5g5b5_to_rgb16(0x2345);
///
/// assert_eq!(inph, image::rgb8_to_rgb16(inpl));
/// ```
pub fn rgb8_to_rgb16(cr: Color8) -> Color16
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
Color16::new(cr.r(), cr.g(), cr.b())
}
2019-03-01 01:27:14 -08:00
/// A generic color matrix image.
pub trait Image
{
2019-07-05 20:21:11 -07:00
/// The type of color this image uses for each pixel.
type Output: Color;
/// Returns the width of the image.
fn w(&self) -> usize;
/// Returns the height of the image.
fn h(&self) -> usize;
/// Returns the color of the pixel at column `x` at row `y`.
///
/// # Panics
///
/// Panics if `x` is greater than the width of the image or `y` is greater
/// than the height of the image.
fn index(&self, x: usize, y: usize) -> &Self::Output;
/// The same as `index`, but will not panic if out of bounds.
fn get(&self, x: usize, y: usize) -> Option<&Self::Output>
{
if x < self.w() && y < self.h() {
Some(self.index(x, y))
} else {
None
}
}
}
2019-03-18 05:18:46 -07:00
/// A generic color matrix image, which may be mutated.
pub trait ImageMut: Image
{
2019-07-05 20:21:11 -07:00
/// Returns the color of the pixel at column `x` at row `y`.
///
/// # Panics
///
/// Panics if `x` is greater than the width of the image or `y` is greater
/// than the height of the image.
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output;
/// The same as `index_mut`, but will not panic if out of bounds.
fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Self::Output>
{
if x < self.w() && y < self.h() {
Some(self.index_mut(x, y))
} else {
None
}
}
2019-03-18 05:18:46 -07:00
}
2019-03-01 01:27:14 -08:00
/// Any color which may be represented as RGBA16.
2019-03-13 07:53:30 -07:00
pub trait Color: Sized + Copy + Clone + Eq + PartialEq
{
2019-07-05 20:21:11 -07:00
/// Returns the red component.
fn r(&self) -> u16;
2019-03-01 01:27:14 -08:00
2019-07-05 20:21:11 -07:00
/// Returns the green component.
fn g(&self) -> u16;
2019-03-01 01:27:14 -08:00
2019-07-05 20:21:11 -07:00
/// Returns the blue component.
fn b(&self) -> u16;
2019-03-01 01:27:14 -08:00
2019-07-05 20:21:11 -07:00
/// Returns the alpha component.
fn a(&self) -> u16;
2018-09-06 09:01:52 -07:00
}
impl Image16
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
/// Creates a new Image16 with no canvas.
pub fn new(w: usize, h: usize) -> Self
{
Self{w, h, cr: Vec::with_capacity(w * h)}
}
/// Creates a new Image16 with an empty canvas.
pub fn new_empty(w: usize, h: usize) -> Self
{
Self{w, h, cr: vec![Color16::new(0, 0, 0); w * h]}
}
}
impl Image for Image16
{
2019-07-05 20:21:11 -07:00
type Output = Color16;
2019-02-08 21:53:27 -08:00
2019-07-05 20:21:11 -07:00
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
2019-07-05 20:21:11 -07:00
fn index(&self, x: usize, y: usize) -> &Self::Output
{
&self.cr[x + y * self.w]
}
}
2019-03-18 05:18:46 -07:00
impl ImageMut for Image16
{
2019-07-05 20:21:11 -07:00
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output
{
&mut self.cr[x + y * self.w]
}
2019-03-18 05:18:46 -07:00
}
impl Image8
{
2019-07-05 20:21:11 -07:00
/// Creates a new Image8 with no canvas.
pub fn new(w: usize, h: usize) -> Self
{
Self{w, h, cr: Vec::with_capacity(w * h)}
}
/// Creates a new Image8 with an empty canvas.
pub fn new_empty(w: usize, h: usize) -> Self
{
Self{w, h, cr: vec![Color8::new(0, 0, 0); w * h]}
}
2018-09-06 09:01:52 -07:00
}
impl Image for Image8
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
type Output = Color8;
2019-07-05 20:21:11 -07:00
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
2018-09-06 09:01:52 -07:00
2019-07-05 20:21:11 -07:00
fn index(&self, x: usize, y: usize) -> &Self::Output
{
&self.cr[x + y * self.w]
}
2018-09-06 09:01:52 -07:00
}
2019-03-18 05:18:46 -07:00
impl ImageMut for Image8
{
2019-07-05 20:21:11 -07:00
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output
{
&mut self.cr[x + y * self.w]
}
2019-03-18 05:18:46 -07:00
}
impl Color16
{
2019-07-05 20:21:11 -07:00
pub const fn new(r: u16, g: u16, b: u16) -> Self {Self(r, g, b)}
}
impl Color for Color16
{
2019-07-05 20:21:11 -07:00
fn r(&self) -> u16 {self.0}
fn g(&self) -> u16 {self.1}
fn b(&self) -> u16 {self.2}
fn a(&self) -> u16 {u16::max_value()}
}
impl Color8
{
2019-07-05 20:21:11 -07:00
pub const fn new(r: u8, g: u8, b: u8) -> Self {Self(r, g, b)}
}
impl Color for Color8
2018-09-06 09:01:52 -07:00
{
2019-07-05 20:21:11 -07:00
fn r(&self) -> u16 {u16::from(self.0) << 8}
fn g(&self) -> u16 {u16::from(self.1) << 8}
fn b(&self) -> u16 {u16::from(self.2) << 8}
fn a(&self) -> u16 {u16::max_value()}
}
2019-03-01 01:27:14 -08:00
/// An RGB16 color.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
2019-03-13 07:53:30 -07:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2019-02-09 21:52:51 -08:00
pub struct Color16(u16, u16, u16);
2019-03-01 01:27:14 -08:00
/// An RGB8 color.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
2019-03-13 07:53:30 -07:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2019-02-09 21:52:51 -08:00
pub struct Color8(u8, u8, u8);
2019-03-01 01:27:14 -08:00
/// An RGB16 image.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
2019-03-13 08:40:12 -07:00
#[derive(Debug, Eq, PartialEq)]
2019-06-25 03:52:21 -07:00
pub struct Image16 {
2019-07-05 20:21:11 -07:00
w: usize,
h: usize,
2019-03-09 14:03:20 -08:00
2019-07-05 20:21:11 -07:00
/// The raw color data for this image.
pub cr: Vec<Color16>,
}
2019-03-01 01:27:14 -08:00
/// An RGB8 image.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
2019-03-13 08:40:12 -07:00
#[derive(Debug, Eq, PartialEq)]
2019-06-25 03:52:21 -07:00
pub struct Image8 {
2019-07-05 20:21:11 -07:00
w: usize,
h: usize,
2019-03-09 14:03:20 -08:00
2019-07-05 20:21:11 -07:00
/// The raw color data for this image.
pub cr: Vec<Color8>,
2018-09-06 09:01:52 -07:00
}
// EOF