gpt4 book ai didi

pdf - 为什么这个 postscript 计算器(类型 4)在高倍率下的渲染速度如此之慢?

转载 作者:行者123 更新时间:2023-12-04 22:22:49 25 4
gpt4 key购买 nike

为了根据规范生成平滑的渐变,我尝试使用类型 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 或 Adob​​e Reader)并放大,渲染程序会缓慢地爬行并且(根据 Activity Monitor)使用 100% CPU(来自一个核心;幸运的是我有其他核心,所以其他应用程序会继续响应)。 我是否在 postscript 函数中做了一些计算量太大的事情?如果是这样,我是否使用了错误或错误的编码实践(内存泄漏、太多的 roll、.. .) 或者这仅仅是我使用的算法的必然结果(例如,渲染器不能处理每个像素五个分区)?

无论哪种方式,为什么只有在我放大时才会出现?渲染器是否试图在内部渲染整个放大图像以防我四处滚动?

最佳答案

您没有说您使用的是哪个 pdf 查看器,但不同的查看器会进行非常不同的优化。

阴影设计为可插值的,即应使用 PS 评估器函数评估阴影内的选定坐标。这些之间的绝大多数像素应该是线性插值的。评估坐标的选择取决于当前的平滑度。在使用 ExtGState 字典的 SM 条目选择的 PDF 中。阴影区域将被分解,直到检测到小区域相对于 SM 值“平滑”为止。你可以试试换SM; Acrobat 中的默认值为 0.02,但 YMMV。

如果您的着色需要很长时间,则可能会发生一些事情。该函数可能是高度非线性的;指数函数和具有尖锐边缘的函数可以阻止线性检测,直到区域变得非常小,可能小到 1 个像素。或者,您的 pdf 查看器未针对阴影进行优化。或者很可能是这两者。 FWIW。我不能说这个 PS 计算器函数是否不适合分解,因为我不知道它在做什么。

关于pdf - 为什么这个 postscript 计算器(类型 4)在高倍率下的渲染速度如此之慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20887929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com