File: images.c

package info (click to toggle)
http-analyze 1.9e-2
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 376 kB
  • ctags: 327
  • sloc: ansic: 3,493; sh: 185; makefile: 106
file content (792 lines) | stat: -rw-r--r-- 23,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#if !defined(lint)
static char rcs_id[] = "$Id: images.c,v 1.8 1996/06/17 19:39:02 stefan Exp $";
#endif

/*
** GIF image drawing functions for http-analyze.
**
** Uses the gd gif-manipulating library by Tom Boutell, <boutell@boutell.com>
** See http://sunsite.unc.edu/boutell/index.html for more infos about gd.
**
** The functions in this source file are part of http-analyze, which is
**	Copyright 1996 by Stefan Stapelberg, <stefan@rent-a-guru.de>
**
** $Log: images.c,v $
** Revision 1.8  1996/06/17  19:39:02  stefan
** Fixed a bug which caused http-analyze to dump core
** by casually writing into other memory regions.
**
** Revision 1.7  1996/06/13  11:30:43  stefan
** Added rcs_id.
**
** Revision 1.6  1996/06/13  10:40:32  stefan
** Fixed a bug which caused the graph to always start at
** 0,0 regardless of the results from previous summary periods.
** Fixed the image drawing functions to preserve 4 pixels
** for padding between the upper border and the graphs/bars.
**
** Revision 1.5  1996/06/07  11:03:13  stefan
** Now avoids same colors for consecutive countries
** in the Total transfers by Country pie chart.
**
** Revision 1.4  1996/05/25  02:00:15  stefan
** Cleaned up program structure somewhat. This is still the worstest
** kind of spaghetti code in http-analyze. Added code for a pie chart
** of the topmost countries accessing the server.
**
** Revision 1.3  1996/05/09  09:12:15  stefan
** Fixed bug in icon code (wrong aspect ratio of icon images).
**
** Revision 1.2  1996/01/28  14:35:07  stefan
** Made it lint-clean somewhat. There is still more cleanup to do.
**
** Revision 1.1  1996/01/17  00:00:00  stefan
** Initial revision
**
*/

#include <unistd.h>	/* for access(2) */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>

#include "defs.h"
#include "gd.h"
#include "gdfontl.h"
#include "gdfonts.h"

#define FWID(arg)	((arg)->w)

static gdPoint points[50];

/* draw beveled borders */
static void makebevel(gdImagePtr im, int mx, int my, int sz,
		      int col1, int col2, int col3, int col4) {
	int pt = 0;

	points[pt].x = 0;	points[pt++].y = 0;
	points[pt].x = sz;	points[pt++].y = sz;
	points[pt].x = mx-sz;	points[pt++].y = sz;
	points[pt].x = mx;	points[pt++].y = 0;
	gdImageFilledPolygon(im, points, pt, col1);

	pt = 0;
	points[pt].x = 0;	points[pt++].y = 0;
	points[pt].x = sz;	points[pt++].y = sz;
	points[pt].x = sz;	points[pt++].y = my-sz;
	points[pt].x = 0;	points[pt++].y = my;
	gdImageFilledPolygon(im, points, pt, col2);

	pt = 0;
	points[pt].x = 0;	points[pt++].y = my;
	points[pt].x = sz;	points[pt++].y = my-sz;
	points[pt].x = mx-sz;	points[pt++].y = my-sz;
	points[pt].x = mx;	points[pt++].y = my;
	gdImageFilledPolygon(im, points, pt, col3);

	pt = 0;
	points[pt].x = mx;	points[pt++].y = my;
	points[pt].x = mx-sz;	points[pt++].y = my-sz;
	points[pt].x = mx-sz;	points[pt++].y = sz;
	points[pt].x = mx;	points[pt++].y = 0;
	gdImageFilledPolygon(im, points, pt, col4);
	return;
}

/* add a bevel to space previously reserved */
static void addbevel(gdImagePtr im, int mx, int my, int sz,
		     int col1, int col2, int col3, int col4) {

	makebevel(im, mx, my, sz, col1, col2, col3, col4);
	/*gdImageRectangle(im, 0, 0, mx, my, black);*/

	return;
}

/* draw bars */
static void drawbars(gdImagePtr im, u_long *values, u_long maxval,
		     int bx, int by, int rx, int ry, int steps,
		     int size, int bwid, int fcol, int bcol) {
	int idx, pt, top, left;
	int hdist = rx / size;

	bx += (hdist-bwid) / 2;
	for (idx=0; idx < steps; idx++) {
		top = by+ry+4-(values[idx]*ry/maxval);
		left = bx+(idx*hdist);

		if (ry+by-top < 2)
			continue;
		pt = 0;
		points[pt].x = left;
		points[pt++].y = ry+by+4;
		points[pt].x = left;
		points[pt++].y = top;
		points[pt].x = left+bwid;
		points[pt++].y = top;
		points[pt].x = left+bwid;
		points[pt++].y = ry+by+4;

		gdImageFilledPolygon(im, points, pt, fcol);
		gdImagePolygon(im, points, pt, bcol);

	}
	return;
}

/* draw bars by day */
int mn_bars(int sizex, int sizey, int base, char *name) {
	char lbuf[SMALLSIZE];
	int idx, len;
	u_long hits_max = 0L;
	u_long file_max = 0L;
	u_long site_max = 0L;
	u_long kb_max = 0L;

	/* the image size */
	int maxx = sizex-1;
	int maxy = sizey-1;

	/* the drawable region */
	int basex = base;
	int basey = base;
	int rangex = maxx-(2*basex);
	int rangey = maxy-(2*basey);

	int bl1 = (rangey+1)/2;
	int bl2 = (rangey+1)/4;
	int hdist = rangex/31;

	/* the font baselines */
	int flx = basex-12;		/* left X axis */
	int frx = maxx-basex;		/* right X axis */
	int fty = basey-18;		/* top Y axis */
	int fby = maxy-basey+4;		/* bottom Y axis */

	/* colors */
	int paper_white, white, grey1, grey2, grey3,
		black, red, green, blue, orange;

	FILE *out;
	gdImagePtr im;

	for (idx=0; idx < (int)tend.mday; idx++) {
		if (daily_hits[idx] > hits_max)
			hits_max = daily_hits[idx];
		if (daily_files[idx] > file_max)
			file_max = daily_files[idx];
		if (daily_sites[idx] > site_max)
			site_max = daily_sites[idx];
		if (daily_kbytes[idx] > kb_max)
			kb_max = daily_kbytes[idx];
	}
	if (hits_max == 0 || site_max == 0 || kb_max == 0)	/* no hits ?!? */
		return 0;

	/* Create the image */
	if ((im = gdImageCreate(sizex, sizey)) == NULL)
		return 0;

	paper_white = gdImageColorAllocate(im, 220, 220, 220);
#if defined(lint)
	paper_white = paper_white;
#endif
	white = gdImageColorAllocate(im, 255, 255, 255);
	grey1 = gdImageColorAllocate(im, 242, 242, 242);
	grey2 = gdImageColorAllocate(im, 102, 102, 102);
	grey3 = gdImageColorAllocate(im, 153, 153, 153);
	black = gdImageColorAllocate(im, 0, 0, 0);
	red = gdImageColorAllocate(im, 220, 0, 0);
	green = gdImageColorAllocate(im, 0, 170, 0);
	blue = gdImageColorAllocate(im, 0, 0, 220);
	orange = gdImageColorAllocate(im, 255, 80, 0);

	for (idx=0; idx < 31; idx++) {
		(void) sprintf(lbuf, "%2d", idx+1);
		gdImageString(im, gdFontSmall, basex+(idx*hdist)+FWID(gdFontSmall)/2, fby, lbuf, black);
	}

	(void) sprintf(lbuf, "%lu", kb_max);
	gdImageStringUp(im, gdFontSmall, flx, maxy-basey-bl2+strlen(lbuf)*FWID(gdFontSmall), lbuf, orange);

	(void) sprintf(lbuf, "%lu", site_max);
	gdImageStringUp(im, gdFontSmall, flx, maxy-basey-(2*bl2)+strlen(lbuf)*FWID(gdFontSmall), lbuf, red);

	(void) sprintf(lbuf, "%lu", hits_max);
	gdImageStringUp(im, gdFontSmall, flx, basey+strlen(lbuf)*FWID(gdFontSmall), lbuf, green);

	(void) sprintf(lbuf, "Hits");
	len = strlen(lbuf);
	gdImageString(im, gdFontLarge, basex, fty, lbuf, green);
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
	len++;
	(void) sprintf(lbuf, "Files");
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, lbuf, blue);
	len += strlen(lbuf);
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
	len++;
	(void) sprintf(lbuf, "Sites");
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, lbuf, red);
	len += strlen(lbuf);
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
	len++;
	(void) sprintf(lbuf, "Kilobytes");
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, lbuf, orange);
	len += strlen(lbuf)+1;
	gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "by day", black);

	gdImageStringUp(im, gdFontSmall, frx+1, sizey/2+gdFontSmall->w*31/2, 
		"Copyright 1996 RENT-A-GURU (TM)", black);

	gdImageLine(im, basex, bl1+basey, maxx-basex, bl1+basey, black);
	gdImageLine(im, basex, bl1+bl2+basey, maxx-basex, bl1+bl2+basey, black);

	drawbars(im, daily_hits, hits_max, basex-1, basey, rangex, bl1-4,
		tend.mday, 31, 6, green, black);

	drawbars(im, daily_files, hits_max, basex+3, basey, rangex, bl1-4,
		tend.mday, 31, 6, blue, black);

	drawbars(im, daily_sites, site_max, basex+1, basey+bl1, rangex, bl2-4,
		tend.mday, 31, 8, red, black);

	drawbars(im, daily_kbytes, kb_max, basex+1, bl1+bl2+basey, rangex, bl2-4,
		tend.mday, 31, 8, orange, black);

	/* Draw a rectangle around the central area. */
	gdImageRectangle(im, basex, basey, maxx-basex, maxy-basey, black);
	makebevel(im, maxx, maxy, 6, white, grey1, grey2, grey3);

	gdImageInterlace(im, 1);		/* make it interlaced */
	if ((out = fopen(name, "w")) != NULL) {
		gdImageGif(im, out);
		(void) fclose(out);
	}
	gdImageDestroy(im);
	return 1;
}

/* draw bars by hour */
int hr_bars(int sizex, int sizey, int base, char *name) {
	char lbuf[SMALLSIZE];
	u_long hits_max = 0L;	
	int idx;

	/* the image size */
	int maxx = sizex-1;
	int maxy = sizey-1;

	/* the drawable region */
	int basex = base;
	int basey = base;
	int rangex = maxx-(2*basex)-3;
	int rangey = maxy-(2*basey);

	int hdist = rangex/24;

	/* the font baselines */
	int flx = basex-12;		/* left X axis */
	/*int frx = maxx-basex;		/* right X axis */
	int fty = basey-18;		/* top Y axis */
	int fby = maxy-basey+4;		/* bottom Y axis */

	/* colors */
	int paper_white, white, grey1, grey2, grey3, black, orange;

	FILE *out;
	gdImagePtr im;

	for (idx=0; idx < 24; idx++) {
		if (hr_hits[idx] > hits_max)
			hits_max = hr_hits[idx];
	}
	if (hits_max == 0)	/* no hits ?!? */
		return 0;

	/* create the image */
	if ((im = gdImageCreate(sizex, sizey)) == NULL)
		return 0;

	/* first color allocated is background */
	paper_white = gdImageColorAllocate(im, 220, 220, 220);
#if defined(lint)
	paper_white = paper_white;
#endif
	white = gdImageColorAllocate(im, 255, 255, 255);
	grey1 = gdImageColorAllocate(im, 242, 242, 242);
	grey2 = gdImageColorAllocate(im, 102, 102, 102);
	grey3 = gdImageColorAllocate(im, 153, 153, 153);
	black = gdImageColorAllocate(im, 0, 0, 0);
	orange = gdImageColorAllocate(im, 255, 80, 0);

	(void) sprintf(lbuf, "Hits by hours since " TIME_FMT,
		tstart.mday, monnam[tstart.mon], EPOCH(tstart.year));

	gdImageString(im, gdFontLarge, basex, fty, lbuf, black);

	for (idx=0; idx < 24; idx++) {
		(void) sprintf(lbuf, "%2d", idx);
		gdImageString(im, gdFontSmall, basex+(idx*hdist)+FWID(gdFontSmall), fby, lbuf, black);
	}
	gdImageStringUp(im, gdFontSmall, flx, fby, "1", black);

	(void) sprintf(lbuf, "%lu", hits_max);
	gdImageStringUp(im, gdFontSmall, flx, basey+strlen(lbuf)*FWID(gdFontSmall), lbuf, orange);

	drawbars(im, hr_hits, hits_max, basex+2, basey, rangex, rangey-4,
		24, 24, 11, orange, black);

	/* Draw a rectangle around the central area. */
	gdImageRectangle(im, basex, basey, maxx-basex, maxy-basey, black);
	makebevel(im, maxx, maxy, 6, white, grey1, grey2, grey3);

	gdImageInterlace(im, 1);		/* make it interlaced */
	if ((out = fopen(name, "w")) != NULL) {
		gdImageGif(im, out);
		(void) fclose(out);
	}
	gdImageDestroy(im);
	return 1;
}

/* draw a graph */
static void drawgraph(gdImagePtr im, u_long *values, u_long maxval,
		      int bx, int by, int rx, int ry,
		      int steps, int size, int fcol, int bcol) {
	int idx, pt, top, left;
	int hdist = rx / size;

	pt = 0;
	points[pt].x = bx;	/* start point */
	points[pt++].y = by+ry+4;

	for (idx=0; idx < steps; idx++) {
		top = by+ry+4-(values[idx]*ry/maxval);
		left = bx+(idx*hdist);
		
		points[pt].x = left;
		points[pt++].y = top;
	}
	points[pt].x = left;	/* end point */
	points[pt++].y = by+ry+4;

	gdImageFilledPolygon(im, points, pt, fcol);
	gdImagePolygon(im, points, pt, bcol);
	return;
}

/* draw graph by month */
int graph(int sizex, int sizey, int base, char *name) {
	char lbuf[SMALLSIZE];
	int idx, len;
	u_long hits_max = 0L;	
	u_long file_max = 0L;
	u_long site_max = 0L;
	u_long kb_max = 0L;

	/* the image size */
	int maxx = sizex-1;
	int maxy = sizey-1;

	/* the drawable region */
	int basex = base;
	int basey = base;
	int rangex = maxx-(2*basex);
	int rangey = maxy-(2*basey);

	int bl1 = (rangey)/2;
	int bl2 = (rangey)/4;
	int hdist = rangex / 12;

	/* the font baselines */
	int flx = basex-12;		/* left X axis */
	int frx = maxx-basex;		/* right X axis */
	int fty = basey-18;		/* top Y axis */
	int fby = maxy-basey+4;		/* bottom Y axis */

	/* colors */
	int white, grey1, grey2, grey3, black, red, green, blue, orange;

	int labels = (base > 0);	/* draw labels? */

	FILE *out;
	gdImagePtr im;

	for (idx=0; idx < 13; idx++) {
		if (mn_hits[idx] > hits_max)
			hits_max = mn_hits[idx];
		if (mn_files[idx] > file_max)
			file_max = mn_files[idx];
		if (mn_sites[idx] > site_max)
			site_max = mn_sites[idx];
		if (mn_kbytes[idx] > kb_max)
			kb_max = mn_kbytes[idx];
	}

	if (!base && (hits_max == 0 || site_max == 0 || kb_max == 0))
		return 0;

	if (!base) {	/* reserve some space for bevel */
		sizex += 9; sizey += 9;
		maxx += 9;  maxy += 9;

		/* the drawable region */
		basex = 5;
		basey = 5;
	}

	/* create the image, allocate colors */
	if ((im = gdImageCreate(sizex, sizey)) == NULL)
		return 0;

	white = gdImageColorAllocate(im, 255, 255, 255);
	black = gdImageColorAllocate(im, 0, 0, 0);
	red = gdImageColorAllocate(im, 220, 0, 0);
	green = gdImageColorAllocate(im, 0, 170, 0);
	blue = gdImageColorAllocate(im, 0, 0, 220);
	orange = gdImageColorAllocate(im, 255, 80, 0);

	if (labels) {
		grey1 = gdImageColorAllocate(im, 242, 242, 242);
		grey2 = gdImageColorAllocate(im, 102, 102, 102);
		grey3 = gdImageColorAllocate(im, 153, 153, 153);
	} else {
		grey1 = gdImageColorAllocate(im, 63, 63, 63);
		grey2 = gdImageColorAllocate(im, 95, 95, 95);
		grey3 = gdImageColorAllocate(im, 215, 215, 215);
	}

	if (hits_max == 0 || site_max == 0 || kb_max == 0) {	/* no hits ?!? */
		(void) sprintf(lbuf, "not hits/sites/kbytes for current and last 12 month?!?");
		gdImageString(im, gdFontSmall,
			basex+(rangex-strlen(lbuf)*FWID(gdFontSmall))/2U, basey+(rangey/2), lbuf, black);
		gdImageInterlace(im, 1);		/* make it interlaced */
		if ((out = fopen(name, "w")) != NULL) {
			gdImageGif(im, out);
			(void) fclose(out);
		}
		gdImageDestroy(im);
		return 1;
	}

	if (labels) {
		for (idx=0; idx < 13; idx++) {
			if (!idx)
				(void) sprintf(lbuf, "%3.3s %d", mn_str[idx], EPOCH(tend.year)-1);
			else if (idx == 12)
				(void) sprintf(lbuf, "%3.3s %d", mn_str[idx], EPOCH(tend.year));
			else	(void) sprintf(lbuf, "%3.3s", mn_str[idx]);
			gdImageString(im, gdFontSmall,
				basex+(idx*hdist)-(strlen(lbuf)*FWID(gdFontSmall))/2U, fby, lbuf, black);
		}

		(void) sprintf(lbuf, "%lu", kb_max);
		gdImageStringUp(im, gdFontSmall, flx, maxy-basey-bl2+strlen(lbuf)*FWID(gdFontSmall), lbuf, orange);
		(void) sprintf(lbuf, "%lu", site_max);
		gdImageStringUp(im, gdFontSmall, flx, maxy-basey-(2*bl2)+strlen(lbuf)*FWID(gdFontSmall), lbuf, red);
		(void) sprintf(lbuf, "%lu", hits_max);
		gdImageStringUp(im, gdFontSmall, flx, basey+strlen(lbuf)*FWID(gdFontSmall), lbuf, green);

		(void) sprintf(lbuf, "Hits");
		len = strlen(lbuf);
		gdImageString(im, gdFontLarge, basex, fty, lbuf, green);
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
		len++;
		(void) sprintf(lbuf, "Files");
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, lbuf, blue);
		len += strlen(lbuf);
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
		len++;
		(void) sprintf(lbuf, "Sites");
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty,
			lbuf, red);
		len += strlen(lbuf);
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "/", black);
		len++;
		(void) sprintf(lbuf, "Kilobytes");
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty,
			lbuf, orange);
		len += strlen(lbuf)+1;
		gdImageString(im, gdFontLarge, basex+(len*FWID(gdFontLarge)), fty, "by month", black);
		gdImageStringUp(im, gdFontSmall, frx+1, sizey/2+gdFontSmall->w*31/2, 
			"Copyright 1996 RENT-A-GURU (TM)", black);

		gdImageLine(im, basex, bl1+basey, basex+12*hdist, bl1+basey, black);
		gdImageLine(im, basex, bl1+bl2+basey, basex+12*hdist, bl1+bl2+basey, black);
	}

	drawgraph(im, mn_hits, hits_max, basex, basey, rangex, bl1-4,
		13, 12, green, black);

	drawgraph(im, mn_files, hits_max, basex, basey, rangex, bl1-4,
		13, 12, blue, black);

	drawgraph(im, mn_sites, site_max, basex, basey+bl1, rangex, bl2-4,
		13, 12, red, black);

	drawgraph(im, mn_kbytes, kb_max, basex, bl1+bl2+basey, rangex, bl2-4,
		13, 12, orange, black);

	/* Draw a rectangle around the central area. */
	gdImageRectangle(im, basex, basey, basex+12*hdist, maxy-basey, black);

	if (labels)
		makebevel(im, maxx, maxy, 6, white, grey1, grey2, grey3);
	else
		addbevel(im, maxx, maxy, 4, grey1, grey2, white, grey3);

	gdImageInterlace(im, 1);		/* make it interlaced */
	if ((out = fopen(name, "w")) != NULL) {
		gdImageGif(im, out);
		(void) fclose(out);
	}
	gdImageDestroy(im);
	return 1;
}

#define RADIUS		120
#define PI		3.14159265358979323846
#define MINPERCENT	1.4
#define MAX_CNTRY	14

#define COLOR_BG	0
#define COLOR_BLACK	1
#define COLOR_WHITE	2
#define COLOR_PWHITE	3
#define COLOR_IDX	4
#define NUM_COLORS	15

static int colors[NUM_COLORS];
static int tcolor[NUM_COLORS];

static struct {
	int x, y;
	int x2, y2;
	int fillcol;
	int textcol;
	char *name;
} pts[MAX_CNTRY+1];

int c_chart(int sizex, int sizey, int base, char *name, COUNTRY **ccp, size_t max) {
	gdImagePtr im;
	double scale, ht;
	double percent;
	char *cp, lbuf[50];
	int idx, pt, other;
	int tidx, cidx;
	int grey1, grey2, grey3;
	int leftb = (sizex-2*RADIUS)/2-4;
	int rightb = leftb+(2*RADIUS)+8;
	int oldx = 0, oldy = 0;
	long total_sum = 0L;
	long total_other = 0L;
	FILE *out;

	im = gdImageCreate(sizex, sizey);
	colors[COLOR_BG] = gdImageColorAllocate(im, 2, 2, 2);
	colors[COLOR_BLACK] = gdImageColorAllocate(im, 0, 0, 0);
	colors[COLOR_WHITE] = gdImageColorAllocate(im, 255, 255, 255);
	colors[COLOR_PWHITE] = gdImageColorAllocate(im, 220, 220, 220);
	grey1 = gdImageColorAllocate(im, 242, 242, 242);
	grey2 = gdImageColorAllocate(im, 102, 102, 102);
	grey3 = gdImageColorAllocate(im, 153, 153, 153);

	cidx = COLOR_IDX;
	colors[cidx] = gdImageColorAllocate(im,   0,   0, 154); /* navyblue */
	tcolor[cidx++] = colors[COLOR_WHITE];

	colors[cidx] = gdImageColorAllocate(im, 204,   0, 255); /* violet */
	tcolor[cidx++] = colors[COLOR_WHITE];

	colors[cidx] = gdImageColorAllocate(im,  52, 255,   0); /* green */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im,  52,   0,   0); /* brown */
	tcolor[cidx++] = colors[COLOR_WHITE];

	colors[cidx] = gdImageColorAllocate(im,   0, 204, 255); /* skyblue */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im, 255, 103,   0); /* orange */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im, 255, 154, 255); /* pink */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im,   0,  52,   0); /* darkgreen */
	tcolor[cidx++] = colors[COLOR_WHITE];

	colors[cidx] = gdImageColorAllocate(im, 255, 255,   0); /* yellow */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im, 255,   0,   0); /* red */
	tcolor[cidx++] = colors[COLOR_BLACK];

	colors[cidx] = gdImageColorAllocate(im, 154, 154,   0); /* olive */
	tcolor[cidx++] = colors[COLOR_BLACK];

	gdImageFilledRectangle(im, 0, 0, sizex-1, sizey-1, colors[COLOR_BG]);
	gdImageArc(im, sizex/2, sizey/2, RADIUS*2, RADIUS*2-60, 0, 360, colors[COLOR_BLACK]);   
	gdImageFill(im, sizex/2, sizey/2, colors[COLOR_BLACK]);

	scale = total_hits ? (PI*2)/total_hits : 0.0;
	total_sum = total_other = 0L;
	cidx = tidx = COLOR_IDX;
	other = 0;

	for (idx=0, pt=0; idx < max && pt < TABSIZE(pts); idx++) {
		if (idx >= MAX_CNTRY || other ||
		    (percent = (ccp[idx]->count*100.0)/total_hits) < MINPERCENT) {
			total_other += ccp[idx]->count;
			total_sum += ccp[idx]->count;
			other = 1;
			if (idx != max-1)
				continue;
		} else {
			ht = total_sum+ccp[idx]->count/2.0;
			total_sum += ccp[idx]->count;
		}

		if (other) {
			percent = (total_other*100.0)/total_hits;
			ht = total_sum-total_other/2.0;
			(void) sprintf(lbuf, "Other %d%%", (int)rint(percent));
		} else
			(void) sprintf(lbuf, "%.14s %d%%", ccp[idx]->name, (int)rint(percent));

		if (idx == max-1 && cidx == COLOR_IDX) {
			cidx += 2;	/* avoid same color for consecutive countries */
			tidx += 2;
		}
		pts[pt].name = strsave(lbuf);	/* check return code below */
		pts[pt].x = (int)(cos((double)total_sum*scale)*RADIUS+(sizex/2));
		pts[pt].y = (int)(sin((double)total_sum*scale)*RADIUS+(sizey/2));
		pts[pt].x2 = (int)(cos((double)ht*scale)*(RADIUS/2)+(sizex/2));
		pts[pt].y2 = (int)(sin((double)ht*scale)*(RADIUS/2)+(sizey/2));
		pts[pt].fillcol = colors[cidx++];
		pts[pt].textcol = tcolor[tidx++];
		pt++;

		if (cidx == TABSIZE(colors))
			cidx = COLOR_IDX;

		if (tidx == TABSIZE(tcolor))
			tidx = COLOR_IDX;
	}

	idx = 0;
	gdImageLine(im, sizex/2, sizey/2,
		pts[idx].x, pts[idx].y, colors[COLOR_BG]);
	for (++idx; idx < pt; idx++) {
		gdImageLine(im, sizex/2, sizey/2,
			pts[idx].x, pts[idx].y, colors[COLOR_BG]);
		gdImageFillToBorder(im, pts[idx].x2, pts[idx].y2,
			colors[COLOR_BG], pts[idx].fillcol);
	}
	gdImageFillToBorder(im, pts[0].x2, pts[0].y2,
		colors[COLOR_BG], pts[0].fillcol);

	idx = (sizex-2*RADIUS)/2;
	gdImageArc(im, sizex/2, sizey/2, RADIUS*2, RADIUS*2-60, 0, 360, colors[COLOR_BLACK]);   
	gdImageLine(im, idx, sizey/2, idx, sizey/2+12, colors[COLOR_BLACK]);
	gdImageLine(im, sizex-idx, sizey/2, sizex-idx, sizey/2+12, colors[COLOR_BLACK]);
	gdImageArc(im, sizex/2, sizey/2+12, RADIUS*2, RADIUS*2-60, 0, 180, colors[COLOR_BLACK]);
	gdImageFill(im, 0, 0, colors[COLOR_WHITE]);
	gdImageFill(im, sizex/2, sizey/2+RADIUS-20, colors[COLOR_BLACK]);

	for (idx=0; idx < pt; idx++) {
		int x, y, txtpos, len;

		if (!pts[idx].x)
			continue;

		x = (pts[idx].x2 <= sizex/2) ? leftb : rightb;
		if ((y=pts[idx].y2) <= sizey/2) {	/* T */
			y -= RADIUS/4;
		} else {				/* B */
			y += RADIUS/4;
		}

                if (oldx == x && oldy != 0) {
			if (x > sizex/2) {
                                if (y < oldy + 12 || (y < oldy + 48) && (y > oldy + 12))
                                        y = oldy + 12;
                        } else if (y > oldy - 12 || (y > oldy - 48) && (y < oldy - 12)) {
                                y = oldy - 12;
			}
                }
		oldx = x;
		oldy = y;
		
		txtpos = (x <= sizex/2) ? leftb-4 : rightb+4;
		gdImageLine(im, pts[idx].x2, pts[idx].y2, x, y, colors[COLOR_BLACK]);
		gdImageLine(im, x, y, txtpos, y, colors[COLOR_BLACK]);

		if (pts[idx].name != NULL) {
			len = strlen(pts[idx].name)*gdFontSmall->w;
			if (x <= sizex/2) {
				txtpos -= len;
				gdImageFilledRectangle(im, txtpos, y-5, txtpos+len, y+4, pts[idx].fillcol);
			} else
				gdImageFilledRectangle(im, txtpos, y-5, txtpos+len, y+4, pts[idx].fillcol);
			gdImageString(im, gdFontSmall,
				txtpos, y-gdFontSmall->h/2,
				pts[idx].name, pts[idx].textcol); 

			free(pts[idx].name);
		}
	}
	makebevel(im, sizex-1, sizey-1, 6, colors[COLOR_WHITE], grey1, grey2, grey3);

	cp = "Total transfers by Country";
	gdImageString(im, gdFontLarge,
		(sizex-strlen(cp)*gdFontLarge->w)/2U, sizey-base/2-gdFontLarge->h,
		cp, colors[COLOR_BLACK]);

	gdImageInterlace(im, 1);		/* make it interlaced */
	if ((out = fopen(name, "w")) != NULL) {
		gdImageGif(im, out);
		(void) fclose(out);
	}
	gdImageDestroy(im);
	return 1;
}
 
ICON_TAB icon_tab[] = {
	{ "sq_green.gif",   0,  170,   0 },
	{ "sq_blue.gif",    0,    0, 220 },
	{ "sq_red.gif",    220,   0,   0 },
	{ "sq_orange.gif", 222,  84,   0 },
	{ "sq_yellow.gif", 242, 242,   0 }
};

static void mkIcon(ICON_TAB *tp) {
	FILE *out;
	gdImagePtr im;
	int bgcol;

	if ((im=gdImageCreate(10, 8)) != NULL) {
		bgcol = gdImageColorAllocate(im, tp->color[0], tp->color[1], tp->color[2]);
#if defined(lint)		/* first color allocated is background */
		bgcol = bgcol;
#endif

		gdImageInterlace(im, 1);
		if ((out=fopen(tp->name, "w")) != NULL) {
			gdImageGif(im, out);
			(void) fclose(out);
		}
		gdImageDestroy(im);
	}
	return;
}

void checkForIcons(void) {
	int idx;

	for (idx=0; idx < TABSIZE(icon_tab); idx++)
		if (access(icon_tab[idx].name, 0))
			mkIcon(&icon_tab[idx]);
	return;
}