def __init__(self, width, height): """Create an ``OffScreenCanvasTarget``. A :class:`.RenderTexture` is created, to be used as the rendering target. :arg width: Width in pixels :arg height: Height in pixels """ from fsleyes.gl.textures import RenderTexture self.__width = width self.__height = height self.__target = RenderTexture('{}({})_RenderTexture'.format( type(self).__name__, id(self)))
class OffScreenCanvasTarget(object): """Base class for canvas objects which support off-screen rendering. """ def __init__(self, width, height): """Create an ``OffScreenCanvasTarget``. A :class:`.RenderTexture` is created, to be used as the rendering target. :arg width: Width in pixels :arg height: Height in pixels """ from fsleyes.gl.textures import RenderTexture self.__width = width self.__height = height self.__target = RenderTexture('{}({})_RenderTexture'.format( type(self).__name__, id(self))) def _setGLContext(self): """Configures the GL context to render to this canvas. """ getGLContext().setTarget(self) return True def _draw(self, *a): """Must be provided by subclasses.""" raise NotImplementedError() def getAnnotations(self): """Must be provided by subclasses.""" raise NotImplementedError() def GetSize(self): """Returns a tuple containing the canvas width and height.""" return self.__width, self.__height def GetScaledSize(self): """Returns a tuple containing the canvas width and height.""" return self.GetSize() def Refresh(self, *a): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def FreezeDraw(self): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def ThawDraw(self): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def FreezeSwapBuffers(self): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def ThawSwapBuffers(self): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def EnableHighDPI(self): """Does nothing. This canvas is for static (i.e. unchanging) rendering. """ pass def draw(self): """Calls the :meth:`_draw` method, which must be provided by subclasses. """ self._setGLContext() self._initGL() self.__target.shape = self.__width, self.__height with self.__target.target(): self._draw() def getBitmap(self): """Return a (height*width*4) shaped numpy array containing the rendered scene as an RGBA bitmap. The bitmap will be full of zeros if the scene has not been drawn (via a call to :meth:`draw`). """ self._setGLContext() return self.__target.getBitmap() def saveToFile(self, filename): """Saves the contents of this canvas as an image, to the specified file. """ import matplotlib.image as mplimg mplimg.imsave(filename, self.getBitmap())