in reply to Font bitmaps manipulation in perl (transposing row like font description to column like)....

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re: Font bitmaps manipulation in perl (transposing row like font description to column like)....
  • Download Code

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

    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.
Re^2: Font bitmaps manipulation in perl (transposing row like font description to column like)....
by Anonymous Monk on Jan 05, 2008 at 22:48 UTC
    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.