1 /* $Id: s_readpix.c,v 1.12 2001/05/16 20:27:12 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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 ASSERT(swrast
->Driver
.SetReadBuffer
);
66 (*swrast
->Driver
.SetReadBuffer
)(ctx
, ctx
->ReadBuffer
,
67 ctx
->Pixel
.DriverReadBuffer
);
69 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
71 /* process image row by row */
72 for (i
= 0; i
< height
; i
++) {
73 GLuint index
[MAX_WIDTH
];
76 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
+ i
, index
);
78 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
79 GL_COLOR_INDEX
, type
, 0, i
, 0);
81 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
82 &ctx
->Pack
, ctx
->_ImageTransferState
);
85 (*swrast
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
,
86 ctx
->Color
.DriverDrawBuffer
);
92 read_depth_pixels( GLcontext
*ctx
,
94 GLsizei width
, GLsizei height
,
95 GLenum type
, GLvoid
*pixels
,
96 const struct gl_pixelstore_attrib
*packing
)
99 GLboolean bias_or_scale
;
102 if (ctx
->Visual
.depthBits
<= 0) {
103 /* No depth buffer */
104 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
108 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
110 if (type
!= GL_BYTE
&&
111 type
!= GL_UNSIGNED_BYTE
&&
113 type
!= GL_UNSIGNED_SHORT
&&
115 type
!= GL_UNSIGNED_INT
&&
117 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
121 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
123 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
124 && !bias_or_scale
&& !packing
->SwapBytes
) {
125 /* Special case: directly read 16-bit unsigned depth values. */
127 for (j
=0;j
<height
;j
++,y
++) {
128 GLdepth depth
[MAX_WIDTH
];
129 GLushort
*dst
= (GLushort
*) _mesa_image_address( packing
, pixels
,
130 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
132 _mesa_read_depth_span(ctx
, width
, x
, y
, depth
);
133 for (i
= 0; i
< width
; i
++)
137 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
138 && !bias_or_scale
&& !packing
->SwapBytes
) {
139 /* Special case: directly read 32-bit unsigned depth values. */
141 for (j
=0;j
<height
;j
++,y
++) {
142 GLdepth
*dst
= (GLdepth
*) _mesa_image_address( packing
, pixels
,
143 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
144 _mesa_read_depth_span(ctx
, width
, x
, y
, dst
);
148 /* General case (slower) */
150 for (j
=0;j
<height
;j
++,y
++) {
151 GLfloat depth
[MAX_WIDTH
];
154 _mesa_read_depth_span_float(ctx
, readWidth
, x
, y
, depth
);
156 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
157 GL_DEPTH_COMPONENT
, type
, 0, j
, 0);
159 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
168 read_stencil_pixels( GLcontext
*ctx
,
170 GLsizei width
, GLsizei height
,
171 GLenum type
, GLvoid
*pixels
,
172 const struct gl_pixelstore_attrib
*packing
)
176 if (type
!= GL_BYTE
&&
177 type
!= GL_UNSIGNED_BYTE
&&
179 type
!= GL_UNSIGNED_SHORT
&&
181 type
!= GL_UNSIGNED_INT
&&
184 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
188 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
190 if (ctx
->Visual
.stencilBits
<= 0) {
191 /* No stencil buffer */
192 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
196 /* process image row by row */
197 for (j
=0;j
<height
;j
++,y
++) {
199 GLstencil stencil
[MAX_WIDTH
];
201 _mesa_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
203 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
204 GL_STENCIL_INDEX
, type
, 0, j
, 0);
206 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
213 * Optimized glReadPixels for particular pixel formats:
214 * GL_UNSIGNED_BYTE, GL_RGBA
215 * when pixel scaling, biasing and mapping are disabled.
218 read_fast_rgba_pixels( GLcontext
*ctx
,
220 GLsizei width
, GLsizei height
,
221 GLenum format
, GLenum type
,
223 const struct gl_pixelstore_attrib
*packing
)
225 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
226 /* can't do scale, bias, mapping, etc */
227 if (ctx
->_ImageTransferState
)
230 /* can't do fancy pixel packing */
231 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
237 GLint readWidth
= width
; /* actual width read */
238 GLint readHeight
= height
; /* actual height read */
239 GLint skipPixels
= packing
->SkipPixels
;
240 GLint skipRows
= packing
->SkipRows
;
243 if (packing
->RowLength
> 0)
244 rowLength
= packing
->RowLength
;
248 /* horizontal clipping */
249 if (srcX
< ctx
->ReadBuffer
->_Xmin
) {
250 skipPixels
+= (ctx
->ReadBuffer
->_Xmin
- srcX
);
251 readWidth
-= (ctx
->ReadBuffer
->_Xmin
- srcX
);
252 srcX
= ctx
->ReadBuffer
->_Xmin
;
254 if (srcX
+ readWidth
> ctx
->ReadBuffer
->_Xmax
)
255 readWidth
-= (srcX
+ readWidth
- ctx
->ReadBuffer
->_Xmax
);
259 /* vertical clipping */
260 if (srcY
< ctx
->ReadBuffer
->_Ymin
) {
261 skipRows
+= (ctx
->ReadBuffer
->_Ymin
- srcY
);
262 readHeight
-= (ctx
->ReadBuffer
->_Ymin
- srcY
);
263 srcY
= ctx
->ReadBuffer
->_Ymin
;
265 if (srcY
+ readHeight
> ctx
->ReadBuffer
->_Ymax
)
266 readHeight
-= (srcY
+ readHeight
- ctx
->ReadBuffer
->_Ymax
);
272 * The window region at (destX, destY) of size (readWidth, readHeight)
274 * We'll write pixel data to buffer pointed to by "pixels" but we'll
275 * skip "skipRows" rows and skip "skipPixels" pixels/row.
278 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
279 #elif CHAN_BITS == 16
280 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
284 GLchan
*dest
= (GLchan
*) pixels
285 + (skipRows
* rowLength
+ skipPixels
) * 4;
287 for (row
=0; row
<readHeight
; row
++) {
288 (*swrast
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
289 (GLchan (*)[4]) dest
);
290 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
291 _mesa_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
292 (GLchan (*)[4]) dest
);
294 dest
+= rowLength
* 4;
300 /* can't do this format/type combination */
309 * Read R, G, B, A, RGB, L, or LA pixels.
312 read_rgba_pixels( GLcontext
*ctx
,
314 GLsizei width
, GLsizei height
,
315 GLenum format
, GLenum type
, GLvoid
*pixels
,
316 const struct gl_pixelstore_attrib
*packing
)
318 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
321 (*swrast
->Driver
.SetReadBuffer
)(ctx
, ctx
->ReadBuffer
, ctx
->Pixel
.DriverReadBuffer
);
323 /* Try optimized path first */
324 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
325 format
, type
, pixels
, packing
)) {
327 (*swrast
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
331 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
333 /* do error checking on pixel type, format was already checked by caller */
335 case GL_UNSIGNED_BYTE
:
337 case GL_UNSIGNED_SHORT
:
339 case GL_UNSIGNED_INT
:
342 case GL_UNSIGNED_BYTE_3_3_2
:
343 case GL_UNSIGNED_BYTE_2_3_3_REV
:
344 case GL_UNSIGNED_SHORT_5_6_5
:
345 case GL_UNSIGNED_SHORT_5_6_5_REV
:
346 case GL_UNSIGNED_SHORT_4_4_4_4
:
347 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
348 case GL_UNSIGNED_SHORT_5_5_5_1
:
349 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
350 case GL_UNSIGNED_INT_8_8_8_8
:
351 case GL_UNSIGNED_INT_8_8_8_8_REV
:
352 case GL_UNSIGNED_INT_10_10_10_2
:
353 case GL_UNSIGNED_INT_2_10_10_10_REV
:
354 /* valid pixel type */
357 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
361 if (!_mesa_is_legal_format_and_type(format
, type
) ||
362 format
== GL_INTENSITY
) {
363 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
367 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
368 const GLuint transferOps
= ctx
->_ImageTransferState
;
369 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
372 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
374 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
377 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
384 /* read full RGBA, FLOAT image */
386 for (row
= 0; row
< height
; row
++, y
++) {
387 GLchan rgba
[MAX_WIDTH
][4];
388 if (ctx
->Visual
.rgbMode
) {
389 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
392 GLuint index
[MAX_WIDTH
];
393 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
394 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
395 _mesa_map_ci(ctx
, readWidth
, index
);
397 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
399 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
400 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
401 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
406 if (ctx
->Pixel
.Convolution2DEnabled
) {
407 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
410 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
411 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
415 /* finish transfer ops and pack the resulting image */
417 for (row
= 0; row
< height
; row
++) {
419 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
420 format
, type
, 0, row
, 0);
421 _mesa_pack_float_rgba_span(ctx
, readWidth
,
422 (const GLfloat (*)[4]) src
,
423 format
, type
, dest
, packing
,
424 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
425 src
+= readWidth
* 4;
431 for (row
= 0; row
< height
; row
++, y
++) {
432 GLchan rgba
[MAX_WIDTH
][4];
434 if (ctx
->Visual
.rgbMode
) {
435 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
438 GLuint index
[MAX_WIDTH
];
439 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
440 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
441 _mesa_map_ci(ctx
, readWidth
, index
);
443 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
445 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
446 format
, type
, 0, row
, 0);
447 if (ctx
->Visual
.redBits
< CHAN_BITS
||
448 ctx
->Visual
.greenBits
< CHAN_BITS
||
449 ctx
->Visual
.blueBits
< CHAN_BITS
) {
450 /* Requantize the color values into floating point and go from
451 * there. This fixes conformance failures with 16-bit color
452 * buffers, for example.
454 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
455 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
456 _mesa_chan_to_float_span(ctx
, readWidth
,
457 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
458 _mesa_pack_float_rgba_span(ctx
, readWidth
,
459 (CONST
GLfloat (*)[4]) rgbaf
,
460 format
, type
, dst
, packing
,
461 ctx
->_ImageTransferState
);
462 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
465 /* GLubytes are fine */
466 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
467 format
, type
, dst
, packing
,
468 ctx
->_ImageTransferState
);
473 (*swrast
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
479 _swrast_ReadPixels( GLcontext
*ctx
,
480 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
481 GLenum format
, GLenum type
,
482 const struct gl_pixelstore_attrib
*pack
,
485 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
488 if (swrast
->NewState
)
489 _swrast_validate_derived( ctx
);
491 RENDER_START(swrast
,ctx
);
495 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
497 case GL_STENCIL_INDEX
:
498 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
500 case GL_DEPTH_COMPONENT
:
501 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
509 case GL_LUMINANCE_ALPHA
:
514 read_rgba_pixels(ctx
, x
, y
, width
, height
,
515 format
, type
, pixels
, &ctx
->Pack
);
518 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
521 RENDER_FINISH(swrast
,ctx
);