home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GXDRAW.C < prev    next >
C/C++ Source or Header  |  1992-06-20  |  14KB  |  438 lines

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