fix various errors in durandal::image
parent
799223f499
commit
824ff5cdd2
|
@ -1,6 +1,6 @@
|
|||
//! Image and color representations.
|
||||
|
||||
use crate::durandal::traits::*;
|
||||
use crate::durandal::{err::*, traits::*};
|
||||
use std::io;
|
||||
|
||||
/// Creates a RGB8 color from a R5G5B5 format color.
|
||||
|
@ -20,7 +20,7 @@ pub fn r5g5b5_to_rgb16(rgb: u16) -> Color16
|
|||
let r = rgb >> 10 & 0x1f;
|
||||
let g = rgb >> 5 & 0x1f;
|
||||
let b = rgb & 0x1f;
|
||||
Color16::new(r << 3 | r >> 2, g << 11, b << 11)
|
||||
Color16::new(r << 11, g << 11, b << 11)
|
||||
}
|
||||
|
||||
/// Creates a RGB16 color from a RGB8 format color.
|
||||
|
@ -30,14 +30,13 @@ pub fn rgb8_to_rgb16(r: u8, g: u8, b: u8) -> Color16
|
|||
}
|
||||
|
||||
/// Writes a PPM file from an image.
|
||||
pub fn write_ppm<T>(out: &mut impl io::Write, im: &T) -> io::Result<()>
|
||||
where T: Image
|
||||
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)?;
|
||||
|
||||
for y in 0..im.h() {
|
||||
for x in 0..im.w() {
|
||||
let cr = im.cr_at(x, y);
|
||||
let cr = im.cr_at(x, y).ok_or_else(|| err_msg("no data in image"))?;
|
||||
write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?;
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +50,7 @@ pub trait Image
|
|||
|
||||
fn w(&self) -> usize;
|
||||
fn h(&self) -> usize;
|
||||
fn cr_at(&self, x: usize, y: usize) -> &Self::Output;
|
||||
fn cr_at(&self, x: usize, y: usize) -> Option<&Self::Output>;
|
||||
}
|
||||
|
||||
pub trait Color
|
||||
|
@ -78,19 +77,12 @@ impl Image for Image16
|
|||
{
|
||||
type Output = Color16;
|
||||
|
||||
fn w(&self) -> usize
|
||||
{
|
||||
self.w
|
||||
}
|
||||
fn w(&self) -> usize {self.w}
|
||||
fn h(&self) -> usize {self.h}
|
||||
|
||||
fn h(&self) -> usize
|
||||
fn cr_at(&self, x: usize, y: usize) -> Option<&Color16>
|
||||
{
|
||||
self.h
|
||||
}
|
||||
|
||||
fn cr_at(&self, x: usize, y: usize) -> &Color16
|
||||
{
|
||||
&self.cr[x + y * self.w]
|
||||
self.cr.get(x + y * self.w)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,19 +99,12 @@ impl Image for Image8
|
|||
{
|
||||
type Output = Color8;
|
||||
|
||||
fn w(&self) -> usize
|
||||
{
|
||||
self.w
|
||||
}
|
||||
fn w(&self) -> usize {self.w}
|
||||
fn h(&self) -> usize {self.h}
|
||||
|
||||
fn h(&self) -> usize
|
||||
fn cr_at(&self, x: usize, y: usize) -> Option<&Color8>
|
||||
{
|
||||
self.h
|
||||
}
|
||||
|
||||
fn cr_at(&self, x: usize, y: usize) -> &Color8
|
||||
{
|
||||
&self.cr[x + y * self.w]
|
||||
self.cr.get(x + y * self.w)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,7 +112,7 @@ impl Color16
|
|||
{
|
||||
pub const fn new(r: u16, g: u16, b: u16) -> Color16
|
||||
{
|
||||
Color16(r, g, b, u16::max_value())
|
||||
Color16(r, g, b)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,32 +121,17 @@ impl Color for Color16
|
|||
type Output = u16;
|
||||
const MAX: usize = u16::max_value() as usize;
|
||||
|
||||
fn r(&self) -> u16
|
||||
{
|
||||
self.0
|
||||
}
|
||||
|
||||
fn g(&self) -> u16
|
||||
{
|
||||
self.1
|
||||
}
|
||||
|
||||
fn b(&self) -> u16
|
||||
{
|
||||
self.2
|
||||
}
|
||||
|
||||
fn a(&self) -> u16
|
||||
{
|
||||
self.3
|
||||
}
|
||||
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
|
||||
{
|
||||
pub const fn new(r: u8, g: u8, b: u8) -> Color8
|
||||
{
|
||||
Color8(r, g, b, u8::max_value())
|
||||
Color8(r, g, b)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,34 +140,19 @@ impl Color for Color8
|
|||
type Output = u8;
|
||||
const MAX: usize = u8::max_value() as usize;
|
||||
|
||||
fn r(&self) -> u8
|
||||
{
|
||||
self.0
|
||||
}
|
||||
|
||||
fn g(&self) -> u8
|
||||
{
|
||||
self.1
|
||||
}
|
||||
|
||||
fn b(&self) -> u8
|
||||
{
|
||||
self.2
|
||||
}
|
||||
|
||||
fn a(&self) -> u8
|
||||
{
|
||||
self.3
|
||||
}
|
||||
fn r(&self) -> u8 {self.0}
|
||||
fn g(&self) -> u8 {self.1}
|
||||
fn b(&self) -> u8 {self.2}
|
||||
fn a(&self) -> u8 {u8::max_value()}
|
||||
}
|
||||
|
||||
/// A RGB16 color.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Color16(u16, u16, u16, u16);
|
||||
pub struct Color16(u16, u16, u16);
|
||||
|
||||
/// A RGB8 color.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Color8(u8, u8, u8, u8);
|
||||
pub struct Color8(u8, u8, u8);
|
||||
|
||||
/// RGB16 image.
|
||||
pub struct Image16
|
||||
|
|
Loading…
Reference in New Issue