Commits

Anonymous committed cbb061b

Fixed array assignments in PixelArray.

Comments (0)

Files changed (2)

     PyPixelArray *val)
 {
     SDL_Surface *surface;
-    SDL_Surface *valsf;
+    SDL_Surface *valsf = NULL;
     Uint32 x;
     Uint32 y;
     Uint32 vx;
     surface = PySurface_AsSurface (array->surface);
     GET_SLICE_VALS (array, start, end, ylen, ystep, xlen, xstep, padding,
         low, high, 1, surface->pitch);
-    
+
     if (val->ylen / val->ystep != ylen / ystep ||
         val->xlen / val->xstep != xlen / xstep)
     {
 
     if (bpp != valbpp)
     {
-        /* bpp do not match. */
-        /* TODO */
+        /* bpp do not match. We cannot guarantee that the padding and co
+         * would be set correctly. */
         PyErr_SetString (PyExc_ValueError, "bit depths do not match");
         return -1;
     }
 
     vx = 0;
     vy = 0;
+
     /* Single value assignment. */
     switch (bpp)
     {
     case 1:
         for (y = 0; y < ylen; y += ystep)
         {
-            vy += val->ystep;
             vx = 0;
             for (x = 0; x < xlen; x += xstep)
             {
                 offset = start + x;
-                vx += val->xstep;
 
                 *((Uint8 *) pixels + y * padding + offset) =
                     (Uint8)*((Uint8 *)
                         valsf->pixels + vy * val->padding + val->start + vx);
+                vx += val->xstep;
             }
+            vy += val->ystep;
         }
         break;
     case 2:
         for (y = 0; y < ylen; y += ystep)
         {
-            vy += val->ystep;
             vx = 0;
             for (x = 0; x < xlen; x += xstep)
             {
                 offset = start + x;
-                vx += val->xstep;
 
                 *((Uint16 *) (pixels + y * padding) + offset) =
                     (Uint16)*((Uint16 *)
-                        ((Uint8*)valsf->pixels + vy * val->padding) + val->start + vy);
+                        ((Uint8*)valsf->pixels + vy * val->padding) + val->start + vx);
+                vx += val->xstep;
             }
+            vy += val->ystep;
         }
         break;
     case 3:
         SDL_PixelFormat *vformat = valsf->format;
         for (y = 0; y < ylen; y += ystep)
         {
-            vy += val->ystep;
             vx = 0;
             for (x = 0; x < xlen; x += xstep)
             {
                 offset = start + x;
-                vx += val->xstep;
 
                 px = (Uint8 *) (pixels + y * padding) + offset * 3;
                 vpx = (Uint8 *) ((Uint8*)valsf->pixels + y * val->padding) +
                 *(px + 2 - (format->Bshift >> 3)) =
                     *(vpx + 2 - (vformat->Bshift >> 3));
 #endif
+                vx += val->xstep;
             }
+            vy += val->ystep;
         }
         break;
     }
     default:
         for (y = 0; y < ylen; y += ystep)
         {
-            vy += val->ystep;
             vx = 0;
             for (x = 0; x < xlen; x += xstep)
             {
                 offset = start + x;
-                vx += val->xstep;
 
                 *((Uint32 *) (pixels + y * padding) + offset) =
                     *((Uint32 *)
                         ((Uint8*)valsf->pixels + y * val->padding) + val->start + vx);
+                vx += val->xstep;
             }
+            vy += val->ystep;
         }
         break;
     }
             high = array->ylen;
     }
 
-    for (i = low; i < high; i++)
+    if (PyPixelArray_Check (value))
     {
-        val = _pxarray_ass_item (array, i, value);
-        if (val != 0)
-            return val;
+        return _array_assign_array (array, low, high, (PyPixelArray *) value);
     }
-
+    else
+    {
+        for (i = low; i < high; i++)
+        {
+            val = _pxarray_ass_item (array, i, value);
+            if (val != 0)
+                return val;
+        }
+    }
     return 0;
 }
 

test/pixelarray_test.py

 
     def test_set_slice (self):
         for bpp in (8, 16, 24, 32):
-            sf = pygame.Surface ((6, 8), 0, 32)
+            sf = pygame.Surface ((6, 8), 0, bpp)
             sf.fill ((0, 0, 0))
             ar = pygame.PixelArray (sf)
 
             self.assertEqual (ar[2][0], val)
             self.assertEqual (ar[3][1], val)
 
+            # Test pixelarray assignment.
+            ar[:] = (0, 0, 0)
+            sf2 = pygame.Surface ((6, 8), 0, bpp)
+            sf2.fill ((255, 0, 255))
+
+            val = sf.map_rgb ((255, 0, 255))
+            ar2 = pygame.PixelArray (sf2)
+
+            ar[:] = ar2[:]
+            self.assertEqual (ar[0][0], val)
+            self.assertEqual (ar[5][7], val)
+
 if __name__ == '__main__':
     unittest.main()