Skip to content

Commit c0c25f4

Browse files
committed
renmatrix: Optional rowmult parameter expands consequtive identical rows
1 parent 92cabfc commit c0c25f4

3 files changed

Lines changed: 112 additions & 13 deletions

File tree

src/renmatrix.ps.src

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ begin
192192

193193
(input.processoptions) //renmatrix.after exec
194194

195+
% Default rowmult to all 1s if not provided
196+
currentdict /rowmult known not {
197+
/rowmult [ pixy { 1 } repeat ] def
198+
} if
199+
200+
/pixycomp rowmult length def
201+
/rowcumul pixycomp 1 add array def
202+
rowcumul 0 0 put
203+
0 1 pixycomp 1 sub { % i
204+
dup rowcumul exch get % ... rowcumul[i]
205+
1 index rowmult exch get add % ... rowcumul[i] + rowmult[i]
206+
exch 1 add exch rowcumul 3 1 roll put % rowcumul[i+1] = rowcumul[i]+rowmult[i]
207+
} for
208+
195209
%
196210
% Input validation
197211
%
@@ -228,10 +242,14 @@ begin
228242
/bwipp.renmatrixBadDimensions (The symbol dimensions must be positive) //raiseerror exec
229243
} if
230244

231-
pixs length pixx pixy mul lt {
245+
pixs length pixx pixycomp mul lt {
232246
/bwipp.renmatrixBadPixelData (The pixel data is shorter than the symbol dimensions require) //raiseerror exec
233247
} if
234248

249+
rowcumul pixycomp get pixy ne {
250+
/bwipp.renmatrixRowmultMismatch (rowmult sum does not equal pixy) //raiseerror exec
251+
} if
252+
235253
(input.validate) //renmatrix.after exec
236254

237255
%
@@ -334,16 +352,16 @@ begin
334352

335353
% Pad the bitmap on all sides
336354
/pixx pixx 2 add def
337-
/pixy pixy 2 add def
355+
/pixycomp pixycomp 2 add def
338356
/pixxpad pixx def
339357
/zerorow [ pixx {0} repeat ] def
340-
pixx pixy mul { array } stopped {
358+
pixx pixycomp mul { array } stopped {
341359
pop /bwipp.renmatrixBitmapTooLarge (The bitmap exceeds the implementation limits) //raiseerror exec
342360
} if /padded exch def
343361
0 pixx padded length 1 sub {
344362
padded exch zerorow putinterval
345363
} for
346-
0 1 pixy 3 sub {
364+
0 1 pixycomp 3 sub {
347365
/row exch def
348366
padded row 1 add pixx mul 1 add pixs row pixx 2 sub mul pixx 2 sub getinterval putinterval
349367
} for
@@ -363,11 +381,11 @@ begin
363381
% Pre-compute abcd values (printed neighbours) for all cells
364382
%
365383
/abcdw pixx 1 sub def
366-
abcdw pixy 1 sub mul { array } stopped {
384+
abcdw pixycomp 1 sub mul { array } stopped {
367385
pop /bwipp.renmatrixBitmapTooLarge (The bitmap exceeds the implementation limits) //raiseerror exec
368386
} if /abcdcache exch def
369387
/abcdrow abcdw array def
370-
0 1 pixy 2 sub {
388+
0 1 pixycomp 2 sub {
371389
/j exch def
372390
0 1 abcdw 1 sub {
373391
dup j abcd abcdrow 3 1 roll put
@@ -381,7 +399,7 @@ begin
381399
% Construct paths by tracing regions avoiding duplication by using the cache
382400
%
383401
/paths [
384-
0 1 pixy 2 sub {
402+
0 1 pixycomp 2 sub {
385403
/j exch def
386404
0 1 pixx 2 sub {
387405
/i exch def
@@ -405,7 +423,7 @@ begin
405423
% Revert the bitmap size
406424
%
407425
/pixx pixx 2 sub def
408-
/pixy pixy 2 sub def
426+
/pixycomp pixycomp 2 sub def
409427

410428
options /debugpaths known
411429
/uk.co.terryburton.bwipp.global_ctx dup where {exch get /enabledebug known} {pop false} ifelse
@@ -422,11 +440,12 @@ begin
422440
paths {
423441
/p exch def
424442
/len p length def
425-
p len 1 sub get unpack
426-
p 0 get unpack
443+
p len 1 sub get unpack rowcumul exch get
444+
p 0 get unpack rowcumul exch get
427445
0 1 len 1 sub { % x1 y1 x2 y2
428446
/i exch def
429-
p i 1 add len mod get unpack 6 -2 roll % x3 y3 x1 y1 x2 y2
447+
p i 1 add len mod get unpack
448+
rowcumul exch get 6 -2 roll % x3 y3 x1 y1 x2 y2
430449
5 index inkspreadh 2 div
431450
4 index 4 -1 roll lt {add} {sub} ifelse % y3<y1 ? x2+ih : x2-ih
432451
4 1 roll
@@ -458,8 +477,11 @@ begin
458477
dup pixx mod /x exch def
459478
pixx idiv /y exch def
460479
x y xyget 1 eq {
461-
x 0.5 add pixy y sub 0.5 sub moveto
462-
x 0.5 add pixy y sub 0.5 sub 0.5 inkspread 2 div sub 0 360 arc
480+
rowcumul y get 1 rowcumul y 1 add get 1 sub {
481+
/vy exch def
482+
x 0.5 add pixy vy sub 0.5 sub moveto
483+
x 0.5 add pixy vy sub 0.5 sub 0.5 inkspread 2 div sub 0 360 arc
484+
} for
463485
} if
464486
} for
465487
fill

tests/ps_tests/renmatrix.ps.test

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,83 @@
586586
{ 0 0 moveto << /ren /renmatrix /pixs [1 1] /pixx 0 /pixy 2 /opt <<>> >> renmatrix } /bwipp.renmatrixBadDimensions isError
587587
{ 0 0 moveto << /ren /renmatrix /pixs [1] /pixx 3 /pixy 3 /opt <<>> >> renmatrix } /bwipp.renmatrixBadPixelData isError
588588

589+
%
590+
% rowmult (vertical RLE)
591+
%
592+
593+
% rowmult: debugpaths on compressed grid — 1x1 bitmap with rowmult=[3] maps to 1x3 visual
594+
{
595+
0 0 moveto
596+
<<
597+
/ren /renmatrix
598+
/pixs [1]
599+
/pixx 1
600+
/pixy 3
601+
/rowmult [3]
602+
/opt << /debugpaths true >>
603+
>> renmatrix
604+
} [
605+
8 0 0 1 0 1 1 0 1
606+
] debugIsEqual
607+
608+
% rowmult: 2x2 compressed with non-uniform rowmult=[1 2] gives pixy=3
609+
{
610+
0 0 moveto
611+
<<
612+
/ren /renmatrix
613+
/pixs [1 1 1 1]
614+
/pixx 2
615+
/pixy 3
616+
/rowmult [1 2]
617+
/opt << /debugpaths true >>
618+
>> renmatrix
619+
} [
620+
8 0 0 2 0 2 2 0 2
621+
] debugIsEqual
622+
623+
% rowmult: smoke test — renders without error
624+
{ 0 0 moveto << /ren /renmatrix /pixs [1 1 1 1] /pixx 2 /pixy 3 /height 3 72 div /width 2 72 div /rowmult [1 2] /opt <<>> >> renmatrix true } true isEqual
625+
626+
% rowmult: checkerboard compressed with uniform rowmult
627+
{
628+
0 0 moveto
629+
<<
630+
/ren /renmatrix
631+
/pixs [1 0 0 1]
632+
/pixx 2
633+
/pixy 6
634+
/rowmult [3 3]
635+
/opt << /debugpaths true >>
636+
>> renmatrix
637+
} [
638+
8 0 0 1 0 1 1 0 1
639+
8 1 1 2 1 2 2 1 2
640+
] debugIsEqual
641+
642+
% rowmult: dotty mode renders without error
643+
{ 0 0 moveto << /ren /renmatrix /pixs [1 0 0 1] /pixx 2 /pixy 4 /height 4 72 div /width 2 72 div /rowmult [1 3] /opt << /dotty true >> >> renmatrix true } true isEqual
644+
645+
% rowmult: identity rowmult [1 1 1] produces same debugpaths as no rowmult
646+
{
647+
0 0 moveto
648+
<<
649+
/ren /renmatrix
650+
/pixs [1 0 1 0 1 0]
651+
/pixx 2
652+
/pixy 3
653+
/rowmult [1 1 1]
654+
/opt << /debugpaths true >>
655+
>> renmatrix
656+
} [
657+
8 0 0 1 0 1 3 0 3
658+
] debugIsEqual
659+
660+
% rowmult: error — sum does not equal pixy
661+
{ 0 0 moveto << /ren /renmatrix /pixs [1] /pixx 1 /pixy 2 /rowmult [3] /opt <<>> >> renmatrix } /bwipp.renmatrixRowmultMismatch isError
662+
663+
% rowmult: error — pixs too short for compressed dimensions
664+
{ 0 0 moveto << /ren /renmatrix /pixs [1] /pixx 2 /pixy 3 /rowmult [1 2] /opt <<>> >> renmatrix } /bwipp.renmatrixBadPixelData isError
665+
589666

590667
%
591668
% text3-9 options and validation

tests/ps_tests/test_utils.ps

445 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)