ctypes, arrays and pointers

Richard Jones richardjones at optushome.com.au
Wed Oct 4 01:12:58 EDT 2006


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





More information about the Python-list mailing list