home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / dec / qdss / qdpixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-19  |  5.4 KB  |  198 lines

  1. /***********************************************************
  2. Copyright 1987, 1988 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.  
  25. #include "X.h"
  26. #include "miscstruct.h"
  27. #include "pixmapstr.h"
  28. #include "region.h"
  29. #include "mistruct.h"
  30. #include "gc.h"        /* for NEXT_SERIAL_NUMBER */
  31.  
  32. #include "Xmd.h"
  33. #include "servermd.h"
  34.  
  35. #include "qd.h"
  36.  
  37. extern    int    Nchannels;
  38.  
  39. int PixmapUseOffscreen = 7;
  40. /* masks:
  41.  * 1 - use new gc code (instea of mfbCreateGC)
  42.  * 2 - use offscreen for bitmaps
  43.  * 4 - use offscreen for pixmaps
  44.  */
  45.  
  46. PixmapPtr
  47. qdCreatePixmap( pScreen, width, height, depth)
  48.     ScreenPtr pScreen;
  49.     int width;
  50.     int height;
  51.     int depth;
  52. {
  53.     register QDPixPtr qdpix;
  54.     int        nbytes;
  55.  
  56.  /* NOTE: the resulting PixMap must be compatible with mfb pixmaps! */
  57.  
  58.     /*
  59.      * allocate the generic and the private pixmap structs in one chunk
  60.      */
  61.     qdpix = (QDPixPtr) Xalloc( sizeof(QDPixRec));
  62.     if ( !qdpix)
  63.     return (PixmapPtr)NULL;
  64.  
  65.     qdpix->pixmap.drawable.type = DRAWABLE_PIXMAP;
  66.     qdpix->pixmap.drawable.pScreen = pScreen;
  67.     qdpix->pixmap.drawable.depth = depth;
  68.     qdpix->pixmap.drawable.class = 0;
  69.     qdpix->pixmap.drawable.bitsPerPixel = depth == 4 ? 8 : depth;
  70.     qdpix->pixmap.drawable.id = 0;
  71.     qdpix->pixmap.drawable.serialNumber = NEXT_SERIAL_NUMBER;
  72.     QDPIX_WIDTH(&qdpix->pixmap) = width;
  73.     QDPIX_HEIGHT(&qdpix->pixmap) = height;
  74.     qdpix->pixmap.refcnt = 1;
  75.  
  76.     QDPIX_X(qdpix) = 0;
  77.     QDPIX_Y(qdpix) = 0;
  78.  
  79.     nbytes = depth > 1 ? width * Nchannels : PixmapBytePad(width, 1);
  80.     qdpix->pixmap.devKind = nbytes;
  81.     if (PixmapUseOffscreen & (depth > 1 ? 4 : 2)) {
  82.     QD_PIX_DATA(&qdpix->pixmap) = NULL;
  83.     if (tlConfirmPixmap(qdpix))
  84.         return (PixmapPtr)qdpix;
  85.     }
  86.     qdpix->planes = 0;
  87.     nbytes *= height;
  88.     if (   nbytes <= 0
  89.     || (QD_PIX_DATA(&qdpix->pixmap) = (unsigned char *) Xalloc( nbytes)) == NULL)
  90.     {
  91.         Xfree( qdpix);
  92.     return (PixmapPtr)NULL;
  93.     }
  94.     return (PixmapPtr)qdpix;
  95. }
  96.  
  97. /*
  98.  * DIX assumes DestroyPixmap may be called with NULL arg
  99.  */
  100. Bool
  101. qdDestroyPixmap( pPixmap)
  102.     PixmapPtr pPixmap;
  103. {
  104.     if ( --pPixmap->refcnt != 0)
  105.     return TRUE;
  106.  
  107.     if (QD_PIX_DATA(pPixmap) != NULL)
  108.     Xfree(QD_PIX_DATA(pPixmap));
  109.     QD_PIX_DATA(pPixmap) = (unsigned char*)1; /* hack to prevent copying */
  110.     tlCancelPixmap((QDPixPtr)pPixmap);
  111.     Xfree(pPixmap);
  112.     return TRUE;
  113. }
  114.  
  115.  
  116. static int
  117. sizeQDdata( pPix)
  118.     PixmapPtr    pPix;
  119. {
  120.     switch ( pPix->drawable.depth)
  121.     {
  122.       case 1:
  123.     return PixmapWidthInPadUnits( QDPIX_WIDTH(pPix), 1);
  124.       case NPLANES:
  125.       default:
  126.     return QDPIX_WIDTH(pPix) * QDPIX_HEIGHT(pPix) * Nchannels;
  127.     }
  128. }
  129.  
  130. static
  131. PixmapPtr
  132. QD1toNplanes( pBit, fgpixel, bgpixel)
  133.     PixmapPtr   pBit;
  134.     int        fgpixel, bgpixel;
  135. {
  136.     register unsigned char *    newbytes;
  137.     register unsigned char *    oldbits = QD_PIX_DATA(pBit);
  138.     register int    r, c;
  139.     register int    pixel;
  140.     
  141.  
  142.     newbytes = (unsigned char *) Xalloc( sizeQDdata( pBit));
  143.     /*
  144.      * Now expand the bitmap and two colors into the full-depth pixmap
  145.      */
  146.     for ( r=0; r < QDPIX_HEIGHT(pBit); r++)
  147.     for ( c=0; c < QDPIX_WIDTH(pBit); c++)
  148.     {
  149.         pixel = oldbits[ r*QPPADBYTES( QDPIX_WIDTH(pBit)) + (c>>3)] & 1<<(c&0x7)
  150.         ? fgpixel
  151.         : bgpixel;
  152.         newbytes[ r * QDPIX_WIDTH(pBit) + c] = pixel;
  153. #if NPLANES==24
  154.         newbytes[ (QDPIX_HEIGHT(pBit)+r)*QDPIX_WIDTH(pBit) + c] = pixel >> 8;
  155.         newbytes[ ((QDPIX_HEIGHT(pBit)<<1)+r)*QDPIX_WIDTH(pBit) + c] = pixel >> 16;
  156. #endif
  157.     }
  158.  
  159.     Xfree( QD_PIX_DATA(pBit));
  160.     QD_PIX_DATA(pBit) = newbytes;
  161.     return pBit;    /* now a PixMap */
  162. }
  163.  
  164. /*
  165. * This macro evaluates to TRUE if i is an integer power of 2; FALSE otherwise.
  166. * It uses this trick from Kernighan and Ritchie:  in a 2's complement number
  167. * system, (i & (i-1)) clears the rightmost 1-bit in i.
  168. */
  169.  
  170. #define POWER_OF_2(i) \
  171.     (((i) & ((i) - 1)) == 0)
  172.  
  173. /*  Return TRUE if this pixmap is natural size:
  174.  *      its width and height must be integer powers of 2;
  175.  *      the hardware requires that the width be at least 16 and the
  176.  *      height be at least 4, but tlrotile duplicates the pixmap if needed;
  177.  *      the hardware allows width and height upto 512, however,
  178.  *      for software convenience, we only allow upto 32.
  179.  */
  180.  
  181. Bool
  182. qdNaturalSizePixmap(pPixmap)
  183.     PixmapPtr pPixmap;
  184. {
  185.     int w;
  186.     int h;
  187.  
  188.     if (pPixmap == (PixmapPtr) NULL)
  189.     {
  190.         return(FALSE);
  191.     }
  192.  
  193.     w = QDPIX_WIDTH(pPixmap);
  194.     h = QDPIX_HEIGHT(pPixmap);
  195.  
  196.     return POWER_OF_2(w) && w <= 32 && POWER_OF_2(h) && h <= 32;
  197. }
  198.