| 1 | /* $Id: bitmap.c,v 1.2 2000-05-23 20:40:24 jeroen Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Mesa 3-D graphics library | 
|---|
| 5 | * Version:  3.3 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 1999  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 | #ifdef PC_HEADER | 
|---|
| 29 | #include "all.h" | 
|---|
| 30 | #else | 
|---|
| 31 | #include "glheader.h" | 
|---|
| 32 | #include "bitmap.h" | 
|---|
| 33 | #include "state.h" | 
|---|
| 34 | #include "context.h" | 
|---|
| 35 | #include "feedback.h" | 
|---|
| 36 | #include "image.h" | 
|---|
| 37 | #include "macros.h" | 
|---|
| 38 | #include "pb.h" | 
|---|
| 39 | #include "pixel.h" | 
|---|
| 40 | #include "types.h" | 
|---|
| 41 | #include "vbrender.h" | 
|---|
| 42 | #endif | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /* | 
|---|
| 47 | * Render a bitmap. | 
|---|
| 48 | */ | 
|---|
| 49 | static void | 
|---|
| 50 | render_bitmap( GLcontext *ctx, GLint px, GLint py, | 
|---|
| 51 | GLsizei width, GLsizei height, | 
|---|
| 52 | const struct gl_pixelstore_attrib *unpack, | 
|---|
| 53 | const GLubyte *bitmap ) | 
|---|
| 54 | { | 
|---|
| 55 | struct pixel_buffer *PB = ctx->PB; | 
|---|
| 56 | GLint row, col; | 
|---|
| 57 | GLdepth fragZ; | 
|---|
| 58 |  | 
|---|
| 59 | ASSERT(ctx->RenderMode == GL_RENDER); | 
|---|
| 60 |  | 
|---|
| 61 | if (!bitmap) { | 
|---|
| 62 | return;  /* NULL bitmap is legal, a no-op */ | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | /* Set bitmap drawing color */ | 
|---|
| 66 | if (ctx->Visual->RGBAflag) { | 
|---|
| 67 | GLint r, g, b, a; | 
|---|
| 68 | r = (GLint) (ctx->Current.RasterColor[0] * 255.0F); | 
|---|
| 69 | g = (GLint) (ctx->Current.RasterColor[1] * 255.0F); | 
|---|
| 70 | b = (GLint) (ctx->Current.RasterColor[2] * 255.0F); | 
|---|
| 71 | a = (GLint) (ctx->Current.RasterColor[3] * 255.0F); | 
|---|
| 72 | PB_SET_COLOR( ctx, PB, r, g, b, a ); | 
|---|
| 73 | } | 
|---|
| 74 | else { | 
|---|
| 75 | PB_SET_INDEX( ctx, PB, ctx->Current.RasterIndex ); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | fragZ = (GLdepth) ( ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF); | 
|---|
| 79 |  | 
|---|
| 80 | for (row=0; row<height; row++) { | 
|---|
| 81 | const GLubyte *src = (const GLubyte *) _mesa_image_address( unpack, | 
|---|
| 82 | bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 ); | 
|---|
| 83 |  | 
|---|
| 84 | if (unpack->LsbFirst) { | 
|---|
| 85 | /* Lsb first */ | 
|---|
| 86 | GLubyte mask = 1U << (unpack->SkipPixels & 0x7); | 
|---|
| 87 | for (col=0; col<width; col++) { | 
|---|
| 88 | if (*src & mask) { | 
|---|
| 89 | PB_WRITE_PIXEL( PB, px+col, py+row, fragZ ); | 
|---|
| 90 | } | 
|---|
| 91 | if (mask == 128U) { | 
|---|
| 92 | src++; | 
|---|
| 93 | mask = 1U; | 
|---|
| 94 | } | 
|---|
| 95 | else { | 
|---|
| 96 | mask = mask << 1; | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | PB_CHECK_FLUSH( ctx, PB ); | 
|---|
| 101 |  | 
|---|
| 102 | /* get ready for next row */ | 
|---|
| 103 | if (mask != 1) | 
|---|
| 104 | src++; | 
|---|
| 105 | } | 
|---|
| 106 | else { | 
|---|
| 107 | /* Msb first */ | 
|---|
| 108 | GLubyte mask = 128U >> (unpack->SkipPixels & 0x7); | 
|---|
| 109 | for (col=0; col<width; col++) { | 
|---|
| 110 | if (*src & mask) { | 
|---|
| 111 | PB_WRITE_PIXEL( PB, px+col, py+row, fragZ ); | 
|---|
| 112 | } | 
|---|
| 113 | if (mask == 1U) { | 
|---|
| 114 | src++; | 
|---|
| 115 | mask = 128U; | 
|---|
| 116 | } | 
|---|
| 117 | else { | 
|---|
| 118 | mask = mask >> 1; | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | PB_CHECK_FLUSH( ctx, PB ); | 
|---|
| 123 |  | 
|---|
| 124 | /* get ready for next row */ | 
|---|
| 125 | if (mask != 128) | 
|---|
| 126 | src++; | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | gl_flush_pb(ctx); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 | /* | 
|---|
| 136 | * Execute a glBitmap command. | 
|---|
| 137 | */ | 
|---|
| 138 | void | 
|---|
| 139 | _mesa_Bitmap( GLsizei width, GLsizei height, | 
|---|
| 140 | GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, | 
|---|
| 141 | const GLubyte *bitmap ) | 
|---|
| 142 | { | 
|---|
| 143 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 144 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBitmap"); | 
|---|
| 145 |  | 
|---|
| 146 | /* Error checking */ | 
|---|
| 147 | if (width < 0 || height < 0) { | 
|---|
| 148 | gl_error( ctx, GL_INVALID_VALUE, "glBitmap" ); | 
|---|
| 149 | return; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | if (ctx->Current.RasterPosValid == GL_FALSE) { | 
|---|
| 153 | return;    /* do nothing */ | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | if (ctx->RenderMode==GL_RENDER) { | 
|---|
| 157 | if (bitmap) { | 
|---|
| 158 | GLint x = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F ); | 
|---|
| 159 | GLint y = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F ); | 
|---|
| 160 | GLboolean completed = GL_FALSE; | 
|---|
| 161 |  | 
|---|
| 162 | if (ctx->NewState) { | 
|---|
| 163 | gl_update_state(ctx); | 
|---|
| 164 | gl_reduced_prim_change( ctx, GL_BITMAP ); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | if (ctx->PB->primitive!=GL_BITMAP) {   /* A.W. 1.1.2000 */ | 
|---|
| 168 | gl_reduced_prim_change( ctx, GL_BITMAP ); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | if (ctx->Driver.Bitmap) { | 
|---|
| 172 | /* let device driver try to render the bitmap */ | 
|---|
| 173 | completed = (*ctx->Driver.Bitmap)( ctx, x, y, width, height, | 
|---|
| 174 | &ctx->Unpack, bitmap ); | 
|---|
| 175 | } | 
|---|
| 176 | if (!completed) { | 
|---|
| 177 | /* use generic function */ | 
|---|
| 178 | render_bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap ); | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 | else if (ctx->RenderMode==GL_FEEDBACK) { | 
|---|
| 183 | GLfloat color[4], texcoord[4], invq; | 
|---|
| 184 | color[0] = ctx->Current.RasterColor[0]; | 
|---|
| 185 | color[1] = ctx->Current.RasterColor[1]; | 
|---|
| 186 | color[2] = ctx->Current.RasterColor[2]; | 
|---|
| 187 | color[3] = ctx->Current.RasterColor[3]; | 
|---|
| 188 | if (ctx->Current.Texcoord[0][3] == 0.0) | 
|---|
| 189 | invq = 1.0F; | 
|---|
| 190 | else | 
|---|
| 191 | invq = 1.0F / ctx->Current.RasterTexCoord[3]; | 
|---|
| 192 | texcoord[0] = ctx->Current.RasterTexCoord[0] * invq; | 
|---|
| 193 | texcoord[1] = ctx->Current.RasterTexCoord[1] * invq; | 
|---|
| 194 | texcoord[2] = ctx->Current.RasterTexCoord[2] * invq; | 
|---|
| 195 | texcoord[3] = ctx->Current.RasterTexCoord[3]; | 
|---|
| 196 | FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN ); | 
|---|
| 197 | gl_feedback_vertex( ctx, | 
|---|
| 198 | ctx->Current.RasterPos, | 
|---|
| 199 | color, ctx->Current.RasterIndex, texcoord ); | 
|---|
| 200 | } | 
|---|
| 201 | else if (ctx->RenderMode==GL_SELECT) { | 
|---|
| 202 | /* Bitmaps don't generate selection hits.  See appendix B of 1.1 spec. */ | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | /* update raster position */ | 
|---|
| 206 | ctx->Current.RasterPos[0] += xmove; | 
|---|
| 207 | ctx->Current.RasterPos[1] += ymove; | 
|---|
| 208 | } | 
|---|