home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / swrast / s_bitmap.cpp < prev    next >
C/C++ Source or Header  |  2002-11-28  |  8KB  |  260 lines

  1. /* $Id: s_bitmap.c,v 1.20 2002/11/25 20:26:59 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  5.0
  6.  *
  7.  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27. /**
  28.  * \file swrast/s_bitmap.c
  29.  * \brief glBitmap rendering.
  30.  * \author Brian Paul
  31.  */
  32.  
  33. #include "glheader.h"
  34. #include "image.h"
  35. #include "macros.h"
  36. #include "mmath.h"
  37. #include "pixel.h"
  38.  
  39. #include "s_context.h"
  40. #include "s_span.h"
  41.  
  42.  
  43.  
  44. /*
  45.  * Render a bitmap.
  46.  */
  47. void
  48. _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
  49.         GLsizei width, GLsizei height,
  50.         const struct gl_pixelstore_attrib *unpack,
  51.         const GLubyte *bitmap )
  52. {
  53.    SWcontext *swrast = SWRAST_CONTEXT(ctx);
  54.    GLint row, col;
  55.    GLuint count = 0;
  56.    struct sw_span span;
  57.  
  58.    ASSERT(ctx->RenderMode == GL_RENDER);
  59.    ASSERT(bitmap);
  60.  
  61.    RENDER_START(swrast,ctx);
  62.  
  63.    if (SWRAST_CONTEXT(ctx)->NewState)
  64.       _swrast_validate_derived( ctx );
  65.  
  66.    INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_XY);
  67.  
  68.    if (ctx->Visual.rgbMode) {
  69.       span.interpMask |= SPAN_RGBA;
  70.       span.red   = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
  71.       span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
  72.       span.blue  = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
  73.       span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
  74.       span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
  75.    }
  76.    else {
  77.       span.interpMask |= SPAN_INDEX;
  78.       span.index = ChanToFixed(ctx->Current.RasterIndex);
  79.       span.indexStep = 0;
  80.    }
  81.  
  82.    if (ctx->Depth.Test)
  83.       _mesa_span_default_z(ctx, &span);
  84.    if (ctx->Fog.Enabled)
  85.       _mesa_span_default_fog(ctx, &span);
  86.    if (ctx->Texture._EnabledUnits)
  87.       _mesa_span_default_texcoords(ctx, &span);
  88.  
  89.    for (row = 0; row < height; row++, span.y++) {
  90.       const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
  91.                  bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
  92.  
  93.       if (unpack->LsbFirst) {
  94.          /* Lsb first */
  95.          GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
  96.          for (col = 0; col < width; col++) {
  97.             if (*src & mask) {
  98.                span.array->x[count] = px + col;
  99.                span.array->y[count] = py + row;
  100.                count++;
  101.             }
  102.             if (mask == 128U) {
  103.                src++;
  104.                mask = 1U;
  105.             }
  106.             else {
  107.                mask = mask << 1;
  108.             }
  109.          }
  110.  
  111.          /* get ready for next row */
  112.          if (mask != 1)
  113.             src++;
  114.       }
  115.       else {
  116.          /* Msb first */
  117.          GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
  118.          for (col = 0; col < width; col++) {
  119.             if (*src & mask) {
  120.                span.array->x[count] = px + col;
  121.                span.array->y[count] = py + row;
  122.                count++;
  123.             }
  124.             if (mask == 1U) {
  125.                src++;
  126.                mask = 128U;
  127.             }
  128.             else {
  129.                mask = mask >> 1;
  130.             }
  131.          }
  132.  
  133.          /* get ready for next row */
  134.          if (mask != 128)
  135.             src++;
  136.       }
  137.  
  138.       if (count + width >= MAX_WIDTH || row + 1 == height) {
  139.          /* flush the span */
  140.          span.end = count;
  141.          if (ctx->Visual.rgbMode)
  142.             _mesa_write_rgba_span(ctx, &span);
  143.          else
  144.             _mesa_write_index_span(ctx, &span);
  145.          span.end = 0;
  146.          count = 0;
  147.       }
  148.    }
  149.  
  150.    RENDER_FINISH(swrast,ctx);
  151. }
  152.  
  153.  
  154. #if 0
  155. /*
  156.  * XXX this is another way to implement Bitmap.  Use horizontal runs of
  157.  * fragments, initializing the mask array to indicate which fragmens to
  158.  * draw or skip.
  159.  */
  160. void
  161. _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
  162.         GLsizei width, GLsizei height,
  163.         const struct gl_pixelstore_attrib *unpack,
  164.         const GLubyte *bitmap )
  165. {
  166.    SWcontext *swrast = SWRAST_CONTEXT(ctx);
  167.    GLint row, col;
  168.    struct sw_span span;
  169.  
  170.    ASSERT(ctx->RenderMode == GL_RENDER);
  171.    ASSERT(bitmap);
  172.  
  173.    RENDER_START(swrast,ctx);
  174.  
  175.    if (SWRAST_CONTEXT(ctx)->NewState)
  176.       _swrast_validate_derived( ctx );
  177.  
  178.    INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_MASK);
  179.  
  180.    /*span.arrayMask |= SPAN_MASK;*/  /* we'll init span.mask[] */
  181.    span.x = px;
  182.    span.y = py;
  183.    /*span.end = width;*/
  184.    if (ctx->Visual.rgbMode) {
  185.       span.interpMask |= SPAN_RGBA;
  186.       span.red   = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
  187.       span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
  188.       span.blue  = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
  189.       span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
  190.       span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
  191.    }
  192.    else {
  193.       span.interpMask |= SPAN_INDEX;
  194.       span.index = ChanToFixed(ctx->Current.RasterIndex);
  195.       span.indexStep = 0;
  196.    }
  197.  
  198.    if (ctx->Depth.Test)
  199.       _mesa_span_default_z(ctx, &span);
  200.    if (ctx->Fog.Enabled)
  201.       _mesa_span_default_fog(ctx, &span);
  202.    if (ctx->Texture._EnabledUnits)
  203.       _mesa_span_default_texcoords(ctx, &span);
  204.  
  205.    for (row=0; row<height; row++, span.y++) {
  206.       const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
  207.                  bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
  208.  
  209.       if (unpack->LsbFirst) {
  210.          /* Lsb first */
  211.          GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
  212.          for (col=0; col<width; col++) {
  213.             span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
  214.             if (mask == 128U) {
  215.                src++;
  216.                mask = 1U;
  217.             }
  218.             else {
  219.                mask = mask << 1;
  220.             }
  221.          }
  222.  
  223.          if (ctx->Visual.rgbMode)
  224.             _mesa_write_rgba_span(ctx, &span);
  225.          else
  226.         _mesa_write_index_span(ctx, &span);
  227.  
  228.          /* get ready for next row */
  229.          if (mask != 1)
  230.             src++;
  231.       }
  232.       else {
  233.          /* Msb first */
  234.          GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
  235.          for (col=0; col<width; col++) {
  236.             span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
  237.             if (mask == 1U) {
  238.                src++;
  239.                mask = 128U;
  240.             }
  241.             else {
  242.                mask = mask >> 1;
  243.             }
  244.          }
  245.  
  246.          if (ctx->Visual.rgbMode)
  247.             _mesa_write_rgba_span(ctx, &span);
  248.          else
  249.             _mesa_write_index_span(ctx, &span);
  250.  
  251.          /* get ready for next row */
  252.          if (mask != 128)
  253.             src++;
  254.       }
  255.    }
  256.  
  257.    RENDER_FINISH(swrast,ctx);
  258. }
  259. #endif
  260.