1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 23:21:08 +03:00

clean up draw_image api

This commit is contained in:
Wez Furlong 2019-09-21 07:44:28 -07:00
parent 896586809f
commit 050dde6b4f
5 changed files with 23 additions and 54 deletions

View File

@ -87,7 +87,7 @@ pub trait BitmapImage {
} }
} }
/// Draw a line starting at (start_x, start_y) and ending at (dest_x, dest_y). /// Draw a line starting at `start` and ending at `end`.
/// The line will be anti-aliased and applied to the surface using the /// The line will be anti-aliased and applied to the surface using the
/// specified Operator. /// specified Operator.
fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) {
@ -157,30 +157,22 @@ pub trait BitmapImage {
); );
} }
fn draw_image(&mut self, top_left: Point, im: &dyn BitmapImage, operator: Operator) { fn draw_image(
let (width, height) = im.image_dimensions();
self.draw_image_subset(
top_left,
Rect {
top_left: Point { x: 0, y: 0 },
width,
height,
},
im,
operator,
)
}
fn draw_image_subset(
&mut self, &mut self,
dest_top_left: Point, dest_top_left: Point,
src_rect: Rect, src_rect: Option<Rect>,
im: &dyn BitmapImage, im: &dyn BitmapImage,
operator: Operator, operator: Operator,
) { ) {
let (dest_width, dest_height) = im.image_dimensions(); let (im_width, im_height) = im.image_dimensions();
let src_rect = src_rect.unwrap_or_else(|| Rect {
top_left: Point { x: 0, y: 0 },
width: im_width,
height: im_height,
});
let (dim_width, dim_height) = self.image_dimensions(); let (dim_width, dim_height) = self.image_dimensions();
debug_assert!(src_rect.width <= dest_width && src_rect.height <= dest_height); debug_assert!(src_rect.width <= im_width && src_rect.height <= im_height);
for y in src_rect.top_left.y as usize..src_rect.top_left.y as usize + src_rect.height { for y in src_rect.top_left.y as usize..src_rect.top_left.y as usize + src_rect.height {
let dest_y = y as isize + dest_top_left.y - src_rect.top_left.y as isize; let dest_y = y as isize + dest_top_left.y - src_rect.top_left.y as isize;
if dest_y < 0 { if dest_y < 0 {

View File

@ -85,24 +85,10 @@ pub trait PaintContext {
/// Clear a rectangle to the specified color /// Clear a rectangle to the specified color
fn clear_rect(&mut self, rect: Rect, color: Color); fn clear_rect(&mut self, rect: Rect, color: Color);
fn draw_image(&mut self, top_left: Point, im: &dyn BitmapImage, operator: Operator) { fn draw_image(
let (width, height) = im.image_dimensions();
self.draw_image_subset(
top_left,
Rect {
top_left: Point { x: 0, y: 0 },
width,
height,
},
im,
operator,
)
}
fn draw_image_subset(
&mut self, &mut self,
dest_top_left: Point, dest_top_left: Point,
src_rect: Rect, src_rect: Option<Rect>,
im: &dyn BitmapImage, im: &dyn BitmapImage,
operator: Operator, operator: Operator,
); );

View File

@ -216,15 +216,15 @@ impl<'a> PaintContext for MacGraphicsContext<'a> {
} }
} }
fn draw_image_subset( fn draw_image(
&mut self, &mut self,
dest_top_left: Point, dest_top_left: Point,
src_rect: Rect, src_rect: Option<Rect>,
im: &dyn BitmapImage, im: &dyn BitmapImage,
operator: Operator, operator: Operator,
) { ) {
self.buffer self.buffer
.draw_image_subset(dest_top_left, src_rect, im, operator) .draw_image(dest_top_left, src_rect, im, operator)
} }
fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) {

View File

@ -345,15 +345,15 @@ impl PaintContext for GdiGraphicsContext {
} }
} }
fn draw_image_subset( fn draw_image(
&mut self, &mut self,
dest_top_left: Point, dest_top_left: Point,
src_rect: Rect, src_rect: Option<Rect>,
im: &dyn BitmapImage, im: &dyn BitmapImage,
operator: Operator, operator: Operator,
) { ) {
self.buffer self.buffer
.draw_image_subset(dest_top_left, src_rect, im, operator) .draw_image(dest_top_left, src_rect, im, operator)
} }
fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) {

View File

@ -101,15 +101,15 @@ impl<'a> PaintContext for X11GraphicsContext<'a> {
} }
} }
fn draw_image_subset( fn draw_image(
&mut self, &mut self,
dest_top_left: Point, dest_top_left: Point,
src_rect: Rect, src_rect: Option<Rect>,
im: &dyn BitmapImage, im: &dyn BitmapImage,
operator: Operator, operator: Operator,
) { ) {
self.buffer self.buffer
.draw_image_subset(dest_top_left, src_rect, im, operator) .draw_image(dest_top_left, src_rect, im, operator)
} }
fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) {
@ -184,16 +184,7 @@ impl WindowInner {
} else { } else {
let mut im = Image::new(rect.width as usize, rect.height as usize); let mut im = Image::new(rect.width as usize, rect.height as usize);
im.draw_image_subset( im.draw_image(Point { x: 0, y: 0 }, Some(rect), buffer, Operator::Source);
0,
0,
rect.x as usize,
rect.y as usize,
rect.width as usize,
rect.height as usize,
buffer,
Operator::Source,
);
self.window_context self.window_context
.put_image(rect.x as i16, rect.y as i16, &im); .put_image(rect.x as i16, rect.y as i16, &im);