1 /* $Id: s_readpix.c,v 1.16 2002/09/21 17:34:56 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
38 #include "s_alphabuf.h"
39 #include "s_context.h"
42 #include "s_stencil.h"
47 * Read a block of color index pixels.
50 read_index_pixels( GLcontext
*ctx
,
52 GLsizei width
, GLsizei height
,
53 GLenum type
, GLvoid
*pixels
,
54 const struct gl_pixelstore_attrib
*packing
)
56 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
60 if (ctx
->Visual
.rgbMode
) {
61 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
65 _swrast_use_read_buffer(ctx
);
67 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
69 /* process image row by row */
70 for (i
= 0; i
< height
; i
++) {
71 GLuint index
[MAX_WIDTH
];
74 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
+ i
, index
);
76 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
77 GL_COLOR_INDEX
, type
, 0, i
, 0);
79 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
80 &ctx
->Pack
, ctx
->_ImageTransferState
);
83 _swrast_use_draw_buffer(ctx
);
89 read_depth_pixels( GLcontext
*ctx
,
91 GLsizei width
, GLsizei height
,
92 GLenum type
, GLvoid
*pixels
,
93 const struct gl_pixelstore_attrib
*packing
)
96 GLboolean bias_or_scale
;
99 if (ctx
->Visual
.depthBits
<= 0) {
100 /* No depth buffer */
101 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
105 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
107 if (type
!= GL_BYTE
&&
108 type
!= GL_UNSIGNED_BYTE
&&
110 type
!= GL_UNSIGNED_SHORT
&&
112 type
!= GL_UNSIGNED_INT
&&
114 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
118 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
120 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
121 && !bias_or_scale
&& !packing
->SwapBytes
) {
122 /* Special case: directly read 16-bit unsigned depth values. */
124 for (j
=0;j
<height
;j
++,y
++) {
125 GLdepth depth
[MAX_WIDTH
];
126 GLushort
*dst
= (GLushort
*) _mesa_image_address( packing
, pixels
,
127 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
129 _mesa_read_depth_span(ctx
, width
, x
, y
, depth
);
130 for (i
= 0; i
< width
; i
++)
134 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
135 && !bias_or_scale
&& !packing
->SwapBytes
) {
136 /* Special case: directly read 32-bit unsigned depth values. */
138 for (j
=0;j
<height
;j
++,y
++) {
139 GLdepth
*dst
= (GLdepth
*) _mesa_image_address( packing
, pixels
,
140 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
141 _mesa_read_depth_span(ctx
, width
, x
, y
, dst
);
145 /* General case (slower) */
147 for (j
=0;j
<height
;j
++,y
++) {
148 GLfloat depth
[MAX_WIDTH
];
151 _mesa_read_depth_span_float(ctx
, readWidth
, x
, y
, depth
);
153 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
154 GL_DEPTH_COMPONENT
, type
, 0, j
, 0);
156 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
165 read_stencil_pixels( GLcontext
*ctx
,
167 GLsizei width
, GLsizei height
,
168 GLenum type
, GLvoid
*pixels
,
169 const struct gl_pixelstore_attrib
*packing
)
173 if (type
!= GL_BYTE
&&
174 type
!= GL_UNSIGNED_BYTE
&&
176 type
!= GL_UNSIGNED_SHORT
&&
178 type
!= GL_UNSIGNED_INT
&&
181 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
185 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
187 if (ctx
->Visual
.stencilBits
<= 0) {
188 /* No stencil buffer */
189 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
193 /* process image row by row */
194 for (j
=0;j
<height
;j
++,y
++) {
196 GLstencil stencil
[MAX_WIDTH
];
198 _mesa_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
200 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
201 GL_STENCIL_INDEX
, type
, 0, j
, 0);
203 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
210 * Optimized glReadPixels for particular pixel formats:
211 * GL_UNSIGNED_BYTE, GL_RGBA
212 * when pixel scaling, biasing and mapping are disabled.
215 read_fast_rgba_pixels( GLcontext
*ctx
,
217 GLsizei width
, GLsizei height
,
218 GLenum format
, GLenum type
,
220 const struct gl_pixelstore_attrib
*packing
)
222 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
223 /* can't do scale, bias, mapping, etc */
224 if (ctx
->_ImageTransferState
)
227 /* can't do fancy pixel packing */
228 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
234 GLint readWidth
= width
; /* actual width read */
235 GLint readHeight
= height
; /* actual height read */
236 GLint skipPixels
= packing
->SkipPixels
;
237 GLint skipRows
= packing
->SkipRows
;
240 if (packing
->RowLength
> 0)
241 rowLength
= packing
->RowLength
;
245 /* horizontal clipping */
251 if (srcX
+ readWidth
> (GLint
) ctx
->ReadBuffer
->Width
)
252 readWidth
-= (srcX
+ readWidth
- (GLint
) ctx
->ReadBuffer
->Width
);
256 /* vertical clipping */
262 if (srcY
+ readHeight
> (GLint
) ctx
->ReadBuffer
->Height
)
263 readHeight
-= (srcY
+ readHeight
- (GLint
) ctx
->ReadBuffer
->Height
);
269 * The window region at (destX, destY) of size (readWidth, readHeight)
271 * We'll write pixel data to buffer pointed to by "pixels" but we'll
272 * skip "skipRows" rows and skip "skipPixels" pixels/row.
275 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
276 #elif CHAN_BITS == 16
277 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
281 GLchan
*dest
= (GLchan
*) pixels
282 + (skipRows
* rowLength
+ skipPixels
) * 4;
285 if (packing
->Invert
) {
286 /* start at top and go down */
287 dest
+= (readHeight
- 1) * rowLength
* 4;
288 rowLength
= -rowLength
;
291 for (row
=0; row
<readHeight
; row
++) {
292 (*swrast
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
293 (GLchan (*)[4]) dest
);
294 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
295 _mesa_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
296 (GLchan (*)[4]) dest
);
298 dest
+= rowLength
* 4;
304 /* can't do this format/type combination */
313 * Read R, G, B, A, RGB, L, or LA pixels.
316 read_rgba_pixels( GLcontext
*ctx
,
318 GLsizei width
, GLsizei height
,
319 GLenum format
, GLenum type
, GLvoid
*pixels
,
320 const struct gl_pixelstore_attrib
*packing
)
322 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
325 _swrast_use_read_buffer(ctx
);
327 /* Try optimized path first */
328 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
329 format
, type
, pixels
, packing
)) {
331 _swrast_use_draw_buffer(ctx
);
335 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
337 /* do error checking on pixel type, format was already checked by caller */
339 case GL_UNSIGNED_BYTE
:
341 case GL_UNSIGNED_SHORT
:
343 case GL_UNSIGNED_INT
:
346 case GL_UNSIGNED_BYTE_3_3_2
:
347 case GL_UNSIGNED_BYTE_2_3_3_REV
:
348 case GL_UNSIGNED_SHORT_5_6_5
:
349 case GL_UNSIGNED_SHORT_5_6_5_REV
:
350 case GL_UNSIGNED_SHORT_4_4_4_4
:
351 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
352 case GL_UNSIGNED_SHORT_5_5_5_1
:
353 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
354 case GL_UNSIGNED_INT_8_8_8_8
:
355 case GL_UNSIGNED_INT_8_8_8_8_REV
:
356 case GL_UNSIGNED_INT_10_10_10_2
:
357 case GL_UNSIGNED_INT_2_10_10_10_REV
:
358 /* valid pixel type */
361 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
365 if (!_mesa_is_legal_format_and_type(format
, type
) ||
366 format
== GL_INTENSITY
) {
367 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
371 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
372 const GLuint transferOps
= ctx
->_ImageTransferState
;
373 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
376 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
378 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
381 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
384 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
388 /* read full RGBA, FLOAT image */
390 for (row
= 0; row
< height
; row
++, y
++) {
391 GLchan rgba
[MAX_WIDTH
][4];
392 if (ctx
->Visual
.rgbMode
) {
393 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
396 GLuint index
[MAX_WIDTH
];
397 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
398 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
399 _mesa_map_ci(ctx
, readWidth
, index
);
401 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
403 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
404 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
405 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
410 if (ctx
->Pixel
.Convolution2DEnabled
) {
411 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
414 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
415 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
419 /* finish transfer ops and pack the resulting image */
421 for (row
= 0; row
< height
; row
++) {
423 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
424 format
, type
, 0, row
, 0);
425 _mesa_pack_float_rgba_span(ctx
, readWidth
,
426 (const GLfloat (*)[4]) src
,
427 format
, type
, dest
, packing
,
428 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
429 src
+= readWidth
* 4;
435 for (row
= 0; row
< height
; row
++, y
++) {
436 GLchan rgba
[MAX_WIDTH
][4];
438 if (ctx
->Visual
.rgbMode
) {
439 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
442 GLuint index
[MAX_WIDTH
];
443 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
444 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
445 _mesa_map_ci(ctx
, readWidth
, index
);
447 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
449 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
450 format
, type
, 0, row
, 0);
451 if (ctx
->Visual
.redBits
< CHAN_BITS
||
452 ctx
->Visual
.greenBits
< CHAN_BITS
||
453 ctx
->Visual
.blueBits
< CHAN_BITS
) {
454 /* Requantize the color values into floating point and go from
455 * there. This fixes conformance failures with 16-bit color
456 * buffers, for example.
458 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
459 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
460 _mesa_chan_to_float_span(ctx
, readWidth
,
461 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
462 _mesa_pack_float_rgba_span(ctx
, readWidth
,
463 (CONST
GLfloat (*)[4]) rgbaf
,
464 format
, type
, dst
, packing
,
465 ctx
->_ImageTransferState
);
466 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
469 /* GLubytes are fine */
470 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
471 format
, type
, dst
, packing
,
472 ctx
->_ImageTransferState
);
477 _swrast_use_draw_buffer(ctx
);
483 _swrast_ReadPixels( GLcontext
*ctx
,
484 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
485 GLenum format
, GLenum type
,
486 const struct gl_pixelstore_attrib
*pack
,
489 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
492 if (swrast
->NewState
)
493 _swrast_validate_derived( ctx
);
495 RENDER_START(swrast
,ctx
);
499 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
501 case GL_STENCIL_INDEX
:
502 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
504 case GL_DEPTH_COMPONENT
:
505 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
513 case GL_LUMINANCE_ALPHA
:
518 read_rgba_pixels(ctx
, x
, y
, width
, height
,
519 format
, type
, pixels
, &ctx
->Pack
);
522 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
525 RENDER_FINISH(swrast
,ctx
);