#!/usr/bin/perl

# Output the data in <pgmfile> as an emotion label followed by the
# vector of pixel values

# Matt Dailey 2000

if (@ARGV != 1) {
  print STDERR "usage: output_emo.pl <pgmfile>\n";
  exit -1;
}

# Map emotion names to a 1-of-6 vector (neutral is 6 0's?)

%emo_to_output = ( 'H', '100000', 'M', '010000', 'F', '001000',
                   'A', '000100', 'S', '000010', 'D', '000001',
                   'N', '000000' );

# Get the emotion purportedly expressed in the given file

$fname = $ARGV[0];
$lab = $fname; $lab =~ s/^.*\///g; $lab =~ s/\.pgm//;
$emo = getemo($lab);

# Output the desired outputs for this pattern

print "$emo_to_output{$emo}\n";

# Convert the raw PGM file to ASCII and output as a vector

open(IN,"pnmnoraw < $fname|") || die "can't run pnmnoraw on $fname: $!";
$_ = <IN>; chop;
$_ eq "P2" || die "$fname is not a PGM file";
$_ = <IN>; chop; $_ =~ /^([\d]+)\s+([\d]+)$/ ||
   die "unexpected stuff in PGM header";
$wid = $1; $ht = $2;
$_ = <IN>; chop; $_ == 255 || die "$fname is not an 8-bit PGM file";
$num = 0;
while(<IN>) {
  chop; @data = split(/\s+/); $out = join("\n",@data); print "$out\n";
  $num += @data;
}
close(IN);
$num == $wid * $ht || die "file data/specification mismatch";

# All done!

exit;

sub getemo {
  $getemo_label = shift;
  %label_to_emo = (
    'a1-2', 'N',
    'a1-6', 'H',
    'a1-14', 'A',
    'a1-24', 'S',
    'a1-25', 'D',
    'a2-6', 'M',
    'c2-18', 'H',
    'c1-18', 'M',
    'c1-23', 'F',
    'c2-12', 'A',
    'c1-10', 'S',
    'c1-4', 'D',
    'c2-3', 'N',
    'em2-11', 'S',
    'em2-4', 'N',
    'em4-17', 'D',
    'em4-24', 'M',
    'em4-7', 'H',
    'em5-14', 'A',
    'em5-21', 'F',
    'em5-24', 'F',
    'gs1-16', 'S',
    'gs1-25', 'F',
    'gs1-4', 'N',
    'gs1-8', 'H',
    'gs2-1', 'M',
    'gs2-25', 'D',
    'gs2-8', 'A',
    'jb1-12', 'S',
    'jb1-16', 'D',
    'jb1-23', 'A',
    'jb1-3', 'N',
    'jb1-9', 'H',
    'jj3-12', 'A',
    'jj3-20', 'D',
    'jj3-4', 'N',
    'jj4-13', 'S',
    'jj4-7', 'H',
    'jj4-8', 'H',
    'jj5-13', 'F',
    'jj5-5', 'M',
    'jm1-16', 'S',
    'jm1-4', 'H',
    'jm1-9', 'N',
    'jm2-8', 'D',
    'jm3-11', 'M',
    'jm5-3', 'A',
    'mf1-2', 'N',
    'mf1-26', 'F',
    'mf1-27', 'F',
    'mf1-30', 'M',
    'mf1-6', 'H',
    'mf1-9', 'S',
    'mf2-13', 'D',
    'mf2-5', 'A',
    'mf2-7', 'A',
    'mo1-14', 'S',
    'mo1-23', 'F',
    'mo1-26', 'F',
    'mo1-30', 'M',
    'mo1-4', 'H',
    'mo1-5', 'N',
    'mo2-11', 'A',
    'mo2-13', 'A',
    'mo2-18', 'D',
    'nr1-14', 'S',
    'nr1-19', 'F',
    'nr1-3', 'N',
    'nr1-6', 'H',
    'nr2-15', 'M',
    'nr2-7', 'A',
    'nr3-29', 'D',
    'pe2-12', 'H',
    'pe2-21', 'A',
    'pe2-31', 'M',
    'pe2-4', 'N',
    'pe2-6', 'H',
    'pe3-16', 'F',
    'pe3-21', 'F',
    'pe4-5', 'D',
    'pe5-10', 'M',
    'pe5-7', 'M',
    'pe6-2', 'S',
    'pf1-16', 'S',
    'pf1-2', 'N',
    'pf1-24', 'D',
    'pf1-5', 'H',
    'pf1-6', 'H',
    'pf2-12', 'M',
    'pf2-16', 'M',
    'pf2-30', 'F',
    'pf2-4', 'A',
    'sw1-16', 'S',
    'sw1-30', 'D',
    'sw2-16', 'M',
    'sw2-30', 'F',
    'sw3-3', 'N',
    'sw3-9', 'H',
    'sw4-9', 'A',
    'wf2-11', 'H',
    'wf2-12', 'H',
    'wf2-16', 'S',
    'wf2-5', 'N',
    'wf3-1', 'A',
    'wf3-11', 'D',
    'wf3-16', 'F',
    'wf3-28', 'M',
    'wf3-4', 'A',
    'wf4-22', 'D',
    'wf5-6', 'M');
  $getemo_emo = $label_to_emo{$getemo_label} || die "bad label: $lab";
  return $getemo_emo;
} 

