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