Author: ArcRiley
Date: 2008-03-18 20:10:23 -0400 (Tue, 18 Mar 2008)
New Revision: 1174

Modified:
   trunk/pysoy/src/textures/Image.pxi
Log:
No Ticket :
  * removed unused cdef's in textures.Image
  * added many comments and more docs


Modified: trunk/pysoy/src/textures/Image.pxi
===================================================================
--- trunk/pysoy/src/textures/Image.pxi  2008-03-18 23:57:32 UTC (rev 1173)
+++ trunk/pysoy/src/textures/Image.pxi  2008-03-19 00:10:23 UTC (rev 1174)
@@ -20,36 +20,82 @@
 cdef class Image (Texture) :
   '''PySoy textures.Image Class
 
-    This loads a 2D image (from PIL) as a texture
+    This class loads an image (from PIL) as a 1D or 2D texture.
+
+    As it loads one-shot, is usually slower, and has lacks many features 
+    the .soy Texture format is recommended when media is included in a game.
   '''
+
+  ############################################################################
+  #
+  # Python functions
+  #
+
   def __cinit__(self, img, *args, **keywords) :
-    cdef int b, c, x, y, ix, iy, tx, ty
-    cdef object d, idata
+    #
+    # _c = channel iterator
+    # _x = column iterator
+    # _y = row iterator
+    #
+    # _ix = original image X
+    # _iy = original image Y
+    #
+    # _tx = texture X (_ix raised to the next 2^n)
+    # _ty = texture Y (_iy raised to the next 2^n)
+    #
+    # idata = image data
+    # modes = dict to map PIL modes to number of channels
+    # texel = each texel being copied
+    #
+    cdef int _c, _x, _y, _ix, _iy, _tx, _ty
+    cdef object idata, modes, texel
     modes = {'L':1, 'LA':2, 'RGB':3, 'RGBA':4}
-    idata = img.transpose(1).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')
+    #
+    # Get the data from PIL, transpose it, and store it in _idata
+    idata = img.transpose(1).getdata()
+    #
+    # Test for 1D or 2D
     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
-      ix = len(idata)
-      iy = 1
+        #
+        # If the image is 1 pixel wide, rotate it to be 1 pixel tall
+        idata = idata.rotate(90)
+      _ix = len(idata)
+      _iy = 1
     else :
+      #
       # 2D Texture
-      ix = img.size[0]
-      iy = img.size[1]
-    tx = self._squareup(ix)
-    ty = self._squareup(iy)
-    self._scaleX = <float> ix / <float> tx
-    self._scaleY = <float> iy / <float> ty 
-    if iy > 1 :
-      self._aspect = float(ix) / float(iy)
-    self._resize(modes[img.mode], tx, ty, 1)
-    for y from 0 <= y < iy :
-      for x from 0 <= x < ix :
-        d = idata[(y*ix)+x]
-        for c from 0 <= c < self._chans :
-          self._texels[(((y*tx)+x)*self._chans)+c] = d[c]
+      _ix = img.size[0]
+      _iy = img.size[1]
+    #
+    # Calc texture size
+    _tx = self._squareup(_ix)
+    _ty = self._squareup(_iy)
+    #
+    # Calc ratio between texture and actual size
+    # This is used for texcoord translation so the image acts like the full
+    # texture, when actually it's just scaled all the texcoords
+    self._scaleX = <float> _ix / <float> _tx
+    self._scaleY = <float> _iy / <float> _ty 
+    #
+    # Calculate aspect ratio when 2D
+    if _iy != 0 :
+      self._aspect = float(_ix) / float(_iy)
+    #
+    # Allocates _texels, set self._width and self._height, etc
+    self._resize(modes[img.mode], _tx, _ty, 1)
+    #
+    # Copy texel data between the image and the texture, row by row
+    for _y from 0 <= _y < _iy :
+      for _x from 0 <= _x < _ix :
+        texel = idata[(_y*_ix)+_x]
+        for _c from 0 <= _c < self._chans :
+          self._texels[(((_y*_tx)+_x)*self._chans)+_c] = texel[_c]

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to