home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GXDRAW.C < prev    next >
C/C++ Source or Header  |  1993-12-20  |  18KB  |  481 lines

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