in reply to Creating X BitMap (XBM) images with directional gradients

It will surprise few, if any, of you, that I had a go at recreating this with PDL. Some moderately neat things that came out of this: The latter two problems have been fixed on PDL git master and will be released soon(ish), once I've finished updating PDL::Graphics::Simple to support the needs of various main-PDL demos (and adding/fixing any PDL deficiencies that result from that), after which I will be generating web-output versions of all the PDL demos and put them on the website. In the meantime, if you have the lust for life to use PDL on git master, you can run the script below to make various sizes and orientations of XBMs fading about the place!
#!/usr/bin/env perl use 5.032; use warnings; use autodie ':all'; use Path::Tiny; use PDL; use PDL::GSL::RNG; die "Usage: $0 name\n" unless @ARGV == 1; my $name = $ARGV[0]; my ($base, $size, $orient) = split /_/, $name; my %bits_for = (s => 16, m => 32, l => 48, b => 96); die "Unknown size '$size'" unless exists $bits_for{$size}; my $bits = $bits_for{$size}; my $matrix = create_matrix($bits); my $oriented = orient_matrix($orient, $matrix); my $filename = get_filename($name, $bits); write_xbm($filename, $oriented); sub create_matrix { my ($bits) = @_; my $ones = ones(byte, ($bits+1)*$bits/2)->tritosquare; my $rng = PDL::GSL::RNG->new('mt19937'); $rng->ran_shuffle_1d($ones); $ones; } my %DISPATCH_TABLE; BEGIN { %DISPATCH_TABLE = ( n => sub { $_[0]->copy }, s => sub { $_[0]->slice(',-1:0')->sever }, e => sub { $_[0]->transpose->sever }, w => sub { $_[0]->slice(',-1:0')->transpose->sever }, ); } sub orient_matrix { my ($orient, $matrix) = @_; my $code = $DISPATCH_TABLE{$orient} or die "Unknown orientation '$orient'"; return $code->($matrix); } sub write_xbm { my ($filename, $matrix) = @_; $matrix->wpic($filename); } sub get_filename { my ($name, $size) = @_; my $dir = '.'; my @files = do { opendir(my $dh, $dir) or die "$dir: $!"; grep /^\Q$name\E_\d{4}/, readdir $dh; }; my $index = '0000'; if (@files) { $index = (sort { $b cmp $a } map /^\Q$name\E_(\d{4})/, @files) +[0]; ++$index; } my $indexed_name = $name . '_' . $index; my $xbm_file = $indexed_name . '.xbm'; return path($dir, $xbm_file); }