forked from measurement-factory/ipv4-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshade.c
70 lines (64 loc) · 1.38 KB
/
shade.c
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
/*
* IPv4 Heatmap
* (C) 2007 The Measurement Factory, Inc
* Licensed under the GPL, version 2
* http://maps.measurement-factory.com/
*/
/*
* Shading routines
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <gd.h>
#include "bbox.h"
extern gdImagePtr image;
extern int debug;
void
shade_cidr(const char *cidr, unsigned int rgb, int alpha)
{
bbox box = bbox_from_cidr(cidr);
int color = gdImageColorAllocateAlpha(image,
rgb >> 16,
(rgb >> 8) & 0xFF,
rgb & 0xFF,
alpha);
if (box.xmin != box.xmax || box.ymin != box.ymax)
bbox_draw_filled(box, image, color);
else
bbox_draw_outline(box, image, color);
}
/*
* Input is a text file with TAB-separated fields First field is a CIDR address
* Second field is an RGB value Third field is an alpha value
*/
void
shade_file(const char *fn)
{
char buf[512];
FILE *fp = fopen(fn, "r");
if (NULL == fp)
err(1, "%s", fn);
while (NULL != fgets(buf, 512, fp)) {
char *cidr;
char *rgbhex;
char *alpha_str;
unsigned int rgb;
int alpha;
cidr = strtok(buf, "\t");
if (NULL == cidr)
continue;
rgbhex = strtok(NULL, "\t\r\n");
if (NULL == rgbhex)
continue;
rgb = strtol(rgbhex, NULL, 16);
alpha_str = strtok(NULL, "\t\r\n");
if (NULL == alpha_str)
continue;
alpha = strtol(alpha_str, NULL, 10);
shade_cidr(cidr, rgb, alpha);
}
fclose(fp);
}