#!/usr/bin/perl

use Image::Magick;
use strict;

my $TBH = 22;
my $O   = $ENV{O} || "../";

my %region = (
    "AmenuButton" => new Region("24x18+1+1"),
    "OmenuButton" => new Region("24x18+1+21"),
    "PmenuButton" => new Region("24x18+1+41"),
    "Adepth"      => new Region("24x18+25+1"),
    "Odepth"      => new Region("24x18+25+21"),
    "Pdepth"      => new Region("24x18+25+41"),
    "Ahide"       => new Region("24x18+49+1"),
    "Ohide"       => new Region("24x18+49+21"),
    "Phide"       => new Region("24x18+49+41"),
    "Arollup"     => new Region("24x18+74+1"),
    "Orollup"     => new Region("24x18+74+21"),
    "Prollup"     => new Region("24x18+74+41"),
    "Arolldown"   => new Region("24x18+74+61"),
    "Orolldown"   => new Region("24x18+74+81"),
    "Prolldown"   => new Region("24x18+74+101"),
    "Aminimize"   => new Region("23x18+98+1"),
    "Ominimize"   => new Region("23x18+98+21"),
    "Pminimize"   => new Region("23x18+98+41"),
    "Amaximize"   => new Region("23x18+121+1"),
    "Omaximize"   => new Region("23x18+121+21"),
    "Pmaximize"   => new Region("23x18+121+41"),
    "Arestore"    => new Region("23x18+121+61"),
    "Orestore"    => new Region("23x18+121+81"),
    "Prestore"    => new Region("23x18+121+101"),
    "Aclose"      => new Region("33x18+144+1"),
    "Oclose"      => new Region("33x18+144+21"),
    "Pclose"      => new Region("33x18+144+41"),
);

foreach my $b (qw(menuButton depth hide rollup rolldown minimize maximize
 restore close))
{
    my($in_active, $in_hover, $in_pressed, $in_aback, $in_iback);
    my($out_size, $out);

    $in_active = Image::Magick->new();
    $in_active->Read("icevista.png");
    $in_active->Crop($region{"A$b"}->geo());

    $in_hover = Image::Magick->new();
    $in_hover->Read("icevista.png");
    $in_hover->Crop($region{"O$b"}->geo());

    $in_pressed = Image::Magick->new();
    $in_pressed->Read("icevista.png");
    $in_pressed->Crop($region{"P$b"}->geo());

    $out_size = sprintf "%dx%d!", $region{"A$b"}->sx(), $TBH;
    $in_aback = Image::Magick->new();
    $in_aback->Read("k_titleAT.png");
    $in_aback->Resize(geometry => $out_size);

    $in_iback = Image::Magick->new();
    $in_iback->Read("k_titleIT.png");
    $in_iback->Resize(geometry => $out_size);

    $out_size = sprintf "%dx%d", $region{"A$b"}->sx(), 2 * $TBH;
    $out = Image::Magick->new(size => $out_size);
    $out->Read("xc:transparent");
    $out->Composite(image => $in_aback, y => 0);
    $out->Composite(image => $in_aback, y => $TBH);
    $out->Composite(image => $in_active, y => 0);
    $out->Composite(image => $in_pressed, y => $TBH);
    $out->Write("$O${b}A.xpm");
    print "Wrote $O${b}A.xpm\n";

    $out = Image::Magick->new(size => $out_size);
    $out->Read("xc:transparent");
    $out->Composite(image => $in_iback, y => 0);
    $out->Composite(image => $in_iback, y => $TBH);
    $out->Composite(image => $in_active, y => 0);
    $out->Composite(image => $in_pressed, y => $TBH);
    $out->Write("$O${b}I.xpm");
    print "Wrote $O${b}I.xpm\n";

    $out = Image::Magick->new(size => $out_size);
    $out->Read("xc:transparent");
    $out->Composite(image => $in_aback, y => 0);
    $out->Composite(image => $in_aback, y => $TBH);
    $out->Composite(image => $in_hover, y => 0);
    $out->Composite(image => $in_pressed, y => $TBH);
    $out->Write("$O${b}O.xpm");
    print "Wrote $O${b}O.xpm\n";
}

package Region;
sub new {
    my $this = shift @_;
    my($sx, $sy, $px, $py) = ($_[0] =~ /^(?:(\d+)x(\d+))?(?:\+(\d+)\+(\d+))?/);
    return bless [$sx, $sy, $px, $py, $_[0]];
}
sub expand {
    my $this = shift @_;
    if(defined($_[0]) && $_[0] > $this->[0]) {
        $this->[0] = $_[0];
    }
    if(defined($_[1]) && $_[1] > $this->[1]) {
        $this->[1] = $_[1];
    }
    return $this;
}
sub sx {
    my $this = shift @_;
    if(defined($_[0])) {
        $this->[0] = $_[0];
        return $this;
    }
    return $this->[0];
}
sub sy {
    my $this = shift @_;
    if(defined $_[0]) {
        $this->[1] = $_[1];
        return $this;
    }
    return $this->[1];
}
sub px {
    my $this = shift @_;
    if(defined($_[0])) {
        $this->[2] = $_[2];
        return $this;
    }
    return $this->[2];
}
sub py {
    my $this = shift @_;
    if(defined $_[3]) {
        $this->[3] = $_[3];
        return $this;
    }
    return $this->[3];
}
sub geo {
    return shift(@_)->[4];
}

#<<eof>>
