Programmable bitmap hack

183 views
Skip to first unread message

luser- -droog

unread,
Jan 24, 2013, 2:42:16 AM1/24/13
to
Just a little bragging about my losing entry to
"Emulate a 7-segment display"
http://codegolf.stackexchange.com/a/9308/2381

While not a winner in terms of size, I'm proud of this code because I think it illustrates a neat way to draw bitmaps
using the postscript `image` operator.

(1101101) %for each char in the input
{neg 49 add 255 mul %'0'->255,1->0
1 string dup 0 4 3 roll put}forall %wrap in 1byte string
(\377){@/g/f/e/d/c/b/a}{exch def}forall %define values
@ a a @ %describe bitmap, top->down, right->left
b @ @ f
b @ @ f
@ g g @
c @ @ e
c @ @ e
@ d d @
4 7 8[.01 0 0 .01 0 0]{}image showpage
%4-pix wide 7-pix tall 8-bits per pix
%[matrix sez: "un-shrink" by 1/100]
%emptry proc lets image find strings already on stack

But you could easily do sthg like

/_ (\0) def
/# (\377) def
_ _ _ #
_ _ # _
_ # _ _
# _ _ _
4 4 8
[.1 0 0 .1 0 0]{}image

Now PS art is as easy as ASCII art. Have fun!

Eli the Bearded

unread,
Jan 31, 2013, 6:01:59 PM1/31/13
to
In comp.lang.postscript, luser- -droog <mij...@yahoo.com> wrote:
> But you could easily do sthg like
>
> /_ (\0) def
> /# (\377) def
> _ _ _ #
> _ _ # _
> _ # _ _
> # _ _ _
> 4 4 8
> [.1 0 0 .1 0 0]{}image
>
> Now PS art is as easy as ASCII art. Have fun!

It's like xpm images in postscript! I like this. If you changed your matrix
to flip the image side-to-side, though, so the ASCII art matches the
orientation of the output, it would be easier to use:

[-.1 0 0 .1 0 0]

Elijah
------
wikipedia has xpm examples if you don't know the format

Eli the Bearded

unread,
Jan 31, 2013, 6:29:52 PM1/31/13
to
In comp.lang.postscript, Eli the Bearded <*@eli.users.panix.com> wrote:
> In comp.lang.postscript, luser- -droog <mij...@yahoo.com> wrote:
[snip]
> > [.1 0 0 .1 0 0]{}image
> > Now PS art is as easy as ASCII art. Have fun!
> It's like xpm images in postscript!

Expansion to colorimage is a bit trickier than I hoped. This ascii image
is nothing like the color image that comes out.

306 392 translate
/_ (\0 \0 \0) def
/a (\77 \0 \0) def
/b (\0 \177 \0) def
/c (\0 \0 \277) def
/# (\377 \377 \377) def
a b b b b a
a _ _ _ # a
a _ _ # _ a
a _ # _ b b
a # _ _ c a
a b b b b a
6 6 8
[-.1 0 0 .1 0 0]{} false 3 colorimage

Elijah
------
does not find colorimage defined in his ancient red book

luser- -droog

unread,
Feb 1, 2013, 2:54:42 AM2/1/13
to
On Thursday, January 31, 2013 5:01:59 PM UTC-6, Eli the Bearded wrote:
> In comp.lang.postscript, luser- -droog <mij...@yahoo.com> wrote:
> > But you could easily do sthg like
> > /_ (\0) def
> > /# (\377) def
> > _ _ _ #
> > _ _ # _
> > _ # _ _
> > # _ _ _
> > 4 4 8
> > [.1 0 0 .1 0 0]{}image
> > Now PS art is as easy as ASCII art. Have fun!

> It's like xpm images in postscript! I like this. If you changed your matrix
> to flip the image side-to-side, though, so the ASCII art matches the
> orientation of the output, it would be easier to use:

> [-.1 0 0 .1 0 0]

Yes. That's much better. But I'm now thinking there's no
advantage in leaving stuff on the stack and passing an
empty procedure (except perhaps in Level-1 interpreters).
The image data might just as well be *in* the procedure.

luser- -droog

unread,
Jul 19, 2013, 3:34:20 AM7/19/13
to
This is wrong. Remember that the procedure gets called each time `image` wants a new piece of data. The procedure *must*
do nothing. That's the whole trick here.

Here's the idea extended to an editable Type 3 bitmap font.

/bitfont <<
/FontType 3
/FontMatrix [.01 0 0 .01 0 0]
/FontBBox [ 0 0 50 70]
/Encoding 128 array
/_ (\377)
/# (\0)
/charprocs <<

/.notdef {
# # # # #
# _ _ # #
# _ _ # #
# _ # _ #
# _ # _ #
# # _ _ #
# # # # #
}

32 {
_ _ _ _ _
_ _ _ _ _
_ _ _ _ _
_ _ _ _ _
_ _ _ _ _
_ _ _ _ _
_ _ _ _ _
}
48 {
_ # # # _
# _ _ _ #
# _ _ # #
# _ # _ #
# # _ _ #
_ # # # _
_ _ _ _ _
}
49 {
_ _ # _ _
_ # # _ _
_ _ # _ _
_ _ # _ _
_ _ # _ _
_ # # # _
_ _ _ _ _
}

>>
/BuildChar {
%100 0 0 0 50 70 setcachedevice
100 0 setcharwidth
-1 1 scale
exch begin
charprocs exch
2 copy known not{pop/.notdef}if
get exec
5 7 8 FontMatrix {} image
end
}
>> definefont pop

100 100 moveto
/bitfont 20 selectfont
(0 1 0 1 ?) show

showpage

luser- -droog

unread,
Oct 29, 2013, 5:50:14 AM10/29/13
to
On Friday, February 1, 2013 1:54:42 AM UTC-6, luser- -droog wrote:
Again, my comment here is wrong. But also,
the matrix needs to add an x-offset now, since
it maps right-to-left. Otherwise it places the
image with the origin at the bottom right corner.

[-.1 0 0 .1 .1 0]

Reply all
Reply to author
Forward
0 new messages