forked from measurement-factory/ipv4-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.c
89 lines (81 loc) · 2.1 KB
/
annotate.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* IPv4 Heatmap
* (C) 2007 The Measurement Factory, Inc
* Licensed under the GPL, version 2
* http://maps.measurement-factory.com/
*/
/*
* Place annotations (text) on the image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <gd.h>
#include "ipv4-heatmap.h"
#include "bbox.h"
#include "text.h"
/*
* FONT_ALPHA sets the transparency for the annotations. in libgd, 0 means 100%
* transparent, and 127 means 100% opaque.
*/
#define FONT_ALPHA 75
int annotateColor = -1;
/*
* Calculate the bounding box, draw an outline of the box, then render the
* text.
*/
static void
annotate_cidr(gdImagePtr image, const char *cidr, const char *label, const char *sublabel)
{
bbox box = bbox_from_cidr(cidr);
if (box.xmin < 0) {
fprintf(stderr, "Warning: annotation %s is out of range for this image\n", cidr);
return;
}
bbox_draw_outline(box, image, annotateColor);
text_in_bbox(image, label, box, annotateColor, 128.0);
if (sublabel) {
bbox box2 = box;
box2.ymin = (box.ymin + box.ymax) / 2 + (int)(_text_last_sz / 2) + 6;
box2.ymax = box2.ymin + 24;
if (0 == strcmp(sublabel, "prefix"))
sublabel = cidr;
text_in_bbox(image, sublabel, box2, annotateColor, 12.0);
}
}
/*
* Input is a text file with TAB-separated fields First field is a CIDR address
* Second field is a text label
*/
void
annotate_file(gdImagePtr image, const char *fn)
{
char buf[512];
FILE *fp = fopen(fn, "r");
if (NULL == fp)
err(1, "%s", fn);
if (annotateColor < 0) {
if (reverse_flag)
annotateColor = gdImageColorAllocateAlpha(image, 0, 0, 0, FONT_ALPHA);
else
annotateColor = gdImageColorAllocateAlpha(image, 255, 255, 255, FONT_ALPHA);
}
if (!gdFTUseFontConfig(1))
warnx("fontconfig not available");
while (NULL != fgets(buf, 512, fp)) {
char *cidr;
char *label;
char *sublabel = NULL;
cidr = strtok(buf, "\t");
if (NULL == cidr)
continue;
label = strtok(NULL, "\t\r\n");
if (NULL == label)
continue;
sublabel = strtok(NULL, "\t\r\n");
annotate_cidr(image, cidr, label, sublabel);
}
fclose(fp);
}