Hi Chris,
thanks for your help.
Yeah, you're right, it works for me that way now (more below).
>> I'm just struggling to move it into a cython-native memoryview now.
>>
> I tried the following, which works, but leaves me with a 1d view:
>> cdef int[:] view = <int[:sizeof(int) * width * height]>buf
>>
>
> did you try simply:
>
> cdef int[:,:] view = <int[:sizeof(int) * width * height]>buf
>
> that should give you a 2D memoryview, just like it does when you use an
> array.array (which are 1D as well)
>
>
I believe I tried this earlier and I got a compilation error, but
thanks to your hint, I finally found a solution:
cdef int[:, ::1] = <int[:width, :height]>buf
Since this command just sets up a memory view on top of the already
existing memory, I needed to use copy() on it to get a self-managed
memory view out of it, because I want to free the original buffer
right afterwards, since I can't know when the user actually trashes
the memory view and the gc recycles it.
Or is there any way to give a function to the view which automatically
frees the memory when the last handle gets released, so I don't need
to copy all of the data again but just write a simple function
instead?
>> Numpy would now allow me to simply reshape the 1d view to get a 2d
>> view, just as I need,
>
>
> when you reshape a numpy array, all yu are doing is chanign the strides,
> the memory buffer is not effected, it's not really doing much.
>
> if you want a memoryview with a different shape pointing ot the same
> buffer, you can simply make a new one.
>
> Does anyone have an idea how to solve this? Maybe there already is a
>> way to read the 1d buffer in as a 2d view,
>
>
> try the above -- there is no such thing as a "1D buffer" a buffer is always
> 1D (or non-dimensional, depending on how you want to think about it -- it's
> just a block of allocated memory. The whole point of a memoryview it to
> provide a way to work with a block of memory wit a bit more "smarts"
>
> -CHB
Thanks. With a bit of optimization, including this solution above, I
already reduced the processing time of a 1280 x 960 pgm file with 4
megabytes size from 0.6 seconds down to 73 msecs, which is absolutely
enough for my current situation.
Thanks again.
Best Regards.
Toni
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
cython-users...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.
>