From 0a4b040ebea844a16684d2329ce954b4d3e2d926 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Tue, 15 Mar 2016 13:20:49 +0000
Subject: [PATCH] Rendering is broken for odd-sized images because some OpenGL
 renderers seem to ignore (UN)PACK_ALIGNMENT. This doesn't fix the problem
 under virtualbox though (I think the renderer simply does not support
 odd-sized textures).

---
 fsl/fsleyes/gl/textures/imagetexture.py |  8 ++++----
 fsl/fsleyes/gl/textures/texture.py      | 17 ++++++++++-------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/fsl/fsleyes/gl/textures/imagetexture.py b/fsl/fsleyes/gl/textures/imagetexture.py
index 02c550dd0..e9a529905 100644
--- a/fsl/fsleyes/gl/textures/imagetexture.py
+++ b/fsl/fsleyes/gl/textures/imagetexture.py
@@ -341,12 +341,12 @@ class ImageTexture(texture.Texture, notifier.Notifier):
             # first dimension as the fastest changing.
             data = data.flatten(order='F')
 
+            self.bindTexture()
+
             # Enable storage of tightly packed data of any size (i.e.
             # our texture shape does not have to be divisible by 4).
-            gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
-            gl.glPixelStorei(gl.GL_PACK_ALIGNMENT,   1)
-
-            self.bindTexture()
+            gl.glPixelStorei(gl.GL_PACK_ALIGNMENT,   data.dtype.alignment)
+            gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, data.dtype.alignment)
 
             # set interpolation routine
             interp = self.__interp
diff --git a/fsl/fsleyes/gl/textures/texture.py b/fsl/fsleyes/gl/textures/texture.py
index b30f3f38e..38a4ef7a1 100644
--- a/fsl/fsleyes/gl/textures/texture.py
+++ b/fsl/fsleyes/gl/textures/texture.py
@@ -261,9 +261,17 @@ class Texture2D(Texture):
             raise ValueError('Invalid size: {}'.format((self.__width,
                                                         self.__height)))
 
+        data = self.__data
+
+        if data is not None:
+            data      = data.ravel('F')
+            alignment = data.dtype.alignemnt
+        else:
+            alignment = 1
+
         self.bindTexture()
-        gl.glPixelStorei(gl.GL_PACK_ALIGNMENT,   1)
-        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
+        gl.glPixelStorei(gl.GL_PACK_ALIGNMENT,   alignment)
+        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)
 
         gl.glTexParameteri(gl.GL_TEXTURE_2D,
                            gl.GL_TEXTURE_MAG_FILTER,
@@ -278,11 +286,6 @@ class Texture2D(Texture):
                            gl.GL_TEXTURE_WRAP_T,
                            gl.GL_CLAMP_TO_BORDER)
 
-        data = self.__data
-
-        if data is not None:
-            data = data.ravel('F')
-
         log.debug('Configuring {} ({}) with size {}x{}'.format(
             type(self).__name__,
             self.getTextureHandle(),
-- 
GitLab