#!/usr/bin/perl
#require 5.008;
our $VERSION = "0.01"; # 2010-03-15

use utf8;
use strict;
use warnings;

use Image::Magick;

{
  my $image = Image::Magick->new(size => "720x480");
#  $image->ReadImage("xc:black");
  $image->Read("Background.jpg");

  foreach my $p (
		 ["０番フラグ", "Title.jpg", 0, 0],
		 ["１番フラグ", "C1.jpg", 110, 0],
		 ["２番フラグ", "C2.jpg", 220, 0],
		 ["３番フラグ", "C3.jpg", 0, 120],
		 ["４番フラグ", "C4.jpg", 110, 120],
		 ["５番フラグ", "C5.jpg", 220, 120],
		) {
    my ($title, $file, $x, $y) = @$p;
    $x += 80;
    $y += 200;
    my $thumbnail = Image::Magick->new;
    $thumbnail->Read($file);
    $thumbnail->Crop(geometry => "706x478+6+1");
    $thumbnail->Resize(geometry => "100x68");
    $image->Composite(image => $thumbnail,
		      compose => "Atop",
		      x => $x, y => $y
		     );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => $title,
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => $title,
		    );
    my $s = $file;
    $s =~ s/\.jpg$//;
  }

  {
    my $x = 80;
    my $y = 80;
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "PREV",
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "PREV",
		    );
  }

  {
    my $x = 80 + 110;
    my $y = 80;
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "NEXT",
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "NEXT",
		    );
  }

  {
    open(FILE, ">", "makemenutest.sh") or die;
    print FILE <<"EOT";
#!/bin/sh
EOT

    open(FILE2, ">", "menutestdvd.xml") or die;
    print FILE2 <<"EOT";
<dvdauthor dest="dvd/" >

<vmgm>
<fpc> g3 = 0; g4 = 0; g5 = 0; g6 = 0; jump titleset 1 menu; </fpc>
</vmgm>

<titleset>
<menus lang="JA">
<video format="ntsc" aspect="4:3" resolution="720x480" />
<audio format="mp2"/>

EOT

    my @flag_list
      = ("Aspect pixel mode", "Interlace mode", "Chroma subsampling mode");
    my @aspect_list 
      = (["-A 1:1", "computer graphics"],
	 ["-A 10:11", "CCIR-601 NTSC"],
	 ["-A 59:54", "CCIR-601 PAL"],
	);
    my @interlace_list 
      = (["-I p", "progressive"],
	 ["-I t -L", "top/upper-field-first interaced"],
	 ["-I b -L", "bottom/lower-field-first interlaced"]
	);
    my @chroma_subsampling_list 
      = (#["-S 444", "no subsampling"], <-- Err. Why?
	 ["-S 420jpeg", "JPEG/MPEG-1"],
	 ["-S 420mpeg2", "MPEG-2, horizontal cositing"]
	);

    my $spec = [scalar @flag_list,
		scalar @aspect_list, scalar @interlace_list, 
		scalar @chroma_subsampling_list];
    my $menunum = 1;
    for (my $p = first_perm($spec); defined $p; $p = next_perm($spec, $p)) {
      my $f = $flag_list[$p->[0]];
      my $a = $aspect_list[$p->[1]];
      my $i = $interlace_list[$p->[2]];
      my $c = $chroma_subsampling_list[$p->[3]];
      my $image2 = $image->Clone;

      {
	my $x = 440;
	my $y = 300;
	my $title 
	  = "Aspect pixel ratio: ". $a->[0] . "\n  " . $a->[1] . "\n"
	    . "Interlace mode: ". $i->[0] . "\n  " . $i->[1] . "\n"
	      . "Subsampling mode: ". $c->[0] . "\n  " . $c->[1] . "\n";

	$image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
			  pointsize => "18",
			  interwordspacing => 0.1,
			  stroke => "black", strokewidth => "3",
			  x => $x, y => $y,
			  gravity => "NorthEast",
			  align => "Left",
			  text => $title,
			 );
	$image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
			  pointsize => "18",
			  fill => "white",
			  x => $x, y => $y,
			  gravity => "NorthEast",
			  align => "Left",
			  text => $title,
			 );
      }

      {
	my $x = 80;
	my $y = 80;
	my $title = "メニューテスト\n$f";

	$image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
			 pointsize => "40",
			 interwordspacing => 0.1,
			 stroke => "black", strokewidth => "3",
			 x => $x, y => $y,
			 gravity => "NorthEast",
			 align => "Left",
			 text => $title,
			);
	$image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
			 pointsize => "40",
			 fill => "white",
			 x => $x, y => $y,
			 gravity => "NorthEast",
			 align => "Left",
			 text => $title,
			);
      }
      my $fbase = sprintf("menuTF%dA%dI%dS%d",  @$p);
      print FILE <<"EOT";

echo make ${fbase}.mpg
png2bmp ${fbase}.png | bmptopnm | ppmtoy4m $c->[0] -n 1 $i->[0] $a->[0] | mpeg2enc -f 8 -n n -o tmp.m2v
mplex -f 8 -o tmp.mpg tmp.m2v nosound-1second.m2a
spumux menuC.xml < tmp.mpg > ${fbase}.mpg
rm tmp.m2v tmp.mpg
EOT

      {
	$menunum++;
	my $fnext = $p->[0] + 1;
	$fnext = 0 if $fnext >= @flag_list;
	my $fprev = $p->[0] - 1;
	$fprev = @flag_list - 1 if $fprev < 0;
	my $g = "g" . (4 + $p->[0]);
	my $entry = "";
	$entry = " entry=\"root\""
	  if ($p->[0] == 0 && $p->[1] == 0 && $p->[2] == 0 && $p->[3] == 0);

	print FILE2 <<"EOT";
<pgc${entry}>
<pre> if (g3 != $p->[0]) { jump menu ${menunum}; }
      if (g4 != $p->[1]) { jump menu ${menunum}; }
      if (g5 != $p->[2]) { jump menu ${menunum}; }
      if (g6 != $p->[3]) { jump menu ${menunum}; }
</pre>
<vob file="${fbase}.mpg" pause="inf" />
<button name="BCAll"> g3 = ${fprev}; jump menu 1; </button>
<button name="BCSubtitle"> g3 = ${fnext}; jump menu 1; </button>
<button name="BCTitle"> ${g} = 0; jump menu 1; </button>
<button name="BCC1"> ${g} = 1; jump menu 1; </button>
<button name="BCC2"> ${g} = 2; jump menu 1; </button>
<button name="BCC3"> ${g} = 3; jump menu 1; </button>
<button name="BCC4"> ${g} = 4; jump menu 1; </button>
<button name="BCC5"> ${g} = 6; jump menu 1; </button>
</pgc>

EOT
      }
      $image2->Write($fbase . ".png");
    }

    print FILE2 sprintf(<<"EOT", @$spec);
<pgc>
<pre> if (g3 >= %d) { g3 = 0; }
      if (g4 >= %d) { g4 = 0; }
      if (g5 >= %d) { g5 = 0; }
      if (g6 >= %d) { g6 = 0; }
      jump menu 1;
</pre>
</pgc>
</menus>

<titles>
<pgc />
</titles>
</titleset>
</dvdauthor>
EOT
      close(FILE2);
    close(FILE);
  }
}


sub first_perm {
  my ($spec) = @_;
  my @new = (0) x (scalar @{$spec});
  return \@new;
}

sub next_perm {
  my ($spec, $prev) = @_;
  my @new = @{$prev};

  for (my $i = @new - 1; $i >= 0; $i--) {
    $new[$i]++;
    if ($new[$i] >= $spec->[$i]) {
      $new[$i] = 0;
      next;
    } else {
      return \@new;
    }
  }
  return undef;
}
