Last active
December 27, 2017 11:15
-
-
Save kanryu/d176b9480e2e15b2b68a16814588a6ba to your computer and use it in GitHub Desktop.
a sample for image processing with look-up-table(after memorized)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_luminorImpl(input, b_sigma, c_sigma, g_sigma, with_alpha): | |
""" | |
input: a Buffer as an bitmap image on memory | |
b_sigma: float value for brightness(default: 0.0, range: -255 to 255) | |
c_sigma: float value for contrast(default: 1.0, range: 0.1 to 10.0) | |
g_sigma: float value for gamma value(default: 1.0, range: 0.1 to 10.0) | |
""" | |
assert type(input) == ImageParam | |
assert input.dimensions() == 3 | |
x, y, c = Var("x"), Var("y"), Var("c") | |
v = Var("v") | |
px = cast(float_t, v) | |
px = px * (1.0/255.0) | |
# change brightness | |
px = px + b_sigma * (1.0/255.0) | |
# change contrast | |
px = (px-0.5)*c_sigma+0.5 | |
# change gamma value | |
px = pow(px, 1.0/g_sigma) | |
px = max(px, 0.0) | |
px = min(px, 1.0) | |
px = cast(uint8_t, px*255) | |
luminor_table = Func('luminor_table') | |
luminor_table[v] = px | |
luminor_table.compute_root().vectorize(v, 16) | |
luminor = Func('luminor') | |
luminor.store_root().compute_at(luminor_table, v) | |
luminor[x, y, c] = luminor_table[input[x, y, c]] | |
# for RGBA pixels, Alpha is not modified | |
if with_alpha: | |
luminor[x, y, 3] = input[x, y, 3] | |
luminor.compute_root().parallel(y).vectorize(x, 16) | |
return luminor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
before memorized, to see https://gist.github.com/kanryu/f66dd3371c220367ffdf9c964bcc8141