impl Drawable for slice (#99)

This commit is contained in:
Collide
2025-05-21 15:39:00 +08:00
committed by GitHub
parent cf21eabcaa
commit 9826fac208
8 changed files with 50 additions and 8 deletions

View File

@ -31,7 +31,10 @@ impl Default for Annotator {
} }
impl Annotator { impl Annotator {
pub fn annotate<T: Drawable>(&self, image: &Image, drawable: &T) -> Result<Image> { pub fn annotate<T>(&self, image: &Image, drawable: &T) -> Result<Image>
where
T: Drawable + ?Sized,
{
let ctx = DrawContext { let ctx = DrawContext {
text_renderer: &self.text_renderer, text_renderer: &self.text_renderer,
prob_style: self.prob_style.as_ref(), prob_style: self.prob_style.as_ref(),

View File

@ -4,7 +4,7 @@ use image::{Rgba, RgbaImage};
use crate::{DrawContext, Hbb, Style, TextLoc}; use crate::{DrawContext, Hbb, Style, TextLoc};
impl Drawable for Vec<Hbb> { impl Drawable for [Hbb] {
fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> { fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> {
self.iter().try_for_each(|x| x.draw(ctx, canvas)) self.iter().try_for_each(|x| x.draw(ctx, canvas))
} }

View File

@ -91,7 +91,7 @@ impl Drawable for Keypoint {
} }
} }
impl Drawable for Vec<Keypoint> { impl Drawable for [Keypoint] {
fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> { fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> {
let nk = self.len(); let nk = self.len();
if nk > 0 { if nk > 0 {
@ -137,7 +137,7 @@ impl Drawable for Vec<Keypoint> {
} }
} }
impl Drawable for Vec<Vec<Keypoint>> { impl Drawable for [Vec<Keypoint>] {
fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> { fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> {
self.iter().try_for_each(|x| x.draw(ctx, canvas)) self.iter().try_for_each(|x| x.draw(ctx, canvas))
} }

View File

@ -70,7 +70,7 @@ fn draw_masks(
Ok(()) Ok(())
} }
impl Drawable for Vec<Mask> { impl Drawable for [Mask] {
fn get_global_style<'a>(&self, ctx: &'a DrawContext) -> Option<&'a Style> { fn get_global_style<'a>(&self, ctx: &'a DrawContext) -> Option<&'a Style> {
ctx.mask_style ctx.mask_style
} }

View File

@ -86,6 +86,45 @@ impl Drawable for Y {
} }
} }
impl<T> Drawable for Vec<T>
where
[T]: Drawable,
{
fn get_local_style(&self) -> Option<&Style> {
self.as_slice().get_local_style()
}
fn get_global_style<'a>(&self, ctx: &'a DrawContext) -> Option<&'a Style> {
self.as_slice().get_global_style(ctx)
}
fn get_id(&self) -> Option<usize> {
self.as_slice().get_id()
}
fn draw_shapes_with_style(
&self,
ctx: &DrawContext,
canvas: &mut image::RgbaImage,
style: &Style,
) -> anyhow::Result<()> {
self.as_slice().draw_shapes_with_style(ctx, canvas, style)
}
fn draw_texts_with_style(
&self,
ctx: &DrawContext,
canvas: &mut image::RgbaImage,
style: &Style,
) -> anyhow::Result<()> {
self.as_slice().draw_texts_with_style(ctx, canvas, style)
}
fn draw(&self, ctx: &DrawContext, canvas: &mut image::RgbaImage) -> anyhow::Result<()> {
self.as_slice().draw(ctx, canvas)
}
}
// impl<T: Drawable> Drawable for Vec<T> { // impl<T: Drawable> Drawable for Vec<T> {
// fn draw_shapes(&self, ctx: &DrawContext, canvas: &mut image::RgbaImage) -> anyhow::Result<()> { // fn draw_shapes(&self, ctx: &DrawContext, canvas: &mut image::RgbaImage) -> anyhow::Result<()> {
// self.iter().try_for_each(|x| x.draw_shapes(ctx, canvas)) // self.iter().try_for_each(|x| x.draw_shapes(ctx, canvas))

View File

@ -4,7 +4,7 @@ use image::{Rgba, RgbaImage};
use crate::{DrawContext, Obb, Style, TextLoc}; use crate::{DrawContext, Obb, Style, TextLoc};
impl Drawable for Vec<Obb> { impl Drawable for [Obb] {
fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> { fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> {
self.iter().try_for_each(|x| x.draw(ctx, canvas)) self.iter().try_for_each(|x| x.draw(ctx, canvas))
} }

View File

@ -4,7 +4,7 @@ use image::{Rgba, RgbaImage};
use crate::{DrawContext, Polygon, Style, TextLoc}; use crate::{DrawContext, Polygon, Style, TextLoc};
impl Drawable for Vec<Polygon> { impl Drawable for [Polygon] {
fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> { fn draw(&self, ctx: &DrawContext, canvas: &mut RgbaImage) -> Result<()> {
self.iter().try_for_each(|x| x.draw(ctx, canvas)) self.iter().try_for_each(|x| x.draw(ctx, canvas))
} }

View File

@ -4,7 +4,7 @@ use image::RgbaImage;
use crate::{DrawContext, Prob, Style, TextLoc}; use crate::{DrawContext, Prob, Style, TextLoc};
impl Drawable for Vec<Prob> { impl Drawable for [Prob] {
fn draw_texts_with_style( fn draw_texts_with_style(
&self, &self,
ctx: &DrawContext, ctx: &DrawContext,