1 /* $Id: s_readpix.c,v 1.8 2001/03/03 20:33:30 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
)
59 if (ctx
->Visual
.rgbMode
) {
60 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
64 ASSERT(ctx
->Driver
.SetReadBuffer
);
65 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->ReadBuffer
,
66 ctx
->Pixel
.DriverReadBuffer
);
68 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
70 /* process image row by row */
71 for (i
= 0; i
< height
; i
++) {
72 GLuint index
[MAX_WIDTH
];
75 (*ctx
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
+ i
, index
);
77 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
78 GL_COLOR_INDEX
, type
, 0, i
, 0);
80 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
81 &ctx
->Pack
, ctx
->_ImageTransferState
);
84 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
,
85 ctx
->Color
.DriverDrawBuffer
);
91 read_depth_pixels( GLcontext
*ctx
,
93 GLsizei width
, GLsizei height
,
94 GLenum type
, GLvoid
*pixels
,
95 const struct gl_pixelstore_attrib
*packing
)
98 GLboolean bias_or_scale
;
101 if (ctx
->Visual
.depthBits
<= 0) {
102 /* No depth buffer */
103 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
107 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
109 if (type
!= GL_BYTE
&&
110 type
!= GL_UNSIGNED_BYTE
&&
112 type
!= GL_UNSIGNED_SHORT
&&
114 type
!= GL_UNSIGNED_INT
&&
116 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
120 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
122 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
123 && !bias_or_scale
&& !packing
->SwapBytes
) {
124 /* Special case: directly read 16-bit unsigned depth values. */
126 for (j
=0;j
<height
;j
++,y
++) {
127 GLdepth depth
[MAX_WIDTH
];
128 GLushort
*dst
= (GLushort
*) _mesa_image_address( packing
, pixels
,
129 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
131 _mesa_read_depth_span(ctx
, width
, x
, y
, depth
);
132 for (i
= 0; i
< width
; i
++)
136 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
137 && !bias_or_scale
&& !packing
->SwapBytes
) {
138 /* Special case: directly read 32-bit unsigned depth values. */
140 for (j
=0;j
<height
;j
++,y
++) {
141 GLdepth
*dst
= (GLdepth
*) _mesa_image_address( packing
, pixels
,
142 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
143 _mesa_read_depth_span(ctx
, width
, x
, y
, dst
);
147 /* General case (slower) */
149 for (j
=0;j
<height
;j
++,y
++) {
150 GLfloat depth
[MAX_WIDTH
];
153 _mesa_read_depth_span_float(ctx
, readWidth
, x
, y
, depth
);
155 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
156 GL_DEPTH_COMPONENT
, type
, 0, j
, 0);
158 _mesa_pack_depth_span(ctx
, readWidth
, dest
, type
, depth
, &ctx
->Pack
);
166 read_stencil_pixels( GLcontext
*ctx
,
168 GLsizei width
, GLsizei height
,
169 GLenum type
, GLvoid
*pixels
,
170 const struct gl_pixelstore_attrib
*packing
)
174 if (type
!= GL_BYTE
&&
175 type
!= GL_UNSIGNED_BYTE
&&
177 type
!= GL_UNSIGNED_SHORT
&&
179 type
!= GL_UNSIGNED_INT
&&
182 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
186 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
188 if (ctx
->Visual
.stencilBits
<= 0) {
189 /* No stencil buffer */
190 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
194 /* process image row by row */
195 for (j
=0;j
<height
;j
++,y
++) {
197 GLstencil stencil
[MAX_WIDTH
];
199 _mesa_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
201 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
202 GL_STENCIL_INDEX
, type
, 0, j
, 0);
204 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
211 * Optimized glReadPixels for particular pixel formats:
212 * GL_UNSIGNED_BYTE, GL_RGBA
213 * when pixel scaling, biasing and mapping are disabled.
216 read_fast_rgba_pixels( GLcontext
*ctx
,
218 GLsizei width
, GLsizei height
,
219 GLenum format
, GLenum type
,
221 const struct gl_pixelstore_attrib
*packing
)
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 */
246 if (srcX
< ctx
->ReadBuffer
->_Xmin
) {
247 skipPixels
+= (ctx
->ReadBuffer
->_Xmin
- srcX
);
248 readWidth
-= (ctx
->ReadBuffer
->_Xmin
- srcX
);
249 srcX
= ctx
->ReadBuffer
->_Xmin
;
251 if (srcX
+ readWidth
> ctx
->ReadBuffer
->_Xmax
)
252 readWidth
-= (srcX
+ readWidth
- ctx
->ReadBuffer
->_Xmax
);
256 /* vertical clipping */
257 if (srcY
< ctx
->ReadBuffer
->_Ymin
) {
258 skipRows
+= (ctx
->ReadBuffer
->_Ymin
- srcY
);
259 readHeight
-= (ctx
->ReadBuffer
->_Ymin
- srcY
);
260 srcY
= ctx
->ReadBuffer
->_Ymin
;
262 if (srcY
+ readHeight
> ctx
->ReadBuffer
->_Ymax
)
263 readHeight
-= (srcY
+ readHeight
- ctx
->ReadBuffer
->_Ymax
);
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 (*ctx
->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
)
317 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->ReadBuffer
, ctx
->Pixel
.DriverReadBuffer
);
319 /* Try optimized path first */
320 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
321 format
, type
, pixels
, packing
)) {
323 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
327 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
329 /* do error checking on pixel type, format was already checked by caller */
331 case GL_UNSIGNED_BYTE
:
333 case GL_UNSIGNED_SHORT
:
335 case GL_UNSIGNED_INT
:
338 case GL_UNSIGNED_BYTE_3_3_2
:
339 case GL_UNSIGNED_BYTE_2_3_3_REV
:
340 case GL_UNSIGNED_SHORT_5_6_5
:
341 case GL_UNSIGNED_SHORT_5_6_5_REV
:
342 case GL_UNSIGNED_SHORT_4_4_4_4
:
343 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
344 case GL_UNSIGNED_SHORT_5_5_5_1
:
345 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
346 case GL_UNSIGNED_INT_8_8_8_8
:
347 case GL_UNSIGNED_INT_8_8_8_8_REV
:
348 case GL_UNSIGNED_INT_10_10_10_2
:
349 case GL_UNSIGNED_INT_2_10_10_10_REV
:
350 /* valid pixel type */
353 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
357 if (!_mesa_is_legal_format_and_type(format
, type
) ||
358 format
== GL_INTENSITY
) {
359 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
363 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
364 const GLuint transferOps
= ctx
->_ImageTransferState
;
365 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
368 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
373 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
376 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
380 /* read full RGBA, FLOAT image */
382 for (row
= 0; row
< height
; row
++, y
++) {
383 GLchan rgba
[MAX_WIDTH
][4];
384 if (ctx
->Visual
.rgbMode
) {
385 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
388 GLuint index
[MAX_WIDTH
];
389 (*ctx
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
390 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
391 _mesa_map_ci(ctx
, readWidth
, index
);
393 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
395 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
396 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
397 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
402 if (ctx
->Pixel
.Convolution2DEnabled
) {
403 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
406 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
407 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
411 /* finish transfer ops and pack the resulting image */
413 for (row
= 0; row
< height
; row
++) {
415 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
416 format
, type
, 0, row
, 0);
417 _mesa_pack_float_rgba_span(ctx
, readWidth
,
418 (const GLfloat (*)[4]) src
,
419 format
, type
, dest
, packing
,
420 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
421 src
+= readWidth
* 4;
427 for (row
= 0; row
< height
; row
++, y
++) {
428 GLchan rgba
[MAX_WIDTH
][4];
430 if (ctx
->Visual
.rgbMode
) {
431 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
434 GLuint index
[MAX_WIDTH
];
435 (*ctx
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
436 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
437 _mesa_map_ci(ctx
, readWidth
, index
);
439 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
441 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
442 format
, type
, 0, row
, 0);
443 if (ctx
->Visual
.redBits
< CHAN_BITS
||
444 ctx
->Visual
.greenBits
< CHAN_BITS
||
445 ctx
->Visual
.blueBits
< CHAN_BITS
) {
446 /* Requantize the color values into floating point and go from
447 * there. This fixes conformance failures with 16-bit color
448 * buffers, for example.
450 GLfloat rgbaf
[MAX_WIDTH
][4];
451 _mesa_chan_to_float_span(ctx
, readWidth
,
452 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
453 _mesa_pack_float_rgba_span(ctx
, readWidth
,
454 (CONST
GLfloat (*)[4]) rgbaf
,
455 format
, type
, dst
, packing
,
456 ctx
->_ImageTransferState
);
459 /* GLubytes are fine */
460 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
461 format
, type
, dst
, packing
,
462 ctx
->_ImageTransferState
);
467 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
473 _swrast_ReadPixels( GLcontext
*ctx
,
474 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
475 GLenum format
, GLenum type
,
476 const struct gl_pixelstore_attrib
*pack
,
481 if (SWRAST_CONTEXT(ctx
)->NewState
)
482 _swrast_validate_derived( ctx
);
486 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
488 case GL_STENCIL_INDEX
:
489 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
491 case GL_DEPTH_COMPONENT
:
492 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
500 case GL_LUMINANCE_ALPHA
:
505 read_rgba_pixels(ctx
, x
, y
, width
, height
,
506 format
, type
, pixels
, &ctx
->Pack
);
509 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );