better names

png-branch
an 2019-02-11 08:44:20 -05:00
parent 9e8c6eea92
commit 5856d2c1c9
2 changed files with 18 additions and 8 deletions

View File

@ -30,13 +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: Image>(out: &mut impl io::Write, im: &T) -> ResultS<()>
pub fn write_ppm(out: &mut impl io::Write, im: &impl Image) -> ResultS<()>
{
write!(out, "P3\n{} {}\n{}\n", im.w(), im.h(), u16::max_value())?;
for y in 0..im.h() {
for x in 0..im.w() {
let cr = im.cr_at(x, y);
let cr = im.index(x, y);
write!(out, "{} {} {} ", cr.r(), cr.g(), cr.b())?;
}
@ -52,12 +52,12 @@ pub trait Image
fn w(&self) -> usize;
fn h(&self) -> usize;
fn cr_at(&self, x: usize, y: usize) -> &Self::Output;
fn index(&self, x: usize, y: usize) -> &Self::Output;
fn cr_get(&self, x: usize, y: usize) -> Option<&Self::Output>
fn get(&self, x: usize, y: usize) -> Option<&Self::Output>
{
if x < self.w() && y < self.h() {
Some(self.cr_at(x, y))
Some(&self.index(x, y))
} else {
None
}
@ -72,6 +72,16 @@ pub trait Color
fn a(&self) -> u16;
}
impl<T: Color> std::ops::Index<(usize, usize)> for Image<Output = T>
{
type Output = T;
fn index(&self, (x, y): (usize, usize)) -> &Self::Output
{
<Self as Image>::index(self, x, y)
}
}
impl Image16
{
/// Creates a new Image16.
@ -88,7 +98,7 @@ impl Image for Image16
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> &Self::Output
fn index(&self, x: usize, y: usize) -> &Self::Output
{
&self.cr[x + y * self.w]
}
@ -110,7 +120,7 @@ impl Image for Image8
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> &Self::Output
fn index(&self, x: usize, y: usize) -> &Self::Output
{
&self.cr[x + y * self.w]
}

View File

@ -262,7 +262,7 @@ impl Image for ImageShp<'_>
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> &Self::Output
fn index(&self, x: usize, y: usize) -> &Self::Output
{
static TRANSLUCENT_COLOR: ColorShp = ColorShp::Translucent;