home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXDRAW.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  16KB  |  489 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gxdraw.c */
  20. /* Primitive drawing routines for Ghostscript imaging library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzht.h"
  29. #include "gzdraw.h"            /* prototypes */
  30.  
  31. /* Define the standard device color types. */
  32. const gx_device_color_procs
  33.   gx_dc_none = { gx_dc_no_load, gx_dc_no_fill_rectangle },
  34.   gx_dc_pure = { gx_dc_pure_load, gx_dc_pure_fill_rectangle },
  35.   gx_dc_ht_binary = { gx_dc_ht_binary_load, gx_dc_ht_binary_fill_rectangle },
  36.   gx_dc_ht_colored = { gx_dc_ht_colored_load, gx_dc_ht_colored_fill_rectangle };
  37.  
  38. /* Null color */
  39. int
  40. gx_dc_no_load(gx_device_color *pdevc, const gs_state *pgs)
  41. {    return 0;
  42. }
  43. int
  44. gx_dc_no_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  45.   int w, int h, gx_device *dev, const gs_state *pgs)
  46. {    return 0;
  47. }
  48.  
  49. /* Pure color */
  50. int
  51. gx_dc_pure_load(gx_device_color *pdevc, const gs_state *pgs)
  52. {    return 0;
  53. }
  54. int
  55. gx_dc_pure_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  56.   int w, int h, gx_device *dev, const gs_state *pgs)
  57. {    return (*dev_proc(dev, fill_rectangle))(dev, x, y, w, h,
  58.                         pdevc->colors.pure);
  59. }
  60.  
  61. /* Binary halftone */
  62. int
  63. gx_dc_ht_binary_fill_rectangle(const gx_device_color *pdevc, int x, int y,
  64.   int w, int h, gx_device *dev, const gs_state *pgs)
  65. {    const gx_ht_order *porder = &pgs->dev_ht->order;
  66.     return (*dev_proc(dev, tile_rectangle))(dev,
  67.                 pdevc->colors.binary.b_tile,
  68.                 x, y, w, h, pdevc->colors.binary.color[0],
  69.                 pdevc->colors.binary.color[1],
  70.                 porder->phase.x, porder->phase.y);
  71. }
  72.  
  73. /*
  74.  * Auxiliary procedures for computing a * b / c and a * b % c
  75.  * when a, b, and c are all non-negative,
  76.  * b < c, and a * b exceeds (or might exceed) the capacity of a long.
  77.  * It's really annoying that C doesn't provide any way to get at
  78.  * the double-length multiply/divide instructions that
  79.  * the machine undoubtedly provides....
  80.  *
  81.  * Note that these routines are exported for the benefit of gxfill.c.
  82.  */
  83.  
  84. fixed
  85. fixed_mult_quo(fixed a, fixed b, fixed c)
  86. {    return (fixed)floor((double)a * b / c);
  87. }
  88. fixed
  89. fixed_mult_rem(fixed a, fixed b, fixed c)
  90. {    double prod = (double)a * b;
  91.     return (fixed)(prod - floor(prod / c) * c);
  92. }
  93.  
  94. /* Fill a trapezoid.  Requires: wt >= 0, wb >= 0, h >= 0. */
  95. /* Note that the arguments are fixeds, not ints! */
  96. /* This is derived from Paul Haeberli's floating point algorithm. */
  97. typedef struct trap_line_s {
  98.     int di; fixed df;        /* dx/dy ratio */
  99.     fixed ldi, ldf;            /* increment per scan line */
  100.     fixed x, xf;            /* current value */
  101. } trap_line;
  102. int
  103. gz_fill_trapezoid_fixed(fixed fx0, fixed fw0, fixed fy0,
  104.   fixed fx1, fixed fw1, fixed fh, bool swap_axes,
  105.   const gx_device_color *pdevc, const gs_state *pgs)
  106. {    const fixed ymin = fixed_rounded(fy0) + fixed_half;
  107.     const fixed ymax = fixed_rounded(fy0 + fh);
  108.     int iy = fixed2int_var(ymin);
  109.     const int iy1 = fixed2int_var(ymax);
  110.     if ( iy >= iy1 ) return 0;    /* no scan lines to sample */
  111.    {    trap_line l, r;
  112.     int rxl, rxr, ry;
  113.     const fixed dxl = fx1 - fx0;
  114.     const fixed dxr = dxl + fw1 - fw0;
  115.     const fixed yline = ymin - fy0;    /* partial pixel offset to */
  116.                     /* first line to sample */
  117.     int fill_direct = color_is_pure(pdevc);
  118.     gx_color_index cindex;
  119.     gx_device *dev;
  120.     dev_proc_fill_rectangle((*fill_rect));
  121.     int code;
  122.     
  123.     if_debug2('z', "[z]y=[%d,%d]\n", iy, iy1);
  124.  
  125.     if ( fill_direct )
  126.       cindex = pdevc->colors.pure,
  127.       dev = gs_currentdevice_inline(pgs),
  128.       fill_rect = dev_proc(dev, fill_rectangle);
  129.     return_if_interrupt();
  130.     r.x = (l.x = fx0 + fixed_half) + fw0;
  131.     ry = iy;
  132.  
  133.     /* If the rounded X values are the same on both sides, */
  134.     /* we can save ourselves a *lot* of work. */
  135.     if ( fixed_floor(l.x) == fixed_rounded(fx1) &&
  136.          fixed_floor(r.x) == fixed_rounded(fx1 + fw1)
  137.        )
  138.     {    rxl = fixed2int_var(l.x);
  139.         rxr = fixed2int_var(r.x);
  140.         iy = iy1;
  141.         if_debug2('z', "[z]rectangle, x=[%d,%d]\n", rxl, rxr);
  142.         goto last;
  143.     }
  144.  
  145. #define fill_trap_rect(x,y,w,h)\
  146.   (fill_direct ?\
  147.     (swap_axes ? (*fill_rect)(dev, y, x, h, w, cindex) :\
  148.      (*fill_rect)(dev, x, y, w, h, cindex)) :\
  149.    swap_axes ? gx_fill_rectangle(y, x, h, w, pdevc, pgs) :\
  150.    gx_fill_rectangle(x, y, w, h, pdevc, pgs))
  151.  
  152.     /* Compute the dx/dy ratios. */
  153.     /* dx# = dx#i + (dx#f / fh). */
  154. #define compute_dx(tl, d)\
  155.   if ( d >= 0 )\
  156.    { if ( d < fh ) tl.di = 0, tl.df = d;\
  157.      else tl.di = (int)(d / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  158.    }\
  159.   else\
  160.    { if ( (tl.df = d + fh) >= 0    /* d >= -fh */ ) tl.di = -1, tl.x -= yline;\
  161.      else tl.di = (int)-((fh - 1 - d) / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  162.    }
  163.  
  164.     /* Compute the x offsets at the first scan line to sample. */
  165.     /* We need to be careful in computing yline * dx#f {/,%} fh */
  166.     /* because the multiplication may overflow.  We know that */
  167.     /* all the quantities involved are non-negative, and that */
  168.     /* yline is less than 1 (as a fixed, of course); this gives us */
  169.     /* a cheap conservative check for overflow in the multiplication. */
  170. #define ymult_limit (max_fixed / fixed_1)
  171. #define ymult_quo(yl, dxxf)\
  172.   (dxxf < ymult_limit ? yl * dxxf / fh : fixed_mult_quo(yl, dxxf, fh))
  173. #define ymult_rem(yl, dxxf)\
  174.   (dxxf < ymult_limit ? yl * dxxf % fh : fixed_mult_rem(yl, dxxf, fh))
  175.  
  176.     /* It's worth checking for dxl == dxr, since this is the case */
  177.     /* for parallelograms (including stroked lines). */
  178.     compute_dx(l, dxl);
  179.     if ( dxr == dxl )
  180.        {    fixed fx = ymult_quo(yline, l.df);
  181.         l.x += fx;
  182.         if ( l.di == 0 )
  183.             r.di = 0, r.df = l.df;
  184.         else            /* too hard to do adjustments right */
  185.             compute_dx(r, dxr);
  186.         r.x += fx;
  187.        }
  188.     else
  189.        {    l.x += ymult_quo(yline, l.df);
  190.         compute_dx(r, dxr);
  191.         r.x += ymult_quo(yline, r.df);
  192.        }
  193.     rxl = fixed2int_var(l.x);
  194.     rxr = fixed2int_var(r.x);
  195.  
  196.     /* Compute one line's worth of dx/dy. */
  197.     /* dx# * fixed_1 = ld#i + (ld#f / fh). */
  198.     /* We don't have to bother with this if */
  199.     /* we're only sampling a single scan line. */
  200.     if ( iy1 - iy == 1 )
  201.        {    iy++;
  202.         goto last;
  203.        }
  204. #define compute_ldx(tl)\
  205.   if ( tl.df < ymult_limit )\
  206.     tl.ldi = int2fixed(tl.di) + int2fixed(tl.df) / fh,\
  207.     tl.ldf = int2fixed(tl.df) % fh,\
  208.     tl.xf = yline * tl.df % fh - fh;\
  209.   else\
  210.     tl.ldi = int2fixed(tl.di) + fixed_mult_quo(fixed_1, tl.df, fh),\
  211.     tl.ldf = fixed_mult_rem(fixed_1, tl.df, fh),\
  212.     tl.xf = fixed_mult_rem(yline, tl.df, fh) - fh
  213.     compute_ldx(l);
  214.     if ( dxr == dxl )
  215.         r.ldi = l.ldi, r.ldf = l.ldf, r.xf = l.xf;
  216.     else
  217.        {    compute_ldx(r);
  218.        }
  219. #undef compute_ldx
  220.  
  221.     while ( ++iy != iy1 )
  222.        {    int ixl, ixr;
  223. #define step_line(tl)\
  224.   tl.x += tl.ldi;\
  225.   if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= fh, tl.x++;
  226.         step_line(l);
  227.         step_line(r);
  228. #undef step_line
  229.         ixl = fixed2int_var(l.x);
  230.         ixr = fixed2int_var(r.x);
  231.         if ( ixl != rxl || ixr != rxr )
  232.            {    code = fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  233.             if ( code < 0 ) goto xit;
  234.             rxl = ixl, rxr = ixr, ry = iy;
  235.            }    
  236.        }
  237. last:    code = fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  238. xit:    if ( code < 0 && fill_direct )
  239.         return_error(code);
  240.     return_if_interrupt();
  241.     return code;
  242.    }
  243. }
  244.  
  245. /* Fill a parallelogram whose points are p, p+a, p+b, and p+a+b. */
  246. /* We should swap axes to get best accuracy, but we don't. */
  247. int
  248. gz_fill_pgram_fixed(fixed px, fixed py, fixed ax, fixed ay,
  249.   fixed bx, fixed by, const gx_device_color *pdevc, const gs_state *pgs)
  250. {    fixed t;
  251.     fixed dy, qx, dx, wx, pax, qax;
  252.     int code;
  253.     /* Ensure ay >= 0, by >= 0. */
  254.     if ( ay < 0 ) px += ax, py += ay, ax = -ax, ay = -ay;
  255.     if ( by < 0 ) px += bx, py += by, bx = -bx, by = -by;
  256.     qx = px + ax + bx;
  257.     /* Make a special fast check for rectangles. */
  258.     if ( (ay | bx) == 0 || (by | ax) == 0 )
  259.     {    int rx = fixed2int_var_rounded(px);
  260.         int ry = fixed2int_var_rounded(py);
  261.         /* Exactly one of (ax,bx) and one of (ay,by) is non-zero. */
  262.         int w = fixed2int_var_rounded(qx) - rx;
  263.         if ( w < 0 ) rx += w, w = -w;
  264.         return gx_fill_rectangle(rx, ry, w,
  265.                      fixed2int_rounded(py + ay + by) - ry,
  266.                      pdevc, pgs);
  267.     }
  268.     /* Not a rectangle.  Ensure ay <= by. */
  269. #define swap(r, s) (t = r, r = s, s = t)
  270.     if ( ay > by ) swap(ax, bx), swap(ay, by);
  271.     /* Compute the distance from p to the point on the line (p, p+b) */
  272.     /* whose Y coordinate is equal to ay. */
  273.     dx = ( ((bx < 0 ? -bx : bx) | ay) < 1L << (size_of(fixed) * 4 - 1) ?
  274.         bx * ay / by :
  275.         (fixed)((double)bx * ay / by)
  276.          );
  277.     if ( dx < ax ) pax = px + dx, qax = qx - ax, wx = ax - dx;
  278.     else pax = px + ax, qax = qx - dx, wx = dx - ax;
  279. #define rounded_same(p, a) /* fixed_rounded(p) == fixed_rounded(p + a) */\
  280.   (fixed_fraction((p) + fixed_half) + (a) < fixed_1) /* know a >= 0 */
  281.     if ( !rounded_same(py, ay) )
  282.        {    code = gz_fill_trapezoid_fixed(px, fixed_0, py, pax, wx, ay,
  283.                            0, pdevc, pgs);
  284.         if ( code < 0 ) return code;
  285.        }
  286.     py += ay;
  287.     dy = by - ay;
  288.     if ( !rounded_same(py, dy) )
  289.        {    code = gz_fill_trapezoid_fixed(pax, wx, py, qax, wx, dy,
  290.                            0, pdevc, pgs);
  291.         if ( code < 0 ) return code;
  292.        }
  293.     py += dy;
  294.     if ( !rounded_same(py, ay) )
  295.         return gz_fill_trapezoid_fixed(qax, wx, py, qx, fixed_0, ay,
  296.                            0, pdevc, pgs);
  297. #undef rounded_same
  298. #undef swap
  299.     return 0;
  300. }
  301.  
  302. /* Default implementation of tile_rectangle */
  303. int
  304. gx_default_tile_rectangle(gx_device *dev, register const gx_tile_bitmap *tile,
  305.   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  306.   int px, int py)
  307. {    /* Fill the rectangle in chunks */
  308.     int width = tile->size.x;
  309.     int height = tile->size.y;
  310.     int raster = tile->raster;
  311.     int rwidth = tile->rep_width;
  312.     int irx = ((rwidth & (rwidth - 1)) == 0 ?    /* power of 2 */
  313.         (x + px) & (rwidth - 1) :
  314.         (x + px) % rwidth);
  315.     int ry = (y + py) % tile->rep_height;
  316.     int icw = width - irx;
  317.     int ch = height - ry;
  318.     byte *row = tile->data + ry * raster;
  319. #define d_proc_mono dev_proc(dev, copy_mono)
  320.     dev_proc_copy_mono((*proc_mono));
  321. #define d_proc_color dev_proc(dev, copy_color)
  322.     dev_proc_copy_color((*proc_color));
  323. #define d_color_halftone\
  324.         (color0 == gx_no_color_index && color1 == gx_no_color_index)
  325.     int color_halftone;
  326. #define get_color_info()\
  327.   if ( (color_halftone = d_color_halftone) ) proc_color = d_proc_color;\
  328.   else proc_mono = d_proc_mono
  329.     int code;
  330. #ifdef DEBUG
  331. if ( gs_debug_c('t') )
  332.    {    int ptx, pty;
  333.     const byte *ptp = tile->data;
  334.     dprintf3("[t]tile %dx%d raster=%d;",
  335.         tile->size.x, tile->size.y, tile->raster);
  336.     dprintf6(" x,y=%d,%d w,h=%d,%d p=%d,%d\n",
  337.         x, y, w, h, px, py);
  338.     for ( pty = 0; pty < tile->size.y; pty++ )
  339.        {    dprintf("   ");
  340.         for ( ptx = 0; ptx < tile->raster; ptx++ )
  341.             dprintf1("%3x", *ptp++);
  342.        }
  343.     dputc('\n');
  344.    }
  345. #endif
  346. /****** SHOULD ALSO PASS id IF COPYING A FULL TILE ******/
  347. #define real_copy_tile(srcx, tx, ty, tw, th)\
  348.   code =\
  349.     (color_halftone ?\
  350.      (*proc_color)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th) :\
  351.      (*proc_mono)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th, color0, color1));\
  352.   if ( code < 0 ) return_error(code);\
  353.   return_if_interrupt()
  354. #ifdef DEBUG
  355. #define copy_tile(sx, tx, ty, tw, th)\
  356.   if ( gs_debug_c('t') )\
  357.     dprintf5("   copy sx=%d x=%d y=%d w=%d h=%d\n",\
  358.          sx, tx, ty, tw, th);\
  359.   real_copy_tile(sx, tx, ty, tw, th)
  360. #else
  361. #define copy_tile(sx, tx, ty, tw, th)\
  362.   real_copy_tile(sx, tx, ty, tw, th)
  363. #endif
  364.     if ( icw >= w )
  365.        {    /* Narrow operation */
  366.         int ey, fey, cy;
  367.         if ( ch >= h )
  368.            {    /* Just one (partial) tile to transfer. */
  369. #define color_halftone d_color_halftone
  370. #define proc_color d_proc_color
  371. #define proc_mono d_proc_mono
  372.             copy_tile(irx, x, y, w, h);
  373. #undef proc_mono
  374. #undef proc_color
  375. #undef color_halftone
  376.             return 0;
  377.            }
  378.         get_color_info();
  379.         ey = y + h;
  380.         fey = ey - height;
  381.         copy_tile(irx, x, y, w, ch);
  382.         cy = y + ch;
  383.         row = tile->data;
  384.         do
  385.            {    ch = (cy > fey ? ey - cy : height);
  386.             copy_tile(irx, x, cy, w, ch);
  387.            }
  388.         while ( (cy += ch) < ey );
  389.         return 0;
  390.        }
  391.     get_color_info();
  392.     if ( ch >= h )
  393.        {    /* Shallow operation */
  394.         int ex = x + w;
  395.         int fex = ex - width;
  396.         int cx = x + icw;
  397.         copy_tile(irx, x, y, icw, h);
  398.         while ( cx <= fex )
  399.            {    copy_tile(0, cx, y, width, h);
  400.             cx += width;
  401.            }
  402.         if ( cx < ex )
  403.            {    copy_tile(0, cx, y, ex - cx, h);
  404.            }
  405.        }
  406.     else
  407.        {    /* Full operation */
  408.         int ex = x + w, ey = y + h;
  409.         int fex = ex - width, fey = ey - height;
  410.         int cx, cy;
  411.         for ( cy = y; ; )
  412.            {    copy_tile(irx, x, cy, icw, ch);
  413.             cx = x + icw;
  414.             while ( cx <= fex )
  415.                {    copy_tile(0, cx, cy, width, ch);
  416.                 cx += width;
  417.                }
  418.             if ( cx < ex )
  419.                {    copy_tile(0, cx, cy, ex - cx, ch);
  420.                }
  421.             if ( (cy += ch) >= ey ) break;
  422.             ch = (cy > fey ? ey - cy : height);
  423.             row = tile->data;
  424.            }
  425.        }
  426. #undef copy_tile
  427. #undef real_copy_tile
  428.     return 0;
  429. }
  430.  
  431. /* Draw a one-pixel-wide line. */
  432. int
  433. gz_draw_line_fixed(fixed ixf, fixed iyf, fixed itoxf, fixed itoyf,
  434.   const gx_device_color *pdevc, const gs_state *pgs)
  435. {    int ix = fixed2int_var(ixf);
  436.     int iy = fixed2int_var(iyf);
  437.     int itox = fixed2int_var(itoxf);
  438.     int itoy = fixed2int_var(itoyf);
  439.     gx_device *dev;
  440.     return_if_interrupt();
  441.     if ( itoy == iy )        /* horizontal line */
  442.       { return (ix <= itox ?
  443.             gx_fill_rectangle(ix, iy, itox - ix + 1, 1, pdevc, pgs) :
  444.             gx_fill_rectangle(itox, iy, ix - itox + 1, 1, pdevc, pgs)
  445.             );
  446.       }
  447.     if ( itox == ix )        /* vertical line */
  448.       { return (iy <= itoy ?
  449.             gx_fill_rectangle(ix, iy, 1, itoy - iy + 1, pdevc, pgs) :
  450.             gx_fill_rectangle(ix, itoy, 1, iy - itoy + 1, pdevc, pgs)
  451.             );
  452.       }
  453.     if ( color_is_pure(pdevc) &&
  454.         (dev = gs_currentdevice_inline(pgs),
  455.          (*dev_proc(dev, draw_line))(dev, ix, iy, itox, itoy,
  456.                      pdevc->colors.pure)) >= 0
  457.        )
  458.       return 0;
  459.     { fixed h = itoyf - iyf;
  460.       fixed w = itoxf - ixf;
  461.       fixed tf;
  462. #define fswap(a, b) tf = a, a = b, b = tf
  463.       if ( (w < 0 ? -w : w) <= (h < 0 ? -h : h) )
  464.         { if ( h < 0 )
  465.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  466.         h = -h;
  467.           return gz_fill_trapezoid_fixed(ixf - fixed_half, fixed_1, iyf,
  468.                          itoxf - fixed_half, fixed_1, h,
  469.                          0, pdevc, pgs);
  470.         }
  471.       else
  472.         { if ( w < 0 )
  473.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  474.         w = -w;
  475.           return gz_fill_trapezoid_fixed(iyf - fixed_half, fixed_1, ixf,
  476.                          itoyf - fixed_half, fixed_1, w,
  477.                          1, pdevc, pgs);
  478.         }
  479. #undef fswap
  480.     }
  481. }
  482.  
  483. /* A stub to force use of the standard procedure. */
  484. int
  485. gx_default_draw_line(gx_device *dev,
  486.   int x0, int y0, int x1, int y1, gx_color_index color)
  487. {    return -1;
  488. }
  489.