Skip to content

Instantly share code, notes, and snippets.

@kanryu
Last active December 27, 2017 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanryu/d176b9480e2e15b2b68a16814588a6ba to your computer and use it in GitHub Desktop.
Save kanryu/d176b9480e2e15b2b68a16814588a6ba to your computer and use it in GitHub Desktop.
a sample for image processing with look-up-table(after memorized)
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
@kanryu
Copy link
Author

kanryu commented Dec 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment