![](https://secure.gravatar.com/avatar/fcc1e9dd91d747f8cc87ebae8d7bc7a3.jpg?s=120&d=mm&r=g)
in the pygame project i've had the ability to map images into Numeric data arrays onto image pixel data. This has worked excellently for me, but in the near future i'd also like to support numarray. early in numarray's development it looked like this was not going to be possible at all. i've been following numarray loosely, and it sure looks like things have 'loosened' up a bit. still there are a few things i am doing that i'm unsure if numarray is ready to handle yet? what is going to make this tricky is i'm doing a bit of 'attribute mangling' to the Numeric array structure. this is necessary as the image data is extremely 'non-flat'. also, since i am referencing data held in another python object, i need to make sure the array holds a reference to the original object. these are the things i'm afraid i'll be stuck on. here is pretty much what i am doing now, simplified quite a bit... PyObject* pixelsarray(SDL_Surface *surf) { int dim[3]; PyObject *array; dim[0] = surf->w; dim[1] = surf->h; dim[2] = 3; array = PyArray_FromDimsAndData(3, dim, PyArray_UBYTE, surf->pixels); if(array) { PyArrayObject *a = (PyArrayObject*)array a->flags = OWN_DIMENSIONS|OWN_STRIDES; a->strides[2] = 1; a->strides[1] = surf->pitch; a->strides[0] = surf->format->BytesPerPixel; a->base = _pyobject_to_surf_; } return array; } note that depending on pixel packing and endianess, the strides[2] can become -1. smiley. also pretend a->base is pointing to a real python object, which it does in the real version. there is likely a way to workaround the "base" requirement with weakrefs i suppose, but i'd rather not jump through the extra hoops. the real necessity is setting the strides how i want. i didn't see any array creation functions that allow me to pick my own strides. once i create the array here i never change any of the array attributes. if this looks doable then it's time for me to sit down with the numarray docs and see what new and exciting things await me :]
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
On Sun, 2003-06-08 at 14:38, Pete Shinners wrote:
Despite old documentation to the contrary, the strides array is now writable. The -1 is more problematic. Anytime a ufunc is called, there is "safety code" which evaluates the shape, strides, byteoffset, buffer base and buffer size to ensure that the ufunc won't attempt an access outside the buffer or generate a misaligned pointer. negative strides are possible *provided* that the byteoffset is chosen so that everything works out.
also pretend a->base is pointing to a real python object, which it does in the real version.
numarray has a "base" pointer which is currently unused and intended for the purpose of backward compatibility. Since numarray does not yet use the base pointer at all, it does not deallocate it at array destruction time.
New in numarray-0.5 is the API function NA_NewAllStrides which allows you to pass in a pointer to a strides array. NA_NewAllStrides does not currently check the validity of the strides array.
It looks close to doable to me. There are probably still a few loose ends. Todd -- Todd Miller jmiller@stsci.edu STSCI / ESS / SSB
![](https://secure.gravatar.com/avatar/faf9400121dca9940496a7473b1d8179.jpg?s=120&d=mm&r=g)
On Sun, 2003-06-08 at 14:38, Pete Shinners wrote:
Despite old documentation to the contrary, the strides array is now writable. The -1 is more problematic. Anytime a ufunc is called, there is "safety code" which evaluates the shape, strides, byteoffset, buffer base and buffer size to ensure that the ufunc won't attempt an access outside the buffer or generate a misaligned pointer. negative strides are possible *provided* that the byteoffset is chosen so that everything works out.
also pretend a->base is pointing to a real python object, which it does in the real version.
numarray has a "base" pointer which is currently unused and intended for the purpose of backward compatibility. Since numarray does not yet use the base pointer at all, it does not deallocate it at array destruction time.
New in numarray-0.5 is the API function NA_NewAllStrides which allows you to pass in a pointer to a strides array. NA_NewAllStrides does not currently check the validity of the strides array.
It looks close to doable to me. There are probably still a few loose ends. Todd -- Todd Miller jmiller@stsci.edu STSCI / ESS / SSB
participants (2)
-
Pete Shinners
-
Todd Miller