1 /* $Id: s_copypix.c,v 1.34 2002/03/14 22:09:25 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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 */
69 else if (srcy
> dsty
+ height
) {
77 /* add one pixel of slop when zooming, just to be safe */
78 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
81 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
84 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
96 * RGBA copypixels with convolution.
99 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
100 GLint width
, GLint height
, GLint destx
, GLint desty
)
102 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
103 GLboolean quick_draw
;
105 GLboolean changeBuffer
;
106 GLchan
*saveReadAlpha
;
107 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
108 const GLuint transferOps
= ctx
->_ImageTransferState
;
109 GLfloat
*dest
, *tmpImage
, *convImage
;
113 span
.arrayMask
|= SPAN_RGBA
;
116 _mesa_span_default_z(ctx
, &span
);
117 if (ctx
->Fog
.Enabled
)
118 _mesa_span_default_fog(ctx
, &span
);
121 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
124 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
125 quick_draw
= GL_TRUE
;
128 quick_draw
= GL_FALSE
;
131 /* If read and draw buffer are different we must do buffer switching */
132 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
133 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
134 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
137 /* allocate space for GLfloat image */
138 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
140 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
143 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
146 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
153 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
154 ctx
->Pixel
.DriverReadBuffer
);
155 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
156 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
157 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
158 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
159 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
160 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
162 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
165 /* read source image */
167 for (row
= 0; row
< height
; row
++) {
168 GLchan rgba
[MAX_WIDTH
][4];
170 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
171 /* convert GLchan to GLfloat */
172 for (i
= 0; i
< width
; i
++) {
173 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
174 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
175 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
176 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
180 /* read from the draw buffer again (in case of blending) */
182 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
183 ctx
->Color
.DriverDrawBuffer
);
184 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
187 /* do image transfer ops up until convolution */
188 for (row
= 0; row
< height
; row
++) {
189 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
192 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
193 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
194 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
195 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
196 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
197 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
199 /* color map lookup */
200 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
201 _mesa_map_rgba(ctx
, width
, rgba
);
203 /* GL_COLOR_TABLE lookup */
204 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
205 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
210 if (ctx
->Pixel
.Convolution2DEnabled
) {
211 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
214 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
215 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
219 /* do remaining image transfer ops */
220 for (row
= 0; row
< height
; row
++) {
221 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
223 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
224 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
225 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
228 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
229 _mesa_transform_rgba(ctx
, width
, rgba
);
231 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
232 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
233 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
235 /* update histogram count */
236 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
237 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
240 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
241 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
245 for (row
= 0; row
< height
; row
++) {
246 const GLfloat
*src
= convImage
+ row
* width
* 4;
249 /* clamp to [0,1] and convert float back to chan */
250 for (i
= 0; i
< width
; i
++) {
251 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
252 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
253 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
254 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
255 span
.color
.rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
256 span
.color
.rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
257 span
.color
.rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
258 span
.color
.rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
261 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._ReallyEnabled
) {
263 _swrast_pixel_texture(ctx
, &span
);
266 /* write row to framebuffer */
269 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
270 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
271 (const GLchan (*)[4])span
.color
.rgba
, NULL
);
277 _mesa_write_zoomed_rgba_span(ctx
, &span
,
278 (CONST
GLchan (*)[4])span
.color
.rgba
,
285 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
297 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
298 GLint width
, GLint height
, GLint destx
, GLint desty
)
300 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
302 GLboolean quick_draw
;
303 GLint sy
, dy
, stepy
, j
;
304 GLboolean changeBuffer
;
305 GLchan
*saveReadAlpha
;
306 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
308 const GLuint transferOps
= ctx
->_ImageTransferState
;
312 span
.arrayMask
|= SPAN_RGBA
;
314 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
315 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
319 /* Determine if copy should be done bottom-to-top or top-to-bottom */
321 /* top-down max-to-min */
322 sy
= srcy
+ height
- 1;
323 dy
= desty
+ height
- 1;
327 /* bottom-up min-to-max */
333 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
334 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
337 _mesa_span_default_z(ctx
, &span
);
338 if (ctx
->Fog
.Enabled
)
339 _mesa_span_default_fog(ctx
, &span
);
341 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
344 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
345 quick_draw
= GL_TRUE
;
348 quick_draw
= GL_FALSE
;
351 /* If read and draw buffer are different we must do buffer switching */
352 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
353 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
354 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
356 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
357 ctx
->Pixel
.DriverReadBuffer
);
361 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
363 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
368 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
369 ctx
->Pixel
.DriverReadBuffer
);
370 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
371 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
372 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
373 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
374 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
375 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
377 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
379 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
380 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
387 tmpImage
= NULL
; /* silence compiler warnings */
391 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
392 /* Get source pixels */
394 /* get from buffered image */
395 MEMCPY(span
.color
.rgba
, p
, width
* sizeof(GLchan
) * 4);
399 /* get from framebuffer */
401 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
402 ctx
->Pixel
.DriverReadBuffer
);
403 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
404 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
406 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
407 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
409 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
410 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
413 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
416 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, span
.color
.rgba
);
420 /* read from the draw buffer again (in case of blending) */
421 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
422 ctx
->Color
.DriverDrawBuffer
);
423 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
427 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
429 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
430 CHECKARRAY(rgbaFloat
, return);
432 /* convert chan to float */
433 for (k
= 0; k
< width
; k
++) {
434 rgbaFloat
[k
][RCOMP
] = (GLfloat
) span
.color
.rgba
[k
][RCOMP
] * scale
;
435 rgbaFloat
[k
][GCOMP
] = (GLfloat
) span
.color
.rgba
[k
][GCOMP
] * scale
;
436 rgbaFloat
[k
][BCOMP
] = (GLfloat
) span
.color
.rgba
[k
][BCOMP
] * scale
;
437 rgbaFloat
[k
][ACOMP
] = (GLfloat
) span
.color
.rgba
[k
][ACOMP
] * scale
;
440 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
441 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
442 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
443 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
444 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
445 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
447 /* color map lookup */
448 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
449 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
451 /* GL_COLOR_TABLE lookup */
452 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
453 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
456 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
457 abort(); /* should never get here; caught at top of function */
459 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
460 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
461 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
462 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
463 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
464 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
465 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
466 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
467 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
468 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
469 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
471 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
472 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
473 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
476 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
477 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
479 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
480 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
481 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
483 /* update histogram count */
484 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
485 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
488 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
489 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
491 /* clamp to [0,1] and convert float back to chan */
492 for (k
= 0; k
< width
; k
++) {
493 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
494 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
495 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
496 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
497 span
.color
.rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
498 span
.color
.rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
499 span
.color
.rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
500 span
.color
.rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
502 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
505 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._ReallyEnabled
) {
507 _swrast_pixel_texture(ctx
, &span
);
510 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
511 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
512 (const GLchan (*)[4])span
.color
.rgba
, NULL
);
518 _mesa_write_zoomed_rgba_span(ctx
, &span
,
519 (CONST
GLchan (*)[4]) span
.color
.rgba
,
526 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
530 /* Restore pixel source to be the draw buffer (for blending, etc) */
531 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
532 ctx
->Color
.DriverDrawBuffer
);
539 static void copy_ci_pixels( GLcontext
*ctx
,
540 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
541 GLint destx
, GLint desty
)
543 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
547 GLboolean changeBuffer
;
548 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
549 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
554 span
.arrayMask
|= SPAN_INDEX
;
556 /* Determine if copy should be bottom-to-top or top-to-bottom */
558 /* top-down max-to-min */
559 sy
= srcy
+ height
- 1;
560 dy
= desty
+ height
- 1;
564 /* bottom-up min-to-max */
570 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
571 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
574 _mesa_span_default_z(ctx
, &span
);
575 if (ctx
->Fog
.Enabled
)
576 _mesa_span_default_fog(ctx
, &span
);
578 /* If read and draw buffer are different we must do buffer switching */
579 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
580 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
582 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
583 ctx
->Pixel
.DriverReadBuffer
);
587 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
589 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
594 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
595 ctx
->Pixel
.DriverReadBuffer
);
597 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
598 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
604 tmpImage
= NULL
; /* silence compiler warning */
608 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
610 MEMCPY(span
.color
.index
, p
, width
* sizeof(GLuint
));
615 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
616 ctx
->Pixel
.DriverReadBuffer
);
618 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
623 /* set read buffer back to draw buffer (in case of logicops) */
624 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
625 ctx
->Color
.DriverDrawBuffer
);
628 if (shift_or_offset
) {
629 _mesa_shift_and_offset_ci( ctx
, width
, span
.color
.index
);
631 if (ctx
->Pixel
.MapColorFlag
) {
632 _mesa_map_ci( ctx
, width
, span
.color
.index
);
639 _mesa_write_zoomed_index_span(ctx
, &span
, desty
);
641 _mesa_write_index_span(ctx
, &span
, GL_BITMAP
);
644 /* Restore pixel source to be the draw buffer (for blending, etc) */
645 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
646 ctx
->Color
.DriverDrawBuffer
);
657 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
658 GLint width
, GLint height
,
659 GLint destx
, GLint desty
)
661 GLfloat depth
[MAX_WIDTH
];
662 GLfloat
*p
, *tmpImage
;
665 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
670 span
.arrayMask
|= SPAN_Z
;
672 if (!ctx
->Visual
.depthBits
) {
673 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
677 /* Determine if copy should be bottom-to-top or top-to-bottom */
679 /* top-down max-to-min */
680 sy
= srcy
+ height
- 1;
681 dy
= desty
+ height
- 1;
685 /* bottom-up min-to-max */
691 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
692 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
694 _mesa_span_default_color(ctx
, &span
);
695 if (ctx
->Fog
.Enabled
)
696 _mesa_span_default_fog(ctx
, &span
);
700 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
702 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
706 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
707 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
713 tmpImage
= NULL
; /* silence compiler warning */
717 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
719 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
723 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
726 for (i
= 0; i
< width
; i
++) {
727 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
728 span
.zArray
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
734 if (ctx
->Visual
.rgbMode
) {
736 _mesa_write_zoomed_rgba_span( ctx
, &span
,
737 (const GLchan (*)[4])span
.color
.rgba
,
740 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
744 _mesa_write_zoomed_index_span( ctx
, &span
, desty
);
746 _mesa_write_index_span(ctx
, &span
, GL_BITMAP
);
756 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
757 GLint width
, GLint height
,
758 GLint destx
, GLint desty
)
762 GLstencil
*p
, *tmpImage
;
763 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
764 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
767 if (!ctx
->Visual
.stencilBits
) {
768 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
772 /* Determine if copy should be bottom-to-top or top-to-bottom */
774 /* top-down max-to-min */
775 sy
= srcy
+ height
- 1;
776 dy
= desty
+ height
- 1;
780 /* bottom-up min-to-max */
786 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
787 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
791 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
793 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
797 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
798 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
804 tmpImage
= NULL
; /* silence compiler warning */
808 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
809 GLstencil stencil
[MAX_WIDTH
];
812 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
816 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
819 if (shift_or_offset
) {
820 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
822 if (ctx
->Pixel
.MapStencilFlag
) {
823 _mesa_map_stencil( ctx
, width
, stencil
);
827 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
830 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
842 _swrast_CopyPixels( GLcontext
*ctx
,
843 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
844 GLint destx
, GLint desty
,
847 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
848 RENDER_START(swrast
,ctx
);
850 if (swrast
->NewState
)
851 _swrast_validate_derived( ctx
);
853 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
854 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
856 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
857 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
859 else if (type
== GL_DEPTH
) {
860 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
862 else if (type
== GL_STENCIL
) {
863 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
866 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
869 RENDER_FINISH(swrast
,ctx
);