#!/usr/bin/perl -w # # .bm file format: # Leading spaces on a line are ignored. # Lines that start with '#' are ignored. # Pixel lines must contain '.', '0' or '1' characters. # Those lines must have a length that is a multiple of 8. # All pixel lines must have the same length. # '.' means a transparent pixel. # '0' and '1' mean a black or white pixel. use strict; use Getopt::Long; use File::Basename; my $program = basename($0); my $version = "0.0.0"; my $numErrors = 0; my $numWarnings = 0; my $dryRun = 0; my $verbose = 0; my $format = "c"; # or "raw" sub usage { my ($exitStatus) = @_; print <<__EOF__; $program $version Copyright (C) 2013 Pierre Sarrazin Usage: $program [OPTIONS] foo.bm > foo.bm.c Options: --help Print this help page. --version Print this program's name and version number. --not-really Only show what would happen, do not do it for real. --verbose Print more details of what is happening. --format=F Use the F format (c or raw; default is c). __EOF__ exit($exitStatus); } sub errmsg { print STDERR "$program: ERROR: "; printf STDERR @_; print STDERR "\n"; ++$numErrors; } sub warnmsg { print STDERR "$program: Warning: "; printf STDERR @_; print STDERR "\n"; ++$numWarnings; } # Receives a string of 8 characters in {'.', '0', '1'}. # Returns ($mask, $image), where $mask is the numerical value # of the mask byte, and $image is the numerical value of the # image bits to be put on the screen. # sub processByte($) { my ($bits) = @_; die unless length($bits) == 8; my $image = 0; my $mask = 0; for (my $i = 0; $i < 8; ++$i) { my $ch = substr($bits, $i, 1); #print "$ch "; my ($imageBit, $maskBit); if ($ch eq '0') { $imageBit = 0; $maskBit = 0; } elsif ($ch eq '1') { $imageBit = 1; $maskBit = 0; } elsif ($ch eq '.') # transparent { $imageBit = 0; $maskBit = 1; # keep bit from screen } $image = ($image << 1) | $imageBit; $mask = ($mask << 1) | $maskBit; } #printf(" image=0x%02x, mask=0x%02x\n", $image, $mask); return ($mask, $image); } # Converts a numerical values into a string of zeroes and ones. # $sizeInBytes indicates the width in bytes of $numValue. # sub toBits($$) { my ($numValue, $sizeInBytes) = @_; my $str = ""; while ($sizeInBytes--) { for (my $i = 8; $i--; $numValue >>= 1) { $str = chr(48 + ($numValue & 1)) . $str; } } return $str; } #die unless toBits(42, 1) eq "00101010"; #die unless toBits(513, 2) eq "0000001000000001"; sub generateShiftedCFormat($$$$) { my ($bitmapName, $widthInBits, $height, $raBytes) = @_; die "max supported height is 42" unless $height <= 42; my $cFile = \*STDOUT; print $cFile "/* $bitmapName */\n"; if ($widthInBits == 16) { printf $cFile qq{#include "PreShiftedBitmap.h"\n}; printf $cFile qq{#include "util.h"\n}; #printf $cFile "enum { PIXMAP_HEIGHT_%s = %u };\n", $bitmapName, $height; #printf $cFile "/* Size in bytes: %u. */\n", 4 * $height * 6; print $cFile "const PreShiftedBitmap $bitmapName =\n{\n"; print $cFile " $height, /* height in pixels rows */\n"; print $cFile " { ", join(", ", 0, $height * 6, $height * 6 * 2, $height * 6 * 3,), " }, /* preShiftOffsets */\n"; print $cFile " {\n"; #print $cFile "{ /* array of 4 pre-shifted bitmaps: */\n"; for (my $shift = 0; $shift <= 6; $shift += 2) { print $cFile " /* [", $shift / 2 , "]: pre-shifted right by $shift bit(s): */\n"; print $cFile " /* ------------- Masks -------------- ------------- Images ------------- */\n"; #print $cFile " /* array of $height pixel rows: */\n"; # # Usage on 6809: # ldd ,x ; load word from screen # anda ,u ; AND with high byte of mask word # andb 1,u ; AND with middle byte of mask word # ora 3,u ; OR in the high byte of the image word # orb 4,u ; OR in the middle byte of the image word # std ,x ; store word to screen # lda 2,x ; load folloing byte from screen # anda 2,u ; AND with low byte of mask word # ora 5,u ; OR in the low byte of the image word # sta 2,x ; store byte to screen # my $pixelRow = 0; for (my $i = 0; $i < @$raBytes; $i += 4, ++$pixelRow) { printf $cFile " /* \$raBytes->[%u..%u] = (0x%02x, 0x%02x, 0x%02x, 0x%02x) */\n", $i, $i + 3, $raBytes->[$i], $raBytes->[$i + 1], $raBytes->[$i + 2], $raBytes->[$i + 3]; my $mask = ($raBytes->[$i ] << 8) | $raBytes->[$i + 2]; my $image = ($raBytes->[$i + 1] << 8) | $raBytes->[$i + 3]; $mask <<= 8 - $shift; $mask |= ~(0xFF >> $shift) << 16; # transparency at left of 16-bit image $mask |= 0xFF >> $shift; # transparency at right of 16-bit image $image <<= 8 - $shift; printf $cFile " /* row %2u: 0x%06x, 0x%06x */ 0b%s, 0b%s, 0b%s, 0b%s, 0b%s, 0b%s,\n", $pixelRow, $mask, $image, toBits($mask >> 16, 1), toBits(($mask >> 8) & 0xFF, 1), toBits($mask & 0xFF, 1), toBits($image >> 16, 1), toBits(($image >> 8) & 0xFF, 1), toBits($image & 0xFF, 1); } die unless $pixelRow == $height; #print $cFile " /* end of array of $height pixel rows */\n"; } #print $cFile "/* end of array of 4 pre-shifted bitmaps */\n"; print $cFile " }\n"; print $cFile "};\n"; } else { die "unsupported width $widthInBits"; } } sub work { my ($bitmapName) = @_; my $width; # in pixels my $height = 0; my @bytes; while (<>) # for each line in the .bm format { s/\R$//; next if /^\s*#/; # ignore comment line if (length($_) % 8 != 0) { errmsg("length of line $. is not a multiple of 8"); next; } if (! /^[01\.]+$/) { errmsg("illegal characters in line $."); next; } if (!defined $width) { $width = length($_); } elsif (length($_) != $width) { errmsg("line $. is not the same length as the first line"); } #print "line: $_\n"; while (s/^(.{8})//) # for each 8 chars { push @bytes, processByte($1); } ++$height; } return if $numErrors; #print "\tfcb\t$width,$height\n"; #my @hex = map { sprintf("\$%02X", $_) } @bytes; #my $fcb = join(",", @hex); #print "\tfcb\t$fcb\n"; if ($format eq "raw") { print pack("C*", $width / 8, $height, @bytes); } elsif ($format eq "c") { print qq!#include "ThinBitmap.h"\n!; print qq!const ThinBitmap $bitmapName = !; printf("{ %u, {\n", $height); my $i = 0; for my $b (@bytes) { printf(" 0x%02x,", $b) if $i % 2 == 1; # mask bytes not needed ++$i; printf("\n") if $i % 8 == 0; } printf("}};\n"); } elsif ($format eq "shiftedc") { generateShiftedCFormat($bitmapName, $width, $height, \@bytes); } } ############################################################################### my $showUsage = 0; if (!GetOptions( "help" => \$showUsage, "version" => \$showUsage, "not-really" => \$dryRun, "verbose" => \$verbose, "format=s" => \$format, )) { exit 1; } usage(0) if $showUsage; die unless @ARGV == 1; my $bitmapName = $ARGV[0]; $bitmapName =~ s/\.s?bm$/_bm/; $| = 1; # no buffering on STDOUT work($bitmapName); #print "$program: $numErrors error(s), $numWarnings warnings(s).\n"; exit($numErrors > 0);