fvid/fvid/fvid_cython.pyx

39 lines
1.3 KiB
Cython
Raw Normal View History

2020-10-17 17:33:12 +02:00
# distutils: language=c
2020-10-13 14:15:56 +02:00
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
2020-10-17 17:33:12 +02:00
cpdef cy_get_bits_from_image(image):
2020-10-13 14:15:56 +02:00
cdef int width, height, x, y
cdef str pixel_bin_rep
cdef (int, int, int) pixel#, white_diff, black_diff
2020-10-13 14:15:56 +02:00
width, height = image.size
px = image.load()
bits = ""
for y in range(height):
for x in range(width):
pixel = px[x, y]
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"0"
2020-10-13 14:15:56 +02:00
2020-10-17 17:33:12 +02:00
# for exact matches, indexing each pixel individually is faster in cython for some reason
2020-10-13 14:15:56 +02:00
if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"1"
2020-10-13 14:15:56 +02:00
elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0:
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"0"
2020-10-13 14:15:56 +02:00
else:
# if the white difference is smaller (comparison part 1), that means the pixel is closer
2020-10-13 14:15:56 +02:00
# to white, otherwise, the pixel must be black
if abs(pixel[0] - 255) < abs(pixel[0] - 0) and abs(pixel[1] - 255) < abs(pixel[1] - 0) and abs(pixel[2] - 255) < abs(pixel[2] - 0):
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"1"
2020-10-13 14:15:56 +02:00
else:
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"0"
2020-10-13 14:15:56 +02:00
# adding bits
bits += pixel_bin_rep
return bits