title: Live Image from camera in GTK
body: <p>First: <br />
  I am working on a dark-field microscopic tracking program. This program is old (2012), we got a new camera but this camera can´t be captured with a capture program. So I need to write a &quot;capture program&quot; for this camera with the SDK (Software development kit) from the company of the camera.</p>
  <p>The tracking program is writen in C and GTK 2, so I would like to have the &quot;capture program&quot; in C and GTK 2 too, so I can just put the &quot;capture program&quot; in my tracking program. I can take a picture inside the &quot;capture program&quot; and show it as <code>gtk_image_new_from_pixbuf</code>.</p>
  <p>Problem: <br />
  After being able to take pictures I found out that the dark-field microscope is not able to let you set the focus for the camera with your eyes. So now I need to capture a &quot;live image&quot;, video or how ever you want to call it. <br />
  My idea was to use a timer so the camera will take a picture every second and display it as an image so the program has to delete the old image and show the new one.
  But that doesn´t happen I am pretty sure that there is no problem with the camera, because it shows the first image. My guess is that the pixbuf isn´t cleared and that causes that there is no new image shown.<br />
  The &quot;capture program&quot; doesn´t have to stay as a C/Gtk2 program but as I am not a computer scientist I just have no idea how to get the images into the tracking program if they are different. <br /></p>
  <p>Code: <br />
  I know that <code>gdk_pixbuf_new_from_data</code> has a starde of <code>width*1</code> and not <code>width*3</code> that is no problem.</p>
  <pre><code>#include &lt;gtk/gtk.h&gt;
  #include &lt;stdlib.h&gt;
  
  unsigned short *buffer = NULL;
  long imageSize = 0;
  
  void draw_call (GtkWidget *window, GdkEvent *event, gpointer data){
  
      (void)event; (void)data;
      GdkPixbuf *pix;
      GtkWidget* image;
      image = gtk_image_new();
  
      free (buffer);
      long result = NOERR;
  
  
  //camera commands had to be changed 
      result = getImageSizeFromCamera( &amp;imageSize);
  
      result = SetBitsPerPixel(8);
  
      buffer = (unsigned short *) malloc( imageSize*2);
  
      result = takePicture(camera, buffer, imageSize/2, NULL, NULL);
  
      result = stopTakingPicture (camera);
  //end of camera commands
  
      pix = gdk_pixbuf_new_from_data ((unsigned int *) buffer, GDK_COLORSPACE_RGB,FALSE, 8,
                 1936, 1460, 1936*1, pixbuf_free, NULL);
  
      image = gtk_image_new_from_pixbuf(pix);
  
  
      return image;
  gtk_widget_unref (pix);
  }
  
  void pixbuf_free(guchar *pixels, gpointer data)
  {
      g_free(pixels) ;
      fprintf(stderr, &quot;pixbuf data freed\n&quot;) ;
  }
  
  int main (int argc, char *argv[])
  {
  
      GdkPixbuf *pixbuf;
  
  
      // GTK
  
      GtkWidget *window;
      GtkWidget* image;
  
      image = gtk_image_new();
  
  
      gtk_init (&amp;argc, &amp;argv);
  
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  
  
      gtk_window_set_title (GTK_WINDOW (window), &quot;Image Viewer&quot;);
  
      g_signal_connect (window, &quot;destroy&quot;, G_CALLBACK (Deinit), NULL);
      g_signal_connect (window, &quot;expose-event&quot;,G_CALLBACK (draw_call), NULL);
  
  
      g_timeout_add (1000, draw_call, NULL);
  
      gtk_widget_set_app_paintable(window, TRUE);
      gtk_widget_set_double_buffered(window, FALSE);
  
  
      gtk_container_add(GTK_CONTAINER (window), image);
  
      gtk_widget_show_all (window);
  
      gtk_main ();
  
  
      return 0;
  }
  
  </code></pre>