1 /* $Id: s_copypix.c,v 1.16 2001/03/19 02:25:36 keithw 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
, GL_BITMAP
);
301 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
302 GLint width
, GLint height
, GLint destx
, GLint desty
)
304 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
305 GLdepth zspan
[MAX_WIDTH
];
306 GLchan rgba
[MAX_WIDTH
][4];
308 GLboolean quick_draw
;
311 GLboolean changeBuffer
;
312 GLchan
*saveReadAlpha
;
313 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
315 const GLuint transferOps
= ctx
->_ImageTransferState
;
317 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
318 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
322 /* Determine if copy should be done bottom-to-top or top-to-bottom */
324 /* top-down max-to-min */
325 sy
= srcy
+ height
- 1;
326 dy
= desty
+ height
- 1;
330 /* bottom-up min-to-max */
336 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
337 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
339 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
340 /* fill in array of z values */
341 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
342 for (i
=0;i
<width
;i
++) {
347 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
350 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
351 quick_draw
= GL_TRUE
;
354 quick_draw
= GL_FALSE
;
357 /* If read and draw buffer are different we must do buffer switching */
358 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
359 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
360 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
362 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
363 ctx
->Pixel
.DriverReadBuffer
);
367 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
369 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
374 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
375 ctx
->Pixel
.DriverReadBuffer
);
376 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
377 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
378 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
379 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
380 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
381 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
383 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
385 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
386 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
388 p
+= (width
* sizeof(GLchan
) * 4);
393 tmpImage
= NULL
; /* silence compiler warnings */
397 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
398 /* Get source pixels */
400 /* get from buffered image */
401 MEMCPY(rgba
, p
, width
* sizeof(GLchan
) * 4);
402 p
+= (width
* sizeof(GLchan
) * 4);
405 /* get from framebuffer */
407 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
408 ctx
->Pixel
.DriverReadBuffer
);
409 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
410 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
412 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
413 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
415 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
416 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
419 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
422 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, rgba
);
426 /* read from the draw buffer again (in case of blending) */
427 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
428 ctx
->Color
.DriverDrawBuffer
);
429 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
433 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
434 GLfloat rgbaFloat
[MAX_WIDTH
][4];
436 /* convert chan to float */
437 for (k
= 0; k
< width
; k
++) {
438 rgbaFloat
[k
][RCOMP
] = (GLfloat
) rgba
[k
][RCOMP
] * scale
;
439 rgbaFloat
[k
][GCOMP
] = (GLfloat
) rgba
[k
][GCOMP
] * scale
;
440 rgbaFloat
[k
][BCOMP
] = (GLfloat
) rgba
[k
][BCOMP
] * scale
;
441 rgbaFloat
[k
][ACOMP
] = (GLfloat
) rgba
[k
][ACOMP
] * scale
;
444 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
445 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
446 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
447 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
448 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
449 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
451 /* color map lookup */
452 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
453 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
455 /* GL_COLOR_TABLE lookup */
456 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
457 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
460 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
463 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
464 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
465 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
466 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
467 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
468 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
469 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
470 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
471 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
472 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
473 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
475 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
476 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
477 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
480 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
481 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
483 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
484 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
485 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
487 /* update histogram count */
488 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
489 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
492 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
493 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
495 /* clamp to [0,1] and convert float back to chan */
496 for (k
= 0; k
< width
; k
++) {
497 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
498 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
499 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
500 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
501 rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
502 rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
503 rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
504 rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
508 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
509 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
510 GLchan primary_rgba
[MAX_WIDTH
][4];
512 /* XXX not sure how multitexture is supposed to work here */
514 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
516 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
517 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
519 _swrast_texture_fragments(ctx
, unit
, width
, s
, t
, r
, NULL
,
520 (CONST
GLchan (*)[4]) primary_rgba
,
525 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
526 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
527 (const GLchan (*)[4])rgba
, NULL
);
530 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
531 (const GLchan (*)[4])rgba
, desty
);
534 _mesa_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
, GL_BITMAP
);
538 /* Restore pixel source to be the draw buffer (for blending, etc) */
539 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
540 ctx
->Color
.DriverDrawBuffer
);
547 static void copy_ci_pixels( GLcontext
*ctx
,
548 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
549 GLint destx
, GLint desty
)
551 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
552 GLdepth zspan
[MAX_WIDTH
];
556 GLboolean changeBuffer
;
557 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
558 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
561 /* Determine if copy should be bottom-to-top or top-to-bottom */
563 /* top-down max-to-min */
564 sy
= srcy
+ height
- 1;
565 dy
= desty
+ height
- 1;
569 /* bottom-up min-to-max */
575 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
576 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
578 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
579 /* fill in array of z values */
580 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
581 for (i
=0;i
<width
;i
++) {
586 /* If read and draw buffer are different we must do buffer switching */
587 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
588 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
590 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
591 ctx
->Pixel
.DriverReadBuffer
);
595 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
597 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
602 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
603 ctx
->Pixel
.DriverReadBuffer
);
605 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
606 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
612 tmpImage
= NULL
; /* silence compiler warning */
616 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
617 GLuint indexes
[MAX_WIDTH
];
619 MEMCPY(indexes
, p
, width
* sizeof(GLuint
));
624 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
625 ctx
->Pixel
.DriverReadBuffer
);
627 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, indexes
);
631 /* set read buffer back to draw buffer (in case of logicops) */
632 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
633 ctx
->Color
.DriverDrawBuffer
);
636 if (shift_or_offset
) {
637 _mesa_shift_and_offset_ci( ctx
, width
, indexes
);
639 if (ctx
->Pixel
.MapColorFlag
) {
640 _mesa_map_ci( ctx
, width
, indexes
);
644 _mesa_write_zoomed_index_span( ctx
, width
, destx
, dy
, zspan
, 0, indexes
, desty
);
647 _mesa_write_index_span(ctx
, width
, destx
, dy
, zspan
, 0, indexes
, GL_BITMAP
);
651 /* Restore pixel source to be the draw buffer (for blending, etc) */
652 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
653 ctx
->Color
.DriverDrawBuffer
);
664 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
665 GLint width
, GLint height
,
666 GLint destx
, GLint desty
)
668 GLfloat depth
[MAX_WIDTH
];
669 GLdepth zspan
[MAX_WIDTH
];
670 GLfloat
*p
, *tmpImage
;
671 GLuint indexes
[MAX_WIDTH
];
672 GLchan rgba
[MAX_WIDTH
][4];
675 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
678 if (!ctx
->Visual
.depthBits
) {
679 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
683 /* Determine if copy should be bottom-to-top or top-to-bottom */
685 /* top-down max-to-min */
686 sy
= srcy
+ height
- 1;
687 dy
= desty
+ height
- 1;
691 /* bottom-up min-to-max */
697 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
698 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
700 /* setup colors or indexes */
701 if (ctx
->Visual
.rgbMode
) {
702 GLuint
*rgba32
= (GLuint
*) rgba
;
703 GLuint color
= *(GLuint
*)( ctx
->Current
.Color
);
704 for (i
= 0; i
< width
; i
++) {
709 for (i
= 0; i
< width
; i
++) {
710 indexes
[i
] = ctx
->Current
.Index
;
716 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
718 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
722 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
723 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
729 tmpImage
= NULL
; /* silence compiler warning */
733 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
735 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
739 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
742 for (i
= 0; i
< width
; i
++) {
743 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
744 zspan
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
747 if (ctx
->Visual
.rgbMode
) {
749 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
750 (const GLchan (*)[4])rgba
, desty
);
753 _mesa_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
759 _mesa_write_zoomed_index_span( ctx
, width
, destx
, dy
,
760 zspan
, 0, indexes
, desty
);
763 _mesa_write_index_span( ctx
, width
, destx
, dy
,
764 zspan
, 0, indexes
, GL_BITMAP
);
775 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
776 GLint width
, GLint height
,
777 GLint destx
, GLint desty
)
781 GLstencil
*p
, *tmpImage
;
782 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
783 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
786 if (!ctx
->Visual
.stencilBits
) {
787 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
791 /* Determine if copy should be bottom-to-top or top-to-bottom */
793 /* top-down max-to-min */
794 sy
= srcy
+ height
- 1;
795 dy
= desty
+ height
- 1;
799 /* bottom-up min-to-max */
805 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
806 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
810 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
812 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
816 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
817 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
823 tmpImage
= NULL
; /* silence compiler warning */
827 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
828 GLstencil stencil
[MAX_WIDTH
];
831 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
835 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
838 if (shift_or_offset
) {
839 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
841 if (ctx
->Pixel
.MapStencilFlag
) {
842 _mesa_map_stencil( ctx
, width
, stencil
);
846 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
849 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
861 _swrast_CopyPixels( GLcontext
*ctx
,
862 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
863 GLint destx
, GLint desty
,
866 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
867 RENDER_START(swrast
,ctx
);
869 if (swrast
->NewState
)
870 _swrast_validate_derived( ctx
);
872 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
873 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
875 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
876 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
878 else if (type
== GL_DEPTH
) {
879 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
881 else if (type
== GL_STENCIL
) {
882 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
885 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
888 RENDER_FINISH(swrast
,ctx
);