Skip to content

Commit 3f44115

Browse files
authored
Fix Hardsubx OCR (#1741)
* fix: hardsubx segmentation fault * fix: hardsubx garbage output * chore: enable hardsubx on test builds
1 parent f09b6ff commit 3f44115

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

.github/workflows/build_linux.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ jobs:
2626
runs-on: ubuntu-latest
2727
steps:
2828
- name: Install dependencies
29-
run: sudo apt update && sudo apt-get install libgpac-dev libtesseract-dev
29+
run: sudo apt update && sudo apt-get install libgpac-dev libtesseract-dev libavcodec-dev libavdevice-dev libx11-dev libxcb1-dev libxcb-shm0-dev
3030
- uses: actions/checkout@v4
3131
- name: build
32-
run: ./build
32+
run: ./build -hardsubx
3333
working-directory: ./linux
3434
- name: Display version information
3535
run: ./ccextractor --version

docs/CHANGES.TXT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1.0 (to be released)
22
-----------------
3+
- Fix: HardSubX OCR on Rust
34
- Removed the Share Module
45
- Fix: Regression failures on DVD files
56
- Fix: Segmentation faults on MP4 files with CEA-708 captions

src/rust/src/hardsubx/decoder.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,25 @@ pub unsafe fn dispatch_classifier_functions(ctx: *mut lib_hardsubx_ctx, im: *mut
4040
// function that calls the classifier functions
4141
match (*ctx).ocr_mode {
4242
0 => {
43-
let ret_char_arr = get_ocr_text_wordwise_threshold(ctx, im, (*ctx).conf_thresh);
44-
ffi::CStr::from_ptr(ret_char_arr)
45-
.to_string_lossy()
46-
.into_owned()
47-
}
48-
1 => {
49-
let ret_char_arr = get_ocr_text_letterwise_threshold(ctx, im, (*ctx).conf_thresh);
43+
let ret_char_arr = get_ocr_text_simple_threshold(ctx, im, (*ctx).conf_thresh);
5044
let text_out_result = ffi::CString::from_raw(ret_char_arr).into_string();
5145
match text_out_result {
5246
Ok(T) => T,
5347
Err(_E) => "".to_string(),
5448
}
5549
}
56-
50+
1 => {
51+
let ret_char_arr = get_ocr_text_wordwise_threshold(ctx, im, (*ctx).conf_thresh);
52+
if ret_char_arr.is_null() {
53+
"".to_string()
54+
} else {
55+
ffi::CStr::from_ptr(ret_char_arr)
56+
.to_string_lossy()
57+
.into_owned()
58+
}
59+
}
5760
2 => {
58-
let ret_char_arr = get_ocr_text_simple_threshold(ctx, im, (*ctx).conf_thresh);
61+
let ret_char_arr = get_ocr_text_letterwise_threshold(ctx, im, (*ctx).conf_thresh);
5962
let text_out_result = ffi::CString::from_raw(ret_char_arr).into_string();
6063
match text_out_result {
6164
Ok(T) => T,
@@ -66,7 +69,6 @@ pub unsafe fn dispatch_classifier_functions(ctx: *mut lib_hardsubx_ctx, im: *mut
6669
_ => {
6770
eprintln!("Invalid OCR Mode");
6871
exit(EXIT_MALFORMED_PARAMETER);
69-
// "".to_string()
7072
}
7173
}
7274
}
@@ -113,7 +115,7 @@ pub unsafe extern "C" fn _process_frame_white_basic(
113115
let mut gray_im: *mut Pix = pixConvertRGBToGray(im, 0.0, 0.0, 0.0);
114116
let mut sobel_edge_im: *mut Pix =
115117
pixSobelEdgeFilter(gray_im, L_VERTICAL_EDGES.try_into().unwrap());
116-
let mut dilate_gray_im: *mut Pix = pixDilateGray(sobel_edge_im, 21, 1);
118+
let mut dilate_gray_im: *mut Pix = pixDilateGray(sobel_edge_im, 21, 11);
117119
let mut edge_im: *mut Pix = pixThresholdToBinary(dilate_gray_im, 50);
118120

119121
let mut feat_im: *mut Pix = pixCreate(width, height, 32);

src/rust/src/hardsubx/imgops.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V
1313

1414
#[no_mangle]
1515
pub extern "C" fn rgb_to_lab(R: f32, G: f32, B: f32, L: &mut f32, a: &mut f32, b: &mut f32) {
16-
let rgb = Srgb::new(R, G, B);
16+
// Normalize input RGB from 0-255 to 0.0-1.0
17+
let rgb = Srgb::new(R / 255.0, G / 255.0, B / 255.0);
1718

18-
// This declaration sets the white-point as per the D65 standard
19-
let lab_rep = Lab::<palette::white_point::D65, f32>::from_color(rgb);
19+
// Convert from sRGB to Lab (D65 white point is default)
20+
let lab_rep = Lab::from_color(rgb);
2021

2122
*L = lab_rep.l;
2223
*a = lab_rep.a;

0 commit comments

Comments
 (0)