9 void err(int ex, const char *fmt, ...)
14 vfprintf(stderr, fmt, list);
16 fputs(strerror(e), stderr);
21 void errx(int ex, const char *fmt, ...)
25 vfprintf(stderr, fmt, list);
30 typedef void (*colorfunc_t) (double x, double y, double dx, double dy, double *r, double *g, double *b);
34 return rand() / (RAND_MAX + 1.0);
37 double softclip(double x, double a)
39 // don't ask what this does - but it works
40 double cse = (2*a*x - x - a + 1) * x;
41 return cse / (cse + (1 - a));
44 void writepic(colorfunc_t f, const char *fn, int width, int height)
49 FILE *file = fopen(fn, "wb");
51 err(1, "fopen >%s", fn);
53 memset(tga, 0, sizeof(tga));
54 tga[2] = 2; // uncompressed type
55 tga[12] = (width >> 0) & 0xFF;
56 tga[13] = (width >> 8) & 0xFF;
57 tga[14] = (height >> 0) & 0xFF;
58 tga[15] = (height >> 8) & 0xFF;
59 tga[16] = 24; // pixel size
61 if(fwrite(&tga, sizeof(tga), 1, file) != 1)
62 err(1, "fwrite >%s", fn);
63 //for(y = height-1; y >= 0; --y)
64 for(y = 0; y < height; ++y)
65 for(x = 0; x < width; ++x)
70 xx = (x + 0.5) / width;
71 yy = (y + 0.5) / height;
72 f(xx, yy, 0.5 / width, 0.5 / height, &rr, &gg, &bb);
73 rgb[2] = floor(rnd() + rr * 255);
74 rgb[1] = floor(rnd() + gg * 255);
75 rgb[0] = floor(rnd() + bb * 255);
76 if(fwrite(rgb, sizeof(rgb), 1, file) != 1)
77 err(1, "fwrite >%s", fn);
90 plotpoint_t *plotpoints;
91 size_t nPlotpoints, allocatedPlotpoints;
93 void readpoints(const char *fn)
97 FILE *infile = fopen(fn, "r");
99 err(1, "fopen <%s", fn);
101 nPlotpoints = allocatedPlotpoints = 0;
104 while(fgets(buf, sizeof(buf), infile))
108 fputs(buf + 1, stdout);
111 if(nPlotpoints >= allocatedPlotpoints)
113 if(allocatedPlotpoints == 0)
114 allocatedPlotpoints = 1024;
116 allocatedPlotpoints = nPlotpoints * 2;
117 plotpoints = (plotpoint_t *) realloc(plotpoints, allocatedPlotpoints * sizeof(*plotpoints));
119 if(sscanf(buf, "%lf %lf %lf %d", &plotpoints[nPlotpoints].x, &plotpoints[nPlotpoints].y, &plotpoints[nPlotpoints].dist, &plotpoints[nPlotpoints].weapon) != 4)
125 void calcplot1(double x, double y, double *out, double sigma2)
129 double val, totalval = 0, weight, totalweight = 0;
131 for(i = 0; i < nPlotpoints; ++i)
133 dist2 = (x - plotpoints[i].x) * (x - plotpoints[i].x) + (y - plotpoints[i].y) * (y - plotpoints[i].y);
134 weight = 1; // / plotpoints[i].dist;
135 val = exp(-dist2 / sigma2);
137 totalweight += weight;
138 totalval += weight * val;
141 *out = softclip(totalval / (totalweight * sqrt(sigma2 * 2 * M_PI)), 0.8);
144 void calcplotp(double x, double y, double dx, double dy, double *out)
149 for(i = 0; i < nPlotpoints; ++i)
151 distx = x - plotpoints[i].x;
152 disty = y - plotpoints[i].y;
165 void calcplot(double x, double y, double dx, double dy, double *r, double *g, double *b)
167 calcplot1(x, y, r, 1/64.0);
168 calcplot1(x, y, g, 1/512.0);
169 calcplot1(x, y, b, 1/4096.0);
170 calcplotp(x, y, dx, dy, b);
173 int main(int argc, char **argv)
176 errx(1, "Usage: %s infile.plot outfile.tga", *argv);
179 writepic(calcplot, argv[2], 512, 512);