punyverse/punyverse/_glgeom.pyx

40 lines
1.4 KiB
Cython
Raw Normal View History

2013-10-22 20:38:37 -04:00
from libc.string cimport memcpy
include "_cyopengl.pxi"
cdef float PI = 3.1415926535897932324626
cdef float TWOPI = PI * 2
cdef extern from "Python.h":
object PyBytes_FromStringAndSize(const char *s, Py_ssize_t len)
const char* PyBytes_AsString(bytes o)
2018-08-22 16:22:08 -04:00
cpdef bytes bgr_to_rgb(bytes buffer, int width, int height, bint alpha=0):
2013-10-22 20:38:37 -04:00
cdef int length = len(buffer)
cdef int depth = length / (width * height)
cdef int depth2 = depth - alpha
cdef object final = PyBytes_FromStringAndSize(NULL, length)
cdef char *result = PyBytes_AsString(final)
2013-10-22 20:38:37 -04:00
cdef const char *source = PyBytes_AsString(buffer)
2018-08-22 16:22:08 -04:00
cdef int x, y, offset, i, row = width * depth
2013-10-22 20:38:37 -04:00
for y in xrange(height):
for x in xrange(width):
2018-08-22 16:22:08 -04:00
offset = y * row + x * depth
2013-10-22 20:38:37 -04:00
for i in xrange(depth2):
2018-08-22 16:22:08 -04:00
result[offset+i] = source[offset+depth2-i-1]
2013-10-22 20:38:37 -04:00
if alpha:
2018-08-22 16:22:08 -04:00
result[offset+depth2] = source[offset+depth2]
return final
cpdef bytes flip_vertical(bytes buffer, int width, int height):
cdef int length = len(buffer)
cdef object final = PyBytes_FromStringAndSize(NULL, length)
cdef char *result = PyBytes_AsString(final)
cdef const char *source = PyBytes_AsString(buffer)
cdef int y1, y2, row = length / height
for y1 in xrange(height):
y2 = height - y1 - 1
memcpy(result + y1 * row, source + y2 * row, row)
2013-10-22 20:38:37 -04:00
return final