Skip to content

Commit

Permalink
Removed bsz-shared. filmic_chroma FFI.
Browse files Browse the repository at this point in the history
Filmic chroma now depends on a pure rust libary using no crates for
maximum portability. Its loaded using python's ctypes and operates
directly on the raw bytestring, so it's about as fast as can be.
Currently releases compiled for linux & windows (mingw). Native python
fallback still present for those weirdos that have MacOS but not
Photoshop.
  • Loading branch information
Beinsezii committed Dec 29, 2020
1 parent 8aef4e4 commit 1edbcae
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[flake8]
ignore = E402, C901
ignore = E402, C901, W504
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
__pycache__/
target/
bsz_shared.so
Cargo.lock
filmic_chroma.dll
filmic_chroma.so

# Stored in separate git project as it's a generic Gtk 3 library useful (for me) outside of GIMP
# The plugins all import the bszgw.py in the git root, so all changes apply
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Currently at the "I *think* I understand this now" phase.

Needs the bszgw.py file from https://github.com/Beinsezii/BSZGW at the root. Will be bundled with releases.

Also needs NumPy for filmic-chroma and future pixel math plugins. Usually already installed in Linux, will look into bundling with Windows versions if GIMP doesn't already.

Should work with windows. Can't really test since there's no 2.99 builds yet. Everything's either python standard library or PyGobject, and gimp should bundle those as it's necessary for thier own python scripts.

## Current Plugins
Expand Down
30 changes: 19 additions & 11 deletions bsz-filmic-chroma/bsz-filmic-chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/../')
from bsz_gimp_lib import PlugIn, ParamNumber, ParamBool

import time
try:
from bsz_shared import filmic_chroma as FC
EXTERN = True
except Exception:
import ctypes
from sys import platform
EXTENSIONS = {"win32": ".dll", "linux": ".so"}
FC = ctypes.CDLL(
os.path.dirname(os.path.realpath(__file__)) +
"/filmic_chroma" + EXTENSIONS.get(platform)
).filmic_chroma
FC.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_bool,
ctypes.c_char_p, ctypes.c_uint]
FFI = True
except Exception as e:
print(f"{e}\n\nFailed to load dynamic library,\
falling back to native python implementation")
import struct
EXTERN = False
FFI = False


# Main function.
Expand All @@ -64,14 +73,13 @@ def filmic_chroma(image, drawable, scale, offset, invert):

# many times faster, but needs the shared library. I currently only
# have Linux and Windows devs setup, so the backup will stay for now
if EXTERN:
pixels = bytearray(buff.get(rect, 1.0, "CIE LCH(ab) alpha double",
Gegl.AbyssPolicy.CLAMP))
FC(scale, offset, invert, pixels)
shadow.set(rect, "CIE LCH(ab) alpha double", bytes(pixels))
if FFI:
pixels = buff.get(rect, 1.0, "CIE LCH(ab) alpha double",
Gegl.AbyssPolicy.CLAMP)
FC(scale, offset, False, pixels, len(pixels))
shadow.set(rect, "CIE LCH(ab) alpha double", pixels)

else:

pixels = buff.get(rect, 1.0, "CIE LCH(ab) alpha double",
Gegl.AbyssPolicy.CLAMP)
# creates clusters of pixels for unpack. 32 = 8 bytes
Expand Down
7 changes: 7 additions & 0 deletions bsz-filmic-chroma/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cd "${0%/*}"
rustc --crate-type cdylib -O filmic-chroma.rs
mv libfilmic_chroma.so filmic_chroma.so
strip filmic_chroma.so
rustc --crate-type cdylib -O --target x86_64-pc-windows-gnu filmic-chroma.rs
rm libfilmic_chroma.dll.a
strip filmic_chroma.dll
28 changes: 28 additions & 0 deletions bsz-filmic-chroma/filmic-chroma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::convert::TryInto;
use std::os::raw;

#[no_mangle]
pub extern "C" fn filmic_chroma(
scale: f64,
offset: f64,
invert: bool,
bytes: *mut raw::c_char,
len: usize) {
let pixels = unsafe {
std::slice::from_raw_parts_mut(bytes.cast::<u8>(), len)
};
for chunk in pixels.chunks_exact_mut(32) {
let l = f64::from_le_bytes(chunk[0..8].try_into().expect("bytefail"));
let c = f64::from_le_bytes(chunk[8..16].try_into().expect("bytefail2"));

let c = match invert {
false => c * (offset - l / scale),
true => c * (offset - (100.0 - l) / scale)
};
let c = c.to_le_bytes();

for x in 0..8 {
chunk[x+8] = c[x];
};
}
}
21 changes: 0 additions & 21 deletions bsz-shared/Cargo.toml

This file was deleted.

32 changes: 0 additions & 32 deletions bsz-shared/src/lib.rs

This file was deleted.

10 changes: 4 additions & 6 deletions create_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

printf -v date '%(%Y-%m-%d)T'

cd ./bsz-shared
cargo build --release
cd ..
strip ./bsz_shared.so
bsz-*/build.sh

zip -q bsz-gimp-plugins_${date}.zip \
bsz_gimp_lib.py \
bszgw.py \
bsz_shared.so \
bsz-*/*.py
bsz-*/*.py \
bsz-*/*.dll \
bsz-*/*.so

git tag ${date}

0 comments on commit 1edbcae

Please sign in to comment.