From 0f4bf6e34f81919bcfb27bc8c7125b7e4fc4262b Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Wed, 17 Jul 2024 23:58:08 +0200 Subject: [PATCH] refacto(vobsub): use `VobSubToImage` in vobsub example add function `conv_to_rgba` as helper --- src/vobsub/img.rs | 13 ++++++++++++- src/vobsub/mod.rs | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/vobsub/img.rs b/src/vobsub/img.rs index a95d5e5..3954722 100644 --- a/src/vobsub/img.rs +++ b/src/vobsub/img.rs @@ -1,7 +1,7 @@ //! Run-length encoded image format for subtitles. use core::fmt::{self, Debug}; -use image::{ImageBuffer, Luma, Pixel, Rgba, RgbaImage}; +use image::{ImageBuffer, Luma, Pixel, Rgb, Rgba, RgbaImage}; use iter_fixed::IntoIteratorFixed; use log::trace; use nom::{ @@ -301,6 +301,17 @@ impl From> for VobSubIndexedImage { } } +/// convert rbg + alpha to Rgba +#[must_use] +pub fn conv_to_rgba(color: Rgb, alpha: u8) -> Rgba { + Rgba([ + color.channels()[0], + color.channels()[1], + color.channels()[2], + alpha, + ]) +} + /// This struct implement [`ToImage`] to generate an `ImageBuffer` from /// a [`VobSubIndexedImage`], a palette and a pixel conversion function. pub struct VobSubToImage<'a, I, P> diff --git a/src/vobsub/mod.rs b/src/vobsub/mod.rs index 611b87f..3a32b4b 100644 --- a/src/vobsub/mod.rs +++ b/src/vobsub/mod.rs @@ -9,9 +9,9 @@ //! extern crate subtile; //! //! use crate::subtile::{ -//! image::{ImageSize, ImageArea}, +//! image::{ImageSize, ImageArea, ToImage}, //! time::TimeSpan, -//! vobsub::VobSubIndexedImage +//! vobsub::{conv_to_rgba, VobSubIndexedImage, VobSubToImage}, //! }; //! //! let idx = subtile::vobsub::Index::open("./fixtures/example.idx").unwrap(); @@ -22,7 +22,7 @@ //! let area = image.area(); //! println!("At: {}, {}", area.left(), area.top()); //! println!("Size: {}x{}", image.width(), image.height()); -//! let img: image::RgbaImage = image.to_image(idx.palette()); +//! let img: image::RgbaImage = VobSubToImage::new(&image, idx.palette(), conv_to_rgba).to_image(); //! //! // You can save or manipulate `img` using the APIs provided by the Rust //! // `image` crate. @@ -76,7 +76,7 @@ mod sub; pub use self::{ idx::{read_palette, Index}, - img::{VobSubIndexedImage, VobSubOcrImage, VobSubToImage}, + img::{conv_to_rgba, VobSubIndexedImage, VobSubOcrImage, VobSubToImage}, palette::{palette, Palette}, probe::{is_idx_file, is_sub_file}, sub::ErrorMissing,