Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'd kindly ask for help in this field :

- I have a font descriptions in text file. Each character is determined in line that with 8 values separated with comma : for instance "!" is determined like (ranging from top line to bottom):
0x04,0x04,0x04,0x04,0x00,0x00,0x04,0x00,

- now I need to transpose font description from row-like to column like fashion (so first byte describes first column, etc...). Operation seems to be transposing matrix of character bits.

I guess this is a bit tricky task for beginner, but probably much easier for more experienced users.... I'd kindly ask for some code snippet that would get me started....

Thanks in advance,

regards,

Rob.
  • Comment on Font bitmaps manipulation in perl (transposing row like font description to column like)....

Replies are listed 'Best First'.
Re: Font bitmaps manipulation in perl (transposing row like font description to column like)....
by BrowserUk (Patriarch) on Jan 05, 2008 at 00:20 UTC

    There are two ways of mapping the bits, here's one of them:

    my @rows = ( 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x04, 0x00 ); my @cols = (0) x 8; for my $c ( 0 .. 7 ){ $rows[ $_ ] & 1 << $c and $cols[ $c ] |= 1 << $_ for 0 .. 7; } printf "0x%02x\n", $_ for @cols; 0x00 0x00 0x4f 0x00 0x00 0x00 0x00 0x00

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I was asked offline to explain the above code.

      rows 0x04 0000 0100 0x04 0000 0100 0x04 0000 0100 0x04 0000 0100 0x00 0000 0000 0x00 0000 0000 0x04 0000 0100 0x00 0000 0000 columns |||| |||+- 0x00 |||| ||+-- 0x00 |||| |+--- 0x4f |||| +---- 0x00 |||+------ 0x00 ||+------- 0x00 |+-------- 0x00 +--------- 0x00

      From the above diagram we can see that

    • The 8 bits of column 0 are the 0th bits of each of the 8 rows.
    • The 8 bits of column 1 are the 1th bits of each of the 8 rows.
    • And so on.

      To achieve that I use bit-wise operations to test the bits of elements of @rows, and when appropriate, to set the bits of the elements of @cols. The following is essentially identical to the code above, but expanded a little to hopefully clarify things a bit:

      ## The rows my @rows = ( 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x04, 0x00 ); ## Initialise the columns my @cols = (0) x 8; ## For each of the 8 columns for my $col ( 0 .. 7 ){ ## look at each of the rows in turn for my $row ( 0 .. 7 ) { ## And if the $col bit of the $row row is set if( $rows[ $row ] & 1 << $col ) { ## Set the $row bit of the $col column $cols[ $col ] |= 1 << $row; } } }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi,

      I liked your solution, but it seems I'm missing something ....
      open(INPUT, "< ./vrsticni_fonti.txt") or die "Couldn't open ./vrsticni_fonti.txt for reading: $!\n"; my $line; while (<INPUT>) { $line = $_ ; print "Line: $line \n"; # my @rows = ( 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x04, 0x00 ); my @rows = ($line =~ m/(0x[a-fA-F0-9]+)/g); print "Extracted numbers : " . join("|", @rows) . "\n"; my @cols = (0) x 8; for my $c ( 0 .. 7 ){ $rows[ $_ ] & 1 << $c and $cols[ $c ] |= 1 << $_ for 0 .. 7; } print "Changed fonts : " . join("|", @cols) . "\n"; # printf "0x%02x\n", $_ for @cols; } close(INPUT);
      It prints all zeros in "Changed fonts" line....
      I'd kindly ask for some more help....

      Thanks in advance,

      Rob.

        If you used strict and warnings then you'd probably see output from your code along the lines of:

        Line: 0xf0 0x0f 0x01 0x02 0x03 0x04 0x05 0x06 Extracted numbers : 0xf0|0x0f|0x01|0x02|0x03|0x04|0x05|0x06 Argument "0xf0" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x0f" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x01" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x02" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x03" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x04" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x05" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Argument "0x06" isn't numeric in bitwise and (&) at c:\test\junk.pl li +ne 12, <> line 1. Changed fonts : 0|0|0|0|0|0|0|0

        Which would identify that whilst Perl knows to convert hex encoded barewords in source code into numbers, it does not automatically do the same for hex strings read in from external sources. Ie. The problem lies with your data parsing not the code I posted.

        The simplest fix is to use the built-in hex function to convert them to numbers:

        open(INPUT, "< ./vrsticni_fonti.txt") or die "Couldn't open ./vrsticni_fonti.txt for reading: $!\n"; my $line; while (<INPUT>) { $line = $_ ; print "Line: $line \n"; # my @rows = ( 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x04, 0x00 ); my @rows = ($line =~ m/(0x[a-fA-F0-9]+)/g); ## convert the hex strings to numbers. @rows = map hex, @rows; print "Extracted numbers : " . join("|", @rows) . "\n"; my @cols = (0) x 8; for my $c ( 0 .. 7 ){ $rows[ $_ ] & 1 << $c and $cols[ $c ] |= 1 << $_ for 0 .. 7; } print "Changed fonts : " . join("|", @cols) . "\n"; # printf "0x%02x\n", $_ for @cols; } close(INPUT); __END__ Line: 0xf0 0x0f 0x01 0x02 0x03 0x04 0x05 0x06 Extracted numbers : 240|15|1|2|3|4|5|6 Changed fonts : 86|154|226|2|1|1|1|1

        Note: The extracted numbers now display as numbers and the transformed output is no longer zeroes.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Font bitmaps manipulation in perl (transposing row like font description to column like)....
by spx2 (Deacon) on Jan 05, 2008 at 00:16 UTC
    I've put together a sample of how you could achieve
    what you want.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @a; my $x = 0; my $y = 0; #fill with 0 all the matrix with one loop(no need for two really :) ) $a[$_/8][$_%8]=0 for 0..63;#it's a 7x7 matrix :) #initialisation of matrix with provided data $a[0][0]=4; $a[0][1]=4; $a[0][2]=4; $a[0][3]=4; $a[0][4]=0; $a[0][5]=0; $a[0][6]=4; $a[0][7]=0; #matrix transpose calculated while(exists $a[$y][0] && $x<=$y) { # the last condition here is due to not # getting to lower triangle and transposing them # back getting exactly what we started with # wich we obviously don't want $x = 0; #reset $x for a new pass tghrough the current row while(exists $a[0][$x]) { ($a[$y][$x],$a[$x][$y]) = ($a[$x][$y],$a[$y][$x]) ; #swap the current element to its corresponding position in the + transpose $x++;#increase the column }; $y++;#increase the row }
    Good luck!