8 typedef void (*colorfunc_t) (double x, double y, double dx, double dy, double *r, double *g, double *b);
12 return rand() / (RAND_MAX + 1.0);
15 double softclip(double x, double a)
17 // don't ask what this does - but it works
18 double cse = (2*a*x - x - a + 1) * x;
19 return cse / (cse + (1 - a));
22 void writepic(colorfunc_t f, const char *fn, int width, int height)
27 FILE *file = fopen(fn, "wb");
29 err(1, "fopen >%s", fn);
31 memset(tga, 0, sizeof(tga));
32 tga[2] = 2; // uncompressed type
33 tga[12] = (width >> 0) & 0xFF;
34 tga[13] = (width >> 8) & 0xFF;
35 tga[14] = (height >> 0) & 0xFF;
36 tga[15] = (height >> 8) & 0xFF;
37 tga[16] = 24; // pixel size
39 if(fwrite(&tga, sizeof(tga), 1, file) != 1)
40 err(1, "fwrite >%s", fn);
41 //for(y = height-1; y >= 0; --y)
42 for(y = 0; y < height; ++y)
43 for(x = 0; x < width; ++x)
50 xx = (x + 0.5) / width;
51 yy = (y + 0.5) / height;
52 f(xx, yy, 0.5 / width, 0.5 / height, &rr, &gg, &bb);
53 rgb[2] = floor(rnd() + rr * 255);
54 rgb[1] = floor(rnd() + gg * 255);
55 rgb[0] = floor(rnd() + bb * 255);
56 if(fwrite(rgb, sizeof(rgb), 1, file) != 1)
57 err(1, "fwrite >%s", fn);
70 plotpoint_t *plotpoints;
71 size_t nPlotpoints, allocatedPlotpoints;
73 void readpoints(const char *fn)
77 FILE *infile = fopen(fn, "r");
79 err(1, "fopen <%s", fn);
81 nPlotpoints = allocatedPlotpoints = 0;
84 while(fgets(buf, sizeof(buf), infile))
88 fputs(buf + 1, stdout);
91 if(nPlotpoints >= allocatedPlotpoints)
93 if(allocatedPlotpoints == 0)
94 allocatedPlotpoints = 1024;
96 allocatedPlotpoints = nPlotpoints * 2;
97 plotpoints = (plotpoint_t *) realloc(plotpoints, allocatedPlotpoints * sizeof(*plotpoints));
99 if(sscanf(buf, "%lf %lf %lf %d", &plotpoints[nPlotpoints].x, &plotpoints[nPlotpoints].y, &plotpoints[nPlotpoints].dist, &plotpoints[nPlotpoints].weapon) != 4)
105 void calcplot1(double x, double y, double *out, double sigma2)
109 double val, totalval = 0, weight, totalweight = 0;
111 for(i = 0; i < nPlotpoints; ++i)
113 dist2 = (x - plotpoints[i].x) * (x - plotpoints[i].x) + (y - plotpoints[i].y) * (y - plotpoints[i].y);
114 weight = 1; // / plotpoints[i].dist;
115 val = exp(-dist2 / sigma2);
117 totalweight += weight;
118 totalval += weight * val;
121 *out = softclip(totalval / (totalweight * sqrt(sigma2 * 2 * M_PI)), 0.8);
124 void calcplotp(double x, double y, double dx, double dy, double *out)
129 for(i = 0; i < nPlotpoints; ++i)
131 distx = x - plotpoints[i].x;
132 disty = y - plotpoints[i].y;
145 void calcplot(double x, double y, double dx, double dy, double *r, double *g, double *b)
147 calcplot1(x, y, r, 1/64.0);
148 calcplot1(x, y, g, 1/512.0);
149 calcplot1(x, y, b, 1/4096.0);
150 calcplotp(x, y, dx, dy, b);
153 int main(int argc, char **argv)
158 errx(1, "Usage: %s infile.plot outfile.tga", *argv);
161 writepic(calcplot, argv[2], 512, 512);