title: user pointer in python
body: <p>*I am trying to display preview from webcam captured using v4l.</p>
  
  <p>Here is an idea of how the code looks like:</p>
  
  <pre><code>from ctypes import *
  from v4l2 import *
  from Image import fromstring
  from Tkinter import Tk, Label
  from ImageTk import PhotoImage
  from ctypes.util import find_library
  
  libc = CDLL(find_library('c'))
  posix_memalign = libc.posix_memalign
  getpagesize = libc.getpagesize
  
  
  device_name = '/dev/video0'
  ps = preview_settings = {
      'width': 320,
      'height': 240,
      'pixformat': 'RGB',
      }
  PIX_FMT = V4L2_PIX_FMT_RGB555
  
  
  preview = Tk()
  image = PhotoImage(ps['pixformat'], (ps['width'], ps['height']))
  label = Label(preview, text='Preview', image=image, width=ps['width'], height=ps['height'])
  label.pack()
  
  
  capability = v4l2_capability()
  size = v4l2_frmsizeenum()
  format = v4l2_format()
  request = v4l2_requestbuffers()
  buffer = v4l2_buffer()
  b_address = c_void_p()
  frame_name_count = '0'
  type = V4L2_BUF_TYPE_VIDEO_CAPTURE
  
  device = open(device_name, 'rw')
  
  ioctl(device, VIDIOC_QUERYCAP, addr(capability))
  
  size.pixel_format = PIX_FMT 
  size.index = 0
  
  format.type = type
  format.fmt.pix.pixelformat = PIX_FMT
  format.fmt.pix.width = size.discrete.width
  format.fmt.pix.height = size.discrete.height
  format.fmt.pix.field = V4L2_FIELD_NONE
  format.fmt.pix.bytesperline = 0
  format.fmt.pix.sizeimage = 0
  
  request.type = type
  request.memory = V4L2_MEMORY_USERPTR
  request.count = 1
  
  ioctl(device, VIDIOC_S_FMT, addr(format))
  
  ioctl(device, VIDIOC_G_FMT, addr(format))
  
  ioctl(device, VIDIOC_REQBUFS, addr(request))
  
  posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
  
  buffer.type = request.type
  buffer.memory = request.memory
  buffer.index = 0
  buffer.m.userptr = b_address.value
  buffer.length = format.fmt.pix.sizeimage
  
  while True:
  
      ioctl(device, VIDIOC_QBUF, addr(buffer))
  
      ioctl(device, VIDIOC_STREAMON, cast(type, c_void_p))
  
      ioctl(device, VIDIOC_DQBUF, addr(buffer))
  
      preview_data = string_at(buffer.m.userptr, buffer.length)
      im = fromstring(ps['pixformat'], (ps['width'], ps['height']), preview_data)
      image.paste(im)
      preview.update()
  </code></pre>
  
  <p><em>and I get</em> <code>ValueError: not enough image data</code></p>
  
  <hr>
  
  <p><em>well, I import</em></p>
  
  <pre><code>c_lib = CDLL(find_library('c'))
  posix_memalign = c_lib.posix_memalign
  getpagesize = c_lib.getpagesize
  </code></pre>
  
  <p><em>and then after</em></p>
  
  <pre><code>ioctl(device, VIDIOC_S_FMT, addr(format))
  ioctl(device, VIDIOC_G_FMT, addr(format))
  </code></pre>
  
  <p><em>and stuff like that, I try to acquire memory</em></p>
  
  <pre><code>posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
  </code></pre>
  
  <p><em>now b_address is no longer = None</em>
  <em>b_address is something like</em> <code>c_void_p(145014784)</code></p>
  
  <p><em>then I start the loop, QBUF, DQBUF, etc..</em></p>
  
  <p><em>the thing is, that when I call pygame.image.frombuffer</em></p>
  
  <pre><code>pg_img = pygame.image.frombuffer(
           buffer.m.userptr,
           (format.fmt.pix.width, format.fmt.pix.height),
           preview_settings['pixformat']
           )
  </code></pre>
  
  <p><em>I get</em> <strong>TypeError: expected a character buffer object</strong></p>