9 void err(int ex, const char *fmt, ...)
14 vfprintf(stderr, fmt, list);
16 fputs(strerror(e), stderr);
21 typedef void (*colorfunc_t) (double x, double y, double dx, double dy, double *r, double *g, double *b);
25 return rand() / (RAND_MAX + 1.0);
28 double softclip(double x, double a)
30 // don't ask what this does - but it works
31 double cse = (2*a*x - x - a + 1) * x;
32 return cse / (cse + (1 - a));
35 void writepic(colorfunc_t f, const char *fn, int width, int height)
40 FILE *file = fopen(fn, "wb");
42 err(1, "fopen >%s", fn);
44 memset(tga, 0, sizeof(tga));
45 tga[2] = 2; // uncompressed type
46 tga[12] = (width >> 0) & 0xFF;
47 tga[13] = (width >> 8) & 0xFF;
48 tga[14] = (height >> 0) & 0xFF;
49 tga[15] = (height >> 8) & 0xFF;
50 tga[16] = 24; // pixel size
52 if(fwrite(&tga, sizeof(tga), 1, file) != 1)
53 err(1, "fwrite >%s", fn);
54 //for(y = height-1; y >= 0; --y)
55 for(y = 0; y < height; ++y)
56 for(x = 0; x < width; ++x)
63 xx = (x + 0.5) / width;
64 yy = (y + 0.5) / height;
65 f(xx, yy, 0.5 / width, 0.5 / height, &rr, &gg, &bb);
66 rgb[2] = floor(rnd() + rr * 255);
67 rgb[1] = floor(rnd() + gg * 255);
68 rgb[0] = floor(rnd() + bb * 255);
69 if(fwrite(rgb, sizeof(rgb), 1, file) != 1)
70 err(1, "fwrite >%s", fn);
83 plotpoint_t *plotpoints;
84 size_t nPlotpoints, allocatedPlotpoints;
86 void readpoints(const char *fn)
90 FILE *infile = fopen(fn, "r");
92 err(1, "fopen <%s", fn);
94 nPlotpoints = allocatedPlotpoints = 0;
97 while(fgets(buf, sizeof(buf), infile))
101 fputs(buf + 1, stdout);
104 if(nPlotpoints >= allocatedPlotpoints)
106 if(allocatedPlotpoints == 0)
107 allocatedPlotpoints = 1024;
109 allocatedPlotpoints = nPlotpoints * 2;
110 plotpoints = (plotpoint_t *) realloc(plotpoints, allocatedPlotpoints * sizeof(*plotpoints));
112 if(sscanf(buf, "%lf %lf %lf %d", &plotpoints[nPlotpoints].x, &plotpoints[nPlotpoints].y, &plotpoints[nPlotpoints].dist, &plotpoints[nPlotpoints].weapon) != 4)
118 void calcplot1(double x, double y, double *out, double sigma2)
122 double val, totalval = 0, weight, totalweight = 0;
124 for(i = 0; i < nPlotpoints; ++i)
126 dist2 = (x - plotpoints[i].x) * (x - plotpoints[i].x) + (y - plotpoints[i].y) * (y - plotpoints[i].y);
127 weight = 1; // / plotpoints[i].dist;
128 val = exp(-dist2 / sigma2);
130 totalweight += weight;
131 totalval += weight * val;
134 *out = softclip(totalval / (totalweight * sqrt(sigma2 * 2 * M_PI)), 0.8);
137 void calcplotp(double x, double y, double dx, double dy, double *out)
142 for(i = 0; i < nPlotpoints; ++i)
144 distx = x - plotpoints[i].x;
145 disty = y - plotpoints[i].y;
158 void calcplot(double x, double y, double dx, double dy, double *r, double *g, double *b)
160 calcplot1(x, y, r, 1/64.0);
161 calcplot1(x, y, g, 1/512.0);
162 calcplot1(x, y, b, 1/4096.0);
163 calcplotp(x, y, dx, dy, b);
166 int main(int argc, char **argv)
171 errx(1, "Usage: %s infile.plot outfile.tga", *argv);
174 writepic(calcplot, argv[2], 512, 512);