From 5856d2c1c976bc4441268770f30efb8c1a115840 Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 11 Feb 2019 08:44:20 -0500 Subject: [PATCH] better names --- src/durandal/image.rs | 24 +++++++++++++++++------- src/marathon/shp.rs | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/durandal/image.rs b/src/durandal/image.rs index e43b1ec..7ee6747 100644 --- a/src/durandal/image.rs +++ b/src/durandal/image.rs @@ -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(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 std::ops::Index<(usize, usize)> for Image +{ + type Output = T; + + fn index(&self, (x, y): (usize, usize)) -> &Self::Output + { + ::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] } diff --git a/src/marathon/shp.rs b/src/marathon/shp.rs index bc01809..b969de1 100644 --- a/src/marathon/shp.rs +++ b/src/marathon/shp.rs @@ -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;