Loading Images Fom Files

The nano-X API contains a function, GrLoadImageFromFile(), which will read an image from a file into the nano-X server's memory and return a GR_IMAGE_ID ID. With this ID the image can be drawn to a window at a later time using the GrDrawImageToFit() function. Multiple image formats (GIF, JPEG, BMP, PNG, XPM, PBM, PGM and PPM) are supported by GrLoadImageFromFile(). The image type is automatically determined when the file is read.

Just as with the GrDrawImageFromFile() function the image file must reside within the nano-X server's file system. The client application just passes the filename to the server then the server reads the file. This is no problem as long as the the client and server are on the same machine. Another thing to be aware of is that since the server is opening the file, all relative paths in the image file name are relative to the nano-X server's current directory rather than the client's current working directory.

The following example shows how to display Tux as an image loaded from file at run time. This approach is a little quicker than the previous example since the program does not go out to disk during each exposure event.

Example Drawing Tux III

Create a directory in which to build this example. Then copy the bitmap image of Tux from the Microwindows sources to this directory. Also copy the example source shown below into a file named "image_03.c".

Note

In this example assume that Microwindows is installed to ~/microwin and the examples are built in ~/mymw/ex_image_03. If you have different locations then you will have to modify the paths used in the example accordingly.

$ cd ~/mymw/ex_image_03
$ cp ~/microwin/src/mwin/bmp/penguin.bmp penguin.bmp

Compile the application with the following command.

Note

If you did not install Microwindows you will need to change the path /usr/include/microwin so that it points to the include directory where you extracted the Microwindows source.

$ gcc image_03.c \
> -I/usr/include/microwin \
> -o image_03 -lnano-X

Example 2-3. image_03.c

 
#include <stdio.h>
#define MWINCLUDECOLORS
#include "microwin/nano-X.h"

GR_WINDOW_ID  wid;
GR_GC_ID      gc;
GR_IMAGE_ID   image;

void event_handler (GR_EVENT *event);

int main (void)
{
     if (GrOpen() < 0)
     {
          fprintf (stderr, "GrOpen failed");
          exit (1);
     }

     image = GrLoadImageFromFile ("penguin.bmp", 0);

     gc = GrNewGC();

     wid = GrNewWindowEx (GR_WM_PROPS_APPFRAME |
                          GR_WM_PROPS_CAPTION  |
                          GR_WM_PROPS_CLOSEBOX,
                          "Tux Window III",
                          GR_ROOT_WINDOW_ID, 50, 50, 
                          100, 200,
                          WHITE);

     GrSelectEvents (wid, GR_EVENT_MASK_EXPOSURE | 
                          GR_EVENT_MASK_CLOSE_REQ);

     GrMapWindow (wid);
     GrMainLoop (event_handler);
     return 0;
}

void event_handler (GR_EVENT *event)
{
     switch (event->type)
     {
     case GR_EVENT_TYPE_EXPOSURE:
     {
          GR_WINDOW_INFO info;

          GrGetWindowInfo (wid, &info);
          GrDrawImageToFit (wid, gc, 0, 0, 
                            info.width, info.height, 
                            image);
          break;
     }
     case GR_EVENT_TYPE_CLOSE_REQ:
          GrClose();
          exit (0);
     }
}

Run the example application with the following command.

$ nano-X& sleep 1; nanowm& sleep 1; ./image_03&