File: hitplot2tga.c

package info (click to toggle)
nexuiz-data 2.5.2-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,294,288 kB
  • sloc: ansic: 10,523; perl: 6,845; sh: 2,188; java: 1,417; xml: 969; lisp: 519; ruby: 136; makefile: 125
file content (182 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
#include <errno.h>

void err(int ex, const char *fmt, ...)
{
	va_list list;
	int e = errno;
	va_start(list, fmt);
	vfprintf(stderr, fmt, list);
	fputs(": ", stderr);
	fputs(strerror(e), stderr);
	fputs("\n", stderr);
	exit(ex);
}

void errx(int ex, const char *fmt, ...)
{
    va_list list;
    va_start(list, fmt);
    vfprintf(stderr, fmt, list);
    fputs("\n", stderr);
    exit(ex);
}

typedef void (*colorfunc_t) (double x, double y, double dx, double dy, double *r, double *g, double *b);

double rnd()
{
	return rand() / (RAND_MAX + 1.0);
}

double softclip(double x, double a)
{
	// don't ask what this does - but it works
	double cse = (2*a*x - x - a + 1) * x;
	return cse / (cse + (1 - a));
}

void writepic(colorfunc_t f, const char *fn, int width, int height)
{
	int x, y;
	uint8_t tga[18];

	FILE *file = fopen(fn, "wb");
	if(!file)
		err(1, "fopen >%s", fn);

	memset(tga, 0, sizeof(tga));
	tga[2] = 2;          // uncompressed type
	tga[12] = (width >> 0) & 0xFF;
	tga[13] = (width >> 8) & 0xFF;
	tga[14] = (height >> 0) & 0xFF;
	tga[15] = (height >> 8) & 0xFF;
	tga[16] = 24;        // pixel size

	if(fwrite(&tga, sizeof(tga), 1, file) != 1)
		err(1, "fwrite >%s", fn);
	//for(y = height-1; y >= 0; --y)
	for(y = 0; y < height; ++y)
		for(x = 0; x < width; ++x)
		{
			uint8_t rgb[3];
			double rr, gg, bb;
			double xx, yy;
			xx = (x + 0.5) / width;
			yy = (y + 0.5) / height;
			f(xx, yy, 0.5 / width, 0.5 / height, &rr, &gg, &bb);
			rgb[2] = floor(rnd() + rr * 255);
			rgb[1] = floor(rnd() + gg * 255);
			rgb[0] = floor(rnd() + bb * 255);
			if(fwrite(rgb, sizeof(rgb), 1, file) != 1)
				err(1, "fwrite >%s", fn);
		}
	
	fclose(file);
}

typedef struct
{ 
	double x, y, dist;
	int weapon;
}
plotpoint_t;

plotpoint_t *plotpoints;
size_t nPlotpoints, allocatedPlotpoints;

void readpoints(const char *fn)
{
	char buf[1024];

	FILE *infile = fopen(fn, "r");
	if(!infile)
		err(1, "fopen <%s", fn);

	nPlotpoints = allocatedPlotpoints = 0;
	plotpoints = NULL;

	while(fgets(buf, sizeof(buf), infile))
	{
		if(*buf == '#') 
		{
			fputs(buf + 1, stdout);
			continue;
		}
		if(nPlotpoints >= allocatedPlotpoints)
		{
			if(allocatedPlotpoints == 0)
				allocatedPlotpoints = 1024;
			else
				allocatedPlotpoints = nPlotpoints * 2;
			plotpoints = (plotpoint_t *) realloc(plotpoints, allocatedPlotpoints * sizeof(*plotpoints));
		}
		if(sscanf(buf, "%lf %lf %lf %d", &plotpoints[nPlotpoints].x, &plotpoints[nPlotpoints].y, &plotpoints[nPlotpoints].dist, &plotpoints[nPlotpoints].weapon) != 4)
			continue;
		++nPlotpoints;
	}
}

void calcplot1(double x, double y, double *out, double sigma2)
{
	size_t i;
	double dist2;
	double val, totalval = 0, weight, totalweight = 0;

	for(i = 0; i < nPlotpoints; ++i)
	{
		dist2 = (x - plotpoints[i].x) * (x - plotpoints[i].x) + (y - plotpoints[i].y) * (y - plotpoints[i].y);
		weight = 1; // / plotpoints[i].dist;
		val = exp(-dist2 / sigma2);

		totalweight += weight;
		totalval += weight * val;
	}

	*out = softclip(totalval / (totalweight * sqrt(sigma2 * 2 * M_PI)), 0.8);
}

void calcplotp(double x, double y, double dx, double dy, double *out)
{
	size_t i;
	double distx, disty;

	for(i = 0; i < nPlotpoints; ++i)
	{
		distx = x - plotpoints[i].x;
		disty = y - plotpoints[i].y;

		if(distx < dx)
		if(distx > -dx)
		if(disty < dy)
		if(disty > -dy)
		{
			*out = 1;
			break;
		}
	}
}

void calcplot(double x, double y, double dx, double dy, double *r, double *g, double *b)
{
	calcplot1(x, y, r, 1/64.0);
	calcplot1(x, y, g, 1/512.0);
	calcplot1(x, y, b, 1/4096.0);
	calcplotp(x, y, dx, dy, b);
}

int main(int argc, char **argv)
{
	if(argc != 3)
		errx(1, "Usage: %s infile.plot outfile.tga", *argv);
	
	readpoints(argv[1]);
	writepic(calcplot, argv[2], 512, 512);

	return 0;
}