Skip to content

Commit

Permalink
refacto(vobsub): remove useless to_image from VobSubIndexedImage
Browse files Browse the repository at this point in the history
replaced by implementing `ToImage` trait for `VobSubToImage`.
`VobSubToImage` use an `VobSubIndexedImage` and a `Palette` as input.
  • Loading branch information
gwen-lg committed Jul 18, 2024
1 parent a0f4417 commit 9607930
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions 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, Rgb, Rgba, RgbaImage};
use image::{ImageBuffer, Luma, Pixel, Rgb, Rgba};
use iter_fixed::IntoIteratorFixed;
use log::trace;
use nom::{
Expand All @@ -13,7 +13,7 @@ use nom::{
};
use thiserror::Error;

use super::{IResultExt, NomError, Palette, VobSubError};
use super::{IResultExt, NomError, VobSubError};
use crate::{
content::{Area, Size},
image::{ImageArea, ImageSize, ToImage, ToOcrImage, ToOcrImageOpt},
Expand Down Expand Up @@ -252,25 +252,6 @@ impl VobSubIndexedImage {
pub fn raw_image(&self) -> &[u8] {
self.raw_image.as_slice()
}

/// Decompress to subtitle to an RBGA image.
/// WIP: replace by more generic
#[must_use]
pub fn to_image(&self, palette: &Palette) -> RgbaImage {
let width = u32::from(self.area.width());
let height = u32::from(self.area.height());
ImageBuffer::from_fn(width, height, |x, y| {
let offset = cast::usize(y * width + x);
// We need to subtract the raw index from 3 to get the same
// results as everybody else. I found this by inspecting the
// Handbrake subtitle decoding routines.
let px = usize::from(3 - self.raw_image[offset]);
let rgb = palette[usize::from(self.palette[px])].0;
let a = self.alpha[px];
let aa = a << 4 | a;
Rgba([rgb[0], rgb[1], rgb[2], aa])
})
}
}

impl fmt::Debug for VobSubIndexedImage {
Expand Down

0 comments on commit 9607930

Please sign in to comment.