1 /* $Id: s_copypix.c,v 1.38 2002/06/15 03:03:11 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
;
110 struct sw_span
*span
= swrast
->span
;
112 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
115 _mesa_span_default_z(ctx
, span
);
116 if (ctx
->Fog
.Enabled
)
117 _mesa_span_default_fog(ctx
, span
);
120 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
123 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
124 quick_draw
= GL_TRUE
;
127 quick_draw
= GL_FALSE
;
130 /* If read and draw buffer are different we must do buffer switching */
131 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
132 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
133 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
136 /* allocate space for GLfloat image */
137 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
139 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
142 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
145 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
152 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
153 ctx
->Pixel
.DriverReadBuffer
);
154 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
155 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
156 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
157 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
158 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
159 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
161 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
164 /* read source image */
166 for (row
= 0; row
< height
; row
++) {
167 GLchan rgba
[MAX_WIDTH
][4];
169 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
170 /* convert GLchan to GLfloat */
171 for (i
= 0; i
< width
; i
++) {
172 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
173 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
174 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
175 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
179 /* read from the draw buffer again (in case of blending) */
181 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
182 ctx
->Color
.DriverDrawBuffer
);
183 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
186 /* do image transfer ops up until convolution */
187 for (row
= 0; row
< height
; row
++) {
188 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
191 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
192 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
193 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
194 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
195 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
196 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
198 /* color map lookup */
199 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
200 _mesa_map_rgba(ctx
, width
, rgba
);
202 /* GL_COLOR_TABLE lookup */
203 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
204 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
209 if (ctx
->Pixel
.Convolution2DEnabled
) {
210 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
213 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
214 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
218 /* do remaining image transfer ops */
219 for (row
= 0; row
< height
; row
++) {
220 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
222 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
223 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
224 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
227 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
228 _mesa_transform_rgba(ctx
, width
, rgba
);
230 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
231 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
232 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
234 /* update histogram count */
235 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
236 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
239 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
240 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
244 for (row
= 0; row
< height
; row
++) {
245 const GLfloat
*src
= convImage
+ row
* 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 span
->color
.rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
255 span
->color
.rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
256 span
->color
.rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
257 span
->color
.rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
260 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
262 _swrast_pixel_texture(ctx
, span
);
265 /* write row to framebuffer */
268 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
269 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
270 (const GLchan (*)[4])span
->color
.rgba
, NULL
);
276 _mesa_write_zoomed_rgba_span(ctx
, span
,
277 (CONST
GLchan (*)[4])span
->color
.rgba
,
284 _mesa_write_rgba_span(ctx
, span
);
296 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
297 GLint width
, GLint height
, GLint destx
, GLint desty
)
299 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
301 GLboolean quick_draw
;
302 GLint sy
, dy
, stepy
, j
;
303 GLboolean changeBuffer
;
304 GLchan
*saveReadAlpha
;
305 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
307 const GLuint transferOps
= ctx
->_ImageTransferState
;
308 struct sw_span
*span
= swrast
->span
;
310 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
312 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
313 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
317 /* Determine if copy should be done bottom-to-top or top-to-bottom */
319 /* top-down max-to-min */
320 sy
= srcy
+ height
- 1;
321 dy
= desty
+ height
- 1;
325 /* bottom-up min-to-max */
331 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
332 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
335 _mesa_span_default_z(ctx
, span
);
336 if (ctx
->Fog
.Enabled
)
337 _mesa_span_default_fog(ctx
, span
);
339 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
342 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
343 quick_draw
= GL_TRUE
;
346 quick_draw
= GL_FALSE
;
349 /* If read and draw buffer are different we must do buffer switching */
350 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
351 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
352 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
354 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
355 ctx
->Pixel
.DriverReadBuffer
);
359 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
361 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
366 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
367 ctx
->Pixel
.DriverReadBuffer
);
368 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
369 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
370 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
371 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
372 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
373 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
375 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
377 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
378 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
385 tmpImage
= NULL
; /* silence compiler warnings */
389 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
390 /* Get source pixels */
392 /* get from buffered image */
393 MEMCPY(span
->color
.rgba
, p
, width
* sizeof(GLchan
) * 4);
397 /* get from framebuffer */
399 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
400 ctx
->Pixel
.DriverReadBuffer
);
401 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
402 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
404 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
405 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
407 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
408 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
411 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
414 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, span
->color
.rgba
);
418 /* read from the draw buffer again (in case of blending) */
419 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
420 ctx
->Color
.DriverDrawBuffer
);
421 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
425 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
427 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
428 CHECKARRAY(rgbaFloat
, return);
430 /* convert chan to float */
431 for (k
= 0; k
< width
; k
++) {
432 rgbaFloat
[k
][RCOMP
] = (GLfloat
) span
->color
.rgba
[k
][RCOMP
] * scale
;
433 rgbaFloat
[k
][GCOMP
] = (GLfloat
) span
->color
.rgba
[k
][GCOMP
] * scale
;
434 rgbaFloat
[k
][BCOMP
] = (GLfloat
) span
->color
.rgba
[k
][BCOMP
] * scale
;
435 rgbaFloat
[k
][ACOMP
] = (GLfloat
) span
->color
.rgba
[k
][ACOMP
] * scale
;
438 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
439 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
440 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
441 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
442 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
443 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
445 /* color map lookup */
446 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
447 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
449 /* GL_COLOR_TABLE lookup */
450 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
451 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
454 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
455 abort(); /* should never get here; caught at top of function */
457 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
458 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
459 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
460 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
461 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
462 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
463 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
464 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
465 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
466 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
467 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
469 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
470 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
471 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
474 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
475 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
477 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
478 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
479 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
481 /* update histogram count */
482 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
483 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
486 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
487 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
489 /* clamp to [0,1] and convert float back to chan */
490 for (k
= 0; k
< width
; k
++) {
491 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
492 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
493 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
494 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
495 span
->color
.rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
496 span
->color
.rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
497 span
->color
.rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
498 span
->color
.rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
500 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
503 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
505 _swrast_pixel_texture(ctx
, span
);
508 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
509 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
510 (const GLchan (*)[4])span
->color
.rgba
, NULL
);
516 _mesa_write_zoomed_rgba_span(ctx
, span
,
517 (CONST
GLchan (*)[4]) span
->color
.rgba
,
524 _mesa_write_rgba_span(ctx
, span
);
528 /* Restore pixel source to be the draw buffer (for blending, etc) */
529 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
530 ctx
->Color
.DriverDrawBuffer
);
537 static void copy_ci_pixels( GLcontext
*ctx
,
538 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
539 GLint destx
, GLint desty
)
541 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
545 GLboolean changeBuffer
;
546 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
547 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
549 struct sw_span
*span
= swrast
->span
;
551 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
553 /* Determine if copy should be bottom-to-top or top-to-bottom */
555 /* top-down max-to-min */
556 sy
= srcy
+ height
- 1;
557 dy
= desty
+ height
- 1;
561 /* bottom-up min-to-max */
567 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
568 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
571 _mesa_span_default_z(ctx
, span
);
572 if (ctx
->Fog
.Enabled
)
573 _mesa_span_default_fog(ctx
, span
);
575 /* If read and draw buffer are different we must do buffer switching */
576 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
577 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
579 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
580 ctx
->Pixel
.DriverReadBuffer
);
584 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
586 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
591 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
592 ctx
->Pixel
.DriverReadBuffer
);
594 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
595 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
601 tmpImage
= NULL
; /* silence compiler warning */
605 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
607 MEMCPY(span
->color
.index
, p
, width
* sizeof(GLuint
));
612 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
613 ctx
->Pixel
.DriverReadBuffer
);
615 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
620 /* set read buffer back to draw buffer (in case of logicops) */
621 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
622 ctx
->Color
.DriverDrawBuffer
);
625 if (shift_or_offset
) {
626 _mesa_shift_and_offset_ci( ctx
, width
, span
->color
.index
);
628 if (ctx
->Pixel
.MapColorFlag
) {
629 _mesa_map_ci( ctx
, width
, span
->color
.index
);
636 _mesa_write_zoomed_index_span(ctx
, span
, desty
);
638 _mesa_write_index_span(ctx
, span
);
641 /* Restore pixel source to be the draw buffer (for blending, etc) */
642 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
643 ctx
->Color
.DriverDrawBuffer
);
654 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
655 GLint width
, GLint height
,
656 GLint destx
, GLint desty
)
658 GLfloat depth
[MAX_WIDTH
];
659 GLfloat
*p
, *tmpImage
;
662 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
664 struct sw_span
*span
= SWRAST_CONTEXT(ctx
)->span
;
666 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
668 if (!ctx
->Visual
.depthBits
) {
669 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
673 /* Determine if copy should be bottom-to-top or top-to-bottom */
675 /* top-down max-to-min */
676 sy
= srcy
+ height
- 1;
677 dy
= desty
+ height
- 1;
681 /* bottom-up min-to-max */
687 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
688 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
690 _mesa_span_default_color(ctx
, span
);
691 if (ctx
->Fog
.Enabled
)
692 _mesa_span_default_fog(ctx
, span
);
696 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
698 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
702 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
703 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
709 tmpImage
= NULL
; /* silence compiler warning */
713 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
715 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
719 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
722 for (i
= 0; i
< width
; i
++) {
723 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
724 span
->zArray
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
730 if (ctx
->Visual
.rgbMode
) {
732 _mesa_write_zoomed_rgba_span( ctx
, span
,
733 (const GLchan (*)[4])span
->color
.rgba
,
736 _mesa_write_rgba_span(ctx
, span
);
740 _mesa_write_zoomed_index_span( ctx
, span
, desty
);
742 _mesa_write_index_span(ctx
, span
);
752 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
753 GLint width
, GLint height
,
754 GLint destx
, GLint desty
)
758 GLstencil
*p
, *tmpImage
;
759 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
760 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
763 if (!ctx
->Visual
.stencilBits
) {
764 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
768 /* Determine if copy should be bottom-to-top or top-to-bottom */
770 /* top-down max-to-min */
771 sy
= srcy
+ height
- 1;
772 dy
= desty
+ height
- 1;
776 /* bottom-up min-to-max */
782 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
783 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
787 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
789 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
793 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
794 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
800 tmpImage
= NULL
; /* silence compiler warning */
804 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
805 GLstencil stencil
[MAX_WIDTH
];
808 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
812 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
815 if (shift_or_offset
) {
816 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
818 if (ctx
->Pixel
.MapStencilFlag
) {
819 _mesa_map_stencil( ctx
, width
, stencil
);
823 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
826 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
838 _swrast_CopyPixels( GLcontext
*ctx
,
839 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
840 GLint destx
, GLint desty
,
843 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
844 RENDER_START(swrast
,ctx
);
846 if (swrast
->NewState
)
847 _swrast_validate_derived( ctx
);
849 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
850 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
852 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
853 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
855 else if (type
== GL_DEPTH
) {
856 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
858 else if (type
== GL_STENCIL
) {
859 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
862 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
865 RENDER_FINISH(swrast
,ctx
);