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

  1. /* Copyright (C) 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. /* gxclip2.c */
  20. /* Mask clipping for patterns */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"
  27. #include "gxclip2.h"
  28.  
  29. private_st_device_tile_clip();
  30.  
  31. /* Device procedures */
  32. private dev_proc_fill_rectangle(tile_clip_fill_rectangle);
  33. private dev_proc_copy_mono(tile_clip_copy_mono);
  34. private dev_proc_copy_color(tile_clip_copy_color);
  35.  
  36. /* The device descriptor. */
  37. private const gx_device_tile_clip gs_tile_clip_device =
  38. {    sizeof(gx_device_tile_clip),
  39.     0,            /* &...dev.std_procs */
  40.     "tile clipper",
  41.     0, 0, 1, 1, no_margins, dci_black_and_white,
  42.     dev_init_misc_open(true),
  43.     {    gx_default_open_device,
  44.         gx_forward_get_initial_matrix,
  45.         gx_default_sync_output,
  46.         gx_default_output_page,
  47.         gx_default_close_device,
  48.         gx_forward_map_rgb_color,
  49.         gx_forward_map_color_rgb,
  50.         tile_clip_fill_rectangle,
  51.         gx_default_tile_rectangle,
  52.         tile_clip_copy_mono,
  53.         tile_clip_copy_color,
  54.         gx_default_draw_line,
  55.         gx_default_get_bits,
  56.         gx_forward_get_params,
  57.         gx_forward_put_params,
  58.         gx_forward_map_cmyk_color,
  59.         gx_forward_get_xfont_procs,
  60.         gx_forward_get_xfont_device,
  61.         gx_forward_map_rgb_alpha_color,
  62.         gx_forward_get_page_device
  63.     }
  64. };
  65.  
  66. /* Initialize a tile clipping device from a mask. */
  67. int
  68. tile_clip_initialize(gx_device_tile_clip *cdev, gx_tile_bitmap *tile,
  69.   gx_device *idev)
  70. {    int buffer_width = tile->size.x;
  71.     int buffer_height =
  72.         tile_clip_buffer_size / (tile->raster + sizeof(byte *));
  73.     *cdev = gs_tile_clip_device;
  74.     cdev->procs = &cdev->std_procs;
  75.     cdev->width = idev->width;
  76.     cdev->height = idev->height;
  77.     cdev->color_info = idev->color_info;
  78.     cdev->target = idev;
  79.     cdev->tile = *tile;
  80.     cdev->phase.x = cdev->phase.y = 0;
  81.     if ( buffer_height > tile->size.y )
  82.         buffer_height = tile->size.y;
  83.     gs_make_mem_mono_device(&cdev->mdev, 0);
  84.     for ( ; ; )
  85.     {    if ( buffer_height <= 0 )
  86.             return_error(gs_error_rangecheck);
  87.         cdev->mdev.width = buffer_width;
  88.         cdev->mdev.height = buffer_height;
  89.         if ( gdev_mem_bitmap_size(&cdev->mdev) <= tile_clip_buffer_size )
  90.             break;
  91.         buffer_height--;
  92.     }
  93.     cdev->mdev.base = cdev->buffer.bytes;
  94.     return (*dev_proc(&cdev->mdev, open_device))((gx_device *)&cdev->mdev);
  95. }
  96.  
  97. #define cdev ((gx_device_tile_clip *)dev)
  98.  
  99. /* Fill a rectangle by tiling with the mask. */
  100. private int
  101. tile_clip_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  102.   gx_color_index color)
  103. {    gx_device *tdev = cdev->target;
  104.     return (*dev_proc(tdev, tile_rectangle))(tdev, &cdev->tile,
  105.         x, y, w, h,
  106.         gx_no_color_index, color, cdev->phase.x, cdev->phase.y);
  107. }
  108.  
  109. /* Copy a monochrome bitmap.  We divide it up into maximal chunks */
  110. /* that line up with a single tile, and then do the obvious Boolean */
  111. /* combination of the tile mask and the source. */
  112. private int
  113. tile_clip_copy_mono(gx_device *dev,
  114.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  115.   int x, int y, int w, int h,
  116.   gx_color_index color0, gx_color_index color1)
  117. {    gx_color_index color, mcolor0, mcolor1;
  118.     int ty, ny;
  119.     if ( color1 != gx_no_color_index )
  120.     {    if ( color0 != gx_no_color_index )
  121.         {    /* Pre-fill with color0. */
  122.             tile_clip_fill_rectangle(dev, x, y, w, h, color0);
  123.         }
  124.         color = color1;
  125.         mcolor0 = 0, mcolor1 = gx_no_color_index;
  126.     }
  127.     else if ( color0 != gx_no_color_index )
  128.     {    color = color0;
  129.         mcolor0 = gx_no_color_index, mcolor1 = 0;
  130.     }
  131.     else
  132.         return 0;
  133.     for ( ty = y; ty < y + h; ty += ny )
  134.     {    int tx, nx;
  135.         int cy = (ty + cdev->phase.y) % cdev->tile.rep_height;
  136.         ny = min(y + h - ty, cdev->tile.size.y - cy);
  137.         if ( ny > cdev->mdev.height )
  138.             ny = cdev->mdev.height;
  139.         for ( tx = x; tx < x + w; tx += nx )
  140.         {    int cx = (tx + cdev->phase.x) % cdev->tile.rep_width;
  141.             nx = min(x + w - tx, cdev->tile.size.x - cx);
  142.             /* Copy a tile slice to the memory device buffer. */
  143.             memcpy(cdev->buffer.bytes,
  144.                    cdev->tile.data + cy * cdev->tile.raster,
  145.                    cdev->tile.raster * ny);
  146.             /* Intersect the tile with the source data. */
  147.             /* mcolor0 and mcolor1 invert the data if needed. */
  148.             (*dev_proc(&cdev->mdev, copy_mono))((gx_device *)&cdev->mdev,
  149.               data + (ty - y) * raster, sourcex + tx - x,
  150.               raster, gx_no_bitmap_id,
  151.               cx, 0, nx, ny, mcolor0, mcolor1);
  152.             /* Now copy the color through the double mask. */
  153.             (*dev_proc(cdev->target, copy_mono))(cdev->target,
  154.               cdev->buffer.bytes, cx, cdev->tile.raster,
  155.               gx_no_bitmap_id,
  156.               tx, ty, nx, ny, gx_no_color_index, color);
  157.         }
  158.     }
  159.     return 0;
  160. }
  161.  
  162. /* Copy a color rectangle.  We can't use the BitBlt tricks; we have to */
  163. /* scan for runs of 1s.  There are many obvious ways to speed this up; */
  164. /* we'll implement some if we need to. */
  165. private int
  166. tile_clip_copy_color(gx_device *dev,
  167.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  168.   int x, int y, int w, int h)
  169. {    const byte *data_row = data;
  170.     int cx0 = (x + cdev->phase.x) % cdev->tile.rep_width;
  171.     int ty;
  172.     for ( ty = y; ty < y + h; ty++, data_row += raster )
  173.     {    const byte *tile_row = cdev->tile.data +
  174.             ((ty + cdev->phase.y) % cdev->tile.rep_height) *
  175.             cdev->tile.raster;
  176.         int cx = cx0;
  177.         const byte *tp = tile_row + (cx >> 3);
  178.         byte tbit = 0x80 >> (cx & 7);
  179.         int tx;
  180.         for ( tx = x; tx < x + w; )
  181.         {    int tx1;
  182. #define t_next()\
  183.   if ( ++cx == cdev->tile.size.x )\
  184.     cx = 0, tp = tile_row, tbit = 0x80;\
  185.   else if ( (tbit >>= 1) == 0 )\
  186.     tp++, tbit = 0x80;\
  187.   tx++
  188.             /* Skip a run of 0s. */
  189.             while ( tx < x + w && (*tp & tbit) == 0 )
  190.             {    t_next();
  191.             }
  192.             if ( tx == x + w )
  193.                 break;
  194.             /* Scan a run of 1s. */
  195.             tx1 = tx;
  196.             do
  197.             {    t_next();
  198.             }
  199.             while ( tx < x + w && (*tp & tbit) != 0 );
  200.             /* Copy the run. */
  201.             (*dev_proc(cdev->target, copy_color))(cdev->target,
  202.               data_row, sourcex + tx1 - x, raster,
  203.               gx_no_bitmap_id, tx1, ty, tx - tx1, 1);
  204.         }
  205.     }
  206.  
  207.     return 0;
  208. }
  209.  
  210. /* ------ Operators for testing ------ */
  211.  
  212. #undef cdev
  213.  
  214. #include "iref.h"
  215. #include "errors.h"
  216. #include "oper.h"
  217. #include "ialloc.h"
  218. #include "igstate.h"
  219.  
  220. /* <bits> <width> <height> .setmask - */
  221. private int
  222. zsetmask(os_ptr op)
  223. {    gx_device_tile_clip *cdev;
  224.     gx_tile_bitmap tile;
  225.     uint raster;
  226.     check_read_type(op[-2], t_string);
  227.     check_type(op[-1], t_integer);
  228.     check_type(*op, t_integer);
  229.     cdev = ialloc_struct(gx_device_tile_clip, &st_device_tile_clip, "setmask");
  230.     if ( cdev == 0 )
  231.         return_error(e_VMerror);
  232.     tile.rep_width = tile.size.x = op[-1].value.intval;
  233.     tile.rep_height = tile.size.y = op->value.intval;
  234.     /* Set mdev.width & .height just to compute the raster. */
  235.     gs_make_mem_mono_device(&cdev->mdev, 0);
  236.     cdev->mdev.width = tile.size.x;
  237.     cdev->mdev.height = tile.size.y;
  238.     raster = gx_device_raster((gx_device *)&cdev->mdev, 1);
  239.     if ( r_size(op - 2) != raster * tile.size.y )
  240.         return_error(e_rangecheck);
  241.     tile.data = op[-2].value.bytes;
  242.     tile.raster = raster;
  243.     tile.id = gx_no_bitmap_id;
  244.     tile_clip_initialize(cdev, &tile, gs_currentdevice(igs));
  245.     gs_setdevice_no_erase(igs, (gx_device *)cdev);
  246.     pop(3);
  247.     return 0;
  248. }
  249.  
  250. /* <bits> <width> <raster> <x> <y> .copycolor - */
  251. private int
  252. zcopycolor(os_ptr op)
  253. {    gx_device *idev = gs_currentdevice(igs);
  254.     uint raster;
  255.     int code;
  256.     check_read_type(op[-4], t_string);
  257.     check_type(op[-3], t_integer);
  258.     check_type(op[-2], t_integer);
  259.     check_type(op[-1], t_integer);
  260.     check_type(*op, t_integer);
  261.     raster = op[-2].value.intval;
  262.     code = (*dev_proc(idev, copy_color))(idev, op[-4].value.const_bytes,
  263.          0, raster, gx_no_bitmap_id, (int)op[-1].value.intval,
  264.          (int)op->value.intval, (int)op[-3].value.intval,
  265.          r_size(op - 4) / raster);
  266.     if ( code >= 0 )
  267.     {    pop(5);
  268.     }
  269.     return code;
  270. }
  271.  
  272. op_def zclip2_op_defs[] = {
  273.     {"3.setmask", zsetmask},
  274.     {"5.copycolor", zcopycolor},
  275.     op_def_end(0)
  276. };
  277.