2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #include "s_alphabuf.h"
37 #include "s_context.h"
40 #include "s_stencil.h"
45 * Read a block of color index pixels.
48 read_index_pixels( GLcontext
*ctx
,
50 GLsizei width
, GLsizei height
,
51 GLenum type
, GLvoid
*pixels
,
52 const struct gl_pixelstore_attrib
*packing
)
54 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
58 if (ctx
->Visual
.rgbMode
) {
59 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
63 if (type
!= GL_BYTE
&&
64 type
!= GL_UNSIGNED_BYTE
&&
66 type
!= GL_UNSIGNED_SHORT
&&
68 type
!= GL_UNSIGNED_INT
&&
70 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(index type)");
74 _swrast_use_read_buffer(ctx
);
76 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
78 /* process image row by row */
79 for (i
= 0; i
< height
; i
++) {
80 GLuint index
[MAX_WIDTH
];
83 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
+ i
, index
);
85 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
86 GL_COLOR_INDEX
, type
, 0, i
, 0);
88 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
89 &ctx
->Pack
, ctx
->_ImageTransferState
);
92 _swrast_use_draw_buffer(ctx
);
98 read_depth_pixels( GLcontext
*ctx
,
100 GLsizei width
, GLsizei height
,
101 GLenum type
, GLvoid
*pixels
,
102 const struct gl_pixelstore_attrib
*packing
)
105 GLboolean bias_or_scale
;
108 if (ctx
->Visual
.depthBits
<= 0) {
109 /* No depth buffer */
110 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
114 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
116 if (type
!= GL_BYTE
&&
117 type
!= GL_UNSIGNED_BYTE
&&
119 type
!= GL_UNSIGNED_SHORT
&&
121 type
!= GL_UNSIGNED_INT
&&
123 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
127 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
129 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
130 && !bias_or_scale
&& !packing
->SwapBytes
) {
131 /* Special case: directly read 16-bit unsigned depth values. */
133 for (j
=0;j
<height
;j
++,y
++) {
134 GLdepth depth
[MAX_WIDTH
];
135 GLushort
*dst
= (GLushort
*) _mesa_image_address( packing
, pixels
,
136 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
138 _swrast_read_depth_span(ctx
, width
, x
, y
, depth
);
139 for (i
= 0; i
< width
; i
++)
143 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
144 && !bias_or_scale
&& !packing
->SwapBytes
) {
145 /* Special case: directly read 32-bit unsigned depth values. */
147 for (j
=0;j
<height
;j
++,y
++) {
148 GLdepth
*dst
= (GLdepth
*) _mesa_image_address( packing
, pixels
,
149 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
150 _swrast_read_depth_span(ctx
, width
, x
, y
, dst
);
154 /* General case (slower) */
156 for (j
=0;j
<height
;j
++,y
++) {
157 GLfloat depth
[MAX_WIDTH
];
160 _swrast_read_depth_span_float(ctx
, readWidth
, x
, y
, depth
);
162 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
163 GL_DEPTH_COMPONENT
, type
, 0, j
, 0);
165 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
174 read_stencil_pixels( GLcontext
*ctx
,
176 GLsizei width
, GLsizei height
,
177 GLenum type
, GLvoid
*pixels
,
178 const struct gl_pixelstore_attrib
*packing
)
182 if (type
!= GL_BYTE
&&
183 type
!= GL_UNSIGNED_BYTE
&&
185 type
!= GL_UNSIGNED_SHORT
&&
187 type
!= GL_UNSIGNED_INT
&&
190 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
194 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
196 if (ctx
->Visual
.stencilBits
<= 0) {
197 /* No stencil buffer */
198 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
202 /* process image row by row */
203 for (j
=0;j
<height
;j
++,y
++) {
205 GLstencil stencil
[MAX_WIDTH
];
207 _swrast_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
209 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
210 GL_STENCIL_INDEX
, type
, 0, j
, 0);
212 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
219 * Optimized glReadPixels for particular pixel formats:
220 * GL_UNSIGNED_BYTE, GL_RGBA
221 * when pixel scaling, biasing and mapping are disabled.
224 read_fast_rgba_pixels( GLcontext
*ctx
,
226 GLsizei width
, GLsizei height
,
227 GLenum format
, GLenum type
,
229 const struct gl_pixelstore_attrib
*packing
)
231 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
232 /* can't do scale, bias, mapping, etc */
233 if (ctx
->_ImageTransferState
)
236 /* can't do fancy pixel packing */
237 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
243 GLint readWidth
= width
; /* actual width read */
244 GLint readHeight
= height
; /* actual height read */
245 GLint skipPixels
= packing
->SkipPixels
;
246 GLint skipRows
= packing
->SkipRows
;
249 if (packing
->RowLength
> 0)
250 rowLength
= packing
->RowLength
;
254 /* horizontal clipping */
260 if (srcX
+ readWidth
> (GLint
) ctx
->ReadBuffer
->Width
)
261 readWidth
-= (srcX
+ readWidth
- (GLint
) ctx
->ReadBuffer
->Width
);
265 /* vertical clipping */
271 if (srcY
+ readHeight
> (GLint
) ctx
->ReadBuffer
->Height
)
272 readHeight
-= (srcY
+ readHeight
- (GLint
) ctx
->ReadBuffer
->Height
);
278 * The window region at (destX, destY) of size (readWidth, readHeight)
280 * We'll write pixel data to buffer pointed to by "pixels" but we'll
281 * skip "skipRows" rows and skip "skipPixels" pixels/row.
284 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
285 #elif CHAN_BITS == 16
286 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
290 GLchan
*dest
= (GLchan
*) pixels
291 + (skipRows
* rowLength
+ skipPixels
) * 4;
294 if (packing
->Invert
) {
295 /* start at top and go down */
296 dest
+= (readHeight
- 1) * rowLength
* 4;
297 rowLength
= -rowLength
;
300 for (row
=0; row
<readHeight
; row
++) {
301 (*swrast
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
302 (GLchan (*)[4]) dest
);
303 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
304 _swrast_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
305 (GLchan (*)[4]) dest
);
307 dest
+= rowLength
* 4;
313 /* can't do this format/type combination */
322 * Read R, G, B, A, RGB, L, or LA pixels.
325 read_rgba_pixels( GLcontext
*ctx
,
327 GLsizei width
, GLsizei height
,
328 GLenum format
, GLenum type
, GLvoid
*pixels
,
329 const struct gl_pixelstore_attrib
*packing
)
331 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
334 _swrast_use_read_buffer(ctx
);
336 /* Try optimized path first */
337 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
338 format
, type
, pixels
, packing
)) {
340 _swrast_use_draw_buffer(ctx
);
344 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
346 /* do error checking on pixel type, format was already checked by caller */
348 case GL_UNSIGNED_BYTE
:
350 case GL_UNSIGNED_SHORT
:
352 case GL_UNSIGNED_INT
:
355 case GL_UNSIGNED_BYTE_3_3_2
:
356 case GL_UNSIGNED_BYTE_2_3_3_REV
:
357 case GL_UNSIGNED_SHORT_5_6_5
:
358 case GL_UNSIGNED_SHORT_5_6_5_REV
:
359 case GL_UNSIGNED_SHORT_4_4_4_4
:
360 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
361 case GL_UNSIGNED_SHORT_5_5_5_1
:
362 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
363 case GL_UNSIGNED_INT_8_8_8_8
:
364 case GL_UNSIGNED_INT_8_8_8_8_REV
:
365 case GL_UNSIGNED_INT_10_10_10_2
:
366 case GL_UNSIGNED_INT_2_10_10_10_REV
:
367 /* valid pixel type */
370 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
374 if (!_mesa_is_legal_format_and_type(format
, type
) ||
375 format
== GL_INTENSITY
) {
376 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
380 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
381 const GLuint transferOps
= ctx
->_ImageTransferState
;
382 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
385 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
387 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
390 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
393 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
397 /* read full RGBA, FLOAT image */
399 for (row
= 0; row
< height
; row
++, y
++) {
400 GLchan rgba
[MAX_WIDTH
][4];
401 if (ctx
->Visual
.rgbMode
) {
402 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
405 GLuint index
[MAX_WIDTH
];
406 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
407 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
408 _mesa_map_ci(ctx
, readWidth
, index
);
410 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
412 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
413 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
414 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
419 if (ctx
->Pixel
.Convolution2DEnabled
) {
420 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
423 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
424 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
428 /* finish transfer ops and pack the resulting image */
430 for (row
= 0; row
< height
; row
++) {
432 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
433 format
, type
, 0, row
, 0);
434 _mesa_pack_float_rgba_span(ctx
, readWidth
,
435 (const GLfloat (*)[4]) src
,
436 format
, type
, dest
, packing
,
437 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
438 src
+= readWidth
* 4;
444 for (row
= 0; row
< height
; row
++, y
++) {
445 GLchan rgba
[MAX_WIDTH
][4];
447 if (ctx
->Visual
.rgbMode
) {
448 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
451 GLuint index
[MAX_WIDTH
];
452 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
453 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
454 _mesa_map_ci(ctx
, readWidth
, index
);
456 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
458 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
459 format
, type
, 0, row
, 0);
460 if (ctx
->Visual
.redBits
< CHAN_BITS
||
461 ctx
->Visual
.greenBits
< CHAN_BITS
||
462 ctx
->Visual
.blueBits
< CHAN_BITS
) {
463 /* Requantize the color values into floating point and go from
464 * there. This fixes conformance failures with 16-bit color
465 * buffers, for example.
467 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
468 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
469 _mesa_chan_to_float_span(ctx
, readWidth
,
470 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
471 _mesa_pack_float_rgba_span(ctx
, readWidth
,
472 (CONST
GLfloat (*)[4]) rgbaf
,
473 format
, type
, dst
, packing
,
474 ctx
->_ImageTransferState
);
475 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
478 /* GLubytes are fine */
479 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
480 format
, type
, dst
, packing
,
481 ctx
->_ImageTransferState
);
486 _swrast_use_draw_buffer(ctx
);
492 _swrast_ReadPixels( GLcontext
*ctx
,
493 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
494 GLenum format
, GLenum type
,
495 const struct gl_pixelstore_attrib
*pack
,
498 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
501 if (swrast
->NewState
)
502 _swrast_validate_derived( ctx
);
504 RENDER_START(swrast
,ctx
);
508 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
510 case GL_STENCIL_INDEX
:
511 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
513 case GL_DEPTH_COMPONENT
:
514 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
522 case GL_LUMINANCE_ALPHA
:
527 read_rgba_pixels(ctx
, x
, y
, width
, height
,
528 format
, type
, pixels
, &ctx
->Pack
);
531 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
534 RENDER_FINISH(swrast
,ctx
);