ctypes, arrays and pointers

R

Richard Jones

Does anyone know how to do the equivalent of this using ctypes?

image_data = malloc(width * height * components);
row_pointers = png_get_rows(png_ptr, info_ptr);
for (y = 0; y < height; y++)
memcpy(&image_data[width * components * y],
row_pointers[height-y-1],
width * components);

That is, I need to get the address of a location *within* an allocated
array. This is what I've got:

image_data = create_string_buffer(width * height * components)
address = addressof(image_data)
row_pointers = libpng.png_get_rows(png_ptr, info_ptr)
for y in xrange(height):
row = string_at(address + width * components * y)
memmove(row, row_pointers[height-y-1], width * components)

but that's not correct. Either the addressof() or string_at() are not
working according to my understanding from the docs, because that code
segfaults. FWIW, I also tried:

row = image_data + width * components * y

but you can't add an int to a c_char_Array_2415600.

Sadly the docs don't seem to be finished :(

http://docs.python.org/dev/lib/ctypes-arrays-pointers.html
 
T

Thomas Heller

Richard said:
Does anyone know how to do the equivalent of this using ctypes?

image_data = malloc(width * height * components);
row_pointers = png_get_rows(png_ptr, info_ptr);
for (y = 0; y < height; y++)
memcpy(&image_data[width * components * y],
row_pointers[height-y-1],
width * components);

That is, I need to get the address of a location *within* an allocated
array. This is what I've got:

image_data = create_string_buffer(width * height * components)
address = addressof(image_data)
row_pointers = libpng.png_get_rows(png_ptr, info_ptr)
for y in xrange(height):
row = string_at(address + width * components * y)
memmove(row, row_pointers[height-y-1], width * components)

but that's not correct. Either the addressof() or string_at() are not
working according to my understanding from the docs, because that code
segfaults. FWIW, I also tried:

row = image_data + width * components * y

but you can't add an int to a c_char_Array_2415600.

This feature request has now come up the second time, so I guess I will
have to find a solution for it. Someone suggested a byref_at(obj, offset) method
which would return a kind of pointer to 'obj' plus offset, and even provided
a patch for it (on the ctypes-users lists, maybe even in the ctypes or Python
SF tracker). Unfortunately that was after the feature freeze for Python 2.5.

This problem wouldn't have been described in the docs anyway, but
keep on bugging me about the docs:

Thomas
 
R

Richard Jones

Thomas said:
This feature request has now come up the second time, so I guess I will
have to find a solution for it. Someone suggested a byref_at(obj, offset)
method which would return a kind of pointer to 'obj' plus offset, and even
provided a patch for it (on the ctypes-users lists, maybe even in the
ctypes or Python SF tracker). Unfortunately that was after the feature
freeze for Python 2.5.

The following will do what I wanted:

def ptr_add(ptr, offset):
address = addressof(ptr.contents) + offset
return pointer(type(ptr.contents).from_address(address))

Thanks to Alex Holkner for the tip.


Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top