> Also, to access char** data in libgd images (if that is still needed), it
> would be something like this:
>
> from cython cimport view
> cdef int sx, sy
> cdef char **image = image_from_libgd
> cdef char[::view.indirect_contiguous, ::1] image_view = \
> <char[:sy:view.indirect_contiguous, :sx:1]> image
trying this, but I'm getting confused. This is what I now have:
def __array__(self):
"""
Returns a numpy array object with a copy of the data
Note that the array is (height, width) in size, in
keeping with image storage standards (e.g. PIL)
"""
# cdef cnp.ndarray[char, ndim=2, mode='c'] arr
# arr = np.zeros((self.height, self.width), dtype=np.uint8)
# cdef unsigned int row
# ##copy the data, row by row
# for row in range(self.height):
# memcpy( &arr[row, 0], self._image.pixels[row], self.width)
# return arr
# create a cython array for the pixels
pixel_array = view.array(shape=(height, width),
itemsize=sizeof(unsigned char), format="b")
# create a memoryview object to wrap the same memory
cdef unsigned char[:, :] image_view = pixel_array
##copy the data from the image pixel array:
cdef int sx = self.width
cdef int sy = self.height
cdef unsigned char **image = self._image.pixels
<unsigned char[::view.indirect_contiguous, ::1]> image_view = \
<unsigned char[:sy:view.indirect_contiguous, :sx:1]> image
return pixel_array
But Cython is giving me:
Error compiling Cython file:
------------------------------------------------------------
...
cdef int sx = self.width
cdef int sy = self.height
cdef unsigned char **image = self._image.pixels
<unsigned char[::view.indirect_contiguous, ::1]> image_view = \
^
------------------------------------------------------------
py_gd/py_gd.pyx:126:8: Cannot assign to or delete this
Error compiling Cython file:
------------------------------------------------------------
...
cdef int sy = self.height
cdef unsigned char **image = self._image.pixels
<unsigned char[::view.indirect_contiguous, ::1]> image_view = \
<unsigned char[:sy:view.indirect_contiguous, :sx:1]> image
^
------------------------------------------------------------
py_gd/py_gd.pyx:127:70: Pointer base type does not match cython.array base type
Error compiling Cython file:
------------------------------------------------------------
...
cdef int sx = self.width
cdef int sy = self.height
cdef unsigned char **image = self._image.pixels
<unsigned char[::view.indirect_contiguous, ::1]> image_view = \
^
------------------------------------------------------------
py_gd/py_gd.pyx:126:68: Can only create cython.array from pointer or array
so clearly I'm confused. Sturla suggested:
cdef char[::view.indirect_contiguous, ::1] image_view = \
<char[:sy:view.indirect_contiguous, :sx:1]> image
but if I read that right, it's creating a memoryview called
image_view, then assigning it to the image buffer.
But I need a copy -- to a contiguous buffer, which I was trying to
create with the view.array constructor. with the right typecasting,
can I do that as a single assignment?
I'm also having trouble figuring out how to copy the data by loping
through the rows:
cdef int row
##loop through the rows to copy data from pixel buffer to new array
for row in range(sy):
image_view[row, :] = image[row]
Error compiling Cython file:
------------------------------------------------------------
...
cdef int row
##loop through the rows to copy data from pixel buffer to new array
for row in range(sy):
image_view[row, :] = image[row]
^
------------------------------------------------------------
py_gd/py_gd.pyx:129:38: Cannot convert 'unsigned char *' to memoryviewslice
Any hints appreciated...