home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxpflat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  14.4 KB  |  450 lines

  1. /* Copyright (C) 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxpflat.c,v 1.2 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Path flattening algorithms */
  21. #include "gx.h"
  22. #include "gxarith.h"
  23. #include "gxfixed.h"
  24. #include "gzpath.h"
  25.  
  26. /* Define whether to merge nearly collinear line segments when flattening */
  27. /* curves.  This is very good for performance, but we feel a little */
  28. /* uneasy about its effects on character appearance. */
  29. #define MERGE_COLLINEAR_SEGMENTS 1
  30.  
  31. /* ---------------- Curve flattening ---------------- */
  32.  
  33. #define x1 pc->p1.x
  34. #define y1 pc->p1.y
  35. #define x2 pc->p2.x
  36. #define y2 pc->p2.y
  37. #define x3 pc->pt.x
  38. #define y3 pc->pt.y
  39.  
  40. /*
  41.  * To calculate how many points to sample along a path in order to
  42.  * approximate it to the desired degree of flatness, we define
  43.  *      dist((x,y)) = abs(x) + abs(y);
  44.  * then the number of points we need is
  45.  *      N = 1 + sqrt(3/4 * D / flatness),
  46.  * where
  47.  *      D = max(dist(p0 - 2*p1 + p2), dist(p1 - 2*p2 + p3)).
  48.  * Since we are going to use a power of 2 for the number of intervals,
  49.  * we can avoid the square root by letting
  50.  *      N = 1 + 2^(ceiling(log2(3/4 * D / flatness) / 2)).
  51.  * (Reference: DEC Paris Research Laboratory report #1, May 1989.)
  52.  *
  53.  * We treat two cases specially.  First, if the curve is very
  54.  * short, we halve the flatness, to avoid turning short shallow curves
  55.  * into short straight lines.  Second, if the curve forms part of a
  56.  * character (indicated by flatness = 0), we let
  57.  *      N = 1 + 2 * max(abs(x3-x0), abs(y3-y0)).
  58.  * This is probably too conservative, but it produces good results.
  59.  */
  60. int
  61. gx_curve_log2_samples(fixed x0, fixed y0, const curve_segment * pc,
  62.               fixed fixed_flat)
  63. {
  64.     fixed
  65.     x03 = x3 - x0,
  66.     y03 = y3 - y0;
  67.     int k;
  68.  
  69.     if (x03 < 0)
  70.     x03 = -x03;
  71.     if (y03 < 0)
  72.     y03 = -y03;
  73.     if ((x03 | y03) < int2fixed(16))
  74.     fixed_flat >>= 1;
  75.     if (fixed_flat == 0) {    /* Use the conservative method. */
  76.     fixed m = max(x03, y03);
  77.  
  78.     for (k = 1; m > fixed_1;)
  79.         k++, m >>= 1;
  80.     } else {
  81.     const fixed
  82.           x12 = x1 - x2, y12 = y1 - y2, dx0 = x0 - x1 - x12, dy0 = y0 - y1 - y12,
  83.           dx1 = x12 - x2 + x3, dy1 = y12 - y2 + y3, adx0 = any_abs(dx0),
  84.           ady0 = any_abs(dy0), adx1 = any_abs(dx1), ady1 = any_abs(dy1);
  85.  
  86.     fixed
  87.         d = max(adx0, adx1) + max(ady0, ady1);
  88.     /*
  89.      * The following statement is split up to work around a
  90.      * bug in the gcc 2.7.2 optimizer on H-P RISC systems.
  91.      */
  92.     uint qtmp = d - (d >> 2) /* 3/4 * D */ +fixed_flat - 1;
  93.     uint q = qtmp / fixed_flat;
  94.  
  95.     if_debug6('2', "[2]d01=%g,%g d12=%g,%g d23=%g,%g\n",
  96.           fixed2float(x1 - x0), fixed2float(y1 - y0),
  97.           fixed2float(-x12), fixed2float(-y12),
  98.           fixed2float(x3 - x2), fixed2float(y3 - y2));
  99.     if_debug2('2', "     D=%f, flat=%f,",
  100.           fixed2float(d), fixed2float(fixed_flat));
  101.     /* Now we want to set k = ceiling(log2(q) / 2). */
  102.     for (k = 0; q > 1;)
  103.         k++, q = (q + 3) >> 2;
  104.     if_debug1('2', " k=%d\n", k);
  105.     }
  106.     return k;
  107. }
  108.  
  109. /*
  110.  * Define the maximum number of points for sampling if we want accurate
  111.  * rasterizing.  2^(k_sample_max*3)-1 must fit into a uint with a bit
  112.  * to spare; also, we must be able to compute 1/2^(3*k) by table lookup.
  113.  */
  114. #define k_sample_max min((size_of(int) * 8 - 1) / 3, 10)
  115.  
  116. /*
  117.  * Split a curve segment into two pieces at the (parametric) midpoint.
  118.  * Algorithm is from "The Beta2-split: A special case of the Beta-spline
  119.  * Curve and Surface Representation," B. A. Barsky and A. D. DeRose, IEEE,
  120.  * 1985, courtesy of Crispin Goswell.
  121.  */
  122. private void
  123. split_curve_midpoint(fixed x0, fixed y0, const curve_segment * pc,
  124.              curve_segment * pc1, curve_segment * pc2)
  125. {                /*
  126.                  * We have to define midpoint carefully to avoid overflow.
  127.                  * (If it overflows, something really pathological is going
  128.                  * on, but we could get infinite recursion that way....)
  129.                  */
  130. #define midpoint(a,b)\
  131.   (arith_rshift_1(a) + arith_rshift_1(b) + ((a) & (b) & 1) + 1)
  132.     fixed x12 = midpoint(x1, x2);
  133.     fixed y12 = midpoint(y1, y2);
  134.  
  135.     /*
  136.      * pc1 or pc2 may be the same as pc, so we must be a little careful
  137.      * about the order in which we store the results.
  138.      */
  139.     pc1->p1.x = midpoint(x0, x1);
  140.     pc1->p1.y = midpoint(y0, y1);
  141.     pc2->p2.x = midpoint(x2, x3);
  142.     pc2->p2.y = midpoint(y2, y3);
  143.     pc1->p2.x = midpoint(pc1->p1.x, x12);
  144.     pc1->p2.y = midpoint(pc1->p1.y, y12);
  145.     pc2->p1.x = midpoint(x12, pc2->p2.x);
  146.     pc2->p1.y = midpoint(y12, pc2->p2.y);
  147.     if (pc2 != pc)
  148.     pc2->pt.x = pc->pt.x,
  149.         pc2->pt.y = pc->pt.y;
  150.     pc1->pt.x = midpoint(pc1->p2.x, pc2->p1.x);
  151.     pc1->pt.y = midpoint(pc1->p2.y, pc2->p1.y);
  152. #undef midpoint
  153. }
  154.  
  155. /*
  156.  * Flatten a segment of the path by repeated sampling.
  157.  * 2^k is the number of lines to produce (i.e., the number of points - 1,
  158.  * including the endpoints); we require k >= 1.
  159.  * If k or any of the coefficient values are too large,
  160.  * use recursive subdivision to whittle them down.
  161.  */
  162. int
  163. gx_flatten_sample(gx_path * ppath, int k, curve_segment * pc,
  164.           segment_notes notes)
  165. {
  166.     fixed x0, y0;
  167.  
  168.     /* x1 ... y3 were defined above */
  169.     fixed cx, bx, ax, cy, by, ay;
  170.     fixed ptx, pty;
  171.     fixed x, y;
  172.  
  173.     /*
  174.      * We can compute successive values by finite differences,
  175.      * using the formulas:
  176.      x(t) =
  177.      a*t^3 + b*t^2 + c*t + d =>
  178.      dx(t) = x(t+e)-x(t) =
  179.      a*(3*t^2*e + 3*t*e^2 + e^3) + b*(2*t*e + e^2) + c*e =
  180.      (3*a*e)*t^2 + (3*a*e^2 + 2*b*e)*t + (a*e^3 + b*e^2 + c*e) =>
  181.      d2x(t) = dx(t+e)-dx(t) =
  182.      (3*a*e)*(2*t*e + e^2) + (3*a*e^2 + 2*b*e)*e =
  183.      (6*a*e^2)*t + (6*a*e^3 + 2*b*e^2) =>
  184.      d3x(t) = d2x(t+e)-d2x(t) =
  185.      6*a*e^3;
  186.      x(0) = d, dx(0) = (a*e^3 + b*e^2 + c*e),
  187.      d2x(0) = 6*a*e^3 + 2*b*e^2;
  188.      * In these formulas, e = 1/2^k; of course, there are separate
  189.      * computations for the x and y values.
  190.      *
  191.      * There is a tradeoff in doing the above computation in fixed
  192.      * point.  If we separate out the constant term (d) and require that
  193.      * all the other values fit in a long, then on a 32-bit machine with
  194.      * 12 bits of fraction in a fixed, k = 4 implies a maximum curve
  195.      * size of 128 pixels; anything larger requires subdividing the
  196.      * curve.  On the other hand, doing the computations in explicit
  197.      * double precision slows down the loop by a factor of 3 or so.  We
  198.      * found to our surprise that the latter is actually faster, because
  199.      * the additional subdivisions cost more than the slower loop.
  200.      *
  201.      * We represent each quantity as I+R/M, where I is an "integer" and
  202.      * the "remainder" R lies in the range 0 <= R < M=2^(3*k).  Note
  203.      * that R may temporarily exceed M; for this reason, we require that
  204.      * M have at least one free high-order bit.  To reduce the number of
  205.      * variables, we don't actually compute M, only M-1 (rmask).  */
  206.     uint i;
  207.     uint rmask;            /* M-1 */
  208.     fixed idx, idy, id2x, id2y, id3x, id3y;    /* I */
  209.     uint rx, ry, rdx, rdy, rd2x, rd2y, rd3x, rd3y;    /* R */
  210.     gs_fixed_point *ppt;
  211.  
  212. #define max_points 50        /* arbitrary */
  213.     gs_fixed_point points[max_points + 1];
  214.  
  215.   top:x0 = ppath->position.x;
  216.     y0 = ppath->position.y;
  217. #ifdef DEBUG
  218.     if (gs_debug_c('3')) {
  219.     dlprintf4("[3]x0=%f y0=%f x1=%f y1=%f\n",
  220.           fixed2float(x0), fixed2float(y0),
  221.           fixed2float(x1), fixed2float(y1));
  222.     dlprintf5("   x2=%f y2=%f x3=%f y3=%f  k=%d\n",
  223.           fixed2float(x2), fixed2float(y2),
  224.           fixed2float(x3), fixed2float(y3), k);
  225.     }
  226. #endif
  227.     {
  228.     fixed x01, x12, y01, y12;
  229.  
  230.     curve_points_to_coefficients(x0, x1, x2, x3, ax, bx, cx,
  231.                      x01, x12);
  232.     curve_points_to_coefficients(y0, y1, y2, y3, ay, by, cy,
  233.                      y01, y12);
  234.     }
  235.  
  236.     if_debug6('3', "[3]ax=%f bx=%f cx=%f\n   ay=%f by=%f cy=%f\n",
  237.           fixed2float(ax), fixed2float(bx), fixed2float(cx),
  238.           fixed2float(ay), fixed2float(by), fixed2float(cy));
  239. #define max_fast (max_fixed / 6)
  240. #define min_fast (-max_fast)
  241. #define in_range(v) (v < max_fast && v > min_fast)
  242.     if (k == 0) {        /* The curve is very short, or anomalous in some way. */
  243.     /* Just add a line and exit. */
  244.     return gx_path_add_line_notes(ppath, x3, y3, notes);
  245.     }
  246.     if (k <= k_sample_max &&
  247.     in_range(ax) && in_range(ay) &&
  248.     in_range(bx) && in_range(by) &&
  249.     in_range(cx) && in_range(cy)
  250.     ) {
  251.     x = x0, y = y0;
  252.     rx = ry = 0;
  253.     ppt = points;
  254.     /* Fast check for n == 3, a common special case */
  255.     /* for small characters. */
  256.     if (k == 1) {
  257. #define poly2(a,b,c)\
  258.   arith_rshift_1(arith_rshift_1(arith_rshift_1(a) + b) + c)
  259.         x += poly2(ax, bx, cx);
  260.         y += poly2(ay, by, cy);
  261. #undef poly2
  262.         if_debug2('3', "[3]dx=%f, dy=%f\n",
  263.               fixed2float(x - x0), fixed2float(y - y0));
  264.         if_debug3('3', "[3]%s x=%g, y=%g\n",
  265.               (((x ^ x0) | (y ^ y0)) & float2fixed(-0.5) ?
  266.                "add" : "skip"),
  267.               fixed2float(x), fixed2float(y));
  268.         if (((x ^ x0) | (y ^ y0)) & float2fixed(-0.5))
  269.         ppt->x = ptx = x,
  270.             ppt->y = pty = y,
  271.             ppt++;
  272.         goto last;
  273.     } else {
  274.         fixed bx2 = bx << 1, by2 = by << 1;
  275.         fixed ax6 = ((ax << 1) + ax) << 1, ay6 = ((ay << 1) + ay) << 1;
  276.  
  277. #define adjust_rem(r, q)\
  278.   if ( r > rmask ) q ++, r &= rmask
  279.         const int k2 = k << 1;
  280.         const int k3 = k2 + k;
  281.  
  282.         rmask = (1 << k3) - 1;
  283.         /* We can compute all the remainders as ints, */
  284.         /* because we know they don't exceed M. */
  285.         /* cx/y terms */
  286.         idx = arith_rshift(cx, k),
  287.         idy = arith_rshift(cy, k);
  288.         rdx = ((uint) cx << k2) & rmask,
  289.         rdy = ((uint) cy << k2) & rmask;
  290.         /* bx/y terms */
  291.         id2x = arith_rshift(bx2, k2),
  292.         id2y = arith_rshift(by2, k2);
  293.         rd2x = ((uint) bx2 << k) & rmask,
  294.         rd2y = ((uint) by2 << k) & rmask;
  295.         idx += arith_rshift_1(id2x),
  296.         idy += arith_rshift_1(id2y);
  297.         rdx += ((uint) bx << k) & rmask,
  298.         rdy += ((uint) by << k) & rmask;
  299.         adjust_rem(rdx, idx);
  300.         adjust_rem(rdy, idy);
  301.         /* ax/y terms */
  302.         idx += arith_rshift(ax, k3),
  303.         idy += arith_rshift(ay, k3);
  304.         rdx += (uint) ax & rmask,
  305.         rdy += (uint) ay & rmask;
  306.         adjust_rem(rdx, idx);
  307.         adjust_rem(rdy, idy);
  308.         id2x += id3x = arith_rshift(ax6, k3),
  309.         id2y += id3y = arith_rshift(ay6, k3);
  310.         rd2x += rd3x = (uint) ax6 & rmask,
  311.         rd2y += rd3y = (uint) ay6 & rmask;
  312.         adjust_rem(rd2x, id2x);
  313.         adjust_rem(rd2y, id2y);
  314. #undef adjust_rem
  315.     }
  316.     } else {            /*
  317.                  * Curve is too long.  Break into two pieces and recur.
  318.                  */
  319.     curve_segment cseg;
  320.     int code;
  321.  
  322.     k--;
  323.     split_curve_midpoint(x0, y0, pc, &cseg, pc);
  324.     code = gx_flatten_sample(ppath, k, &cseg, notes);
  325.     if (code < 0)
  326.         return code;
  327.     notes |= sn_not_first;
  328.     goto top;
  329.     }
  330.     if_debug1('2', "[2]sampling k=%d\n", k);
  331.     ptx = x0, pty = y0;
  332.     for (i = (1 << k) - 1;;) {
  333.     int code;
  334.  
  335. #ifdef DEBUG
  336.     if (gs_debug_c('3')) {
  337.         dlprintf4("[3]dx=%f+%d, dy=%f+%d\n",
  338.               fixed2float(idx), rdx,
  339.               fixed2float(idy), rdy);
  340.         dlprintf4("   d2x=%f+%d, d2y=%f+%d\n",
  341.               fixed2float(id2x), rd2x,
  342.               fixed2float(id2y), rd2y);
  343.         dlprintf4("   d3x=%f+%d, d3y=%f+%d\n",
  344.               fixed2float(id3x), rd3x,
  345.               fixed2float(id3y), rd3y);
  346.     }
  347. #endif
  348. #define accum(i, r, di, dr)\
  349.   if ( (r += dr) > rmask ) r &= rmask, i += di + 1;\
  350.   else i += di
  351.     accum(x, rx, idx, rdx);
  352.     accum(y, ry, idy, rdy);
  353.     if_debug3('3', "[3]%s x=%g, y=%g\n",
  354.           (((x ^ ptx) | (y ^ pty)) & float2fixed(-0.5) ?
  355.            "add" : "skip"),
  356.           fixed2float(x), fixed2float(y));
  357.     /*
  358.      * Skip very short segments -- those that lie entirely within
  359.      * a square half-pixel.  Also merge nearly collinear
  360.      * segments -- those where one coordinate of all three points
  361.      * (the two endpoints and the midpoint) lie within the same
  362.      * half-pixel and both coordinates are monotonic.
  363.      * Note that ptx/y, the midpoint, is the same as ppt[-1].x/y;
  364.      * the previous point is ppt[-2].x/y.
  365.      */
  366. #define coord_near(v, ptv)\
  367.   (!( ((v) ^ (ptv)) & float2fixed(-0.5) ))
  368. #define coords_in_order(v0, v1, v2)\
  369.   ( (((v1) - (v0)) ^ ((v2) - (v1))) >= 0 )
  370.     if (coord_near(x, ptx)) {    /* X coordinates are within a half-pixel. */
  371.         if (coord_near(y, pty))
  372.         goto skip;    /* short segment */
  373. #if MERGE_COLLINEAR_SEGMENTS
  374.         /* Check for collinear segments. */
  375.         if (ppt > points + 1 && coord_near(x, ppt[-2].x) &&
  376.         coords_in_order(ppt[-2].x, ptx, x) &&
  377.         coords_in_order(ppt[-2].y, pty, y)
  378.         )
  379.         --ppt;        /* remove middle point */
  380. #endif
  381.     } else if (coord_near(y, pty)) {    /* Y coordinates are within a half-pixel. */
  382. #if MERGE_COLLINEAR_SEGMENTS
  383.         /* Check for collinear segments. */
  384.         if (ppt > points + 1 && coord_near(y, ppt[-2].y) &&
  385.         coords_in_order(ppt[-2].x, ptx, x) &&
  386.         coords_in_order(ppt[-2].y, pty, y)
  387.         )
  388.         --ppt;        /* remove middle point */
  389. #endif
  390.     }
  391. #undef coord_near
  392. #undef coords_in_order
  393.     /* Add a line. */
  394.     if (ppt == &points[max_points]) {
  395.         if (notes & sn_not_first)
  396.         code = gx_path_add_lines_notes(ppath, points, max_points,
  397.                            notes);
  398.         else {
  399.         code = gx_path_add_line_notes(ppath, points[0].x,
  400.                           points[0].y, notes);
  401.         if (code < 0)
  402.             return code;
  403.         code = gx_path_add_lines_notes(ppath, points,
  404.                       max_points - 1, notes | sn_not_first);
  405.         }
  406.         if (code < 0)
  407.         return code;
  408.         ppt = points;
  409.         notes |= sn_not_first;
  410.     }
  411.     ppt->x = ptx = x;
  412.     ppt->y = pty = y;
  413.     ppt++;
  414.       skip:if (--i == 0)
  415.         break;        /* don't bother with last accum */
  416.     accum(idx, rdx, id2x, rd2x);
  417.     accum(id2x, rd2x, id3x, rd3x);
  418.     accum(idy, rdy, id2y, rd2y);
  419.     accum(id2y, rd2y, id3y, rd3y);
  420. #undef accum
  421.     }
  422.   last:if_debug2('3', "[3]last x=%g, y=%g\n",
  423.           fixed2float(x3), fixed2float(y3));
  424.     if (ppt > points) {
  425.     int count = ppt + 1 - points;
  426.     gs_fixed_point *pts = points;
  427.  
  428.     if (!(notes & sn_not_first)) {
  429.         int code = gx_path_add_line_notes(ppath,
  430.                           points[0].x, points[0].y,
  431.                           notes);
  432.  
  433.         if (code < 0)
  434.         return code;
  435.         ++pts, --count;
  436.         notes |= sn_not_first;
  437.     }
  438.     ppt->x = x3, ppt->y = y3;
  439.     return gx_path_add_lines_notes(ppath, pts, count, notes);
  440.     }
  441.     return gx_path_add_line_notes(ppath, x3, y3, notes);
  442. }
  443.  
  444. #undef x1
  445. #undef y1
  446. #undef x2
  447. #undef y2
  448. #undef x3
  449. #undef y3
  450.