3 # This program connects to the database and creates one image at a time in a loop
4 # It can thus be run in parallel
16 if ($ARGV[0] eq '-f' ) {
27 my $run = $json->decode($data);
32 my (@min, @max); # bounding box so we can know how big to make the image
36 # get the vertex coordinates for each hex
39 foreach my $hex (@{$run->{hexlist}}) {
40 my $c = `./hexverts $scale $hex`;
44 warn "$i $hex = $c\n";
47 %coords = do 'verts.pl';
49 #warn keys %coords, "\n";
55 my $elems = @{$run->{gendata}};
56 my $perfork = int($elems / $forks);
57 my $leftover = $elems - $perfork * $forks;
60 for my $fork (0..$forks-1) {
61 $forks[$fork] = { start => $start, end => $start + $perfork-1 };
62 $forks[$fork]{end}++ if $fork < $leftover;
63 $start = $forks[$fork]{end}+1;
67 my $forkinfo = join(' ', map { sprintf('[%s %s]', $_->{start}, $_->{end}) } @forks);
68 warn "forks = $forkinfo\n";
70 while (my $f = shift @forks) {
73 foreach my $gen (@{$run->{gendata}}[$f->{start}..$f->{end}]) {
74 my $image = Image::Magick->new(size => '364x404');
75 $image->ReadImage('canvas:white');
76 # foreach (qw(background fill size colorspace)) {
77 # warn "$_ ", $image->Get($_), "\n";
81 foreach my $hex (@{$gen->{hexes}}) {
82 my $frac = $hex->{freq}[0]/$hex->{pop}/2.0;
83 my $maxgenfrac = $hex->{pop} / $gen->{maxpop};
85 (4.0 / (1.0 + exp(-($frac - 0.5 ))) - 1.5) / 2.0 + 0.075, # hue
87 0.15+0.85*sqrt($maxgenfrac) # value
94 my $color = sprintf('hsb(%f%%, %f%%, %f%%)', 100 * $hsv[0], 100 * $hsv[1], 100 * $hsv[2]);
95 #warn "drawing $color\n";
96 die "missing coords for hex ", $hex->{hex}, "\n" unless exists $coords{$hex->{hex}};
97 $image->Draw(fill => $color, primitive=>'polygon', antialias => 1, points => $coords{$hex->{hex}});
98 $drawn{$hex->{hex}} = 1;
101 foreach my $hex (keys %coords) {
102 next if $drawn{$hex};
103 $image->Draw(fill => 'black', primitive=>'polygon', points => $coords{$hex});
106 # make a band on the top for a label
107 # make sure this is a multiple of four, some
108 # of the movie formats require it
109 $image->Splice(geometry => '0x24', background => 'white', gravity => 'North');
111 # now annotate the image
112 my $x = 5; # where to put the annotation
114 my $font = 'SourceCodePro-Light.ttf';
116 font => $font, fill => 'black', stroke => 'black',
117 pointsize => 15, antialias => 1,
120 # set up a title caption
121 $image->Annotate(%params, x => 5, y => 16, text => sprintf('-m %g -s %u', $run->{options}->{capfactor}, $run->{seed}));
123 sprintf('gen: %8d', $gen->{gen}),
124 sprintf(q{pop: %8d}, $gen->{pop}),
125 sprintf(q{ybp: %8d}, $gen->{year}),
129 $image->Annotate(%params, x => $x, y => $y, text => $_);
132 # $text = sprintf(q{ams: %8d}, $genlen*$gen);
133 # $image->Annotate(x => $x, y => $y, -text => $text); $y += 15;
135 my $prefix = $ENV{IMGPREFIX};
138 my $name = $prefix . sprintf('%04d.png', $gen->{gen});
139 $image->Write($name);
140 warn sprintf("%s gen %d pop %d\n", $name, $gen->{gen}, $gen->{pop});
145 while (wait != -1) {};
149 foreach my $comp (@_) {
151 mkdir $path unless -d $path;
152 die "can't make director $path: $!" unless -d $path;