Skip to content

Commit

Permalink
refacto(vobsub): use VobSubToImage in vobsub example
Browse files Browse the repository at this point in the history
add function `conv_to_rgba` as helper
  • Loading branch information
gwen-lg committed Jul 18, 2024
1 parent 36916a1 commit 0f4bf6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/vobsub/img.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -301,6 +301,17 @@ impl From<VobSubRleImage<'_>> for VobSubIndexedImage {
}
}

/// convert rbg + alpha to Rgba
#[must_use]
pub fn conv_to_rgba(color: Rgb<u8>, alpha: u8) -> Rgba<u8> {
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>
Expand Down
8 changes: 4 additions & 4 deletions src/vobsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0f4bf6e

Please sign in to comment.