mirror of https://github.com/AlfredoSequeida/fvid
Speedup by 40-45%
This commit is contained in:
parent
db8e45547c
commit
048cf66e99
|
@ -1,12 +1,13 @@
|
||||||
# distutils: language=c++
|
# distutils: language=c
|
||||||
# cython: boundscheck=False
|
# cython: boundscheck=False
|
||||||
# cython: cdivision=True
|
# cython: cdivision=True
|
||||||
# cython: wraparound=False
|
# cython: wraparound=False
|
||||||
|
|
||||||
cpdef str cy_get_bits_from_image(image):
|
cpdef cy_get_bits_from_image(image):
|
||||||
cdef int width, height, x, y
|
cdef int width, height, x, y
|
||||||
cdef str pixel_bin_rep
|
cdef str pixel_bin_rep
|
||||||
cdef tuple white_diff, black_diff, pixel
|
cdef (int, int, int) pixel, white_diff, black_diff
|
||||||
|
cdef (bint, bint, bint) truth_tuple
|
||||||
|
|
||||||
width, height = image.size
|
width, height = image.size
|
||||||
|
|
||||||
|
@ -17,13 +18,13 @@ cpdef str cy_get_bits_from_image(image):
|
||||||
for x in range(width):
|
for x in range(width):
|
||||||
pixel = px[x, y]
|
pixel = px[x, y]
|
||||||
|
|
||||||
pixel_bin_rep = "0"
|
pixel_bin_rep = <str>"0"
|
||||||
|
|
||||||
# for exact matches
|
# for exact matches, indexing each pixel individually is faster in cython for some reason
|
||||||
if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
|
if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
|
||||||
pixel_bin_rep = "1"
|
pixel_bin_rep = <str>"1"
|
||||||
elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0:
|
elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0:
|
||||||
pixel_bin_rep = "0"
|
pixel_bin_rep = <str>"0"
|
||||||
else:
|
else:
|
||||||
white_diff = (abs(pixel[0] - 255), abs(pixel[1] - 255), abs(pixel[2] - 255))
|
white_diff = (abs(pixel[0] - 255), abs(pixel[1] - 255), abs(pixel[2] - 255))
|
||||||
# min_diff = white_diff
|
# min_diff = white_diff
|
||||||
|
@ -31,10 +32,11 @@ cpdef str cy_get_bits_from_image(image):
|
||||||
|
|
||||||
# if the white difference is smaller, that means the pixel is closer
|
# if the white difference is smaller, that means the pixel is closer
|
||||||
# to white, otherwise, the pixel must be black
|
# to white, otherwise, the pixel must be black
|
||||||
if all((white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2])):
|
truth_tuple = (white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2])
|
||||||
pixel_bin_rep = "1"
|
if all(truth_tuple):
|
||||||
|
pixel_bin_rep = <str>"1"
|
||||||
else:
|
else:
|
||||||
pixel_bin_rep = "0"
|
pixel_bin_rep = <str>"0"
|
||||||
|
|
||||||
# adding bits
|
# adding bits
|
||||||
bits += pixel_bin_rep
|
bits += pixel_bin_rep
|
||||||
|
|
Loading…
Reference in New Issue