Drawing Images in Fltk

To draw images, you can either do it directly from data in your memory, or you can create Fl_Bitmap or Fl_Image or Fl_Pixmap objects. The advantage of drawing directly is that it is more intuitive, and it is faster if the image data changes more often than it is redrawn. The advantage of using the object is that fltk will cache translated forms of the image (on X it uses a server pixmap) and thus redrawing it is much faster.


Direct Image Drawing
#include <FL/fl_draw.H>

It is undefined whether the location or drawing of the image is affected by the current transformation, so you should only call these when it is the identity.

All untyped arguments are integers.

void fl_draw_bitmap(const uchar*, X, Y, W, H, LD = 0);

void fl_draw_image(const uchar*, X, Y, W, H, D = 3, LD = 0);
void fl_draw_image_mono(const uchar*, X, Y, W, H, D = 1, LD = 0);

typedef void (*fl_draw_image_cb)(void*, x, y, w, uchar*);
void fl_draw_image(fl_draw_image_cb, void*, X, Y, W, H, D = 3);
void fl_draw_image_mono(fl_draw_image_cb, void*, X, Y, W, H, D = 1);

int fl_draw_pixmap(char** data, X, Y, Fl_Color=FL_GRAY);

int fl_measure_pixmap(char** data, int &w, int &h);


class Fl_Bitmap
#include <FL/Fl_Bitmap.H>

This object encapsulates the width, height, and bits of an Xbitmap (XBM), and allows you to make an Fl_Widget use a bitmap as a label, or to just draw the bitmap directly. Under X it will create an offscreen pixmap the first time it is drawn, and copy this each subsequent time it is drawn.

Fl_Bitmap(const char *bits, int W, int H);
Fl_Bitmap(const uchar *bits, int W, int H);

Construct from an Xbitmap. The bits pointer is simply copied to the object, so it must point at persistent storage. I provide two constructors because various X implementations disagree about the type of bitmap data. To use an XBM file, #include "foo.xbm", and then do "new Fl_Bitmap(foo_bits,foo_width,foo_height)"

~Fl_Bitmap()

The destructor will destroy any X pixmap created. It does not do anything to the bits data.

void draw(int x, int y, int w, int h, int ox=0, int oy=0);

x,y,w,h indicates a destination rectangle. ox,oy,w,h is a source rectangle. This source rectangle from the bitmap is drawn in the destination. 1 bits are drawn with the current color, 0 bits are unchanged. The source rectangle may extend outside the bitmap (i.e. ox and oy may be negative and w and h may be bigger than the bitmap) and this area is left unchanged.

void draw(int x, int y);

Draws the bitmap with the upper-left corner at x,y. This is the same as doing draw(x,y,this->w,this->h,0,0).

void label(Fl_Widget *);

Change the label() and the labeltype() of the widget to draw the bitmap. 1 bits will be drawn with the labelcolor(), zero bits will be unchanged. You can use the same bitmap for many widgets.


class Fl_Pixmap
#include <FL/Fl_Pixmap.H>

This object encapsulates the data from an XPM image, and allows you to make an Fl_Widget use a pixmap as a label, or to just draw the pixmap directly. Under X it will create an offscreen pixmap the first time it is drawn, and copy this each subsequent time it is drawn.

The current implementation converts the pixmap to 8 bit color data and uses fl_draw_image() to draw it. Thus you will get dithered colors on an 8 bit screen.

Fl_Pixmap(char * const * data);

Construct from XPM data. The data pointer is simply copied to the object, so it must point at persistent storage. To use an XPM file, #include "foo.xpm", and then do "new Fl_Pixmap(foo)"

~Fl_Pixmap()

The destructor will destroy any X pixmap created. It does not do anything to the data.

void draw(int x, int y, int w, int h, int ox=0, int oy=0);

x,y,w,h indicates a destination rectangle. ox,oy,w,h is a source rectangle. This source rectangle is copied to the destination. The source rectangle may extend outside the pixmap (i.e. ox and oy may be negative and w and h may be bigger than the pixmap) and this area is left unchanged.

void draw(int x, int y);

Draws the image with the upper-left corner at x,y. This is the same as doing draw(x,y,this->w,this->h,0,0).

void label(Fl_Widget *);

Change the label() and the labeltype() of the widget to draw the pixmap. You can use the same pixmap for many widgets.


class Fl_Image
#include <FL/Fl_Image.H>

This object encapsulates a full-color RGB image, and allows you to make an Fl_Widget use a Image as a label, or to just draw the Image directly. Under X it will create an offscreen pixmap the first time it is drawn, and copy this each subsequent time it is drawn.

See fl_draw_image() for what happens. On 8 bit screens dithering is used.

Fl_Image(char uchar *data, int W, int H, int D=3, int LD=0);

Construct from a pointer to RGB data. W and H are the size of the image in pixels. D is the delta between pixels (it may be more than 3 to skip alpha or other data, or negative to flip the image left/right). LD is the delta between lines (it may be more than D*W to crop images, or negative to flip the image vertically). The data pointer is simply copied to the object, so it must point at persistent storage.

~Fl_Image()

The destructor will destroy any X pixmap created. It does not do anything to the data.

void draw(int x, int y, int w, int h, int ox=0, int oy=0);

x,y,w,h indicates a destination rectangle. ox,oy,w,h is a source rectangle. This source rectangle is copied to the destination. The source rectangle may extend outside the image (i.e. ox and oy may be negative and w and h may be bigger than the image) and this area is left unchanged.

void draw(int x, int y);

Draws the image with the upper-left corner at x,y. This is the same as doing draw(x,y,this->w,this->h,0,0).

void label(Fl_Widget *);

Change the label() and the labeltype() of the widget to draw the Image. You can use the same Image for many widgets.

(back to contents)