1 /* $Id: s_copypix.c,v 1.17 2001/05/15 21:30:27 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_context.h"
40 #include "s_histogram.h"
41 #include "s_pixeltex.h"
43 #include "s_stencil.h"
44 #include "s_texture.h"
50 * Determine if there's overlap in an image copy.
51 * This test also compensates for the fact that copies are done from
52 * bottom to top and overlaps can sometimes be handled correctly
53 * without making a temporary image copy.
56 regions_overlap(GLint srcx
, GLint srcy
,
57 GLint dstx
, GLint dsty
,
58 GLint width
, GLint height
,
59 GLfloat zoomX
, GLfloat zoomY
)
61 if (zoomX
== 1.0 && zoomY
== 1.0) {
63 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
66 else if (srcy
< dsty
) { /* this is OK */
74 /* add one pixel of slop when zooming, just to be safe */
75 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
78 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
81 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
93 * RGBA copypixels with convolution.
96 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
97 GLint width
, GLint height
, GLint destx
, GLint desty
)
99 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
100 GLdepth zspan
[MAX_WIDTH
];
101 GLboolean quick_draw
;
103 GLboolean changeBuffer
;
104 GLchan
*saveReadAlpha
;
105 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
106 const GLuint transferOps
= ctx
->_ImageTransferState
;
107 GLfloat
*dest
, *tmpImage
, *convImage
;
109 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
110 /* fill in array of z values */
111 GLdepth z
= (GLdepth
)
112 (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
114 for (i
= 0; i
< width
; i
++) {
119 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
122 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
123 quick_draw
= GL_TRUE
;
126 quick_draw
= GL_FALSE
;
129 /* If read and draw buffer are different we must do buffer switching */
130 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
131 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
132 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
135 /* allocate space for GLfloat image */
136 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
138 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
141 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
144 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
151 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
152 ctx
->Pixel
.DriverReadBuffer
);
153 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
154 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
155 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
156 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
157 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
158 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
160 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
163 /* read source image */
165 for (row
= 0; row
< height
; row
++) {
166 GLchan rgba
[MAX_WIDTH
][4];
168 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
169 /* convert GLchan to GLfloat */
170 for (i
= 0; i
< width
; i
++) {
171 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
172 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
173 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
174 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
178 /* read from the draw buffer again (in case of blending) */
180 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
181 ctx
->Color
.DriverDrawBuffer
);
182 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
185 /* do image transfer ops up until convolution */
186 for (row
= 0; row
< height
; row
++) {
187 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) tmpImage
+ row
* width
* 4;
190 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
191 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
192 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
193 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
194 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
195 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
197 /* color map lookup */
198 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
199 _mesa_map_rgba(ctx
, width
, rgba
);
201 /* GL_COLOR_TABLE lookup */
202 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
203 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
208 if (ctx
->Pixel
.Convolution2DEnabled
) {
209 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
212 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
213 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
217 /* do remaining image transfer ops */
218 for (row
= 0; row
< height
; row
++) {
219 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) convImage
+ row
* width
* 4;
221 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
222 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
223 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
226 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
227 _mesa_transform_rgba(ctx
, width
, rgba
);
229 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
230 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
231 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
233 /* update histogram count */
234 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
235 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
238 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
239 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
243 for (row
= 0; row
< height
; row
++) {
244 const GLfloat
*src
= convImage
+ row
* width
* 4;
245 GLchan rgba
[MAX_WIDTH
][4];
248 /* clamp to [0,1] and convert float back to chan */
249 for (i
= 0; i
< width
; i
++) {
250 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
251 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
252 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
253 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
254 rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
255 rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
256 rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
257 rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
260 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
261 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
262 GLchan primary_rgba
[MAX_WIDTH
][4];
264 /* XXX not sure how multitexture is supposed to work here */
266 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
268 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
269 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
271 _swrast_texture_fragments(ctx
, unit
, width
, s
, t
, r
, NULL
,
272 (CONST
GLchan (*)[4]) primary_rgba
,
277 /* write row to framebuffer */
280 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
281 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
282 (const GLchan (*)[4])rgba
, NULL
);
285 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
286 (const GLchan (*)[4])rgba
, desty
);
289 _mesa_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
,
302 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
303 GLint width
, GLint height
, GLint destx
, GLint desty
)
305 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
306 GLdepth zspan
[MAX_WIDTH
];
307 GLchan rgba
[MAX_WIDTH
][4];
309 GLboolean quick_draw
;
312 GLboolean changeBuffer
;
313 GLchan
*saveReadAlpha
;
314 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
316 const GLuint transferOps
= ctx
->_ImageTransferState
;
318 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
319 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
323 /* Determine if copy should be done bottom-to-top or top-to-bottom */
325 /* top-down max-to-min */
326 sy
= srcy
+ height
- 1;
327 dy
= desty
+ height
- 1;
331 /* bottom-up min-to-max */
337 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
338 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
340 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
341 /* fill in array of z values */
342 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
343 for (i
=0;i
<width
;i
++) {
348 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
351 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
352 quick_draw
= GL_TRUE
;
355 quick_draw
= GL_FALSE
;
358 /* If read and draw buffer are different we must do buffer switching */
359 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
360 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
361 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
363 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
364 ctx
->Pixel
.DriverReadBuffer
);
368 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
370 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
375 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
376 ctx
->Pixel
.DriverReadBuffer
);
377 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
378 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
379 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
380 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
381 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
382 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
384 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
386 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
387 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
389 p
+= (width
* sizeof(GLchan
) * 4);
394 tmpImage
= NULL
; /* silence compiler warnings */
398 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
399 /* Get source pixels */
401 /* get from buffered image */
402 MEMCPY(rgba
, p
, width
* sizeof(GLchan
) * 4);
403 p
+= (width
* sizeof(GLchan
) * 4);
406 /* get from framebuffer */
408 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
409 ctx
->Pixel
.DriverReadBuffer
);
410 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
411 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
413 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
414 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
416 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
417 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
420 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
423 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, rgba
);
427 /* read from the draw buffer again (in case of blending) */
428 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
429 ctx
->Color
.DriverDrawBuffer
);
430 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
434 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
435 GLfloat rgbaFloat
[MAX_WIDTH
][4];
437 /* convert chan to float */
438 for (k
= 0; k
< width
; k
++) {
439 rgbaFloat
[k
][RCOMP
] = (GLfloat
) rgba
[k
][RCOMP
] * scale
;
440 rgbaFloat
[k
][GCOMP
] = (GLfloat
) rgba
[k
][GCOMP
] * scale
;
441 rgbaFloat
[k
][BCOMP
] = (GLfloat
) rgba
[k
][BCOMP
] * scale
;
442 rgbaFloat
[k
][ACOMP
] = (GLfloat
) rgba
[k
][ACOMP
] * scale
;
445 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
446 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
447 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
448 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
449 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
450 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
452 /* color map lookup */
453 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
454 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
456 /* GL_COLOR_TABLE lookup */
457 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
458 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
461 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
464 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
465 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
466 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
467 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
468 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
469 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
470 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
471 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
472 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
473 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
474 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
476 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
477 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
478 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
481 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
482 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
484 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
485 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
486 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
488 /* update histogram count */
489 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
490 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
493 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
494 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
496 /* clamp to [0,1] and convert float back to chan */
497 for (k
= 0; k
< width
; k
++) {
498 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
499 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
500 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
501 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
502 rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
503 rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
504 rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
505 rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
509 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
510 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
511 GLchan primary_rgba
[MAX_WIDTH
][4];
513 /* XXX not sure how multitexture is supposed to work here */
515 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
517 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
518 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
520 _swrast_texture_fragments(ctx
, unit
, width
, s
, t
, r
, NULL
,
521 (CONST
GLchan (*)[4]) primary_rgba
,
526 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
527 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
528 (const GLchan (*)[4])rgba
, NULL
);
531 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
532 (const GLchan (*)[4])rgba
, desty
);
535 _mesa_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
,
540 /* Restore pixel source to be the draw buffer (for blending, etc) */
541 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
542 ctx
->Color
.DriverDrawBuffer
);
549 static void copy_ci_pixels( GLcontext
*ctx
,
550 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
551 GLint destx
, GLint desty
)
553 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
554 GLdepth zspan
[MAX_WIDTH
];
558 GLboolean changeBuffer
;
559 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
560 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
563 /* Determine if copy should be bottom-to-top or top-to-bottom */
565 /* top-down max-to-min */
566 sy
= srcy
+ height
- 1;
567 dy
= desty
+ height
- 1;
571 /* bottom-up min-to-max */
577 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
578 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
580 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
581 /* fill in array of z values */
582 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
583 for (i
=0;i
<width
;i
++) {
588 /* If read and draw buffer are different we must do buffer switching */
589 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
590 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
592 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
593 ctx
->Pixel
.DriverReadBuffer
);
597 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
599 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
604 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
605 ctx
->Pixel
.DriverReadBuffer
);
607 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
608 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
614 tmpImage
= NULL
; /* silence compiler warning */
618 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
619 GLuint indexes
[MAX_WIDTH
];
621 MEMCPY(indexes
, p
, width
* sizeof(GLuint
));
626 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
627 ctx
->Pixel
.DriverReadBuffer
);
629 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, indexes
);
633 /* set read buffer back to draw buffer (in case of logicops) */
634 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
635 ctx
->Color
.DriverDrawBuffer
);
638 if (shift_or_offset
) {
639 _mesa_shift_and_offset_ci( ctx
, width
, indexes
);
641 if (ctx
->Pixel
.MapColorFlag
) {
642 _mesa_map_ci( ctx
, width
, indexes
);
646 _mesa_write_zoomed_index_span(ctx
, width
, destx
, dy
, zspan
, 0,
650 _mesa_write_index_span(ctx
, width
, destx
, dy
, zspan
, 0, indexes
,
655 /* Restore pixel source to be the draw buffer (for blending, etc) */
656 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
657 ctx
->Color
.DriverDrawBuffer
);
668 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
669 GLint width
, GLint height
,
670 GLint destx
, GLint desty
)
672 GLfloat depth
[MAX_WIDTH
];
673 GLdepth zspan
[MAX_WIDTH
];
674 GLfloat
*p
, *tmpImage
;
675 GLuint indexes
[MAX_WIDTH
];
676 GLchan rgba
[MAX_WIDTH
][4];
679 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
682 if (!ctx
->Visual
.depthBits
) {
683 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
687 /* Determine if copy should be bottom-to-top or top-to-bottom */
689 /* top-down max-to-min */
690 sy
= srcy
+ height
- 1;
691 dy
= desty
+ height
- 1;
695 /* bottom-up min-to-max */
701 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
702 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
704 /* setup colors or indexes */
705 if (ctx
->Visual
.rgbMode
) {
706 GLuint
*rgba32
= (GLuint
*) rgba
;
707 GLuint color
= *(GLuint
*)( ctx
->Current
.Color
);
708 for (i
= 0; i
< width
; i
++) {
713 for (i
= 0; i
< width
; i
++) {
714 indexes
[i
] = ctx
->Current
.Index
;
720 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
722 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
726 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
727 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
733 tmpImage
= NULL
; /* silence compiler warning */
737 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
739 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
743 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
746 for (i
= 0; i
< width
; i
++) {
747 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
748 zspan
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
751 if (ctx
->Visual
.rgbMode
) {
753 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
754 (const GLchan (*)[4])rgba
, desty
);
757 _mesa_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
758 rgba
, NULL
, GL_BITMAP
);
763 _mesa_write_zoomed_index_span( ctx
, width
, destx
, dy
,
764 zspan
, 0, indexes
, desty
);
767 _mesa_write_index_span( ctx
, width
, destx
, dy
,
768 zspan
, 0, indexes
, NULL
, GL_BITMAP
);
779 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
780 GLint width
, GLint height
,
781 GLint destx
, GLint desty
)
785 GLstencil
*p
, *tmpImage
;
786 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
787 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
790 if (!ctx
->Visual
.stencilBits
) {
791 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
795 /* Determine if copy should be bottom-to-top or top-to-bottom */
797 /* top-down max-to-min */
798 sy
= srcy
+ height
- 1;
799 dy
= desty
+ height
- 1;
803 /* bottom-up min-to-max */
809 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
810 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
814 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
816 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
820 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
821 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
827 tmpImage
= NULL
; /* silence compiler warning */
831 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
832 GLstencil stencil
[MAX_WIDTH
];
835 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
839 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
842 if (shift_or_offset
) {
843 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
845 if (ctx
->Pixel
.MapStencilFlag
) {
846 _mesa_map_stencil( ctx
, width
, stencil
);
850 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
853 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
865 _swrast_CopyPixels( GLcontext
*ctx
,
866 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
867 GLint destx
, GLint desty
,
870 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
871 RENDER_START(swrast
,ctx
);
873 if (swrast
->NewState
)
874 _swrast_validate_derived( ctx
);
876 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
877 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
879 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
880 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
882 else if (type
== GL_DEPTH
) {
883 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
885 else if (type
== GL_STENCIL
) {
886 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
889 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
892 RENDER_FINISH(swrast
,ctx
);