Blame gifti_write_example.py

f172162
from pygiftiio import *
f172162
from ctypes import *
f172162
import numpy
f172162
f172162
# setup
f172162
image = GiftiImage()
f172162
# CTypes provides constructors for pointer-based types.  This is important
f172162
# to the initialization of the various members of the structs we need
f172162
# to write via the library.
f172162
image.version = c_char_p("version_string")
f172162
image.darray = POINTER(POINTER(GiftiDataArray))()
f172162
image.data = c_void_p(None)
f172162
image.numDA = c_int(0)
f172162
image.swapped = c_int(0)
f172162
image.compressed = c_int(0)
f172162
f172162
# type(im) = GiftiImage
f172162
# type(ver) = str
f172162
def set_version(im, ver):
f172162
    image.version = c_char_p(ver)
f172162
f172162
# type(im) = GiftiImage
f172162
# type(numDA) = int
f172162
def set_numDA(im, numDA):
f172162
    im.numDA = c_int(numDA)
f172162
f172162
# type(im) = GiftiImage
f172162
# type(md) = GiftiMetaData
f172162
def set_meta_data(im, md):
f172162
    im.meta = md
f172162
f172162
# type(im) = GiftiImage
f172162
# type(da) = GiftiDataArray
f172162
def add_data_array(im, da):
f172162
    cur_numda = im.numDA
f172162
f172162
    # Create a pointer to the new dataarray
f172162
    da_ptr = pointer(da.data)
f172162
    # Grab the pointer to the image's dataarrays
f172162
    ptr = image.darray
f172162
f172162
    # Create a new dataarray array pointer
f172162
    ar = (POINTER(GiftiDataArray)*(cur_numda+1))()
f172162
    # We need to cast the resulting pointer for use by C
f172162
    ar = cast(ar, POINTER(POINTER(GiftiDataArray)))
f172162
f172162
    # Copy all of the current da's to the new array.  This just copies the pointers!
f172162
    for i in xrange(num_da):
f172162
        ar[i] = im.darray[i]
f172162
f172162
    # Add the new data array to the image's data
f172162
    ar[num_da] = da_ptr
f172162
f172162
    # Reassign the pointer
f172162
    im.darray = ar
f172162
f172162
    # Tell the image it has an extra DA now
f172162
    cur_numda += 1
f172162
    im.numDA = c_int(cur_numda)
f172162
f172162
# type(da) = GiftiDataArray
f172162
# type(axis) = int
f172162
# type(val) = int
f172162
def set_da_dim(da, axis, val):
f172162
    # Simple setter.  However, the axis variable here is a
f172162
    # python array index (as the da dims is an array)
f172162
    # To properly assign the value, the val variable must
f172162
    # be usable by C, so we must form a c_int type.
f172162
    # This is true for all Setters.
f172162
    da.dims[axis] = c_int(val)
f172162
f172162
# type(filename) = string
f172162
# type(im) = GiftiImage
f172162
def write_image(filename, im):
f172162
    return gifti_write_image, im, filename, 1)