#!/usr/local/bin/perl
#
# xpmmergemask
#
# Copyright (C) 1998,2000 Taiji Yamada <taiji@aihara.co.jp>

require 'getopts.pl';
&Getopts('m:')
    || die "usage: $0 -m mask.xpm < in > out\n".
    "\tmask.xpm ... xpm file which includes transparent pixels.\n";

if (!$opt_m) {
  goto ignore;
}

open(MASK,"$opt_m");
$header = 0;
$name = ""; $chars_per_pixel = 0; $chars_per_line = 0;
$colors = 0;
$pixels = 0;
$pixels_flag = 0;
$y = 0;
%image_buf = ();
while (<MASK>) {
  if (/\/\*\s*XPM\s\*\//) {
    $header = !0;
  }
  elsif ($header && $chars_per_pixel
         && (/\/\*\s*colors\s\*\//
             || /\"[^\"]{$chars_per_pixel,$chars_per_pixel}\s+((c|m|g|g4|s)\s+[^\"]+)+\"/)) {
    $colors = !0;
  }
  elsif ($header && $colors && $chars_per_line
         && (/\/\*\s*pixels\s\*\//
             || /\"[^\"]{$chars_per_line,$chars_per_line}\"/)) {
    $pixels = !0;
  }
  if ($header && !$colors && !$pixels) {
    if (/static\s+char\s+\*\s*(.*)\[\]\s*=\s*\{/) {
      $name = $1;
    }
    elsif (/\"\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*\"/) {
      ($width,$height,$num_colors,$chars_per_pixel) = ($1,$2,$3,$4);
      $chars_per_line = $width*$chars_per_pixel;
    }
  }
  elsif ($colors && !$pixels) {
    if (/\"([^\"]{$chars_per_pixel,$chars_per_pixel})\s+((c|m|g|g4|s)\s+[^\"]+)+\"/) {
      ($pixel,$value) = ($1,$2);
      if ($value =~ /[nN]one/ || $value =~ /[tT]ransparent/) {
        $mask = $pixel;
      }
    }
  }
  elsif ($pixels) {
    if (!$pixels_flag) {
      ;
      $pixels_flag = !0;
    }
    if (/\"([^\"]*)\"/) {
      $buf = $1;
      for ($x=0; $x<$width; $x++) {
        if (substr($buf,$x*$chars_per_pixel,$chars_per_pixel) eq $mask) {
          $image_buf{"$x,$y"} = "None";
        }
        else {
          $image_buf{"$x,$y"} = "black";
        }
      }
      $y++;
    }
  } 
}
close(MASK);

ignore:

$header = 0;
$name = ""; $chars_per_pixel = 0; $chars_per_line = 0;
$colors = 0;
$pixels = 0;
$pixels_flag = 0;
$y = 0;
$mask = "";
%pixel_values = ();
while (<>) {
  if (/\/\*\s*XPM\s\*\//) {
    $header = !0;
  }
  elsif ($header && $chars_per_pixel
         && (/\/\*\s*colors\s\*\//
             || /\"[^\"]{$chars_per_pixel,$chars_per_pixel}\s+((c|m|g|g4|s)\s+[^\"]+)+\"/)) {
    $colors = !0;
  }
  elsif ($header && $colors && $chars_per_line
         && (/\/\*\s*pixels\s\*\//
             || /\"[^\"]{$chars_per_line,$chars_per_line}\"/)) {
    $pixels = !0;
  }
  if ($header && !$colors && !$pixels) {
    if (/static\s+char\s+\*\s*(.*)\[\]\s*=\s*\{/) {
      $name = $1;
      print "/* XPM */\n";
      print "static char *$name\[\] = {\n";
    }
    elsif (/\"\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*\"/) {
      ($width,$height,$num_colors,$chars_per_pixel) = ($1,$2,$3,$4);
      $chars_per_line = $width*$chars_per_pixel;
      print "/* width height num_colors chars_per_pixel */\n";
    }
  }
  elsif ($colors && !$pixels) {
    if (/\"([^\"]{$chars_per_pixel,$chars_per_pixel})\s+((c|m|g|g4|s)\s+[^\"]+)+\"/) {
      ($pixel,$value) = ($1,$2);
      if ($value =~ /[nN]one/ || $value =~ /[tT]ransparent/) {
        $mask = $pixel;
      }
      $value =~ s/([tT]ransparent|none)/None/;
      $pixel_values{$pixel} = $value;
    }
  }
  elsif ($pixels) {
    if (!$pixels_flag) {
      if (!$mask) {
        $num_colors++;
      }
      print "\"$width $height $num_colors $chars_per_pixel\",\n";
      print "/* colors */\n";
      foreach $pixel ((@pixels=sort(keys(%pixel_values)))) {
        print "\"$pixel $pixel_values{$pixel}\",\n";
      }
      if (!$mask) {
        $pixel = $pixels[@pixels-1];
        $mask = $pixel;
        substr($mask,$chars_per_pixel-1,1)
            = chr(ord(substr($pixel,$chars_per_pixel-1,1))+1);
        $pixel_values{$mask} = "c None";
        print "\"$mask $pixel_values{$mask}\",\n";
      }
      print "/* pixels */\n";
      $pixels_flag = !0;
    }
    if (/\"([^\"]*)\"/) {
      $buf = $1;
      print "\"";
      for ($x=0; $x<$width; $x++) {
        if ($image_buf{"$x,$y"} eq "None") {
          print "$mask";
        }
        else {
          print substr($buf,$x*$chars_per_pixel,$chars_per_pixel);
        }
      }
      print "\",\n";
      $y++;
    }
  } 
}
print "};\n";
