#! /usr/bin/perl -w
#*****************************************************************
# The following lines generate an XPM-File
# The image should aproximate a cloud
#*****************************************************************

use POSIX;
$zp=257; #Gives the size of the picture in pixels, should be 2^n+1

sub stringfromnumber { my ($nr) = @_;
  my $a=int($nr/26);
  my $r=$nr-($a*26);
  @abc=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','
x','y','z','#','.');
  return $abc[$a].$abc[$r];
}

sub zufall { my ($wu,$wo,$gu,$go,$br) = @_;
  my $minus=int($br/2);
  my $mw=int(($wu+$wo)/2);
  if ($mw-$minus < $gu) {
    $minus=$mw-$gu;
    $br=$br-$minus+$mw-$gu;
  }
  if ($mw-$minus+$br > $go) {
    $br=$go-$mw+$minus;
  }
  return $mw-$minus+int(rand($br+1));
}


sub min { my ($a,$b) = @_;
  if ($a < $b) { 
    return $a;
  } else {
    return $b;
  }
}

sub max { my ($a,$b) = @_;
  if ($a < $b) {
    return $b;
  } else {
    return $a;
  }
}


sub color { my ($ox,$oy,$ux,$uy,$rm) = @_;
  if ($ux-$ox > 1 && $uy-$oy > 1) {
     my $mx=($ox+$ux)/2;
     my $my=($oy+$uy)/2;

     if ($a[$mx][$oy] == 0) {
     $a[$mx][$oy]=zufall($a[$ox][$oy],$a[$ux][$oy],1,255,$rm);
     }

     if ($a[$mx][$uy] == 0) {
     $a[$mx][$uy]=zufall($a[$ox][$uy],$a[$ux][$uy],1,255,$rm);
     }

     if ($a[$ox][$my] == 0) {
     $a[$ox][$my]=zufall($a[$ox][$oy],$a[$ox][$uy],1,255,$rm);
     }

     if ($a[$ux][$my] == 0) {
     $a[$ux][$my]=zufall($a[$ux][$oy],$a[$ux][$uy],1,255,$rm);
     }

     if ($a[$mx][$my] == 0) {
     $a[$mx][$my]=zufall(($a[$ox][$my]+$a[$ux][$my])/2,($a[$mx][$oy]+$a[$mx][$uy])/2,1,255,$rm);
     }

     if ($ux-$ox > 2 && $uy-$oy > 2) {
       color($ox,$oy,$mx,$my,ceil($rm/2));
       color($mx,$oy,$ux,$my,ceil($rm/2));
       color($ox,$my,$mx,$uy,ceil($rm/2));
       color($mx,$my,$ux,$uy,ceil($rm/2));
     }
  }
}

for($i=1;$i<=$zp;$i++) {
  for($j=1;$j<=$zp;$j++) {
    $a[$i][$j]=0;
  }
}


setpriority 0,0,15;

$a[1][1]=int(rand(256));
$a[1][$zp]=150;#int(rand(256));
$a[$zp][1]=int(rand(256));
$a[$zp][$zp]=200; #int(rand(256));
$a[($zp+1)/2][($zp+1)/2]=200;
 
color(1,1,$zp,$zp,127);

print "/* XPM */\n";
print "static char *wolken[] = {\n";
print "\"  $zp $zp 256 2\",\n";
print "/* colors */\n";
for ($i=0;$i<256;$i++) {
  print "\"";
  print stringfromnumber($i)." c #";
  printf("%02x%02xff\",\n",$i,$i);
}
print "/* pixels */\n";


for($i=1;$i<=$zp;$i++) {
  print "\"";
  for($j=1;$j<=$zp;$j++) {
    print stringfromnumber($a[$i][$j]);
  }
  print"\"";
  if ($i < $zp) {
    print ",";
  }
  print "\n";
}
print "};\n";

