1 /* $Id: s_readpix.c,v 1.15 2002/07/09 01:22:52 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;
284 for (row
=0; row
<readHeight
; row
++) {
285 (*swrast
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
286 (GLchan (*)[4]) dest
);
287 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
288 _mesa_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
289 (GLchan (*)[4]) dest
);
291 dest
+= rowLength
* 4;
297 /* can't do this format/type combination */
306 * Read R, G, B, A, RGB, L, or LA pixels.
309 read_rgba_pixels( GLcontext
*ctx
,
311 GLsizei width
, GLsizei height
,
312 GLenum format
, GLenum type
, GLvoid
*pixels
,
313 const struct gl_pixelstore_attrib
*packing
)
315 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
318 _swrast_use_read_buffer(ctx
);
320 /* Try optimized path first */
321 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
322 format
, type
, pixels
, packing
)) {
324 _swrast_use_draw_buffer(ctx
);
328 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
330 /* do error checking on pixel type, format was already checked by caller */
332 case GL_UNSIGNED_BYTE
:
334 case GL_UNSIGNED_SHORT
:
336 case GL_UNSIGNED_INT
:
339 case GL_UNSIGNED_BYTE_3_3_2
:
340 case GL_UNSIGNED_BYTE_2_3_3_REV
:
341 case GL_UNSIGNED_SHORT_5_6_5
:
342 case GL_UNSIGNED_SHORT_5_6_5_REV
:
343 case GL_UNSIGNED_SHORT_4_4_4_4
:
344 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
345 case GL_UNSIGNED_SHORT_5_5_5_1
:
346 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
347 case GL_UNSIGNED_INT_8_8_8_8
:
348 case GL_UNSIGNED_INT_8_8_8_8_REV
:
349 case GL_UNSIGNED_INT_10_10_10_2
:
350 case GL_UNSIGNED_INT_2_10_10_10_REV
:
351 /* valid pixel type */
354 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
358 if (!_mesa_is_legal_format_and_type(format
, type
) ||
359 format
== GL_INTENSITY
) {
360 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
364 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
365 const GLuint transferOps
= ctx
->_ImageTransferState
;
366 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
369 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
371 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
374 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
377 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
381 /* read full RGBA, FLOAT image */
383 for (row
= 0; row
< height
; row
++, y
++) {
384 GLchan rgba
[MAX_WIDTH
][4];
385 if (ctx
->Visual
.rgbMode
) {
386 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
389 GLuint index
[MAX_WIDTH
];
390 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
391 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
392 _mesa_map_ci(ctx
, readWidth
, index
);
394 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
396 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
397 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
398 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
403 if (ctx
->Pixel
.Convolution2DEnabled
) {
404 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
407 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
408 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
412 /* finish transfer ops and pack the resulting image */
414 for (row
= 0; row
< height
; row
++) {
416 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
417 format
, type
, 0, row
, 0);
418 _mesa_pack_float_rgba_span(ctx
, readWidth
,
419 (const GLfloat (*)[4]) src
,
420 format
, type
, dest
, packing
,
421 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
422 src
+= readWidth
* 4;
428 for (row
= 0; row
< height
; row
++, y
++) {
429 GLchan rgba
[MAX_WIDTH
][4];
431 if (ctx
->Visual
.rgbMode
) {
432 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
435 GLuint index
[MAX_WIDTH
];
436 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
437 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
438 _mesa_map_ci(ctx
, readWidth
, index
);
440 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
442 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
443 format
, type
, 0, row
, 0);
444 if (ctx
->Visual
.redBits
< CHAN_BITS
||
445 ctx
->Visual
.greenBits
< CHAN_BITS
||
446 ctx
->Visual
.blueBits
< CHAN_BITS
) {
447 /* Requantize the color values into floating point and go from
448 * there. This fixes conformance failures with 16-bit color
449 * buffers, for example.
451 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
452 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
453 _mesa_chan_to_float_span(ctx
, readWidth
,
454 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
455 _mesa_pack_float_rgba_span(ctx
, readWidth
,
456 (CONST
GLfloat (*)[4]) rgbaf
,
457 format
, type
, dst
, packing
,
458 ctx
->_ImageTransferState
);
459 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
462 /* GLubytes are fine */
463 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
464 format
, type
, dst
, packing
,
465 ctx
->_ImageTransferState
);
470 _swrast_use_draw_buffer(ctx
);
476 _swrast_ReadPixels( GLcontext
*ctx
,
477 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
478 GLenum format
, GLenum type
,
479 const struct gl_pixelstore_attrib
*pack
,
482 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
485 if (swrast
->NewState
)
486 _swrast_validate_derived( ctx
);
488 RENDER_START(swrast
,ctx
);
492 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
494 case GL_STENCIL_INDEX
:
495 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
497 case GL_DEPTH_COMPONENT
:
498 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
506 case GL_LUMINANCE_ALPHA
:
511 read_rgba_pixels(ctx
, x
, y
, width
, height
,
512 format
, type
, pixels
, &ctx
->Pack
);
515 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
518 RENDER_FINISH(swrast
,ctx
);