home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mfb / mfbpixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-28  |  6.9 KB  |  263 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: mfbpixmap.c,v 5.5 89/07/28 11:59:59 rws Exp $ */
  25.  
  26. /* pixmap management
  27.    written by drewry, september 1986
  28.  
  29.    on a monchrome device, a pixmap is a bitmap.
  30. */
  31.  
  32. #include "Xmd.h"
  33. #include "pixmapstr.h"
  34. #include "maskbits.h"
  35.  
  36. #include "mfb.h"
  37. #include "mi.h"
  38.  
  39. #include "servermd.h"
  40.  
  41. PixmapPtr
  42. mfbCreatePixmap (pScreen, width, height, depth)
  43.     ScreenPtr    pScreen;
  44.     int        width;
  45.     int        height;
  46.     int        depth;
  47. {
  48.     register PixmapPtr pPixmap;
  49.     int size;
  50.  
  51.     if (depth != 1)
  52.     return NullPixmap;
  53.  
  54.     size = PixmapBytePad(width, 1);
  55.     pPixmap = (PixmapPtr)xalloc(sizeof(PixmapRec) + (height * size));
  56.     if (!pPixmap)
  57.     return NullPixmap;
  58.     pPixmap->drawable.type = DRAWABLE_PIXMAP;
  59.     pPixmap->drawable.class = 0;
  60.     pPixmap->drawable.pScreen = pScreen;
  61.     pPixmap->drawable.depth = 1;
  62.     pPixmap->drawable.bitsPerPixel = 1;
  63.     pPixmap->drawable.id = 0;
  64.     pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  65.     pPixmap->drawable.x = 0;
  66.     pPixmap->drawable.y = 0;
  67.     pPixmap->drawable.width = width;
  68.     pPixmap->drawable.height = height;
  69.     pPixmap->devKind = size;
  70.     pPixmap->refcnt = 1;
  71.     pPixmap->devPrivate.ptr = (pointer)(pPixmap + 1);
  72.     return pPixmap;
  73. }
  74.  
  75. Bool
  76. mfbDestroyPixmap(pPixmap)
  77.     PixmapPtr pPixmap;
  78. {
  79.     if(--pPixmap->refcnt)
  80.     return TRUE;
  81.     xfree(pPixmap);
  82.     return TRUE;
  83. }
  84.  
  85.  
  86. PixmapPtr
  87. mfbCopyPixmap(pSrc)
  88.     register PixmapPtr    pSrc;
  89. {
  90.     register PixmapPtr    pDst;
  91.     int        size;
  92.  
  93.     size = pSrc->drawable.height * pSrc->devKind;
  94.     pDst = (PixmapPtr)xalloc(sizeof(PixmapRec) + size);
  95.     if (!pDst)
  96.     return NullPixmap;
  97.     pDst->drawable = pSrc->drawable;
  98.     pDst->drawable.id = 0;
  99.     pDst->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  100.     pDst->devKind = pSrc->devKind;
  101.     pDst->refcnt = 1;
  102.     pDst->devPrivate.ptr = (pointer)(pDst + 1);
  103.     bcopy((char *)pSrc->devPrivate.ptr, (char *)pDst->devPrivate.ptr, size);
  104.     return pDst;
  105. }
  106.  
  107.  
  108. /* replicates a pattern to be a full 32 bits wide.
  109.    relies on the fact that each scnaline is longword padded.
  110.    doesn't do anything if pixmap is not a factor of 32 wide.
  111.    changes width field of pixmap if successful, so that the fast
  112.     XRotatePixmap code gets used if we rotate the pixmap later.
  113.  
  114.    calculate number of times to repeat
  115.    for each scanline of pattern
  116.       zero out area to be filled with replicate
  117.       left shift and or in original as many times as needed
  118. */
  119. void
  120. mfbPadPixmap(pPixmap)
  121.     PixmapPtr pPixmap;
  122. {
  123.     register int width = pPixmap->drawable.width;
  124.     register int h;
  125.     register int mask;
  126.     register unsigned int *p;
  127.     register unsigned int bits;    /* real pattern bits */
  128.     register int i;
  129.     int rep;            /* repeat count for pattern */
  130.  
  131.     if (width >= 32)
  132.     return;
  133.  
  134.     rep = 32/width;
  135.     if (rep*width != 32)
  136.     return;
  137.  
  138.     mask = endtab[width];
  139.  
  140.     p = (unsigned int *)(pPixmap->devPrivate.ptr);
  141.     for (h=0; h < pPixmap->drawable.height; h++)
  142.     {
  143.     *p &= mask;
  144.     bits = *p;
  145.     for(i=1; i<rep; i++)
  146.     {
  147.         bits = SCRRIGHT(bits, width);
  148.         *p |= bits;
  149.     }
  150.     p++;
  151.     }
  152.     pPixmap->drawable.width = 32;
  153. }
  154.  
  155. /* Rotates pixmap pPix by w pixels to the right on the screen. Assumes that
  156.  * words are 32 bits wide, and that the least significant bit appears on the
  157.  * left.
  158.  */
  159. void
  160. mfbXRotatePixmap(pPix, rw)
  161.     PixmapPtr    pPix;
  162.     register int rw;
  163. {
  164.     register long    *pw, *pwFinal;
  165.     register unsigned long    t;
  166.  
  167.     if (pPix == NullPixmap)
  168.         return;
  169.  
  170.     pw = (long *)pPix->devPrivate.ptr;
  171.     rw %= (int)pPix->drawable.width;
  172.     if (rw < 0)
  173.     rw += (int)pPix->drawable.width;
  174.     if(pPix->drawable.width == 32)
  175.     {
  176.         pwFinal = pw + pPix->drawable.height;
  177.     while(pw < pwFinal)
  178.     {
  179.         t = *pw;
  180.         *pw++ = SCRRIGHT(t, rw) | 
  181.             (SCRLEFT(t, (32-rw)) & endtab[rw]);
  182.     }
  183.     }
  184.     else
  185.     {
  186.     /* We no longer do this.  Validate doesn't try to rotate odd-size
  187.      * tiles or stipples.  mfbUnnatural<tile/stipple>FS works directly off
  188.      * the unrotate tile/stipple in the GC
  189.      */
  190.         ErrorF("X internal error: trying to rotate odd-sized pixmap.\n");
  191.     }
  192.  
  193. }
  194.  
  195. /* Rotates pixmap pPix by h lines.  Assumes that h is always less than
  196.    pPix->height
  197.    works on any width.
  198.  */
  199. void
  200. mfbYRotatePixmap(pPix, rh)
  201.     register PixmapPtr    pPix;
  202.     int    rh;
  203. {
  204.     int nbyDown;    /* bytes to move down to row 0; also offset of
  205.                row rh */
  206.     int nbyUp;        /* bytes to move up to line rh; also
  207.                offset of first line moved down to 0 */
  208.     char *pbase;
  209.     char *ptmp;
  210.     int    height;
  211.  
  212.     if (pPix == NullPixmap)
  213.     return;
  214.     height = (int) pPix->drawable.height;
  215.     rh %= height;
  216.     if (rh < 0)
  217.     rh += height;
  218.  
  219.     pbase = (char *)pPix->devPrivate.ptr;
  220.  
  221.     nbyDown = rh * pPix->devKind;
  222.     nbyUp = (pPix->devKind * height) - nbyDown;
  223.     if(!(ptmp = (char *)ALLOCATE_LOCAL(nbyUp)))
  224.     return;
  225.  
  226.     bcopy(pbase, ptmp, nbyUp);        /* save the low rows */
  227.     bcopy(pbase+nbyUp, pbase, nbyDown);    /* slide the top rows down */
  228.     bcopy(ptmp, pbase+nbyDown, nbyUp);    /* move lower rows up to row rh */
  229.     DEALLOCATE_LOCAL(ptmp);
  230. }
  231.  
  232. void
  233. mfbCopyRotatePixmap(psrcPix, ppdstPix, xrot, yrot)
  234.     register PixmapPtr psrcPix, *ppdstPix;
  235.     int    xrot, yrot;
  236. {
  237.     register PixmapPtr pdstPix;
  238.  
  239.     if ((pdstPix = *ppdstPix) &&
  240.     (pdstPix->devKind == psrcPix->devKind) &&
  241.     (pdstPix->drawable.height == psrcPix->drawable.height))
  242.     {
  243.     bcopy((char *)psrcPix->devPrivate.ptr,
  244.           (char *)pdstPix->devPrivate.ptr,
  245.           psrcPix->drawable.height * psrcPix->devKind);
  246.     pdstPix->drawable.width = psrcPix->drawable.width;
  247.     pdstPix->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  248.     }
  249.     else
  250.     {
  251.     if (pdstPix)
  252.         mfbDestroyPixmap(pdstPix);
  253.     *ppdstPix = pdstPix = mfbCopyPixmap(psrcPix);
  254.     if (!pdstPix)
  255.         return;
  256.     }
  257.     mfbPadPixmap(pdstPix);
  258.     if (xrot)
  259.     mfbXRotatePixmap(pdstPix, xrot);
  260.     if (yrot)
  261.     mfbYRotatePixmap(pdstPix, yrot);
  262. }
  263.