Committing in .
[mesa.git] / src / mesa / swrast / s_bitmap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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
57 bitmap = _swrast_validate_pbo_access(unpack, width, height, 1,
58 GL_COLOR_INDEX, GL_BITMAP,
59 (GLvoid *) bitmap);
60 if (!bitmap) {
61 /* XXX GL_INVALID_OPERATION? */
62 return;
63 }
64
65 RENDER_START(swrast,ctx);
66
67 if (SWRAST_CONTEXT(ctx)->NewState)
68 _swrast_validate_derived( ctx );
69
70 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_XY);
71
72 if (ctx->Visual.rgbMode) {
73 span.interpMask |= SPAN_RGBA;
74 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
75 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
76 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
77 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
78 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
79 }
80 else {
81 span.interpMask |= SPAN_INDEX;
82 span.index = FloatToFixed(ctx->Current.RasterIndex);
83 span.indexStep = 0;
84 }
85
86 if (ctx->Depth.Test)
87 _swrast_span_default_z(ctx, &span);
88 if (ctx->Fog.Enabled)
89 _swrast_span_default_fog(ctx, &span);
90 if (ctx->Texture._EnabledCoordUnits)
91 _swrast_span_default_texcoords(ctx, &span);
92
93 for (row = 0; row < height; row++, span.y++) {
94 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
95 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
96
97 if (unpack->LsbFirst) {
98 /* Lsb first */
99 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
100 for (col = 0; col < width; col++) {
101 if (*src & mask) {
102 span.array->x[count] = px + col;
103 span.array->y[count] = py + row;
104 count++;
105 }
106 if (mask == 128U) {
107 src++;
108 mask = 1U;
109 }
110 else {
111 mask = mask << 1;
112 }
113 }
114
115 /* get ready for next row */
116 if (mask != 1)
117 src++;
118 }
119 else {
120 /* Msb first */
121 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
122 for (col = 0; col < width; col++) {
123 if (*src & mask) {
124 span.array->x[count] = px + col;
125 span.array->y[count] = py + row;
126 count++;
127 }
128 if (mask == 1U) {
129 src++;
130 mask = 128U;
131 }
132 else {
133 mask = mask >> 1;
134 }
135 }
136
137 /* get ready for next row */
138 if (mask != 128)
139 src++;
140 }
141
142 if (count + width >= MAX_WIDTH || row + 1 == height) {
143 /* flush the span */
144 span.end = count;
145 if (ctx->Visual.rgbMode)
146 _swrast_write_rgba_span(ctx, &span);
147 else
148 _swrast_write_index_span(ctx, &span);
149 span.end = 0;
150 count = 0;
151 }
152 }
153
154 RENDER_FINISH(swrast,ctx);
155 }
156
157
158 #if 0
159 /*
160 * XXX this is another way to implement Bitmap. Use horizontal runs of
161 * fragments, initializing the mask array to indicate which fragmens to
162 * draw or skip.
163 */
164 void
165 _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
166 GLsizei width, GLsizei height,
167 const struct gl_pixelstore_attrib *unpack,
168 const GLubyte *bitmap )
169 {
170 SWcontext *swrast = SWRAST_CONTEXT(ctx);
171 GLint row, col;
172 struct sw_span span;
173
174 ASSERT(ctx->RenderMode == GL_RENDER);
175 ASSERT(bitmap);
176
177 RENDER_START(swrast,ctx);
178
179 if (SWRAST_CONTEXT(ctx)->NewState)
180 _swrast_validate_derived( ctx );
181
182 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_MASK);
183
184 /*span.arrayMask |= SPAN_MASK;*/ /* we'll init span.mask[] */
185 span.x = px;
186 span.y = py;
187 /*span.end = width;*/
188 if (ctx->Visual.rgbMode) {
189 span.interpMask |= SPAN_RGBA;
190 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
191 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
192 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
193 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
194 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
195 }
196 else {
197 span.interpMask |= SPAN_INDEX;
198 span.index = FloatToFixed(ctx->Current.RasterIndex);
199 span.indexStep = 0;
200 }
201
202 if (ctx->Depth.Test)
203 _swrast_span_default_z(ctx, &span);
204 if (ctx->Fog.Enabled)
205 _swrast_span_default_fog(ctx, &span);
206 if (ctx->Texture._EnabledCoordUnits)
207 _swrast_span_default_texcoords(ctx, &span);
208
209 for (row=0; row<height; row++, span.y++) {
210 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
211 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
212
213 if (unpack->LsbFirst) {
214 /* Lsb first */
215 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
216 for (col=0; col<width; col++) {
217 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
218 if (mask == 128U) {
219 src++;
220 mask = 1U;
221 }
222 else {
223 mask = mask << 1;
224 }
225 }
226
227 if (ctx->Visual.rgbMode)
228 _swrast_write_rgba_span(ctx, &span);
229 else
230 _swrast_write_index_span(ctx, &span);
231
232 /* get ready for next row */
233 if (mask != 1)
234 src++;
235 }
236 else {
237 /* Msb first */
238 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
239 for (col=0; col<width; col++) {
240 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
241 if (mask == 1U) {
242 src++;
243 mask = 128U;
244 }
245 else {
246 mask = mask >> 1;
247 }
248 }
249
250 if (ctx->Visual.rgbMode)
251 _swrast_write_rgba_span(ctx, &span);
252 else
253 _swrast_write_index_span(ctx, &span);
254
255 /* get ready for next row */
256 if (mask != 128)
257 src++;
258 }
259 }
260
261 RENDER_FINISH(swrast,ctx);
262 }
263 #endif