Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

primitive raster image procset

29 views
Skip to first unread message

luser- -droog

unread,
Aug 19, 2013, 9:58:51 PM8/19/13
to
I said "primitive", didn't I?
Maximum image size: 65535x65535x8bit.

% w h img [img]
% img x y getpix b
% img x y b putpix -

/img {
[ 3 1 roll % [ w h
{ % [ ... w
dup string exch % [ ... s w
} repeat
pop
]
} def

/getpix {
3 2 roll exch % x img y
get exch get
} def

/putpix {
exch 4 3 roll exch % x b img y
get 3 1 roll put
} def


/p 5 5 img def

p 1 1 50 putpix
p 2 2 60 putpix
p 3 3 70 putpix

p ==
p {{=only( )=only}forall(\n)=only}forall

luser- -droog

unread,
Aug 26, 2013, 3:29:16 AM8/26/13
to
A little more fleshed-out.
Reads and writes pgm files.


% w h img8 [img]
% img x y getpix8 b
% img x y b putpix8 -
% img x1 y1 x2 y2 b drawline8 -
% img x y w h b drawrect8 -
% img x y w h b fillrect8 -
% (filename) loadimg8 [img]
% img (filename) saveimg8 -
% img printimg -

/img8 {
dup array 3 1 roll % a w h
0 1 3 2 roll 1 sub { % a w i
3 copy exch string put pop
} for
pop
} def

/getpix8 {
3 2 roll exch % x img y
get exch get
} def

/putpix8 {
exch 4 3 roll exch % x b img y
get 3 1 roll put
} def

/Sign {
dup 0 le {
0 lt {
-1
}{
0
} ifelse
}{
pop 1
} ifelse
} def

/drawline8 { % a x1 y1 x2 y2 b
6 dict begin
{b y2 x2 y1 x1 a}{exch def}forall
/x x1 def
/y y1 def
x2 x1 sub dup abs /dx exch def
Sign /s1 exch def
y2 y1 sub dup abs /dy exch def
Sign /s2 exch def
/interchange dy dx gt def
interchange { /dx dy /dy dx def def } if
/e 2 dy mul dx sub def
1 1 dx { pop
a x y b putpix8
{
e 0 ge not {exit} if
interchange {
/x x s1 add def
}{
/y y s2 add def
} ifelse
/e e 2 dx mul sub def
} loop
interchange {
/y y s2 add def
}{
/x x s1 add def
} ifelse
/e e 2 dy mul add def
} for
end
} def

% |-----
% | |
% | |
% |-----
/drawrect8 { % a x y w h b
6 dict begin
{b h w y x a}{exch def}forall
x
0 1 h { y add
a exch % x a y+h
2 index % x a y+h x
exch b putpix8
} for
w add
1 1 h 1 sub { y add
a exch % x a y+h
2 index % x a y+h x
exch b putpix8
} for
pop
y
1 1 w { x add
a exch % y a x+w
2 index % y a x+w y
b putpix8
} for
h add
1 1 w { x add
a exch % y a x+w
2 index % y a x+w y
b putpix8
} for
pop
} def

/fillrect8 { % a x y w h b
6 dict begin
{b h w y x a}{exch def}forall
0 1 h { y add
0 1 w { x add
a exch % y+h a x+w
2 index % y+h a x+w y+h
b putpix8
} for
pop
} for
end
} def

% (filename) loadimg8 img true
% false
/loadimg8 {
2 dict begin
{
{
(r) file
/f exch def
f token not{stop}if
(P2)ne{stop}if
/w f token not{stop}if def
/h f token not{stop}if def
f token not{stop}if pop % discard depth
/a w h img8 def
0 1 h 1 sub {
0 1 w 1 sub {
a exch 2 index % y a x y
f token not{stop}if putpix8
} for
pop
} for

f closefile
exit } loop
} stopped {
false
}{
a true
} ifelse
end
} def

/printimg8 { % PGM format
(P2\n)=only
dup 0 get length =only %w
( )=only
dup length =only %h
(\n)=only
(255\n)=only %max
{{=only( )=only}forall(\n)=only}forall %data
(\n)=only
} def

/saveimg8 { % img (filename)
(w) file
2 dict begin
{f a}{exch def}forall
f (P2\n) writestring
f a 0 get length =string cvs writestring
f ( ) writestring
f a length =string cvs writestring
f (\n255\n) writestring
a {
{
=string cvs dup length exch
f exch writestring
neg 4 add { f ( ) writestring } repeat
} forall
f (\n) writestring
} forall
f closefile
end
} def


/DisplayConvexPolygon { % The Goal.
} def

{
/p 20 20 img8 def
p 1 1 5 putpix8
p 2 2 6 putpix8
p 3 3 7 putpix8
p 2 2 9 15 9 drawline8
p 3 3 14 14 4 drawrect8

p (mysavedimg.pgm) saveimg8
} pop%exec

{
/p (mysavedimg.pgm) loadimg8 {
def
p printimg8
} if
} exec
0 new messages