s_bitmap.c revision 7117f1b4
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 */
47void
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   _swrast_span_default_secondary_color(ctx, &span);
89   if (ctx->Depth.Test)
90      _swrast_span_default_z(ctx, &span);
91   if (swrast->_FogEnabled)
92      _swrast_span_default_fog(ctx, &span);
93   if (ctx->Texture._EnabledCoordUnits)
94      _swrast_span_default_texcoords(ctx, &span);
95
96   for (row = 0; row < height; row++) {
97      const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
98                 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
99
100      if (unpack->LsbFirst) {
101         /* Lsb first */
102         GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
103         for (col = 0; col < width; col++) {
104            if (*src & mask) {
105               span.array->x[count] = px + col;
106               span.array->y[count] = py + row;
107               count++;
108            }
109            if (mask == 128U) {
110               src++;
111               mask = 1U;
112            }
113            else {
114               mask = mask << 1;
115            }
116         }
117
118         /* get ready for next row */
119         if (mask != 1)
120            src++;
121      }
122      else {
123         /* Msb first */
124         GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
125         for (col = 0; col < width; col++) {
126            if (*src & mask) {
127               span.array->x[count] = px + col;
128               span.array->y[count] = py + row;
129               count++;
130            }
131            if (mask == 1U) {
132               src++;
133               mask = 128U;
134            }
135            else {
136               mask = mask >> 1;
137            }
138         }
139
140         /* get ready for next row */
141         if (mask != 128)
142            src++;
143      }
144
145      if (count + width >= MAX_WIDTH || row + 1 == height) {
146         /* flush the span */
147         span.end = count;
148         if (ctx->Visual.rgbMode)
149            _swrast_write_rgba_span(ctx, &span);
150         else
151            _swrast_write_index_span(ctx, &span);
152         span.end = 0;
153         count = 0;
154      }
155   }
156
157   RENDER_FINISH(swrast,ctx);
158
159   if (unpack->BufferObj->Name) {
160      /* done with PBO so unmap it now */
161      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
162                              unpack->BufferObj);
163   }
164}
165
166
167#if 0
168/*
169 * XXX this is another way to implement Bitmap.  Use horizontal runs of
170 * fragments, initializing the mask array to indicate which fragments to
171 * draw or skip.
172 */
173void
174_swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
175		GLsizei width, GLsizei height,
176		const struct gl_pixelstore_attrib *unpack,
177		const GLubyte *bitmap )
178{
179   SWcontext *swrast = SWRAST_CONTEXT(ctx);
180   GLint row, col;
181   SWspan span;
182
183   ASSERT(ctx->RenderMode == GL_RENDER);
184   ASSERT(bitmap);
185
186   RENDER_START(swrast,ctx);
187
188   if (SWRAST_CONTEXT(ctx)->NewState)
189      _swrast_validate_derived( ctx );
190
191   INIT_SPAN(span, GL_BITMAP, width, 0, SPAN_MASK);
192
193   /*span.arrayMask |= SPAN_MASK;*/  /* we'll init span.mask[] */
194   span.x = px;
195   span.y = py;
196   /*span.end = width;*/
197
198   _swrast_span_default_color(ctx, &span);
199   if (ctx->Depth.Test)
200      _swrast_span_default_z(ctx, &span);
201   if (swrast->_FogEnabled)
202      _swrast_span_default_fog(ctx, &span);
203   if (ctx->Texture._EnabledCoordUnits)
204      _swrast_span_default_texcoords(ctx, &span);
205
206   for (row=0; row<height; row++, span.y++) {
207      const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
208                 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
209
210      if (unpack->LsbFirst) {
211         /* Lsb first */
212         GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
213         for (col=0; col<width; col++) {
214            span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
215            if (mask == 128U) {
216               src++;
217               mask = 1U;
218            }
219            else {
220               mask = mask << 1;
221            }
222         }
223
224         if (ctx->Visual.rgbMode)
225            _swrast_write_rgba_span(ctx, &span);
226         else
227	    _swrast_write_index_span(ctx, &span);
228
229         /* get ready for next row */
230         if (mask != 1)
231            src++;
232      }
233      else {
234         /* Msb first */
235         GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
236         for (col=0; col<width; col++) {
237            span.array->mask[col] = (*src & mask) ? GL_TRUE : GL_FALSE;
238            if (mask == 1U) {
239               src++;
240               mask = 128U;
241            }
242            else {
243               mask = mask >> 1;
244            }
245         }
246
247         if (ctx->Visual.rgbMode)
248            _swrast_write_rgba_span(ctx, &span);
249         else
250            _swrast_write_index_span(ctx, &span);
251
252         /* get ready for next row */
253         if (mask != 128)
254            src++;
255      }
256   }
257
258   RENDER_FINISH(swrast,ctx);
259}
260#endif
261