380 likes | 690 Views
Graphics. CSE 500 Summer 1998. Graphics in X. X assumes bitmapped graphics The screen is a rectangular array of pixels Drawing consists of setting the values of pixels the array of pixels may have a depth (planes) X provides primitives to draw figures X provides the means to draw text
E N D
Graphics CSE 500 Summer 1998
Graphics in X • X assumes bitmapped graphics • The screen is a rectangular array of pixels • Drawing consists of setting the values of pixels • the array of pixels may have a depth (planes) • X provides primitives to draw figures • X provides the means to draw text • X provides the means to specify images directly.
Drawing • Drawing is to a connection called a Display. • A window is created to hold the drawing • An invisible Pixmap can be created and drawn into instead of a window. • Drawables are windows or pixmaps. • A data structure called a graphics context (GC) specifies the attributed used when drawing.
Steps of drawing • Select the pixels to draw • Apply patterns, color found in GC • Clip the picture by rectangles in GC (according to a clipping rule and mask in GC) • Draw on the screen
The Graphics Context (GC) • The graphics context is a C struct containing information required to render a drawing • There may be several GCs but they should be viewed as limited resources. • Common practice is to create several GC as required and use as required • A single GC can be used and changed.
Tradeoffs • 1 GC and values changed with XChangeGC degrades performance • 2 or more GC use memory on the server (multiple clients make problem worse)
As the workstation draws graphics sent to it by an application program it interprets those primitives using attribute values contained in the specified GC. • Thus every application must have at least one GC resourc and load values to it before it can issue a graphic output request.
Widget drawingArea; Display display; Window w; GC gc,newgc; drawingArea = XmCreateDrawingArea(toplevel,”da”,al,ac); /* a drawing area comes with a gc */ gc = XtGetGC(drawingArea,mask, &values); /* a window (or other drawable) must create a gc */ newgc = XCreateGC(display,window, mask, &values); , creation or copy of a gc supports changing the GC structure by masking out certain fields of the structureand setting new values
Steps in setting up the values structure. • Define an XGCValues structures • set members to the desired value • set the “mask” to reflect the members you are changing. • Create the GC with XCreateGC, XtGetGC • Destroy any unwanted GC with XFreeGC(gc) • Change or copy a GC with XChangeGC or XCopyGC
The XGCValues structure • members of this struct have a name • e.g. unsigned long plane_mask; • int line_width; • a mask name • e.g. GCPlaneMask • GCLineWidth • and a stage in the drawing when it has an effect • e.g. at raster output time • at pixel selection time
The graphics pipeline • Pixel Selection time • The pixels that are to be set are defined • Patterning stage • The pixels set are masked by a pattern • GC clip stage • Pixels are clipped by gc clipping rectangles • Window clip stage • Pixels are clipped by the edges of windows • Raster output stage • Pixels are drawn in the drawable
Pixel Selection Time This stage of the pipeline operates on the output of graphics primitives such as line, arc, polygon, drawing primitives. These primitives generally generate foreground pixels, some generate background pixels, and some both (e.g. a dashed line) Attributes in the associated GC object such as line_width, join_style, etc. effect this stage.
Patterning Stage This stage applies a pattern of values (colors) to foreground and background pixels. The following gc attributes are used in this stage • foreground - “the draw color” sets the color to use for foreground pixels • background - the value to apply to background pixls • stipple - an X resource identifier to use for effects such as hatch-patterns or screen-door patterning • tile - used for repeat patterning • fill-style - selects way to apply patterns ( tiling, hatching, solid fill, etc.)
GC clipping stage • This stage discards all pixels outside a specified region. This can be a clip mask defined in the gc or the result of an XSetClipRectangles request. • The default is no clipping at this stage
Window Clipping Stage • This stage discards all pixels that lie outside the boundaries of the drawable. The only control the programmer has is to create subwindows and set attributes such as • ClipByChildren (default) • IncludeInferiors (draw through subwindows)
Raster Output Stage • This final stage combines the generated values with the values already present. • Two GC attributes control this behavior • function: one of 16 boolean functions of two variables. • plane_mask: a value of 1 causes update a value of 0 leaves the pixel alone.
Changing a GC is like creating Display display; GC gc; unsigned long mask; XGCValues values; Xchange(display,gc,mask,&values);
Important note • If the drawable is a window, a window id must exist. • A window id does not exist until a widget is realized.
Setting and manipulating the GC • Setting and manipulating the GC really means dealing with the XGCValues structure, a struct with over 20 fields. typedef struct { int function; /* GCFunction */ unsigned long plane_mask;/*GCPlaneMask */ unsigned long foreground /*GCForeground*/
The XGCValues Structure • function: as previously noted, this applies to raster output and can be one of 16 boolean values that indicate the logical meaning of new applied to old bits GXClear - the new value is 0 GXSet - the new value is 1 GXxor - xor the new and old values Gxor - or the new and old values … GXcopy - (default) overwrite old with new
The XGCValues Structure • function: a convenience function can be used to alter this without using XChangeGC or creating a new GC XSetFunction(display,gc, GXxor);
The XGCValues Structure • plane_mask the bits set can be set in different planes. The default value is AllPlanes but one can set bits in planes indicated by a 1 in the mask XSetPlaneMask(display, 0x0a); • foreground • background foreground and background are pixel values for the draw color. Default foreground is 0, background in 1
The XGCValues Structure • foreground • background Safest black and white and using a convenience function Display *display GC mygc; XSetForeground(display, mygc, BlackPixel(display), DefaultScreen(display)) XSetBackground(display, mygc, WhitePixel(display), DefaultScreen(display))
The XGCValues Structure The next group of entries in the struct effect line attributes, • int line_width; • int line_style; line styles such as LineSolid, LineOnOffDash, LineDoubleDash are provided line styles such as LineOnOffDash allow one to set 3 pixels on and 3 pixels off, or with LineDoubleDash 3 on 2 off 4 on 2 off, values can be set with the XSetDashes function.
The XGCValues Structure • int cap_style,determines how drawing ends a line. CapNotLast, CapButt,CapRound CapProjecting are values. • int join_style; defines how lines are joined together. JoinMiter, JoinBevel,JoinRound. • and a convenience function XSetLineAttributes(display,gc,line_width,line_style, cap_style,join_style);
The XGCValues Structure • int fill_style; has constants such as FillSolid, FillTiled, FillStippled FillTiled uses the associated field Pixmap tile; FillStippled uses the associated field Pixmap stipple; • int fill_rule addresses the issue of what is the inside of a drawn polygonal figure. Rules such as EvenOddRule, say that if a line from the point to the outside crosses an odd number of lines it is inside. A WindingRule considers a line from test point to each vertex in both counter clockwise and clockwise directions to determine fill.
The XGCValues Structure • int arc_mode; supports filling of arcs drawn as either a ArcPieSlice or ArcChord • Pixmap tile,stipple have been mentioned already as associated with the fill rules. • int ts_x_orgin, ts_y_origin x,y specify the relationship between the picture and the stipple mask or tile mask.. i.e shift the resulting design up or down, right or left.
The XGCValues Structure • Font font; is the default text font to be used. • int subwindow_mode; • Bool graphics_exposures;. are concerned with window clipping and whether exposure events should be generated • int ts_x_orgin, ts_y_origin x,y specify the relationship between the picture and the stipple mask or tile mask.. i.e shift the resulting design up or down, right or left.
The XGCValues Structure • int clip_x_origin, clip_y_origin; are used to define the region for a clipping mask. • Pixmap clipMask; defines the clipping mask • int dash_offset; defines dash behavior, and • char dashes; defines dash appearance
It’s time to draw • Having defined how the graphics pipeline is to behave, X routines provide input to the pipeline. • A Classification of routines is: Points Lines Arc and Circle Outlines Filled Arcs and Circles Rectangle Outlines Filled Rectangles Polygons
Points • XDrawPoint XDrawPoint(display,window,gc,x,y); • XDrawPoints XPoint points[1000]; int npts=1000; for(..) { points[i].x = ; ..} XDrawPoints(display,window,points,npts,CoordModeOrigin);/* relative to window origin */
Lines • XDrawLine XDrawLine(display, window,gc,x1,y1,x2,y2); • XDrawLines XDrawLines(display,window,points,npts, CoordModeOrigin); • XDrawSegment XSegment segments[2]= { {x1,y1,x2,y2}, {u1,v1,u2,v2}}; XDrawSegments(display,window,segments,2);
Arcs and Circle Outlines • Defining an arc: • x,y position of the upper left corner of bounding rectangle • width, height of bounding rectangle • angle1 is the starting angle (in 64th of degrees) of the arc to be drawn • angle 2 is the angle to draw the arc beginning at angle1. (angle1 = 0 and angle2=23040 (64*360) is a closed arc) • XDrawArc XDrawArc(display, window,gc,x,y,width,height angle1,angle2);
Arcs and Circle Outlines • using the Xtypedef struct { short x,y; unsigned short width,height; short angle1,angle2 } XArc; • XDrawArcs XArc arcs[]; XDrawArc(display, window,gc,arcs,narcs);
Filled Arcs • Important to set the mode • ArcPieSlice specifies that the filled arc is to be drawn as a pie slice (default value) • ArcChord fills the arc that is closed b drawing a line from start to end of arc • XSetArcMode(display,gc,ArcChord); XFillArc(display,window,gc,x,y,width, height,angle1,angle2);
Filled Arcs • XFillArcs corresponds to XDrawArcs
Rectangles XDrawRectangle(display,window,gc,x,y, width,height); XDrawFillRectangle(display,window,gc,x,y, width,height); XRectangle r[] XDrawRectangles(display,window,gc,r,nrec); XDrawFillRectangles( . . .)
Polygons • A polygon is defined by lines connecting vertices. XDrawLines can be used to draw polygons. XFillPolygon looks like the XDrawLines call, but it fills the polygon acording to a rule specified in the GC. XSetFillRule(display,gc,WindingRule); XFillPolygon(display,window,gc,points, npoints,shape,mode); • Shape is Convex,NonConvex,Complex, and allows a speedup of filling.