[MESA]
[reactos.git] / reactos / dll / opengl / mesa / swrast / s_bitmap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 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 "main/glheader.h"
32 #include "main/bufferobj.h"
33 #include "main/image.h"
34 #include "main/macros.h"
35 #include "main/pbo.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( struct gl_context *ctx, GLint px, GLint py,
49 GLsizei width, GLsizei height,
50 const struct gl_pixelstore_attrib *unpack,
51 const GLubyte *bitmap )
52 {
53 GLint row, col;
54 GLuint count = 0;
55 SWspan span;
56
57 ASSERT(ctx->RenderMode == GL_RENDER);
58
59 bitmap = (const GLubyte *) _mesa_map_pbo_source(ctx, unpack, bitmap);
60 if (!bitmap)
61 return;
62
63 swrast_render_start(ctx);
64
65 if (SWRAST_CONTEXT(ctx)->NewState)
66 _swrast_validate_derived( ctx );
67
68 INIT_SPAN(span, GL_BITMAP);
69 span.end = width;
70 span.arrayMask = SPAN_XY;
71 _swrast_span_default_attribs(ctx, &span);
72
73 for (row = 0; row < height; row++) {
74 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
75 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
76
77 if (unpack->LsbFirst) {
78 /* Lsb first */
79 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
80 for (col = 0; col < width; col++) {
81 if (*src & mask) {
82 span.array->x[count] = px + col;
83 span.array->y[count] = py + row;
84 count++;
85 }
86 if (mask == 128U) {
87 src++;
88 mask = 1U;
89 }
90 else {
91 mask = mask << 1;
92 }
93 }
94
95 /* get ready for next row */
96 if (mask != 1)
97 src++;
98 }
99 else {
100 /* Msb first */
101 GLubyte mask = 128U >> (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 == 1U) {
109 src++;
110 mask = 128U;
111 }
112 else {
113 mask = mask >> 1;
114 }
115 }
116
117 /* get ready for next row */
118 if (mask != 128)
119 src++;
120 }
121
122 if (count + width >= MAX_WIDTH || row + 1 == height) {
123 /* flush the span */
124 span.end = count;
125 _swrast_write_rgba_span(ctx, &span);
126 span.end = 0;
127 count = 0;
128 }
129 }
130
131 swrast_render_finish(ctx);
132
133 _mesa_unmap_pbo_source(ctx, unpack);
134 }
135
136
137 #if 0
138 /*
139 * XXX this is another way to implement Bitmap. Use horizontal runs of
140 * fragments, initializing the mask array to indicate which fragments to
141 * draw or skip.
142 */
143 void
144 _swrast_Bitmap( struct gl_context *ctx, GLint px, GLint py,
145 GLsizei width, GLsizei height,
146 const struct gl_pixelstore_attrib *unpack,
147 const GLubyte *bitmap )
148 {
149 SWcontext *swrast = SWRAST_CONTEXT(ctx);
150 GLint row, col;
151 SWspan span;
152
153 ASSERT(ctx->RenderMode == GL_RENDER);
154 ASSERT(bitmap);
155
156 swrast_render_start(ctx);
157
158 if (SWRAST_CONTEXT(ctx)->NewState)
159 _swrast_validate_derived( ctx );
160
161 INIT_SPAN(span, GL_BITMAP);
162 span.end = width;
163 span.arrayMask = SPAN_MASK;
164 _swrast_span_default_attribs(ctx, &span);
165
166 /*span.arrayMask |= SPAN_MASK;*/ /* we'll init span.mask[] */
167 span.x = px;
168 span.y = py;
169 /*span.end = width;*/
170
171 for (row=0; row<height; row++, span.y++) {
172 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
173 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
174
175 if (unpack->LsbFirst) {
176 /* Lsb first */
177 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
178 for (col=0; col<width; col++) {
179 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
180 if (mask == 128U) {
181 src++;
182 mask = 1U;
183 }
184 else {
185 mask = mask << 1;
186 }
187 }
188
189 _swrast_write_rgba_span(ctx, &span);
190
191 /* get ready for next row */
192 if (mask != 1)
193 src++;
194 }
195 else {
196 /* Msb first */
197 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
198 for (col=0; col<width; col++) {
199 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
200 if (mask == 1U) {
201 src++;
202 mask = 128U;
203 }
204 else {
205 mask = mask >> 1;
206 }
207 }
208
209 _swrast_write_rgba_span(ctx, &span);
210
211 /* get ready for next row */
212 if (mask != 128)
213 src++;
214 }
215 }
216
217 swrast_render_finish(ctx);
218 }
219 #endif