I'm working (on and on and on ...) on a program that converts a BMP
image to text. I don't know what this is called, so I can't search for
it. I got it via email from a friend who got it from a friend ... so I
can't pin down the source either.
Here's the image that inspired me to start upon it:
Please note that /I did not do this/.
%Y=iiiii+VXRRXXXXXXXRRRBBBBMMMBBVYt=;,,tBBMMMBBBBMMR
%Y;:=+ttIYXRRXXXXXXXRRRRRRBMBBBRVYI+;,,:YMMMBBBBBMMR
%Y;;;;;=+XVtYVVVVVXXXRRRRXBMBBBRVYIt;,,,:RBBBBBBBBMX
%I=:;=+;=t==+ttIYYVVVXXXXXRXXXXVIt++=,,,,+BBBBBBBMMV
%I++ii:.++;;;;+tYVVVVXRRRRRRRRXXYtt+=,,,,,RBBBBBBMBX
%Y+tII:.I+=+=+IYVXXXXRRRXRRRRBRRVYYIi;,,,,IBBBBBBBBX
%Y+IYI..Vt++tIi+;:..:;iIIYVIii:,,,;===:::,iBBMMBMMMR
%YiIt+tiItiII;::+i++,..,=Yi,...:=++;,:;;;:+i=:IBMMMB
%YiI..;==iIXi=iit:..;=,=IXI;.:=+;,:ti=ii;:::,,:BMMMR
%Yit,+I+.+VXXYVi+++==+t+VXXt=;,,;==iiYYi=,,:+:YMMMMR
%Iii;I;.;+iVXYItIttIYVIIXRRViiYIttIYVVYt=+,:+IMMMMWR
%Yi:.tY;+i:tIVYXRRRRVtiXRRRXI=iYXRRRXVI+==;iXMMMMWWR
%Ii:,+XIIi++YXXVtIt=:YRXRRRVtVI:=iitIIi+++itMMWWWWWR
%It:=IBMRRV==i+;,..+ii+IYIti+===;,:====+YIIBMMMWWWWB
%It:;IBMXYI+;;,,.;YVYVt::..:,=IIII,,::=tYYRMMMWWWWWR
%II++IMBVBBY;:;,iii;:.......:;,:==t+;;+RBBMMMMWWWWWR
%It;=IMRYMBtY=;:+,=ti++;;;;;=;=+=;ii:+RBMBMBMMMWWWWR
%ti+XMWWBMBIBBt::itttiiIIIti=+ttit:,:=RBMBMBBMMMWWWB
%ti+BMWWWMMMMBRY;=tYI+===;===tItti:,:==IRMMBMMMMWWWR
%titMWWWWMMMBBBBXt;=iYVYYVVVVVt+:.,;++iiIXRBMMWWWWWX
%XtIWWWWMMMMBBBBRRt;,.,::::,,,,,,:==+tIVXXVVRBMWWWWX
%XtVWMMWMMMMMBBBRXI+==+:......,,,;ittIVXRXXXXRBRRWWX
%XtXBMMMWMMWMBBRBVI+t+.....,.,,:;+iIIIVXRRXXXRBRRRRX
%XtIYIYVXRRBBBBBRVY=::,:;::::==+tIYVVXVVXXRRBBBBRBBX
You must see it in some fixed-width font for it to make any sense.
Seeing it with a background darker than the foreground will show that
it is the picture of Mohandas Karamchand Gandhi, The Father of our
Indian Nation. (If you've never seen a picture of him before, then
just take my word upon it).
I've written the program to convert a two-bit color depth BMP image to
text, however, because only two colors are used I lose a lot of detail
in my images and the most important features of the face such as eyes,
nose, mouth don't come out very well.
In any conversion from image to text, the horizontal width will always
be less than the vertical. To make up for this, I take 8 pixels on the
horizontal and 14 on the vertical as an array and map this to an
alphabet.
Note that a sequence of numbers can be taken as a 2-D array of bits,
which is how I treat it. The vectors are as: (255 0 17 35 23
...fourteen numbers)
Each number is 8 pixels on the horizontal and there are 14 numbers for
the vertical. For example, a white block with a black cross running
along its vertical and horizontal centers would be:
(231 231 231 231 231 231 0 0 231 231 231 231 231 231)
11100111
11100111
11100111
11100111
11100111
11100111
00000000
00000000
11100111
11100111
11100111
11100111
11100111
11100111
0 - black; 1 - white
The mapping function is defined thus:
It takes one 8 x 14 portion of the image and maps it to a letter.
I know that instead of keeping the key of the alist ALPHA as a list, I
can keep it as the number of bits each numbers take, but since this is
not the completed program, I keep it so, so that I can visualize the
image. And it will be better as a hash table, but efficiency is not my
prime concern just yet.
(let
((alpha
'(((0 0 0 0 0 0 0 0 0 0 0 0 0 0) . (#\M #\W #\N))
((255 255 255 255 255 255 255 255 255 255 255 255 255 255) .
(#\Space))
((1 1 1 1 1 1 1 1 1 1 1 1 1 1) . (#\8 #\K #\B #\Space))
((3 3 3 3 3 3 3 3 3 3 3 3 3 3) . (#\O #\D #\B #\Q #\Space))
((7 7 7 7 7 7 7 7 7 7 7 7 7 7) . (#\T #\I #\H #\X #\J #\Space))
((15 15 15 15 15 15 15 15 15 15 15 15 15 15) . (#\*))
((0 0 0 0 0 0 0 1 1 1 1 1 1 1) . (#\Space))
((1 1 1 1 1 1 1 0 0 0 0 0 0 0) . (#\* #\^))
((64 64 64 64 64 64 64 64 64 64 64 64 64 64) . (#\| #\\ #\/ #\{
#\} #\!))
((127 127 127 127 127 127 127 127 127 127 127 127 127 127) . (#\.
#\`))
)))
(defun map-vect--alpha (numvect)
;;Assume that NUMVECT is a list
(flet ((heuristic (numvec1 numvec2)
(apply #'+ (mapcar #'(lambda (x y)
(abs (- x y)))
(mapcar #'logcount numvec1)
(mapcar #'logcount numvec2)))))
(let ((smallest most-positive-fixnum)
(smalli 0) ;smallest-index
(i 0))
(dolist (v alpha)
(let ((heur (heuristic (car v) numvect)))
(when (<= heur smallest)
(setf smallest heur
smalli i))
(when (zerop heur) (return)))
(incf i))
(random-item (cdr (elt alpha smalli)))))))
(defun random-item (sequence)
(elt (random (length sequence)) sequence))
Now my problem is that if I update this mapping to accomodate images
with 256 color depth I will be dealing with 64 bit numbers. While this
is possible in Lisp, I cannot enumerate so many combinations of lists
of length 14 and map them to each alphabet. Can someone please give me
a suggestion for a better mapping algorithm, or a link.
At first I thougth of including the code that seperates the image into
vectors, but, it is some 80+ lines, and this post is quite long in
itself. If however you need to see it, I can email it to you or post
it here. I don't have a webpage.
Thanks,
Vijay L