Outils pour utilisateurs

Outils du site


Action disabled: diff
cython

Ceci est une ancienne révision du document !


sinus.pyx
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=<unsigned char> (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 maxdim<imageHeight:
    maxdim=imageHeight
  singleLine=<unsigned char *>malloc(maxdim)
  for u in range(maxdim):
    singleLine[u] = <unsigned char> (127*(1+cmath.sin(((u+phasePixel)*2*cmath.pi*frequencyPixel/<float>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("-------------------------------------------------------")
#################################################################################################################
tsinus.py
#!/usr/bin/python3
# -*- coding: utf-8 -*
#Bertrand Vandeportaele 2017
 
 
#compile the library to .so
import os
os.system('python3 setup.py build_ext --inplace')
import time
import sinus
import math
import sip
#https://pythonspot.com/qt4-pixmaps-images/
import sys
from PyQt4.QtGui import *
from PyQt4 import *
from PIL import Image
import numpy
###############################"
COLORTABLE=[]
for i in range(256): COLORTABLE.append(QtGui.qRgb(i,i,i))
###############################"
 
def update():
  global label
  global imageWidth
  global imageHeight
  global img
  update.cpt=(update.cpt+1) #%255;
  t = time.time()
  #sinus.createImgSlow(img,imageHeight,imageWidth,update.cpt)
  sinus.createImgFast(img,imageHeight,imageWidth,(update.cpt//40),(update.cpt%40)*2.*math.pi/40.,(update.cpt/20)%2,(update.cpt%2)+1)
 
  #sinus.createImgFast(img,imageHeight,imageWidth,10,update.cpt,0,(update.cpt%2)+1)
  #sinus.createImgFast(img,imageHeight,imageWidth,(update.cpt//40),(update.cpt%40)*imageWidth/(update.cpt/40),(update.cpt/20)%2,(update.cpt%2)+1)
  elapsed = time.time() - t
  print(str(elapsed))
  QI=QtGui.QImage(img.data, imageWidth, imageHeight, QtGui.QImage.Format_Indexed8)    
  QI.setColorTable(COLORTABLE)
  label.setPixmap(QtGui.QPixmap.fromImage(QI))    
  #always reset this windows as the active one.... only for linux
  command='wmctrl -a \''+windowsName+'\''
  os.system(command)
###############################"
#equivalent to the initialization of a static variable
update.cpt=40
 
# Create window
app = QApplication(sys.argv)
#w = QWidget()
#for fullscreen: https://stackoverflow.com/questions/1732935/pyqt-display-fullscreen-image
w = QWidget(None,QtCore.Qt.FramelessWindowHint)
windowsName="PyQT4 Sinus"  
w.setWindowTitle(windowsName) 
 
# Create widget
label = QLabel(w)
 
imageWidth=1920*2
imageHeight=1080*2
# Memoryview on a NumPy array,   # help (numpy.empty)
img = numpy.empty((imageHeight,imageWidth), dtype=numpy.dtype("uint8"))
 
update()
 
IM = QtGui.QImage(imageWidth, imageHeight, QtGui.QImage.Format_Indexed8)
label.setGeometry(QtCore.QRect(0,0,imageWidth,imageHeight))
 
#timer adpated from example 1 of https://www.programcreek.com/python/example/52106/PyQt4.QtCore.QTimer
timer = QtCore.QTimer()
app.connect(timer,  QtCore.SIGNAL('timeout()'), update)
timer.start(1 * 10) # 1 seconds
# Draw window
w.show()
#for fullscreen:
w.showFullScreen()
#set the focus to this windows so ALT+F4 close it.. no it is the qt focus only...
w.setFocus()
 
sys.exit(app.exec_())
setup.py
from distutils.core import setup
from Cython.Build import cythonize
 
setup(
  name = 'sinus lib',
  ext_modules = cythonize("sinus.pyx"),
)

Installation

sudo apt-get install libqt4-dev
sudo apt install build-essential python3-dev libqt4-dev
sudo apt-get install python3-pyqt4
sudo apt-get install python3-numpy python3-pip
pip3 install --upgrade pip
pip3 install Cython
sudo apt install cython
pip3 install sip

Pour compiler la librairie sinus:

python3 setup.py build_ext --inplace

Pour analyser le code C généré et détecter les surcouts liés à python (en jaune)

cython  -3 -a sinus.pyx
firefox sinus.html
cython.1512986661.txt.gz · Dernière modification : 2017/12/11 11:04 de bvandepo