473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3612
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 Python 2.3 and higher. ctypes allows to call...
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. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
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 *Value) The function is supposed to read out...
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: session : the current session classId :...
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 c_not_null(pv): return (ctypes.cast(pv,...
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 == 0xffffFFFF) works often.
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 example: ..... StringVector = c_char_p *...
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 2008 x86_64 GNU/Linux $ cat foo.c void*...
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, if you are passing in arrays from ctypes, *they*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.