home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / src / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  6.7 KB  |  271 lines

  1. /* bitmap.c */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  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.  * glBitmap()
  26.  */
  27.  
  28.  
  29. /*
  30. $Id: bitmap.c,v 1.13 1995/09/07 19:46:56 brianp Exp $
  31.  
  32. $Log: bitmap.c,v $
  33.  * Revision 1.13  1995/09/07  19:46:56  brianp
  34.  * use CC.NewState convention
  35.  * truncate, don't round raster position to integer position
  36.  *
  37.  * Revision 1.12  1995/07/24  20:34:16  brianp
  38.  * replaced memset() with MEMSET() and memcpy() with MEMCPY()
  39.  *
  40.  * Revision 1.11  1995/07/11  15:29:33  brianp
  41.  * round rasterpos to nearest integer before drawing
  42.  *
  43.  * Revision 1.10  1995/06/20  16:19:10  brianp
  44.  * removed PB clipflag stuff
  45.  *
  46.  * Revision 1.9  1995/05/22  21:02:41  brianp
  47.  * Release 1.2
  48.  *
  49.  * Revision 1.8  1995/05/12  19:22:23  brianp
  50.  * added #include "macros.h"
  51.  *
  52.  * Revision 1.7  1995/05/12  19:20:19  brianp
  53.  * replaced CC.Mode!=0 with INSIDE_BEGIN_END
  54.  *
  55.  * Revision 1.6  1995/05/12  16:27:28  brianp
  56.  * better bitmap clipping scheme
  57.  *
  58.  * Revision 1.5  1995/03/13  15:58:42  brianp
  59.  * fixed bitmap bugs per Thorsten Ohl
  60.  *
  61.  * Revision 1.4  1995/03/07  14:19:41  brianp
  62.  * updated for new XSetForeground/GC scheme
  63.  *
  64.  * Revision 1.3  1995/03/04  19:29:44  brianp
  65.  * 1.1 beta revision
  66.  *
  67.  * Revision 1.2  1995/02/27  22:48:16  brianp
  68.  * modified for PB
  69.  *
  70.  * Revision 1.1  1995/02/24  14:16:45  brianp
  71.  * Initial revision
  72.  *
  73.  */
  74.  
  75.  
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include "context.h"
  79. #include "feedback.h"
  80. #include "list.h"
  81. #include "macros.h"
  82. #include "pb.h"
  83. #include "pixel.h"
  84.  
  85.  
  86.  
  87.  
  88.  
  89. void gl_bitmap( GLsizei width, GLsizei height,
  90.             GLfloat xorig, GLfloat yorig,
  91.             GLfloat xmove, GLfloat ymove,
  92.             const GLubyte *bitmap )
  93. {
  94.    /* Error checks */
  95.    if (width<0 || height<0) {
  96.       gl_error( GL_INVALID_VALUE, "glBitmap" );
  97.       return;
  98.    }
  99.    if (INSIDE_BEGIN_END) {
  100.       gl_error( GL_INVALID_OPERATION, "glBitmap" );
  101.       return;
  102.    }
  103.    if (CC.Current.RasterPosValid==GL_FALSE) {
  104.       /* do nothing */
  105.       return;
  106.    }
  107.  
  108.    if (CC.NewState) {
  109.       gl_update_state();
  110.    }
  111.  
  112.    if (CC.RenderMode==GL_RENDER) {
  113.       GLint bx, by;      /* bitmap position */
  114.       GLint px, py, pz;  /* pixel position */
  115.       GLubyte *ptr;
  116.  
  117.       if (CC.RGBAflag) {
  118.      PB_SET_COLOR( CC.Current.RasterColor );
  119.       }
  120.       else {
  121.      PB_SET_INDEX( CC.Current.RasterIndex );
  122.       }
  123.  
  124.       px = (GLint) ( (CC.Current.RasterPos[0] - xorig) + 0.0F );
  125.       py = (GLint) ( (CC.Current.RasterPos[1] - yorig) + 0.0F );
  126.       pz = (GLint) ( CC.Current.RasterPos[2] * MAX_DEPTH );
  127.       ptr = (GLubyte *) bitmap;
  128.  
  129.       for (by=0;by<height;by++) {
  130.      GLubyte bitmask;
  131.  
  132.      /* do a row */
  133.      bitmask = 128;
  134.      for (bx=0;bx<width;bx++) {
  135.         if (*ptr&bitmask) {
  136.            PB_WRITE_PIXEL( px+bx, py+by, pz );
  137.         }
  138.         bitmask = bitmask >> 1;
  139.         if (bitmask==0) {
  140.            ptr++;
  141.            bitmask = 128;
  142.         }
  143.      }
  144.  
  145.      PB_CHECK_FLUSH
  146.  
  147.      /* get ready for next row */
  148.      if (width%8)  ptr++;
  149.       }
  150.  
  151.       gl_flush_pb();
  152.  
  153.       /* update raster position */
  154.       CC.Current.RasterPos[0] += xmove;
  155.       CC.Current.RasterPos[1] += ymove;
  156.    }
  157.    else if (CC.RenderMode==GL_FEEDBACK) {
  158.       APPEND_TOKEN( (GLfloat) GL_BITMAP_TOKEN );
  159.       /* TODO: Verify XYZW values are correct: */
  160.       gl_feedback_vertex( CC.Current.RasterPos[0] - xorig,
  161.               CC.Current.RasterPos[1] - yorig,
  162.               CC.Current.RasterPos[2],
  163.               CC.Current.RasterPos[3],
  164.               CC.Current.Color, CC.Current.Index,
  165.               CC.Current.TexCoord );
  166.    }
  167.    else if (CC.RenderMode==GL_SELECT) {
  168.       /* TODO: verify that this is correct */
  169.       CC.HitFlag = GL_TRUE;
  170.       if (CC.Current.RasterPos[2] < CC.HitMinZ) {
  171.      CC.HitMinZ = CC.Current.RasterPos[2];
  172.       }
  173.       if (CC.Current.RasterPos[2] > CC.HitMaxZ) {
  174.      CC.HitMaxZ = CC.Current.RasterPos[2];
  175.       }
  176.    }
  177. }
  178.  
  179.  
  180.  
  181. void glBitmap( GLsizei width, GLsizei height,
  182.            GLfloat xorig, GLfloat yorig,
  183.            GLfloat xmove, GLfloat ymove,
  184.            const GLubyte *bitmap )
  185. {
  186.    GLubyte *data;  /* The unpacked bitmap data */
  187.  
  188.    /*
  189.     * Data conversion:  we want:  unpack MSB first, unpack row length = width,
  190.     * unpack skip pixels = 0, unpack skip rows = 0, unpack alignment = 1
  191.     */
  192.    if (CC.UnpackLSBFirst==GL_FALSE && CC.UnpackAlignment==1
  193.        && CC.UnpackRowLength==0 && CC.UnpackSkipPixels==0
  194.        && CC.UnpackSkipRows==0) {
  195.       /* bitmap is already in desired form */
  196.       data = (GLubyte *) bitmap;
  197.    }
  198.    else {
  199.       /* Copy the data to a new buffer while applying all the glPixelStore
  200.        * pixel unpacking options.
  201.        */
  202.       GLuint bytes, bytes_per_row, width_in_bytes, skip;
  203.       GLuint i, j;
  204.       GLubyte *src, *dst;
  205.  
  206.       width_in_bytes = (width+7)/8;
  207.       bytes = width_in_bytes * height;
  208.  
  209.       /* allocate storage for unpacked bitmap data */
  210.       data = (GLubyte *) malloc( bytes );
  211.       if (!data) {
  212.      gl_error( GL_OUT_OF_MEMORY, "glBitmap" );
  213.      return;
  214.       }
  215.  
  216.       if (CC.UnpackRowLength==0) {
  217.      bytes_per_row = width_in_bytes;
  218.       }
  219.       else {
  220.      bytes_per_row = (CC.UnpackRowLength+7)/8;
  221.       }
  222.  
  223.       if (bytes_per_row % CC.UnpackAlignment) {
  224.      skip = CC.UnpackAlignment - bytes_per_row % CC.UnpackAlignment;
  225.       }
  226.       else {
  227.      skip = 0;
  228.       }
  229.  
  230.       skip += bytes_per_row - width_in_bytes;
  231.  
  232.       src = (GLubyte *) bitmap + CC.UnpackSkipPixels/8
  233.                       + CC.UnpackSkipRows*bytes_per_row;
  234.       dst = data;
  235.  
  236.       /* copy bytes from src to dst */
  237.       for (i=0;i<height;i++) {
  238.      for (j=0;j<width_in_bytes;j++) {
  239.         *dst++ = *src++;
  240.      }
  241.      src += skip;
  242.       }
  243.  
  244.       if (CC.UnpackLSBFirst) {
  245.      /* Flip the bits in each byte */
  246.      gl_flip_bytes( data, bytes );
  247.       }
  248.    }
  249.  
  250.    if (CC.CompileFlag) {
  251.       if (data==bitmap) {
  252.      /* make a copy to save in the display list */
  253.      GLuint bytes = (width + 7) / 8 * height;
  254.      data = (GLubyte *) malloc( bytes );
  255.      if (data) {
  256.         MEMCPY( data, bitmap, bytes );
  257.      }
  258.       }
  259.       gl_save_bitmap( width, height, xorig, yorig, xmove, ymove, data );
  260.    }
  261.    if (CC.ExecuteFlag) {
  262.       gl_bitmap( width, height, xorig, yorig, xmove, ymove, data );
  263.       if (!CC.CompileFlag && data!=bitmap) {
  264.      /* Free the unpacked data */
  265.      free( data );
  266.       }
  267.    }
  268. }
  269.  
  270.  
  271.