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