3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include "s_alphabuf.h"
38 #include "s_context.h"
41 #include "s_stencil.h"
46 * Read a block of color index pixels.
49 read_index_pixels( GLcontext
*ctx
,
51 GLsizei width
, GLsizei height
,
52 GLenum type
, GLvoid
*pixels
,
53 const struct gl_pixelstore_attrib
*packing
)
55 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
59 if (ctx
->Visual
.rgbMode
) {
60 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
64 _swrast_use_read_buffer(ctx
);
66 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
68 /* process image row by row */
69 for (i
= 0; i
< height
; i
++) {
70 GLuint index
[MAX_WIDTH
];
73 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
+ i
, index
);
75 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
76 GL_COLOR_INDEX
, type
, 0, i
, 0);
78 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
79 &ctx
->Pack
, ctx
->_ImageTransferState
);
82 _swrast_use_draw_buffer(ctx
);
88 read_depth_pixels( GLcontext
*ctx
,
90 GLsizei width
, GLsizei height
,
91 GLenum type
, GLvoid
*pixels
,
92 const struct gl_pixelstore_attrib
*packing
)
95 GLboolean bias_or_scale
;
98 if (ctx
->Visual
.depthBits
<= 0) {
100 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
104 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
106 if (type
!= GL_BYTE
&&
107 type
!= GL_UNSIGNED_BYTE
&&
109 type
!= GL_UNSIGNED_SHORT
&&
111 type
!= GL_UNSIGNED_INT
&&
113 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
117 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
119 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
120 && !bias_or_scale
&& !packing
->SwapBytes
) {
121 /* Special case: directly read 16-bit unsigned depth values. */
123 for (j
=0;j
<height
;j
++,y
++) {
124 GLdepth depth
[MAX_WIDTH
];
125 GLushort
*dst
= (GLushort
*) _mesa_image_address( packing
, pixels
,
126 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
128 _swrast_read_depth_span(ctx
, width
, x
, y
, depth
);
129 for (i
= 0; i
< width
; i
++)
133 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
134 && !bias_or_scale
&& !packing
->SwapBytes
) {
135 /* Special case: directly read 32-bit unsigned depth values. */
137 for (j
=0;j
<height
;j
++,y
++) {
138 GLdepth
*dst
= (GLdepth
*) _mesa_image_address( packing
, pixels
,
139 width
, height
, GL_DEPTH_COMPONENT
, type
, 0, j
, 0 );
140 _swrast_read_depth_span(ctx
, width
, x
, y
, dst
);
144 /* General case (slower) */
146 for (j
=0;j
<height
;j
++,y
++) {
147 GLfloat depth
[MAX_WIDTH
];
150 _swrast_read_depth_span_float(ctx
, readWidth
, x
, y
, depth
);
152 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
153 GL_DEPTH_COMPONENT
, type
, 0, j
, 0);
155 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
164 read_stencil_pixels( GLcontext
*ctx
,
166 GLsizei width
, GLsizei height
,
167 GLenum type
, GLvoid
*pixels
,
168 const struct gl_pixelstore_attrib
*packing
)
172 if (type
!= GL_BYTE
&&
173 type
!= GL_UNSIGNED_BYTE
&&
175 type
!= GL_UNSIGNED_SHORT
&&
177 type
!= GL_UNSIGNED_INT
&&
180 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
184 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
186 if (ctx
->Visual
.stencilBits
<= 0) {
187 /* No stencil buffer */
188 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
192 /* process image row by row */
193 for (j
=0;j
<height
;j
++,y
++) {
195 GLstencil stencil
[MAX_WIDTH
];
197 _swrast_read_stencil_span(ctx
, readWidth
, x
, y
, stencil
);
199 dest
= _mesa_image_address(packing
, pixels
, width
, height
,
200 GL_STENCIL_INDEX
, type
, 0, j
, 0);
202 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, &ctx
->Pack
);
209 * Optimized glReadPixels for particular pixel formats:
210 * GL_UNSIGNED_BYTE, GL_RGBA
211 * when pixel scaling, biasing and mapping are disabled.
214 read_fast_rgba_pixels( GLcontext
*ctx
,
216 GLsizei width
, GLsizei height
,
217 GLenum format
, GLenum type
,
219 const struct gl_pixelstore_attrib
*packing
)
221 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
222 /* can't do scale, bias, mapping, etc */
223 if (ctx
->_ImageTransferState
)
226 /* can't do fancy pixel packing */
227 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
233 GLint readWidth
= width
; /* actual width read */
234 GLint readHeight
= height
; /* actual height read */
235 GLint skipPixels
= packing
->SkipPixels
;
236 GLint skipRows
= packing
->SkipRows
;
239 if (packing
->RowLength
> 0)
240 rowLength
= packing
->RowLength
;
244 /* horizontal clipping */
250 if (srcX
+ readWidth
> (GLint
) ctx
->ReadBuffer
->Width
)
251 readWidth
-= (srcX
+ readWidth
- (GLint
) ctx
->ReadBuffer
->Width
);
255 /* vertical clipping */
261 if (srcY
+ readHeight
> (GLint
) ctx
->ReadBuffer
->Height
)
262 readHeight
-= (srcY
+ readHeight
- (GLint
) ctx
->ReadBuffer
->Height
);
268 * The window region at (destX, destY) of size (readWidth, readHeight)
270 * We'll write pixel data to buffer pointed to by "pixels" but we'll
271 * skip "skipRows" rows and skip "skipPixels" pixels/row.
274 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
275 #elif CHAN_BITS == 16
276 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
280 GLchan
*dest
= (GLchan
*) pixels
281 + (skipRows
* rowLength
+ skipPixels
) * 4;
284 if (packing
->Invert
) {
285 /* start at top and go down */
286 dest
+= (readHeight
- 1) * rowLength
* 4;
287 rowLength
= -rowLength
;
290 for (row
=0; row
<readHeight
; row
++) {
291 (*swrast
->Driver
.ReadRGBASpan
)(ctx
, readWidth
, srcX
, srcY
,
292 (GLchan (*)[4]) dest
);
293 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
294 _swrast_read_alpha_span(ctx
, readWidth
, srcX
, srcY
,
295 (GLchan (*)[4]) dest
);
297 dest
+= rowLength
* 4;
303 /* can't do this format/type combination */
312 * Read R, G, B, A, RGB, L, or LA pixels.
315 read_rgba_pixels( GLcontext
*ctx
,
317 GLsizei width
, GLsizei height
,
318 GLenum format
, GLenum type
, GLvoid
*pixels
,
319 const struct gl_pixelstore_attrib
*packing
)
321 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
324 _swrast_use_read_buffer(ctx
);
326 /* Try optimized path first */
327 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
328 format
, type
, pixels
, packing
)) {
330 _swrast_use_draw_buffer(ctx
);
334 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
336 /* do error checking on pixel type, format was already checked by caller */
338 case GL_UNSIGNED_BYTE
:
340 case GL_UNSIGNED_SHORT
:
342 case GL_UNSIGNED_INT
:
345 case GL_UNSIGNED_BYTE_3_3_2
:
346 case GL_UNSIGNED_BYTE_2_3_3_REV
:
347 case GL_UNSIGNED_SHORT_5_6_5
:
348 case GL_UNSIGNED_SHORT_5_6_5_REV
:
349 case GL_UNSIGNED_SHORT_4_4_4_4
:
350 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
351 case GL_UNSIGNED_SHORT_5_5_5_1
:
352 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
353 case GL_UNSIGNED_INT_8_8_8_8
:
354 case GL_UNSIGNED_INT_8_8_8_8_REV
:
355 case GL_UNSIGNED_INT_10_10_10_2
:
356 case GL_UNSIGNED_INT_2_10_10_10_REV
:
357 /* valid pixel type */
360 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
364 if (!_mesa_is_legal_format_and_type(format
, type
) ||
365 format
== GL_INTENSITY
) {
366 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
370 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
371 const GLuint transferOps
= ctx
->_ImageTransferState
;
372 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
375 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
377 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
380 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
383 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
387 /* read full RGBA, FLOAT image */
389 for (row
= 0; row
< height
; row
++, y
++) {
390 GLchan rgba
[MAX_WIDTH
][4];
391 if (ctx
->Visual
.rgbMode
) {
392 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
395 GLuint index
[MAX_WIDTH
];
396 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
397 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
398 _mesa_map_ci(ctx
, readWidth
, index
);
400 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
402 _mesa_pack_rgba_span(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
403 GL_RGBA
, GL_FLOAT
, dest
, &_mesa_native_packing
,
404 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
409 if (ctx
->Pixel
.Convolution2DEnabled
) {
410 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
413 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
414 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
418 /* finish transfer ops and pack the resulting image */
420 for (row
= 0; row
< height
; row
++) {
422 dest
= _mesa_image_address(packing
, pixels
, readWidth
, height
,
423 format
, type
, 0, row
, 0);
424 _mesa_pack_float_rgba_span(ctx
, readWidth
,
425 (const GLfloat (*)[4]) src
,
426 format
, type
, dest
, packing
,
427 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
428 src
+= readWidth
* 4;
434 for (row
= 0; row
< height
; row
++, y
++) {
435 GLchan rgba
[MAX_WIDTH
][4];
437 if (ctx
->Visual
.rgbMode
) {
438 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
, readWidth
, x
, y
, rgba
);
441 GLuint index
[MAX_WIDTH
];
442 (*swrast
->Driver
.ReadCI32Span
)(ctx
, readWidth
, x
, y
, index
);
443 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
444 _mesa_map_ci(ctx
, readWidth
, index
);
446 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
448 dst
= _mesa_image_address(packing
, pixels
, width
, height
,
449 format
, type
, 0, row
, 0);
450 if (ctx
->Visual
.redBits
< CHAN_BITS
||
451 ctx
->Visual
.greenBits
< CHAN_BITS
||
452 ctx
->Visual
.blueBits
< CHAN_BITS
) {
453 /* Requantize the color values into floating point and go from
454 * there. This fixes conformance failures with 16-bit color
455 * buffers, for example.
457 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
458 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
459 _mesa_chan_to_float_span(ctx
, readWidth
,
460 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
461 _mesa_pack_float_rgba_span(ctx
, readWidth
,
462 (CONST
GLfloat (*)[4]) rgbaf
,
463 format
, type
, dst
, packing
,
464 ctx
->_ImageTransferState
);
465 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
468 /* GLubytes are fine */
469 _mesa_pack_rgba_span(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
470 format
, type
, dst
, packing
,
471 ctx
->_ImageTransferState
);
476 _swrast_use_draw_buffer(ctx
);
482 _swrast_ReadPixels( GLcontext
*ctx
,
483 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
484 GLenum format
, GLenum type
,
485 const struct gl_pixelstore_attrib
*pack
,
488 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
491 if (swrast
->NewState
)
492 _swrast_validate_derived( ctx
);
494 RENDER_START(swrast
,ctx
);
498 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
500 case GL_STENCIL_INDEX
:
501 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
, &ctx
->Pack
);
503 case GL_DEPTH_COMPONENT
:
504 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, &ctx
->Pack
);
512 case GL_LUMINANCE_ALPHA
:
517 read_rgba_pixels(ctx
, x
, y
, width
, height
,
518 format
, type
, pixels
, &ctx
->Pack
);
521 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
524 RENDER_FINISH(swrast
,ctx
);