pixmaps, bitmaps and images how x applications can create, draw, or process rasters,
Post on 20-Dec-2015
221 views
TRANSCRIPT
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.
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
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.
Pixmaps vs Windows
Events cannot originate from pixmaps, only from the windows into which they are drawn.
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.
Pixmaps
Three functions are important: destroy– XFreePixmap(display,pixmap) ;
if an application no longer needs a pixmap
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.
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.
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, … };
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.
An example
/* label_pixmap.c */
#include <Xm/Xm.h>
#include <Xm/Label.h>
#include "btbuxton.xbm"
XtAppContext context;
Widget toplevel, label;
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;
}
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);
/* 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);
/* 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);
}
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);
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);
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)
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.
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.
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);
Tools
Lots of tools to generate bitmaps – bitmap
and to convert formats– bmtoa– atobm
and between other image formats.