Anonymous avatar Anonymous committed 68793bd

Changed various PixelArray loops for mappings. Fixed 2.4 compatibility issues introduced with rev. 1153.

Comments (0)

Files changed (3)

 #include "pygamedocs.h"
 #include "surface.h"
 
+#if PY_VERSION_HEX < 0x02050000
+#define PyIndex_Check(op) 0
+#endif
+
 #define GET_SLICE_VALS(array, start, end, ylen, ystep, xlen, xstep, padding, \
     _low, _high, _step, _pad)                                           \
-    start = array->start + _low;                                        \
-    end = array->end - array->xlen + _high;                             \
+    start = array->start + _low * array->xstep;                         \
+    end = (_high - _low > 0) ?                                          \
+        array->end - array->xlen + _high * array->xstep :               \
+        array->end - array->xlen + _low * array->xstep;                 \
     ylen = array->ylen;                                                 \
     ystep = array->ystep;                                               \
-    xlen = _high - _low;                                                \
-    xstep = _step;                                                      \
+    xlen = (_high - _low < 0) ? _low - _high : _high - _low;            \
+    xstep = array->xstep * _step;                                       \
     padding = _pad;
 
+
+/*
+  end = array->end - array->xlen + _high * array->xstep;
+  xstep = (_step > 1) ? (array->xstep + _step) : array->xstep;
+*/
+
 typedef struct
 {
     PyObject_HEAD
     Uint32 end;         /* End offset for subarrays */
     Uint32 xlen;        /* X segment length. */
     Uint32 ylen;        /* Y segment length. */
-    Uint32 xstep;       /* X offset step width. */
-    Uint32 ystep;       /* Y offset step width. */
+    Sint32 xstep;       /* X offset step width. */
+    Sint32 ystep;       /* Y offset step width. */
     Uint32 padding;     /* Padding to get to the next x offset. */
     PyObject *parent;   /* Parent pixel array */
 
 
 static PyPixelArray* _pxarray_new_internal (PyTypeObject *type,
     PyObject *surface, Uint32 start, Uint32 end, Uint32 xlen, Uint32 ylen,
-    Uint32 xstep, Uint32 ystep, Uint32 padding, PyObject *parent);
+    Sint32 xstep, Sint32 ystep, Uint32 padding, PyObject *parent);
 static PyObject* _pxarray_new (PyTypeObject *type, PyObject *args,
     PyObject *kwds);
 static void _pxarray_dealloc (PyPixelArray *self);
     Uint32 row);
 static void _set_single_pixel (Uint8 *pixels, int bpp, Uint32 _index,
     Uint32 row, SDL_PixelFormat *format, Uint32 color);
-static PyObject* _array_slice_internal (PyPixelArray *array, Uint32 _start,
-    Uint32 _end, Uint32 _step);
+static PyObject* _array_slice_internal (PyPixelArray *array, Sint32 _start,
+    Sint32 _end, Sint32 _step);
 
 /* Sequence methods */
 static Py_ssize_t _pxarray_length (PyPixelArray *array);
 static PyObject* _pxarray_inplace_repeat (PyPixelArray *array, Py_ssize_t n);
 
 /* Mapping methods */
-
 static int _get_subslice (PyPixelArray *array, PyObject *op, Py_ssize_t length,
     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
 static PyObject* _pxarray_subscript (PyPixelArray *array, PyObject *op);
 /**
  * Mapping interface support for the PyPixelArray.
  */
-
 static PyMappingMethods _pxarray_mapping =
 {
     (inquiry) _pxarray_length,              /*mp_length*/
 {
     PyObject_HEAD_INIT(NULL)
     0,
-    "PixelArray",               /* tp_name */
+    "pygame.PixelArray",        /* tp_name */
     sizeof (PyPixelArray),      /* tp_basicsize */
     0,                          /* tp_itemsize */
     (destructor) _pxarray_dealloc, /* tp_dealloc */
     (reprfunc) &_pxarray_repr,  /* tp_repr */
     0,                          /* tp_as_number */
     &_pxarray_sequence,         /* tp_as_sequence */
-    0,/*&_pxarray_mapping,          /* tp_as_mapping */
+    0, /*&_pxarray_mapping,          /* tp_as_mapping */
     0,                          /* tp_hash */
     0,                          /* tp_call */
     0,                          /* tp_str */
 static PyPixelArray*
 _pxarray_new_internal (PyTypeObject *type, PyObject *surface,
     Uint32 start, Uint32 end, Uint32 xlen, Uint32 ylen,
-    Uint32 xstep, Uint32 ystep, Uint32 padding, PyObject *parent)
+    Sint32 xstep, Sint32 ystep, Uint32 padding, PyObject *parent)
 {
     PyPixelArray *self = (PyPixelArray *) type->tp_alloc (type, 0);
     if (!self)
     Uint32 pixel;
     Uint32 x = 0;
     Uint32 y = 0;
+    Sint32 xlen = 0;
+    Sint32 absxstep;
+    Sint32 absystep;
+    Uint32 posx = 0;
+    Uint32 posy = 0;
 
     surface = PySurface_AsSurface (array->surface);
     bpp = surface->format->BytesPerPixel;
         array->ylen, array->ystep, array->padding);
 */
     string = PyString_FromString ("PixelArray(");
+
+    absxstep = ABS (array->xstep);
+    absystep = ABS (array->ystep);
+    xlen = (Sint32) array->xlen - absxstep;
+
     switch (bpp)
     {
     case 1:
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
             /* Construct the rows */
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
-
-            for (x = 0; x < array->xlen - array->xstep; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != (Uint32)xlen)
             {
                 /* Construct the columns */
                 pixel = (Uint32) *((Uint8 *) pixels + array->start + x +
                     y * array->padding);
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%d, ", pixel));
+                x += array->xstep;
+                posx += absxstep;
             }
             pixel = (Uint32) *((Uint8 *) pixels + array->start + x +
                 y * array->padding);
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%d]", pixel));
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     case 2:
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
             /* Construct the rows */
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
-
-            for (x = 0; x < array->xlen - array->xstep; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != (Uint32)xlen)
             {
                 /* Construct the columns */
-                pixel = (Uint32)*((Uint16 *) (pixels + y * array->padding) +
+                pixel = (Uint32) *((Uint16 *) (pixels + y * array->padding) +
                     array->start + x);
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%d, ", pixel));
+                x += array->xstep;
+                posx += absxstep;
             }
-            pixel = (Uint32)*((Uint16 *) (pixels + y * array->padding) +
+            pixel = (Uint32) *((Uint16 *) (pixels + y * array->padding) +
                 array->start + x);
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%d]", pixel));
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     case 3:
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
             /* Construct the rows */
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
-
-            for (x = 0; x < array->xlen - array->xstep; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != (Uint32)xlen)
             {
                 /* Construct the columns */
                 px24 = ((Uint8 *) (pixels + y * array->padding) +
 #endif
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%d, ", pixel));
+                x += array->xstep;
+                posx += absxstep;
             }
             px24 = ((Uint8 *) (pixels + y * array->padding) +
                 array->start * 3 + x * 3);
 #endif
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%d]", pixel));
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     default: /* 4bpp */
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
             /* Construct the rows */
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
-
-            for (x = 0; x < array->xlen - array->xstep; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != (Uint32)xlen)
             {
                 /* Construct the columns */
                 pixel = *((Uint32 *) (pixels + y * array->padding) +
                     array->start + x);
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%d, ", pixel));
+                x += array->xstep;
+                posx += absxstep;
             }
             pixel = *((Uint32 *) (pixels + y * array->padding) +
                 array->start + x);
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%d]", pixel));
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     }
  * Creates a 2D slice of the array.
  */
 static PyObject*
-_array_slice_internal (PyPixelArray *array, Uint32 _start, Uint32 _end,
-    Uint32 _step)
+_array_slice_internal (PyPixelArray *array, Sint32 _start, Sint32 _end,
+    Sint32 _step)
 {
     Uint32 start;
     Uint32 end;
     Uint32 xlen;
     Uint32 ylen;
-    Uint32 xstep;
-    Uint32 ystep;
+    Sint32 xstep;
+    Sint32 ystep;
     Uint32 padding;
     SDL_Surface *sf = PySurface_AsSurface (array->surface);
 
     GET_SLICE_VALS (array, start, end, ylen, ystep, xlen, xstep, padding,
         _start, _end, _step, sf->pitch);
 
+/*
+    printf ("VALS: %d:%d, %d:%d %d:%d %d\n",
+        start, end, xlen, xstep, ylen, ystep, padding);
+*/
     return (PyObject *) _pxarray_new_internal
         (&PyPixelArray_Type, array->surface, start, end, xlen, ylen,
          xstep, ystep, padding, (PyObject *) array);
 _pxarray_length (PyPixelArray *array)
 {
     if (array->xlen > 1)
-        return array->xlen / array->xstep;
-    return array->ylen / array->ystep;
+        return array->xlen / ABS (array->xstep);
+    return array->ylen / ABS (array->ystep);
 }
 
 /**
         return _get_single_pixel ((Uint8 *) surface->pixels, bpp,
             array->start + _index * array->xstep, _index * array->padding);
 
-    return _array_slice_internal
-        (array, (Uint32) _index, (Uint32) _index + 1, 1);
+    return _array_slice_internal (array, _index, _index + 1, 1);
 }
 
 /**
     if (low == high)
         Py_RETURN_NONE;
 
-    return _array_slice_internal (array, (Uint32) low, (Uint32) high, 1);
+    return _array_slice_internal (array, low, high, 1);
 }
 
 static int
 {
     SDL_Surface *surface;
     SDL_Surface *valsf = NULL;
-    Uint32 x;
-    Uint32 y;
-    Uint32 vx;
-    Uint32 vy;
+    Uint32 x = 0;
+    Uint32 y = 0;
+    Uint32 vx = 0;
+    Uint32 vy = 0;
     int bpp;
     int valbpp;
     Uint8 *pixels;
     Uint32 end;
     Uint32 xlen;
     Uint32 ylen;
-    Uint32 xstep;
-    Uint32 ystep;
+    Sint32 xstep;
+    Sint32 ystep;
     Uint32 padding;
+    Uint32 posx = 0;
+    Uint32 posy = 0;
+    Sint32 absxstep;
+    Sint32 absystep;
 
     /* Set the correct slice indices */
     surface = PySurface_AsSurface (array->surface);
         return -1;
     }
 
-    vx = 0;
-    vy = 0;
+    absxstep = ABS (xstep);
+    absystep = ABS (ystep);
 
     /* Single value assignment. */
     switch (bpp)
     {
     case 1:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
             vx = 0;
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
-
                 *((Uint8 *) pixels + y * padding + offset) =
                     (Uint8)*((Uint8 *)
                         valsf->pixels + vy * val->padding + val->start + vx);
                 vx += val->xstep;
+                x += xstep;
+                posx += absxstep;
             }
             vy += val->ystep;
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 2:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
             vx = 0;
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
-
                 *((Uint16 *) (pixels + y * padding) + offset) =
                     (Uint16)*((Uint16 *)
                         ((Uint8*)valsf->pixels + vy * val->padding) +
                         val->start + vx);
                 vx += val->xstep;
+                x += xstep;
+                posx += absxstep;
             }
             vy += val->ystep;
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 3:
         Uint8 *vpx;
         SDL_PixelFormat *format = surface->format;
         SDL_PixelFormat *vformat = valsf->format;
-        for (y = 0; y < ylen; y += ystep)
+
+        while (posy != ylen)
         {
             vx = 0;
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
 
                     *(vpx + 2 - (vformat->Bshift >> 3));
 #endif
                 vx += val->xstep;
+                x += xstep;
+                posx += absxstep;
             }
             vy += val->ystep;
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
     default:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
             vx = 0;
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
 
                         ((Uint8*)valsf->pixels + y * val->padding) +
                         val->start + vx);
                 vx += val->xstep;
+                x += xstep;
+                posx += absxstep;
             }
             vy += val->ystep;
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
     Uint32 end;
     Uint32 xlen;
     Uint32 ylen;
-    Uint32 xstep;
-    Uint32 ystep;
+    Sint32 xstep;
+    Sint32 ystep;
     Uint32 padding;
+    Uint32 posx = 0;
+    Uint32 posy = 0;
+    Sint32 absxstep;
+    Sint32 absystep;
 
     surface = PySurface_AsSurface (array->surface);
     bpp = surface->format->BytesPerPixel;
         colorvals[offset] = color;
     }
 
+    absxstep = ABS (xstep);
+    absystep = ABS (ystep);
+
     switch (bpp)
     {
     case 1:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 color = *colorvals++;
                 *((Uint8 *) pixels + y * padding + offset) = (Uint8) color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 2:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 color = *colorvals++;
                 *((Uint16 *) (pixels + y * padding) + offset) = (Uint16) color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 3:
     {
         Uint8 *px;
         SDL_PixelFormat *format = surface->format;
-        for (y = 0; y < ylen; y += ystep)
+
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 color = *colorvals++;
-
                 px = (Uint8 *) (pixels + y * padding) + offset * 3;
 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
                 *(px + (format->Rshift >> 3)) = (Uint8) (color >> 16);
                 *(px + 2 - (format->Gshift >> 3)) = (Uint8) (color >> 8);
                 *(px - (format->Bshift >> 3)) = (Uint8) color;
 #endif
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
     default:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 color = *colorvals++;
                 *((Uint32 *) (pixels + y * padding) + offset) = color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
 _pxarray_ass_item (PyPixelArray *array, Py_ssize_t _index, PyObject *value)
 {
     SDL_Surface *surface;
-    Uint32 x;
-    Uint32 y;
+    Uint32 x = 0;
+    Uint32 y = 0;
     int bpp;
     Uint8 *pixels;
     Uint32 color = 0;
     Uint32 end;
     Uint32 xlen;
     Uint32 ylen;
-    Uint32 xstep;
-    Uint32 ystep;
+    Sint32 xstep;
+    Sint32 ystep;
     Uint32 padding;
+    Uint32 posx = 0;
+    Uint32 posy = 0;
+    Sint32 absxstep;
+    Sint32 absystep;
 
     surface = PySurface_AsSurface (array->surface);
     bpp = surface->format->BytesPerPixel;
     GET_SLICE_VALS (array, start, end, ylen, ystep, xlen, xstep, padding,
         _index, _index + 1, 1, surface->pitch);
 
+/*
+    printf ("ITEM: %d:%d, %d:%d %d:%d %d\n",
+        start, end, xlen, xstep, ylen, ystep, padding);
+*/
+    absxstep = ABS (xstep);
+    absystep = ABS (ystep);
+
     /* Single value assignment. */
     switch (bpp)
     {
     case 1:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 *((Uint8 *) pixels + y * padding + offset) = (Uint8) color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 2:
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 *((Uint16 *) (pixels + y * padding) + offset) = (Uint16) color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     case 3:
     {
         Uint8 *px;
         SDL_PixelFormat *format = surface->format;
-        for (y = 0; y < ylen; y += ystep)
+
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 px = (Uint8 *) (pixels + y * padding) + offset * 3;
                 *(px + 2 - (format->Gshift >> 3)) = (Uint8) (color >> 8);
                 *(px - (format->Bshift >> 3)) = (Uint8) color;
 #endif
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
     default: /* 4 bpp */
-        for (y = 0; y < ylen; y += ystep)
+        while (posy != ylen)
         {
-            for (x = 0; x < xlen; x += xstep)
+            posx = 0;
+            x = 0;
+            while (posx != xlen)
             {
                 offset = start + x;
                 *((Uint32 *) (pixels + y * padding) + offset) = color;
+                x += xstep;
+                posx += absxstep;
             }
+            y += ystep;
+            posy += absystep;
         }
         break;
     }
-
     return 0;
 }
 
 _pxarray_contains (PyPixelArray *array, PyObject *value)
 {
     SDL_Surface *surface;
-    Uint32 x;
-    Uint32 y;
+    Uint32 x = 0;
+    Uint32 y = 0;
     Py_ssize_t _index;
     Uint8 *pixels;
     int bpp;
     Uint32 color;
 
+    Uint32 posx = 0;
+    Uint32 posy = 0;
+    Sint32 absxstep;
+    Sint32 absystep;
+
     surface = PySurface_AsSurface (array->surface);
     bpp = surface->format->BytesPerPixel;
     pixels = (Uint8 *) surface->pixels;
         return -1;
     }
 
+    absxstep = ABS (array->xstep);
+    absystep = ABS (array->ystep);
+
     switch (bpp)
     {
     case 1:
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
-            for (x = 0; x < array->xlen; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != array->xlen)
             {
                 _index = array->start + x;
                 if (*((Uint8 *) pixels + y * array->padding + _index)
                     == (Uint8) color)
                     return 1;
+                x += array->xstep;
+                posx += absxstep;
             }
+            y += array->ystep;
+            posy += absystep;
         }
-	break;
+        break;
     case 2:
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
-            for (x = 0; x < array->xlen; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != array->xlen)
             {
                 _index = array->start + x;
                 if (*((Uint16 *) (pixels + y * array->padding) + _index)
                     == (Uint16) color)
                     return 1;
+                x += array->xstep;
+                posx += absxstep;
             }
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     case 3:
     {
         Uint32 pxcolor;
         Uint8 *pix;
-        for (y = 0; y < array->ylen; y += array->ystep)
+
+        while (posy != array->ylen)
         {
-            for (x = 0; x < array->xlen; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != array->xlen)
             {
                 _index = array->start + x;
                 pix = ((Uint8 *) (pixels + y * array->padding) + _index * 3);
 #endif
                 if (pxcolor == color)
                     return 1;
+                x += array->xstep;
+                posx += absxstep;
             }
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     }
     default: /* 4 bpp */
-        for (y = 0; y < array->ylen; y += array->ystep)
+        while (posy != array->ylen)
         {
-            for (x = 0; x < array->xlen; x += array->xstep)
+            posx = 0;
+            x = 0;
+            while (posx != array->xlen)
             {
                 _index = array->start + x;
                 if (*((Uint32 *) (pixels + y * array->padding) + _index)
                     == color)
                     return 1;
+                x += array->xstep;
+                posx += absxstep;
             }
+            y += array->ystep;
+            posy += absystep;
         }
         break;
     }
         if (size > 2 || (size == 2 && array->xlen == 1))
             return RAISE (PyExc_IndexError, "too many indices for the array");
 
-        lenx = (array->xlen > 1) ? array->xlen / array->xstep : -1;
+        lenx = (array->xlen > 1) ? array->xlen / array->xstep : 0;
         leny = array->ylen / array->ystep;
 
         obj = PySequence_Fast_GET_ITEM (op, 0);
                 surface->format->BytesPerPixel, array->start + xstart,
                 ystart * array->padding * array->ystep);
         }
+
 /*
         printf ("X: %d:%d:%d Y: %d:%d:%d\n", xstart, xstop, xstep,
             ystart, ystop, ystep);
 */
 /*
+        printf ("NEW ARRAY: %d:%d %d:%d:%d %d:%d:%d\n",
+            array->start + ystart * array->padding + xstart,
+            array->start + ystop * array->padding + xstop,
+            xstart, xstop, xstep, ystart, ystop, ystep);
+*/
+/*
+        lenx = MAX((xstop - xstart) / xstep, 1);
+        leny = MAX((ystop - ystart) / ystep, 1);
+*/
+
+/*
 static PyPixelArray* _pxarray_new_internal (PyTypeObject *type,
     PyObject *surface, Uint32 start, Uint32 end, Uint32 xlen, Uint32 ylen,
     Uint32 xstep, Uint32 ystep, Uint32 padding, PyObject *parent);
             (Uint32) array->start + ystop * array->padding + xstop,
             (Uint32) xstop - xstart,
             (Uint32) ystop - ystart,
-            (Uint32) xstep,
-            (Uint32) ystep,
+            (Sint32) xstep,
+            (Sint32) ystep,
             (Uint32) array->padding, (PyObject *) array);
     }
     else if (PySlice_Check (op))
     {
         /* A slice */
-        PyObject *result;
         Py_ssize_t slicelen;
         Py_ssize_t step;
         Py_ssize_t start;
         {
             /* 2D array - slice along the x axis */
             retval = PySlice_GetIndicesEx ((PySliceObject *) op,
-                array->xlen / array->xstep, &start, &stop, &step, &slicelen);
+                (Py_ssize_t) (array->xlen / array->xstep), &start, &stop, &step,
+                &slicelen);
         }
         else
         {
             /* 1D array - use the y axis. */
             retval = PySlice_GetIndicesEx ((PySliceObject *) op,
-                array->ylen / array->ystep, &start, &stop, &step, &slicelen);
+                (Py_ssize_t) (array->ylen / array->ystep), &start, &stop, &step,
+                &slicelen);
         }
-        if (retval < 0)
+        if (retval < 0 || slicelen < 0)
             return NULL;
+        if (slicelen == 0)
+            Py_RETURN_NONE;
 
-        if (slicelen < 0) /* TODO: empty surface with 0x0 px? */
+/*
+        printf ("start: %d, stop: %d, step: %d, len: %d\n", start, stop,
+            step, slicelen);
+*/
+        return (PyObject *) _array_slice_internal (array, start, stop, step);
+    }
+    else if (PyIndex_Check (op) || PyInt_Check (op) || PyLong_Check (op))
+    {
+        Py_ssize_t i;
+#if PY_VERSION_HEX >= 0x02050000
+        PyObject *val = PyNumber_Index (op);
+        if (!val)
             return NULL;
-
-        return (PyObject *) _array_slice_internal
-            (array, (Uint32) start, (Uint32) stop, (Uint32) step);
-/*
-        result = (PyObject *) _pxarray_new_internal
-            (&PyPixelArray_Type, array->surface,
-                (Uint32) start, (Uint32) stop * array->ylen,
-                1, stop - start, 1, step, (PyObject *) array);
-        return result;
-*/
-    }
-    else if (PyIndex_Check (op))
-    {
         /* A simple index. */
-        Py_ssize_t i = PyNumber_AsSsize_t (op, PyExc_IndexError);
-
+        i = PyNumber_AsSsize_t (val, PyExc_IndexError);
+#else
+        if (PyInt_Check (op))
+            i = PyInt_AsLong (op);
+        else
+            i = PyLong_AsLong (op);
+#endif 
         if (i == -1 && PyErr_Occurred ())
             return NULL;
-
         if (i < 0)
-            i += (array->xlen > 1) ? array->xlen / array->xstep :
-                array->ylen / array->ystep;
+            i += (array->xlen > 1) ? array->xlen / ABS (array->xstep) :
+                array->ylen / ABS (array->ystep);
 
         return _pxarray_item (array, i);
     }
 {
     SDL_Surface *surface = PySurface_AsSurface (array->surface);
     
-    if (PyIndex_Check (op))
+    if (PyIndex_Check (op) || PyInt_Check (op) || PyLong_Check (op))
     {
+        Py_ssize_t i;
+#if PY_VERSION_HEX >= 0x02050000
+        PyObject *val = PyNumber_Index (op);
+        if (!val)
+            return -1;
         /* A simple index. */
-        Py_ssize_t i = PyNumber_AsSsize_t (op, PyExc_IndexError);
+        i = PyNumber_AsSsize_t (val, PyExc_IndexError);
+#else
+        if (PyInt_Check (op))
+            i = PyInt_AsLong (op);
+        else
+            i = PyLong_AsLong (op);
+#endif 
         if (i == -1 && PyErr_Occurred ())
             return -1;
 
 #define MAX(a,b) ( (a) > (b) ? (a) : (b))
 #endif
 
+#ifndef ABS
+#define ABS(a) (((a) < 0) ? -(a) : (a))
+#endif
+
 /* test sdl initializations */
 #define VIDEO_INIT_CHECK()                                              \
     if(!SDL_WasInit(SDL_INIT_VIDEO))                                    \

test/pixelarray_test.py

                      (204, 0, 204), (17, 17, 17), (204, 0, 204), (17, 17, 17))
             self.assertEqual (ar[1][0], sf.map_rgb ((204, 0, 204)))
             self.assertEqual (ar[1][1], sf.map_rgb ((17, 17, 17)))
+            self.assertEqual (ar[1][2], sf.map_rgb ((204, 0, 204)))
         
             # Test pixel array assignment.
             ar[1] = ar2[3]
             self.assertEqual (ar[5][7], val)
 
 ##     def test_subscript (self):
-##         for bpp in (8, 16, 24, 32):
+##         for bpp in (32, ):#16, 24, 32):
 ##             sf = pygame.Surface ((6, 8), 0, bpp)
 ##             sf.set_at ((1, 3), (0, 255, 0))
 ##             sf.set_at ((0, 0), (0, 255, 0))
 
 ##             ar = pygame.PixelArray (sf)
 
-##             # Test single value assignments.
+##             # Test single value requests.
 ##             self.assertEqual (ar[1,3], val)
 ##             self.assertEqual (ar[0,0], val)
 ##             self.assertEqual (ar[4,4], val)
 ##             self.assertEqual (len (ar[...,3]), 6)
 
 ##             # Test simple slicing
+##             self.assertEqual (len (ar[:,:]), 6)
 ##             self.assertEqual (len (ar[:,]), 6)
 ##             self.assertEqual (len (ar[1,:]), 8)
 ##             self.assertEqual (len (ar[:,2]), 6)
 ##             ar[3] = 3
 ##             ar[4] = 4
 ##             ar[5] = 5
+            
+##             # We should receive something like [0,2,4]
+##             self.assertEqual (ar[::2,1][0], 0)
+##             self.assertEqual (ar[::2,1][1], 2)
+##             self.assertEqual (ar[::2,1][2], 4)
+##             # We should receive something like [2,2,2]
+##             self.assertEqual (ar[2,::2][0], 2)
+##             self.assertEqual (ar[2,::2][1], 2)
+##             self.assertEqual (ar[2,::2][2], 2)
+            
+##             # Should create a 3x3 array of [0,2,4]
+##             ar2 = ar[::2,::2]
+##             self.assertEqual (len (ar2), 3)
+##             self.assertEqual (ar2[0][0], 0)
+##             self.assertEqual (ar2[0][1], 0)
+##             self.assertEqual (ar2[0][2], 0)
+##             self.assertEqual (ar2[2][0], 4)
+##             self.assertEqual (ar2[2][1], 4)
+##             self.assertEqual (ar2[2][2], 4)
+##             self.assertEqual (ar2[1][0], 2)
+##             self.assertEqual (ar2[2][0], 4)
+##             self.assertEqual (ar2[1][1], 2)
 
-##             print ar, ar[1]
-##             # We should receive something like [0,2,4]
-##             #print ar[::2,0]
-##             #print ar[::2,1]
-##             #print ar[::2,]
-##             #print ar[::2,2]
-##             #self.assertEqual (ar[::2,1][0], 0)
-##             #self.assertEqual (ar[::2,1][1], 2)
-##             #self.assertEqual (ar[::2,1][2], 4)
+##             # Should create a reversed 3x8 array over X of [1,2,3] -> [3,2,1]
+##             ar2 = ar[3:0:-1]
+##             self.assertEqual (len (ar2), 3)
+##             self.assertEqual (ar2[0][0], 3)
+##             self.assertEqual (ar2[0][1], 3)
+##             self.assertEqual (ar2[0][2], 3)
+##             self.assertEqual (ar2[0][7], 3)
+##             self.assertEqual (ar2[2][0], 1)
+##             self.assertEqual (ar2[2][1], 1)
+##             self.assertEqual (ar2[2][2], 1)
+##             self.assertEqual (ar2[2][7], 1)
+##             self.assertEqual (ar2[1][0], 2)
+##             self.assertEqual (ar2[1][1], 2)
+##             # Should completely reverse the array over X -> [5,4,3,2,1,0]
+##             ar2 = ar[::-1]
+##             self.assertEqual (len (ar2), 6)
+##             self.assertEqual (ar2[0][0], 5)
+##             self.assertEqual (ar2[0][1], 5)
+##             self.assertEqual (ar2[0][3], 5)
+##             self.assertEqual (ar2[0][-1], 5)
+##             self.assertEqual (ar2[1][0], 4)
+##             self.assertEqual (ar2[1][1], 4)
+##             self.assertEqual (ar2[1][3], 4)
+##             self.assertEqual (ar2[1][-1], 4)
+##             self.assertEqual (ar2[-1][-1], 0)
+##             self.assertEqual (ar2[-2][-2], 1)
+##             self.assertEqual (ar2[-3][-1], 2)
+
+##             # Test advanced slicing
+##             ar[:] = 0
+##             print "done"
+##             ar2 = ar[:,1]
+##             ar2[:] = 99
+##             print ar
 
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.