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