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