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 / GDEVMEM1.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  17KB  |  550 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. /* gdevmem1.c */
  21. /* Generic and monobit "memory" (stored bitmap) device */
  22. /* for Ghostscript library. */
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gxdevice.h"
  27. #include "gxdevmem.h"            /* semi-public definitions */
  28. #include "gdevmem.h"            /* private definitions */
  29.  
  30. /* Define the chunk size for monobit operations. */
  31. #if arch_is_big_endian
  32. #  define mono_chunk uint
  33. #else
  34. #  define mono_chunk ushort
  35. #endif
  36.  
  37. /* ------ Generic code ------ */
  38.  
  39. /* Return the appropriate memory device for a given */
  40. /* number of bits per pixel (0 if none suitable). */
  41. const gx_device_memory *
  42. gdev_mem_device_for_bits(int bits_per_pixel)
  43. {    switch ( bits_per_pixel )
  44.        {
  45.     case 1: return &mem_mono_device;
  46.     case 2: return &mem_mapped2_color_device;
  47.     case 4: return &mem_mapped4_color_device;
  48.     case 8: return &mem_mapped8_color_device;
  49.     case 16: return &mem_true16_color_device;
  50.     case 24: return &mem_true24_color_device;
  51.     case 32: return &mem_true32_color_device;
  52.     default: return 0;
  53.        }
  54. }
  55.  
  56. /* Compute the size of the bitmap storage, */
  57. /* including the space for the scan line pointer table. */
  58. /* Note that scan lines are padded to a multiple of 4 bytes. */
  59. ulong
  60. gdev_mem_bitmap_size(const gx_device_memory *dev)
  61. {    return (ulong)dev->height * (gdev_mem_raster(dev) + sizeof(byte *));
  62. }
  63.  
  64. /* Open a memory device, allocating the data area if appropriate, */
  65. /* and create the scan line table. */
  66. int
  67. mem_open(gx_device *dev)
  68. {    byte *scan_line;
  69.     uint raster = mdev->raster = gdev_mem_raster((gx_device_memory *)dev);
  70.     byte **pptr;
  71.     byte **pend;
  72.     if ( mdev->memory_procs.alloc != 0 )
  73.     {    /* Allocate the data now. */
  74.         ulong size = gdev_mem_bitmap_size(mdev);
  75.         if ( (uint)size != size )
  76.             return gs_error_limitcheck;
  77.         mdev->base = (byte *)(*mdev->memory_procs.alloc)(1, (uint)size, "mem_open");
  78.         if ( mdev->base == 0 )
  79.             return gs_error_VMerror;
  80.     }
  81.     scan_line = mdev->base;
  82.     pptr = (byte **)(scan_line + (uint)dev->height * raster);
  83.     pend = pptr + dev->height;
  84.     mdev->line_ptrs = pptr;
  85.     while ( pptr < pend )
  86.        {    *pptr++ = scan_line;
  87.         scan_line += raster;
  88.        }
  89.     mdev->bytes_le =
  90. #if arch_is_big_endian
  91.         0
  92. #else
  93.         /* NOTE: mem_mono_get_bits relies on the fact that */
  94.         /* sizeof(mono_chunk) == 2! */
  95.         (mdev->color_info.depth < 8 ? sizeof(mono_chunk) : 0);
  96. #endif
  97.         ;
  98.     return 0;
  99. }
  100.  
  101. /* Return the initial transformation matrix */
  102. void
  103. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  104. {    pmat->xx = mdev->initial_matrix.xx;
  105.     pmat->xy = mdev->initial_matrix.xy;
  106.     pmat->yx = mdev->initial_matrix.yx;
  107.     pmat->yy = mdev->initial_matrix.yy;
  108.     pmat->tx = mdev->initial_matrix.tx;
  109.     pmat->ty = mdev->initial_matrix.ty;
  110. }
  111.  
  112. /* Test whether a device is a memory device */
  113. int
  114. gs_device_is_memory(const gx_device *dev)
  115. {    /* We can't just compare the procs, or even an individual proc, */
  116.     /* because we might be tracing.  Compare the device name, */
  117.     /* and hope for the best. */
  118.     const char *name = dev->dname;
  119.     int i;
  120.     for ( i = 0; i < 6; i++ )
  121.       if ( name[i] != "image("[i] ) return 0;
  122.     return 1;
  123. }
  124.  
  125. /* Ensure that the data bytes are in big-endian order. */
  126. /* This is never called on big-endian platforms; */
  127. /* on little-endian platforms, the chunk size is ushort, */
  128. /* regardless of the size of an int. */
  129. void
  130. gdev_mem_ensure_byte_order(gx_device_memory *dev)
  131. {
  132. #if !arch_is_big_endian
  133.     if ( !dev->bytes_le ) return;    /* already in order */
  134.     memswab2(dev->base, dev->base, dev->raster * dev->height);
  135.     dev->bytes_le = 0;
  136. #endif
  137. }
  138.  
  139. /* Close a memory device, freeing the data area if appropriate. */
  140. int
  141. mem_close(gx_device *dev)
  142. {    if ( mdev->memory_procs.free != 0 )
  143.       (*mdev->memory_procs.free)((char *)mdev->base,
  144.         1, (uint)gdev_mem_bitmap_size(mdev), "mem_close");
  145.     return 0;
  146. }
  147.  
  148. /* Copy one or more scan lines to a client. */
  149. #undef chunk
  150. #define chunk byte
  151. int
  152. mem_get_bits(gx_device *dev, int y, byte *str, uint size, int pad_to_word)
  153. {    uint bytes_per_line = gx_device_raster(dev, pad_to_word);
  154.     byte *src = scan_line_base(mdev, y);
  155.     byte *dest = str;
  156.     uint count = min(size / bytes_per_line, dev->height - y);
  157.     int swap = mdev->bytes_le;
  158.     if ( bytes_per_line == mdev->raster )
  159.        {    if ( swap && pad_to_word >= 0 )
  160.             memswab2(src, dest, bytes_per_line * count);
  161.         else
  162.             memcpy(dest, src, bytes_per_line * count);
  163.        }
  164.     else                /* know pad_to_word == 0 */
  165.        {    uint c;
  166.         for ( c = count; c-- != 0; )
  167.            {    if ( swap )
  168.                {    /* We have to take extra care if */
  169.                 /* bytes_per_line is odd. */
  170.                 if ( bytes_per_line & 1 )
  171.                    {    memswab2(src, dest, bytes_per_line - 1);
  172.                     dest[bytes_per_line - 1] =
  173.                       src[bytes_per_line];
  174.                    }
  175.                 else
  176.                     memswab2(src, dest, bytes_per_line);
  177.                }
  178.             else
  179.                 memcpy(dest, src, bytes_per_line);
  180.             src += mdev->raster;
  181.             dest += bytes_per_line;
  182.            }
  183.        }
  184.     return (swap && pad_to_word < 0 ? swap : 0);
  185. }
  186.  
  187. /* ------ Monochrome ------ */
  188.  
  189. /* Procedures */
  190. private dev_proc_copy_mono(mem_mono_copy_mono);
  191. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  192.  
  193. /* The device descriptor. */
  194. private gx_device_procs mem_mono_procs =
  195.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  196.     mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
  197.  
  198. /* The instance is public. */
  199. const gx_device_memory mem_mono_device =
  200.   mem_device("image(mono)", 1, mem_mono_procs);
  201.  
  202. /* Convert x coordinate to byte offset in scan line. */
  203. #define x_to_byte(x) ((x) >> 3)
  204.  
  205. /* Fill a rectangle with a color. */
  206. #undef chunk
  207. #define chunk mono_chunk
  208. private int
  209. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  210.   gx_color_index color)
  211. {    uint bit;
  212.     chunk right_mask;
  213.     byte fill;
  214.     declare_scan_ptr(dest);
  215.     fit_fill(dev, x, y, w, h);
  216.     setup_rect(dest);
  217. #define write_loop(stat)\
  218.  { int line_count = h;\
  219.    chunk *ptr = dest;\
  220.    do { stat; inc_chunk_ptr(ptr, draster); }\
  221.    while ( --line_count );\
  222.  }
  223. #define write_partial(msk)\
  224.    if ( fill ) write_loop(*ptr |= msk)\
  225.    else write_loop(*ptr &= ~msk)
  226.     switch ( color )
  227.        {
  228.     case 0: fill = mdev->invert; break;
  229.     case 1: fill = ~mdev->invert; break;
  230.     case gx_no_color_index: return 0;        /* transparent */
  231.     default: return -1;        /* invalid */
  232.        }
  233.     bit = x & chunk_bit_mask;
  234.     if ( bit + w <= chunk_bits )
  235.        {    /*
  236.          * Only one word. We have to split following statement
  237.          * because of a bug in the Xenix C compiler (it produces
  238.          * a signed rather than an unsigned shift if we don't
  239.          * split).
  240.          */
  241.         right_mask =
  242.           (w == chunk_bits ? chunk_all_bits : chunk_hi_bits(w));
  243.         right_mask >>= bit;
  244.        }
  245.     else
  246.        {    int byte_count;
  247.         if ( bit )
  248.            {    /* We have to split the following statement */
  249.             /* into two because of a bug in the DEC */
  250.             /* VAX/VMS C compiler. */
  251.             chunk mask = chunk_all_bits;
  252.             mask >>= bit;
  253.             write_partial(mask);
  254.             dest++;
  255.             w += bit - chunk_bits;
  256.            }
  257.         right_mask = chunk_hi_bits(w & chunk_bit_mask);
  258.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  259.            {    write_loop(memset(ptr, fill, byte_count));
  260.             inc_chunk_ptr(dest, byte_count);
  261.            }
  262.        }
  263.     if ( right_mask )
  264.         write_partial(right_mask);
  265.     return 0;
  266. }
  267.  
  268. /* Copy a monochrome bitmap. */
  269.  
  270. /* Fetch a chunk from the source. */
  271. /* Note that the source data are always stored big-endian. */
  272. /* Note also that the macros always cast cptr, */
  273. /* so it doesn't matter what the type of cptr is. */
  274. #undef chunk
  275. #if arch_is_big_endian
  276. #  define chunk uint
  277. #  define cfetch(cptr) (*(chunk *)(cptr))
  278. #else
  279. #  define chunk ushort
  280. #  define cfetch(cptr) (((chunk)*(byte *)(cptr) << 8) + ((byte *)(cptr))[1])
  281. #endif
  282. /* Fetch a chunk that straddles a chunk boundary. */
  283. /***
  284. #if arch_is_big_endian
  285. ***/
  286. #  define cfetch2(cptr, cskew, skew)\
  287.     ((cfetch(cptr) << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  288. /***
  289. #else
  290. #  define cfetch2(cptr, cskew, skew)\
  291.     (cskew <= 8 ?\
  292.      (cfetch(cptr) << cskew) + (((byte *)(cptr))[2] >> (skew - 8)) :\
  293.      (((byte *)(cptr))[1] << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  294. #endif
  295. ***/
  296.  
  297. /* copy_function and copy_shift get added together for dispatch */
  298. typedef enum {
  299.     copy_or = 0, copy_store, copy_and, copy_funny
  300. } copy_function;
  301. /* copy_right/left is not an enum, because compilers complain about */
  302. /* an enumeration clash when these are added to a copy_function. */
  303. #define copy_right ((copy_function)0)
  304. #define copy_left ((copy_function)4)
  305. typedef struct {
  306.     short invert;
  307.     ushort op;            /* copy_function */
  308. } copy_mode;
  309. /* Map from <c0,c1,invert> to copy_mode. */
  310. #define cm(i,op) { i, (ushort)op }
  311. private copy_mode copy_modes[9*2] = {
  312.     cm(-1, copy_funny),        /* NN */
  313.     cm(-1, copy_and),        /* N0 */
  314.     cm(0, copy_or),            /* N1 */
  315.     cm(0, copy_and),        /* 0N */
  316.     cm(0, copy_funny),        /* 00 */
  317.     cm(0, copy_store),        /* 01 */
  318.     cm(-1, copy_or),        /* 1N */
  319.     cm(-1, copy_store),        /* 10 */
  320.     cm(0, copy_funny),        /* 11 */
  321.     cm(-1, copy_funny),        /* NNi */
  322.     cm(0, copy_or),            /* N1i */
  323.     cm(-1, copy_and),        /* N0i */
  324.     cm(-1, copy_or),        /* 1Ni */
  325.     cm(0, copy_funny),        /* 11i */
  326.     cm(-1, copy_store),        /* 10i */
  327.     cm(0, copy_and),        /* 0Ni */
  328.     cm(0, copy_store),        /* 01i */
  329.     cm(0, copy_funny)        /* 00i */
  330. };
  331. private int
  332. mem_mono_copy_mono(gx_device *dev,
  333.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  334.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  335. {    register const byte *bptr;        /* actually chunk * */
  336.     int dbit, wleft;
  337.     uint mask;
  338.     copy_mode mode;
  339. #define function (copy_function)(mode.op)
  340.     declare_scan_ptr_as(dbptr, byte *);
  341. #define optr ((chunk *)dbptr)
  342.     register int skew;
  343.     register uint invert;
  344.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  345. #if gx_no_color_value != -1        /* hokey! */
  346.     if ( zero == gx_no_color_index ) zero = -1;
  347.     if ( one == gx_no_color_index ) one = -1;
  348. #endif
  349. #define izero (int)zero
  350. #define ione (int)one
  351.     mode =
  352.       copy_modes[(mdev->invert & 9) + izero + izero + izero + ione + 4];
  353. #undef izero
  354. #undef ione
  355.     invert = (uint)(int)mode.invert;    /* load register */
  356.     setup_rect_as(dbptr, byte *);
  357.     bptr = base + ((sourcex & ~chunk_bit_mask) >> 3);
  358.     dbit = x & chunk_bit_mask;
  359.     skew = dbit - (sourcex & chunk_bit_mask);
  360.     /* We have to split the following statement */
  361.     /* into two because of a bug in the DEC */
  362.     /* VAX/VMS C compiler. */
  363.     mask = chunk_all_bits;
  364.     mask >>= dbit;
  365. /* Macros for writing partial chunks. */
  366. /* The destination pointer is always named optr, */
  367. /* and must be declared as chunk *. */
  368. /* cinvert may be temporarily redefined. */
  369. #define cinvert(bits) ((bits) ^ invert)
  370. #define write_or_masked(bits, mask, off)\
  371.   optr[off] |= (cinvert(bits) & mask)
  372. #define write_store_masked(bits, mask, off)\
  373.   optr[off] = ((optr[off] & ~mask) | (cinvert(bits) & mask))
  374. #define write_and_masked(bits, mask, off)\
  375.   optr[off] &= (cinvert(bits) | ~mask)
  376. /* Macros for writing full chunks. */
  377. #define write_or(bits)  *optr |= cinvert(bits)
  378. #define write_store(bits) *optr = cinvert(bits)
  379. #define write_and(bits) *optr &= cinvert(bits)
  380. /* Macro for incrementing to next chunk. */
  381. #define next_x_chunk\
  382.   bptr += chunk_bytes; dbptr += chunk_bytes
  383. /* Common macro for the end of each scan line. */
  384. #define end_y_loop(sdelta, ddelta)\
  385.   if ( --h == 0 ) break;\
  386.   bptr += sdelta; dbptr += ddelta
  387.     if ( (wleft = w + dbit - chunk_bits) <= 0 )
  388.        {    /* The entire operation fits in one (destination) chunk. */
  389.         /* Some machines can't handle w == chunk_bits! */
  390. #if arch_cant_shift_full_chunk
  391.         if ( w != chunk_bits )
  392. #endif
  393.           mask -= mask >> w;
  394. #define write_single(wr_op, src)\
  395.   for ( ; ; )\
  396.    { wr_op(src, mask, 0);\
  397.      end_y_loop(sraster, draster);\
  398.    }
  399. #define write1_loop(src)\
  400.   switch ( function ) {\
  401.     case copy_or: write_single(write_or_masked, src); break;\
  402.     case copy_store: write_single(write_store_masked, src); break;\
  403.     case copy_and: write_single(write_and_masked, src); break;\
  404.     default: goto funny;\
  405.   }
  406.         if ( skew >= 0 )    /* single -> single, right/no shift */
  407.            {    write1_loop(cfetch(bptr) >> skew);
  408.            }
  409.         else if ( wleft <= skew )    /* single -> single, left shift */
  410.            {    skew = -skew;
  411.             write1_loop(cfetch(bptr) << skew);
  412.            }
  413.         else            /* double -> single */
  414.            {    int cskew = -skew;
  415.             skew += chunk_bits;
  416.             write1_loop(cfetch2(bptr, cskew, skew));
  417.            }
  418. #undef write1_loop
  419. #undef write_single
  420.        }
  421.     else if ( wleft <= skew )
  422.        {    /* 1 source chunk -> 2 destination chunks. */
  423.         /* This is an important special case for */
  424.         /* both characters and halftone tiles. */
  425.         register uint bits;
  426.         uint rmask = chunk_hi_bits(wleft);
  427.         int cskew = chunk_bits - skew;
  428. #define write_1to2(wr_op)\
  429.   for ( ; ; )\
  430.    { bits = cfetch(bptr) ^ invert;\
  431.      wr_op(bits >> skew, mask, 0);\
  432.      wr_op(bits << cskew, rmask, 1);\
  433.      end_y_loop(sraster, draster);\
  434.    }
  435. #undef cinvert
  436. #define cinvert(bits) (bits)        /* pre-inverted here */
  437.         switch ( function )
  438.            {
  439.         case copy_or: write_1to2(write_or_masked); break;
  440.         case copy_store: write_1to2(write_store_masked); break;
  441.         case copy_and: write_1to2(write_and_masked); break;
  442.         default: goto funny;
  443.            }
  444. #undef cinvert
  445. #define cinvert(bits) ((bits) ^ invert)
  446. #undef write_1to2
  447.        }
  448.     else
  449.        {    /* More than one source chunk and more than one */
  450.         /* destination chunk are involved. */
  451.         uint rmask = chunk_hi_bits(wleft & chunk_bit_mask);
  452.         int words = (wleft & ~chunk_bit_mask) >> 3;
  453.         uint sskip = sraster - words;
  454.         uint dskip = draster - words;
  455.         register uint bits;
  456.         if ( skew == 0 )    /* optimize the aligned case */
  457.            {
  458. #define write_aligned(wr_op, wr_op_masked)\
  459.   for ( ; ; )\
  460.    { int count = wleft;\
  461.      /* Do first partial chunk. */\
  462.      wr_op_masked(cfetch(bptr), mask, 0);\
  463.      /* Do full chunks. */\
  464.      while ( (count -= chunk_bits) >= 0 )\
  465.       { next_x_chunk; wr_op(cfetch(bptr)); }\
  466.      /* Do last chunk */\
  467.      if ( count > -chunk_bits )\
  468.       { wr_op_masked(cfetch(bptr + chunk_bytes), rmask, 1); }\
  469.      end_y_loop(sskip, dskip);\
  470.    }
  471.             switch ( function )
  472.               {
  473.               case copy_or:
  474.                 write_aligned(write_or, write_or_masked);
  475.                 break;
  476.               case copy_store:
  477.                 write_aligned(write_store, write_store_masked);
  478.                 break;
  479.               case copy_and:
  480.                 write_aligned(write_and, write_and_masked);
  481.                 break;
  482.               default:
  483.                 goto funny;
  484.               }
  485. #undef write_aligned
  486.            }
  487.         else            /* not aligned */
  488.            {    int ccase =
  489.               (skew >= 0 ? copy_right :
  490.                ((bptr += chunk_bytes), copy_left))
  491.               + (int)function;
  492.             int cskew = -skew & chunk_bit_mask;
  493.             skew &= chunk_bit_mask;
  494.             for ( ; ; )
  495.                {    int count = wleft;
  496. #define prefetch_right\
  497.   bits = cfetch(bptr) >> skew
  498. #define prefetch_left\
  499.   bits = cfetch2(bptr - chunk_bytes, cskew, skew)
  500. #define write_unaligned(wr_op, wr_op_masked)\
  501.   wr_op_masked(bits, mask, 0);\
  502.   /* Do full chunks. */\
  503.   while ( count >= chunk_bits )\
  504.     { bits = cfetch2(bptr, cskew, skew);\
  505.       next_x_chunk; wr_op(bits); count -= chunk_bits;\
  506.     }\
  507.   /* Do last chunk */\
  508.   if ( count > 0 )\
  509.     { bits = cfetch(bptr) << cskew;\
  510.       if ( count > skew ) bits += cfetch(bptr + chunk_bytes) >> skew;\
  511.       wr_op_masked(bits, rmask, 1);\
  512.     }
  513.                 switch ( ccase )
  514.                   {
  515.                   case copy_or + copy_left:
  516.                     prefetch_left; goto uor;
  517.                   case copy_or + copy_right:
  518.                     prefetch_right;
  519. uor:                    write_unaligned(write_or, write_or_masked);
  520.                     break;
  521.                   case copy_store + copy_left:
  522.                     prefetch_left; goto ustore;
  523.                   case copy_store + copy_right:
  524.                     prefetch_right;
  525. ustore:                    write_unaligned(write_store, write_store_masked);
  526.                     break;
  527.                   case copy_and + copy_left:
  528.                     prefetch_left; goto uand;
  529.                   case copy_and + copy_right:
  530.                     prefetch_right;
  531. uand:                    write_unaligned(write_and, write_and_masked);
  532.                     break;
  533.                   default:
  534.                     goto funny;
  535.                   }
  536.                 end_y_loop(sskip, dskip);
  537. #undef write_unaligned
  538. #undef prefetch_left
  539. #undef prefetch_right
  540.                }
  541.            }
  542.        }
  543. #undef end_y_loop
  544. #undef next_x_chunk
  545.     return 0;
  546.     /* Handle the funny cases that aren't supposed to happen. */
  547. funny:    return (invert ? -1 : mem_mono_fill_rectangle(dev, x, y, w, h, zero));
  548. #undef optr
  549. }
  550.