import cython from libc cimport math as cmath from libc.string cimport memcpy as c_memcpy from libc.string cimport memset as c_memset from libc.stdlib cimport malloc, free import numpy as np ################################################################################################################# def createImgSlow(img,imageHeight,imageWidth,phasePixel,orientation): cdef unsigned char [:, :] img_view = img cdef int u = 0 cdef int v = 0 cdef unsigned char val; for u in range(imageWidth): for v in range(imageHeight): val= (127*(1+cmath.sin(((u+phasePixel)/10.)))) img_view[v][u] = val ################################################################################################################# def createImgFast(img,int imageHeight,int imageWidth,float frequencyPixel,float phasePixel,int orientation,int drawSquare): cdef unsigned char [:, :] img_view = img cdef int u = 0 cdef int v = 0 cdef unsigned char *singleLine cdef unsigned int squaresize=20 #generate one line of sinusoidal signal cdef int maxdim=imageWidth if maxdimmalloc(maxdim) for u in range(maxdim): singleLine[u] = (127*(1+cmath.sin(((u+phasePixel)*2*cmath.pi*frequencyPixel/imageWidth)))) #vertical lines if orientation==0: for v in range(0,imageHeight,1): c_memcpy(&(img_view[v][0]),&(singleLine[0]),imageWidth) #horizontal lines else: if orientation==1: for v in range(0,imageHeight,1): c_memset(&(img_view[v][0]),singleLine[v],imageWidth) #may be draw a black or white square in the corner if drawSquare!=0: for v in range(imageHeight-squaresize,imageHeight,1): c_memset(&(img_view[v][imageWidth-squaresize]),(drawSquare-1)*255,squaresize) free(singleLine) ################################################################################################################# def testsinus(): imageWidth=1920 imageHeight=1080 # Memoryview on a NumPy array img = np.empty((imageHeight,imageWidth), dtype=np.dtype("uint8")) # help (np.empty) print("-------------------------------------------------------") createImgSlow(img,imageHeight,imageWidth,0,0) createImgFast(img,imageHeight,imageWidth,1,0,0,1) print("-------------------------------------------------------") #################################################################################################################