Commits

marcus  committed 87a02c3

Fixed sequence assignments for x and y axes.

  • Participants
  • Parent commits 0f1c490

Comments (0)

Files changed (2)

File src/pixelarray.c

     Uint32 x = 0;
     Uint32 y = 0;
     int bpp;
+    int gooverx = 0;
     Uint8 *pixels;
     Uint32 color = 0;
     Uint32 *colorvals = NULL;
         padding = array->padding;
     }
 
-/*
-    printf ("LEN: %d:%d - %d\n", xlen / xstep, ylen / ystep, seqsize);
-*/
     if ((Uint32)seqsize != ylen / ABS (ystep))
     {
         if ((Uint32)seqsize != xlen / ABS (xstep))
             PyErr_SetString(PyExc_ValueError, "sequence size mismatch");
             return -1;
         }
+        gooverx = 1; /* We have to iterate over the x axis. */
     }
-   
+
     if (seqsize == 1)
     {
         /* Single value assignment. */
     colorvals = malloc (sizeof (Uint32) * seqsize);
     if (!colorvals)
     {
-        PyErr_SetString(PyExc_ValueError, "could not copy colors");
+        PyErr_SetString (PyExc_ValueError, "could not copy colors");
         return -1;
     }
 
     switch (bpp)
     {
     case 1:
-        while (posy < ylen)
+        if (gooverx)
         {
-            posx = 0;
-            x = xstart;
-            color = *nextcolor++;
-            while (posx < xlen)
+            while (posy < ylen)
             {
-                *((Uint8 *) pixels + y * padding + x) = (Uint8) color;
-                x += xstep;
-                posx += absxstep;
+                posx = 0;
+                x = xstart;
+                nextcolor = colorvals;
+                while (posx < xlen)
+                {
+                    color = *nextcolor++;
+                    *((Uint8 *) pixels + y * padding + x) = (Uint8) color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
             }
-            y += ystep;
-            posy += absystep;
         }
+        else
+        {
+            while (posy < ylen)
+            {
+                posx = 0;
+                x = xstart;
+                color = *nextcolor++;
+                while (posx < xlen)
+                {
+                    *((Uint8 *) pixels + y * padding + x) = (Uint8) color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
+            }
+        }        
         break;
     case 2:
-        while (posy < ylen)
+        if (gooverx)
         {
-            posx = 0;
-            x = xstart;
-            color = *nextcolor++;
-            while (posx < xlen)
+            while (posy < ylen)
             {
-                *((Uint16 *) (pixels + y * padding) + x) = (Uint16) color;
-                x += xstep;
-                posx += absxstep;
+                posx = 0;
+                x = xstart;
+                nextcolor = colorvals;
+                while (posx < xlen)
+                {
+                    color = *nextcolor++;
+                    *((Uint16 *) (pixels + y * padding) + x) = (Uint16) color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
             }
-            y += ystep;
-            posy += absystep;
+        }
+        else
+        {
+            while (posy < ylen)
+            {
+                posx = 0;
+                x = xstart;
+                color = *nextcolor++;
+                while (posx < xlen)
+                {
+                    *((Uint16 *) (pixels + y * padding) + x) = (Uint16) color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
+            }
         }
         break;
     case 3:
         Uint8 *px;
         SDL_PixelFormat *format = surface->format;
 
-        while (posy < ylen)
+        if (gooverx)
         {
-            posx = 0;
-            x = xstart;
-            color = *nextcolor++;
-            while (posx < xlen)
+            while (posy < ylen)
             {
-                px = ((Uint8 *) (pixels + y * padding) + x * 3);
+                posx = 0;
+                x = xstart;
+                nextcolor = colorvals;
+                while (posx < xlen)
+                {
+                    color = *nextcolor++;
+                    px = ((Uint8 *) (pixels + y * padding) + x * 3);
 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
-                *(px + (format->Rshift >> 3)) = (Uint8) (color >> 16);
-                *(px + (format->Gshift >> 3)) = (Uint8) (color >> 8);
-                *(px + (format->Bshift >> 3)) = (Uint8) color;
+                    *(px + (format->Rshift >> 3)) = (Uint8) (color >> 16);
+                    *(px + (format->Gshift >> 3)) = (Uint8) (color >> 8);
+                    *(px + (format->Bshift >> 3)) = (Uint8) color;
 #else
-                *(px + 2 - (format->Rshift >> 3)) = (Uint8) (color >> 16);
-                *(px + 2 - (format->Gshift >> 3)) = (Uint8) (color >> 8);
-                *(px + 2 - (format->Bshift >> 3)) = (Uint8) color;
+                    *(px + 2 - (format->Rshift >> 3)) = (Uint8) (color >> 16);
+                    *(px + 2 - (format->Gshift >> 3)) = (Uint8) (color >> 8);
+                    *(px + 2 - (format->Bshift >> 3)) = (Uint8) color;
 #endif
-                x += xstep;
-                posx += absxstep;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
             }
-            y += ystep;
-            posy += absystep;
+        }
+        else
+        {
+            while (posy < ylen)
+            {
+                posx = 0;
+                x = xstart;
+                color = *nextcolor++;
+                while (posx < xlen)
+                {
+                    px = ((Uint8 *) (pixels + y * padding) + x * 3);
+#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
+                    *(px + (format->Rshift >> 3)) = (Uint8) (color >> 16);
+                    *(px + (format->Gshift >> 3)) = (Uint8) (color >> 8);
+                    *(px + (format->Bshift >> 3)) = (Uint8) color;
+#else
+                    *(px + 2 - (format->Rshift >> 3)) = (Uint8) (color >> 16);
+                    *(px + 2 - (format->Gshift >> 3)) = (Uint8) (color >> 8);
+                    *(px + 2 - (format->Bshift >> 3)) = (Uint8) color;
+#endif
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
+            }
         }
         break;
     }
     default:
-        while (posy < ylen)
+        if (gooverx)
         {
-            posx = 0;
-            x = xstart;
-            color = *nextcolor++;
-            while (posx < xlen)
+            while (posy < ylen)
             {
-                *((Uint32 *) (pixels + y * padding) + x) = color;
-                x += xstep;
-                posx += absxstep;
+                posx = 0;
+                x = xstart;
+                nextcolor = colorvals;
+                while (posx < xlen)
+                {
+                    color = *nextcolor++;
+                    *((Uint32 *) (pixels + y * padding) + x) = color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
             }
-            y += ystep;
-            posy += absystep;
+        }
+        else
+        {
+            while (posy < ylen)
+            {
+                posx = 0;
+                x = xstart;
+                color = *nextcolor++;
+                while (posx < xlen)
+                {
+                    *((Uint32 *) (pixels + y * padding) + x) = color;
+                    x += xstep;
+                    posx += absxstep;
+                }
+                y += ystep;
+                posy += absystep;
+            }
         }
         break;
     }

File test/pixelarray_test.py

             self.assertEqual (ar[4][0], val)
             self.assertEqual (ar[-1][1], val)
 
-            # Test list assignment.
+            # Test list assignment, this is a vertical assignment.
             val = sf.map_rgb ((0, 255, 0))
             ar.__setslice__ (2, 4, [val] * 8)
             self.assertEqual (ar[2][0], val)
+            self.assertEqual (ar[2][1], val)
+            self.assertEqual (ar[2][4], val)
+            self.assertEqual (ar[2][5], val)
+            self.assertEqual (ar[3][0], val)
             self.assertEqual (ar[3][1], val)
+            self.assertEqual (ar[3][4], val)
+            self.assertEqual (ar[3][5], val)
+
+            # And the horizontal assignment.
+            val = sf.map_rgb ((255, 0, 0))
+            val2 = sf.map_rgb ((128, 0, 255))
+            ar.__setslice__ (0, 2, [val, val2])
+            self.assertEqual (ar[0][0], val)
+            self.assertEqual (ar[1][0], val2)
+            self.assertEqual (ar[0][1], val)
+            self.assertEqual (ar[1][1], val2)
+            self.assertEqual (ar[0][4], val)
+            self.assertEqual (ar[1][4], val2)
+            self.assertEqual (ar[0][5], val)
+            self.assertEqual (ar[1][5], val2)
 
             # Test pixelarray assignment.
             ar[:] = (0, 0, 0)