Last active
July 11, 2018 16:15
-
-
Save karronoli/1d1e61ddcbea20e50bc19d274c8a1553 to your computer and use it in GitHub Desktop.
Convert SBPL GrapHic operand to pbm image file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use utf8; | |
use v5.28; | |
use File::Basename; | |
if (!@ARGV || $ARGV[0] !~ /\.sbpl$/) { | |
print qq($^X $0 graphic.sbpl && convert graphic.pbm graphic.png\n); | |
exit(1); | |
} | |
my $input = $ARGV[0]; | |
my $output = basename($input, '.sbpl') . '.pbm'; | |
my ($width, $height, $graphic) = do { | |
local $/; | |
open my $fh, '<', $input or die $!; | |
my ($w, $h, $g) = ($fh->getline =~ /\x{1b}GH(\d{3})(\d{3})([^\x{1b}]+)/); | |
(int($w), int($h), $g) | |
}; | |
open my $fh, '>', $output or die $!; | |
$fh->printf("P1\n%d %d\n", $width * 8, $height * 8) or die $!; | |
for (my $i = 0; $i < length $graphic; $i += $width) { | |
my $row = substr($graphic, $i, $width); | |
my @bin = map {sprintf('%04b', hex($_))} split(//, $row); | |
$fh->say(join(' ', @bin)) or die $!; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment