home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / xmesa2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  92.4 KB  |  3,237 lines

  1. /* $Id: xmesa2.c,v 1.11 1997/05/26 20:34:40 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.3
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: xmesa2.c,v $
  26.  * Revision 1.11  1997/05/26 20:34:40  brianp
  27.  * added PF_TRUEDITHER pixel format
  28.  *
  29.  * Revision 1.10  1997/04/20 20:29:11  brianp
  30.  * replaced abort() with gl_problem()
  31.  *
  32.  * Revision 1.9  1997/04/12 16:24:07  brianp
  33.  * removed several xmesa_setup_DD_pointers calls, removed die()
  34.  *
  35.  * Revision 1.8  1997/04/04 05:23:57  brianp
  36.  * fixed problem with black/white being reversed on monochrome displays
  37.  *
  38.  * Revision 1.7  1997/03/21 01:58:32  brianp
  39.  * added RendererString() function
  40.  *
  41.  * Revision 1.6  1996/10/22 02:46:29  brianp
  42.  * now use DITHER_SETUP and XDITHER macros
  43.  * use array indexing instead of pointer dereferencing in inner loops
  44.  *
  45.  * Revision 1.5  1996/10/11 00:23:58  brianp
  46.  * fixed dithering bug in write_span_DITHER_pixmap() again!
  47.  *
  48.  * Revision 1.4  1996/10/09 23:09:56  brianp
  49.  * fixed dithering bug in write_span_DITHER_pixmap()
  50.  *
  51.  * Revision 1.3  1996/09/27 01:31:42  brianp
  52.  * removed unused variables
  53.  *
  54.  * Revision 1.2  1996/09/19 03:16:04  brianp
  55.  * new X/Mesa interface with XMesaContext, XMesaVisual, and XMesaBuffer types
  56.  *
  57.  * Revision 1.1  1996/09/13 01:38:16  brianp
  58.  * Initial revision
  59.  *
  60.  */
  61.  
  62.  
  63. /*
  64.  * Mesa/X11 interface, part 2.
  65.  *
  66.  * This file contains the implementations of all the device driver functions.
  67.  */
  68.  
  69.  
  70.  
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <X11/Xlib.h>
  75. #include "GL/xmesa.h"
  76. #include "context.h"
  77. #include "macros.h"
  78. #include "types.h"
  79. #include "xmesaP.h"
  80.  
  81.  
  82.  
  83.  
  84. /*
  85.  * The following functions are used to trap XGetImage() calls which
  86.  * generate BadMatch errors if the drawable isn't mapped.
  87.  */
  88.  
  89. static int caught_xgetimage_error = 0;
  90. static int (*old_xerror_handler)( Display *dpy, XErrorEvent *ev );
  91. static unsigned long xgetimage_serial;
  92.  
  93. /*
  94.  * This is the error handler which will be called if XGetImage fails.
  95.  */
  96. static int xgetimage_error_handler( Display *dpy, XErrorEvent *ev )
  97. {
  98.    if (ev->serial==xgetimage_serial && ev->error_code==BadMatch) {
  99.       /* caught the expected error */
  100.       caught_xgetimage_error = 0;
  101.    }
  102.    else {
  103.       /* call the original X error handler, if any.  otherwise ignore */
  104.       if (old_xerror_handler) {
  105.          (*old_xerror_handler)( dpy, ev );
  106.       }
  107.    }
  108.    return 0;
  109. }
  110.  
  111.  
  112. /*
  113.  * Call this right before XGetImage to setup error trap.
  114.  */
  115. static void catch_xgetimage_errors( Display *dpy )
  116. {
  117.    xgetimage_serial = NextRequest( dpy );
  118.    old_xerror_handler = XSetErrorHandler( xgetimage_error_handler );
  119.    caught_xgetimage_error = 0;
  120. }
  121.  
  122.  
  123. /*
  124.  * Call this right after XGetImage to check if an error occured.
  125.  */
  126. static int check_xgetimage_errors( void )
  127. {
  128.    /* restore old handler */
  129.    (void) XSetErrorHandler( old_xerror_handler );
  130.    /* return 0=no error, 1=error caught */
  131.    return caught_xgetimage_error;
  132. }
  133.  
  134.  
  135. /*
  136.  * Read a pixel from an X drawable.
  137.  */
  138. static unsigned long read_pixel( Display *dpy, Drawable d, int x, int y )
  139. {
  140.    XImage *pixel;
  141.    unsigned long p;
  142.    int error;
  143.    catch_xgetimage_errors( dpy );
  144.    pixel = XGetImage( dpy, d, x, y, 1, 1, AllPlanes, ZPixmap );
  145.    error = check_xgetimage_errors();
  146.    if (pixel && !error) {
  147.       p = XGetPixel( pixel, 0, 0 );
  148.    }
  149.    else {
  150.       p = 0;
  151.    }
  152.    if (pixel) {
  153.       XDestroyImage( pixel );
  154.    }
  155.    return p;
  156. }
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.  * Return the size (width,height of the current color buffer.
  163.  * This function should be called by the glViewport function because
  164.  * glViewport is often called when the window gets resized.  We need to
  165.  * update some X/Mesa stuff when that happens.
  166.  * Output:  width - width of buffer in pixels.
  167.  *          height - height of buffer in pixels.
  168.  */
  169. static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
  170. {
  171.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  172.    Window root;
  173.    int winx, winy;
  174.    unsigned int winwidth, winheight;
  175.    unsigned int bw, d;
  176.  
  177.    XGetGeometry( xmesa->display, xmesa->xm_buffer->frontbuffer, &root,
  178.          &winx, &winy, &winwidth, &winheight, &bw, &d );
  179.  
  180.    *width = winwidth;
  181.    *height = winheight;
  182.  
  183.    if (   winwidth!=xmesa->xm_buffer->width
  184.        || winheight!=xmesa->xm_buffer->height) {
  185.       xmesa->xm_buffer->width = winwidth;
  186.       xmesa->xm_buffer->height = winheight;
  187.       xmesa_alloc_back_buffer( xmesa->xm_buffer );
  188.    }
  189.  
  190.    /* Needed by FLIP macro */
  191.    xmesa->xm_buffer->bottom = (int) winheight - 1;
  192.  
  193.    if (xmesa->xm_buffer->backimage) {
  194.       /* Needed by PIXELADDR1 macro */
  195.       xmesa->xm_buffer->ximage_width1
  196.                   = xmesa->xm_buffer->backimage->bytes_per_line;
  197.       xmesa->xm_buffer->ximage_origin1
  198.                   = (GLubyte *) xmesa->xm_buffer->backimage->data
  199.                     + xmesa->xm_buffer->ximage_width1 * (winheight-1);
  200.  
  201.       /* Needed by PIXELADDR2 macro */
  202.       xmesa->xm_buffer->ximage_width2
  203.                   = xmesa->xm_buffer->backimage->bytes_per_line / 2;
  204.       xmesa->xm_buffer->ximage_origin2
  205.                   = (GLushort *) xmesa->xm_buffer->backimage->data
  206.                     + xmesa->xm_buffer->ximage_width2 * (winheight-1);
  207.  
  208.       /* Needed by PIXELADDR4 macro */
  209.       xmesa->xm_buffer->ximage_width4 = xmesa->xm_buffer->backimage->width;
  210.       xmesa->xm_buffer->ximage_origin4
  211.                   = (GLuint *) xmesa->xm_buffer->backimage->data
  212.                     + xmesa->xm_buffer->ximage_width4 * (winheight-1);
  213.    }
  214. }
  215.  
  216.  
  217. static void finish( GLcontext *ctx )
  218. {
  219.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  220.    if (xmesa) {
  221.       XSync( xmesa->display, False );
  222.    }
  223. }
  224.  
  225.  
  226. static void flush( GLcontext *ctx )
  227. {
  228.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  229.    if (xmesa) {
  230.       XFlush( xmesa->display );
  231.    }
  232. }
  233.  
  234.  
  235.  
  236. static GLboolean set_buffer( GLcontext *ctx, GLenum mode )
  237. {
  238.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  239.    if (mode==GL_FRONT) {
  240.       /* read/write front buffer */
  241.       xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
  242.       ctx->NewState |= NEW_RASTER_OPS;
  243.       gl_update_state(ctx);
  244.       /*xmesa_setup_DD_pointers( ctx );*/
  245.       return GL_TRUE;
  246.    }
  247.    else if (mode==GL_BACK && xmesa->xm_buffer->db_state) {
  248.       /* read/write back buffer */
  249.       if (xmesa->xm_buffer->backpixmap) {
  250.          xmesa->xm_buffer->buffer = xmesa->xm_buffer->backpixmap;
  251.       }
  252.       else if (xmesa->xm_buffer->backimage) {
  253.          xmesa->xm_buffer->buffer = None;
  254.       }
  255.       else {
  256.          /* just in case there wasn't enough memory for back buffer */
  257.          xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
  258.       }
  259.       ctx->NewState |= NEW_RASTER_OPS;
  260.       gl_update_state(ctx);
  261.       /*xmesa_setup_DD_pointers( ctx );*/
  262.       return GL_TRUE;
  263.    }
  264.    else {
  265.       return GL_FALSE;
  266.    }
  267. }
  268.  
  269.  
  270.  
  271. static void clear_index( GLcontext *ctx, GLuint index )
  272. {
  273.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  274.    xmesa->clearpixel = (unsigned long) index;
  275.    XSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
  276.                    (unsigned long) index );
  277. }
  278.  
  279.  
  280. static void clear_color( GLcontext *ctx,
  281.                          GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  282. {
  283.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  284.    xmesa->clearcolor[0] = r;
  285.    xmesa->clearcolor[1] = g;
  286.    xmesa->clearcolor[2] = b;
  287.    xmesa->clearcolor[3] = a;
  288.    xmesa->clearpixel = xmesa_color_to_pixel( xmesa, r, g, b, a );
  289.    XSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
  290.                    xmesa->clearpixel );
  291. }
  292.  
  293.  
  294. /* Set current color index */
  295. static void set_index( GLcontext *ctx, GLuint index )
  296. {
  297.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  298.    unsigned long p = (unsigned long) index;
  299.    xmesa->pixel = p;
  300.    XSetForeground( xmesa->display, xmesa->xm_buffer->gc1, p );
  301. }
  302.  
  303.  
  304. /* Set current drawing color */
  305. static void set_color( GLcontext *ctx,
  306.                        GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  307. {
  308.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  309.    xmesa->red   = r;
  310.    xmesa->green = g;
  311.    xmesa->blue  = b;
  312.    xmesa->alpha = a;
  313.    xmesa->pixel = xmesa_color_to_pixel( xmesa, r, g, b, a );;
  314.    XSetForeground( xmesa->display, xmesa->xm_buffer->gc1, xmesa->pixel );
  315. }
  316.  
  317.  
  318.  
  319. /* Set index mask ala glIndexMask */
  320. static GLboolean index_mask( GLcontext *ctx, GLuint mask )
  321. {
  322.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  323.    if (xmesa->xm_buffer->buffer==XIMAGE) {
  324.       return GL_FALSE;
  325.    }
  326.    else {
  327.       unsigned long m;
  328.       if (mask==0xffffffff) {
  329.          m = AllPlanes;
  330.       }
  331.       else {
  332.          m = (unsigned long) mask;
  333.       }
  334.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc1, m );
  335.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc2, m );
  336.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
  337.       return GL_TRUE;
  338.    }
  339. }
  340.  
  341.  
  342. /* Implements glColorMask() */
  343. static GLboolean color_mask( GLcontext *ctx,
  344.                              GLboolean rmask, GLboolean gmask,
  345.                              GLboolean bmask, GLboolean amask )
  346. {
  347.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  348. #if defined(__cplusplus) || defined(c_plusplus)
  349.    int xclass = xmesa->xm_visual->visinfo->c_class;
  350. #else
  351.    int xclass = xmesa->xm_visual->visinfo->class;
  352. #endif
  353.  
  354.    if (xmesa->xm_buffer->buffer!=XIMAGE
  355.        && (xclass==TrueColor || xclass==DirectColor)) {
  356.       unsigned long m;
  357.       if (rmask && gmask && bmask) {
  358.          m = AllPlanes;
  359.       }
  360.       else {
  361.          m = 0;
  362.          if (rmask)   m |= xmesa->xm_visual->visinfo->red_mask;
  363.          if (gmask)   m |= xmesa->xm_visual->visinfo->green_mask;
  364.          if (bmask)   m |= xmesa->xm_visual->visinfo->blue_mask;
  365.       }
  366.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc1, m );
  367.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->gc2, m );
  368.       XSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
  369.       return GL_TRUE;
  370.    }
  371.    else {
  372.       return GL_FALSE;
  373.    }
  374. }
  375.  
  376.  
  377. /*
  378.  * Set the pixel logic operation.  Return GL_TRUE if the device driver
  379.  * can perform the operation, otherwise return GL_FALSE.  GL_COPY _must_
  380.  * be operational, obviously.
  381.  */
  382. static GLboolean logicop( GLcontext *ctx, GLenum op )
  383. {
  384.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  385.    int func;
  386.    if ((xmesa->xm_buffer->buffer==XIMAGE) && op!=GL_COPY) {
  387.       /* X can't do logic ops in Ximages, except for GL_COPY */
  388.       return GL_FALSE;
  389.    }
  390.    switch (op) {
  391.       case GL_CLEAR:        func = GXclear;        break;
  392.       case GL_SET:        func = GXset;        break;
  393.       case GL_COPY:        func = GXcopy;        break;
  394.       case GL_COPY_INVERTED:    func = GXcopyInverted;    break;
  395.       case GL_NOOP:        func = GXnoop;        break;
  396.       case GL_INVERT:        func = GXinvert;    break;
  397.       case GL_AND:        func = GXand;        break;
  398.       case GL_NAND:        func = GXnand;        break;
  399.       case GL_OR:        func = GXor;        break;
  400.       case GL_NOR:        func = GXnor;        break;
  401.       case GL_XOR:        func = GXxor;        break;
  402.       case GL_EQUIV:        func = GXequiv;        break;
  403.       case GL_AND_REVERSE:    func = GXandReverse;    break;
  404.       case GL_AND_INVERTED:    func = GXandInverted;    break;
  405.       case GL_OR_REVERSE:    func = GXorReverse;    break;
  406.       case GL_OR_INVERTED:    func = GXorInverted;    break;
  407.       default:  return GL_FALSE;
  408.    }
  409.    XSetFunction( xmesa->display, xmesa->xm_buffer->gc1, func );
  410.    XSetFunction( xmesa->display, xmesa->xm_buffer->gc2, func );
  411.    return GL_TRUE;
  412. }
  413.  
  414.  
  415. /*
  416.  * Enable/disable dithering
  417.  */
  418. static void dither( GLcontext *ctx, GLboolean enable )
  419. {
  420.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  421.    if (enable) {
  422.       xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
  423.    }
  424.    else {
  425.       xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
  426.    }
  427. }
  428.  
  429.  
  430.  
  431. /**********************************************************************/
  432. /*** glClear implementations                                        ***/
  433. /**********************************************************************/
  434.  
  435. static void clear_pixmap( GLcontext *ctx, GLboolean all,
  436.                           GLint x, GLint y, GLint width, GLint height )
  437. {
  438.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  439.    if (all) {
  440.       XFillRectangle( xmesa->display, xmesa->xm_buffer->buffer,
  441.                       xmesa->xm_buffer->cleargc,
  442.                       0, 0,
  443.                       xmesa->xm_buffer->width+1, xmesa->xm_buffer->height+1 );
  444.    }
  445.    else {
  446.       XFillRectangle( xmesa->display, xmesa->xm_buffer->buffer,
  447.                       xmesa->xm_buffer->cleargc,
  448.                       x, xmesa->xm_buffer->height - y - height,
  449.                       width, height );
  450.    }
  451. }
  452.  
  453.  
  454. static void clear_8bit_ximage( GLcontext *ctx, GLboolean all,
  455.                                GLint x, GLint y, GLint width, GLint height )
  456. {
  457.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  458.    if (all) {
  459.       size_t n = xmesa->xm_buffer->backimage->bytes_per_line
  460.                * xmesa->xm_buffer->backimage->height;
  461.       MEMSET( xmesa->xm_buffer->backimage->data, xmesa->clearpixel, n );
  462.    }
  463.    else {
  464.       GLint i;
  465.       for (i=0;i<height;i++) {
  466.          GLubyte *ptr = PIXELADDR1( x, y+i );
  467.          MEMSET( ptr, xmesa->clearpixel, width );
  468.       }
  469.    }
  470. }
  471.  
  472.  
  473. static void clear_16bit_ximage( GLcontext *ctx, GLboolean all,
  474.                                 GLint x, GLint y, GLint width, GLint height )
  475. {
  476.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  477.    if (all) {
  478.       register GLuint n;
  479.       register GLushort *ptr2 =(GLushort *) xmesa->xm_buffer->backimage->data;
  480.       register GLushort pixel = (GLushort) xmesa->clearpixel;
  481.       if (xmesa->swapbytes) {
  482.          pixel = ((pixel >> 8) & 0x00ff)
  483.                | ((pixel << 8) & 0xff00);
  484.       }
  485.       if ((pixel & 0xff) == (pixel >> 8)) {
  486.          /* low and high bytes are equal so use memset() */
  487.          n = xmesa->xm_buffer->backimage->bytes_per_line
  488.              * xmesa->xm_buffer->height;
  489.          MEMSET( ptr2, pixel & 0xff, n );
  490.       }
  491.       else {
  492.          n = xmesa->xm_buffer->backimage->bytes_per_line / 2
  493.              * xmesa->xm_buffer->height;
  494.      do {
  495.         *ptr2++ = pixel;
  496.         n--;
  497.      } while (n!=0);
  498.       }
  499.    }
  500.    else {
  501.       register int i, j;
  502.       register GLushort pixel = (GLushort) xmesa->clearpixel;
  503.       for (j=0;j<height;j++) {
  504.      register GLushort *ptr2 = PIXELADDR2( x, y+j );
  505.          for (i=0;i<width;i++) {
  506.             *ptr2++ = pixel;
  507.          }
  508.       }
  509.    }
  510. }
  511.  
  512.  
  513. static void clear_32bit_ximage( GLcontext *ctx, GLboolean all,
  514.                                 GLint x, GLint y, GLint width, GLint height )
  515. {
  516.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  517.    if (all) {
  518.       register GLint n = xmesa->xm_buffer->width * xmesa->xm_buffer->height;
  519.       register GLuint *ptr4 = (GLuint *) xmesa->xm_buffer->backimage->data;
  520.       register GLuint pixel = (GLuint) xmesa->clearpixel;
  521.       if (xmesa->swapbytes) {
  522.          pixel = ((pixel >> 24) & 0x000000ff)
  523.                | ((pixel >> 8)  & 0x0000ff00)
  524.                | ((pixel << 8)  & 0x00ff0000)
  525.                | ((pixel << 24) & 0xff000000);
  526.       }
  527.       if (pixel==0) {
  528.          MEMSET( ptr4, pixel, 4*n );
  529.       }
  530.       else {
  531.          do {
  532.             *ptr4++ = pixel;
  533.             n--;
  534.          } while (n!=0);
  535.       }
  536.    }
  537.    else {
  538.       register int i, j;
  539.       register GLuint pixel = (GLuint) xmesa->clearpixel;
  540.       for (j=0;j<height;j++) {
  541.          register GLuint *ptr4 = PIXELADDR4( x, y+j );
  542.          for (i=0;i<width;i++) {
  543.             *ptr4++ = pixel;
  544.          }
  545.       }
  546.    }
  547. }
  548.  
  549.  
  550. static void clear_nbit_ximage( GLcontext *ctx, GLboolean all,
  551.                                GLint x, GLint y, GLint width, GLint height )
  552. {
  553.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  554.    XImage *img = xmesa->xm_buffer->backimage;
  555.    if (all) {
  556.       register int i, j;
  557.       width = xmesa->xm_buffer->width;
  558.       height = xmesa->xm_buffer->height;
  559.       for (j=0;j<height;j++) {
  560.          for (i=0;i<width;i++) {
  561.             XPutPixel( img, i, j, xmesa->clearpixel );
  562.          }
  563.       }
  564.    }
  565.    else {
  566.       /* TODO: optimize this */
  567.       register int i, j;
  568.       y = FLIP(y);
  569.       for (j=0;j<height;j++) {
  570.          for (i=0;i<width;i++) {
  571.             XPutPixel( img, x+i, y-j, xmesa->clearpixel );
  572.          }
  573.       }
  574.    }
  575. }
  576.  
  577.  
  578.  
  579.  
  580. /*
  581.  * The Mesa library needs to be able to draw pixels in a number of ways:
  582.  *   1. RGB vs Color Index
  583.  *   2. as horizontal spans (polygons, images) vs random locations (points,
  584.  *      lines)
  585.  *   3. different color per-pixel or same color for all pixels
  586.  *
  587.  * Furthermore, the X driver needs to support rendering to 3 possible
  588.  * "buffers", usually one, but sometimes two at a time:
  589.  *   1. The front buffer as an X window
  590.  *   2. The back buffer as a Pixmap
  591.  *   3. The back buffer as an XImage
  592.  *
  593.  * Finally, if the back buffer is an XImage, we can avoid using XPutPixel and
  594.  * optimize common cases such as 24-bit and 8-bit modes.
  595.  *
  596.  * By multiplication, there's at least 48 possible combinations of the above.
  597.  *
  598.  * Below are implementations of the most commonly used combinations.  They are
  599.  * accessed through function pointers which get initialized here and are used
  600.  * directly from the Mesa library.  The 8 function pointers directly correspond
  601.  * to the first 3 cases listed above.
  602.  *
  603.  *
  604.  * The function naming convention is:
  605.  *
  606.  *   write_[span|pixels]_[mono]_[format]_[pixmap|ximage]
  607.  *
  608.  * New functions optimized for specific cases can be added without too much
  609.  * trouble.  An example might be the 24-bit TrueColor mode 8A8R8G8B which is
  610.  * found on IBM RS/6000 X servers.
  611.  */
  612.  
  613.  
  614.  
  615.  
  616. /**********************************************************************/
  617. /*** Write COLOR SPAN functions                                     ***/
  618. /**********************************************************************/
  619.  
  620.  
  621. #define COLOR_SPAN_ARGS    GLcontext *ctx,                    \
  622.             GLuint n, GLint x, GLint y,            \
  623.             const GLubyte red[], const GLubyte green[],    \
  624.             const GLubyte blue[], const GLubyte alpha[],    \
  625.             const GLubyte mask[]
  626.  
  627. /* NOTE: if mask==NULL, draw all pixels */
  628.  
  629.  
  630. /*
  631.  * Write a span of PF_TRUECOLOR pixels to a pixmap.
  632.  */
  633. static void write_span_TRUECOLOR_pixmap( COLOR_SPAN_ARGS )
  634. {
  635.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  636.    Display *dpy = xmesa->xm_visual->display;
  637.    Drawable buffer = xmesa->xm_buffer->buffer;
  638.    GC gc = xmesa->xm_buffer->gc2;
  639.    register GLuint i;
  640.    y = FLIP(y);
  641.    if (mask) {
  642.       for (i=0;i<n;i++,x++) {
  643.          if (mask[i]) {
  644.             unsigned long p;
  645.             PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  646.             XSetForeground( dpy, gc, p );
  647.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  648.          }
  649.       }
  650.    }
  651.    else {
  652.       /* draw all pixels */
  653.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  654.       for (i=0;i<n;i++) {
  655.          unsigned long p;
  656.          PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  657.          XPutPixel( rowimg, i, 0, p );
  658.       }
  659.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  660.    }
  661. }
  662.  
  663.  
  664. /*
  665.  * Write a span of PF_TRUEDITHER pixels to a pixmap.
  666.  */
  667. static void write_span_TRUEDITHER_pixmap( COLOR_SPAN_ARGS )
  668. {
  669.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  670.    Display *dpy = xmesa->xm_visual->display;
  671.    Drawable buffer = xmesa->xm_buffer->buffer;
  672.    GC gc = xmesa->xm_buffer->gc2;
  673.    register GLuint i;
  674.    y = FLIP( y );
  675.    if (mask) {
  676.       for (i=0;i<n;i++,x++) {
  677.          if (mask[i]) {
  678.             unsigned long p;
  679.             PACK_TRUEDITHER(p, x, y, red[i], green[i], blue[i]);
  680.             XSetForeground( dpy, gc, p );
  681.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  682.          }
  683.       }
  684.    }
  685.    else {
  686.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  687.       for (i=0;i<n;i++) {
  688.          unsigned long p;
  689.          PACK_TRUEDITHER(p, x+i, y, red[i], green[i], blue[i]);
  690.          XPutPixel( rowimg, i, 0, p );
  691.       }
  692.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  693.    }
  694. }
  695.  
  696.  
  697.  
  698. /*
  699.  * Write a span of PF_8A8B8G8R pixels to a pixmap.
  700.  */
  701. static void write_span_8A8B8G8R_pixmap( COLOR_SPAN_ARGS )
  702. {
  703.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  704.    Display *dpy = xmesa->xm_visual->display;
  705.    Drawable buffer = xmesa->xm_buffer->buffer;
  706.    GC gc = xmesa->xm_buffer->gc2;
  707.    register GLuint i;
  708.    y = FLIP( y );
  709.    if (mask) {
  710.       for (i=0;i<n;i++,x++) {
  711.          if (mask[i]) {
  712.             XSetForeground( dpy, gc,
  713.                          PACK_8A8B8G8R(red[i], green[i], blue[i], alpha[i]) );
  714.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  715.          }
  716.       }
  717.    }
  718.    else {
  719.       /* draw all pixels */
  720.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  721.       register GLuint *ptr4 = (GLuint *) rowimg->data;
  722.       for (i=0;i<n;i++) {
  723.          *ptr4++ = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  724.       }
  725.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  726.    }
  727. }
  728.  
  729.  
  730. /*
  731.  * Write a span of PF_8R8G8B pixels to a pixmap.
  732.  */
  733. static void write_span_8R8G8B_pixmap( COLOR_SPAN_ARGS )
  734. {
  735.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  736.    Display *dpy = xmesa->xm_visual->display;
  737.    Drawable buffer = xmesa->xm_buffer->buffer;
  738.    GC gc = xmesa->xm_buffer->gc2;
  739.    register GLuint i;
  740.    y = FLIP( y );
  741.    if (mask) {
  742.       for (i=0;i<n;i++,x++) {
  743.          if (mask[i]) {
  744.             XSetForeground( dpy, gc, PACK_8R8G8B( red[i], green[i], blue[i] ));
  745.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  746.          }
  747.       }
  748.    }
  749.    else {
  750.       /* draw all pixels */
  751.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  752.       register GLuint *ptr4 = (GLuint *) rowimg->data;
  753.       for (i=0;i<n;i++) {
  754.          *ptr4++ = PACK_8R8G8B( red[i], green[i], blue[i] );
  755.       }
  756.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  757.    }
  758. }
  759.  
  760.  
  761. /*
  762.  * Write a span of PF_5R6G5B pixels to a pixmap.
  763.  */
  764. static void write_span_5R6G5B_pixmap( COLOR_SPAN_ARGS )
  765. {
  766.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  767.    Display *dpy = xmesa->xm_visual->display;
  768.    Drawable buffer = xmesa->xm_buffer->buffer;
  769.    GC gc = xmesa->xm_buffer->gc2;
  770.    register GLuint i;
  771.    y = FLIP( y );
  772.    if (mask) {
  773.       for (i=0;i<n;i++,x++) {
  774.          if (mask[i]) {
  775.             XSetForeground( dpy, gc, PACK_5R6G5B( red[i], green[i], blue[i] ));
  776.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  777.          }
  778.       }
  779.    }
  780.    else {
  781.       /* draw all pixels */
  782.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  783.       register GLushort *ptr2 = (GLushort *) rowimg->data;
  784.       for (i=0;i<n;i++) {
  785.          ptr2[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  786.       }
  787.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  788.    }
  789. }
  790.  
  791.  
  792. /*
  793.  * Write a span of PF_DITHER pixels to a pixmap.
  794.  */
  795. static void write_span_DITHER_pixmap( COLOR_SPAN_ARGS )
  796. {
  797.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  798.    Display *dpy = xmesa->xm_visual->display;
  799.    Drawable buffer = xmesa->xm_buffer->buffer;
  800.    GC gc = xmesa->xm_buffer->gc2;
  801.    register GLuint i;
  802.    XDITHER_SETUP(y);
  803.    y = FLIP( y );
  804.    if (mask) {
  805.       for (i=0;i<n;i++,x++) {
  806.          if (mask[i]) {
  807.             XSetForeground( dpy, gc, XDITHER(x, red[i], green[i], blue[i]) );
  808.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  809.          }
  810.       }
  811.    }
  812.    else {
  813.       /* draw all pixels */
  814.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  815.       for (i=0;i<n;i++) {
  816.          XPutPixel( rowimg, i, 0, XDITHER(x+i, red[i], green[i], blue[i]) );
  817.       }
  818.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  819.    }
  820. }
  821.  
  822.  
  823. /*
  824.  * Write a span of PF_1BIT pixels to a pixmap.
  825.  */
  826. static void write_span_1BIT_pixmap( COLOR_SPAN_ARGS )
  827. {
  828.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  829.    Display *dpy = xmesa->xm_visual->display;
  830.    Drawable buffer = xmesa->xm_buffer->buffer;
  831.    GC gc = xmesa->xm_buffer->gc2;
  832.    register GLuint i;
  833.    SETUP_1BIT;
  834.    y = FLIP( y );
  835.    if (mask) {
  836.       for (i=0;i<n;i++,x++) {
  837.          if (mask[i]) {
  838.             XSetForeground( dpy, gc,
  839.                             DITHER_1BIT( x, y, red[i], green[i], blue[i] ) );
  840.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  841.          }
  842.       }
  843.    }
  844.    else {
  845.       /* draw all pixels */
  846.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  847.       for (i=0;i<n;i++) {
  848.          XPutPixel( rowimg, i, 0,
  849.                     DITHER_1BIT( x+i, y, red[i], green[i], blue[i] ) );
  850.       }
  851.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  852.    }
  853. }
  854.  
  855.  
  856.  
  857. /*
  858.  * Write a span of PF_HPCR pixels to a pixmap.
  859.  */
  860. static void write_span_HPCR_pixmap( COLOR_SPAN_ARGS )
  861. {
  862.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  863.    Display *dpy = xmesa->xm_visual->display;
  864.    Drawable buffer = xmesa->xm_buffer->buffer;
  865.    GC gc = xmesa->xm_buffer->gc2;
  866.    register GLuint i;
  867.    y = FLIP( y );
  868.    if (mask) {
  869.       for (i=0;i<n;i++,x++) {
  870.          if (mask[i]) {
  871.             XSetForeground( dpy, gc,
  872.                             DITHER_HPCR( x, y, red[i], green[i], blue[i] ) );
  873.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  874.          }
  875.       }
  876.    }
  877.    else {
  878.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  879.       register GLubyte *ptr = (GLubyte *) xmesa->xm_buffer->rowimage->data;
  880.       for (i=0;i<n;i++) {
  881.          ptr[i] = DITHER_HPCR( (x+i), y, red[i], green[i], blue[i] );
  882.       }
  883.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  884.    }
  885. }
  886.  
  887.  
  888. /*
  889.  * Write a span of PF_LOOKUP pixels to a pixmap.
  890.  */
  891. static void write_span_LOOKUP_pixmap( COLOR_SPAN_ARGS )
  892. {
  893.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  894.    Display *dpy = xmesa->xm_visual->display;
  895.    Drawable buffer = xmesa->xm_buffer->buffer;
  896.    GC gc = xmesa->xm_buffer->gc2;
  897.    register GLuint i;
  898.    LOOKUP_SETUP;
  899.    y = FLIP( y );
  900.    if (mask) {
  901.       for (i=0;i<n;i++,x++) {
  902.          if (mask[i]) {
  903.             XSetForeground( dpy, gc, LOOKUP( red[i], green[i], blue[i] ) );
  904.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  905.          }
  906.       }
  907.    }
  908.    else {
  909.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  910.       for (i=0;i<n;i++) {
  911.          XPutPixel( rowimg, i, 0, LOOKUP(red[i],green[i],blue[i]) );
  912.       }
  913.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  914.    }
  915. }
  916.  
  917.  
  918.  
  919. /*
  920.  * Write a span of PF_GRAYSCALE pixels to a pixmap.
  921.  */
  922. static void write_span_GRAYSCALE_pixmap( COLOR_SPAN_ARGS )
  923. {
  924.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  925.    Display *dpy = xmesa->xm_visual->display;
  926.    Drawable buffer = xmesa->xm_buffer->buffer;
  927.    GC gc = xmesa->xm_buffer->gc2;
  928.    register GLuint i;
  929.    y = FLIP( y );
  930.    if (mask) {
  931.       for (i=0;i<n;i++,x++) {
  932.          if (mask[i]) {
  933.             XSetForeground( dpy, gc, GRAY_RGB( red[i], green[i], blue[i] ) );
  934.             XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  935.          }
  936.       }
  937.    }
  938.    else {
  939.       XImage *rowimg = xmesa->xm_buffer->rowimage;
  940.       for (i=0;i<n;i++) {
  941.          XPutPixel( rowimg, i, 0, GRAY_RGB(red[i],green[i],blue[i]) );
  942.       }
  943.       XPutImage( dpy, buffer, gc, rowimg, 0, 0, x, y, n, 1 );
  944.    }
  945. }
  946.  
  947.  
  948. /*
  949.  * Write a span of PF_TRUECOLOR pixels to an XImage.
  950.  */
  951. static void write_span_TRUECOLOR_ximage( COLOR_SPAN_ARGS )
  952. {
  953.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  954.    XImage *img = xmesa->xm_buffer->backimage;
  955.    register GLuint i;
  956.    y = FLIP(y);
  957.    if (mask) {
  958.       for (i=0;i<n;i++,x++) {
  959.          if (mask[i]) {
  960.             unsigned long p;
  961.             PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  962.             XPutPixel( img, x, y, p );
  963.          }
  964.       }
  965.    }
  966.    else {
  967.       /* draw all pixels */
  968.       for (i=0;i<n;i++,x++) {
  969.          unsigned long p;
  970.          PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  971.          XPutPixel( img, x, y, p );
  972.       }
  973.    }
  974. }
  975.  
  976.  
  977. /*
  978.  * Write a span of PF_TRUEDITHER pixels to an XImage.
  979.  */
  980. static void write_span_TRUEDITHER_ximage( COLOR_SPAN_ARGS )
  981. {
  982.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  983.    XImage *img = xmesa->xm_buffer->backimage;
  984.    register GLuint i;
  985.    y = FLIP(y);
  986.    if (mask) {
  987.       for (i=0;i<n;i++,x++) {
  988.          if (mask[i]) {
  989.             unsigned long p;
  990.             PACK_TRUEDITHER(p, x, y, red[i], green[i], blue[i]);
  991.             XPutPixel( img, x, y, p );
  992.          }
  993.       }
  994.    }
  995.    else {
  996.       /* draw all pixels */
  997.       for (i=0;i<n;i++,x++) {
  998.          unsigned long p;
  999.          PACK_TRUEDITHER(p, x, y, red[i], green[i], blue[i]);
  1000.          XPutPixel( img, x, y, p );
  1001.       }
  1002.    }
  1003. }
  1004.  
  1005.  
  1006. /*
  1007.  * Write a span of PF_8A8B8G8R-format pixels to an ximage.
  1008.  */
  1009. static void write_span_8A8B8G8R_ximage( COLOR_SPAN_ARGS )
  1010. {
  1011.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1012.    register GLuint i;
  1013.    register GLuint *ptr = PIXELADDR4( x, y );
  1014.    if (mask) {
  1015.       for (i=0;i<n;i++) {
  1016.          if (mask[i]) {
  1017.             ptr[i] = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  1018.          }
  1019.       }
  1020.    }
  1021.    else {
  1022.       /* draw all pixels */
  1023.       for (i=0;i<n;i++) {
  1024.          ptr[i] = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  1025.       }
  1026.    }
  1027. }
  1028.  
  1029.  
  1030. /*
  1031.  * Write a span of PF_8R8G8B-format pixels to an ximage.
  1032.  */
  1033. static void write_span_8R8G8B_ximage( COLOR_SPAN_ARGS )
  1034. {
  1035.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1036.    register GLuint i;
  1037.    register GLuint *ptr = PIXELADDR4( x, y );
  1038.    if (mask) {
  1039.       for (i=0;i<n;i++) {
  1040.          if (mask[i]) {
  1041.             ptr[i] = PACK_8R8G8B( red[i], green[i], blue[i] );
  1042.          }
  1043.       }
  1044.    }
  1045.    else {
  1046.       /* draw all pixels */
  1047.       for (i=0;i<n;i++) {
  1048.          ptr[i] = PACK_8R8G8B( red[i], green[i], blue[i] );
  1049.       }
  1050.    }
  1051. }
  1052.  
  1053.  
  1054. /*
  1055.  * Write a span of PF_5R6G5B-format pixels to an ximage.
  1056.  */
  1057. static void write_span_5R6G5B_ximage( COLOR_SPAN_ARGS )
  1058. {
  1059.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1060.    register GLuint i;
  1061.    register GLushort *ptr = PIXELADDR2( x, y );
  1062.    if (mask) {
  1063.       for (i=0;i<n;i++) {
  1064.          if (mask[i]) {
  1065.             ptr[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  1066.          }
  1067.       }
  1068.    }
  1069.    else {
  1070.       /* draw all pixels */
  1071.       for (i=0;i<n;i++) {
  1072.          ptr[i] = PACK_5R6G5B( red[i], green[i], blue[i] );
  1073.       }
  1074.    }
  1075. }
  1076.  
  1077.  
  1078. /*
  1079.  * Write a span of PF_DITHER pixels to an XImage.
  1080.  */
  1081. static void write_span_DITHER_ximage( COLOR_SPAN_ARGS )
  1082. {
  1083.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1084.    XImage *img = xmesa->xm_buffer->backimage;
  1085.    register GLuint i;
  1086.    int yy = FLIP(y);
  1087.    XDITHER_SETUP(yy);
  1088.    if (mask) {
  1089.       for (i=0;i<n;i++,x++) {
  1090.          if (mask[i]) {
  1091.             XPutPixel( img, x, yy, XDITHER( x, red[i], green[i], blue[i] ) );
  1092.          }
  1093.       }
  1094.    }
  1095.    else {
  1096.       /* draw all pixels */
  1097.       for (i=0;i<n;i++,x++) {
  1098.          XPutPixel( img, x, yy, XDITHER( x, red[i], green[i], blue[i] ) );
  1099.       }
  1100.    }
  1101. }
  1102.  
  1103.  
  1104.  
  1105. /*
  1106.  * Write a span of 8-bit PF_DITHER pixels to an XImage.
  1107.  */
  1108. static void write_span_DITHER8_ximage( COLOR_SPAN_ARGS )
  1109. {
  1110.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1111.    register GLuint i;
  1112.    register GLubyte *ptr = PIXELADDR1( x, y );
  1113.    XDITHER_SETUP(y);
  1114.    if (mask) {
  1115.       for (i=0;i<n;i++,x++) {
  1116.          if (mask[i]) {
  1117.             ptr[i] = XDITHER( x, red[i], green[i], blue[i] );
  1118.          }
  1119.       }
  1120.    }
  1121.    else {
  1122.       for (i=0;i<n;i++,x++) {
  1123.          ptr[i] = XDITHER( x, red[i], green[i], blue[i] );
  1124.       }
  1125.    }
  1126. }
  1127.  
  1128.  
  1129.  
  1130. /*
  1131.  * Write a span of PF_1BIT pixels to an XImage.
  1132.  */
  1133. static void write_span_1BIT_ximage( COLOR_SPAN_ARGS )
  1134. {
  1135.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1136.    XImage *img = xmesa->xm_buffer->backimage;
  1137.    register GLuint i;
  1138.    SETUP_1BIT;
  1139.    y = FLIP(y);
  1140.    if (mask) {
  1141.       for (i=0;i<n;i++,x++) {
  1142.          if (mask[i]) {
  1143.             XPutPixel(img, x, y, DITHER_1BIT(x, y, red[i], green[i], blue[i]));
  1144.          }
  1145.       }
  1146.    }
  1147.    else {
  1148.       for (i=0;i<n;i++,x++) {
  1149.          XPutPixel( img, x, y, DITHER_1BIT(x, y, red[i], green[i], blue[i]) );
  1150.       }
  1151.    }
  1152. }
  1153.  
  1154.  
  1155. /*
  1156.  * Write a span of PF_HPCR pixels to an XImage.
  1157.  */
  1158. static void write_span_HPCR_ximage( COLOR_SPAN_ARGS )
  1159. {
  1160.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1161.    register GLuint i;
  1162.    register GLubyte *ptr = PIXELADDR1( x, y );
  1163.    if (mask) {
  1164.       for (i=0;i<n;i++,x++) {
  1165.          if (mask[i]) {
  1166.             ptr[i] = DITHER_HPCR( x, y, red[i], green[i], blue[i] );
  1167.          }
  1168.       }
  1169.    }
  1170.    else {
  1171.       /* draw all pixels */
  1172.       for (i=0;i<n;i++,x++) {
  1173.          ptr[i] = DITHER_HPCR( x, y, red[i], green[i], blue[i] );
  1174.       }
  1175.    }
  1176. }
  1177.  
  1178.  
  1179. /*
  1180.  * Write a span of PF_LOOKUP pixels to an XImage.
  1181.  */
  1182. static void write_span_LOOKUP_ximage( COLOR_SPAN_ARGS )
  1183. {
  1184.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1185.    XImage *img = xmesa->xm_buffer->backimage;
  1186.    register GLuint i;
  1187.    LOOKUP_SETUP;
  1188.    y = FLIP(y);
  1189.    if (mask) {
  1190.       for (i=0;i<n;i++,x++) {
  1191.          if (mask[i]) {
  1192.             XPutPixel( img, x, y, LOOKUP( red[i], green[i], blue[i] ) );
  1193.          }
  1194.       }
  1195.    }
  1196.    else {
  1197.       /* draw all pixels */
  1198.       for (i=0;i<n;i++,x++) {
  1199.          XPutPixel( img, x, y, LOOKUP( red[i], green[i], blue[i] ) );
  1200.       }
  1201.    }
  1202. }
  1203.  
  1204.  
  1205. /*
  1206.  * Write a span of 8-bit PF_LOOKUP pixels to an XImage.
  1207.  */
  1208. static void write_span_LOOKUP8_ximage( COLOR_SPAN_ARGS )
  1209. {
  1210.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1211.    register GLuint i;
  1212.    register GLubyte *ptr = PIXELADDR1( x, y );
  1213.    LOOKUP_SETUP;
  1214.    if (mask) {
  1215.       for (i=0;i<n;i++,x++) {
  1216.          if (mask[i]) {
  1217.             ptr[i] = LOOKUP( red[i], green[i], blue[i] );
  1218.          }
  1219.       }
  1220.    }
  1221.    else {
  1222.       /* draw all pixels */
  1223.       for (i=0;i<n;i++,x++) {
  1224.          ptr[i] = LOOKUP( red[i], green[i], blue[i] );
  1225.       }
  1226.    }
  1227. }
  1228.  
  1229.  
  1230.  
  1231.  
  1232. /*
  1233.  * Write a span of PF_GRAYSCALE pixels to an XImage.
  1234.  */
  1235. static void write_span_GRAYSCALE_ximage( COLOR_SPAN_ARGS )
  1236. {
  1237.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1238.    XImage *img = xmesa->xm_buffer->backimage;
  1239.    register GLuint i;
  1240.    y = FLIP(y);
  1241.    if (mask) {
  1242.       for (i=0;i<n;i++,x++) {
  1243.          if (mask[i]) {
  1244.             XPutPixel( img, x, y, GRAY_RGB( red[i], green[i], blue[i] ) );
  1245.          }
  1246.       }
  1247.    }
  1248.    else {
  1249.       /* draw all pixels */
  1250.       for (i=0;i<n;i++,x++) {
  1251.          XPutPixel( img, x, y, GRAY_RGB( red[i], green[i], blue[i] ) );
  1252.       }
  1253.    }
  1254. }
  1255.  
  1256.  
  1257.  
  1258. /*
  1259.  * Write a span of 8-bit PF_GRAYSCALE pixels to an XImage.
  1260.  */
  1261. static void write_span_GRAYSCALE8_ximage( COLOR_SPAN_ARGS )
  1262. {
  1263.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1264.    register GLuint i;
  1265.    register GLubyte *ptr = PIXELADDR1( x, y );
  1266.    if (mask) {
  1267.       for (i=0;i<n;i++) {
  1268.          if (mask[i]) {
  1269.             ptr[i] = GRAY_RGB( red[i], green[i], blue[i] );
  1270.          }
  1271.       }
  1272.    }
  1273.    else {
  1274.       /* draw all pixels */
  1275.       for (i=0;i<n;i++) {
  1276.          ptr[i] = GRAY_RGB( red[i], green[i], blue[i] );
  1277.       }
  1278.    }
  1279. }
  1280.  
  1281.  
  1282.  
  1283.  
  1284. /**********************************************************************/
  1285. /*** Write COLOR PIXEL functions                                    ***/
  1286. /**********************************************************************/
  1287.  
  1288.  
  1289. #define COLOR_PIXEL_ARGS   GLcontext *ctx,                \
  1290.                GLuint n, const GLint x[], const GLint y[],    \
  1291.                const GLubyte red[], const GLubyte green[],    \
  1292.                const GLubyte blue[], const GLubyte alpha[],    \
  1293.                const GLubyte mask[]
  1294.  
  1295.  
  1296. /*
  1297.  * Write an array of PF_TRUECOLOR pixels to a pixmap.
  1298.  */
  1299. static void write_pixels_TRUECOLOR_pixmap( COLOR_PIXEL_ARGS )
  1300. {
  1301.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1302.    Display *dpy = xmesa->xm_visual->display;
  1303.    Drawable buffer = xmesa->xm_buffer->buffer;
  1304.    GC gc = xmesa->xm_buffer->gc2;
  1305.    register GLuint i;
  1306.    for (i=0;i<n;i++) {
  1307.       if (mask[i]) {
  1308.          unsigned long p;
  1309.          PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  1310.      XSetForeground( dpy, gc, p );
  1311.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1312.       }
  1313.    }
  1314. }
  1315.  
  1316.  
  1317. /*
  1318.  * Write an array of PF_TRUEDITHER pixels to a pixmap.
  1319.  */
  1320. static void write_pixels_TRUEDITHER_pixmap( COLOR_PIXEL_ARGS )
  1321. {
  1322.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1323.    Display *dpy = xmesa->xm_visual->display;
  1324.    Drawable buffer = xmesa->xm_buffer->buffer;
  1325.    GC gc = xmesa->xm_buffer->gc2;
  1326.    register GLuint i;
  1327.    for (i=0;i<n;i++) {
  1328.       if (mask[i]) {
  1329.          unsigned long p;
  1330.          PACK_TRUEDITHER(p, x[i], y[i], red[i], green[i], blue[i]);
  1331.          XSetForeground( dpy, gc, p );
  1332.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1333.       }
  1334.    }
  1335. }
  1336.  
  1337.  
  1338. /*
  1339.  * Write an array of PF_8A8B8G8R pixels to a pixmap.
  1340.  */
  1341. static void write_pixels_8A8B8G8R_pixmap( COLOR_PIXEL_ARGS )
  1342. {
  1343.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1344.    Display *dpy = xmesa->xm_visual->display;
  1345.    Drawable buffer = xmesa->xm_buffer->buffer;
  1346.    GC gc = xmesa->xm_buffer->gc2;
  1347.    register GLuint i;
  1348.    for (i=0;i<n;i++) {
  1349.       if (mask[i]) {
  1350.      XSetForeground( dpy, gc,
  1351.                          PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] ));
  1352.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1353.       }
  1354.    }
  1355. }
  1356.  
  1357.  
  1358. /*
  1359.  * Write an array of PF_8R8G8B pixels to a pixmap.
  1360.  */
  1361. static void write_pixels_8R8G8B_pixmap( COLOR_PIXEL_ARGS )
  1362. {
  1363.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1364.    Display *dpy = xmesa->xm_visual->display;
  1365.    Drawable buffer = xmesa->xm_buffer->buffer;
  1366.    GC gc = xmesa->xm_buffer->gc2;
  1367.    register GLuint i;
  1368.    for (i=0;i<n;i++) {
  1369.       if (mask[i]) {
  1370.      XSetForeground( dpy, gc, PACK_8R8G8B( red[i], green[i], blue[i] ) );
  1371.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1372.       }
  1373.    }
  1374. }
  1375.  
  1376.  
  1377. /*
  1378.  * Write an array of PF_5R6G5B pixels to a pixmap.
  1379.  */
  1380. static void write_pixels_5R6G5B_pixmap( COLOR_PIXEL_ARGS )
  1381. {
  1382.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1383.    Display *dpy = xmesa->xm_visual->display;
  1384.    Drawable buffer = xmesa->xm_buffer->buffer;
  1385.    GC gc = xmesa->xm_buffer->gc2;
  1386.    register GLuint i;
  1387.    for (i=0;i<n;i++) {
  1388.       if (mask[i]) {
  1389.      XSetForeground( dpy, gc, PACK_5R6G5B( red[i], green[i], blue[i] ) );
  1390.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1391.       }
  1392.    }
  1393. }
  1394.  
  1395.  
  1396. /*
  1397.  * Write an array of PF_DITHER pixels to a pixmap.
  1398.  */
  1399. static void write_pixels_DITHER_pixmap( COLOR_PIXEL_ARGS )
  1400. {
  1401.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1402.    Display *dpy = xmesa->xm_visual->display;
  1403.    Drawable buffer = xmesa->xm_buffer->buffer;
  1404.    GC gc = xmesa->xm_buffer->gc2;
  1405.    register GLuint i;
  1406.    DITHER_SETUP;
  1407.    for (i=0;i<n;i++) {
  1408.       if (mask[i]) {
  1409.      XSetForeground( dpy, gc,
  1410.                          DITHER(x[i], y[i], red[i], green[i], blue[i]) );
  1411.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1412.       }
  1413.    }
  1414. }
  1415.  
  1416.  
  1417. /*
  1418.  * Write an array of PF_1BIT pixels to a pixmap.
  1419.  */
  1420. static void write_pixels_1BIT_pixmap( COLOR_PIXEL_ARGS )
  1421. {
  1422.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1423.    Display *dpy = xmesa->xm_visual->display;
  1424.    Drawable buffer = xmesa->xm_buffer->buffer;
  1425.    GC gc = xmesa->xm_buffer->gc2;
  1426.    register GLuint i;
  1427.    SETUP_1BIT;
  1428.    for (i=0;i<n;i++) {
  1429.       if (mask[i]) {
  1430.      XSetForeground( dpy, gc,
  1431.                          DITHER_1BIT( x[i], y[i], red[i], green[i], blue[i] ));
  1432.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1433.       }
  1434.    }
  1435. }
  1436.  
  1437.  
  1438. /*
  1439.  * Write an array of PF_HPCR pixels to a pixmap.
  1440.  */
  1441. static void write_pixels_HPCR_pixmap( COLOR_PIXEL_ARGS )
  1442. {
  1443.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1444.    Display *dpy = xmesa->xm_visual->display;
  1445.    Drawable buffer = xmesa->xm_buffer->buffer;
  1446.    GC gc = xmesa->xm_buffer->gc2;
  1447.    register GLuint i;
  1448.    for (i=0;i<n;i++) {
  1449.       if (mask[i]) {
  1450.          XSetForeground( dpy, gc,
  1451.                          DITHER_HPCR( x[i], y[i], red[i], green[i], blue[i] ));
  1452.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1453.       }
  1454.    }
  1455. }
  1456.  
  1457.  
  1458. /*
  1459.  * Write an array of PF_LOOKUP pixels to a pixmap.
  1460.  */
  1461. static void write_pixels_LOOKUP_pixmap( COLOR_PIXEL_ARGS )
  1462. {
  1463.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1464.    Display *dpy = xmesa->xm_visual->display;
  1465.    Drawable buffer = xmesa->xm_buffer->buffer;
  1466.    GC gc = xmesa->xm_buffer->gc2;
  1467.    register GLuint i;
  1468.    LOOKUP_SETUP;
  1469.    for (i=0;i<n;i++) {
  1470.       if (mask[i]) {
  1471.          XSetForeground( dpy, gc, LOOKUP( red[i], green[i], blue[i] ) );
  1472.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1473.       }
  1474.    }
  1475. }
  1476.  
  1477.  
  1478. /*
  1479.  * Write an array of PF_GRAYSCALE pixels to a pixmap.
  1480.  */
  1481. static void write_pixels_GRAYSCALE_pixmap( COLOR_PIXEL_ARGS )
  1482. {
  1483.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1484.    Display *dpy = xmesa->xm_visual->display;
  1485.    Drawable buffer = xmesa->xm_buffer->buffer;
  1486.    GC gc = xmesa->xm_buffer->gc2;
  1487.    register GLuint i;
  1488.    for (i=0;i<n;i++) {
  1489.       if (mask[i]) {
  1490.          XSetForeground( dpy, gc, GRAY_RGB( red[i], green[i], blue[i] ) );
  1491.          XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  1492.       }
  1493.    }
  1494. }
  1495.  
  1496.  
  1497. /*
  1498.  * Write an array of PF_TRUECOLOR pixels to an ximage.
  1499.  */
  1500. static void write_pixels_TRUECOLOR_ximage( COLOR_PIXEL_ARGS )
  1501. {
  1502.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1503.    XImage *img = xmesa->xm_buffer->backimage;
  1504.    register GLuint i;
  1505.    for (i=0;i<n;i++) {
  1506.       if (mask[i]) {
  1507.          unsigned long p;
  1508.          PACK_TRUECOLOR( p, red[i], green[i], blue[i] );
  1509.      XPutPixel( img, x[i], FLIP(y[i]), p );
  1510.       }
  1511.    }
  1512. }
  1513.  
  1514.  
  1515. /*
  1516.  * Write an array of PF_TRUEDITHER pixels to an XImage.
  1517.  */
  1518. static void write_pixels_TRUEDITHER_ximage( COLOR_PIXEL_ARGS )
  1519. {
  1520.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1521.    XImage *img = xmesa->xm_buffer->backimage;
  1522.    register GLuint i;
  1523.    for (i=0;i<n;i++) {
  1524.       if (mask[i]) {
  1525.          unsigned long p;
  1526.          PACK_TRUEDITHER(p, x[i], FLIP(y[i]), red[i], green[i], blue[i]);
  1527.      XPutPixel( img, x[i], FLIP(y[i]), p );
  1528.       }
  1529.    }
  1530. }
  1531.  
  1532.  
  1533. /*
  1534.  * Write an array of PF_8A8B8G8R pixels to an ximage.
  1535.  */
  1536. static void write_pixels_8A8B8G8R_ximage( COLOR_PIXEL_ARGS )
  1537. {
  1538.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1539.    register GLuint i;
  1540.    for (i=0;i<n;i++) {
  1541.       if (mask[i]) {
  1542.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1543.          *ptr = PACK_8A8B8G8R( red[i], green[i], blue[i], alpha[i] );
  1544.       }
  1545.    }
  1546. }
  1547.  
  1548.  
  1549. /*
  1550.  * Write an array of PF_8R8G8B pixels to an ximage.
  1551.  */
  1552. static void write_pixels_8R8G8B_ximage( COLOR_PIXEL_ARGS )
  1553. {
  1554.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1555.    register GLuint i;
  1556.    for (i=0;i<n;i++) {
  1557.       if (mask[i]) {
  1558.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  1559.          *ptr = PACK_8R8G8B( red[i], green[i], blue[i] );
  1560.       }
  1561.    }
  1562. }
  1563.  
  1564.  
  1565. /*
  1566.  * Write an array of PF_5R6G5B pixels to an ximage.
  1567.  */
  1568. static void write_pixels_5R6G5B_ximage( COLOR_PIXEL_ARGS )
  1569. {
  1570.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1571.    register GLuint i;
  1572.    for (i=0;i<n;i++) {
  1573.       if (mask[i]) {
  1574.      GLushort *ptr = PIXELADDR2( x[i], y[i] );
  1575.          *ptr = PACK_5R6G5B( red[i], green[i], blue[i] );
  1576.       }
  1577.    }
  1578. }
  1579.  
  1580.  
  1581. /*
  1582.  * Write an array of PF_DITHER pixels to an XImage.
  1583.  */
  1584. static void write_pixels_DITHER_ximage( COLOR_PIXEL_ARGS )
  1585. {
  1586.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1587.    XImage *img = xmesa->xm_buffer->backimage;
  1588.    register GLuint i;
  1589.    DITHER_SETUP;
  1590.    for (i=0;i<n;i++) {
  1591.       if (mask[i]) {
  1592.      XPutPixel( img, x[i], FLIP(y[i]),
  1593.                     DITHER( x[i], y[i], red[i], green[i], blue[i] ) );
  1594.       }
  1595.    }
  1596. }
  1597.  
  1598.  
  1599. /*
  1600.  * Write an array of 8-bit PF_DITHER pixels to an XImage.
  1601.  */
  1602. static void write_pixels_DITHER8_ximage( COLOR_PIXEL_ARGS )
  1603. {
  1604.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1605.    register GLuint i;
  1606.    DITHER_SETUP;
  1607.    for (i=0;i<n;i++) {
  1608.       if (mask[i]) {
  1609.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1610.      *ptr = DITHER( x[i], y[i], red[i], green[i], blue[i] );
  1611.       }
  1612.    }
  1613. }
  1614.  
  1615.  
  1616. /*
  1617.  * Write an array of PF_1BIT pixels to an XImage.
  1618.  */
  1619. static void write_pixels_1BIT_ximage( COLOR_PIXEL_ARGS )
  1620. {
  1621.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1622.    XImage *img = xmesa->xm_buffer->backimage;
  1623.    register GLuint i;
  1624.    SETUP_1BIT;
  1625.    for (i=0;i<n;i++) {
  1626.       if (mask[i]) {
  1627.      XPutPixel( img, x[i], FLIP(y[i]),
  1628.                     DITHER_1BIT( x[i], y[i], red[i], green[i], blue[i] ));
  1629.       }
  1630.    }
  1631. }
  1632.  
  1633.  
  1634. /*
  1635.  * Write an array of PF_HPCR pixels to an XImage.
  1636.  */
  1637. static void write_pixels_HPCR_ximage( COLOR_PIXEL_ARGS )
  1638. {
  1639.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1640.    register GLuint i;
  1641.    for (i=0;i<n;i++) {
  1642.       if (mask[i]) {
  1643.          GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1644.          *ptr = DITHER_HPCR( x[i], y[i], red[i], green[i], blue[i] );
  1645.       }
  1646.    }
  1647. }
  1648.  
  1649.  
  1650. /*
  1651.  * Write an array of PF_LOOKUP pixels to an XImage.
  1652.  */
  1653. static void write_pixels_LOOKUP_ximage( COLOR_PIXEL_ARGS )
  1654. {
  1655.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1656.    XImage *img = xmesa->xm_buffer->backimage;
  1657.    register GLuint i;
  1658.    LOOKUP_SETUP;
  1659.    for (i=0;i<n;i++) {
  1660.       if (mask[i]) {
  1661.      XPutPixel( img, x[i], FLIP(y[i]), LOOKUP(red[i], green[i], blue[i]) );
  1662.       }
  1663.    }
  1664. }
  1665.  
  1666.  
  1667. /*
  1668.  * Write an array of 8-bit PF_LOOKUP pixels to an XImage.
  1669.  */
  1670. static void write_pixels_LOOKUP8_ximage( COLOR_PIXEL_ARGS )
  1671. {
  1672.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1673.    register GLuint i;
  1674.    LOOKUP_SETUP;
  1675.    for (i=0;i<n;i++) {
  1676.       if (mask[i]) {
  1677.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  1678.      *ptr = LOOKUP( red[i], green[i], blue[i] );
  1679.       }
  1680.    }
  1681. }
  1682.  
  1683.  
  1684. /*
  1685.  * Write an array of PF_GRAYSCALE pixels to an XImage.
  1686.  */
  1687. static void write_pixels_GRAYSCALE_ximage( COLOR_PIXEL_ARGS )
  1688. {
  1689.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1690.    XImage *img = xmesa->xm_buffer->backimage;
  1691.    register GLuint i;
  1692.    for (i=0;i<n;i++) {
  1693.       if (mask[i]) {
  1694.      XPutPixel( img, x[i], FLIP(y[i]),
  1695.                     GRAY_RGB( red[i], green[i], blue[i] ) );
  1696.       }
  1697.    }
  1698. }
  1699.  
  1700.  
  1701. /*
  1702.  * Write an array of 8-bit PF_GRAYSCALE pixels to an XImage.
  1703.  */
  1704. static void write_pixels_GRAYSCALE8_ximage( COLOR_PIXEL_ARGS )
  1705. {
  1706.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1707.    register GLuint i;
  1708.    for (i=0;i<n;i++) {
  1709.       if (mask[i]) {
  1710.      GLubyte *ptr = PIXELADDR1( x[i], y[i] );
  1711.      *ptr = GRAY_RGB( red[i], green[i], blue[i] );
  1712.       }
  1713.    }
  1714. }
  1715.  
  1716.  
  1717.  
  1718.  
  1719. /**********************************************************************/
  1720. /*** Write MONO COLOR SPAN functions                                ***/
  1721. /**********************************************************************/
  1722.  
  1723. #define MONO_SPAN_ARGS    GLcontext *ctx,    \
  1724.              GLuint n, GLint x, GLint y, const GLubyte mask[]
  1725.  
  1726.  
  1727. /*
  1728.  * Write a span of identical pixels to a pixmap.  The pixel value is
  1729.  * the one set by DD.color() or DD.index().
  1730.  */
  1731. static void write_span_mono_pixmap( MONO_SPAN_ARGS )
  1732. {
  1733.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1734.    Display *dpy = xmesa->xm_visual->display;
  1735.    Drawable buffer = xmesa->xm_buffer->buffer;
  1736.    GC gc = xmesa->xm_buffer->gc1;
  1737.    register GLuint i;
  1738.    register GLboolean write_all;
  1739.    y = FLIP( y );
  1740.    write_all = GL_TRUE;
  1741.    for (i=0;i<n;i++) {
  1742.       if (!mask[i]) {
  1743.      write_all = GL_FALSE;
  1744.      break;
  1745.       }
  1746.    }
  1747.    if (write_all) {
  1748.       XFillRectangle( dpy, buffer, gc, (int) x, (int) y, n, 1 );
  1749.    }
  1750.    else {
  1751.       for (i=0;i<n;i++,x++) {
  1752.      if (mask[i]) {
  1753.         XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  1754.      }
  1755.       }
  1756.    }
  1757. }
  1758.  
  1759.  
  1760. /*
  1761.  * Write a span of PF_TRUEDITHER pixels to a pixmap.  The pixel value is
  1762.  * the one set by DD.color() or DD.index().
  1763.  */
  1764. static void write_span_mono_TRUEDITHER_pixmap( MONO_SPAN_ARGS )
  1765. {
  1766.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1767.    Display *dpy = xmesa->xm_visual->display;
  1768.    Drawable buffer = xmesa->xm_buffer->buffer;
  1769.    GC gc = xmesa->xm_buffer->gc2;
  1770.    register GLuint i;
  1771.    register GLubyte r, g, b;
  1772.    int yy = FLIP( y );
  1773.    r = xmesa->red;
  1774.    g = xmesa->green;
  1775.    b = xmesa->blue;
  1776.    for (i=0;i<n;i++,x++) {
  1777.       if (mask[i]) {
  1778.          unsigned long p;
  1779.          PACK_TRUEDITHER(p, x, yy, r, g, b);
  1780.          XSetForeground( dpy, gc, p );
  1781.          XDrawPoint( dpy, buffer, gc, (int) x, (int) yy );
  1782.       }
  1783.    }
  1784. }
  1785.  
  1786.  
  1787. /*
  1788.  * Write a span of PF_DITHER pixels to a pixmap.  The pixel value is
  1789.  * the one set by DD.color() or DD.index().
  1790.  */
  1791. static void write_span_mono_DITHER_pixmap( MONO_SPAN_ARGS )
  1792. {
  1793.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1794.    Display *dpy = xmesa->xm_visual->display;
  1795.    Drawable buffer = xmesa->xm_buffer->buffer;
  1796.    GC gc = xmesa->xm_buffer->gc2;
  1797.    register GLuint i;
  1798.    register GLubyte r, g, b;
  1799.    int yy = FLIP( y );
  1800.    XDITHER_SETUP(yy);
  1801.    r = xmesa->red;
  1802.    g = xmesa->green;
  1803.    b = xmesa->blue;
  1804.    for (i=0;i<n;i++,x++) {
  1805.       if (mask[i]) {
  1806.          XSetForeground( dpy, gc, XDITHER( x, r, g, b ) );
  1807.          XDrawPoint( dpy, buffer, gc, (int) x, (int) yy );
  1808.       }
  1809.    }
  1810. }
  1811.  
  1812.  
  1813. /*
  1814.  * Write a span of PF_1BIT pixels to a pixmap.  The pixel value is
  1815.  * the one set by DD.color() or DD.index().
  1816.  */
  1817. static void write_span_mono_1BIT_pixmap( MONO_SPAN_ARGS )
  1818. {
  1819.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1820.    Display *dpy = xmesa->xm_visual->display;
  1821.    Drawable buffer = xmesa->xm_buffer->buffer;
  1822.    GC gc = xmesa->xm_buffer->gc2;
  1823.    register GLuint i;
  1824.    register GLubyte r, g, b;
  1825.    SETUP_1BIT;
  1826.    r = xmesa->red;
  1827.    g = xmesa->green;
  1828.    b = xmesa->blue;
  1829.    y = FLIP( y );
  1830.    for (i=0;i<n;i++,x++) {
  1831.       if (mask[i]) {
  1832.          XSetForeground( dpy, gc, DITHER_1BIT( x, y, r, g, b ) );
  1833.          XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  1834.       }
  1835.    }
  1836. }
  1837.  
  1838.  
  1839. /*
  1840.  * Write a span of identical pixels to an XImage.  The pixel value is
  1841.  * the one set by DD.color() or DD.index().
  1842.  */
  1843. static void write_span_mono_ximage( MONO_SPAN_ARGS )
  1844. {
  1845.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1846.    XImage *img = xmesa->xm_buffer->backimage;
  1847.    register GLuint i;
  1848.    register unsigned long p = xmesa->pixel;
  1849.    y = FLIP( y );
  1850.    for (i=0;i<n;i++,x++) {
  1851.       if (mask[i]) {
  1852.      XPutPixel( img, x, y, p );
  1853.       }
  1854.    }
  1855. }
  1856.  
  1857.  
  1858. /*
  1859.  * Write a span of identical PF_TRUEDITHER pixels to an XImage.  The pixel
  1860.  * value is the one set by DD.color().
  1861.  */
  1862. static void write_span_mono_TRUEDITHER_ximage( MONO_SPAN_ARGS )
  1863. {
  1864.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1865.    XImage *img = xmesa->xm_buffer->backimage;
  1866.    GLuint i;
  1867.    GLint r = xmesa->red;
  1868.    GLint g = xmesa->green;
  1869.    GLint b = xmesa->blue;
  1870.    y = FLIP( y );
  1871.    for (i=0;i<n;i++) {
  1872.       if (mask[i]) {
  1873.          unsigned long p;
  1874.          PACK_TRUEDITHER( p, x+i, y, r, g, b);
  1875.      XPutPixel( img, x+i, y, p );
  1876.       }
  1877.    }
  1878. }
  1879.  
  1880.  
  1881. /*
  1882.  * Write a span of identical 8A8B8G8R pixels to an XImage.  The pixel
  1883.  * value is the one set by DD.color().
  1884.  */
  1885. static void write_span_mono_8A8B8G8R_ximage( MONO_SPAN_ARGS )
  1886. {
  1887.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1888.    GLuint i, p, *ptr;
  1889.    p = (GLuint) xmesa->pixel;
  1890.    ptr = PIXELADDR4( x, y );
  1891.    for (i=0;i<n;i++) {
  1892.       if (mask[i]) {
  1893.      ptr[i] = p;
  1894.       }
  1895.    }
  1896. }
  1897.  
  1898.  
  1899. /*
  1900.  * Write a span of identical 8R8G8B pixels to an XImage.  The pixel
  1901.  * value is the one set by DD.color().
  1902.  */
  1903. static void write_span_mono_8R8G8B_ximage( MONO_SPAN_ARGS )
  1904. {
  1905.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1906.    GLuint i, p, *ptr;
  1907.    p = (GLuint) xmesa->pixel;
  1908.    ptr = PIXELADDR4( x, y );
  1909.    for (i=0;i<n;i++) {
  1910.       if (mask[i]) {
  1911.      ptr[i] = p;
  1912.       }
  1913.    }
  1914. }
  1915.  
  1916.  
  1917. /*
  1918.  * Write a span of identical DITHER pixels to an XImage.  The pixel
  1919.  * value is the one set by DD.color().
  1920.  */
  1921. static void write_span_mono_DITHER_ximage( MONO_SPAN_ARGS )
  1922. {
  1923.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1924.    XImage *img = xmesa->xm_buffer->backimage;
  1925.    register GLuint i;
  1926.    register GLubyte r, g, b;
  1927.    int yy = FLIP(y);
  1928.    XDITHER_SETUP(yy);
  1929.    r = xmesa->red;
  1930.    g = xmesa->green;
  1931.    b = xmesa->blue;
  1932.    for (i=0;i<n;i++,x++) {
  1933.       if (mask[i]) {
  1934.      XPutPixel( img, x, yy, XDITHER( x, r, g, b ) );
  1935.       }
  1936.    }
  1937. }
  1938.  
  1939.  
  1940. /*
  1941.  * Write a span of identical 8-bit DITHER pixels to an XImage.  The pixel
  1942.  * value is the one set by DD.color().
  1943.  */
  1944. static void write_span_mono_DITHER8_ximage( MONO_SPAN_ARGS )
  1945. {
  1946.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1947.    register GLuint i;
  1948.    register GLubyte *ptr = PIXELADDR1(x,y);
  1949.    register GLubyte r, g, b;
  1950.    XDITHER_SETUP(y);
  1951.    r = xmesa->red;
  1952.    g = xmesa->green;
  1953.    b = xmesa->blue;
  1954.    for (i=0;i<n;i++,x++) {
  1955.       if (mask[i]) {
  1956.      ptr[i] = XDITHER( x, r, g, b );
  1957.       }
  1958.    }
  1959. }
  1960.  
  1961.  
  1962. /*
  1963.  * Write a span of identical 8-bit LOOKUP pixels to an XImage.  The pixel
  1964.  * value is the one set by DD.color().
  1965.  */
  1966. static void write_span_mono_LOOKUP8_ximage( MONO_SPAN_ARGS )
  1967. {
  1968.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1969.    register GLuint i;
  1970.    register GLubyte *ptr = PIXELADDR1(x,y);
  1971.    register GLubyte pixel = xmesa->pixel;
  1972.    for (i=0;i<n;i++) {
  1973.       if (mask[i]) {
  1974.      ptr[i] = pixel;
  1975.       }
  1976.    }
  1977. }
  1978.  
  1979.  
  1980. /*
  1981.  * Write a span of identical PF_1BIT pixels to an XImage.  The pixel
  1982.  * value is the one set by DD.color().
  1983.  */
  1984. static void write_span_mono_1BIT_ximage( MONO_SPAN_ARGS )
  1985. {
  1986.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  1987.    XImage *img = xmesa->xm_buffer->backimage;
  1988.    register GLuint i;
  1989.    register GLubyte r, g, b;
  1990.    SETUP_1BIT;
  1991.    r = xmesa->red;
  1992.    g = xmesa->green;
  1993.    b = xmesa->blue;
  1994.    y = FLIP(y);
  1995.    for (i=0;i<n;i++,x++) {
  1996.       if (mask[i]) {
  1997.      XPutPixel( img, x, y, DITHER_1BIT( x, y, r, g, b ) );
  1998.       }
  1999.    }
  2000. }
  2001.  
  2002.  
  2003. /*
  2004.  * Write a span of identical HPCR pixels to an XImage.  The pixel
  2005.  * value is the one set by DD.color().
  2006.  */
  2007. static void write_span_mono_HPCR_ximage( MONO_SPAN_ARGS )
  2008. {
  2009.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2010.    register GLuint i;
  2011.    register GLubyte *ptr = PIXELADDR1(x,y);
  2012.    register GLubyte r, g, b;
  2013.    r = xmesa->red;
  2014.    g = xmesa->green;
  2015.    b = xmesa->blue;
  2016.    for (i=0;i<n;i++,x++) {
  2017.       if (mask[i]) {
  2018.          ptr[i] = DITHER_HPCR( x, y, r, g, b );
  2019.       }
  2020.    }
  2021. }
  2022.  
  2023.  
  2024.  
  2025. /*
  2026.  * Write a span of identical 8-bit GRAYSCALE pixels to an XImage.  The pixel
  2027.  * value is the one set by DD.color().
  2028.  */
  2029. static void write_span_mono_GRAYSCALE8_ximage( MONO_SPAN_ARGS )
  2030. {
  2031.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2032.    GLuint i;
  2033.    unsigned long p = xmesa->pixel;
  2034.    GLubyte *ptr = PIXELADDR1(x,y);
  2035.    for (i=0;i<n;i++) {
  2036.       if (mask[i]) {
  2037.      ptr[i] = p;
  2038.       }
  2039.    }
  2040. }
  2041.  
  2042.  
  2043.  
  2044. /**********************************************************************/
  2045. /*** Write MONO COLOR PIXELS functions                              ***/
  2046. /**********************************************************************/
  2047.  
  2048. #define MONO_PIXEL_ARGS    GLcontext *ctx,                    \
  2049.             GLuint n, const GLint x[], const GLint y[],    \
  2050.             const GLubyte mask[]
  2051.  
  2052. /*
  2053.  * Write an array of identical pixels to a pixmap.  The pixel value is
  2054.  * the one set by DD.color() or DD.index.
  2055.  */
  2056. static void write_pixels_mono_pixmap( MONO_PIXEL_ARGS )
  2057. {
  2058.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2059.    Display *dpy = xmesa->xm_visual->display;
  2060.    Drawable buffer = xmesa->xm_buffer->buffer;
  2061.    GC gc = xmesa->xm_buffer->gc1;
  2062.    register GLuint i;
  2063.    for (i=0;i<n;i++) {
  2064.       if (mask[i]) {
  2065.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2066.       }
  2067.    }
  2068. }
  2069.  
  2070.  
  2071. /*
  2072.  * Write an array of PF_TRUEDITHER pixels to a pixmap.  The pixel value is
  2073.  * the one set by DD.color() or DD.index.
  2074.  */
  2075. static void write_pixels_mono_TRUEDITHER_pixmap( MONO_PIXEL_ARGS )
  2076. {
  2077.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2078.    Display *dpy = xmesa->xm_visual->display;
  2079.    Drawable buffer = xmesa->xm_buffer->buffer;
  2080.    GC gc = xmesa->xm_buffer->gc2;
  2081.    register GLuint i;
  2082.    register GLubyte r, g, b;
  2083.    r = xmesa->red;
  2084.    g = xmesa->green;
  2085.    b = xmesa->blue;
  2086.    for (i=0;i<n;i++) {
  2087.       if (mask[i]) {
  2088.          unsigned long p;
  2089.          PACK_TRUEDITHER(p, x[i], y[i], r, g, b);
  2090.          XSetForeground( dpy, gc, p );
  2091.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2092.       }
  2093.    }
  2094. }
  2095.  
  2096.  
  2097. /*
  2098.  * Write an array of PF_DITHER pixels to a pixmap.  The pixel value is
  2099.  * the one set by DD.color() or DD.index.
  2100.  */
  2101. static void write_pixels_mono_DITHER_pixmap( MONO_PIXEL_ARGS )
  2102. {
  2103.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2104.    Display *dpy = xmesa->xm_visual->display;
  2105.    Drawable buffer = xmesa->xm_buffer->buffer;
  2106.    GC gc = xmesa->xm_buffer->gc2;
  2107.    register GLuint i;
  2108.    register GLubyte r, g, b;
  2109.    DITHER_SETUP;
  2110.    r = xmesa->red;
  2111.    g = xmesa->green;
  2112.    b = xmesa->blue;
  2113.    for (i=0;i<n;i++) {
  2114.       if (mask[i]) {
  2115.          XSetForeground( dpy, gc, DITHER( x[i], y[i], r, g, b ) );
  2116.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2117.       }
  2118.    }
  2119. }
  2120.  
  2121.  
  2122. /*
  2123.  * Write an array of PF_1BIT pixels to a pixmap.  The pixel value is
  2124.  * the one set by DD.color() or DD.index.
  2125.  */
  2126. static void write_pixels_mono_1BIT_pixmap( MONO_PIXEL_ARGS )
  2127. {
  2128.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2129.    Display *dpy = xmesa->xm_visual->display;
  2130.    Drawable buffer = xmesa->xm_buffer->buffer;
  2131.    GC gc = xmesa->xm_buffer->gc2;
  2132.    register GLuint i;
  2133.    register GLubyte r, g, b;
  2134.    SETUP_1BIT;
  2135.    r = xmesa->red;
  2136.    g = xmesa->green;
  2137.    b = xmesa->blue;
  2138.    for (i=0;i<n;i++) {
  2139.       if (mask[i]) {
  2140.          XSetForeground( dpy, gc, DITHER_1BIT( x[i], y[i], r, g, b ) );
  2141.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2142.       }
  2143.    }
  2144. }
  2145.  
  2146.  
  2147. /*
  2148.  * Write an array of identical pixels to an XImage.  The pixel value is
  2149.  * the one set by DD.color() or DD.index.
  2150.  */
  2151. static void write_pixels_mono_ximage( MONO_PIXEL_ARGS )
  2152. {
  2153.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2154.    XImage *img = xmesa->xm_buffer->backimage;
  2155.    register GLuint i;
  2156.    register unsigned long p = xmesa->pixel;
  2157.    for (i=0;i<n;i++) {
  2158.       if (mask[i]) {
  2159.      XPutPixel( img, x[i], FLIP(y[i]), p );
  2160.       }
  2161.    }
  2162. }
  2163.  
  2164.  
  2165. /*
  2166.  * Write an array of identical TRUEDITHER pixels to an XImage.
  2167.  * The pixel value is the one set by DD.color() or DD.index.
  2168.  */
  2169. static void write_pixels_mono_TRUEDITHER_ximage( MONO_PIXEL_ARGS )
  2170. {
  2171.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2172.    XImage *img = xmesa->xm_buffer->backimage;
  2173.    register GLuint i;
  2174.    int r = xmesa->red;
  2175.    int g = xmesa->green;
  2176.    int b = xmesa->blue;
  2177.    for (i=0;i<n;i++) {
  2178.       if (mask[i]) {
  2179.          unsigned long p;
  2180.          PACK_TRUEDITHER(p, x[i], FLIP(y[i]), r, g, b);
  2181.      XPutPixel( img, x[i], FLIP(y[i]), p );
  2182.       }
  2183.    }
  2184. }
  2185.  
  2186.  
  2187.  
  2188. /*
  2189.  * Write an array of identical 8A8B8G8R pixels to an XImage.  The pixel value
  2190.  * is the one set by DD.color().
  2191.  */
  2192. static void write_pixels_mono_8A8B8G8R_ximage( MONO_PIXEL_ARGS )
  2193. {
  2194.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2195.    register GLuint i;
  2196.    register GLuint p = (GLuint) xmesa->pixel;
  2197.    for (i=0;i<n;i++) {
  2198.       if (mask[i]) {
  2199.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  2200.      *ptr = p;
  2201.       }
  2202.    }
  2203. }
  2204.  
  2205.  
  2206. /*
  2207.  * Write an array of identical 8R8G8B pixels to an XImage.  The pixel value
  2208.  * is the one set by DD.color().
  2209.  */
  2210. static void write_pixels_mono_8R8G8B_ximage( MONO_PIXEL_ARGS )
  2211. {
  2212.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2213.    register GLuint i;
  2214.    register GLuint p = (GLuint) xmesa->pixel;
  2215.    for (i=0;i<n;i++) {
  2216.       if (mask[i]) {
  2217.      GLuint *ptr = PIXELADDR4( x[i], y[i] );
  2218.      *ptr = p;
  2219.       }
  2220.    }
  2221. }
  2222.  
  2223.  
  2224. /*
  2225.  * Write an array of identical PF_DITHER pixels to an XImage.  The pixel
  2226.  * value is the one set by DD.color().
  2227.  */
  2228. static void write_pixels_mono_DITHER_ximage( MONO_PIXEL_ARGS )
  2229. {
  2230.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2231.    XImage *img = xmesa->xm_buffer->backimage;
  2232.    register GLuint i;
  2233.    register GLubyte r, g, b;
  2234.    DITHER_SETUP;
  2235.    r = xmesa->red;
  2236.    g = xmesa->green;
  2237.    b = xmesa->blue;
  2238.    for (i=0;i<n;i++) {
  2239.       if (mask[i]) {
  2240.      XPutPixel( img, x[i], FLIP(y[i]), DITHER( x[i], y[i], r, g, b ) );
  2241.       }
  2242.    }
  2243. }
  2244.  
  2245.  
  2246. /*
  2247.  * Write an array of identical 8-bit PF_DITHER pixels to an XImage.  The
  2248.  * pixel value is the one set by DD.color().
  2249.  */
  2250. static void write_pixels_mono_DITHER8_ximage( MONO_PIXEL_ARGS )
  2251. {
  2252.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2253.    register GLuint i;
  2254.    register GLubyte r, g, b;
  2255.    DITHER_SETUP;
  2256.    r = xmesa->red;
  2257.    g = xmesa->green;
  2258.    b = xmesa->blue;
  2259.    for (i=0;i<n;i++) {
  2260.       if (mask[i]) {
  2261.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2262.      *ptr = DITHER( x[i], y[i], r, g, b );
  2263.       }
  2264.    }
  2265. }
  2266.  
  2267.  
  2268. /*
  2269.  * Write an array of identical 8-bit PF_LOOKUP pixels to an XImage.  The
  2270.  * pixel value is the one set by DD.color().
  2271.  */
  2272. static void write_pixels_mono_LOOKUP8_ximage( MONO_PIXEL_ARGS )
  2273. {
  2274.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2275.    register GLuint i;
  2276.    register GLubyte pixel = xmesa->pixel;
  2277.    for (i=0;i<n;i++) {
  2278.       if (mask[i]) {
  2279.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2280.      *ptr = pixel;
  2281.       }
  2282.    }
  2283. }
  2284.  
  2285.  
  2286.  
  2287. /*
  2288.  * Write an array of identical PF_1BIT pixels to an XImage.  The pixel
  2289.  * value is the one set by DD.color().
  2290.  */
  2291. static void write_pixels_mono_1BIT_ximage( MONO_PIXEL_ARGS )
  2292. {
  2293.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2294.    XImage *img = xmesa->xm_buffer->backimage;
  2295.    register GLuint i;
  2296.    register GLubyte r, g, b;
  2297.    SETUP_1BIT;
  2298.    r = xmesa->red;
  2299.    g = xmesa->green;
  2300.    b = xmesa->blue;
  2301.    for (i=0;i<n;i++) {
  2302.       if (mask[i]) {
  2303.      XPutPixel( img, x[i], FLIP(y[i]), DITHER_1BIT( x[i], y[i], r, g, b ));
  2304.       }
  2305.    }
  2306. }
  2307.  
  2308.  
  2309. /*
  2310.  * Write an array of identical PF_HPCR pixels to an XImage.  The
  2311.  * pixel value is the one set by DD.color().
  2312.  */
  2313. static void write_pixels_mono_HPCR_ximage( MONO_PIXEL_ARGS )
  2314. {
  2315.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2316.    register GLuint i;
  2317.    register GLubyte r, g, b;
  2318.    r = xmesa->red;
  2319.    g = xmesa->green;
  2320.    b = xmesa->blue;
  2321.    for (i=0;i<n;i++) {
  2322.       if (mask[i]) {
  2323.          GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2324.          *ptr = DITHER_HPCR( x[i], y[i], r, g, b );
  2325.       }
  2326.    }
  2327. }
  2328.  
  2329.  
  2330. /*
  2331.  * Write an array of identical 8-bit PF_GRAYSCALE pixels to an XImage.  The
  2332.  * pixel value is the one set by DD.color().
  2333.  */
  2334. static void write_pixels_mono_GRAYSCALE8_ximage( MONO_PIXEL_ARGS )
  2335. {
  2336.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2337.    register GLuint i;
  2338.    register unsigned long p = xmesa->pixel;
  2339.    for (i=0;i<n;i++) {
  2340.       if (mask[i]) {
  2341.      GLubyte *ptr = PIXELADDR1(x[i],y[i]);
  2342.      *ptr = p;
  2343.       }
  2344.    }
  2345. }
  2346.  
  2347.  
  2348.  
  2349.  
  2350. /**********************************************************************/
  2351. /*** Write INDEX SPAN functions                                     ***/
  2352. /**********************************************************************/
  2353.  
  2354. #define INDEX_SPAN_ARGS    GLcontext *ctx,                    \
  2355.             GLuint n, GLint x, GLint y, const GLuint index[], \
  2356.             const GLubyte mask[]
  2357.  
  2358.  
  2359. /*
  2360.  * Write a span of CI pixels to a Pixmap.
  2361.  */
  2362. static void write_span_index_pixmap( INDEX_SPAN_ARGS )
  2363. {
  2364.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2365.    Display *dpy = xmesa->xm_visual->display;
  2366.    Drawable buffer = xmesa->xm_buffer->buffer;
  2367.    GC gc = xmesa->xm_buffer->gc2;
  2368.    register GLuint i;
  2369.    y = FLIP(y);
  2370.    for (i=0;i<n;i++,x++) {
  2371.       if (mask[i]) {
  2372.      XSetForeground( dpy, gc, (unsigned long) index[i] );
  2373.      XDrawPoint( dpy, buffer, gc, (int) x, (int) y );
  2374.       }
  2375.    }
  2376. }
  2377.  
  2378.  
  2379. /*
  2380.  * Write a span of CI pixels to an XImage.
  2381.  */
  2382. static void write_span_index_ximage( INDEX_SPAN_ARGS )
  2383. {
  2384.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2385.    XImage *img = xmesa->xm_buffer->backimage;
  2386.    register GLuint i;
  2387.    y = FLIP(y);
  2388.    for (i=0;i<n;i++,x++) {
  2389.       if (mask[i]) {
  2390.      XPutPixel( img, x, y, (unsigned long) index[i] );
  2391.       }
  2392.    }
  2393. }
  2394.  
  2395.  
  2396.  
  2397. /**********************************************************************/
  2398. /*** Write INDEX PIXELS functions                                   ***/
  2399. /**********************************************************************/
  2400.  
  2401. #define INDEX_PIXELS_ARGS    GLcontext *ctx,                \
  2402.                 GLuint n, const GLint x[], const GLint y[], \
  2403.                 const GLuint index[], const GLubyte mask[]
  2404.  
  2405.  
  2406. /*
  2407.  * Write an array of CI pixels to a Pixmap.
  2408.  */
  2409. static void write_pixels_index_pixmap( INDEX_PIXELS_ARGS )
  2410. {
  2411.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2412.    Display *dpy = xmesa->xm_visual->display;
  2413.    Drawable buffer = xmesa->xm_buffer->buffer;
  2414.    GC gc = xmesa->xm_buffer->gc2;
  2415.    register GLuint i;
  2416.    for (i=0;i<n;i++) {
  2417.       if (mask[i]) {
  2418.      XSetForeground( dpy, gc, (unsigned long) index[i] );
  2419.      XDrawPoint( dpy, buffer, gc, (int) x[i], (int) FLIP(y[i]) );
  2420.       }
  2421.    }
  2422. }
  2423.  
  2424.  
  2425. /*
  2426.  * Write an array of CI pixels to an XImage.
  2427.  */
  2428. static void write_pixels_index_ximage( INDEX_PIXELS_ARGS )
  2429. {
  2430.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2431.    XImage *img = xmesa->xm_buffer->backimage;
  2432.    register GLuint i;
  2433.    for (i=0;i<n;i++) {
  2434.       if (mask[i]) {
  2435.      XPutPixel( img, x[i], FLIP(y[i]), (unsigned long) index[i] );
  2436.       }
  2437.    }
  2438. }
  2439.  
  2440.  
  2441.  
  2442.  
  2443. /**********************************************************************/
  2444. /*****                      Pixel reading                         *****/
  2445. /**********************************************************************/
  2446.  
  2447.  
  2448.  
  2449. /*
  2450.  * Read a horizontal span of color-index pixels.
  2451.  */
  2452. static void read_index_span( GLcontext *ctx,
  2453.                  GLuint n, GLint x, GLint y, GLuint index[] )
  2454. {
  2455.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2456.    int i;
  2457.  
  2458.    y = FLIP(y);
  2459.  
  2460.    if (xmesa->xm_buffer->buffer) {
  2461.       XImage *span;
  2462.       int error;
  2463.       catch_xgetimage_errors( xmesa->display );
  2464.       span = XGetImage( xmesa->display, xmesa->xm_buffer->buffer,
  2465.                 x, y, n, 1, AllPlanes, ZPixmap );
  2466.       error = check_xgetimage_errors();
  2467.       if (span && !error) {
  2468.      for (i=0;i<n;i++) {
  2469.         index[i] = (GLuint) XGetPixel( span, i, 0 );
  2470.      }
  2471.       }
  2472.       else {
  2473.      /* return 0 pixels */
  2474.      for (i=0;i<n;i++) {
  2475.         index[i] = 0;
  2476.      }
  2477.       }
  2478.       if (span) {
  2479.      XDestroyImage( span );
  2480.       }
  2481.    }
  2482.    else if (xmesa->xm_buffer->backimage) {
  2483.       XImage *img = xmesa->xm_buffer->backimage;
  2484.       for (i=0;i<n;i++,x++) {
  2485.      index[i] = (GLuint) XGetPixel( img, x, y );
  2486.       }
  2487.    }
  2488. }
  2489.  
  2490.  
  2491.  
  2492. /*
  2493.  * Read a horizontal span of color pixels.
  2494.  */
  2495. static void read_color_span( GLcontext *ctx,
  2496.                  GLuint n, GLint x, GLint y,
  2497.                              GLubyte red[], GLubyte green[],
  2498.                              GLubyte blue[], GLubyte alpha[] )
  2499. {
  2500.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2501.    register GLuint i;
  2502.  
  2503.    if (xmesa->xm_buffer->buffer) {
  2504.       XImage *span;
  2505.       int error;
  2506.       catch_xgetimage_errors( xmesa->display );
  2507.       span = XGetImage( xmesa->display, xmesa->xm_buffer->buffer,
  2508.                 x, FLIP(y), n, 1, AllPlanes, ZPixmap );
  2509.       error = check_xgetimage_errors();
  2510.       if (span && !error) {
  2511.      switch (xmesa->pixelformat) {
  2512.         case PF_TRUECOLOR:
  2513.         case PF_TRUEDITHER:
  2514.             case PF_5R6G5B:
  2515.                {
  2516.                   unsigned long rMask = xmesa->xm_visual->visinfo->red_mask;
  2517.                   unsigned long gMask = xmesa->xm_visual->visinfo->green_mask;
  2518.                   unsigned long bMask = xmesa->xm_visual->visinfo->blue_mask;
  2519.                   GLubyte *pixelToR = xmesa->xm_visual->PixelToR;
  2520.                   GLubyte *pixelToG = xmesa->xm_visual->PixelToG;
  2521.                   GLubyte *pixelToB = xmesa->xm_visual->PixelToB;
  2522.                   GLint rShift = xmesa->xm_visual->rshift;
  2523.                   GLint gShift = xmesa->xm_visual->gshift;
  2524.                   GLint bShift = xmesa->xm_visual->bshift;
  2525.                   for (i=0;i<n;i++) {
  2526.                      unsigned long p = XGetPixel( span, i, 0 );
  2527.                      red[i]   = pixelToR[(p & rMask) >> rShift];
  2528.                      green[i] = pixelToG[(p & gMask) >> gShift];
  2529.                      blue[i]  = pixelToB[(p & bMask) >> bShift];
  2530.                      alpha[i] = 255;
  2531.                   }
  2532.                }
  2533.            break;
  2534.         case PF_8A8B8G8R:
  2535.                {
  2536.                   GLuint *ptr4 = (GLuint *) span->data;
  2537.                   for (i=0;i<n;i++) {
  2538.                      GLuint p4 = *ptr4++;
  2539.                      red[i]   = (GLubyte) ( p4        & 0xff);
  2540.                      green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2541.                      blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2542.                      alpha[i] = (GLubyte) ((p4 >> 24) & 0xff);
  2543.                   }
  2544.            }
  2545.            break;
  2546.             case PF_8R8G8B:
  2547.                {
  2548.                   GLuint *ptr4 = (GLuint *) span->data;
  2549.                   for (i=0;i<n;i++) {
  2550.                      GLuint p4 = *ptr4++;
  2551.                      red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2552.                      green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2553.                      blue[i]  = (GLubyte) ( p4        & 0xff);
  2554.                      alpha[i] = 255;
  2555.                   }
  2556.            }
  2557.            break;
  2558.             case PF_HPCR:
  2559.                {
  2560.                   GLubyte *ptr1 = (GLubyte *) span->data;
  2561.                   for (i=0;i<n;i++) {
  2562.                      GLubyte p = *ptr1++;
  2563.                      red[i]   =  p & 0xE0;
  2564.                      green[i] = (p & 0x1C) << 3;
  2565.                      blue[i]  = (p & 0x03) << 6;
  2566.                      alpha[i] = 255;
  2567.                   }
  2568.                }
  2569.                break;
  2570.         case PF_DITHER:
  2571.         case PF_LOOKUP:
  2572.         case PF_GRAYSCALE:
  2573.                {
  2574.                   GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2575.                   GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2576.                   GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2577.                   if (xmesa->xm_visual->visinfo->depth==8) {
  2578.                      GLubyte *ptr1 = (GLubyte *) span->data;
  2579.                      for (i=0;i<n;i++) {
  2580.                         unsigned long p = *ptr1++;
  2581.                         red[i]   = red_table[p];
  2582.                         green[i] = green_table[p];
  2583.                         blue[i]  = blue_table[p];
  2584.                         alpha[i] = 255;
  2585.                      }
  2586.                   }
  2587.                   else {
  2588.                      for (i=0;i<n;i++) {
  2589.                         unsigned long p = XGetPixel( span, i, 0 );
  2590.                         red[i]   = red_table[p];
  2591.                         green[i] = green_table[p];
  2592.                         blue[i]  = blue_table[p];
  2593.                         alpha[i] = 255;
  2594.                      }
  2595.                   }
  2596.                }
  2597.            break;
  2598.         case PF_1BIT:
  2599.                {
  2600.                   int bitFlip = xmesa->xm_visual->bitFlip;
  2601.                   for (i=0;i<n;i++) {
  2602.                      unsigned long p = XGetPixel( span, i, 0 ) ^ bitFlip;
  2603.                      red[i]   = (GLubyte) (p * 255);
  2604.                      green[i] = (GLubyte) (p * 255);
  2605.                      blue[i]  = (GLubyte) (p * 255);
  2606.                      alpha[i] = 255;
  2607.                   }
  2608.                }
  2609.            break;
  2610.         default:
  2611.            gl_problem(NULL,"Problem in DD.read_color_span (1)");
  2612.                return;
  2613.      }
  2614.       }
  2615.       else {
  2616.      /* return black pixels */
  2617.      for (i=0;i<n;i++) {
  2618.         red[i] = green[i] = blue[i] = alpha[i] = 0;
  2619.      }
  2620.       }
  2621.       if (span) {
  2622.      XDestroyImage( span );
  2623.       }
  2624.    }
  2625.    else if (xmesa->xm_buffer->backimage) {
  2626.       switch (xmesa->pixelformat) {
  2627.          case PF_TRUECOLOR:
  2628.          case PF_TRUEDITHER:
  2629.          case PF_5R6G5B:
  2630.             {
  2631.                unsigned long rMask = xmesa->xm_visual->visinfo->red_mask;
  2632.                unsigned long gMask = xmesa->xm_visual->visinfo->green_mask;
  2633.                unsigned long bMask = xmesa->xm_visual->visinfo->blue_mask;
  2634.                GLubyte *pixelToR = xmesa->xm_visual->PixelToR;
  2635.                GLubyte *pixelToG = xmesa->xm_visual->PixelToG;
  2636.                GLubyte *pixelToB = xmesa->xm_visual->PixelToB;
  2637.                GLint rShift = xmesa->xm_visual->rshift;
  2638.                GLint gShift = xmesa->xm_visual->gshift;
  2639.                GLint bShift = xmesa->xm_visual->bshift;
  2640.                XImage *img = xmesa->xm_buffer->backimage;
  2641.                y = FLIP(y);
  2642.                for (i=0;i<n;i++) {
  2643.                   unsigned long p = XGetPixel( img, x+i, y );
  2644.                   red[i]   = pixelToR[(p & rMask) >> rShift];
  2645.                   green[i] = pixelToG[(p & gMask) >> gShift];
  2646.                   blue[i]  = pixelToB[(p & bMask) >> bShift];
  2647.                   alpha[i] = 255;
  2648.                }
  2649.             }
  2650.             break;
  2651.      case PF_8A8B8G8R:
  2652.             {
  2653.                GLuint *ptr4 = PIXELADDR4( x, y );
  2654.                for (i=0;i<n;i++) {
  2655.                   GLuint p4 = *ptr4++;
  2656.                   red[i]   = (GLubyte) ( p4        & 0xff);
  2657.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2658.                   blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2659.                   alpha[i] = (GLint)   ((p4 >> 24) & 0xff);
  2660.                }
  2661.             }
  2662.         break;
  2663.      case PF_8R8G8B:
  2664.             {
  2665.                GLuint *ptr4 = PIXELADDR4( x, y );
  2666.                for (i=0;i<n;i++) {
  2667.                   GLuint p4 = *ptr4++;
  2668.                   red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2669.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2670.                   blue[i]  = (GLubyte) ( p4        & 0xff);
  2671.                   alpha[i] = 255;
  2672.                }
  2673.             }
  2674.         break;
  2675.          case PF_HPCR:
  2676.             {
  2677.                GLubyte *ptr1 = PIXELADDR1( x, y );
  2678.                for (i=0;i<n;i++) {
  2679.                   GLubyte p = *ptr1++;
  2680.                   red[i]   =  p & 0xE0;
  2681.                   green[i] = (p & 0x1C) << 3;
  2682.                   blue[i]  = (p & 0x03) << 6;
  2683.                   alpha[i] = 255;
  2684.                }
  2685.             }
  2686.             break;
  2687.      case PF_DITHER:
  2688.      case PF_LOOKUP:
  2689.      case PF_GRAYSCALE:
  2690.             {
  2691.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2692.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2693.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2694.                if (xmesa->xm_visual->visinfo->depth==8) {
  2695.                   GLubyte *ptr1 = PIXELADDR1(x,y);
  2696.                   for (i=0;i<n;i++) {
  2697.                      unsigned long p = *ptr1++;
  2698.                      red[i]   = red_table[p];
  2699.                      green[i] = green_table[p];
  2700.                      blue[i]  = blue_table[p];
  2701.                      alpha[i] = 255;
  2702.                   }
  2703.                }
  2704.                else {
  2705.                   XImage *img = xmesa->xm_buffer->backimage;
  2706.                   y = FLIP(y);
  2707.                   for (i=0;i<n;i++,x++) {
  2708.                      unsigned long p = XGetPixel( img, x, y );
  2709.                      red[i]   = red_table[p];
  2710.                      green[i] = green_table[p];
  2711.                      blue[i]  = blue_table[p];
  2712.                      alpha[i] = 255;
  2713.                   }
  2714.                }
  2715.             }
  2716.         break;
  2717.      case PF_1BIT:
  2718.             {
  2719.                XImage *img = xmesa->xm_buffer->backimage;
  2720.                int bitFlip = xmesa->xm_visual->bitFlip;
  2721.                y = FLIP(y);
  2722.                for (i=0;i<n;i++,x++) {
  2723.                   unsigned long p = XGetPixel( img, x, y ) ^ bitFlip;
  2724.                   red[i]   = (GLubyte) (p * 255);
  2725.                   green[i] = (GLubyte) (p * 255);
  2726.                   blue[i]  = (GLubyte) (p * 255);
  2727.                   alpha[i] = 255;
  2728.                }
  2729.         }
  2730.         break;
  2731.      default:
  2732.         gl_problem(NULL,"Problem in DD.read_color_span (2)");
  2733.             return;
  2734.       }
  2735.    }
  2736. }
  2737.  
  2738.  
  2739.  
  2740. /*
  2741.  * Read an array of color index pixels.
  2742.  */
  2743. static void read_index_pixels( GLcontext *ctx,
  2744.                    GLuint n, const GLint x[], const GLint y[],
  2745.                                GLuint indx[], const GLubyte mask[] )
  2746. {
  2747.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2748.    register GLuint i;
  2749.    if (xmesa->xm_buffer->buffer) {
  2750.       for (i=0;i<n;i++) {
  2751.          if (mask[i]) {
  2752.             indx[i] = (GLuint) read_pixel( xmesa->display,
  2753.                                            xmesa->xm_buffer->buffer,
  2754.                                            x[i], FLIP(y[i]) );
  2755.          }
  2756.       }
  2757.    }
  2758.    else if (xmesa->xm_buffer->backimage) {
  2759.       XImage *img = xmesa->xm_buffer->backimage;
  2760.       for (i=0;i<n;i++) {
  2761.          if (mask[i]) {
  2762.             indx[i] = (GLuint) XGetPixel( img, x[i], FLIP(y[i]) );
  2763.          }
  2764.       }
  2765.    }
  2766. }
  2767.  
  2768.  
  2769.  
  2770. static void read_color_pixels( GLcontext *ctx,
  2771.                    GLuint n, const GLint x[], const GLint y[],
  2772.                                GLubyte red[], GLubyte green[],
  2773.                                GLubyte blue[], GLubyte alpha[],
  2774.                                const GLubyte mask[] )
  2775. {
  2776.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  2777.    Display *dpy = xmesa->xm_visual->display;
  2778.    Drawable buffer = xmesa->xm_buffer->buffer;
  2779.    register GLuint i;
  2780.  
  2781.    if (xmesa->xm_buffer->buffer) {
  2782.       switch (xmesa->pixelformat) {
  2783.      case PF_TRUECOLOR:
  2784.          case PF_TRUEDITHER:
  2785.          case PF_5R6G5B:
  2786.             {
  2787.                unsigned long rMask = xmesa->xm_visual->visinfo->red_mask;
  2788.                unsigned long gMask = xmesa->xm_visual->visinfo->green_mask;
  2789.                unsigned long bMask = xmesa->xm_visual->visinfo->blue_mask;
  2790.                GLubyte *pixelToR = xmesa->xm_visual->PixelToR;
  2791.                GLubyte *pixelToG = xmesa->xm_visual->PixelToG;
  2792.                GLubyte *pixelToB = xmesa->xm_visual->PixelToB;
  2793.                GLint rShift = xmesa->xm_visual->rshift;
  2794.                GLint gShift = xmesa->xm_visual->gshift;
  2795.                GLint bShift = xmesa->xm_visual->bshift;
  2796.                for (i=0;i<n;i++) {
  2797.                   if (mask[i]) {
  2798.                      unsigned long p = read_pixel( dpy, buffer,
  2799.                                                    x[i], FLIP(y[i]) );
  2800.                      red[i]   = pixelToR[(p & rMask) >> rShift];
  2801.                      green[i] = pixelToG[(p & gMask) >> gShift];
  2802.                      blue[i]  = pixelToB[(p & bMask) >> bShift];
  2803.                      alpha[i] = 255;
  2804.                   }
  2805.                }
  2806.             }
  2807.             break;
  2808.      case PF_8A8B8G8R:
  2809.         for (i=0;i<n;i++) {
  2810.                if (mask[i]) {
  2811.                   unsigned long p = read_pixel( dpy, buffer,
  2812.                                                 x[i], FLIP(y[i]) );
  2813.                   red[i]   = (GLubyte) ( p        & 0xff);
  2814.                   green[i] = (GLubyte) ((p >> 8)  & 0xff);
  2815.                   blue[i]  = (GLubyte) ((p >> 16) & 0xff);
  2816.                   alpha[i] = (GLubyte) ((p >> 24) & 0xff);
  2817.                }
  2818.         }
  2819.         break;
  2820.      case PF_8R8G8B:
  2821.         for (i=0;i<n;i++) {
  2822.                if (mask[i]) {
  2823.                   unsigned long p = read_pixel( dpy, buffer,
  2824.                                                 x[i], FLIP(y[i]) );
  2825.                   red[i]   = (GLubyte) ((p >> 16) & 0xff);
  2826.                   green[i] = (GLubyte) ((p >> 8)  & 0xff);
  2827.                   blue[i]  = (GLubyte) ( p        & 0xff);
  2828.                   alpha[i] = 255;
  2829.                }
  2830.         }
  2831.         break;
  2832.          case PF_HPCR:
  2833.             {
  2834.                for (i=0;i<n;i++) {
  2835.                   if (mask[i]) {
  2836.                      unsigned long p = read_pixel( dpy, buffer,
  2837.                                                    x[i], FLIP(y[i]) );
  2838.                      red[i]   =  p & 0xE0;
  2839.                      green[i] = (p & 0x1C) << 3;
  2840.                      blue[i]  = (p & 0x03) << 6;
  2841.                      alpha[i] = 255;
  2842.                   }
  2843.                }
  2844.             }
  2845.             break;
  2846.      case PF_DITHER:
  2847.      case PF_LOOKUP:
  2848.      case PF_GRAYSCALE:
  2849.             {
  2850.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2851.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2852.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2853.                for (i=0;i<n;i++) {
  2854.                   if (mask[i]) {
  2855.                      unsigned long p = read_pixel( dpy, buffer,
  2856.                                                    x[i], FLIP(y[i]) );
  2857.                      red[i]   = red_table[p];
  2858.                      green[i] = green_table[p];
  2859.                      blue[i]  = blue_table[p];
  2860.                      alpha[i] = 255;
  2861.                   }
  2862.                }
  2863.         }
  2864.         break;
  2865.      case PF_1BIT:
  2866.             {
  2867.                int bitFlip = xmesa->xm_visual->bitFlip;
  2868.                for (i=0;i<n;i++) {
  2869.                   if (mask[i]) {
  2870.                      unsigned long p = read_pixel( dpy, buffer,
  2871.                                                    x[i], FLIP(y[i])) ^ bitFlip;
  2872.                      red[i]   = (GLubyte) (p * 255);
  2873.                      green[i] = (GLubyte) (p * 255);
  2874.                      blue[i]  = (GLubyte) (p * 255);
  2875.                      alpha[i] = 255;
  2876.                   }
  2877.                }
  2878.         }
  2879.         break;
  2880.      default:
  2881.         gl_problem(NULL,"Problem in DD.read_color_pixels (1)");
  2882.             return;
  2883.       }
  2884.    }
  2885.    else if (xmesa->xm_buffer->backimage) {
  2886.       switch (xmesa->pixelformat) {
  2887.      case PF_TRUECOLOR:
  2888.          case PF_TRUEDITHER:
  2889.          case PF_5R6G5B:
  2890.             {
  2891.                unsigned long rMask = xmesa->xm_visual->visinfo->red_mask;
  2892.                unsigned long gMask = xmesa->xm_visual->visinfo->green_mask;
  2893.                unsigned long bMask = xmesa->xm_visual->visinfo->blue_mask;
  2894.                GLubyte *pixelToR = xmesa->xm_visual->PixelToR;
  2895.                GLubyte *pixelToG = xmesa->xm_visual->PixelToG;
  2896.                GLubyte *pixelToB = xmesa->xm_visual->PixelToB;
  2897.                GLint rShift = xmesa->xm_visual->rshift;
  2898.                GLint gShift = xmesa->xm_visual->gshift;
  2899.                GLint bShift = xmesa->xm_visual->bshift;
  2900.                XImage *img = xmesa->xm_buffer->backimage;
  2901.                for (i=0;i<n;i++) {
  2902.                   if (mask[i]) {
  2903.                      unsigned long p = XGetPixel( img, x[i], FLIP(y[i]) );
  2904.                      red[i]   = pixelToR[(p & rMask) >> rShift];
  2905.                      green[i] = pixelToG[(p & gMask) >> gShift];
  2906.                      blue[i]  = pixelToB[(p & bMask) >> bShift];
  2907.                      alpha[i] = 255;
  2908.                   }
  2909.                }
  2910.             }
  2911.             break;
  2912.      case PF_8A8B8G8R:
  2913.         for (i=0;i<n;i++) {
  2914.            if (mask[i]) {
  2915.                   GLuint *ptr4 = PIXELADDR4( x[i], y[i] );
  2916.                   GLuint p4 = *ptr4;
  2917.                   red[i]   = (GLubyte) ( p4        & 0xff);
  2918.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2919.                   blue[i]  = (GLubyte) ((p4 >> 16) & 0xff);
  2920.                   alpha[i] = (GLubyte) ((p4 >> 24) & 0xff);
  2921.                }
  2922.         }
  2923.         break;
  2924.      case PF_8R8G8B:
  2925.         for (i=0;i<n;i++) {
  2926.            if (mask[i]) {
  2927.                   GLuint *ptr4 = PIXELADDR4( x[i], y[i] );
  2928.                   GLuint p4 = *ptr4;
  2929.                   red[i]   = (GLubyte) ((p4 >> 16) & 0xff);
  2930.                   green[i] = (GLubyte) ((p4 >> 8)  & 0xff);
  2931.                   blue[i]  = (GLubyte) ( p4        & 0xff);
  2932.                   alpha[i] = 255;
  2933.                }
  2934.         }
  2935.         break;
  2936.          case PF_HPCR:
  2937.             for (i=0;i<n;i++) {
  2938.                if (mask[i]) {
  2939.                   GLubyte *ptr1 = PIXELADDR1( x[i], y[i] );
  2940.                   GLubyte p = *ptr1;
  2941.                   red[i]   =  p & 0xE0;
  2942.                   green[i] = (p & 0x1C) << 3;
  2943.                   blue[i]  = (p & 0x03) << 6;
  2944.                   alpha[i] = 255;
  2945.                }
  2946.             }
  2947.             break;
  2948.      case PF_DITHER:
  2949.      case PF_LOOKUP:
  2950.      case PF_GRAYSCALE:
  2951.             {
  2952.                GLubyte *red_table   = xmesa->xm_buffer->pixel_to_r;
  2953.                GLubyte *green_table = xmesa->xm_buffer->pixel_to_g;
  2954.                GLubyte *blue_table  = xmesa->xm_buffer->pixel_to_b;
  2955.                XImage *img = xmesa->xm_buffer->backimage;
  2956.                for (i=0;i<n;i++) {
  2957.                   if (mask[i]) {
  2958.                      unsigned long p;
  2959.                      p = XGetPixel( img, x[i], FLIP(y[i]) );
  2960.                      red[i]   = red_table[p];
  2961.                      green[i] = green_table[p];
  2962.                      blue[i]  = blue_table[p];
  2963.                      alpha[i] = 255;
  2964.                   }
  2965.                }
  2966.         }
  2967.         break;
  2968.      case PF_1BIT:
  2969.             {
  2970.                XImage *img = xmesa->xm_buffer->backimage;
  2971.                int bitFlip = xmesa->xm_visual->bitFlip;
  2972.                for (i=0;i<n;i++) {
  2973.                   if (mask[i]) {
  2974.                      unsigned long p;
  2975.                      p = XGetPixel( img, x[i], FLIP(y[i]) ) ^ bitFlip;
  2976.                      red[i]   = (GLubyte) (p * 255);
  2977.                      green[i] = (GLubyte) (p * 255);
  2978.                      blue[i]  = (GLubyte) (p * 255);
  2979.                      alpha[i] = 255;
  2980.                   }
  2981.                }
  2982.         }
  2983.         break;
  2984.      default:
  2985.         gl_problem(NULL,"Problem in DD.read_color_pixels (1)");
  2986.             return;
  2987.       }
  2988.    }
  2989. }
  2990.  
  2991.  
  2992. static const char *renderer_string(void)
  2993. {
  2994.    return "X11";
  2995. }
  2996.  
  2997.  
  2998. /*
  2999.  * Initialize all the DD.* function pointers depending on the color
  3000.  * buffer configuration.  This is mainly called by XMesaMakeCurrent.
  3001.  */
  3002. void xmesa_setup_DD_pointers( GLcontext *ctx )
  3003. {
  3004.    XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
  3005.    /*
  3006.     * Always the same:
  3007.     */
  3008.    ctx->Driver.RendererString = renderer_string;
  3009.    ctx->Driver.UpdateState = xmesa_setup_DD_pointers;
  3010.    ctx->Driver.GetBufferSize = get_buffer_size;
  3011.    ctx->Driver.Flush = flush;
  3012.    ctx->Driver.Finish = finish;
  3013.  
  3014.    ctx->Driver.SetBuffer = set_buffer;
  3015.  
  3016.    ctx->Driver.Index = set_index;
  3017.    ctx->Driver.Color = set_color;
  3018.    ctx->Driver.ClearIndex = clear_index;
  3019.    ctx->Driver.ClearColor = clear_color;
  3020.    ctx->Driver.IndexMask = index_mask;
  3021.    ctx->Driver.ColorMask = color_mask;
  3022.    ctx->Driver.LogicOp = logicop;
  3023.    ctx->Driver.Dither = dither;
  3024.  
  3025.    if (!ctx->Driver.PointsFunc)
  3026.       ctx->Driver.PointsFunc = xmesa_get_points_func( ctx );
  3027.    if (!ctx->Driver.LineFunc)
  3028.       ctx->Driver.LineFunc = xmesa_get_line_func( ctx );
  3029.    if (!ctx->Driver.TriangleFunc)
  3030.       ctx->Driver.TriangleFunc = xmesa_get_triangle_func( ctx );
  3031.  
  3032.    /*
  3033.     * These drawing functions depend on color buffer config:
  3034.     */
  3035.    if (xmesa->xm_buffer->buffer!=XIMAGE) {
  3036.       /* Writing to window or back pixmap */
  3037.       ctx->Driver.Clear = clear_pixmap;
  3038.       switch (xmesa->pixelformat) {
  3039.      case PF_INDEX:
  3040.         ctx->Driver.WriteIndexSpan       = write_span_index_pixmap;
  3041.         ctx->Driver.WriteMonoindexSpan   = write_span_mono_pixmap;
  3042.         ctx->Driver.WriteIndexPixels     = write_pixels_index_pixmap;
  3043.         ctx->Driver.WriteMonoindexPixels = write_pixels_mono_pixmap;
  3044.         break;
  3045.      case PF_TRUECOLOR:
  3046.         ctx->Driver.WriteColorSpan       = write_span_TRUECOLOR_pixmap;
  3047.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3048.         ctx->Driver.WriteColorPixels     = write_pixels_TRUECOLOR_pixmap;
  3049.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3050.         break;
  3051.      case PF_TRUEDITHER:
  3052.         ctx->Driver.WriteColorSpan       = write_span_TRUEDITHER_pixmap;
  3053.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_TRUEDITHER_pixmap;
  3054.         ctx->Driver.WriteColorPixels     = write_pixels_TRUEDITHER_pixmap;
  3055.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_TRUEDITHER_pixmap;
  3056.         break;
  3057.      case PF_8A8B8G8R:
  3058.         ctx->Driver.WriteColorSpan       = write_span_8A8B8G8R_pixmap;
  3059.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3060.         ctx->Driver.WriteColorPixels     = write_pixels_8A8B8G8R_pixmap;
  3061.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3062.         break;
  3063.      case PF_8R8G8B:
  3064.         ctx->Driver.WriteColorSpan       = write_span_8R8G8B_pixmap;
  3065.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3066.         ctx->Driver.WriteColorPixels     = write_pixels_8R8G8B_pixmap;
  3067.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3068.         break;
  3069.      case PF_5R6G5B:
  3070.         ctx->Driver.WriteColorSpan       = write_span_5R6G5B_pixmap;
  3071.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3072.         ctx->Driver.WriteColorPixels     = write_pixels_5R6G5B_pixmap;
  3073.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3074.         break;
  3075.      case PF_DITHER:
  3076.         ctx->Driver.WriteColorSpan       = write_span_DITHER_pixmap;
  3077.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER_pixmap;
  3078.         ctx->Driver.WriteColorPixels     = write_pixels_DITHER_pixmap;
  3079.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER_pixmap;
  3080.         break;
  3081.      case PF_1BIT:
  3082.         ctx->Driver.WriteColorSpan       = write_span_1BIT_pixmap;
  3083.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_1BIT_pixmap;
  3084.         ctx->Driver.WriteColorPixels     = write_pixels_1BIT_pixmap;
  3085.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_1BIT_pixmap;
  3086.         break;
  3087.          case PF_HPCR:
  3088.             ctx->Driver.WriteColorSpan       = write_span_HPCR_pixmap;
  3089.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3090.             ctx->Driver.WriteColorPixels     = write_pixels_HPCR_pixmap;
  3091.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3092.             break;
  3093.          case PF_LOOKUP:
  3094.             ctx->Driver.WriteColorSpan       = write_span_LOOKUP_pixmap;
  3095.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3096.             ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP_pixmap;
  3097.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3098.             break;
  3099.          case PF_GRAYSCALE:
  3100.             ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE_pixmap;
  3101.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_pixmap;
  3102.             ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE_pixmap;
  3103.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_pixmap;
  3104.             break;
  3105.      default:
  3106.         gl_problem(NULL,"Bad pixel format in xmesa_setup_DD_pointers (1)");
  3107.             return;
  3108.       }
  3109.    }
  3110.    else if (xmesa->xm_buffer->buffer==XIMAGE) {
  3111.       /* Writing to back XImage */
  3112.       if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
  3113.          /* Do this on Crays */
  3114.          ctx->Driver.Clear = clear_nbit_ximage;
  3115.       }
  3116.       else {
  3117.          /* Do this on most machines */
  3118.          switch (xmesa->xm_buffer->backimage->bits_per_pixel) {
  3119.             case 8:
  3120.                ctx->Driver.Clear = clear_8bit_ximage;
  3121.                break;
  3122.             case 16:
  3123.                ctx->Driver.Clear = clear_16bit_ximage;
  3124.                break;
  3125.             case 32:
  3126.                ctx->Driver.Clear = clear_32bit_ximage;
  3127.                break;
  3128.             default:
  3129.                ctx->Driver.Clear = clear_nbit_ximage;
  3130.                break;
  3131.          }
  3132.       }
  3133.       switch (xmesa->pixelformat) {
  3134.      case PF_INDEX:
  3135.         ctx->Driver.WriteIndexSpan       = write_span_index_ximage;
  3136.         ctx->Driver.WriteMonoindexSpan   = write_span_mono_ximage;
  3137.         ctx->Driver.WriteIndexPixels     = write_pixels_index_ximage;
  3138.         ctx->Driver.WriteMonoindexPixels = write_pixels_mono_ximage;
  3139.         break;
  3140.      case PF_TRUECOLOR:
  3141.         /* Generic RGB */
  3142.         ctx->Driver.WriteColorSpan       = write_span_TRUECOLOR_ximage;
  3143.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  3144.         ctx->Driver.WriteColorPixels     = write_pixels_TRUECOLOR_ximage;
  3145.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  3146.         break;
  3147.      case PF_TRUEDITHER:
  3148.         ctx->Driver.WriteColorSpan       = write_span_TRUEDITHER_ximage;
  3149.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_TRUEDITHER_ximage;
  3150.         ctx->Driver.WriteColorPixels     = write_pixels_TRUEDITHER_ximage;
  3151.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_TRUEDITHER_ximage;
  3152.         break;
  3153.      case PF_8A8B8G8R:
  3154.         ctx->Driver.WriteColorSpan       = write_span_8A8B8G8R_ximage;
  3155.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_8A8B8G8R_ximage;
  3156.         ctx->Driver.WriteColorPixels     = write_pixels_8A8B8G8R_ximage;
  3157.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_8A8B8G8R_ximage;
  3158.         break;
  3159.      case PF_8R8G8B:
  3160.         ctx->Driver.WriteColorSpan       = write_span_8R8G8B_ximage;
  3161.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_8R8G8B_ximage;
  3162.         ctx->Driver.WriteColorPixels     = write_pixels_8R8G8B_ximage;
  3163.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_8R8G8B_ximage;
  3164.         break;
  3165.      case PF_5R6G5B:
  3166.         ctx->Driver.WriteColorSpan       = write_span_5R6G5B_ximage;
  3167.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  3168.         ctx->Driver.WriteColorPixels     = write_pixels_5R6G5B_ximage;
  3169.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  3170.         break;
  3171.      case PF_DITHER:
  3172.         if (xmesa->xm_visual->visinfo->depth==8) {
  3173.            ctx->Driver.WriteColorSpan       = write_span_DITHER8_ximage;
  3174.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER8_ximage;
  3175.            ctx->Driver.WriteColorPixels     = write_pixels_DITHER8_ximage;
  3176.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER8_ximage;
  3177.         }
  3178.         else {
  3179.            ctx->Driver.WriteColorSpan       = write_span_DITHER_ximage;
  3180.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_DITHER_ximage;
  3181.            ctx->Driver.WriteColorPixels     = write_pixels_DITHER_ximage;
  3182.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_DITHER_ximage;
  3183.         }
  3184.         break;
  3185.      case PF_1BIT:
  3186.         ctx->Driver.WriteColorSpan       = write_span_1BIT_ximage;
  3187.         ctx->Driver.WriteMonocolorSpan   = write_span_mono_1BIT_ximage;
  3188.         ctx->Driver.WriteColorPixels     = write_pixels_1BIT_ximage;
  3189.         ctx->Driver.WriteMonocolorPixels = write_pixels_mono_1BIT_ximage;
  3190.         break;
  3191.          case PF_HPCR:
  3192.             ctx->Driver.WriteColorSpan       = write_span_HPCR_ximage;
  3193.             ctx->Driver.WriteMonocolorSpan   = write_span_mono_HPCR_ximage;
  3194.             ctx->Driver.WriteColorPixels     = write_pixels_HPCR_ximage;
  3195.             ctx->Driver.WriteMonocolorPixels = write_pixels_mono_HPCR_ximage;
  3196.             break;
  3197.          case PF_LOOKUP:
  3198.         if (xmesa->xm_visual->visinfo->depth==8) {
  3199.                ctx->Driver.WriteColorSpan       = write_span_LOOKUP8_ximage;
  3200.                ctx->Driver.WriteMonocolorSpan   = write_span_mono_LOOKUP8_ximage;
  3201.                ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP8_ximage;
  3202.                ctx->Driver.WriteMonocolorPixels = write_pixels_mono_LOOKUP8_ximage;
  3203.             }
  3204.             else {
  3205.                ctx->Driver.WriteColorSpan       = write_span_LOOKUP_ximage;
  3206.                ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  3207.                ctx->Driver.WriteColorPixels     = write_pixels_LOOKUP_ximage;
  3208.                ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  3209.             }
  3210.             break;
  3211.          case PF_GRAYSCALE:
  3212.         if (xmesa->xm_visual->visinfo->depth==8) {
  3213.            ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE8_ximage;
  3214.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_GRAYSCALE8_ximage;
  3215.            ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE8_ximage;
  3216.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_GRAYSCALE8_ximage;
  3217.         }
  3218.         else {
  3219.            ctx->Driver.WriteColorSpan       = write_span_GRAYSCALE_ximage;
  3220.            ctx->Driver.WriteMonocolorSpan   = write_span_mono_ximage;
  3221.            ctx->Driver.WriteColorPixels     = write_pixels_GRAYSCALE_ximage;
  3222.            ctx->Driver.WriteMonocolorPixels = write_pixels_mono_ximage;
  3223.         }
  3224.         break;
  3225.      default:
  3226.         gl_problem(NULL,"Bad pixel format in xmesa_setup_DD_pointers (2)");
  3227.             return;
  3228.       }
  3229.    }
  3230.  
  3231.    /* Pixel/span reading functions: */
  3232.    ctx->Driver.ReadIndexSpan = read_index_span;
  3233.    ctx->Driver.ReadColorSpan = read_color_span;
  3234.    ctx->Driver.ReadIndexPixels = read_index_pixels;
  3235.    ctx->Driver.ReadColorPixels = read_color_pixels;
  3236. }
  3237.