#!/usr/bin/perl

# Stars.pl writes out a GIF file with a distribution of funky-looking
# stars.
use GD;
use strict;
use vars qw($W $H $im @stars %pixels);
binmode STDOUT;
srand();

# The image width and height
($W,$H)=(500,500);
# The magnitude-frequency distribution
my(@count)=qw(244 98 39 16 6 3 1);

# The star is given by an 81 pixel pattern
# Each row of nine pixels can be represented by a binary number
# (1 for pixel on, 0 for pixel off)
# The following is the list of numbers
%pixels = (
	"0"=>"000000000",
	"16"=>"000010000",
	"24"=>"000110000",
	"56"=>"000111000",
	"84"=>"001010100",
	"146"=>"010010010",
	"254"=>"011111110",
	"511"=>"111111111");

# A star of a given magnitude has nine rows, each with one of the above
# numbers.
@stars = (
	[0,0,0,0,16,0,0,0,0],
	[0,0,0,24,24,0,0,0,0],
	[0,0,0,16,56,16,0,0,0],
	[0,0,16,56,124,56,16,0,0],
	[0,16,16,56,254,56,16,16,0],
	[16,16,84,56,511,56,84,16,16],
	[16,146,84,56,511,56,84,146,16]);

# Create the image and allocate colors
$im = new GD::Image($W,$H); 
my $black = $im->colorAllocate(0,0,0);
my $white = $im->colorAllocate(255,255,255);

# make it an interlaced GIF
$im->interlaced('true');


my $N=0; my $i=0;

foreach $N (0..$#count) { foreach (1..$count[$N]) {
	my ($x,$y) = (int(rand($W)),int(rand($H))); 
	$b = $y;
	foreach $i (0..8) { # read each row of pixel values
		$a = $x;
		my $row = $pixels{$stars[$N][$i]};
		$row=~/(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)/;
		my @arr = ($1,$2,$3,$4,$5,$6,$7,$8,$9);
		foreach $_ (@arr) { 
			if($_ eq "1") { 
			$im->rectangle($a,$b,$a+1,$b+1,$white); }
			++$a;
			if($a>$W) { $a -= $W+1; }
			} # end for @arr
		++$b;
		if($b>$H) { $b -= $H+1; }
		} # end for i
	} # end for N
} # end for count

print $im->gif;