xref: /reactos/dll/opengl/mesa/bitmap.c (revision 5f2bebf7)
1 /* $Id: bitmap.c,v 1.9 1998/02/03 23:45:02 brianp Exp $ */
2 
3 /*
4  * Mesa 3-D graphics library
5  * Version:  2.5
6  * Copyright (C) 1995-1997  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: bitmap.c,v $
26  * Revision 1.9  1998/02/03 23:45:02  brianp
27  * added casts to prevent warnings with Amiga StormC compiler
28  *
29  * Revision 1.8  1997/10/02 03:06:42  brianp
30  * added #include <assert.h>
31  *
32  * Revision 1.7  1997/09/27 00:15:39  brianp
33  * changed parameters to gl_unpack_image()
34  *
35  * Revision 1.6  1997/07/24 01:24:45  brianp
36  * changed precompiled header symbol from PCH to PC_HEADER
37  *
38  * Revision 1.5  1997/06/20 02:18:09  brianp
39  * replaced Current.IntColor with Current.ByteColor
40  *
41  * Revision 1.4  1997/05/28 03:23:48  brianp
42  * added precompiled header (PCH) support
43  *
44  * Revision 1.3  1996/11/06 04:23:18  brianp
45  * replaced 0 with GL_COLOR_INDEX in gl_unpack_bitmap()
46  *
47  * Revision 1.2  1996/09/15 14:18:10  brianp
48  * now use GLframebuffer and GLvisual
49  *
50  * Revision 1.1  1996/09/13 01:38:16  brianp
51  * Initial revision
52  *
53  */
54 
55 
56 #ifdef PC_HEADER
57 #include "all.h"
58 #else
59 #include <assert.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include "bitmap.h"
63 #include "context.h"
64 #include "feedback.h"
65 #include "image.h"
66 #include "macros.h"
67 #include "pb.h"
68 #include "types.h"
69 #endif
70 
71 
72 
73 /*
74  * Unpack a bitmap image
75  */
gl_unpack_bitmap(GLcontext * ctx,GLsizei width,GLsizei height,const GLubyte * bitmap)76 struct gl_image *gl_unpack_bitmap( GLcontext* ctx,
77                                    GLsizei width, GLsizei height,
78                                    const GLubyte *bitmap )
79 {
80    return gl_unpack_image( ctx, width, height,
81                            GL_COLOR_INDEX, GL_BITMAP, bitmap );
82 }
83 
84 
85 
86 
87 /*
88  * Do actual rendering of a bitmap.
89  */
gl_render_bitmap(GLcontext * ctx,GLsizei width,GLsizei height,GLfloat xorig,GLfloat yorig,GLfloat xmove,GLfloat ymove,const struct gl_image * bitmap)90 void gl_render_bitmap( GLcontext* ctx,
91                        GLsizei width, GLsizei height,
92                        GLfloat xorig, GLfloat yorig,
93                        GLfloat xmove, GLfloat ymove,
94                        const struct gl_image *bitmap )
95 {
96    struct pixel_buffer *PB = ctx->PB;
97    GLint bx, by;      /* bitmap position */
98    GLint px, py, pz;  /* pixel position */
99    GLubyte *ptr;
100 
101    assert(bitmap);
102    assert(bitmap->Type == GL_BITMAP);
103    assert(bitmap->Format == GL_COLOR_INDEX);
104 
105    if (ctx->NewState) {
106       gl_update_state(ctx);
107       PB_INIT( PB, GL_BITMAP );
108    }
109 
110    if (ctx->Visual->RGBAflag) {
111       GLint r, g, b, a;
112       r = (GLint) (ctx->Current.RasterColor[0] * ctx->Visual->RedScale);
113       g = (GLint) (ctx->Current.RasterColor[1] * ctx->Visual->GreenScale);
114       b = (GLint) (ctx->Current.RasterColor[2] * ctx->Visual->BlueScale);
115       a = (GLint) (ctx->Current.RasterColor[3] * ctx->Visual->AlphaScale);
116       PB_SET_COLOR( ctx, PB, r, g, b, a );
117    }
118    else {
119       PB_SET_INDEX( ctx, PB, ctx->Current.RasterIndex );
120    }
121 
122    px = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F );
123    py = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F );
124    pz = (GLint) ( ctx->Current.RasterPos[2] * DEPTH_SCALE );
125    ptr = (GLubyte *) bitmap->Data;
126 
127    for (by=0;by<height;by++) {
128       GLubyte bitmask;
129 
130       /* do a row */
131       bitmask = 128;
132       for (bx=0;bx<width;bx++) {
133          if (*ptr&bitmask) {
134             PB_WRITE_PIXEL( PB, px+bx, py+by, pz );
135          }
136          bitmask = bitmask >> 1;
137          if (bitmask==0) {
138             ptr++;
139             bitmask = 128;
140          }
141       }
142 
143       PB_CHECK_FLUSH( ctx, PB )
144 
145       /* get ready for next row */
146       if (bitmask!=128)  ptr++;
147    }
148 
149    gl_flush_pb(ctx);
150 }
151 
152 
153 
154 
155 /*
156  * Execute a glBitmap command:
157  *   1. check for errors
158  *   2. feedback/render/select
159  *   3. advance raster position
160  */
gl_Bitmap(GLcontext * ctx,GLsizei width,GLsizei height,GLfloat xorig,GLfloat yorig,GLfloat xmove,GLfloat ymove,const struct gl_image * bitmap)161 void gl_Bitmap( GLcontext* ctx,
162                 GLsizei width, GLsizei height,
163 	        GLfloat xorig, GLfloat yorig,
164 	        GLfloat xmove, GLfloat ymove,
165                 const struct gl_image *bitmap )
166 {
167    if (width<0 || height<0) {
168       gl_error( ctx, GL_INVALID_VALUE, "glBitmap" );
169       return;
170    }
171    if (INSIDE_BEGIN_END(ctx)) {
172       gl_error( ctx, GL_INVALID_OPERATION, "glBitmap" );
173       return;
174    }
175    if (ctx->Current.RasterPosValid==GL_FALSE) {
176       /* do nothing */
177       return;
178    }
179 
180    if (ctx->RenderMode==GL_RENDER) {
181       GLboolean completed = GL_FALSE;
182       if (ctx->Driver.Bitmap) {
183          /* let device driver try to render the bitmap */
184          completed = (*ctx->Driver.Bitmap)( ctx, width, height, xorig, yorig,
185                                             xmove, ymove, bitmap );
186       }
187       if (!completed) {
188          /* use generic function */
189          gl_render_bitmap( ctx, width, height, xorig, yorig,
190                            xmove, ymove, bitmap );
191       }
192    }
193    else if (ctx->RenderMode==GL_FEEDBACK) {
194       GLfloat color[4], texcoord[4], invq;
195       color[0] = ctx->Current.ByteColor[0] * ctx->Visual->InvRedScale;
196       color[1] = ctx->Current.ByteColor[1] * ctx->Visual->InvGreenScale;
197       color[2] = ctx->Current.ByteColor[2] * ctx->Visual->InvBlueScale;
198       color[3] = ctx->Current.ByteColor[3] * ctx->Visual->InvAlphaScale;
199       invq = 1.0F / ctx->Current.TexCoord[3];
200       texcoord[0] = ctx->Current.TexCoord[0] * invq;
201       texcoord[1] = ctx->Current.TexCoord[1] * invq;
202       texcoord[2] = ctx->Current.TexCoord[2] * invq;
203       texcoord[3] = ctx->Current.TexCoord[3];
204       FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
205       /* TODO: Verify XYZW values are correct: */
206       gl_feedback_vertex( ctx, ctx->Current.RasterPos[0] - xorig,
207 			  ctx->Current.RasterPos[1] - yorig,
208 			  ctx->Current.RasterPos[2],
209 			  ctx->Current.RasterPos[3],
210 			  color, ctx->Current.Index, texcoord );
211    }
212    else if (ctx->RenderMode==GL_SELECT) {
213       /* Bitmaps don't generate selection hits.  See appendix B of 1.1 spec. */
214    }
215 
216    /* update raster position */
217    ctx->Current.RasterPos[0] += xmove;
218    ctx->Current.RasterPos[1] += ymove;
219 }
220 
221 
222