home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / gxdraw < prev    next >
Encoding:
Text File  |  1991-10-26  |  11.5 KB  |  371 lines

  1. /* Copyright (C) 1989, 1990, 1991 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 "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gxbitmap.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, gx_device_color *pdevc,
  34.   gs_state *pgs)
  35. {    gx_color_index darker = pdevc->color1;
  36.     gx_color_index lighter;
  37.     gx_device *dev = pgs->device->info;
  38.     gx_bitmap *tile;
  39.     int px, py, code;
  40. #ifdef DEBUG
  41. if ( gs_debug['q'] )
  42.     dprintf7("[q]x=%d y=%d w=%d h=%d  c1=%ld c2=%ld htl=%d\n",
  43.          x, y, w, h, darker, (long)pdevc->color2,
  44.          (long)pdevc->halftone_level);
  45. #endif
  46.     if ( color_is_pure(pdevc) )    /* no halftoning */
  47.        {    return (*dev->procs->fill_rectangle)(dev, x, y, w, h, darker);
  48.        }
  49.     px = pgs->phase_mod.x;
  50.     py = pgs->phase_mod.y;
  51.     lighter = pdevc->color2;
  52.     tile = pdevc->tile;
  53.     /* See if the entire transfer falls within a single tile. */
  54.     /* This is worth a quick check, because tiling is slow. */
  55.     if ( w <= tile->width && h <= tile->height )
  56.        {    int xmod = (x + px) % tile->width, ymod;
  57.         if ( xmod + w <= tile->width &&
  58.              (ymod = (y + py) % tile->height) + h <= tile->height
  59.            )
  60.            {    /* Just do a copy. */
  61.             int raster = tile->raster;
  62.             byte *tdata = tile->data + ymod * raster;
  63.             return (color_is_color_halftone(pdevc) ?
  64.                 (*dev->procs->copy_color)(dev, tdata,
  65.                     xmod, raster, x, y, w, h) :
  66.                 (*dev->procs->copy_mono)(dev, tdata,
  67.                     xmod, raster, x, y, w, h,
  68.                     darker, lighter));
  69.            }
  70.        }
  71.     /* Try to tile the rectangle primitively; */
  72.     /* if this fails, use the default implementation. */
  73.     if ( color_is_color_halftone(pdevc) )
  74.         darker = lighter = gx_no_color_index;
  75.     code = (*dev->procs->tile_rectangle)(dev, tile,
  76.         x, y, w, h, darker, lighter, px, py);
  77.     if ( code < 0 )
  78.        {    /* Use the default implementation */
  79.         code = gx_default_tile_rectangle(dev, tile,
  80.             x, y, w, h, darker, lighter, px, py);
  81.        }
  82.     return code;
  83. }
  84.  
  85. /*
  86.  * Auxiliary procedures for computing a * b / c and a * b % c
  87.  * when a, b, and c are all non-negative,
  88.  * b < c, and a * b exceeds (or might exceed) the capacity of a long.
  89.  * It's really annoying that C doesn't provide any way to get at
  90.  * the double-length multiply/divide instructions that
  91.  * the machine undoubtedly provides....
  92.  *
  93.  * Note that these routines are exported for the benefit of gxfill.c.
  94.  */
  95.  
  96. fixed
  97. fixed_mult_quo(fixed a, fixed b, fixed c)
  98. {    return (fixed)floor((double)a * b / c);
  99. }
  100. fixed
  101. fixed_mult_rem(fixed a, fixed b, fixed c)
  102. {    double prod = (double)a * b;
  103.     return (fixed)(prod - floor(prod / c) * c);
  104. }
  105.  
  106. /* Fill a trapezoid.  Requires: wt >= 0, wb >= 0, h >= 0. */
  107. /* Note that the arguments are fixeds, not ints! */
  108. /* This is derived from Paul Haeberli's floating point algorithm. */
  109. typedef struct trap_line_s {
  110.     int di; fixed df;        /* dx/dy ratio */
  111.     fixed ldi, ldf;            /* increment per scan line */
  112.     fixed x, xf;            /* current value */
  113. } trap_line;
  114. int
  115. gz_fill_trapezoid_fixed(fixed fx0, fixed fw0, fixed fy0,
  116.   fixed fx1, fixed fw1, fixed fh, int swap_axes,
  117.   gx_device_color *pdevc, gs_state *pgs)
  118. {    const fixed ymin = fixed_rounded(fy0) + float2fixed(0.5);
  119.     const fixed ymax = fixed_rounded(fy0 + fh);
  120.     int iy = fixed2int_var(ymin);
  121.     const int iy1 = fixed2int_var(ymax);
  122.     if ( iy >= iy1 ) return 0;    /* no scan lines to sample */
  123.    {    trap_line l, r;
  124.     int rxl, rxr, ry;
  125.     const fixed dxl = fx1 - fx0;
  126.     const fixed dxr = dxl + fw1 - fw0;
  127.     const fixed yline = ymin - fy0;    /* partial pixel offset to */
  128.                     /* first line to sample */
  129.     int fill_direct = color_is_pure(pdevc);
  130.     gx_color_index cindex;
  131.     gx_device *dev;
  132.     dev_proc_fill_rectangle((*fill_rect));
  133.  
  134.     r.x = (l.x = fx0 + float2fixed(0.5)) + fw0;
  135.     if ( fill_direct )
  136.       cindex = pdevc->color1,
  137.       dev = pgs->device->info,
  138.       fill_rect = dev->procs->fill_rectangle;
  139. #define fill_trap_rect(x,y,w,h)\
  140.   (fill_direct ?\
  141.     (swap_axes ? (*fill_rect)(dev, y, x, h, w, cindex) :\
  142.      (*fill_rect)(dev, x, y, w, h, cindex)) :\
  143.    swap_axes ? gz_fill_rectangle(y, x, h, w, pdevc, pgs) :\
  144.    gz_fill_rectangle(x, y, w, h, pdevc, pgs))
  145.  
  146.     /* Compute the dx/dy ratios. */
  147.     /* dx# = dx#i + (dx#f / fh). */
  148. #define compute_dx(tl, d)\
  149.   if ( d >= 0 )\
  150.    { if ( d < fh ) tl.di = 0, tl.df = d;\
  151.      else tl.di = (int)(d / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  152.    }\
  153.   else\
  154.    { if ( (tl.df = d + fh) >= 0 /* d >= -fh */ ) tl.di = -1, tl.x -= yline;\
  155.      else tl.di = (int)-((fh - 1 - d) / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  156.    }
  157.     compute_dx(l, dxl);
  158.     compute_dx(r, dxr);
  159.  
  160.     /* We need to be careful in computing yline * dx#f {/,%} fh */
  161.     /* because the multiplication may overflow.  We know that */
  162.     /* all the quantities involved are non-negative, and that */
  163.     /* yline is less than 1 (as a fixed, of course); this gives us */
  164.     /* a cheap conservative check for overflow in the multiplication. */
  165. #define ymult_limit (max_fixed / int2fixed(1))
  166. #define ymult_quo(yl, dxxf)\
  167.   (dxxf < ymult_limit ? yl * dxxf / fh : fixed_mult_quo(yl, dxxf, fh))
  168. #define ymult_rem(yl, dxxf)\
  169.   (dxxf < ymult_limit ? yl * dxxf % fh : fixed_mult_rem(yl, dxxf, fh))
  170.  
  171.     /* Compute the x offsets at the first scan line to sample. */
  172.     l.x += ymult_quo(yline, l.df);
  173.     r.x += ymult_quo(yline, r.df);
  174.     rxl = fixed2int_var(l.x);
  175.     rxr = fixed2int_var(r.x);
  176.     ry = iy;
  177.  
  178.     /* Compute one line's worth of dx/dy. */
  179.     /* dx# * int2fixed(1) = ld#i + (ld#f / fh). */
  180.     /* We don't have to bother with this if */
  181.     /* we're only sampling a single scan line. */
  182.     if ( iy1 - iy == 1 )
  183.        {    iy++;
  184.         goto last;
  185.        }
  186. #define compute_ldx(tl)\
  187.   if ( tl.df < ymult_limit )\
  188.     tl.ldi = int2fixed(tl.di) + int2fixed(tl.df) / fh,\
  189.     tl.ldf = int2fixed(tl.df) % fh,\
  190.     tl.xf = yline * tl.df % fh - fh;\
  191.   else\
  192.     tl.ldi = int2fixed(tl.di) + fixed_mult_quo(int2fixed(1), tl.df, fh),\
  193.     tl.ldf = fixed_mult_rem(int2fixed(1), tl.df, fh),\
  194.     tl.xf = fixed_mult_rem(yline, tl.df, fh) - fh
  195.     compute_ldx(l);
  196.     compute_ldx(r);
  197. #undef compute_ldx
  198.  
  199.     while ( ++iy != iy1 )
  200.        {    int ixl, ixr;
  201. #define step_line(tl)\
  202.   tl.x += tl.ldi;\
  203.   if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= fh, tl.x++;
  204.         step_line(l);
  205.         step_line(r);
  206. #undef step_line
  207.         ixl = fixed2int_var(l.x);
  208.         ixr = fixed2int_var(r.x);
  209.         if ( ixl != rxl || ixr != rxr )
  210.            {    int code = fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  211.             if ( code < 0 ) return code;
  212.             rxl = ixl, rxr = ixr, ry = iy;
  213.            }    
  214.        }
  215. last:    return fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  216.    }
  217. }
  218.  
  219. /* Default implementation of tile_rectangle */
  220. int
  221. gx_default_tile_rectangle(gx_device *dev, register gx_bitmap *tile,
  222.   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  223.   int px, int py)
  224. {    /* Fill the rectangle in chunks */
  225.     int width = tile->width;
  226.     int height = tile->height;
  227.     int raster = tile->raster;
  228.     int ex = x + w, ey = y + h;
  229.     int fex = ex - width, fey = ey - height;
  230.     int irx = ((width & (width - 1)) == 0 ?    /* power of 2 */
  231.         (x + px) & (width - 1) :
  232.         (x + px) % width);
  233.     int ry = (y + py) % height;
  234.     int icw = width - irx;
  235.     int ch = height - ry;
  236.     byte *row = tile->data + ry * raster;
  237.     int (*proc_mono)(P10(gx_device *, byte *, int, int,
  238.         int, int, int, int, gx_color_index, gx_color_index));
  239.     int (*proc_color)(P8(gx_device *, byte *, int, int,
  240.         int, int, int, int));
  241.     int color_halftone =
  242.         (color0 == gx_no_color_index && color1 == gx_no_color_index);
  243.     int cx, cy;
  244.     int code;
  245.     if ( color_halftone )
  246.         proc_color = dev->procs->copy_color;
  247.     else
  248.         proc_mono = dev->procs->copy_mono;
  249. #ifdef DEBUG
  250. if ( gs_debug['t'] )
  251.    {    int ptx, pty;
  252.     byte *ptp = tile->data;
  253.     dprintf3("[t]tile %dx%d raster=%d;",
  254.         tile->width, tile->height, tile->raster);
  255.     dprintf6(" x,y=%d,%d w,h=%d,%d p=%d,%d\n",
  256.         x, y, w, h, px, py);
  257.     for ( pty = 0; pty < tile->height; pty++ )
  258.        {    dprintf("   ");
  259.         for ( ptx = 0; ptx < tile->raster; ptx++ )
  260.             dprintf1("%3x", *ptp++);
  261.        }
  262.     dputc('\n');
  263.    }
  264. #endif
  265.     if ( icw > w ) icw = w;
  266.     if ( ch > h ) ch = h;
  267. #define real_copy_tile(sourcex, tx, tw)\
  268.   code =\
  269.     (color_halftone ?\
  270.      (*proc_color)(dev, row, sourcex, raster, tx, cy, tw, ch) :\
  271.      (*proc_mono)(dev, row, sourcex, raster, tx, cy, tw, ch, color0, color1));\
  272.   if ( code < 0 ) return code
  273. #ifdef DEBUG
  274. #define copy_tile(sx, tx, tw)\
  275.   if ( gs_debug['t'] )\
  276.     dprintf5("   copy sx=%d x=%d y=%d w=%d h=%d\n",\
  277.          sx, tx, cy, tw, ch);\
  278.   real_copy_tile(sx, tx, tw)
  279. #else
  280. #define copy_tile(sx, tx, tw)\
  281.   real_copy_tile(sx, tx, tw)
  282. #endif
  283.     for ( cy = y; cy < ey; )
  284.        {    copy_tile(irx, x, icw);
  285.         cx = x + icw;
  286.         while ( cx <= fex )
  287.            {    copy_tile(0, cx, width);
  288.             cx += width;
  289.            }
  290.         if ( cx < ex )
  291.            {    copy_tile(0, cx, ex - cx);
  292.            }
  293.         cy += ch;
  294.         ch = (cy > fey ? ey - cy : height);
  295.         row = tile->data;
  296.        }
  297. #undef copy_tile
  298. #undef real_copy_tile
  299.     return 0;
  300. }
  301.  
  302. /* Draw a one-pixel-wide line. */
  303. int
  304. gz_draw_line_fixed(fixed ixf, fixed iyf, fixed itoxf, fixed itoyf,
  305.   gx_device_color *pdevc, gs_state *pgs)
  306. {    int ix = fixed2int_var_rounded(ixf);
  307.     int iy = fixed2int_var_rounded(iyf);
  308.     int itox = fixed2int_var_rounded(itoxf);
  309.     int itoy = fixed2int_var_rounded(itoyf);
  310.     gx_device *dev;
  311.     if ( itoy == iy )        /* horizontal line */
  312.       { return (ix <= itox ?
  313.             gz_fill_rectangle(ix, iy, itox - ix, 1, pdevc, pgs) :
  314.             gz_fill_rectangle(itox, iy, ix - itox, 1, pdevc, pgs)
  315.             );
  316.       }
  317.     if ( color_is_pure(pdevc) &&
  318.         (dev = pgs->device->info,
  319.          (*dev->procs->draw_line)(dev, ix, iy, itox, itoy,
  320.                       pdevc->color1)) >= 0 )
  321.       return 0;
  322.     { fixed h = itoyf - iyf;
  323.       fixed w = itoxf - ixf;
  324.       fixed tf;
  325. #define fswap(a, b) tf = a, a = b, b = tf
  326. #define half float2fixed(0.5)
  327.       if ( (w < 0 ? -w : w) <= (h < 0 ? -h : h) )
  328.         { if ( h < 0 )
  329.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  330.         h = -h;
  331.           return gz_fill_trapezoid_fixed(ixf - half, int2fixed(1), iyf,
  332.                          itoxf - half, int2fixed(1), h,
  333.                          0, pdevc, pgs);
  334.         }
  335.       else
  336.         { if ( w < 0 )
  337.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  338.         w = -w;
  339.           return gz_fill_trapezoid_fixed(iyf - half, int2fixed(1), ixf,
  340.                          itoyf - half, int2fixed(1), w,
  341.                          1, pdevc, pgs);
  342.         }
  343. #undef half
  344. #undef fswap
  345.     }
  346. }
  347.  
  348. /****** STUBS ******/
  349. int
  350. gx_default_draw_line(gx_device *dev,
  351.   int x0, int y0, int x1, int y1, gx_color_index color)
  352. {    return -1;
  353. }
  354. int
  355. gx_default_fill_trapezoid(gx_device *dev,
  356.   int x0, int y0, int width0, int x1, int y1, int width1,
  357.   gx_color_index color)
  358. {    return -1;
  359. }
  360. int
  361. gx_default_tile_trapezoid(gx_device *dev, gx_bitmap *tile,
  362.   int x0, int y0, int width0, int x1, int y1, int width1,
  363.   gx_color_index color0, gx_color_index color1, int px, int py)
  364. {    return -1;
  365. }
  366. int
  367. gx_default_tile_trapezoid_color(gx_device *dev, gx_bitmap *tile,
  368.   int x0, int y0, int width0, int x1, int y1, int width1, int px, int py)
  369. {    return -1;
  370. }
  371.