home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / cfb / cfbpixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-18  |  10.6 KB  |  420 lines

  1. /* $XConsortium: cfbpixmap.c,v 5.7 91/07/18 23:36:46 keith Exp $ */
  2. /***********************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25. /* pixmap management
  26.    written by drewry, september 1986
  27.  
  28.    on a monchrome device, a pixmap is a bitmap.
  29. */
  30.  
  31. #include "Xmd.h"
  32. #include "servermd.h"
  33. #include "pixmapstr.h"
  34. #include "cfbmskbits.h"
  35.  
  36. #include "cfb.h"
  37. #include "mi.h"
  38.  
  39. extern void mfbXRotatePixmap(), mfbYRotatePixmap();
  40.  
  41. #if (BITMAP_BIT_ORDER == MSBFirst)
  42. static int masktab[32] = 
  43.     {
  44.         0x00000000,
  45.         0x80000000,
  46.         0xC0000000,
  47.         0xE0000000,
  48.         0xF0000000,
  49.         0xF8000000,
  50.         0xFC000000,
  51.         0xFE000000,
  52.         0xFF000000,
  53.         0xFF800000,
  54.         0xFFC00000,
  55.         0xFFE00000,
  56.         0xFFF00000,
  57.         0xFFF80000,
  58.         0xFFFC0000,
  59.         0xFFFE0000,
  60.         0xFFFF0000,
  61.         0xFFFF8000,
  62.         0xFFFFC000,
  63.         0xFFFFE000,
  64.         0xFFFFF000,
  65.         0xFFFFF800,
  66.         0xFFFFFC00,
  67.         0xFFFFFE00,
  68.         0xFFFFFF00,
  69.         0xFFFFFF80,
  70.         0xFFFFFFC0,
  71.         0xFFFFFFE0,
  72.         0xFFFFFFF0,
  73.         0xFFFFFFF8,
  74.         0xFFFFFFFC,
  75.         0xFFFFFFFE
  76.     };
  77. #else
  78. static int masktab[32] =
  79.         {
  80.         0x00000000,
  81.         0x00000001,
  82.         0x00000003,
  83.         0x00000007,
  84.         0x0000000F,
  85.         0x0000001F,
  86.         0x0000003F,
  87.         0x0000007F,
  88.         0x000000FF,
  89.         0x000001FF,
  90.         0x000003FF,
  91.         0x000007FF,
  92.         0x00000FFF,
  93.         0x00001FFF,
  94.         0x00003FFF,
  95.         0x00007FFF,
  96.         0x0000FFFF,
  97.         0x0001FFFF,
  98.         0x0003FFFF,
  99.         0x0007FFFF,
  100.         0x000FFFFF,
  101.         0x001FFFFF,
  102.         0x003FFFFF,
  103.         0x007FFFFF,
  104.         0x00FFFFFF,
  105.         0x01FFFFFF,
  106.         0x03FFFFFF,
  107.         0x07FFFFFF,
  108.         0x0FFFFFFF,
  109.         0x1FFFFFFF,
  110.         0x3FFFFFFF,
  111.         0x7FFFFFFF
  112.         };
  113. #endif
  114.  
  115. PixmapPtr
  116. cfbCreatePixmap (pScreen, width, height, bitsPerPixel)
  117.     ScreenPtr    pScreen;
  118.     int        width;
  119.     int        height;
  120.     int        bitsPerPixel;
  121. {
  122.     register PixmapPtr pPixmap;
  123.     int size;
  124.  
  125.     if (bitsPerPixel != 1 && bitsPerPixel != PSZ)
  126.     return NullPixmap;
  127.  
  128.     size = PixmapBytePad(width, bitsPerPixel);
  129.     pPixmap = (PixmapPtr)xalloc(sizeof(PixmapRec) + (height * size));
  130.     if (!pPixmap)
  131.     return NullPixmap;
  132.     pPixmap->drawable.type = DRAWABLE_PIXMAP;
  133.     pPixmap->drawable.class = 0;
  134.     pPixmap->drawable.pScreen = pScreen;
  135.     pPixmap->drawable.depth = bitsPerPixel;
  136.     pPixmap->drawable.bitsPerPixel = bitsPerPixel;
  137.     pPixmap->drawable.id = 0;
  138.     pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  139.     pPixmap->drawable.x = 0;
  140.     pPixmap->drawable.y = 0;
  141.     pPixmap->drawable.width = width;
  142.     pPixmap->drawable.height = height;
  143.     pPixmap->devKind = size;
  144.     pPixmap->refcnt = 1;
  145.     pPixmap->devPrivate.ptr = (pointer)(pPixmap + 1);
  146.     return pPixmap;
  147. }
  148.  
  149. Bool
  150. cfbDestroyPixmap(pPixmap)
  151.     PixmapPtr pPixmap;
  152. {
  153.     if(--pPixmap->refcnt)
  154.     return TRUE;
  155.     xfree(pPixmap);
  156.     return TRUE;
  157. }
  158.  
  159. PixmapPtr
  160. cfbCopyPixmap(pSrc)
  161.     register PixmapPtr    pSrc;
  162. {
  163.     register PixmapPtr    pDst;
  164.     int        size;
  165.  
  166.     size = pSrc->drawable.height * pSrc->devKind;
  167.     pDst = (PixmapPtr) xalloc(sizeof(PixmapRec) + size);
  168.     if (!pDst)
  169.     return NullPixmap;
  170.     pDst->drawable = pSrc->drawable;
  171.     pDst->drawable.id = 0;
  172.     pDst->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  173.     pDst->devKind = pSrc->devKind;
  174.     pDst->refcnt = 1;
  175.     pDst->devPrivate.ptr = (pointer)(pDst + 1);
  176.     bcopy((char *)pSrc->devPrivate.ptr, (char *)pDst->devPrivate.ptr, size);
  177.     return pDst;
  178. }
  179.  
  180.  
  181. /* replicates a pattern to be a full 32 bits wide.
  182.    relies on the fact that each scnaline is longword padded.
  183.    doesn't do anything if pixmap is not a factor of 32 wide.
  184.    changes width field of pixmap if successful, so that the fast
  185.     cfbXRotatePixmap code gets used if we rotate the pixmap later.
  186.     cfbYRotatePixmap code gets used if we rotate the pixmap later.
  187.  
  188.    calculate number of times to repeat
  189.    for each scanline of pattern
  190.       zero out area to be filled with replicate
  191.       left shift and or in original as many times as needed
  192. */
  193. void
  194. cfbPadPixmap(pPixmap)
  195.     PixmapPtr pPixmap;
  196. {
  197.     register int width = (pPixmap->drawable.width) * (pPixmap->drawable.bitsPerPixel);
  198.     register int h;
  199.     register int mask;
  200.     register unsigned int *p;
  201.     register unsigned int bits; /* real pattern bits */
  202.     register int i;
  203.     int rep;                    /* repeat count for pattern */
  204.  
  205.     if (width >= 32)
  206.         return;
  207.  
  208.     rep = 32/width;
  209.     if (rep*width != 32)
  210.         return;
  211.  
  212.     mask = masktab[width];
  213.  
  214.     p = (unsigned int *)(pPixmap->devPrivate.ptr);
  215.     for (h=0; h < pPixmap->drawable.height; h++)
  216.     {
  217.         *p &= mask;
  218.         bits = *p;
  219.         for(i=1; i<rep; i++)
  220.         {
  221. #if (BITMAP_BIT_ORDER == MSBFirst) 
  222.             bits >>= width;
  223. #else
  224.         bits <<= width;
  225. #endif
  226.             *p |= bits;
  227.         }
  228.         p++;
  229.     }    
  230.     pPixmap->drawable.width = 32/(pPixmap->drawable.bitsPerPixel);
  231. }
  232.  
  233.  
  234. #ifdef notdef
  235. /*
  236.  * cfb debugging routine -- assumes pixmap is 1 byte deep 
  237.  */
  238. static cfbdumppixmap(pPix)
  239.     PixmapPtr    pPix;
  240. {
  241.     unsigned int *pw;
  242.     char *psrc, *pdst;
  243.     int    i, j;
  244.     char    line[66];
  245.  
  246.     ErrorF(  "pPixmap: 0x%x\n", pPix);
  247.     ErrorF(  "%d wide %d high\n", pPix->drawable.width, pPix->drawable.height);
  248.     if (pPix->drawable.width > 64)
  249.     {
  250.     ErrorF(  "too wide to see\n");
  251.     return;
  252.     }
  253.  
  254.     pw = (unsigned int *) pPix->devPrivate.ptr;
  255.     psrc = (char *) pw;
  256.  
  257. /*
  258.     for ( i=0; i<pPix->drawable.height; ++i )
  259.     ErrorF( "0x%x\n", pw[i] );
  260. */
  261.  
  262.     for ( i = 0; i < pPix->drawable.height; ++i ) {
  263.     pdst = line;
  264.     for(j = 0; j < pPix->drawable.width; j++) {
  265.         *pdst++ = *psrc++ ? 'X' : ' ' ;
  266.     }
  267.     *pdst++ = '\n';
  268.     *pdst++ = '\0';
  269.     ErrorF( "%s", line);
  270.     }
  271. }
  272. #endif /* notdef */
  273.  
  274. /* Rotates pixmap pPix by w pixels to the right on the screen. Assumes that
  275.  * words are 32 bits wide, and that the least significant bit appears on the
  276.  * left.
  277.  */
  278. void
  279. cfbXRotatePixmap(pPix, rw)
  280.     PixmapPtr    pPix;
  281.     register int rw;
  282. {
  283.     register unsigned int    *pw, *pwFinal;
  284.     register unsigned int    t;
  285.     int                rot;
  286.  
  287.     if (pPix == NullPixmap)
  288.         return;
  289.  
  290.     switch (((DrawablePtr) pPix)->bitsPerPixel) {
  291.     case PSZ:
  292.         break;
  293.     case 1:
  294.         mfbXRotatePixmap(pPix, rw);
  295.         return;
  296.     default:
  297.         ErrorF("cfbXRotatePixmap: unsupported bitsPerPixel %d\n", ((DrawablePtr) pPix)->bitsPerPixel);
  298.         return;
  299.     }
  300.     pw = (unsigned int *)pPix->devPrivate.ptr;
  301.     modulus (rw, (int) pPix->drawable.width, rot);
  302.     if(pPix->drawable.width == PPW)
  303.     {
  304.         pwFinal = pw + pPix->drawable.height;
  305.     while(pw < pwFinal)
  306.     {
  307.         t = *pw;
  308.         *pw++ = SCRRIGHT(t, rot) |
  309.             (SCRLEFT(t, (PPW-rot)) & cfbendtab[rot]);
  310.     }
  311.     }
  312.     else
  313.     {
  314.         ErrorF("cfb internal error: trying to rotate odd-sized pixmap.\n");
  315. #ifdef notdef
  316.     register unsigned int *pwTmp;
  317.     int size, tsize;
  318.  
  319.     tsize = PixmapBytePad(pPix->drawable.width - rot, PSZ);
  320.     pwTmp = (unsigned int *) ALLOCATE_LOCAL(pPix->drawable.height * tsize);
  321.     if (!pwTmp)
  322.         return;
  323.     /* divide pw (the pixmap) in two vertically at (w - rot) and swap */
  324.     tsize >>= 2;
  325.     size = pPix->devKind >> 2;
  326.     cfbQuickBlt((int *)pw, (int *)pwTmp,
  327.             0, 0, 0, 0,
  328.             (int)pPix->drawable.width - rot, (int)pPix->drawable.height,
  329.             size, tsize);
  330.     cfbQuickBlt((int *)pw, (int *)pw,
  331.             (int)pPix->drawable.width - rot, 0, 0, 0,
  332.             rot, (int)pPix->drawable.height,
  333.             size, size);
  334.     cfbQuickBlt((int *)pwTmp, (int *)pw,
  335.             0, 0, rot, 0,
  336.             (int)pPix->drawable.width - rot, (int)pPix->drawable.height,
  337.             tsize, size);
  338.     DEALLOCATE_LOCAL(pwTmp);
  339. #endif
  340.     }
  341. }
  342.  
  343. /* Rotates pixmap pPix by h lines.  Assumes that h is always less than
  344.    pPix->drawable.height
  345.    works on any width.
  346.  */
  347. void
  348. cfbYRotatePixmap(pPix, rh)
  349.     register PixmapPtr    pPix;
  350.     int    rh;
  351. {
  352.     int nbyDown;    /* bytes to move down to row 0; also offset of
  353.                row rh */
  354.     int nbyUp;        /* bytes to move up to line rh; also
  355.                offset of first line moved down to 0 */
  356.     char *pbase;
  357.     char *ptmp;
  358.     int    rot;
  359.  
  360.     if (pPix == NullPixmap)
  361.     return;
  362.     switch (((DrawablePtr) pPix)->bitsPerPixel) {
  363.     case PSZ:
  364.         break;
  365.     case 1:
  366.         mfbYRotatePixmap(pPix, rh);
  367.         return;
  368.     default:
  369.         ErrorF("cfbYRotatePixmap: unsupported bitsPerPixel %d\n", ((DrawablePtr) pPix)->bitsPerPixel);
  370.         return;
  371.     }
  372.  
  373.     modulus (rh, (int) pPix->drawable.height, rot);
  374.     pbase = (char *)pPix->devPrivate.ptr;
  375.  
  376.     nbyDown = rot * pPix->devKind;
  377.     nbyUp = (pPix->devKind * pPix->drawable.height) - nbyDown;
  378.     if(!(ptmp = (char *)ALLOCATE_LOCAL(nbyUp)))
  379.     return;
  380.  
  381.     bcopy(pbase, ptmp, nbyUp);        /* save the low rows */
  382.     bcopy(pbase+nbyUp, pbase, nbyDown);    /* slide the top rows down */
  383.     bcopy(ptmp, pbase+nbyDown, nbyUp);    /* move lower rows up to row rot */
  384.     DEALLOCATE_LOCAL(ptmp);
  385. }
  386.  
  387. void
  388. cfbCopyRotatePixmap(psrcPix, ppdstPix, xrot, yrot)
  389.     register PixmapPtr psrcPix, *ppdstPix;
  390.     int    xrot, yrot;
  391. {
  392.     register PixmapPtr pdstPix;
  393.  
  394.     if ((pdstPix = *ppdstPix) &&
  395.     (pdstPix->devKind == psrcPix->devKind) &&
  396.     (pdstPix->drawable.height == psrcPix->drawable.height))
  397.     {
  398.     bcopy((char *)psrcPix->devPrivate.ptr,
  399.           (char *)pdstPix->devPrivate.ptr,
  400.           psrcPix->drawable.height * psrcPix->devKind);
  401.     pdstPix->drawable.width = psrcPix->drawable.width;
  402.     pdstPix->drawable.depth = psrcPix->drawable.depth;
  403.     pdstPix->drawable.bitsPerPixel = psrcPix->drawable.bitsPerPixel;
  404.     pdstPix->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  405.     }
  406.     else
  407.     {
  408.     if (pdstPix)
  409.         cfbDestroyPixmap(pdstPix);
  410.     *ppdstPix = pdstPix = cfbCopyPixmap(psrcPix);
  411.     if (!pdstPix)
  412.         return;
  413.     }
  414.     cfbPadPixmap(pdstPix);
  415.     if (xrot)
  416.     cfbXRotatePixmap(pdstPix, xrot);
  417.     if (yrot)
  418.     cfbYRotatePixmap(pdstPix, yrot);
  419. }
  420.