2011-09-21 80 views
3

我学习后记我试图创建一个方法,将绘制垂直梯度后记:绘制渐变

这里是我的代码:

%!PS-Adobe-3.0 

%%%%%%%%%%%%%%%%%%%%%%% 
% draw a RECTANGLE 
/Rect { 
/h exch def % height 
/w exch def % width 
w 0 rlineto 
0 h rlineto 
-1.0 w mul 0 rlineto 
0 -1.0 h mul rlineto 
} def 
%%%%%%%%%%%%%%%%%%%%%%% 
% draw a Gradient 
/VGrad { 
/h exch def % height 
/w exch def % width 
/c2 exch def %gray-end 
/c1 exch def %gray-start 
/index 0.0 def %loop-index 

0 1 h { %loop over height 
    gsave 
    c2 c1 sub index h div mul c1 add setgray 

    w h index sub Rect 
    stroke 

    /index index 1.0 add def % index++ 
    grestore 
    } for 

} def 
%%%%%%%%%%%%%%%%%%%%%%% 
%test script 
200 600 moveto 
.1 .9 100 10 VGrad 

showpage 

但GS引发错误:

GPL Ghostscript 8.70 (2009-07-31) 
Copyright (C) 2009 Artifex Software, Inc. All rights reserved. 
This software comes with NO WARRANTY: see the file PUBLIC for details. 
Error: /undefinedresult in --div-- 
Operand stack: 
    0 1 2 3 4 5 0.8 5.0 0.0 
Execution stack: 
    %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3 %oparray_pop 1739 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- 6 1 10 --nostringval-- %for_pos_int_continue --nostringval-- 
Dictionary stack: 
    --dict:1150/1684(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)-- 
Current allocation mode is local 
Current file position is 588 
GPL Ghostscript GPL Ghostscript 8.708.70: : Unrecoverable error, exit code 1 
Unrecoverable error, exit code 1 

我在哪里错了?

+0

BTW,除非你使用DSC意见,魔线应该是'%!PS'甚至只是'%!'。 '-Adobe-3.0'部分是文件打算符合的DSC版本号。 –

回答

3

好的,我发现问题:看来索引是一个保留字。这里是一个功能版本:

/box 
{ 
4 dict begin 
/height exch def 
/width exch def 
/y exch def 
/x exch def 
x y moveto 
width 0 rlineto 
0 height rlineto 
width -1 mul 0 rlineto 
0 height -1 mul rlineto 
end 
} bind def 


/gradient 
{ 
4 dict begin 
/height exch def 
/width exch def 
/y exch def 
/x exch def 
/i 0 def 
height 2 div /i exch def 

0 1 height 2 div { 
    1 i height 2.0 div div sub setgray 
    newpath 
    x 
    y height 2 div i sub add 
    width 
    i 2 mul 
    box 
    closepath 
    fill 
    i 1 sub /i exch def 
    }for 
newpath 
0 setgray 
0.4 setlinewidth 
x y width height box 
closepath 
stroke 
end 
} bind def 
2

我没有完全理解你的代码。但错误消息试图告诉你,你除以零(查看剩余操作数堆栈的顶层元素:“0”)。

仅仅通过加“1”到您的h变量(插入h1 add)使得通过Ghostscript的解释器中运行您的PostScript程序,让它吸取的东西(尽管这可能不会像你设想的....)。

6

你的程序将通过巧妙的堆栈操作和CTM影响执行得更快。

这不是一个像你这样的平滑渐变,但执行速度要快得多,并且函数计算为单行语句(我只是好像更好,无法解释为什么)。

也很高兴张贴使用和可能是一个样本页面(容易修剪,当你不需要它,但当你需要它?? ?? !!)无论如何,这是你的程序改写我的方式,FWIW。

正如我所说的,它的输出并不像现在这样漂亮。但是您可以通过使用currenttransfersettransfer更改灰度传递函数和/或将框的计算更改为对数缩放来更改梯度,并更改灰度的范围和速度。这些东西应该更容易在“更紧密”的代码中看到。堆栈评论可帮助您在每行结尾处“检查您的理解”。

编辑:我不能停止玩它!我已经分析了这个循环,并找出了一些更多的参数。

编辑:另一个扩展。那么一张漂亮的照片怎么样?

%! 

/box { % x y w h 
    4 2 roll moveto % w h 
    1 index 0 rlineto % w h 
    0 exch rlineto % w 
    neg 0 rlineto % 
    closepath 
} def 

/poly { % n 
    0.5 0 moveto 
    { ? rotate 0.5 0 lineto } % n proc 
    dup 0 360 4 index div put % n {360/n...} 
    repeat 
    closepath 
} def 

% num-slices shapeproc matrix grayproc agrad - 
% repeatedly (fill shape, concat matrix, transform currentgray) 
/agrad { 
    3 dict begin /gray exch def /mat exch def /shape exch def 
    ({ //shape exec //mat concat currentgray //gray exec setgray }) 
    token pop exch pop end bind repeat 
} def 

/shapes [ 
    { -0.5 -0.5 1 1 box fill } %box shape 
    { 0 0 0.5 0 360 arc fill } %circle shape 
    { 0 0 0.5 0 180 arc fill } %fan shape 
    { 5 poly fill } %pentagon 
    { 6 poly fill } %hexagon 
    ] def 

/mats [ 
    {1 index 2 exch div 1 exch sub dup matrix scale } %pyramid matrix 
    {1 index 2 exch div 1 exch sub 1 matrix scale } %horizontal matrix 
    {1 index 2 exch div 1 exch sub 1 exch matrix scale } %vertical matrix 
    ] def 

% mat-no shape-no gray0 grayF n x y w h dograd - 
/dograd { 
    gsave 
     4 2 roll translate % m sh g0 gF n w h 
     scale % m sh g0 gF n 
     3 1 roll % m sh n g0 gF 
     1 index sub 2 index div % m sh n g0 (gF-g0)/n 
     [ exch /add cvx ] cvx % m sh n g0 grayproc 
     3 1 roll setgray % m sh grayproc n 
     3 -1 roll shapes exch get % m gray n shape 
     4 -1 roll mats exch get exec % gray n shape mat 
     4 -1 roll %n shape matrix gray 
     agrad 
    grestore 
} def 

%mat shape g0 gF n x y w h 
0 4 .7 .1 20 300 400 600 800 dograd 

0 0 0 1 10 100 650 200 200 dograd 
1 1 0 1 20 300 650 200 200 dograd 
2 2 .5 1 30 500 650 200 200 dograd 

0 3 1 0 40 100 400 200 200 dograd 
1 4 1 .5 50 300 400 200 200 dograd 
2 1 .5 0 60 500 400 200 200 dograd 

0 2 .1 .9 10 100 150 200 200 dograd 
1 3 .2 .8 20 300 150 200 200 dograd 
2 4 .3 .7 30 500 150 200 200 dograd 

showpage 

png output

+0

我破解了一些[螺旋渐变](http://groups.google.com/group/comp.lang.postscript/browse_thread/thread/2465c34c29662914/c8f902f387a25726#c8f902f387a25726)。图像很漂亮,但代码有点乱。 –

+0

即使早期程序'ygrad'没有在这个图像中使用,阅读它将对希望了解后面的程序的postscript程序员非常有用。不要嘟my我自己的号角,但这不是初学者级的后记。在将它们馈送给循环操作员以获得最大速度之前,它动态修改过程体。这个速度让我们有机会做......呃..老鼠,我从来没有对这种“未经优化的”代码进行测量。所有这些hlalaballoo可能是完全没有必要的。 :) –