Skip to content

Commit

Permalink
refactor: inline code of process_image_for_ocr
Browse files Browse the repository at this point in the history
create a simple function to create ToOcrImage options for crate Opt
  • Loading branch information
gwen-lg committed Aug 6, 2024
1 parent 582a82b commit 002de58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ mod ocr;
mod opt;
mod preprocessor;

pub use crate::{ocr::OcrOpt, opt::Opt, preprocessor::process_images_for_ocr};
pub use crate::{ocr::OcrOpt, opt::Opt};

use log::warn;
use rayon::ThreadPoolBuildError;
use preprocessor::rgb_palette_to_luminance;
use rayon::{
iter::{IntoParallelRefIterator, ParallelIterator},
ThreadPoolBuildError,
};
use std::{
fs::File,
io::{self, BufWriter},
path::PathBuf,
};
use subtile::{
image::dump_images,
image::{dump_images, ToOcrImage, ToOcrImageOpt},
srt,
time::TimeSpan,
vobsub::{self, VobSubError, VobSubIndexedImage},
vobsub::{self, VobSubError, VobSubIndexedImage, VobSubOcrImage},
SubtileError,
};
use thiserror::Error;
Expand Down Expand Up @@ -84,7 +88,19 @@ pub fn run(opt: &Opt) -> Result<(), Error> {
.unzip()
};

let images_for_ocr = preprocessor::process_images_for_ocr(idx, images, opt.border);
let images_for_ocr = {
profiling::scope!("Convert images for OCR");

let ocr_opt = ocr_opt(opt);
let palette = rgb_palette_to_luminance(idx.palette());
images
.par_iter()
.map(|vobsub_img| {
let converter = VobSubOcrImage::new(vobsub_img, &palette);
converter.image(&ocr_opt)
})
.collect::<Vec<_>>()
};

// Dump images if requested.
if opt.dump {
Expand All @@ -101,6 +117,14 @@ pub fn run(opt: &Opt) -> Result<(), Error> {
Ok(())
}

/// Create [`ToOcrImageOpt`] from [`Opt`]
fn ocr_opt(opt: &Opt) -> ToOcrImageOpt {
ToOcrImageOpt {
border: opt.border,
..Default::default()
}
}

/// Log errors and remove bad results.
///
/// # Errors
Expand Down
30 changes: 3 additions & 27 deletions src/preprocessor.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
use image::GrayImage;
use rayon::prelude::*;
use subtile::{
image::{ToOcrImage, ToOcrImageOpt},
vobsub::{self, VobSubIndexedImage, VobSubOcrImage},
};

/// Return a vector of processed images for OCR.
#[profiling::function]
pub fn process_images_for_ocr<I>(idx: vobsub::Index, images: I, border: u32) -> Vec<GrayImage>
where
I: IntoParallelIterator<Item = VobSubIndexedImage>,
{
let opt = ToOcrImageOpt {
border,
..Default::default()
};
let palette = rgb_palette_to_luminance(idx.palette());
images
.into_par_iter()
.map(|vobsub_img| {
let converter = VobSubOcrImage::new(&vobsub_img, &palette);
converter.image(&opt)
})
.collect()
}
use subtile::vobsub;

/// Convert an sRGB palette to a luminance palette.
fn rgb_palette_to_luminance(palette: &vobsub::Palette) -> [f32; 16] {
#[must_use]
pub fn rgb_palette_to_luminance(palette: &vobsub::Palette) -> [f32; 16] {
palette.map(|x| {
let r = srgb_to_linear(x[0]);
let g = srgb_to_linear(x[1]);
Expand Down

0 comments on commit 002de58

Please sign in to comment.