Skip to content

Instantly share code, notes, and snippets.

@saitoha
Last active April 11, 2019 14:10
Show Gist options
  • Save saitoha/4443520 to your computer and use it in GitHub Desktop.
Save saitoha/4443520 to your computer and use it in GitHub Desktop.
Convert an image to DRCS characters
#!/usr/bin/env perl
#
# This snippet is in public domain.
#
use strict;
use warnings;
use encoding "UTF-8";
if ($#ARGV != 0) {
print "usage: drcsconv.pl <filename>.";
exit 0;
}
my $file = $ARGV[0];
my $tmpfile = ".tmp.xpm";
# character size : 15 x 18
my $width = 15;
my $height = 18;
my $dscs = 64; # a
my $column = 64;
my $row = 24;
my $imageWidth = $width * $column;
my $imageHeight = $height * $row;
system("convert -resize $imageWidth"."x"."$imageHeight! $file $tmpfile");
system("convert -monochrome $tmpfile $tmpfile");
#print "row: $row, col: $column\n";
my @char_map;
open (IN, $tmpfile) or die "Cannot open : $!";
my $i = 0;
while (<IN>) {
if ($_ =~ /\* pixels \*/) {
last;
}
}
while (<IN>) {
if ($_ =~ /^"/) {
my $r = int($i / $height);
for my $c (0..$column - 1) {
my $line = substr($_, 1 + $c * $width, $width);
$char_map[$r][$c] .= $line;
}
$i++;
}
}
close IN;
for my $r (0..$row - 1) {
print "\eP1;0;0;$width;1;2;$height;0{ ", chr $dscs + $r, "\n";
for my $c (0..$column - 1) {
my $str = $char_map[$r][$c];
my @data = split(//, $str);
my @lines = ();
for my $y (0..($height / 6 - 1)) {
my $line = "";
for my $x (0..$width - 1) {
my $acc = 0;
for my $b (1..6) {
$acc *= 2;
my $index = ($y * 6 + 6 - $b) * $width + $x;
my $c = $data[$index];
if (not $c) {
print $y, "-",
$b, "-",
$height, "-",
$#data, "#",
$index, "\n";
}
if ($c eq ".") {
$acc += 1;
}
}
$acc += 0x3f;
$line .= chr $acc;
}
push @lines, $line;
}
print join("/", @lines), ";", "\n";
}
print "\e\\";
}
print "\n";
for my $r (0..($row - 1)) {
if (0) {
for my $c (0..$column - 1) {
my $code = 0x21 + $c;
if ($code == 0x7f) {
} else {
$code = 0x100000 | ($dscs + $r) << 8 | $code;
print chr $code;
}
}
} else {
print "\e( ", chr($dscs + $r);
for my $c (0..$column - 1) {
my $code = 0x21 + $c;
if ($code == 0x7f) {
} else {
print chr $code;
}
}
}
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment