Skip to content

Commit d488cd7

Browse files
committed
renmatrix: Use a packed integer to avoid allocating lots of arrays for paths
1 parent 9ab4c32 commit d488cd7

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/renmatrix.ps.src

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,13 @@ begin
256256
exch pixs exch get 8 mul add
257257
} def
258258

259-
/left {dir //renmatrix.left ne {x y dir cput [x y]} if /x x 1 sub def /dir //renmatrix.left def} def
260-
/right {dir //renmatrix.right ne {x y dir cput [x y]} if /x x 1 add def /dir //renmatrix.right def} def
261-
/up {dir //renmatrix.up ne {x y dir cput [x y]} if /y y 1 sub def /dir //renmatrix.up def} def
262-
/down {dir //renmatrix.down ne {x y dir cput [x y]} if /y y 1 add def /dir //renmatrix.down def} def
259+
/pack { pixxpad mul add } def % x y -> y*pixxpad+x
260+
/unpack { dup pixxpad mod exch pixxpad idiv } def % packed -> x y
261+
262+
/left {dir //renmatrix.left ne {x y dir cput x y pack} if /x x 1 sub def /dir //renmatrix.left def} def
263+
/right {dir //renmatrix.right ne {x y dir cput x y pack} if /x x 1 add def /dir //renmatrix.right def} def
264+
/up {dir //renmatrix.up ne {x y dir cput x y pack} if /y y 1 sub def /dir //renmatrix.up def} def
265+
/down {dir //renmatrix.down ne {x y dir cput x y pack} if /y y 1 add def /dir //renmatrix.down def} def
263266

264267
%
265268
% Walk the outline of a region emitting edge coordinates of the path
@@ -270,7 +273,7 @@ begin
270273
/sx x def /sy y def /sdir dir def
271274

272275
mark {
273-
//renmatrix.tracestep x y abcd get exec
276+
//renmatrix.tracestep x y abcdget get exec
274277
x sx eq y sy eq and dir sdir eq and {exit} if
275278
} loop
276279
counttomark array astore exch pop
@@ -337,12 +340,27 @@ begin
337340
] def
338341
/pixx pixx 2 add def
339342
/pixy pixy 2 add def
343+
/pixxpad pixx def
340344

341345
%
342346
% Cache of visited corners for each direction
343347
%
344348
/cache [ pixs length {0} repeat ] def
345349

350+
%
351+
% Pre-compute abcd values (printed neighbours) for all cells
352+
%
353+
/abcdcache [
354+
0 1 pixy 2 sub {
355+
0 1 pixx 2 sub {
356+
1 index abcd exch % result j
357+
} for
358+
pop
359+
} for
360+
] def
361+
362+
/abcdget { pixx 1 sub mul add abcdcache exch get } def
363+
346364
%
347365
% Construct paths by tracing regions avoiding duplication by using the cache
348366
%
@@ -351,7 +369,7 @@ begin
351369
/j exch def
352370
0 1 pixx 2 sub {
353371
/i exch def
354-
/k i j abcd def
372+
/k i j abcdget def
355373
k 2#0001 eq % Black region stem corners:
356374
k 2#1001 eq or { %
357375
//renmatrix.up i j cget 0 eq { % 0 0 1 0
@@ -380,11 +398,11 @@ begin
380398
paths {
381399
/p exch def
382400
/len p length def
383-
p len 1 sub get aload pop
384-
p 0 get aload pop
401+
p len 1 sub get unpack
402+
p 0 get unpack
385403
0 1 len 1 sub { % x1 y1 x2 y2
386404
/i exch def
387-
p i 1 add len mod get aload pop 6 -2 roll % x3 y3 x1 y1 x2 y2
405+
p i 1 add len mod get unpack 6 -2 roll % x3 y3 x1 y1 x2 y2
388406
5 index inkspreadh
389407
4 index 4 -1 roll lt {add} {sub} ifelse % y3<y1 ? x2+i : x2-i
390408
4 1 roll

0 commit comments

Comments
 (0)