Author: ArcRiley
Date: 2007-07-03 18:17:42 -0400 (Tue, 03 Jul 2007)
New Revision: 329

Added:
   trunk/pysoy/src/textures/Image.pxi
   trunk/pysoy/tests/arc.png
   trunk/pysoy/tests/rgb.png
Modified:
   trunk/pysoy/src/textures/Texture.pxi
   trunk/pysoy/src/textures/soy.textures.pxd
   trunk/pysoy/src/textures/soy.textures.pyx
   trunk/pysoy/src/widgets/Canvas.pxi
   trunk/pysoy/tests/tex_pyramid.py
Log:
split Image and Texture, improved Image loading


Copied: trunk/pysoy/src/textures/Image.pxi (from rev 323, 
trunk/pysoy/src/textures/Texture.pxi)
===================================================================
--- trunk/pysoy/src/textures/Image.pxi                          (rev 0)
+++ trunk/pysoy/src/textures/Image.pxi  2007-07-03 22:17:42 UTC (rev 329)
@@ -0,0 +1,52 @@
+# PySoy's textures.Image class
+#
+# Copyright (C) 2007 Team PySoy
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, see http://www.gnu.org/licenses
+#
+# $Id$
+
+cdef class Image (Texture) :
+  '''PySoy textures.Image Class
+
+     This loads a 2D image (from PIL) as a texture
+  '''
+  def __new__(self, img, *args, **keywords) :
+    cdef int b, c, lx, ly
+    cdef object d, idata
+    modes = {'L':1, 'LA':2, 'RGB':3, 'RGBA':4}
+    idata = img.getdata()
+    if img.size[0] == 0 or img.size[1] == 0 :
+      raise ValueError('Image of non-null size must be provided')
+    if not modes.has_key(img.mode) :
+      raise NotImplementedError('Cannot handle this image format')
+    if img.size[0] == 1 or img.size[1] == 1 :
+      # 1D Texture
+      if img.size[1] != 1 :
+        idata = idata.rotate(90)  # Make the data horizontal
+      lx = self._squareup(len(data))
+      ly = 1
+      if lx != len(data) :
+        idata = idata.resize((lx, 1))
+    else :
+      # 2D Texture
+      lx = self._squareup(img.size[0])
+      ly = self._squareup(img.size[1])
+      if lx != img.size[0] or ly != img.size[1] :
+        idata = idata.resize((lx, ly))
+    self._resize(modes[img.mode], lx, ly, 1)
+    for b from 0 <= b < lx*ly :
+      d = idata[b]
+      for c from 0 <= c < self._chans :
+        self._texels[(b*self._chans)+c] = d[c]

Modified: trunk/pysoy/src/textures/Texture.pxi
===================================================================
--- trunk/pysoy/src/textures/Texture.pxi        2007-07-03 22:16:16 UTC (rev 
328)
+++ trunk/pysoy/src/textures/Texture.pxi        2007-07-03 22:17:42 UTC (rev 
329)
@@ -57,9 +57,8 @@
                .type will be a property for what a texture applies to.
 
         '''
-    self._filename = ''
 
-
+  # This property is for debugging only
   property id :
     '''Texture.id
        
@@ -69,6 +68,7 @@
       return self._textureID
     # PYTHON SHOULD NOT EVER SET THE TEXTURE ID!    
 
+  # This property is for debugging only
   property target :
     '''Texture.target
 
@@ -78,15 +78,6 @@
       return self._textureTarget
 
 
-  property filename :
-    '''Texture.filename
-
-    Complete path of the image file
-    '''
-    def __get__(self) :
-      return self._filename
-
-
   property size :
     '''Texture.size
 
@@ -104,42 +95,6 @@
     def __get__(self) :
       return <char *> self._texels
 
-
-  def fromPIL(self, object img) :
-    cdef int i, l
-    self._filename = img.filename
-    if img.mode == 'RGB' :
-      self._resize(3, img.size[0], img.size[1], 0)
-    elif img.mode == 'RGBA' :
-      self._resize(4, img.size[0], img.size[1], 0)
-    else :
-      raise NotImplementedError('Cannot handle this image format')
-    if self._isNPOT() :
-      raise NotImplementedError('Cannot handle Non Power of Two textures yet')
-    buff = img.tostring()
-    l = len(buff)
-    for i from 0 <= i < l :
-      self._texels[i] = ord(buff[i])
-
-
-  cdef int _isNPOT(self) :
-    cdef int _sz
-
-    _sz = self._width
-    while _sz > 1 and _sz % 2 == 0:
-      _sz = _sz / 2
-    if _sz != 1 :
-      return 1
-
-    _sz = self._height
-    while _sz > 1 and _sz % 2 == 0:
-      _sz = _sz / 2
-    if _sz != 1 :
-      return 1
-    else :
-      return 0
-
-
   cdef void _bind(self) :
     if self._textureID == 0 :
       gl.glGenTextures(1, &self._textureID)
@@ -165,35 +120,39 @@
     #if max_size > self._width or max_size > self._height :
     #  raise ValueError('Image size exceeds max texture size, which is %d 
pixels for each side'%max_size)
 
+
   cdef void _resize(self, int c, int x, int y, int z) :
     cdef long int _size
-    if z == 0 :
-      if y == 0 :
-        if x == 0 :
-          self._textureTarget = 0
-          if self._width != 0 :
-            py.PyMem_Free(self._texels)
-            _size = 0
-        else :
-          self._textureTarget = gl.GL_TEXTURE_1D
-          _size = c*x
+    _size = c*x*y*z
+    if _size == 0 :
+      self._textureTarget = 0
+      if self._width != 0 :
+        py.PyMem_Free(self._texels)
+    elif z == 1 :
+      if y == 1 :
+        self._textureTarget = gl.GL_TEXTURE_1D
       else :
         self._textureTarget = gl.GL_TEXTURE_2D
-        _size = c*x*y
     else :
       self._textureTarget = gl.GL_TEXTURE_3D
-      _size = c*x*y*z
     if _size :
       if self._width :
         self._texels = <gl.GLubyte *> py.PyMem_Realloc(self._texels, _size)
       else :
         self._texels = <gl.GLubyte *> py.PyMem_Malloc(_size)
-    self._chans = c
+    self._chans  = c
     self._width  = x
     self._height = y
     self._depth  = z
 
 
+  cdef int _squareup(self, int size) :
+    cdef int e
+    for e from 0 <= e <= 12 :
+      if size <= 1 << e :
+        return 1 << e
+
+
   def __dealloc__(self) :
     if self._textureID != 0 :
       gl.glDeleteTextures(1, &self._textureID)

Modified: trunk/pysoy/src/textures/soy.textures.pxd
===================================================================
--- trunk/pysoy/src/textures/soy.textures.pxd   2007-07-03 22:16:16 UTC (rev 
328)
+++ trunk/pysoy/src/textures/soy.textures.pxd   2007-07-03 22:17:42 UTC (rev 
329)
@@ -29,9 +29,8 @@
   cdef int          _width
   cdef int          _height
   cdef int          _depth
-  cdef object       _filename
   cdef gl.GLubyte  *_texels
   # C functions
-  cdef int          _isNPOT(self)
   cdef void         _bind(self)
   cdef void         _resize(self, int, int, int, int)
+  cdef int          _squareup(self, int)

Modified: trunk/pysoy/src/textures/soy.textures.pyx
===================================================================
--- trunk/pysoy/src/textures/soy.textures.pyx   2007-07-03 22:16:16 UTC (rev 
328)
+++ trunk/pysoy/src/textures/soy.textures.pyx   2007-07-03 22:17:42 UTC (rev 
329)
@@ -24,3 +24,4 @@
 __version__ = 'Trunk (r'+'$Rev$'[6:-2]+')'
 
 include "Texture.pxi"
+include "Image.pxi"

Modified: trunk/pysoy/src/widgets/Canvas.pxi
===================================================================
--- trunk/pysoy/src/widgets/Canvas.pxi  2007-07-03 22:16:16 UTC (rev 328)
+++ trunk/pysoy/src/widgets/Canvas.pxi  2007-07-03 22:17:42 UTC (rev 329)
@@ -24,14 +24,14 @@
   '''
   def __new__(self, parent, position=(0,0), size=(0,0), 
               texture=None, *args, **keywords) :
+    self._verts[0].ty = 1.0
     self._verts[1].px = 1.0
     self._verts[1].tx = 1.0
+    self._verts[1].ty = 1.0
     self._verts[2].px = 1.0
     self._verts[2].py = 1.0
     self._verts[2].tx = 1.0
-    self._verts[2].ty = 1.0
     self._verts[3].py = 1.0
-    self._verts[3].ty = 1.0
     self._faces[0].a = 0
     self._faces[0].b = 1
     self._faces[0].c = 2

Added: trunk/pysoy/tests/arc.png
===================================================================
(Binary files differ)


Property changes on: trunk/pysoy/tests/arc.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/pysoy/tests/rgb.png
===================================================================
(Binary files differ)


Property changes on: trunk/pysoy/tests/rgb.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/pysoy/tests/tex_pyramid.py
===================================================================
--- trunk/pysoy/tests/tex_pyramid.py    2007-07-03 22:16:16 UTC (rev 328)
+++ trunk/pysoy/tests/tex_pyramid.py    2007-07-03 22:17:42 UTC (rev 329)
@@ -5,9 +5,11 @@
 from time import sleep
 
 img = Image.open('lava.png')
-tex = soy.textures.Texture()
-tex.fromPIL(img)
+lava = soy.textures.Image(img)
 
+img = Image.open('arc.png')
+face = soy.textures.Image(img)
+
 scr = soy.Screen()
 win = soy.Window(scr, 'Simulation Test')
 
@@ -15,10 +17,10 @@
 cam = soy.bodies.Camera(sce)
 cam.position = (0.0, 0.0, 5.0)
 
-ca1 = soy.widgets.Canvas(win, size=(300,220), position=(10,10), texture=tex)
+ca1 = soy.widgets.Canvas(win, size=(300,220), position=(10,10), texture=lava)
 pro = soy.widgets.Projector(win, size=(280, 200), position=(20,20), camera=cam)
-ca2 = soy.widgets.Canvas(win, size=(50,50), position=(100,100), texture=tex)
-pyr = soy.bodies.Pyramid(sce, tex)
+ca2 = soy.widgets.Canvas(win, size=(64,96), position=(100,100), texture=face)
+pyr = soy.bodies.Pyramid(sce, face)
 pyr.rotation = (1.0, 1.0, 0.0)
 
 if __name__ == '__main__' :

_______________________________________________
PySoy-SVN mailing list
[email protected]
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to