replace _mesa_ prefix with _swrast_, remove s_histogram.[ch]
[mesa.git] / src / mesa / swrast / s_bitmap.c
1 /* $Id: s_bitmap.c,v 1.22 2003/03/25 02:23:45 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.0
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /**
28 * \file swrast/s_bitmap.c
29 * \brief glBitmap rendering.
30 * \author Brian Paul
31 */
32
33 #include "glheader.h"
34 #include "image.h"
35 #include "macros.h"
36 #include "pixel.h"
37
38 #include "s_context.h"
39 #include "s_span.h"
40
41
42
43 /*
44 * Render a bitmap.
45 */
46 void
47 _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
48 GLsizei width, GLsizei height,
49 const struct gl_pixelstore_attrib *unpack,
50 const GLubyte *bitmap )
51 {
52 SWcontext *swrast = SWRAST_CONTEXT(ctx);
53 GLint row, col;
54 GLuint count = 0;
55 struct sw_span span;
56
57 ASSERT(ctx->RenderMode == GL_RENDER);
58 ASSERT(bitmap);
59
60 RENDER_START(swrast,ctx);
61
62 if (SWRAST_CONTEXT(ctx)->NewState)
63 _swrast_validate_derived( ctx );
64
65 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_XY);
66
67 if (ctx->Visual.rgbMode) {
68 span.interpMask |= SPAN_RGBA;
69 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
70 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
71 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
72 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
73 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
74 }
75 else {
76 span.interpMask |= SPAN_INDEX;
77 span.index = ChanToFixed(ctx->Current.RasterIndex);
78 span.indexStep = 0;
79 }
80
81 if (ctx->Depth.Test)
82 _swrast_span_default_z(ctx, &span);
83 if (ctx->Fog.Enabled)
84 _swrast_span_default_fog(ctx, &span);
85 if (ctx->Texture._EnabledUnits)
86 _swrast_span_default_texcoords(ctx, &span);
87
88 for (row = 0; row < height; row++, span.y++) {
89 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
90 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, 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
152
153 #if 0
154 /*
155 * XXX this is another way to implement Bitmap. Use horizontal runs of
156 * fragments, initializing the mask array to indicate which fragmens to
157 * draw or skip.
158 */
159 void
160 _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
161 GLsizei width, GLsizei height,
162 const struct gl_pixelstore_attrib *unpack,
163 const GLubyte *bitmap )
164 {
165 SWcontext *swrast = SWRAST_CONTEXT(ctx);
166 GLint row, col;
167 struct sw_span span;
168
169 ASSERT(ctx->RenderMode == GL_RENDER);
170 ASSERT(bitmap);
171
172 RENDER_START(swrast,ctx);
173
174 if (SWRAST_CONTEXT(ctx)->NewState)
175 _swrast_validate_derived( ctx );
176
177 INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_MASK);
178
179 /*span.arrayMask |= SPAN_MASK;*/ /* we'll init span.mask[] */
180 span.x = px;
181 span.y = py;
182 /*span.end = width;*/
183 if (ctx->Visual.rgbMode) {
184 span.interpMask |= SPAN_RGBA;
185 span.red = FloatToFixed(ctx->Current.RasterColor[0] * CHAN_MAXF);
186 span.green = FloatToFixed(ctx->Current.RasterColor[1] * CHAN_MAXF);
187 span.blue = FloatToFixed(ctx->Current.RasterColor[2] * CHAN_MAXF);
188 span.alpha = FloatToFixed(ctx->Current.RasterColor[3] * CHAN_MAXF);
189 span.redStep = span.greenStep = span.blueStep = span.alphaStep = 0;
190 }
191 else {
192 span.interpMask |= SPAN_INDEX;
193 span.index = ChanToFixed(ctx->Current.RasterIndex);
194 span.indexStep = 0;
195 }
196
197 if (ctx->Depth.Test)
198 _swrast_span_default_z(ctx, &span);
199 if (ctx->Fog.Enabled)
200 _swrast_span_default_fog(ctx, &span);
201 if (ctx->Texture._EnabledUnits)
202 _swrast_span_default_texcoords(ctx, &span);
203
204 for (row=0; row<height; row++, span.y++) {
205 const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack,
206 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
207
208 if (unpack->LsbFirst) {
209 /* Lsb first */
210 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
211 for (col=0; col<width; col++) {
212 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
213 if (mask == 128U) {
214 src++;
215 mask = 1U;
216 }
217 else {
218 mask = mask << 1;
219 }
220 }
221
222 if (ctx->Visual.rgbMode)
223 _swrast_write_rgba_span(ctx, &span);
224 else
225 _swrast_write_index_span(ctx, &span);
226
227 /* get ready for next row */
228 if (mask != 1)
229 src++;
230 }
231 else {
232 /* Msb first */
233 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
234 for (col=0; col<width; col++) {
235 span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
236 if (mask == 1U) {
237 src++;
238 mask = 128U;
239 }
240 else {
241 mask = mask >> 1;
242 }
243 }
244
245 if (ctx->Visual.rgbMode)
246 _swrast_write_rgba_span(ctx, &span);
247 else
248 _swrast_write_index_span(ctx, &span);
249
250 /* get ready for next row */
251 if (mask != 128)
252 src++;
253 }
254 }
255
256 RENDER_FINISH(swrast,ctx);
257 }
258 #endif