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