1 /* $Id: s_readpix.c,v 1.9 2001/03/07 05:06: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
)
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
, (GLdepth
*) dest
, type
,
167 read_stencil_pixels( GLcontext
*ctx
,
169 GLsizei width
, GLsizei height
,
170 GLenum type
, GLvoid
*pixels
,
171 const struct gl_pixelstore_attrib
*packing
)
175 if (type
!= GL_BYTE
&&
176 type
!= GL_UNSIGNED_BYTE
&&
178 type
!= GL_UNSIGNED_SHORT
&&
180 type
!= GL_UNSIGNED_INT
&&
183 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
187 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
189 if (ctx
->Visual
.stencilBits
<= 0) {
190 /* No stencil buffer */
191 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
195 /* process image row by row */
196 for (j
=0;j
<height
;j
++,y
++) {
198 GLstencil stencil
[MAX_WIDTH
];
200 _mesa_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
202 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
203 GL_STENCIL_INDEX
, type
, 0, j
, 0);
205 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
212 * Optimized glReadPixels for particular pixel formats:
213 * GL_UNSIGNED_BYTE, GL_RGBA
214 * when pixel scaling, biasing and mapping are disabled.
217 read_fast_rgba_pixels( GLcontext
*ctx
,
219 GLsizei width
, GLsizei height
,
220 GLenum format
, GLenum type
,
222 const struct gl_pixelstore_attrib
*packing
)
224 /* can't do scale, bias, mapping, etc */
225 if (ctx
->_ImageTransferState
)
228 /* can't do fancy pixel packing */
229 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
235 GLint readWidth
= width
; /* actual width read */
236 GLint readHeight
= height
; /* actual height read */
237 GLint skipPixels
= packing
->SkipPixels
;
238 GLint skipRows
= packing
->SkipRows
;
241 if (packing
->RowLength
> 0)
242 rowLength
= packing
->RowLength
;
246 /* horizontal clipping */
247 if (srcX
< ctx
->ReadBuffer
->_Xmin
) {
248 skipPixels
+= (ctx
->ReadBuffer
->_Xmin
- srcX
);
249 readWidth
-= (ctx
->ReadBuffer
->_Xmin
- srcX
);
250 srcX
= ctx
->ReadBuffer
->_Xmin
;
252 if (srcX
+ readWidth
> ctx
->ReadBuffer
->_Xmax
)
253 readWidth
-= (srcX
+ readWidth
- ctx
->ReadBuffer
->_Xmax
);
257 /* vertical clipping */
258 if (srcY
< ctx
->ReadBuffer
->_Ymin
) {
259 skipRows
+= (ctx
->ReadBuffer
->_Ymin
- srcY
);
260 readHeight
-= (ctx
->ReadBuffer
->_Ymin
- srcY
);
261 srcY
= ctx
->ReadBuffer
->_Ymin
;
263 if (srcY
+ readHeight
> ctx
->ReadBuffer
->_Ymax
)
264 readHeight
-= (srcY
+ readHeight
- ctx
->ReadBuffer
->_Ymax
);
270 * The window region at (destX, destY) of size (readWidth, readHeight)
272 * We'll write pixel data to buffer pointed to by "pixels" but we'll
273 * skip "skipRows" rows and skip "skipPixels" pixels/row.
276 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
277 #elif CHAN_BITS == 16
278 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
282 GLchan
*dest
= (GLchan
*) pixels
283 + (skipRows
* rowLength
+ skipPixels
) * 4;
285 for (row
=0; row
<readHeight
; row
++) {
286 (*ctx
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
287 (GLchan (*)[4]) dest
);
288 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
289 _mesa_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
290 (GLchan (*)[4]) dest
);
292 dest
+= rowLength
* 4;
298 /* can't do this format/type combination */
307 * Read R, G, B, A, RGB, L, or LA pixels.
310 read_rgba_pixels( GLcontext
*ctx
,
312 GLsizei width
, GLsizei height
,
313 GLenum format
, GLenum type
, GLvoid
*pixels
,
314 const struct gl_pixelstore_attrib
*packing
)
318 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->ReadBuffer
, ctx
->Pixel
.DriverReadBuffer
);
320 /* Try optimized path first */
321 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
322 format
, type
, pixels
, packing
)) {
324 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
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 (*ctx
->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 (*ctx
->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 GLfloat rgbaf
[MAX_WIDTH
][4];
452 _mesa_chan_to_float_span(ctx
, readWidth
,
453 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
454 _mesa_pack_float_rgba_span(ctx
, readWidth
,
455 (CONST
GLfloat (*)[4]) rgbaf
,
456 format
, type
, dst
, packing
,
457 ctx
->_ImageTransferState
);
460 /* GLubytes are fine */
461 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
462 format
, type
, dst
, packing
,
463 ctx
->_ImageTransferState
);
468 (*ctx
->Driver
.SetReadBuffer
)(ctx
, ctx
->DrawBuffer
, ctx
->Color
.DriverDrawBuffer
);
474 _swrast_ReadPixels( GLcontext
*ctx
,
475 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
476 GLenum format
, GLenum type
,
477 const struct gl_pixelstore_attrib
*pack
,
482 if (SWRAST_CONTEXT(ctx
)->NewState
)
483 _swrast_validate_derived( ctx
);
487 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
489 case GL_STENCIL_INDEX
:
490 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
492 case GL_DEPTH_COMPONENT
:
493 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
501 case GL_LUMINANCE_ALPHA
:
506 read_rgba_pixels(ctx
, x
, y
, width
, height
,
507 format
, type
, pixels
, &ctx
->Pack
);
510 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );