Pdf 为什么此postscript计算器(类型4)着色在高缩放时渲染如此缓慢?

Pdf 为什么此postscript计算器(类型4)着色在高缩放时渲染如此缓慢?,pdf,pdf-generation,postscript,Pdf,Pdf Generation,Postscript,为了产生符合规格的平滑渐变,我尝试了使用类型4(postscript计算器)着色,这样我就可以编写指定每个点的颜色的函数。这里是我生成的函数,它接受两个实数(x和y坐标在[0,1]x[0,1]),并返回三个实数(颜色的r、g、b分量): 以下是生成上述字符串以及实际pdf文件的代码: // input: a nonnegative real number r^2 (the square of the distance) // output: min(1/r^2, 10000.0) string

为了产生符合规格的平滑渐变,我尝试了使用类型4(postscript计算器)着色,这样我就可以编写指定每个点的颜色的函数。这里是我生成的函数,它接受两个实数(x和y坐标在[0,1]x[0,1]),并返回三个实数(颜色的r、g、b分量):

以下是生成上述字符串以及实际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});
//输入:非负实数r^2(距离的平方)
//输出:最小值(1/r^2,10000.0)
字符串ps_weight_rsquared='dup.0001 le{pop 10000.0}{1.0 exch div}ifelse';
//输入:向量的x和y坐标
//输出:x^2+y^2
字符串ps_distsquared='dup mul exch dup mul add';
//输入:x和y坐标
//输出:在(x,y)处的重量
管柱重量位移=重量平方+重量平方;
//输入:x,y
//输出:向量((x,y)-点处的权重
字符串ps\u\u对(对点){
//计算位移:
string toreturn=''+(字符串)点.y+'子exch'+(字符串)点.x+'子exch';
//根据位移计算重量:
返回toreturn+ps\U重量\U位移;
}
/*该字符串将是一个接受
*一对,返回原始权重列表,其中包含最深的权重
*在与点[0]对应的堆栈上。
*/
字符串ps\u\u对(对[]点){
字符串返回=“”;
对于(int i=0;i
以下是输出,转换为
png
文件:

这和我的想法差不多


问题:如果我打开pdf文件(使用Apple Previewer或Adobe Reader)并放大,渲染程序会慢到爬行,并且(根据活动监视器)使用100%的CPU(来自一个核心;幸运的是我有其他核心,所以其他应用程序会继续响应)我是否在postscript函数中做了一些计算过于密集的事情?如果是,我是否使用了错误或糟糕的编码实践(内存泄漏、过多的
roll
s…),或者这仅仅是我使用的算法的必然结果(例如,渲染器能否处理每像素五个分割)


不管怎样,为什么只有在放大时才会显示?在我滚动的情况下,渲染器是否尝试在内部渲染整个放大图像?

您没有说您使用的是哪个pdf查看器,但不同的查看器将得到非常不同的优化

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

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

乍一看,许多
0
// 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});