为了产生规格的平滑渐变,我尝试使用类型4(postscript计算器)着色,以便我可以编写指定每个点的颜色的函数。这是我生成的函数,它接受两个实数([0,1] x [0,1]上的x和y坐标)并返回三个实数(颜色的r,g,b分量):
2 copy 0.25 sub exch 0.25 sub exch dup mul exch dup mul add dup .0001 le {pop 10000.0} {1.0 exch div} ifelse
3 1 roll 2 copy 0.75 sub exch 0.75 sub exch dup mul exch dup mul add dup .0001 le {pop 10000.0} {1.0 exch div} ifelse
3 1 roll 2 copy 0.75 sub exch 0.25 sub exch dup mul exch dup mul add dup .0001 le {pop 10000.0} {1.0 exch div} ifelse
3 1 roll 0.25 sub exch 0.75 sub exch dup mul exch dup mul add dup .0001 le {pop 10000.0} {1.0 exch div} ifelse
4 copy 0.0 add add add add 1.0 exch div
dup 3 1 roll mul 5 1 roll
dup 3 1 roll mul 5 1 roll
dup 3 1 roll mul 5 1 roll
dup 3 1 roll mul 5 1 roll pop
4 copy 0.0 exch 0 mul add exch 1 mul add exch 0 mul add exch 1 mul add 5 1 roll
4 copy 0.0 exch 0 mul add exch 1 mul add exch 1 mul add exch 0 mul add 5 1 roll
0.0 exch 1 mul add exch 0 mul add exch 0 mul add exch 0 mul add
以下是生成上述字符串的Asymptote代码以及实际的pdf文件:
// input: a nonnegative real number r^2 (the square of the distance)
// output: min(1/r^2, 10000.0)
string ps_weight_rsquared = ' dup .0001 le {pop 10000.0} {1.0 exch div} ifelse';
// input: x and y coordinates of a vector
// output: x^2 + y^2
string ps_distsquared = ' dup mul exch dup mul add';
//input: x and y coordinates
//output: the weight at (x,y)
string ps_weight_displacement = ps_distsquared + ps_weight_rsquared;
//input: x, y
//output: weight at the vector ((x,y) - point)
string ps_naiveWeight_pair(pair point) {
// compute displacement:
string toreturn = ' ' + (string)point.y + ' sub exch ' + (string)point.x + ' sub exch' ;
// compute weight from displacement:
return toreturn + ps_weight_displacement;
}
/* The string will be an postscript calculator formula that accepts
* a pair and returns a list of naive weights, with the deepest weight
* on the stack corresponding to points[0].
*/
string ps_naiveWeights_pair(pair[] points) {
string toreturn = '';
for (int i = 0; i < points.length; ++i) {
if (i < points.length - 1)
toreturn += ' 2 copy';
toreturn += ps_naiveWeight_pair(points[i]);
if (i < points.length - 1)
toreturn += ' 3 1 roll';
}
return toreturn;
}
// input: x,y
// output: the weights of all the displacement vectors ((x,y) - points[i]), normalized so that their sum is one
string ps_partitionWeights_pair(pair[] points) {
string toreturn = ps_naiveWeights_pair(points);
// compute the sum of the all the naive weights:
toreturn += ' ' + (string)points.length + ' copy 0.0';
for (int i = 0; i < points.length; ++i)
toreturn += ' add';
// take the reciprocal of the sum:
toreturn += ' 1.0 exch div';
for (int i = 1; i <= points.length; ++i) {
// multiply a weight by the sum reciprocal and roll the new weight to the back:
toreturn += ' dup 3 1 roll mul ' + (string)(1+points.length) + ' 1 roll';
}
//discard the sum reciprocal, which is no longer needed:
toreturn += ' pop';
return toreturn;
}
// Assumes the weights are already on the stack, with the deepest weight
// corresponding to summands[0].
string ps_weighted_sum(real[] summands) {
// At each step, the top element of the stack should be the sum so far:
string toreturn = ' 0.0';
while(summands.length > 0) {
toreturn += ' exch ' + (string)(summands.pop()) + ' mul add';
}
return toreturn;
}
// input: real numbers x, y
// output: shading function based on a weighted sum of the colors, with the weight of the color of point p equal to 1/(dist to p)^2 (and the weights normalized to have sum one)
string ps_interpolate_shade(path g, pair[] points, pen[] pointcolors) {
pair min = min(g);
pair max = max(g);
real[] reds, greens, blues;
for (pen thecolor : pointcolors) {
real[] thecolors = colors(rgb(thecolor));
reds.push(thecolors[0]);
greens.push(thecolors[1]);
blues.push(thecolors[2]);
}
transform t = scale(1/(max.x - min.x), 1/(max.y - min.y)) * shift(-min);
points = t * points;
string toreturn = ps_partitionWeights_pair(points);
toreturn += ' ' + (string)points.length + ' copy';
toreturn += ps_weighted_sum(reds);
toreturn += ' ' + (string)(points.length + 1) + ' 1 roll';
toreturn += ' ' + (string)points.length + ' copy';
toreturn += ps_weighted_sum(greens);
toreturn += ' ' + (string)(points.length + 1) + ' 1 roll';
toreturn += ps_weighted_sum(blues);
return toreturn;
}
void applyInterpolateShade(path g, pair[] points, pen[] pointcolors) {
string shader = ps_interpolate_shade(g, points, pointcolors);
write(shader); //output the ps string to the terminal
functionshade(g, fillrule=rgb(zerowinding), shader=shader);
}
/********************************************/
settings.tex = "pdflatex";
size(5cm);
applyInterpolateShade(unitcircle, new pair[] {(-.5,-.5), (.5,.5), (-.5,.5), (.5,-.5)}, new pen[] {red, green, yellow, blue});
这是输出,转换为png
文件:
这几乎是我的想法。
问题:如果我打开pdf文件(使用Apple Previewer或Adobe Reader)并放大,渲染程序会慢慢爬行(并根据活动监视器)使用100% CPU(来自一个核心;幸运的是我有其他核心,因此其他应用程序继续响应)。 我在postscript函数中做了哪些计算过于密集的事情?如果是这样,我是使用错误还是错误的编码实践(内存泄漏,roll
s太多,......)或者它只是我正在使用的算法的一个不可避免的后果(例如,渲染器可以不处理每像素五个分区)?
无论哪种方式,为什么这只会在我放大时出现?渲染器是否尝试在内部渲染整个放大图像,以防我滚动?
答案 0 :(得分:1)
您不会说您正在使用哪种pdf查看器,但不同的查看器将以非常不同的方式进行优化。
阴影设计为插值,即阴影中的选定坐标应使用PS评估函数进行评估。这些像素之间的绝大多数像素应该是线性插值的。评估坐标的选择取决于当前的平滑度。在使用ExtGState字典的SM条目选择的PDF中。阴影区域将被分解,直到检测到小区域为“平滑”区域。相对于SM值。你可以尝试改变SM; Acrobat中的默认值为0.02,但是YMMV。
如果您的阴影需要很长时间,可能会发生一些事情。该功能可能是高度非线性的;具有尖锐边缘的指数函数和函数可以防止线性度的检测,直到区域变得非常小,可能小到1个像素。或者,您的pdf查看器不会针对着色进行优化。或者很可能这两个。 FWIW。我无法说这个PS计算器功能是否适合分解,因为我无法说出它在做什么。