| 1 |
/* $Id: readpix.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1997 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include "alphabuf.h" |
| 27 |
#include "context.h" |
| 28 |
#include "depth.h" |
| 29 |
#include "feedback.h" |
| 30 |
#include "dlist.h" |
| 31 |
#include "macros.h" |
| 32 |
#include "image.h" |
| 33 |
#include "readpix.h" |
| 34 |
#include "span.h" |
| 35 |
#include "stencil.h" |
| 36 |
#include "types.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/* |
| 43 |
* Read a block of color index pixels. |
| 44 |
*/ |
| 45 |
static void read_index_pixels( GLcontext *ctx, |
| 46 |
GLint x, GLint y, |
| 47 |
GLsizei width, GLsizei height, |
| 48 |
GLenum type, GLvoid *pixels ) |
| 49 |
{ |
| 50 |
GLint i, j; |
| 51 |
GLuint a, s, k, l, start; |
| 52 |
|
| 53 |
/* error checking */ |
| 54 |
if (ctx->Visual->RGBAflag) { |
| 55 |
gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" ); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
/* Size of each component */ |
| 60 |
s = gl_sizeof_type( type ); |
| 61 |
if (s<=0) { |
| 62 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
/* Compute packing parameters */ |
| 67 |
a = ctx->Pack.Alignment; |
| 68 |
if (ctx->Pack.RowLength>0) { |
| 69 |
l = ctx->Pack.RowLength; |
| 70 |
} |
| 71 |
else { |
| 72 |
l = width; |
| 73 |
} |
| 74 |
/* k = offset between rows in components */ |
| 75 |
if (s>=a) { |
| 76 |
k = l; |
| 77 |
} |
| 78 |
else { |
| 79 |
k = a/s * CEILING( s*l, a ); |
| 80 |
} |
| 81 |
|
| 82 |
/* offset to first component returned */ |
| 83 |
start = ctx->Pack.SkipRows * k + ctx->Pack.SkipPixels; |
| 84 |
|
| 85 |
/* process image row by row */ |
| 86 |
for (j=0;j<height;j++,y++) { |
| 87 |
GLuint index[MAX_WIDTH]; |
| 88 |
(*ctx->Driver.ReadIndexSpan)( ctx, width, x, y, index ); |
| 89 |
|
| 90 |
if (ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0) { |
| 91 |
GLuint s; |
| 92 |
if (ctx->Pixel.IndexShift<0) { |
| 93 |
/* right shift */ |
| 94 |
s = -ctx->Pixel.IndexShift; |
| 95 |
for (i=0;i<width;i++) { |
| 96 |
index[i] = (index[i] >> s) + ctx->Pixel.IndexOffset; |
| 97 |
} |
| 98 |
} |
| 99 |
else { |
| 100 |
/* left shift */ |
| 101 |
s = ctx->Pixel.IndexShift; |
| 102 |
for (i=0;i<width;i++) { |
| 103 |
index[i] = (index[i] << s) + ctx->Pixel.IndexOffset; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (ctx->Pixel.MapColorFlag) { |
| 109 |
for (i=0;i<width;i++) { |
| 110 |
index[i] = ctx->Pixel.MapItoI[ index[i] ]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
switch (type) { |
| 115 |
case GL_UNSIGNED_BYTE: |
| 116 |
{ |
| 117 |
GLubyte *dst = (GLubyte *) pixels + start + j * k; |
| 118 |
for (i=0;i<width;i++) { |
| 119 |
*dst++ = (GLubyte) index[i]; |
| 120 |
} |
| 121 |
} |
| 122 |
break; |
| 123 |
case GL_BYTE: |
| 124 |
{ |
| 125 |
GLbyte *dst = (GLbyte *) pixels + start + j * k; |
| 126 |
for (i=0;i<width;i++) { |
| 127 |
*dst++ = (GLbyte) index[i]; |
| 128 |
} |
| 129 |
} |
| 130 |
break; |
| 131 |
case GL_UNSIGNED_SHORT: |
| 132 |
{ |
| 133 |
GLushort *dst = (GLushort *) pixels + start + j * k; |
| 134 |
for (i=0;i<width;i++) { |
| 135 |
*dst++ = (GLushort) index[i]; |
| 136 |
} |
| 137 |
if (ctx->Pack.SwapBytes) { |
| 138 |
gl_swap2( (GLushort *) pixels + start + j * k, width ); |
| 139 |
} |
| 140 |
} |
| 141 |
break; |
| 142 |
case GL_SHORT: |
| 143 |
{ |
| 144 |
GLshort *dst = (GLshort *) pixels + start + j * k; |
| 145 |
for (i=0;i<width;i++) { |
| 146 |
*dst++ = (GLshort) index[i]; |
| 147 |
} |
| 148 |
if (ctx->Pack.SwapBytes) { |
| 149 |
gl_swap2( (GLushort *) pixels + start + j * k, width ); |
| 150 |
} |
| 151 |
} |
| 152 |
break; |
| 153 |
case GL_UNSIGNED_INT: |
| 154 |
{ |
| 155 |
GLuint *dst = (GLuint *) pixels + start + j * k; |
| 156 |
for (i=0;i<width;i++) { |
| 157 |
*dst++ = (GLuint) index[i]; |
| 158 |
} |
| 159 |
if (ctx->Pack.SwapBytes) { |
| 160 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 161 |
} |
| 162 |
} |
| 163 |
break; |
| 164 |
case GL_INT: |
| 165 |
{ |
| 166 |
GLint *dst = (GLint *) pixels + start + j * k; |
| 167 |
for (i=0;i<width;i++) { |
| 168 |
*dst++ = (GLint) index[i]; |
| 169 |
} |
| 170 |
if (ctx->Pack.SwapBytes) { |
| 171 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 172 |
} |
| 173 |
} |
| 174 |
break; |
| 175 |
case GL_FLOAT: |
| 176 |
{ |
| 177 |
GLfloat *dst = (GLfloat *) pixels + start + j * k; |
| 178 |
for (i=0;i<width;i++) { |
| 179 |
*dst++ = (GLfloat) index[i]; |
| 180 |
} |
| 181 |
if (ctx->Pack.SwapBytes) { |
| 182 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 183 |
} |
| 184 |
} |
| 185 |
break; |
| 186 |
default: |
| 187 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static void read_depth_pixels( GLcontext *ctx, |
| 195 |
GLint x, GLint y, |
| 196 |
GLsizei width, GLsizei height, |
| 197 |
GLenum type, GLvoid *pixels ) |
| 198 |
{ |
| 199 |
GLint i, j; |
| 200 |
GLuint a, s, k, l, start; |
| 201 |
GLboolean bias_or_scale; |
| 202 |
|
| 203 |
/* Error checking */ |
| 204 |
if (ctx->Visual->DepthBits<=0) { |
| 205 |
/* No depth buffer */ |
| 206 |
gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" ); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0; |
| 211 |
|
| 212 |
/* Size of each component */ |
| 213 |
s = gl_sizeof_type( type ); |
| 214 |
if (s<=0) { |
| 215 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
/* Compute packing parameters */ |
| 220 |
a = ctx->Pack.Alignment; |
| 221 |
if (ctx->Pack.RowLength>0) { |
| 222 |
l = ctx->Pack.RowLength; |
| 223 |
} |
| 224 |
else { |
| 225 |
l = width; |
| 226 |
} |
| 227 |
/* k = offset between rows in components */ |
| 228 |
if (s>=a) { |
| 229 |
k = l; |
| 230 |
} |
| 231 |
else { |
| 232 |
k = a/s * CEILING( s*l, a ); |
| 233 |
} |
| 234 |
|
| 235 |
/* offset to first component returned */ |
| 236 |
start = ctx->Pack.SkipRows * k + ctx->Pack.SkipPixels; |
| 237 |
|
| 238 |
if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort) |
| 239 |
&& !bias_or_scale && !ctx->Pack.SwapBytes) { |
| 240 |
/* Special case: directly read 16-bit unsigned depth values. */ |
| 241 |
for (j=0;j<height;j++,y++) { |
| 242 |
GLushort *dst = (GLushort *) pixels + start + j * k; |
| 243 |
(*ctx->Driver.ReadDepthSpanInt)( ctx, width, x, y, (GLdepth*) dst); |
| 244 |
} |
| 245 |
} |
| 246 |
else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint) |
| 247 |
&& !bias_or_scale && !ctx->Pack.SwapBytes) { |
| 248 |
/* Special case: directly read 32-bit unsigned depth values. */ |
| 249 |
/* Compute shift value to scale depth values up to 32-bit uints. */ |
| 250 |
GLuint shift = 0; |
| 251 |
GLuint max = MAX_DEPTH; |
| 252 |
while ((max&0x80000000)==0) { |
| 253 |
max = max << 1; |
| 254 |
shift++; |
| 255 |
} |
| 256 |
for (j=0;j<height;j++,y++) { |
| 257 |
GLuint *dst = (GLuint *) pixels + start + j * k; |
| 258 |
(*ctx->Driver.ReadDepthSpanInt)( ctx, width, x, y, (GLdepth*) dst); |
| 259 |
for (i=0;i<width;i++) { |
| 260 |
dst[i] = dst[i] << shift; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
else { |
| 265 |
/* General case (slow) */ |
| 266 |
for (j=0;j<height;j++,y++) { |
| 267 |
GLfloat depth[MAX_WIDTH]; |
| 268 |
|
| 269 |
(*ctx->Driver.ReadDepthSpanFloat)( ctx, width, x, y, depth ); |
| 270 |
|
| 271 |
if (bias_or_scale) { |
| 272 |
for (i=0;i<width;i++) { |
| 273 |
GLfloat d; |
| 274 |
d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias; |
| 275 |
depth[i] = CLAMP( d, 0.0, 1.0 ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
switch (type) { |
| 280 |
case GL_UNSIGNED_BYTE: |
| 281 |
{ |
| 282 |
GLubyte *dst = (GLubyte *) pixels + start + j * k; |
| 283 |
for (i=0;i<width;i++) { |
| 284 |
*dst++ = FLOAT_TO_UBYTE( depth[i] ); |
| 285 |
} |
| 286 |
} |
| 287 |
break; |
| 288 |
case GL_BYTE: |
| 289 |
{ |
| 290 |
GLbyte *dst = (GLbyte *) pixels + start + j * k; |
| 291 |
for (i=0;i<width;i++) { |
| 292 |
*dst++ = FLOAT_TO_BYTE( depth[i] ); |
| 293 |
} |
| 294 |
} |
| 295 |
break; |
| 296 |
case GL_UNSIGNED_SHORT: |
| 297 |
{ |
| 298 |
GLushort *dst = (GLushort *) pixels + start + j * k; |
| 299 |
for (i=0;i<width;i++) { |
| 300 |
*dst++ = FLOAT_TO_USHORT( depth[i] ); |
| 301 |
} |
| 302 |
if (ctx->Pack.SwapBytes) { |
| 303 |
gl_swap2( (GLushort *) pixels + start + j * k, width ); |
| 304 |
} |
| 305 |
} |
| 306 |
break; |
| 307 |
case GL_SHORT: |
| 308 |
{ |
| 309 |
GLshort *dst = (GLshort *) pixels + start + j * k; |
| 310 |
for (i=0;i<width;i++) { |
| 311 |
*dst++ = FLOAT_TO_SHORT( depth[i] ); |
| 312 |
} |
| 313 |
if (ctx->Pack.SwapBytes) { |
| 314 |
gl_swap2( (GLushort *) pixels + start + j * k, width ); |
| 315 |
} |
| 316 |
} |
| 317 |
break; |
| 318 |
case GL_UNSIGNED_INT: |
| 319 |
{ |
| 320 |
GLuint *dst = (GLuint *) pixels + start + j * k; |
| 321 |
for (i=0;i<width;i++) { |
| 322 |
*dst++ = FLOAT_TO_UINT( depth[i] ); |
| 323 |
} |
| 324 |
if (ctx->Pack.SwapBytes) { |
| 325 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 326 |
} |
| 327 |
} |
| 328 |
break; |
| 329 |
case GL_INT: |
| 330 |
{ |
| 331 |
GLint *dst = (GLint *) pixels + start + j * k; |
| 332 |
for (i=0;i<width;i++) { |
| 333 |
*dst++ = FLOAT_TO_INT( depth[i] ); |
| 334 |
} |
| 335 |
if (ctx->Pack.SwapBytes) { |
| 336 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 337 |
} |
| 338 |
} |
| 339 |
break; |
| 340 |
case GL_FLOAT: |
| 341 |
{ |
| 342 |
GLfloat *dst = (GLfloat *) pixels + start + j * k; |
| 343 |
for (i=0;i<width;i++) { |
| 344 |
*dst++ = depth[i]; |
| 345 |
} |
| 346 |
if (ctx->Pack.SwapBytes) { |
| 347 |
gl_swap4( (GLuint *) pixels + start + j * k, width ); |
| 348 |
} |
| 349 |
} |
| 350 |
break; |
| 351 |
default: |
| 352 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static void read_stencil_pixels( GLcontext *ctx, |
| 362 |
GLint x, GLint y, |
| 363 |
GLsizei width, GLsizei height, |
| 364 |
GLenum type, GLvoid *pixels ) |
| 365 |
{ |
| 366 |
GLint i, j; |
| 367 |
GLuint a, s, k, l, start; |
| 368 |
GLboolean shift_or_offset; |
| 369 |
|
| 370 |
if (ctx->Visual->StencilBits<=0) { |
| 371 |
/* No stencil buffer */ |
| 372 |
gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" ); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
shift_or_offset = ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0; |
| 377 |
|
| 378 |
/* Size of each component */ |
| 379 |
s = gl_sizeof_type( type ); |
| 380 |
if (s<=0) { |
| 381 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
/* Compute packing parameters */ |
| 386 |
a = ctx->Pack.Alignment; |
| 387 |
if (ctx->Pack.RowLength>0) { |
| 388 |
l = ctx->Pack.RowLength; |
| 389 |
} |
| 390 |
else { |
| 391 |
l = width; |
| 392 |
} |
| 393 |
/* k = offset between rows in components */ |
| 394 |
if (s>=a) { |
| 395 |
k = l; |
| 396 |
} |
| 397 |
else { |
| 398 |
k = a/s * CEILING( s*l, a ); |
| 399 |
} |
| 400 |
|
| 401 |
/* offset to first component returned */ |
| 402 |
start = ctx->Pack.SkipRows * k + ctx->Pack.SkipPixels; |
| 403 |
|
| 404 |
/* process image row by row */ |
| 405 |
for (j=0;j<height;j++,y++) { |
| 406 |
GLubyte stencil[MAX_WIDTH]; |
| 407 |
|
| 408 |
gl_read_stencil_span( ctx, width, x, y, stencil ); |
| 409 |
|
| 410 |
if (shift_or_offset) { |
| 411 |
GLuint s; |
| 412 |
if (ctx->Pixel.IndexShift<0) { |
| 413 |
/* right shift */ |
| 414 |
s = -ctx->Pixel.IndexShift; |
| 415 |
for (i=0;i<width;i++) { |
| 416 |
stencil[i] = (stencil[i] >> s) + ctx->Pixel.IndexOffset; |
| 417 |
} |
| 418 |
} |
| 419 |
else { |
| 420 |
/* left shift */ |
| 421 |
s = ctx->Pixel.IndexShift; |
| 422 |
for (i=0;i<width;i++) { |
| 423 |
stencil[i] = (stencil[i] << s) + ctx->Pixel.IndexOffset; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if (ctx->Pixel.MapStencilFlag) { |
| 429 |
for (i=0;i<width;i++) { |
| 430 |
stencil[i] = ctx->Pixel.MapStoS[ stencil[i] ]; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
switch (type) { |
| 435 |
case GL_UNSIGNED_BYTE: |
| 436 |
{ |
| 437 |
GLubyte *dst = (GLubyte *) pixels + start + j * k; |
| 438 |
MEMCPY( dst, stencil, width ); |
| 439 |
} |
| 440 |
break; |
| 441 |
case GL_BYTE: |
| 442 |
{ |
| 443 |
GLbyte *dst = (GLbyte *) pixels + start + j * k; |
| 444 |
MEMCPY( dst, stencil, width ); |
| 445 |
} |
| 446 |
break; |
| 447 |
case GL_UNSIGNED_SHORT: |
| 448 |
{ |
| 449 |
GLushort *dst = (GLushort *) pixels + start + j * k; |
| 450 |
for (i=0;i<width;i++) { |
| 451 |
*dst++ = (GLushort) stencil[i]; |
| 452 |
} |
| 453 |
if (ctx->Pack.SwapBytes) { |
| 454 |
gl_swap2( (GLushort *) pixels + start +j * k, width ); |
| 455 |
} |
| 456 |
} |
| 457 |
break; |
| 458 |
case GL_SHORT: |
| 459 |
{ |
| 460 |
GLshort *dst = (GLshort *) pixels + start + j * k; |
| 461 |
for (i=0;i<width;i++) { |
| 462 |
*dst++ = (GLshort) stencil[i]; |
| 463 |
} |
| 464 |
if (ctx->Pack.SwapBytes) { |
| 465 |
gl_swap2( (GLushort *) pixels + start +j * k, width ); |
| 466 |
} |
| 467 |
} |
| 468 |
break; |
| 469 |
case GL_UNSIGNED_INT: |
| 470 |
{ |
| 471 |
GLuint *dst = (GLuint *) pixels + start + j * k; |
| 472 |
for (i=0;i<width;i++) { |
| 473 |
*dst++ = (GLuint) stencil[i]; |
| 474 |
} |
| 475 |
if (ctx->Pack.SwapBytes) { |
| 476 |
gl_swap4( (GLuint *) pixels + start +j * k, width ); |
| 477 |
} |
| 478 |
} |
| 479 |
break; |
| 480 |
case GL_INT: |
| 481 |
{ |
| 482 |
GLint *dst = (GLint *) pixels + start + j * k; |
| 483 |
for (i=0;i<width;i++) { |
| 484 |
*dst++ = (GLint) stencil[i]; |
| 485 |
} |
| 486 |
if (ctx->Pack.SwapBytes) { |
| 487 |
gl_swap4( (GLuint *) pixels + start +j * k, width ); |
| 488 |
} |
| 489 |
} |
| 490 |
break; |
| 491 |
case GL_FLOAT: |
| 492 |
{ |
| 493 |
GLfloat *dst = (GLfloat *) pixels + start + j * k; |
| 494 |
for (i=0;i<width;i++) { |
| 495 |
*dst++ = (GLfloat) stencil[i]; |
| 496 |
} |
| 497 |
if (ctx->Pack.SwapBytes) { |
| 498 |
gl_swap4( (GLuint *) pixels + start +j * k, width ); |
| 499 |
} |
| 500 |
} |
| 501 |
break; |
| 502 |
default: |
| 503 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
/* |
| 511 |
* Test if scaling or biasing of colors is needed. |
| 512 |
*/ |
| 513 |
static GLboolean scale_or_bias_rgba( GLcontext *ctx ) |
| 514 |
{ |
| 515 |
if (ctx->Pixel.RedScale!=1.0F || ctx->Pixel.RedBias!=0.0F || |
| 516 |
ctx->Pixel.GreenScale!=1.0F || ctx->Pixel.GreenBias!=0.0F || |
| 517 |
ctx->Pixel.BlueScale!=1.0F || ctx->Pixel.BlueBias!=0.0F || |
| 518 |
ctx->Pixel.AlphaScale!=1.0F || ctx->Pixel.AlphaBias!=0.0F) { |
| 519 |
return GL_TRUE; |
| 520 |
} |
| 521 |
else { |
| 522 |
return GL_FALSE; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
/* |
| 529 |
* Apply scale and bias factors to an array of RGBA pixels. |
| 530 |
*/ |
| 531 |
static void scale_and_bias_rgba( GLcontext *ctx, |
| 532 |
GLint n, |
| 533 |
GLfloat red[], GLfloat green[], |
| 534 |
GLfloat blue[], GLfloat alpha[] ) |
| 535 |
{ |
| 536 |
register GLint i; |
| 537 |
register GLfloat r, g, b, a; |
| 538 |
|
| 539 |
for (i=0;i<n;i++) { |
| 540 |
r = red[i] * ctx->Pixel.RedScale + ctx->Pixel.RedBias; |
| 541 |
g = green[i] * ctx->Pixel.GreenScale + ctx->Pixel.GreenBias; |
| 542 |
b = blue[i] * ctx->Pixel.BlueScale + ctx->Pixel.BlueBias; |
| 543 |
a = alpha[i] * ctx->Pixel.AlphaScale + ctx->Pixel.AlphaBias; |
| 544 |
red[i] = CLAMP( r, 0.0F, 1.0F ); |
| 545 |
green[i] = CLAMP( g, 0.0F, 1.0F ); |
| 546 |
blue[i] = CLAMP( b, 0.0F, 1.0F ); |
| 547 |
alpha[i] = CLAMP( a, 0.0F, 1.0F ); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
/* |
| 554 |
* Apply pixel mapping to an array of RGBA pixels. |
| 555 |
*/ |
| 556 |
static void map_rgba( GLcontext *ctx, |
| 557 |
GLint n, |
| 558 |
GLfloat red[], GLfloat green[], |
| 559 |
GLfloat blue[], GLfloat alpha[] ) |
| 560 |
{ |
| 561 |
GLfloat rscale = ctx->Pixel.MapRtoRsize-1; |
| 562 |
GLfloat gscale = ctx->Pixel.MapGtoGsize-1; |
| 563 |
GLfloat bscale = ctx->Pixel.MapBtoBsize-1; |
| 564 |
GLfloat ascale = ctx->Pixel.MapAtoAsize-1; |
| 565 |
GLint i; |
| 566 |
|
| 567 |
for (i=0;i<n;i++) { |
| 568 |
red[i] = ctx->Pixel.MapRtoR[ (GLint) (red[i] * rscale) ]; |
| 569 |
green[i] = ctx->Pixel.MapGtoG[ (GLint) (green[i] * gscale) ]; |
| 570 |
blue[i] = ctx->Pixel.MapBtoB[ (GLint) (blue[i] * bscale) ]; |
| 571 |
alpha[i] = ctx->Pixel.MapAtoA[ (GLint) (alpha[i] * ascale) ]; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
/* |
| 579 |
* Read R, G, B, A, RGB, L, or LA pixels. |
| 580 |
*/ |
| 581 |
static void read_color_pixels( GLcontext *ctx, |
| 582 |
GLint x, GLint y, |
| 583 |
GLsizei width, GLsizei height, |
| 584 |
GLenum format, GLenum type, GLvoid *pixels ) |
| 585 |
{ |
| 586 |
GLint i, j, n, a, s, l, k; |
| 587 |
GLboolean scale_or_bias; |
| 588 |
DEFARRAY(GLfloat, red, MAX_WIDTH); |
| 589 |
DEFARRAY(GLfloat, green, MAX_WIDTH); |
| 590 |
DEFARRAY(GLfloat, blue, MAX_WIDTH); |
| 591 |
DEFARRAY(GLfloat, alpha, MAX_WIDTH); |
| 592 |
DEFARRAY(GLfloat, luminance, MAX_WIDTH); |
| 593 |
GLboolean r_flag, g_flag, b_flag, a_flag, l_flag; |
| 594 |
GLuint start; |
| 595 |
|
| 596 |
scale_or_bias = scale_or_bias_rgba( ctx ); |
| 597 |
|
| 598 |
/* Determine how many / which components to return */ |
| 599 |
r_flag = g_flag = b_flag = a_flag = l_flag = GL_FALSE; |
| 600 |
switch (format) { |
| 601 |
case GL_RED: r_flag = GL_TRUE; n = 1; break; |
| 602 |
case GL_GREEN: g_flag = GL_TRUE; n = 1; break; |
| 603 |
case GL_BLUE: b_flag = GL_TRUE; n = 1; break; |
| 604 |
case GL_ALPHA: a_flag = GL_TRUE; n = 1; break; |
| 605 |
case GL_LUMINANCE: l_flag = GL_TRUE; n = 1; break; |
| 606 |
case GL_LUMINANCE_ALPHA: l_flag = a_flag = GL_TRUE; n = 2; break; |
| 607 |
case GL_RGB: r_flag = g_flag = b_flag = GL_TRUE; n = 3; break; |
| 608 |
case GL_RGBA: r_flag = g_flag = b_flag = a_flag = GL_TRUE; n = 4; break; |
| 609 |
default: |
| 610 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" ); |
| 611 |
UNDEFARRAY( red ); |
| 612 |
UNDEFARRAY( green ); |
| 613 |
UNDEFARRAY( blue ); |
| 614 |
UNDEFARRAY( alpha ); |
| 615 |
UNDEFARRAY( luminance ); |
| 616 |
return; |
| 617 |
} |
| 618 |
|
| 619 |
/* Size of each component */ |
| 620 |
s = gl_sizeof_type( type ); |
| 621 |
if (s<=0) { |
| 622 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 623 |
UNDEFARRAY( red ); |
| 624 |
UNDEFARRAY( green ); |
| 625 |
UNDEFARRAY( blue ); |
| 626 |
UNDEFARRAY( alpha ); |
| 627 |
UNDEFARRAY( luminance ); |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
/* Compute packing parameters */ |
| 632 |
a = ctx->Pack.Alignment; |
| 633 |
if (ctx->Pack.RowLength>0) { |
| 634 |
l = ctx->Pack.RowLength; |
| 635 |
} |
| 636 |
else { |
| 637 |
l = width; |
| 638 |
} |
| 639 |
/* k = offset between rows in components */ |
| 640 |
if (s>=a) { |
| 641 |
k = n * l; |
| 642 |
} |
| 643 |
else { |
| 644 |
k = a/s * CEILING( s*n*l, a ); |
| 645 |
} |
| 646 |
|
| 647 |
/* offset to first component returned */ |
| 648 |
start = ctx->Pack.SkipRows * k + ctx->Pack.SkipPixels * n; |
| 649 |
|
| 650 |
/* process image row by row */ |
| 651 |
for (j=0;j<height;j++,y++) { |
| 652 |
|
| 653 |
/* |
| 654 |
* Read the pixels from frame buffer |
| 655 |
*/ |
| 656 |
if (ctx->Visual->RGBAflag) { |
| 657 |
DEFARRAY(GLubyte, r, MAX_WIDTH); |
| 658 |
DEFARRAY(GLubyte, g, MAX_WIDTH); |
| 659 |
DEFARRAY(GLubyte, b, MAX_WIDTH); |
| 660 |
DEFARRAY(GLubyte, a, MAX_WIDTH); |
| 661 |
GLfloat rscale = 1.0F * ctx->Visual->InvRedScale; |
| 662 |
GLfloat gscale = 1.0F * ctx->Visual->InvGreenScale; |
| 663 |
GLfloat bscale = 1.0F * ctx->Visual->InvBlueScale; |
| 664 |
GLfloat ascale = 1.0F * ctx->Visual->InvAlphaScale; |
| 665 |
|
| 666 |
/* read colors and convert to floats */ |
| 667 |
(*ctx->Driver.ReadColorSpan)( ctx, width, x, y, r, g, b, a ); |
| 668 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 669 |
gl_read_alpha_span( ctx, width, x, y, a ); |
| 670 |
} |
| 671 |
for (i=0;i<width;i++) { |
| 672 |
red[i] = r[i] * rscale; |
| 673 |
green[i] = g[i] * gscale; |
| 674 |
blue[i] = b[i] * bscale; |
| 675 |
alpha[i] = a[i] * ascale; |
| 676 |
} |
| 677 |
|
| 678 |
if (scale_or_bias) { |
| 679 |
scale_and_bias_rgba( ctx, width, red, green, blue, alpha ); |
| 680 |
} |
| 681 |
if (ctx->Pixel.MapColorFlag) { |
| 682 |
map_rgba( ctx, width, red, green, blue, alpha ); |
| 683 |
} |
| 684 |
UNDEFARRAY(r); |
| 685 |
UNDEFARRAY(g); |
| 686 |
UNDEFARRAY(b); |
| 687 |
UNDEFARRAY(a); |
| 688 |
} |
| 689 |
else { |
| 690 |
/* convert CI values to RGBA */ |
| 691 |
GLuint index[MAX_WIDTH]; |
| 692 |
(*ctx->Driver.ReadIndexSpan)( ctx, width, x, y, index ); |
| 693 |
|
| 694 |
if (ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0) { |
| 695 |
GLuint s; |
| 696 |
if (ctx->Pixel.IndexShift<0) { |
| 697 |
/* right shift */ |
| 698 |
s = -ctx->Pixel.IndexShift; |
| 699 |
for (i=0;i<width;i++) { |
| 700 |
index[i] = (index[i] >> s) + ctx->Pixel.IndexOffset; |
| 701 |
} |
| 702 |
} |
| 703 |
else { |
| 704 |
/* left shift */ |
| 705 |
s = ctx->Pixel.IndexShift; |
| 706 |
for (i=0;i<width;i++) { |
| 707 |
index[i] = (index[i] << s) + ctx->Pixel.IndexOffset; |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
for (i=0;i<width;i++) { |
| 713 |
red[i] = ctx->Pixel.MapItoR[ index[i] ]; |
| 714 |
green[i] = ctx->Pixel.MapItoG[ index[i] ]; |
| 715 |
blue[i] = ctx->Pixel.MapItoB[ index[i] ]; |
| 716 |
alpha[i] = ctx->Pixel.MapItoA[ index[i] ]; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if (l_flag) { |
| 721 |
for (i=0;i<width;i++) { |
| 722 |
GLfloat sum = red[i] + green[i] + blue[i]; |
| 723 |
luminance[i] = CLAMP( sum, 0.0F, 1.0F ); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
/* |
| 728 |
* Pack/transfer/store the pixels |
| 729 |
*/ |
| 730 |
|
| 731 |
switch (type) { |
| 732 |
case GL_UNSIGNED_BYTE: |
| 733 |
{ |
| 734 |
GLubyte *dst = (GLubyte *) pixels + start + j * k; |
| 735 |
for (i=0;i<width;i++) { |
| 736 |
if (r_flag) *dst++ = FLOAT_TO_UBYTE( red[i] ); |
| 737 |
if (g_flag) *dst++ = FLOAT_TO_UBYTE( green[i] ); |
| 738 |
if (b_flag) *dst++ = FLOAT_TO_UBYTE( blue[i] ); |
| 739 |
if (l_flag) *dst++ = FLOAT_TO_UBYTE( luminance[i] ); |
| 740 |
if (a_flag) *dst++ = FLOAT_TO_UBYTE( alpha[i] ); |
| 741 |
} |
| 742 |
} |
| 743 |
break; |
| 744 |
case GL_BYTE: |
| 745 |
{ |
| 746 |
GLbyte *dst = (GLbyte *) pixels + start + j * k; |
| 747 |
for (i=0;i<width;i++) { |
| 748 |
if (r_flag) *dst++ = FLOAT_TO_BYTE( red[i] ); |
| 749 |
if (g_flag) *dst++ = FLOAT_TO_BYTE( green[i] ); |
| 750 |
if (b_flag) *dst++ = FLOAT_TO_BYTE( blue[i] ); |
| 751 |
if (l_flag) *dst++ = FLOAT_TO_BYTE( luminance[i] ); |
| 752 |
if (a_flag) *dst++ = FLOAT_TO_BYTE( alpha[i] ); |
| 753 |
} |
| 754 |
} |
| 755 |
break; |
| 756 |
case GL_UNSIGNED_SHORT: |
| 757 |
{ |
| 758 |
GLushort *dst = (GLushort *) pixels + start + j * k; |
| 759 |
for (i=0;i<width;i++) { |
| 760 |
if (r_flag) *dst++ = FLOAT_TO_USHORT( red[i] ); |
| 761 |
if (g_flag) *dst++ = FLOAT_TO_USHORT( green[i] ); |
| 762 |
if (b_flag) *dst++ = FLOAT_TO_USHORT( blue[i] ); |
| 763 |
if (l_flag) *dst++ = FLOAT_TO_USHORT( luminance[i] ); |
| 764 |
if (a_flag) *dst++ = FLOAT_TO_USHORT( alpha[i] ); |
| 765 |
} |
| 766 |
} |
| 767 |
if (ctx->Pack.SwapBytes) { |
| 768 |
gl_swap2( (GLushort *) pixels + start + j * k, width*n ); |
| 769 |
} |
| 770 |
break; |
| 771 |
case GL_SHORT: |
| 772 |
{ |
| 773 |
GLshort *dst = (GLshort *) pixels + start + j * k; |
| 774 |
for (i=0;i<width;i++) { |
| 775 |
if (r_flag) *dst++ = FLOAT_TO_SHORT( red[i] ); |
| 776 |
if (g_flag) *dst++ = FLOAT_TO_SHORT( green[i] ); |
| 777 |
if (b_flag) *dst++ = FLOAT_TO_SHORT( blue[i] ); |
| 778 |
if (l_flag) *dst++ = FLOAT_TO_SHORT( luminance[i] ); |
| 779 |
if (a_flag) *dst++ = FLOAT_TO_SHORT( alpha[i] ); |
| 780 |
} |
| 781 |
if (ctx->Pack.SwapBytes) { |
| 782 |
gl_swap2( (GLushort *) pixels + start + j * k, width*n ); |
| 783 |
} |
| 784 |
} |
| 785 |
break; |
| 786 |
case GL_UNSIGNED_INT: |
| 787 |
{ |
| 788 |
GLuint *dst = (GLuint *) pixels + start + j * k; |
| 789 |
for (i=0;i<width;i++) { |
| 790 |
if (r_flag) *dst++ = FLOAT_TO_UINT( red[i] ); |
| 791 |
if (g_flag) *dst++ = FLOAT_TO_UINT( green[i] ); |
| 792 |
if (b_flag) *dst++ = FLOAT_TO_UINT( blue[i] ); |
| 793 |
if (l_flag) *dst++ = FLOAT_TO_UINT( luminance[i] ); |
| 794 |
if (a_flag) *dst++ = FLOAT_TO_UINT( alpha[i] ); |
| 795 |
} |
| 796 |
if (ctx->Pack.SwapBytes) { |
| 797 |
gl_swap4( (GLuint *) pixels + start + j * k, width*n ); |
| 798 |
} |
| 799 |
} |
| 800 |
break; |
| 801 |
case GL_INT: |
| 802 |
{ |
| 803 |
GLint *dst = (GLint *) pixels + start + j * k; |
| 804 |
for (i=0;i<width;i++) { |
| 805 |
if (r_flag) *dst++ = FLOAT_TO_INT( red[i] ); |
| 806 |
if (g_flag) *dst++ = FLOAT_TO_INT( green[i] ); |
| 807 |
if (b_flag) *dst++ = FLOAT_TO_INT( blue[i] ); |
| 808 |
if (l_flag) *dst++ = FLOAT_TO_INT( luminance[i] ); |
| 809 |
if (a_flag) *dst++ = FLOAT_TO_INT( alpha[i] ); |
| 810 |
} |
| 811 |
if (ctx->Pack.SwapBytes) { |
| 812 |
gl_swap4( (GLuint *) pixels + start + j * k, width*n ); |
| 813 |
} |
| 814 |
} |
| 815 |
break; |
| 816 |
case GL_FLOAT: |
| 817 |
{ |
| 818 |
GLfloat *dst = (GLfloat *) pixels + start + j * k; |
| 819 |
for (i=0;i<width;i++) { |
| 820 |
if (r_flag) *dst++ = red[i]; |
| 821 |
if (g_flag) *dst++ = green[i]; |
| 822 |
if (b_flag) *dst++ = blue[i]; |
| 823 |
if (l_flag) *dst++ = luminance[i]; |
| 824 |
if (a_flag) *dst++ = alpha[i]; |
| 825 |
} |
| 826 |
if (ctx->Pack.SwapBytes) { |
| 827 |
gl_swap4( (GLuint *) pixels + start + j * k, width*n ); |
| 828 |
} |
| 829 |
} |
| 830 |
break; |
| 831 |
default: |
| 832 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" ); |
| 833 |
} |
| 834 |
} |
| 835 |
UNDEFARRAY( red ); |
| 836 |
UNDEFARRAY( green ); |
| 837 |
UNDEFARRAY( blue ); |
| 838 |
UNDEFARRAY( alpha ); |
| 839 |
UNDEFARRAY( luminance ); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
void gl_ReadPixels( GLcontext *ctx, |
| 845 |
GLint x, GLint y, GLsizei width, GLsizei height, |
| 846 |
GLenum format, GLenum type, GLvoid *pixels ) |
| 847 |
{ |
| 848 |
(void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer ); |
| 849 |
|
| 850 |
switch (format) { |
| 851 |
case GL_COLOR_INDEX: |
| 852 |
read_index_pixels( ctx, x, y, width, height, type, pixels ); |
| 853 |
break; |
| 854 |
case GL_STENCIL_INDEX: |
| 855 |
read_stencil_pixels( ctx, x, y, width, height, type, pixels ); |
| 856 |
break; |
| 857 |
case GL_DEPTH_COMPONENT: |
| 858 |
read_depth_pixels( ctx, x, y, width, height, type, pixels ); |
| 859 |
break; |
| 860 |
case GL_RED: |
| 861 |
case GL_GREEN: |
| 862 |
case GL_BLUE: |
| 863 |
case GL_ALPHA: |
| 864 |
case GL_RGB: |
| 865 |
case GL_LUMINANCE: |
| 866 |
case GL_LUMINANCE_ALPHA: |
| 867 |
case GL_RGBA: |
| 868 |
read_color_pixels( ctx, x, y, width, height, format, type, pixels ); |
| 869 |
break; |
| 870 |
default: |
| 871 |
gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" ); |
| 872 |
} |
| 873 |
|
| 874 |
(void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer ); |
| 875 |
} |