views:

1230

answers:

6

I'm quite comfortable with C for numerical computation, but not for graphical programming. I have a Nx by Ny by 3 RGB matrix in a command-line program for linux (gcc, ubuntu) and I want to pop up a window with it as an image. What's the easiest way to do this? It has to be relatively quick because I'll be updating the image frequently.

Thanks

A: 

Draw the image to a pixmap, create an X window (or allocate a framebuffer), and write the pixmap to the window.

Or, look at ImageMagick's implementation of the display command.

If you don't mind learning GTK+, then that might be the most straightforward way. Create a window and a drawable, and make the drawable a child of the window. Create a pixmap of your image and draw it to the drawable. Cairo will simplify this, since you'll only need to tweak your image format to make it work.

greyfade
Qt is probably easier than GTK for a beginner to graphics programming... it's written in C++ but I think it has C bindings.
David Zaslavsky
+1  A: 

The C language itself doesn't have any built-in graphics capability. You can use a graphics toolkit like Qt, gtk, wxWidgets, etc. You could also construct an image file (BMP is pretty simple), and then launch (using fork and exec) an application to view it. Ubuntu uses gnome by default, you could fork "gnome-open" with a command-line argument that is the name of the file you created, and gnome-open will then launch whichever application is associated with the file type.

KeyserSoze
+1  A: 

It sounds like maybe you're doing some scientific computing and just need a quick way to display results.

Something like gnuplot may give you better mileage than learning a generic windowing toolkit. It's a general plotting utility and specifically geared towards displaying data sets.

+2  A: 

You could try using image magick it seems to have a function for displaying an image to an x window with a minimal amount of work. I have used image magick in the past and have been pleased with it. I have not tried this specific function though.

Display function

fuzzy-waffle
Seconded. ImageMagick is probably the easiest way to do this from C.
Benson
+2  A: 

Here is how to display and image with Gtk2 using C. You can find more information (including a tutorial) on the Gtk2 site.

#include <gtk/gtk.h>

void destroy(void) {
  gtk_main_quit();
}

int main (int argc, char** argv) {
  GtkWidget* window;
  GtkWidget* image;

  gtk_init (&argc, &argv);


  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  image  = gtk_image_new_from_file(argv[1]);

  gtk_signal_connect(GTK_OBJECT (window), "destroy",
          GTK_SIGNAL_FUNC (destroy), NULL);

  gtk_container_add(GTK_CONTAINER (window), image);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

Here is how to compile it:

gcc -Wall img.c -o img `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
Chas. Owens
A: 

The cairo graphics library is also a good way to go, see http://cairographics.org/ . It's cross platform, fairly efficient, and will let you work with Gnome/KDE/Windows or just generating image files fairly simply. It's designed for vectors, so also handles clipping, fonts and scaling well.

Anthony Towns