23
Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps, Bitmaps and Images

How X applications can create, draw, or process rasters,

Page 2: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Rasters

Rasters are rectangular areas of pixels. X represents rasters as– Pixmaps : X resources on which one can draw.– Bitmaps :

pixmaps with values of 1 bit Text files defining an array of bits

– Images : data structures that can be drawn into windows or pixmaps.

Page 3: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps

A pixmap is a drawable that has– width and height– depth– an associated screen

A pixmap is like a window – coordinates with (0,0) being upper left– sizes expressed in pixels

Page 4: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps and Windows

Pixmaps differ from windows – Some procedures do not apply to pixmaps

XClearArea( ) does not clear a pixmap but instead to clear an area one uses XFillRectangle( ) to set all pixels to aknown value.

Pixmaps are drawn into windows, not mapped to the screen.

– Pixmaps have no borders.– Pixmaps do not belong to a widget hierarchy as do

windows.

Page 5: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps vs Windows

Events cannot originate from pixmaps, only from the windows into which they are drawn.

Page 6: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps

Three functions are important: create– XCreatePixmap( Display* display, – Drawable screen,– unsigned int width,– unsigned int height,– unsigned int depth);

The screen parameter must represent some existing Drawable, a window or pixmap.

Page 7: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps

Three functions are important: destroy– XFreePixmap(display,pixmap) ;

if an application no longer needs a pixmap

Page 8: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Pixmaps Three functions are important: inquire

– Status XGetGeometry(Display* display,– Drawable drawable,– Window *root,– int *x, int *y,– unsigned int *border_width,– int *depth)

the pointers return the information and is useful when the drawable is a pixmap.

Page 9: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Bitmaps

A bitmap is a Pixmap with depth 1

or a text file containing ASCII information

that defines an array of bits. XReadBitmapFile ( ) to create a bitmap

from a file XWriteBitmapFile( ) to write a bitmap file.

Page 10: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Bitmap file format

#define arrow_width 16

#define arrow_height 16

#define arrow_x_hot 12

#define arrow_y_hot 1

static char arrow_bits[ ] = {

0x00, 0x00, 0x00, 0x10, … };

Page 11: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Reading the bitmap file

int status;

status =XReadBitmapFile(display,drawable,

filename, &width,&height,

&pixmap, &x_hot, &y_hot);

which creates a bitmap and returns it in the pixmap parameter.

Page 12: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

An example

/* label_pixmap.c */

#include <Xm/Xm.h>

#include <Xm/Label.h>

#include "btbuxton.xbm"

XtAppContext context;

Widget toplevel, label;

Page 13: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

An example

unsigned int get_depth(Widget w)

/* gets the depth of the display holding w. */

{ Window r;

unsigned int x,y,wd,ht,bw,depth;

XGetGeometry(XtDisplay(w),

RootWindowOfScreen(XtScreen(toplevel)),

&r,&x,&y,&wd,&ht,&bw,&depth);

return depth;

}

Page 14: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

An example

main(int argc,char *argv) {

Arg al[10];

int ac;

int foreground,background;

Pixmap pix;

unsigned int depth;

toplevel = XtAppInitialize(&context,

"",NULL,0,&argc,argv,NULL,NULL,0);

/* create the label */

ac=0;

label=XmCreateLabel(toplevel,"label",al,ac);

XtManageChild(label);

Page 15: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

/* get colors of label */

ac=0;

XtSetArg(al[ac], XmNforeground, &foreground); ac++;

XtSetArg(al[ac], XmNbackground, &background); ac++;

XtGetValues(label, al, ac);

/* get the depth so pixmap can be created. */

depth=get_depth(toplevel);

/* create the pixmap */

pix=XCreatePixmapFromBitmapData(XtDisplay(toplevel),

RootWindowOfScreen(XtScreen(toplevel)),

btbuxton_bits,btbuxton_width,btbuxton_height,

foreground,background,depth);

Page 16: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

/* set appropriate label resources to

* attach the pixmap to the label

*/

ac=0;

XtSetArg(al[ac], XmNlabelType, XmPIXMAP); ac++;

XtSetArg(al[ac], XmNlabelPixmap, pix); ac++;

XtSetArg(al[ac], XmNshadowThickness, 4); ac++;

XtSetValues(label, al, ac);

XtRealizeWidget(toplevel);

XtAppMainLoop(context);

}

Page 17: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Another Example: Pixmap to Window./* get the current fg and bg colors. */

ac=0;

XtSetArg(al[ac], XmNforeground, &fc); ac++;

XtSetArg(al[ac], XmNbackground, &bc); ac++;

XtGetValues(drawing_area, al, ac);

depth=get_depth(drawing_area);

p=XCreatePixmapFromBitmapData(

XtDisplay(drawing_area),

XtWindow(drawing_area),btbuxton_bits,

btbuxton_width,btbuxton_height,

fc, bc, depth);

Page 18: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Another Example: Pixmap to Window./* Draw the Pixmap in the drawing area */

XCopyArea(XtDisplay(drawing_area),

p,XtWindow(drawing_area),gc,0,0,

btbuxton_width, btbuxton_height, 100,100 );

/* Free up resources no longer needed */

XFreePixmap(XtDisplay(drawing_area),p);

Page 19: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Images

X provides support for image manipulation Images are typically large

– 512x512 pixel image with 8 bit pixels = .25M Images must be communicated to the client so go

through the X protocol (network?) and are thus slow.

Xlib must translate for different forms of clients (for example big endian image and little endian clients)

Page 20: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Images

Color maps are a problem. Many times one must translate pixel values from the image into values obtained from color cell allocation before the image is displayed.

Page 21: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

XImages

XImage is an X datatype (struct) accompanied by formats – XYBitMap image format– XYPixMap image format– ZPixmap image format (organized as an array

of pixel values, called pixel mode images) input/output is binary i/o of objects of this

type.

Page 22: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Example lines of code

format = XYPixmap;

bitmap_pad = 32;

image = XCreateImage( display,

DefaultVisual(display,screen), depth,

format,0,0,width,height,bitmap_pad,0);

newimage = XSubImage(image,x,y,width,height);

pixel = XGetPixel(image, x,y);

status = XPutPixel(image,x,y,pixel);

XPutImage(display,drawable,gc,image,src_x,src_y,dest_x

dest_y,width,height);

Page 23: Pixmaps, Bitmaps and Images How X applications can create, draw, or process rasters,

Tools

Lots of tools to generate bitmaps – bitmap

and to convert formats– bmtoa– atobm

and between other image formats.