472,347 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

ctypes, arrays and pointers

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/ctype...-pointers.html
Oct 4 '06 #1
2 3567
Richard Jones schrieb:
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.
Sadly the docs don't seem to be finished :(

http://docs.python.org/dev/lib/ctype...-pointers.html

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

Thomas

Oct 6 '06 #2
Thomas Heller wrote:
Richard Jones schrieb:
> 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.
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

Oct 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for...
3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. ...
3
by: rubbishemail | last post by:
Hello, i've got a problem with pointers in the following function which i want to use: I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: ...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def...
3
by: p.lavarre | last post by:
Subject: Python CTypes translation of (pv != NULL) And so then the next related Faq is: Q: How should I test for ((void *) -1)? A: (pv ==...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For...
3
by: Ron Garret | last post by:
CTypes on a 64-bit machine appears to be truncating pointers to 32 bits: $ uname -a Linux monster1 2.6.18-6-amd64 #1 SMP Mon Jun 16 22:30:01 UTC...
2
by: Robert Kern | last post by:
dcharno wrote: Well, stack alignment would be a problem with how the shared library gets compiled, nothing to do with ctypes (I think). However,...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.