Chapter 2. Working With Images

Table of Contents
Embeddeding Images in an Application
Drawing Images Fom Files
Loading Images Fom Files

Embeddeding Images in an Application

Microwindows comes with a utility program called convbmp. This utility is used to convert Windows™ style bitmap files into C source code. This allows an your application to have a small number of images embedded within its program memory. This method is very useful for small embedded systems with no file system to store images.

This first image example will show you how to embed an image wthin your application. After the first example more examples will be given to show you how to read the same image from a file at run time.

Convert a Bitmap Image to C Source

Create a directory in which to build this example. Then copy the bitmap image of Tux from the Microwindows sources to this directory. Lastly use convbmp to convert the bitmap file to a C source file that we can compile into our application.

Note

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

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

Examine the contents of the file penguin.c that you just created. The file contains three structures. The first static structure is a color palette of up to 256 unique colors that convbmp found within the image. The second static structure is an array of the bits from the image. The last structure is the public MWIMAGEHDR structure that your application will reference. This structure is named image_penguin in this example. convbmp will always use the naming convention "image_" followed by the base filename. In this example we started with penguin.bmp, therefore our image structure name "image_penguin".

Nano-X Client Fixup

The current version of nano-X (0.89pre7) has an arbitrary limit of 10,000 bytes per message that may be sent through the socket from the client to the server. Some functions, such as GrArea() will break up messages that exceed this size into smaller chunks. Unfortunately the function GrDrawImageBits() does not break up large messages into smaller chunks. The penguin bitmap is larger than 10,000 bytes so this example is not going to work as is. You could run the example with a smaller image, or increase the maximum message length. To increase the maximum image length you need to edit the nano-X source file ~/microwin/src/nanox/nxproto.h. Change the line:
#define MAXREQUESTSZ	10000		/* max request size (65532)*/
to:
#define MAXREQUESTSZ	30000		/* max request size (65532)*/
Then rebuild and reinstall the Microwindows package.

Example Drawing Tux I

Copy the source shown below into a file named "image_01.c". 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_01.c penguin.c \
> -I/usr/include/microwin \
> -o image_01 -lnano-X

Example 2-1. image_01.c

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

GR_WINDOW_ID  wid;
GR_GC_ID      gc;

extern GR_IMAGE_HDR  image_penguin;

void event_handler (GR_EVENT *event);

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

    gc = GrNewGC();

    wid = GrNewWindowEx (GR_WM_PROPS_APPFRAME |
                         GR_WM_PROPS_CAPTION  |
                         GR_WM_PROPS_CLOSEBOX,
                         "Tux Window I",
                         GR_ROOT_WINDOW_ID, 50, 50, 
                         image_penguin.width, 
                         image_penguin.height, 
                         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:
        GrDrawImageBits (wid , gc , 0 , 0, &image_penguin);
        break;

    case GR_EVENT_TYPE_CLOSE_REQ:
        GrClose();
        exit (0);
    }
}

Run the example application with the following command. You will see a window appear as shown below.

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

Figure 2-1. Tux I Example

<< Image Object Missing >>