switch BasicImage to use an associated type for the pixel type

this ends up with less repetition when using BasicImage elsewhere
which is nice
This commit is contained in:
Gered 2023-03-08 17:22:02 -05:00
parent 144d4c395b
commit bcb21bc2ba

View file

@ -19,69 +19,76 @@ where PixelType: PrimInt + Unsigned {
/// Trait that provides "bit-depth-agnostic" access to bitmap drawing operations. This is useful for implementing /// Trait that provides "bit-depth-agnostic" access to bitmap drawing operations. This is useful for implementing
/// drawing functionality that is to be made generic across all supported bitmap types and is not specific to /// drawing functionality that is to be made generic across all supported bitmap types and is not specific to
/// any one pixel-depth. Note that this does not provide cross-bit-depth drawing support. /// any one pixel-depth. Note that this does not provide cross-bit-depth drawing support.
pub trait BasicImage<PixelType> pub trait BasicImage {
where PixelType: PrimInt + Unsigned { type PixelType: PrimInt + Unsigned;
/// Returns the width of the bitmap in pixels. /// Returns the width of the bitmap in pixels.
fn width(&self) -> u32; fn width(&self) -> u32;
/// Returns the height of the bitmap in pixels. /// Returns the height of the bitmap in pixels.
fn height(&self) -> u32; fn height(&self) -> u32;
/// Returns a rect representing the full bitmap boundaries, ignoring the current clipping
/// region set on this bitmap.
fn full_bounds(&self) -> Rect;
/// Returns the bit-depth of this bitmap's pixels. /// Returns the bit-depth of this bitmap's pixels.
fn bpp(&self) -> usize { fn bpp(&self) -> usize {
std::mem::size_of::<PixelType>() * 8 std::mem::size_of::<Self::PixelType>() * 8
} }
/// Fills the entire bitmap with the given color. /// Fills the entire bitmap with the given color.
fn clear(&mut self, color: PixelType); fn clear(&mut self, color: Self::PixelType);
/// Sets the pixel at the given coordinates to the color specified. If the coordinates lie /// Sets the pixel at the given coordinates to the color specified. If the coordinates lie
/// outside of the bitmaps clipping region, no pixels will be changed. /// outside of the bitmaps clipping region, no pixels will be changed.
fn set_pixel(&mut self, x: i32, y: i32, color: PixelType); fn set_pixel(&mut self, x: i32, y: i32, color: Self::PixelType);
/// Gets the pixel at the given coordinates. If the coordinates lie outside of the bitmaps /// Gets the pixel at the given coordinates. If the coordinates lie outside of the bitmaps
/// clipping region, None is returned. /// clipping region, None is returned.
fn get_pixel(&self, x: i32, y: i32) -> Option<PixelType>; fn get_pixel(&self, x: i32, y: i32) -> Option<Self::PixelType>;
/// Draws a line from x1,y1 to x2,y2. /// Draws a line from x1,y1 to x2,y2.
fn line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: PixelType); fn line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: Self::PixelType);
/// Draws a horizontal line from x1,y to x2,y. /// Draws a horizontal line from x1,y to x2,y.
fn horiz_line(&mut self, x1: i32, x2: i32, y: i32, color: PixelType); fn horiz_line(&mut self, x1: i32, x2: i32, y: i32, color: Self::PixelType);
/// Draws a vertical line from x,y1 to x,y2. /// Draws a vertical line from x,y1 to x,y2.
fn vert_line(&mut self, x: i32, y1: i32, y2: i32, color: PixelType); fn vert_line(&mut self, x: i32, y1: i32, y2: i32, color: Self::PixelType);
/// Draws an empty box (rectangle) using the points x1,y1 and x2,y2 to form the box to be /// Draws an empty box (rectangle) using the points x1,y1 and x2,y2 to form the box to be
/// drawn, assuming they are specifying the top-left and bottom-right corners respectively. /// drawn, assuming they are specifying the top-left and bottom-right corners respectively.
fn rect(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: PixelType); fn rect(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: Self::PixelType);
/// Draws a filled box (rectangle) using the points x1,y1 and x2,y2 to form the box to be /// Draws a filled box (rectangle) using the points x1,y1 and x2,y2 to form the box to be
/// drawn, assuming they are specifying the top-left and bottom-right corners respectively. /// drawn, assuming they are specifying the top-left and bottom-right corners respectively.
fn filled_rect(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: PixelType); fn filled_rect(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: Self::PixelType);
/// Draws the outline of a circle formed by the center point and radius given. /// Draws the outline of a circle formed by the center point and radius given.
fn circle(&mut self, center_x: i32, center_y: i32, radius: u32, color: PixelType); fn circle(&mut self, center_x: i32, center_y: i32, radius: u32, color: Self::PixelType);
/// Draws a filled circle formed by the center point and radius given. /// Draws a filled circle formed by the center point and radius given.
fn filled_circle(&mut self, center_x: i32, center_y: i32, radius: u32, color: PixelType); fn filled_circle(&mut self, center_x: i32, center_y: i32, radius: u32, color: Self::PixelType);
fn blit_region( fn blit_region(
&mut self, &mut self,
method: BasicBlitMethod<PixelType>, method: BasicBlitMethod<Self::PixelType>,
src: &Self, src: &Self,
src_region: &Rect, src_region: &Rect,
dest_x: i32, dest_x: i32,
dest_y: i32 dest_y: i32
); );
fn blit(&mut self, method: BasicBlitMethod<PixelType>, src: &Self, x: i32, y: i32) { fn blit(&mut self, method: BasicBlitMethod<Self::PixelType>, src: &Self, x: i32, y: i32) {
let src_region = Rect::new(0, 0, src.width(), src.height()); let src_region = Rect::new(0, 0, src.width(), src.height());
self.blit_region(method, src, &src_region, x, y); self.blit_region(method, src, &src_region, x, y);
} }
} }
impl BasicImage<u8> for indexed::Bitmap { impl BasicImage for indexed::Bitmap {
type PixelType = u8;
#[inline] #[inline]
fn width(&self) -> u32 { fn width(&self) -> u32 {
self.width() self.width()