Merge branch 'master' into i915-unification
[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 _swrast_span_default_attribs(ctx, &span);
87
88 for (row = 0; row < height; row++) {
89 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
90 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
91
92 if (unpack->LsbFirst) {
93 /* Lsb first */
94 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
95 for (col = 0; col < width; col++) {
96 if (*src & mask) {
97 span.array->x[count] = px + col;
98 span.array->y[count] = py + row;
99 count++;
100 }
101 if (mask == 128U) {
102 src++;
103 mask = 1U;
104 }
105 else {
106 mask = mask << 1;
107 }
108 }
109
110 /* get ready for next row */
111 if (mask != 1)
112 src++;
113 }
114 else {
115 /* Msb first */
116 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
117 for (col = 0; col < width; col++) {
118 if (*src & mask) {
119 span.array->x[count] = px + col;
120 span.array->y[count] = py + row;
121 count++;
122 }
123 if (mask == 1U) {
124 src++;
125 mask = 128U;
126 }
127 else {
128 mask = mask >> 1;
129 }
130 }
131
132 /* get ready for next row */
133 if (mask != 128)
134 src++;
135 }
136
137 if (count + width >= MAX_WIDTH || row + 1 == height) {
138 /* flush the span */
139 span.end = count;
140 if (ctx->Visual.rgbMode)
141 _swrast_write_rgba_span(ctx, &span);
142 else
143 _swrast_write_index_span(ctx, &span);
144 span.end = 0;
145 count = 0;
146 }
147 }
148
149 RENDER_FINISH(swrast,ctx);
150
151 if (unpack->BufferObj->Name) {
152 /* done with PBO so unmap it now */
153 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
154 unpack->BufferObj);
155 }
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 fragments 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 SWspan 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 _swrast_span_default_attribs(ctx, &span);
185
186 /*span.arrayMask |= SPAN_MASK;*/ /* we'll init span.mask[] */
187 span.x = px;
188 span.y = py;
189 /*span.end = width;*/
190
191 for (row=0; row<height; row++, span.y++) {
192 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
193 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
194
195 if (unpack->LsbFirst) {
196 /* Lsb first */
197 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
198 for (col=0; col<width; col++) {
199 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
200 if (mask == 128U) {
201 src++;
202 mask = 1U;
203 }
204 else {
205 mask = mask << 1;
206 }
207 }
208
209 if (ctx->Visual.rgbMode)
210 _swrast_write_rgba_span(ctx, &span);
211 else
212 _swrast_write_index_span(ctx, &span);
213
214 /* get ready for next row */
215 if (mask != 1)
216 src++;
217 }
218 else {
219 /* Msb first */
220 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
221 for (col=0; col<width; col++) {
222 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
223 if (mask == 1U) {
224 src++;
225 mask = 128U;
226 }
227 else {
228 mask = mask >> 1;
229 }
230 }
231
232 if (ctx->Visual.rgbMode)
233 _swrast_write_rgba_span(ctx, &span);
234 else
235 _swrast_write_index_span(ctx, &span);
236
237 /* get ready for next row */
238 if (mask != 128)
239 src++;
240 }
241 }
242
243 RENDER_FINISH(swrast,ctx);
244 }
245 #endif