518e0587df0cfcb10eadb4d8c45291bee908e12e
[mesa.git] / src / mesa / swrast / s_bitmap.c
1 /* $Id: s_bitmap.c,v 1.17 2002/04/12 15:39:59 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
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 /*span.arrayMask |= SPAN_XY;
68 span.end = width;*/
69 if (ctx->Visual.rgbMode) {
70 span.interpMask |= SPAN_RGBA;
71 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
72 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
73 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
74 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
75 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
76 }
77 else {
78 span.interpMask |= SPAN_INDEX;
79 span.index = ChanToFixed(ctx->Current.RasterIndex);
80 span.indexStep = 0;
81 }
82
83 if (ctx->Depth.Test)
84 _mesa_span_default_z(ctx, &span);
85 if (ctx->Fog.Enabled)
86 _mesa_span_default_fog(ctx, &span);
87
88 for (row = 0; row < height; row++, span.y++) {
89 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
90 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
91
92 if (unpack->LsbFirst) {
93 /* Lsb first */
94 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
95 for (col = 0; col < width; col++) {
96 if (*src & mask) {
97 span.xArray[count] = px + col;
98 span.yArray[count] = py + row;
99 count++;
100 }
101 if (mask == 128U) {
102 src++;
103 mask = 1U;
104 }
105 else {
106 mask = mask << 1;
107 }
108 }
109
110 /* get ready for next row */
111 if (mask != 1)
112 src++;
113 }
114 else {
115 /* Msb first */
116 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
117 for (col = 0; col < width; col++) {
118 if (*src & mask) {
119 span.xArray[count] = px + col;
120 span.yArray[count] = py + row;
121 count++;
122 }
123 if (mask == 1U) {
124 src++;
125 mask = 128U;
126 }
127 else {
128 mask = mask >> 1;
129 }
130 }
131
132 /* get ready for next row */
133 if (mask != 128)
134 src++;
135 }
136
137 if (count + width >= MAX_WIDTH || row + 1 == height) {
138 /* flush the span */
139 span.end = count;
140 if (ctx->Visual.rgbMode)
141 _mesa_write_rgba_span(ctx, &span);
142 else
143 _mesa_write_index_span(ctx, &span);
144 span.end = 0;
145 count = 0;
146 }
147 }
148
149 RENDER_FINISH(swrast,ctx);
150 }
151
152
153 #if 0
154 /*
155 * XXX this is another way to implement Bitmap. Use horizontal runs of
156 * fragments, initializing the mask array to indicate which fragmens to
157 * draw or skip.
158 */
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 /*span.arrayMask |= SPAN_MASK;*/ /* we'll init span.mask[] */
180 span.x = px;
181 span.y = py;
182 /*span.end = width;*/
183 if (ctx->Visual.rgbMode) {
184 span.interpMask |= SPAN_RGBA;
185 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
186 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
187 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
188 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
189 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
190 }
191 else {
192 span.interpMask |= SPAN_INDEX;
193 span.index = ChanToFixed(ctx->Current.RasterIndex);
194 span.indexStep = 0;
195 }
196
197 if (ctx->Depth.Test)
198 _mesa_span_default_z(ctx, &span);
199 if (ctx->Fog.Enabled)
200 _mesa_span_default_fog(ctx, &span);
201
202 for (row=0; row<height; row++, span.y++) {
203 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
204 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
205
206 if (unpack->LsbFirst) {
207 /* Lsb first */
208 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
209 for (col=0; col<width; col++) {
210 span.mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
211 if (mask == 128U) {
212 src++;
213 mask = 1U;
214 }
215 else {
216 mask = mask << 1;
217 }
218 }
219
220 if (ctx->Visual.rgbMode)
221 _mesa_write_rgba_span(ctx, &span);
222 else
223 _mesa_write_index_span(ctx, &span);
224
225 /* get ready for next row */
226 if (mask != 1)
227 src++;
228 }
229 else {
230 /* Msb first */
231 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
232 for (col=0; col<width; col++) {
233 span.mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
234 if (mask == 1U) {
235 src++;
236 mask = 128U;
237 }
238 else {
239 mask = mask >> 1;
240 }
241 }
242
243 if (ctx->Visual.rgbMode)
244 _mesa_write_rgba_span(ctx, &span);
245 else
246 _mesa_write_index_span(ctx, &span);
247
248 /* get ready for next row */
249 if (mask != 128)
250 src++;
251 }
252 }
253
254 RENDER_FINISH(swrast,ctx);
255 }
256 #endif