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

  1. /* Copyright (C) 1989, 1992 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. /* gdevmem1.c */
  20. /* Generic and monobit "memory" (stored bitmap) device */
  21. /* for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gxdevice.h"
  27. #include "gxdevmem.h"            /* semi-public definitions */
  28. #include "gdevmem.h"            /* private definitions */
  29.  
  30. /* Define masks for little-endian operation. */
  31. #if !arch_is_big_endian
  32. const bits16 gdev_mem_swapped_left_masks[17] = {
  33.     0xffff, 0xff7f, 0xff3f, 0xff1f, 0xff0f, 0xff07, 0xff03, 0xff01,
  34.     0xff00, 0x7f00, 0x3f00, 0x1f00, 0x0f00, 0x0700, 0x0300, 0x0100,
  35.     0x0000
  36. };
  37. #endif
  38.  
  39. /* Structure descriptor */
  40. public_st_device_memory();
  41.  
  42. /* GC procedures */
  43. #define mptr ((gx_device_memory *)vptr)
  44. private ENUM_PTRS_BEGIN(device_memory_enum_ptrs) {
  45.     return (*st_device_forward.enum_ptrs)(vptr, sizeof(gx_device_forward), index-2, pep);
  46.     }
  47.     case 0:
  48.       *pep = (mptr->foreign_bits ? NULL : (void *)mptr->base);
  49.       break;
  50.     ENUM_STRING_PTR(1, gx_device_memory, palette);
  51. ENUM_PTRS_END
  52. private RELOC_PTRS_BEGIN(device_memory_reloc_ptrs) {
  53.     if ( !mptr->foreign_bits )
  54.     {    byte *base_old = mptr->base;
  55.         uint reloc;
  56.         int y;
  57.         RELOC_PTR(gx_device_memory, base);
  58.         reloc = base_old - mptr->base;
  59.         for ( y = 0; y < mptr->height; y++ )
  60.           mptr->line_ptrs[y] -= reloc;
  61.     }
  62.     RELOC_STRING_PTR(gx_device_memory, palette);
  63.     (*st_device_forward.reloc_ptrs)(vptr, sizeof(gx_device_forward), gcst);
  64. } RELOC_PTRS_END
  65. #undef mptr
  66.  
  67. /* ------ Generic code ------ */
  68.  
  69. /* Return the appropriate memory device for a given */
  70. /* number of bits per pixel (0 if none suitable). */
  71. const gx_device_memory *
  72. gdev_mem_device_for_bits(int bits_per_pixel)
  73. {    switch ( bits_per_pixel )
  74.        {
  75.     case 1: return &mem_mono_device;
  76.     case 2: return &mem_mapped2_color_device;
  77.     case 4: return &mem_mapped4_color_device;
  78.     case 8: return &mem_mapped8_color_device;
  79.     case 16: return &mem_true16_color_device;
  80.     case 24: return &mem_true24_color_device;
  81.     case 32: return &mem_true32_color_device;
  82.     default: return 0;
  83.        }
  84. }
  85.  
  86. /* Make a memory device. */
  87. void
  88. gs_make_mem_device(gx_device_memory *dev, const gx_device_memory *cdev,
  89.   gs_memory_t *mem, int page_device)
  90. {    *dev = *cdev;
  91.     dev->memory = mem;
  92.     dev->procs = &dev->std_procs;
  93.     switch ( page_device )
  94.       {
  95.       case -1:
  96.         dev->std_procs.get_page_device = gx_default_get_page_device;
  97.         break;
  98.       case 1:
  99.         dev->std_procs.get_page_device = gx_page_device_get_page_device;
  100.         break;
  101.       }
  102. }
  103. /* Make a monobit memory device.  This is never a page device. */
  104. void
  105. gs_make_mem_mono_device(gx_device_memory *dev, gs_memory_t *mem)
  106. {    gs_make_mem_device(dev, &mem_mono_device, mem, -1);
  107. }
  108.  
  109. /* Compute the size of the bitmap storage, */
  110. /* including the space for the scan line pointer table. */
  111. /* Note that scan lines are padded to a multiple of align_bitmap_mod bytes, */
  112. /* and additional padding may be needed if the pointer table */
  113. /* must be aligned to an even larger modulus. */
  114. private ulong
  115. mem_bitmap_bits_size(const gx_device_memory *dev)
  116. {    return round_up((ulong)dev->height * gdev_mem_raster(dev),
  117.             max(align_bitmap_mod, arch_align_ptr_mod));
  118. }
  119. ulong
  120. gdev_mem_bitmap_size(const gx_device_memory *dev)
  121. {        return mem_bitmap_bits_size(dev) +
  122.         (ulong)dev->height * sizeof(byte *);
  123. }
  124.  
  125. /* Open a memory device, allocating the data area if appropriate, */
  126. /* and create the scan line table. */
  127. int
  128. mem_open(gx_device *dev)
  129. {    byte *scan_line;
  130.     uint raster = mdev->raster = gdev_mem_raster(mdev);
  131.     byte **pptr;
  132.     byte **pend;
  133.     if ( mdev->bitmap_memory != 0 )
  134.     {    /* Allocate the data now. */
  135.         ulong size = gdev_mem_bitmap_size(mdev);
  136.         if ( (uint)size != size )
  137.             return_error(gs_error_limitcheck);
  138.         mdev->base = gs_alloc_bytes(mdev->bitmap_memory, (uint)size,
  139.                         "mem_open");
  140.         if ( mdev->base == 0 )
  141.             return_error(gs_error_VMerror);
  142.         mdev->foreign_bits = false;
  143.     }
  144.     scan_line = mdev->base;
  145.     pptr = (byte **)byte_ptr_add(scan_line, mem_bitmap_bits_size(mdev));
  146.     pend = pptr + dev->height;
  147.     mdev->line_ptrs = pptr;
  148.     while ( pptr < pend )
  149.        {    *pptr++ = scan_line;
  150.         scan_line = byte_ptr_add(scan_line, raster);
  151.        }
  152.     return 0;
  153. }
  154.  
  155. /* Return the initial transformation matrix */
  156. void
  157. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  158. {    pmat->xx = mdev->initial_matrix.xx;
  159.     pmat->xy = mdev->initial_matrix.xy;
  160.     pmat->yx = mdev->initial_matrix.yx;
  161.     pmat->yy = mdev->initial_matrix.yy;
  162.     pmat->tx = mdev->initial_matrix.tx;
  163.     pmat->ty = mdev->initial_matrix.ty;
  164. }
  165.  
  166. /* Test whether a device is a memory device */
  167. bool
  168. gs_device_is_memory(const gx_device *dev)
  169. {    /* We can't just compare the procs, or even an individual proc, */
  170.     /* because we might be tracing.  Compare the device name, */
  171.     /* and hope for the best. */
  172.     const char *name = dev->dname;
  173.     int i;
  174.     for ( i = 0; i < 6; i++ )
  175.       if ( name[i] != "image("[i] ) return false;
  176.     return true;
  177. }
  178.  
  179. /* Close a memory device, freeing the data area if appropriate. */
  180. int
  181. mem_close(gx_device *dev)
  182. {    if ( mdev->bitmap_memory != 0 )
  183.       gs_free_object(mdev->bitmap_memory, mdev->base, "mem_close");
  184.     return 0;
  185. }
  186.  
  187. /* Copy a scan line to a client. */
  188. #undef chunk
  189. #define chunk byte
  190. int
  191. mem_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  192. {    byte *src;
  193.     if ( y < 0 || y >= dev->height )
  194.         return_error(gs_error_rangecheck);
  195.     src = scan_line_base(mdev, y);
  196.     if ( actual_data == 0 )
  197.         memcpy(str, src, gx_device_raster(dev, 0));
  198.     else
  199.         *actual_data = src;
  200.     return 0;
  201. }
  202.  
  203. /* ------ Monochrome ------ */
  204.  
  205. /* Procedures */
  206. private dev_proc_copy_mono(mem_mono_copy_mono);
  207. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  208.  
  209. /* The device descriptor. */
  210. /* The instance is public. */
  211. const gx_device_memory far_data mem_mono_device =
  212.   mem_device("image(mono)", 0, 1,
  213.     gx_default_map_rgb_color, gx_default_map_color_rgb,
  214.     mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
  215.  
  216. /* Convert x coordinate to byte offset in scan line. */
  217. #define x_to_byte(x) ((x) >> 3)
  218.  
  219. /* Fill a rectangle with a color. */
  220. #undef chunk
  221. #define chunk mono_chunk
  222. private int
  223. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  224.   gx_color_index color)
  225. {    uint bit;
  226.     chunk right_mask;
  227.     byte fill;
  228.     declare_scan_ptr(dest);
  229.     fit_fill(dev, x, y, w, h);
  230.     setup_rect(dest);
  231. #define write_loop(stat)\
  232.  { int line_count = h;\
  233.    chunk *ptr = dest;\
  234.    do { stat; inc_chunk_ptr(ptr, draster); }\
  235.    while ( --line_count );\
  236.  }
  237. #define write_partial(msk)\
  238.    if ( fill ) write_loop(*ptr |= msk)\
  239.    else write_loop(*ptr &= ~msk)
  240.     switch ( color )
  241.        {
  242.     case 0: fill = mdev->invert; break;
  243.     case 1: fill = ~mdev->invert; break;
  244.     case gx_no_color_index: return 0;        /* transparent */
  245.     default: return -1;        /* invalid */
  246.        }
  247.     bit = x & chunk_align_bit_mask;
  248.     if ( bit + w <= chunk_bits )
  249.        {    /* Only one word. */
  250.         set_mono_thin_mask(right_mask, w, bit);
  251.        }
  252.     else
  253.        {    int byte_count;
  254.         if ( bit )
  255.            {    chunk mask;
  256.             set_mono_left_mask(mask, bit);
  257.             write_partial(mask);
  258.             dest++;
  259.             w += bit - chunk_bits;
  260.            }
  261.         set_mono_right_mask(right_mask, w & chunk_bit_mask);
  262.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  263.           switch ( byte_count )
  264.         {
  265.         case chunk_bytes:
  266.         {    chunk cfill = -(chunk)(fill & 1);
  267.             write_loop(*ptr = cfill);
  268.             inc_chunk_ptr(dest, chunk_bytes);
  269.         }    break;
  270.         case chunk_bytes * 2:
  271.         {    chunk cfill = -(chunk)(fill & 1);
  272. #define set2(p, v) (*p = v, p[1] = v)
  273.             write_loop(set2(ptr, cfill));
  274. #undef set2
  275.             inc_chunk_ptr(dest, chunk_bytes * 2);
  276.         }    break;
  277.         default:
  278.             write_loop(memset(ptr, fill, byte_count));
  279.             inc_chunk_ptr(dest, byte_count);
  280.             break;
  281.         }
  282.        }
  283.     if ( right_mask )
  284.         write_partial(right_mask);
  285.     return 0;
  286. }
  287.  
  288. /* Copy a monochrome bitmap. */
  289.  
  290. /* Fetch a chunk from the source. */
  291. /* The source data are always stored big-endian. */
  292. /* Note that the macros always cast cptr, */
  293. /* so it doesn't matter what the type of cptr is. */
  294. /* cshift = chunk_bits - shift. */
  295. #undef chunk
  296. #if arch_is_big_endian
  297. #  define chunk uint
  298. #  define cfetch_right(cptr, shift, cshift)\
  299.     (cfetch_aligned(cptr) >> shift)
  300. #  define cfetch_left(cptr, shift, cshift)\
  301.     (cfetch_aligned(cptr) << shift)
  302. /* Fetch a chunk that straddles a chunk boundary. */
  303. #  define cfetch2(cptr, cskew, skew)\
  304.     (cfetch_left(cptr, cskew, skew) +\
  305.      cfetch_right((chunk *)(cptr) + 1, skew, cskew))
  306. #else                /* little-endian */
  307. #  define chunk bits16
  308. private const bits16 right_masks2[9] = {
  309.     0xffff, 0x7f7f, 0x3f3f, 0x1f1f, 0x0f0f, 0x0707, 0x0303, 0x0101, 0x0000
  310. };
  311. private const bits16 left_masks2[9] = {
  312.     0xffff, 0xfefe, 0xfcfc, 0xf8f8, 0xf0f0, 0xe0e0, 0xc0c0, 0x8080, 0x0000
  313. };
  314. #  define ccont(cptr, off) (((chunk *)(cptr))[off])
  315. #  define cfetch_right(cptr, shift, cshift)\
  316.     ((shift) < 8 ?\
  317.      ((ccont(cptr, 0) >> (shift)) & right_masks2[shift]) +\
  318.       (ccont(cptr, 0) << (cshift)) :\
  319.      ((chunk)*(byte *)(cptr) << (cshift)) & 0xff00)
  320. #  define cfetch_left(cptr, shift, cshift)\
  321.     ((shift) < 8 ?\
  322.      ((ccont(cptr, 0) << (shift)) & left_masks2[shift]) +\
  323.       (ccont(cptr, 0) >> (cshift)) :\
  324.      ((ccont(cptr, 0) & 0xff00) >> (cshift)) & 0xff)
  325. /* Fetch a chunk that straddles a chunk boundary. */
  326. /* We can avoid testing the shift amount twice */
  327. /* by expanding the cfetch_left/right macros in-line. */
  328. #  define cfetch2(cptr, cskew, skew)\
  329.     ((cskew) < 8 ?\
  330.      ((ccont(cptr, 0) << (cskew)) & left_masks2[cskew]) +\
  331.       (ccont(cptr, 0) >> (skew)) +\
  332.       (((chunk)(((byte *)(cptr))[2]) << (cskew)) & 0xff00) :\
  333.      (((ccont(cptr, 0) & 0xff00) >> (skew)) & 0xff) +\
  334.       ((ccont(cptr, 1) >> (skew)) & right_masks2[skew]) +\
  335.        (ccont(cptr, 1) << (cskew)))
  336. #endif
  337. /* Since source and destination are both always big-endian, */
  338. /* fetching an aligned chunk never requires byte swapping. */
  339. #  define cfetch_aligned(cptr)\
  340.     (*(chunk *)(cptr))
  341.  
  342. /* copy_function and copy_shift get added together for dispatch */
  343. typedef enum {
  344.     copy_or = 0, copy_store, copy_and, copy_funny
  345. } copy_function;
  346. /* copy_right/left is not an enum, because compilers complain about */
  347. /* an enumeration clash when these are added to a copy_function. */
  348. #define copy_right ((copy_function)0)
  349. #define copy_left ((copy_function)4)
  350. typedef struct {
  351.     short invert;
  352.     ushort op;            /* copy_function */
  353. } copy_mode;
  354. /* Map from <c0,c1,invert> to copy_mode. */
  355. #define cm(i,op) { i, (ushort)op }
  356. private copy_mode copy_modes[9*2] = {
  357.     cm(-1, copy_funny),        /* NN */
  358.     cm(-1, copy_and),        /* N0 */
  359.     cm(0, copy_or),            /* N1 */
  360.     cm(0, copy_and),        /* 0N */
  361.     cm(0, copy_funny),        /* 00 */
  362.     cm(0, copy_store),        /* 01 */
  363.     cm(-1, copy_or),        /* 1N */
  364.     cm(-1, copy_store),        /* 10 */
  365.     cm(0, copy_funny),        /* 11 */
  366.     cm(-1, copy_funny),        /* NNi */
  367.     cm(0, copy_or),            /* N1i */
  368.     cm(-1, copy_and),        /* N0i */
  369.     cm(-1, copy_or),        /* 1Ni */
  370.     cm(0, copy_funny),        /* 11i */
  371.     cm(-1, copy_store),        /* 10i */
  372.     cm(0, copy_and),        /* 0Ni */
  373.     cm(0, copy_store),        /* 01i */
  374.     cm(0, copy_funny)        /* 00i */
  375. };
  376. private int
  377. mem_mono_copy_mono(gx_device *dev,
  378.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  379.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  380. {    register const byte *bptr;        /* actually chunk * */
  381.     int dbit, wleft;
  382.     uint mask;
  383.     copy_mode mode;
  384. #define function (copy_function)(mode.op)
  385.     declare_scan_ptr_as(dbptr, byte *);
  386. #define optr ((chunk *)dbptr)
  387.     register int skew;
  388.     register uint invert;
  389.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  390. #if gx_no_color_index_value != -1        /* hokey! */
  391.     if ( zero == gx_no_color_index ) zero = -1;
  392.     if ( one == gx_no_color_index ) one = -1;
  393. #endif
  394. #define izero (int)zero
  395. #define ione (int)one
  396.     mode =
  397.       copy_modes[(mdev->invert & 9) + izero + izero + izero + ione + 4];
  398. #undef izero
  399. #undef ione
  400.     invert = (uint)(int)mode.invert;    /* load register */
  401.     setup_rect_as(dbptr, byte *);
  402.     bptr = base + ((sourcex & ~chunk_align_bit_mask) >> 3);
  403.     dbit = x & chunk_align_bit_mask;
  404.     skew = dbit - (sourcex & chunk_align_bit_mask);
  405. /* Macros for writing partial chunks. */
  406. /* The destination pointer is always named optr, */
  407. /* and must be declared as chunk *. */
  408. /* cinvert may be temporarily redefined. */
  409. #define cinvert(bits) ((bits) ^ invert)
  410. #define write_or_masked(bits, mask, off)\
  411.   optr[off] |= (cinvert(bits) & mask)
  412. #define write_store_masked(bits, mask, off)\
  413.   optr[off] = ((optr[off] & ~mask) | (cinvert(bits) & mask))
  414. #define write_and_masked(bits, mask, off)\
  415.   optr[off] &= (cinvert(bits) | ~mask)
  416. /* Macros for writing full chunks. */
  417. #define write_or(bits)  *optr |= cinvert(bits)
  418. #define write_store(bits) *optr = cinvert(bits)
  419. #define write_and(bits) *optr &= cinvert(bits)
  420. /* Macro for incrementing to next chunk. */
  421. #define next_x_chunk\
  422.   bptr += chunk_bytes; dbptr += chunk_bytes
  423. /* Common macro for the end of each scan line. */
  424. #define end_y_loop(sdelta, ddelta)\
  425.   if ( --h == 0 ) break;\
  426.   bptr += sdelta; dbptr += ddelta
  427.     if ( (wleft = w + dbit - chunk_bits) <= 0 )
  428.        {    /* The entire operation fits in one (destination) chunk. */
  429.         set_mono_thin_mask(mask, w, dbit);
  430. #define write_single(wr_op, src)\
  431.   for ( ; ; )\
  432.    { wr_op(src, mask, 0);\
  433.      end_y_loop(sraster, draster);\
  434.    }
  435. #define write1_loop(src)\
  436.   switch ( function ) {\
  437.     case copy_or: write_single(write_or_masked, src); break;\
  438.     case copy_store: write_single(write_store_masked, src); break;\
  439.     case copy_and: write_single(write_and_masked, src); break;\
  440.     default: goto funny;\
  441.   }
  442.         if ( skew >= 0 )    /* single -> single, right/no shift */
  443.         {    if ( skew == 0 )    /* no shift */
  444.             {    write1_loop(cfetch_aligned(bptr));
  445.             }
  446.             else            /* right shift */
  447.             {    int cskew = chunk_bits - skew;
  448.                 write1_loop(cfetch_right(bptr, skew, cskew));
  449.             }
  450.         }
  451.         else if ( wleft <= skew )    /* single -> single, left shift */
  452.         {    int cskew = chunk_bits + skew;
  453.             skew = -skew;
  454.             write1_loop(cfetch_left(bptr, skew, cskew));
  455.         }
  456.         else            /* double -> single */
  457.         {    int cskew = -skew;
  458.             skew += chunk_bits;
  459.             write1_loop(cfetch2(bptr, cskew, skew));
  460.         }
  461. #undef write1_loop
  462. #undef write_single
  463.        }
  464.     else if ( wleft <= skew )
  465.        {    /* 1 source chunk -> 2 destination chunks. */
  466.         /* This is an important special case for */
  467.         /* both characters and halftone tiles. */
  468.         register uint bits;
  469.         uint rmask;
  470.         int cskew = chunk_bits - skew;
  471.         set_mono_left_mask(mask, dbit);
  472.         set_mono_right_mask(rmask, wleft);
  473. #undef cinvert
  474. #define cinvert(bits) (bits)        /* pre-inverted here */
  475. #if arch_is_big_endian            /* no byte swapping */
  476. #  define write_1to2(wr_op)\
  477.   for ( ; ; )\
  478.    { bits = cfetch_aligned(bptr) ^ invert;\
  479.      wr_op(bits >> skew, mask, 0);\
  480.      wr_op(bits << cskew, rmask, 1);\
  481.      end_y_loop(sraster, draster);\
  482.    }
  483. #else                    /* byte swapping */
  484. #  define write_1to2(wr_op)\
  485.   for ( ; ; )\
  486.    { wr_op(cfetch_right(bptr, skew, cskew) ^ invert, mask, 0);\
  487.      wr_op(cfetch_left(bptr, cskew, skew) ^ invert, rmask, 1);\
  488.      end_y_loop(sraster, draster);\
  489.    }
  490. #endif
  491.         switch ( function )
  492.            {
  493.         case copy_or: write_1to2(write_or_masked); break;
  494.         case copy_store: write_1to2(write_store_masked); break;
  495.         case copy_and: write_1to2(write_and_masked); break;
  496.         default: goto funny;
  497.            }
  498. #undef cinvert
  499. #define cinvert(bits) ((bits) ^ invert)
  500. #undef write_1to2
  501.        }
  502.     else
  503.        {    /* More than one source chunk and more than one */
  504.         /* destination chunk are involved. */
  505.         uint rmask;
  506.         int words = (wleft & ~chunk_bit_mask) >> 3;
  507.         uint sskip = sraster - words;
  508.         uint dskip = draster - words;
  509.         register uint bits;
  510.         set_mono_left_mask(mask, dbit);
  511.         set_mono_right_mask(rmask, wleft & chunk_bit_mask);
  512.         if ( skew == 0 )    /* optimize the aligned case */
  513.            {
  514. #define write_aligned(wr_op, wr_op_masked)\
  515.   for ( ; ; )\
  516.    { int count = wleft;\
  517.      /* Do first partial chunk. */\
  518.      wr_op_masked(cfetch_aligned(bptr), mask, 0);\
  519.      /* Do full chunks. */\
  520.      while ( (count -= chunk_bits) >= 0 )\
  521.       { next_x_chunk; wr_op(cfetch_aligned(bptr)); }\
  522.      /* Do last chunk */\
  523.      if ( count > -chunk_bits )\
  524.       { wr_op_masked(cfetch_aligned(bptr + chunk_bytes), rmask, 1); }\
  525.      end_y_loop(sskip, dskip);\
  526.    }
  527.             switch ( function )
  528.               {
  529.               case copy_or:
  530.                 write_aligned(write_or, write_or_masked);
  531.                 break;
  532.               case copy_store:
  533.                 write_aligned(write_store, write_store_masked);
  534.                 break;
  535.               case copy_and:
  536.                 write_aligned(write_and, write_and_masked);
  537.                 break;
  538.               default:
  539.                 goto funny;
  540.               }
  541. #undef write_aligned
  542.            }
  543.         else            /* not aligned */
  544.            {    int ccase =
  545.               (skew >= 0 ? copy_right :
  546.                ((bptr += chunk_bytes), copy_left))
  547.               + (int)function;
  548.             int cskew = -skew & chunk_bit_mask;
  549.             skew &= chunk_bit_mask;
  550.             for ( ; ; )
  551.                {    int count = wleft;
  552. #define prefetch_right\
  553.   bits = cfetch_right(bptr, skew, cskew)
  554. #define prefetch_left\
  555.   bits = cfetch2(bptr - chunk_bytes, cskew, skew)
  556. #define write_unaligned(wr_op, wr_op_masked)\
  557.   wr_op_masked(bits, mask, 0);\
  558.   /* Do full chunks. */\
  559.   while ( count >= chunk_bits )\
  560.     { bits = cfetch2(bptr, cskew, skew);\
  561.       next_x_chunk; wr_op(bits); count -= chunk_bits;\
  562.     }\
  563.   /* Do last chunk */\
  564.   if ( count > 0 )\
  565.     { bits = cfetch_left(bptr, cskew, skew);\
  566.       if ( count > skew ) bits += cfetch_right(bptr + chunk_bytes, skew, cskew);\
  567.       wr_op_masked(bits, rmask, 1);\
  568.     }
  569.                 switch ( ccase )
  570.                   {
  571.                   case copy_or + copy_left:
  572.                     prefetch_left; goto uor;
  573.                   case copy_or + copy_right:
  574.                     prefetch_right;
  575. uor:                    write_unaligned(write_or, write_or_masked);
  576.                     break;
  577.                   case copy_store + copy_left:
  578.                     prefetch_left; goto ustore;
  579.                   case copy_store + copy_right:
  580.                     prefetch_right;
  581. ustore:                    write_unaligned(write_store, write_store_masked);
  582.                     break;
  583.                   case copy_and + copy_left:
  584.                     prefetch_left; goto uand;
  585.                   case copy_and + copy_right:
  586.                     prefetch_right;
  587. uand:                    write_unaligned(write_and, write_and_masked);
  588.                     break;
  589.                   default:
  590.                     goto funny;
  591.                   }
  592.                 end_y_loop(sskip, dskip);
  593. #undef write_unaligned
  594. #undef prefetch_left
  595. #undef prefetch_right
  596.                }
  597.            }
  598.        }
  599. #undef end_y_loop
  600. #undef next_x_chunk
  601.     return 0;
  602.     /* Handle the funny cases that aren't supposed to happen. */
  603. funny:    return (invert ? -1 : mem_mono_fill_rectangle(dev, x, y, w, h, zero));
  604. #undef optr
  605. }
  606.