#!/usr/local/bin/perl #Author: John V. Messina #email: messina@eeel.nist.gov #warnings: Use at your own risk ########FUNCTIONS################################################### ## Function: REVERSE_CHOP ## Purpose: Removes the first character in a character string sub REVERSE_CHOP { ($tmp) = @_; #set local variable to passed in argument $tmp = reverse($tmp); chop($tmp); $tmp = reverse($tmp); return $tmp; } #################################################################### #MAIN DECLARATION $line_counter = 1; $des_counter = 0; $obj_counter = 0; @descriptions; #Contains all # descriptions @types; #Contains all types @pl; #Contains all point lines @image_size; #[0] contains X, [1] contains Y $default; #store default string value #MAIN PROGRAM #Test number of arguments if ($#ARGV != 0) { print("Error: inaccurate number of arguments.\n"); print("\tSyntax: imapcvrt.pl \n"); exit(0); } #Open File Handler to file name read in as first arguement if (!(open(IMAGE, "$ARGV[0]"))) { print("Error: Can not open file: $ARGV[0]\n"); print("\tSyntax: imapcvrt.pl \n"); exit(0); } #Read in the first two non-blank lines: 1st) size 2nd) default $line = ; #Skip blank lines while ($line eq "\n") { $line = ; } #Check to see if first line is the default if (($pos = index($line, 'default', $pos)) > -1) { #First line is the Default Line #Store Default Line ($default, $defaulturl) = split / /, $line; #eliminate excess white space $default =~ s/^\s*((\s*\S+)*)\s*$/$1/e; $defaulturl =~ s/^\s*((\s*\S+)*)\s*$/$1/e; } else { #First line is the Image Size #Record X size and Y size $tmp = &REVERSE_CHOP($line); @image_size = split /x/, $tmp; } #Initialize Counter Variables $i = 0; $j = 0; $counter = 0; $k = 0; $b = 0; $line = ; while ($line) { if ($line ne "\n") { $pos = -1; if (($pos = index($line, 'default', $pos)) > -1) { #Check to see if default is at start of line if ($pos == 0) { #Store Default Line ($default, $defaulturl) = split / /, $line; #eliminate excess white space #Set default to current description $default = $descriptions[$i]; $descriptions[$i] = ''; $defaulturl =~ s/^\s*((\s*\S+)*)\s*$/$1/e; } } else { $pos = -1; if (($pos = index($line, '#', $pos)) > -1) { $line = &REVERSE_CHOP($line); $descriptions[$i] = $line; $descriptions[$i] =~ s/^\s*((\s*\S+)*)\s*$/$1/e; } else { @words = split / /, $line; if ($words[0] eq "point") { $types[$j] = '0'; ($xc, $yc) = split /,/, $words[2]; $xc = $xc / $image_size[0]; $yc = 1 - ($yc / $image_size[1]); $xc = substr($xc, 0, 8); $yc = substr($yc, 0, 8); $pl[$counter] = "$xc, $yc"; $counter++; $i++; } if ($words[0] eq "circle") { $types[$j] = '1'; ($xc, $yc) = split /,/, $words[2]; ($ex, $ey) = split /,/, $words[3]; $cxt = $xc / $image_size[0]; $cyt = 1 - ($yc / $image_size[1]); $rd = sqrt((($ex - $xc)**2) + (($ey - $yc)**2)); $rx = $rd / $image_size[0]; $ry = $rd / $image_size[1]; $cxt = substr($cxt, 0, 8); $cyt = substr($cyt, 0, 8); $rx = substr($rx, 0, 8); $ry = substr($ry, 0, 8); $pl[$counter] = "$cxt, $cyt $rx, $ry"; $counter++; $i++; } if ($words[0] eq "rect") { $types[$j] = '2'; ($xc, $yc) = split /,/, $words[2]; $xc = $xc / $image_size[0]; $yc = 1 - ($yc / $image_size[1]); ($xc2, $yc2) = split /,/, $words[3]; $xc2 = $xc2 / $image_size[0]; $yc2 = 1 - ($yc2 / $image_size[1]); $xc = substr($xc, 0, 8); $yc = substr($yc, 0, 8); $xc2 = substr($xc2, 0, 8); $yc2 = substr($yc2, 0, 8); $pl[$counter] = "$xc, $yc $xc2, $yc2"; $counter++; $i++; } if ($words[0] eq "poly") { $k = 2; while ($words[$k] ne '') { $k++; } $k = $k - 3; $types[$j] = $k; for ($b = 0; $b < $k; $b++) { ($xc, $yc) = split /,/, $words[$b+2]; $xc = $xc / $image_size[0]; $yc = 1 - ($yc / $image_size[1]); $xc = substr($xc, 0, 8); $yc = substr($yc, 0, 8); $pl[$counter] .= "$xc, $yc "; } chop($pl[$counter]); $counter++; $i++; } $urls[$j] = $words[1]; $j++; } } } $line = ; } #Print Results #Determine total number of images $counter = $#pl + 1; print ("VAlue of counter :$counter:\n"); print ("ImageMap {\n"); print ("\tdefaultURL \"$defaulturl\"\n"); print ("\tdefaultDescription \"$default\"\n"); print ("\ttype ["); $i = 0; for ($i = 0; $i < $counter; $i++) { print ("$types[$i],"); } print ("-1]\n"); print ("\tpoint [ "); $i = 0; for ($i = 0; $i < $counter; $i++) { if ($i != ($counter - 1)) { print ("$pl[$i],\n\t"); } else { print ("$pl[$i]"); } } print ("]\n"); print ("\turl ["); $i = 0; for ($i = 0; $i < $counter; $i++) { if ($i != ($counter - 1)) { print ("\"$urls[$i]\",\n\t"); } else { print ("\"$urls[$i]\""); } } print ("]\n"); print ("\tdescription ["); $i = 0; for ($i = 0; $i < $counter; $i++) { if ($i != ($counter - 1)) { print ("\"$descriptions[$i]\",\n\t"); } else { print ("\"$descriptions[$i]\""); } } print ("]\n"); print ("}\n"); exit(0);