Skip to content

Commit

Permalink
Port hardsubx imgops (CCExtractor#1439)
Browse files Browse the repository at this point in the history
* add hardsubx rust module and expose it

* port rgb_to_hsv to rust

* add dependency fast-math and extern it

* port rgb_to_lab to rust

also make preprocessor to not allow compilation of hardsubx_imgops
if WITHOUT_RUST is OFF

* improve if-else constructs for readability

* unroll  macros that were only used once and remove their definition

* Improve readability of rgb_to_lab function (and fixes)

The function in Rust behaves slightly differently than its C counterpart

* remove fast math library, use palette library and rewrite imgops using it

* run formatter

* replace destructuring assignment statement with normal assignment statements because of build rust compiler issues

* run formatter on C code for imgops

* remove extern for modules because it is not required

* improve comment placement in rust imgops

Co-authored-by: Punit Lodha <[email protected]>
  • Loading branch information
shashwat1002 and PunitLodha authored Jun 15, 2022
1 parent 689d92a commit 0f90afa
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/lib_ccx/hardsubx_imgops.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "lib_ccx.h"
#include "utility.h"

#ifdef ENABLE_HARDSUBX
//TODO: Correct FFMpeg integration
#if defined(ENABLE_HARDSUBX) && defined(DISABLE_RUST)
// TODO: Correct FFMpeg integration
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
Expand All @@ -18,7 +18,7 @@

void rgb_to_hsv(float R, float G, float B, float *H, float *S, float *V)
{
//Conversion into HSV color space to get Hue
// Conversion into HSV color space to get Hue
float r = R / 255.0f;
float g = G / 255.0f;
float b = B / 255.0f;
Expand Down Expand Up @@ -68,7 +68,7 @@ void rgb_to_hsv(float R, float G, float B, float *H, float *S, float *V)

void rgb_to_lab(float R, float G, float B, float *L, float *a, float *b)
{
//Conversion to the CIE-LAB color space to get the Luminance
// Conversion to the CIE-LAB color space to get the Luminance
float X, Y, Z, fX, fY, fZ;

X = 0.412453 * R + 0.357580 * G + 0.180423 * B;
Expand Down
199 changes: 197 additions & 2 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ crate-type = ["staticlib"]
log = "0.4.0"
env_logger = "0.8.4"
iconv = "0.1.1"
palette = "0.6.0"

[build-dependencies]
bindgen = "0.58.1"
24 changes: 24 additions & 0 deletions src/rust/src/hardsubx/imgops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use palette::{FromColor, Hsv, Lab, Srgb};

#[no_mangle]
pub extern "C" fn rgb_to_hsv(R: f32, G: f32, B: f32, H: &mut f32, S: &mut f32, V: &mut f32) {
let rgb = Srgb::new(R, G, B);

let hsv_rep = Hsv::from_color(rgb);

*H = hsv_rep.hue.to_positive_degrees();
*S = hsv_rep.saturation;
*V = hsv_rep.value;
}

#[no_mangle]
pub extern "C" fn rgb_to_lab(R: f32, G: f32, B: f32, L: &mut f32, a: &mut f32, b: &mut f32) {
let rgb = Srgb::new(R, G, B);

// This declaration sets the white-point as per the D65 standard
let lab_rep = Lab::<palette::white_point::D65, f32>::from_color(rgb);

*L = lab_rep.l;
*a = lab_rep.a;
*b = lab_rep.b;
}
1 change: 1 addition & 0 deletions src/rust/src/hardsubx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod imgops;
1 change: 1 addition & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
pub mod decoder;
pub mod hardsubx;
pub mod utils;

#[cfg(windows)]
Expand Down

0 comments on commit 0f90afa

Please sign in to comment.