#!/usr/bin/perl -w

# Import all the module that needed in this perl script
use strict;

open(INFILE, "12x24.bdf");
open (OUTFILE, ">>myFile.txt");
my $line;
my $header;
my $x;
my $startChar = 0;
my @number;
my $output;
my $charLine;

# Reading the file line by line
while($line = <INFILE>) {
    if($line =~ m/^STARTCHAR\s(\w+)/) { 
        $x=[];
        for(my $i=0; $i<24; $i++) { 
            for(my $j=0; $j<16; $j++) {
                $$x[$i][$j] = 0; 
            }
        }
        $header = $1;
        print(OUTFILE "$header ");
    }
    if($line =~ m/^ENCODING\s(.*)/) {
        print(OUTFILE "$1\n");
    }
    if($line =~ m/^BBX\s\d+\s(\d+)/) { 
    	my $temp = 24 - $1;
    	use integer;
    	$charLine = $temp / 2;
    }
    if($line =~ m/^ENDCHAR/) {
    	$startChar = 0;
    	$charLine = 0;
    	#for(my $i=0; $i<24; $i++) { 
        #    for(my $j=0; $j<16; $j++) {
        #        print "$$x[$i][$j]"; 
        #    }
        #    print "\n";
        #}
        #print "\n";
    	my ($re0, $re1, $re2);
    	my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = (0, 0, 0, 0, 0, 0, 0, 0, 0); 
    	#my $a = 0;
    	#my $b = 0;
    	#my $c = 0;
    	#my $d = 0;
    	#my $e = 0;
    	#my $f = 0;
    	#my $g = 0;
    	#my $h = 0;
    	#my $i = 0;
    	for (my $l=0; $l<16; $l++) {
    	    $a = $$x[0][$l] + 2*$$x[1][$l] + 4*$$x[2][$l];
    	    $b = $$x[3][$l] + 2*$$x[4][$l] + 4*$$x[5][$l];
    	    $c = $$x[6][$l] + 2*$$x[7][$l];
    	    $d = $$x[8][$l] + 2*$$x[9][$l] + 4*$$x[10][$l];
    	    $e = $$x[11][$l] + 2*$$x[12][$l] + 4*$$x[13][$l];
    	    $f = $$x[14][$l] + 2*$$x[15][$l];
    	    $g = $$x[16][$l] + 2*$$x[17][$l] + 4*$$x[18][$l];
    	    $h = $$x[19][$l] + 2*$$x[20][$l] + 4*$$x[21][$l];
    	    $i = $$x[22][$l] + 2*$$x[23][$l];
    	    $re0 = "$c$b$a";
      	    $re1 = "$f$e$d";
      	    $re2 = "$i$h$g";
      	    print (OUTFILE "$re2 $re1 $re0\n");
      	}
    	print(OUTFILE "\n");
    }
    if($startChar == 1) {
    	@number = split(//, $line);
    	my $useless = pop(@number);
    	push(@number, "0");
    	push(@number, "0");
    	for(my $k=0; $k<4; $k++) {
    	    if ($number[$k] eq "a") {
    	        $output = 10;
            }
            elsif ($number[$k] eq "b") {
    	        $output = 11;
            }
            elsif ($number[$k] eq "c") {
    	        $output = 12;
            }
            elsif ($number[$k] eq "d") {
    	        $output = 13;
            }
            elsif ($number[$k] eq "e") {
    	        $output = 14;
            }
            elsif ($number[$k] eq "f") {
    	        $output = 15;
            }
            elsif ($number[$k] < 10) {
    	        $output = $number[$k];
            }
    	    my ($J0, $J1, $J2, $J3); 
    	    my $a0 = 0;
    	    my $a1 = 0;
    	    my $a2 = 0;
    	    my $a3 = 0;
    	    if($output >= 8) {
    	        $output = $output - 8;
    	        $J3 = ($k*4);
    	        $$x[$charLine][$J3] = 1; 
        	$a3 = $$x[$charLine][$J3];
    	    }
    	    if($output >= 4) {
    	        $output = $output - 4;
    	        $J2 = ($k*4)+1;
    	        $$x[$charLine][$J2] = 1; 
    	        $a2 = $$x[$charLine][$J2];
    	    }
    	    if($output >= 2) {
    	        $output = $output - 2;
    	        $J1 = ($k*4)+2;
    	        $$x[$charLine][$J1] = 1; 
    	        $a1 = $$x[$charLine][$J1];
    	    }
    	    if($output >=1) {
    	        $output = $output - 1;
    	        $J0 = ($k*4)+3;
    	        $$x[$charLine][$J0] = 1; 
    	        $a0 = $$x[$charLine][$J0];
    	    }
    	    #my $printOut = "$a0 $a1 $a2 $a3\n";
    	    #print(OUTFILE "$printOut");
    	}
    	$charLine++;
    }
    if($line =~ m/^BITMAP/) {
        $startChar = 1;
    }
}

# Close the file
close(INFILE);
close(OUTFILE);
