PrimInt may fuck off into oblivion

png-branch
an 2019-02-11 06:28:53 -05:00
parent b7a2e2cfde
commit 48b917998a
5 changed files with 33 additions and 53 deletions

View File

@ -20,11 +20,11 @@ macro_rules! c_enum
impl $E impl $E
{ {
pub fn from_repr(n: $T) -> Result<$E, ReprError<$T>> pub fn from_repr(n: $T) -> Result<$E, ReprError>
{ {
match n { match n {
$($value => Ok($E::$Enum),)+ $($value => Ok($E::$Enum),)+
n => Err(ReprError(n)) n => Err(ReprError(n.into()))
} }
} }
} }

View File

@ -2,7 +2,6 @@
pub use failure::{Error, Fail}; pub use failure::{Error, Fail};
use crate::durandal::traits::PrimInt;
use std::fmt; use std::fmt;
macro_rules! ok { macro_rules! ok {
@ -25,10 +24,10 @@ pub fn err_msg(msg: &'static str) -> Error
Error::from(ErrMsg(msg)) Error::from(ErrMsg(msg))
} }
impl<T> Fail for ReprError<T> where T: PrimInt {} impl Fail for ReprError {}
impl Fail for ErrMsg {} impl Fail for ErrMsg {}
impl<T> fmt::Display for ReprError<T> where T: PrimInt impl fmt::Display for ReprError
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{ {
@ -36,7 +35,7 @@ impl<T> fmt::Display for ReprError<T> where T: PrimInt
} }
} }
impl<T> fmt::Debug for ReprError<T> where T: PrimInt impl fmt::Debug for ReprError
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{ {
@ -61,7 +60,7 @@ impl fmt::Debug for ErrMsg
} }
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct ReprError<T>(pub T) where T: PrimInt; pub struct ReprError(pub i64);
struct ErrMsg(&'static str); struct ErrMsg(&'static str);

View File

@ -1,6 +1,6 @@
//! Image and color representations. //! Image and color representations.
use crate::durandal::{err::*, traits::*}; use crate::durandal::err::*;
use std::io; use std::io;
/// Creates a RGB8 color from a R5G5B5 format color. /// Creates a RGB8 color from a R5G5B5 format color.
@ -32,13 +32,15 @@ pub fn rgb8_to_rgb16(r: u8, g: u8, b: u8) -> Color16
/// Writes a PPM file from an image. /// Writes a PPM file from an image.
pub fn write_ppm<T: Image>(out: &mut impl io::Write, im: &T) -> ResultS<()> pub fn write_ppm<T: Image>(out: &mut impl io::Write, im: &T) -> ResultS<()>
{ {
write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), T::Output::MAX)?; write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), u16::max_value())?;
for y in 0..im.h() { for y in 0..im.h() {
for x in 0..im.w() { for x in 0..im.w() {
let cr = ok!(im.cr_at(x, y), "not enough data in image")?; let cr = im.cr_at(x, y);
write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?; write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?;
} }
out.write(b"\n")?;
} }
Ok(()) Ok(())
@ -50,18 +52,24 @@ pub trait Image
fn w(&self) -> usize; fn w(&self) -> usize;
fn h(&self) -> usize; fn h(&self) -> usize;
fn cr_at(&self, x: usize, y: usize) -> Option<&Self::Output>; fn cr_at(&self, x: usize, y: usize) -> &Self::Output;
fn cr_get(&self, x: usize, y: usize) -> Option<&Self::Output>
{
if x < self.w() && y < self.h() {
Some(self.cr_at(x, y))
} else {
None
}
}
} }
pub trait Color pub trait Color
{ {
type Comp: PrimInt; fn r(&self) -> u16;
const MAX: u32; fn g(&self) -> u16;
fn b(&self) -> u16;
fn r(&self) -> Self::Comp; fn a(&self) -> u16;
fn g(&self) -> Self::Comp;
fn b(&self) -> Self::Comp;
fn a(&self) -> Self::Comp;
} }
impl Image16 impl Image16
@ -80,9 +88,9 @@ impl Image for Image16
fn w(&self) -> usize {self.w} fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h} fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> Option<&Color16> fn cr_at(&self, x: usize, y: usize) -> &Self::Output
{ {
self.cr.get(x + y * self.w) &self.cr[x + y * self.w]
} }
} }
@ -102,9 +110,9 @@ impl Image for Image8
fn w(&self) -> usize {self.w} fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h} fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> Option<&Color8> fn cr_at(&self, x: usize, y: usize) -> &Self::Output
{ {
self.cr.get(x + y * self.w) &self.cr[x + y * self.w]
} }
} }
@ -118,9 +126,6 @@ impl Color16
impl Color for Color16 impl Color for Color16
{ {
type Comp = u16;
const MAX: u32 = u16::max_value() as u32;
fn r(&self) -> u16 {self.0} fn r(&self) -> u16 {self.0}
fn g(&self) -> u16 {self.1} fn g(&self) -> u16 {self.1}
fn b(&self) -> u16 {self.2} fn b(&self) -> u16 {self.2}
@ -137,13 +142,10 @@ impl Color8
impl Color for Color8 impl Color for Color8
{ {
type Comp = u8; fn r(&self) -> u16 {u16::from(self.0) << 8}
const MAX: u32 = u8::max_value() as u32; fn g(&self) -> u16 {u16::from(self.1) << 8}
fn b(&self) -> u16 {u16::from(self.2) << 8}
fn r(&self) -> u8 {self.0} fn a(&self) -> u16 {u16::max_value()}
fn g(&self) -> u8 {self.1}
fn b(&self) -> u8 {self.2}
fn a(&self) -> u8 {u8::max_value()}
} }
/// A RGB16 color. /// A RGB16 color.

View File

@ -11,6 +11,5 @@ pub mod crc;
pub mod fx32; pub mod fx32;
pub mod image; pub mod image;
pub mod text; pub mod text;
pub mod traits;
// EOF // EOF

View File

@ -1,20 +0,0 @@
//! Traits for basic types, because Rust doesn't provide them.
/// Any primitive integer type.
pub trait PrimInt:
'static + Send + Sync + std::fmt::Display + PartialEq
{
}
impl PrimInt for u8 {}
impl PrimInt for i8 {}
impl PrimInt for u16 {}
impl PrimInt for i16 {}
impl PrimInt for u32 {}
impl PrimInt for i32 {}
impl PrimInt for u64 {}
impl PrimInt for i64 {}
impl PrimInt for u128 {}
impl PrimInt for i128 {}
// EOF