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