1 a0d146ed 2005-07-12 devnull #include "stdinc.h"
2 a0d146ed 2005-07-12 devnull #include "dat.h"
3 a0d146ed 2005-07-12 devnull #include "fns.h"
8 a0d146ed 2005-07-12 devnull Bottom = 1,
9 a0d146ed 2005-07-12 devnull Left = 40,
10 a0d146ed 2005-07-12 devnull Right = 0,
11 a0d146ed 2005-07-12 devnull MinWidth = Left+Right+2,
12 a0d146ed 2005-07-12 devnull MinHeight = Top+Bottom+2,
13 a0d146ed 2005-07-12 devnull DefaultWidth = Left+Right+500,
14 a0d146ed 2005-07-12 devnull DefaultHeight = Top+Bottom+40
17 a0d146ed 2005-07-12 devnull QLock memdrawlock;
18 a0d146ed 2005-07-12 devnull static Memsubfont *smallfont;
19 a0d146ed 2005-07-12 devnull static Memimage *black;
20 a0d146ed 2005-07-12 devnull static Memimage *blue;
21 a0d146ed 2005-07-12 devnull static Memimage *red;
22 a0d146ed 2005-07-12 devnull static Memimage *lofill[6];
23 a0d146ed 2005-07-12 devnull static Memimage *hifill[6];
24 a0d146ed 2005-07-12 devnull static Memimage *grid;
26 a0d146ed 2005-07-12 devnull static ulong fill[] = {
27 a0d146ed 2005-07-12 devnull 0xFFAAAAFF, 0xBB5D5DFF, /* peach */
28 a0d146ed 2005-07-12 devnull DPalegreygreen, DPurpleblue, /* aqua */
29 a0d146ed 2005-07-12 devnull DDarkyellow, DYellowgreen, /* yellow */
30 a0d146ed 2005-07-12 devnull DMedgreen, DDarkgreen, /* green */
31 a0d146ed 2005-07-12 devnull 0x00AAFFFF, 0x0088CCFF, /* blue */
32 a0d146ed 2005-07-12 devnull 0xCCCCCCFF, 0x888888FF, /* grey */
35 a0d146ed 2005-07-12 devnull Memimage*
36 a0d146ed 2005-07-12 devnull allocrepl(ulong color)
38 a0d146ed 2005-07-12 devnull Memimage *m;
40 a0d146ed 2005-07-12 devnull m = allocmemimage(Rect(0,0,1,1), RGB24);
41 a0d146ed 2005-07-12 devnull memfillcolor(m, color);
42 a0d146ed 2005-07-12 devnull m->flags |= Frepl;
43 a0d146ed 2005-07-12 devnull m->clipr = Rect(-1000000, -1000000, 1000000, 1000000);
44 a0d146ed 2005-07-12 devnull return m;
47 a0d146ed 2005-07-12 devnull static void
48 a0d146ed 2005-07-12 devnull ginit(void)
50 a0d146ed 2005-07-12 devnull static int first = 1;
53 a0d146ed 2005-07-12 devnull if(!first)
56 a0d146ed 2005-07-12 devnull first = 0;
57 a0d146ed 2005-07-12 devnull memimageinit();
58 a0d146ed 2005-07-12 devnull smallfont = openmemsubfont(unsharp("#9/font/lucidasans/lstr.10"));
59 a0d146ed 2005-07-12 devnull black = memblack;
60 a0d146ed 2005-07-12 devnull blue = allocrepl(DBlue);
61 a0d146ed 2005-07-12 devnull red = allocrepl(DRed);
62 a0d146ed 2005-07-12 devnull grid = allocrepl(0x77777777);
63 a0d146ed 2005-07-12 devnull for(i=0; i<nelem(fill)/2 && i<nelem(lofill) && i<nelem(hifill); i++){
64 a0d146ed 2005-07-12 devnull lofill[i] = allocrepl(fill[2*i]);
65 a0d146ed 2005-07-12 devnull hifill[i] = allocrepl(fill[2*i+1]);
69 a0d146ed 2005-07-12 devnull static void
70 a0d146ed 2005-07-12 devnull mklabel(char *str, int v)
72 a0d146ed 2005-07-12 devnull if(v < 0){
74 a0d146ed 2005-07-12 devnull *str++ = '-';
76 a0d146ed 2005-07-12 devnull if(v < 10000)
77 a0d146ed 2005-07-12 devnull sprint(str, "%d", v);
78 a0d146ed 2005-07-12 devnull else if(v < 10000000)
79 a0d146ed 2005-07-12 devnull sprint(str, "%dk", v/1000);
81 a0d146ed 2005-07-12 devnull sprint(str, "%dM", v/1000000);
84 a0d146ed 2005-07-12 devnull static void
85 a0d146ed 2005-07-12 devnull drawlabel(Memimage *m, Point p, int n)
87 a0d146ed 2005-07-12 devnull char buf[30];
90 a0d146ed 2005-07-12 devnull mklabel(buf, n);
91 a0d146ed 2005-07-12 devnull w = memsubfontwidth(smallfont, buf);
92 a0d146ed 2005-07-12 devnull memimagestring(m, Pt(p.x-5-w.x, p.y), memblack, ZP, smallfont, buf);
95 a0d146ed 2005-07-12 devnull static int
96 a0d146ed 2005-07-12 devnull scalept(int val, int valmin, int valmax, int ptmin, int ptmax)
98 a0d146ed 2005-07-12 devnull if(val <= valmin)
99 a0d146ed 2005-07-12 devnull val = valmin;
100 a0d146ed 2005-07-12 devnull if(val >= valmax)
101 a0d146ed 2005-07-12 devnull val = valmax;
102 a0d146ed 2005-07-12 devnull if(valmax == valmin)
103 a0d146ed 2005-07-12 devnull valmax++;
104 a0d146ed 2005-07-12 devnull return ptmin + (vlong)(val-valmin)*(ptmax-ptmin)/(valmax-valmin);
107 a0d146ed 2005-07-12 devnull Memimage*
108 a0d146ed 2005-07-12 devnull statgraph(Graph *g)
110 a0d146ed 2005-07-12 devnull int i, lastlo, nbin, x, lo, hi, min, max, first;
111 a0d146ed 2005-07-12 devnull Memimage *m;
112 a0d146ed 2005-07-12 devnull Rectangle r;
113 a0d146ed 2005-07-12 devnull Statbin *b, bin[2000]; /* 32 kB, but whack is worse */
115 a0d146ed 2005-07-12 devnull needstack(8192); /* double check that bin didn't kill us */
117 a0d146ed 2005-07-12 devnull if(g->wid <= MinWidth)
118 a0d146ed 2005-07-12 devnull g->wid = DefaultWidth;
119 a0d146ed 2005-07-12 devnull if(g->ht <= MinHeight)
120 a0d146ed 2005-07-12 devnull g->ht = DefaultHeight;
121 a0d146ed 2005-07-12 devnull if(g->wid > nelem(bin))
122 a0d146ed 2005-07-12 devnull g->wid = nelem(bin);
123 a0d146ed 2005-07-12 devnull if(g->fill < 0)
124 a0d146ed 2005-07-12 devnull g->fill = ((uint)g->arg>>8)%nelem(lofill);
125 a0d146ed 2005-07-12 devnull if(g->fill > nelem(lofill))
126 a0d146ed 2005-07-12 devnull g->fill %= nelem(lofill);
128 a0d146ed 2005-07-12 devnull nbin = g->wid - (Left+Right);
129 a0d146ed 2005-07-12 devnull binstats(g->fn, g->arg, g->t0, g->t1, bin, nbin);
132 a0d146ed 2005-07-12 devnull * compute bounds
134 a0d146ed 2005-07-12 devnull min = g->min;
135 a0d146ed 2005-07-12 devnull max = g->max;
136 a0d146ed 2005-07-12 devnull if(min < 0 || max <= min){
137 a0d146ed 2005-07-12 devnull min = max = 0;
138 a0d146ed 2005-07-12 devnull first = 1;
139 a0d146ed 2005-07-12 devnull for(i=0; i<nbin; i++){
140 a0d146ed 2005-07-12 devnull b = &bin[i];
141 a0d146ed 2005-07-12 devnull if(b->nsamp == 0)
142 a0d146ed 2005-07-12 devnull continue;
143 a0d146ed 2005-07-12 devnull if(first || b->min < min)
144 a0d146ed 2005-07-12 devnull min = b->min;
145 a0d146ed 2005-07-12 devnull if(first || b->max > max)
146 a0d146ed 2005-07-12 devnull max = b->max;
147 a0d146ed 2005-07-12 devnull first = 0;
151 a0d146ed 2005-07-12 devnull qlock(&memdrawlock);
152 a0d146ed 2005-07-12 devnull ginit();
153 a0d146ed 2005-07-12 devnull if(smallfont==nil || black==nil || blue==nil || red==nil || hifill==nil || lofill==nil){
154 a0d146ed 2005-07-12 devnull werrstr("graphics initialization failed");
155 a0d146ed 2005-07-12 devnull qunlock(&memdrawlock);
156 a0d146ed 2005-07-12 devnull return nil;
159 a0d146ed 2005-07-12 devnull /* fresh image */
160 a0d146ed 2005-07-12 devnull m = allocmemimage(Rect(0,0,g->wid,g->ht), ABGR32);
161 a0d146ed 2005-07-12 devnull if(m == nil){
162 a0d146ed 2005-07-12 devnull qunlock(&memdrawlock);
163 a0d146ed 2005-07-12 devnull return nil;
165 a0d146ed 2005-07-12 devnull r = Rect(Left, Top, g->wid-Right, g->ht-Bottom);
166 a0d146ed 2005-07-12 devnull memfillcolor(m, DTransparent);
168 a0d146ed 2005-07-12 devnull /* x axis */
169 a0d146ed 2005-07-12 devnull memimagedraw(m, Rect(r.min.x, r.max.y, r.max.x, r.max.y+1), black, ZP, memopaque, ZP, S);
171 a0d146ed 2005-07-12 devnull /* y labels */
172 a0d146ed 2005-07-12 devnull drawlabel(m, r.min, max);
173 a0d146ed 2005-07-12 devnull if(min != 0)
174 a0d146ed 2005-07-12 devnull drawlabel(m, Pt(r.min.x, r.max.y-smallfont->height), min);
176 a0d146ed 2005-07-12 devnull /* actual data */
177 a0d146ed 2005-07-12 devnull lastlo = -1;
178 a0d146ed 2005-07-12 devnull for(i=0; i<nbin; i++){
179 a0d146ed 2005-07-12 devnull b = &bin[i];
180 a0d146ed 2005-07-12 devnull if(b->nsamp == 0)
181 a0d146ed 2005-07-12 devnull continue;
182 a0d146ed 2005-07-12 devnull lo = scalept(b->min, min, max, r.max.y, r.min.y);
183 a0d146ed 2005-07-12 devnull hi = scalept(b->max, min, max, r.max.y, r.min.y);
184 a0d146ed 2005-07-12 devnull x = r.min.x+i;
187 a0d146ed 2005-07-12 devnull if(lastlo != -1){
188 a0d146ed 2005-07-12 devnull if(lastlo < lo)
189 a0d146ed 2005-07-12 devnull memimagedraw(m, Rect(x-1, lastlo, x, lo), hifill[g->fill], ZP, memopaque, ZP, S);
190 a0d146ed 2005-07-12 devnull else if(lastlo > lo)
191 a0d146ed 2005-07-12 devnull memimagedraw(m, Rect(x-1, lo, x, lastlo), hifill[g->fill], ZP, memopaque, ZP, S);
193 a0d146ed 2005-07-12 devnull memimagedraw(m, Rect(x, hi, x+1,lo), hifill[g->fill], ZP, memopaque, ZP, S);
194 a0d146ed 2005-07-12 devnull memimagedraw(m, Rect(x, lo, x+1, r.max.y), lofill[g->fill], ZP, memopaque, ZP, S);
195 a0d146ed 2005-07-12 devnull lastlo = lo;
198 a0d146ed 2005-07-12 devnull if(bin[nbin-1].nsamp)
199 a0d146ed 2005-07-12 devnull drawlabel(m, Pt(r.max.x, r.min.y+(Dy(r)-smallfont->height)/2), bin[nbin-1].avg);
200 a0d146ed 2005-07-12 devnull qunlock(&memdrawlock);
201 a0d146ed 2005-07-12 devnull return m;