1 /* $Id: s_copypix.c,v 1.32 2002/01/31 00:27:43 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"
41 #include "s_histogram.h"
42 #include "s_pixeltex.h"
44 #include "s_stencil.h"
45 #include "s_texture.h"
51 * Determine if there's overlap in an image copy.
52 * This test also compensates for the fact that copies are done from
53 * bottom to top and overlaps can sometimes be handled correctly
54 * without making a temporary image copy.
57 regions_overlap(GLint srcx
, GLint srcy
,
58 GLint dstx
, GLint dsty
,
59 GLint width
, GLint height
,
60 GLfloat zoomX
, GLfloat zoomY
)
62 if (zoomX
== 1.0 && zoomY
== 1.0) {
64 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
67 else if (srcy
< dsty
) { /* this is OK */
70 else if (srcy
> dsty
+ height
) {
78 /* add one pixel of slop when zooming, just to be safe */
79 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
82 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
85 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
97 * RGBA copypixels with convolution.
100 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
101 GLint width
, GLint height
, GLint destx
, GLint desty
)
103 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
104 GLboolean quick_draw
;
106 GLboolean changeBuffer
;
107 GLchan
*saveReadAlpha
;
108 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
109 const GLuint transferOps
= ctx
->_ImageTransferState
;
110 GLfloat
*dest
, *tmpImage
, *convImage
;
114 span
.arrayMask
|= SPAN_RGBA
;
117 _mesa_span_default_z(ctx
, &span
);
118 if (ctx
->Fog
.Enabled
)
119 _mesa_span_default_fog(ctx
, &span
);
122 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
125 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
126 quick_draw
= GL_TRUE
;
129 quick_draw
= GL_FALSE
;
132 /* If read and draw buffer are different we must do buffer switching */
133 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
134 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
135 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
138 /* allocate space for GLfloat image */
139 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
141 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
144 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
147 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
154 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
155 ctx
->Pixel
.DriverReadBuffer
);
156 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
157 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
158 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
159 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
160 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
161 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
163 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
166 /* read source image */
168 for (row
= 0; row
< height
; row
++) {
169 GLchan rgba
[MAX_WIDTH
][4];
171 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
172 /* convert GLchan to GLfloat */
173 for (i
= 0; i
< width
; i
++) {
174 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
175 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
176 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
177 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
181 /* read from the draw buffer again (in case of blending) */
183 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
184 ctx
->Color
.DriverDrawBuffer
);
185 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
188 /* do image transfer ops up until convolution */
189 for (row
= 0; row
< height
; row
++) {
190 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
193 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
194 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
195 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
196 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
197 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
198 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
200 /* color map lookup */
201 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
202 _mesa_map_rgba(ctx
, width
, rgba
);
204 /* GL_COLOR_TABLE lookup */
205 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
206 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
211 if (ctx
->Pixel
.Convolution2DEnabled
) {
212 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
215 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
216 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
220 /* do remaining image transfer ops */
221 for (row
= 0; row
< height
; row
++) {
222 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
224 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
225 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
226 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
229 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
230 _mesa_transform_rgba(ctx
, width
, rgba
);
232 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
233 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
234 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
236 /* update histogram count */
237 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
238 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
241 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
242 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
246 for (row
= 0; row
< height
; row
++) {
247 const GLfloat
*src
= convImage
+ row
* width
* 4;
250 /* clamp to [0,1] and convert float back to chan */
251 for (i
= 0; i
< width
; i
++) {
252 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
253 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
254 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
255 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
256 span
.color
.rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
257 span
.color
.rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
258 span
.color
.rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
259 span
.color
.rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
262 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._ReallyEnabled
) {
264 _swrast_pixel_texture(ctx
, &span
);
267 /* write row to framebuffer */
270 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
271 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
272 (const GLchan (*)[4])span
.color
.rgba
, NULL
);
278 _mesa_write_zoomed_rgba_span(ctx
, &span
,
279 (CONST
GLchan (*)[4])span
.color
.rgba
,
286 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
298 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
299 GLint width
, GLint height
, GLint destx
, GLint desty
)
301 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
303 GLboolean quick_draw
;
304 GLint sy
, dy
, stepy
, j
;
305 GLboolean changeBuffer
;
306 GLchan
*saveReadAlpha
;
307 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
309 const GLuint transferOps
= ctx
->_ImageTransferState
;
313 span
.arrayMask
|= SPAN_RGBA
;
315 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
316 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
320 /* Determine if copy should be done bottom-to-top or top-to-bottom */
322 /* top-down max-to-min */
323 sy
= srcy
+ height
- 1;
324 dy
= desty
+ height
- 1;
328 /* bottom-up min-to-max */
334 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
335 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
338 _mesa_span_default_z(ctx
, &span
);
339 if (ctx
->Fog
.Enabled
)
340 _mesa_span_default_fog(ctx
, &span
);
342 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
345 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
346 quick_draw
= GL_TRUE
;
349 quick_draw
= GL_FALSE
;
352 /* If read and draw buffer are different we must do buffer switching */
353 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
354 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
355 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
357 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
358 ctx
->Pixel
.DriverReadBuffer
);
362 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
364 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
369 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
370 ctx
->Pixel
.DriverReadBuffer
);
371 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
372 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
373 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
374 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
375 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
376 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
378 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
380 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
381 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
383 p
+= (width
* sizeof(GLchan
) * 4);
388 tmpImage
= NULL
; /* silence compiler warnings */
392 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
393 /* Get source pixels */
395 /* get from buffered image */
396 MEMCPY(span
.color
.rgba
, p
, width
* sizeof(GLchan
) * 4);
397 p
+= (width
* sizeof(GLchan
) * 4);
400 /* get from framebuffer */
402 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
403 ctx
->Pixel
.DriverReadBuffer
);
404 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
405 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
407 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
408 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
410 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
411 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
414 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
417 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, span
.color
.rgba
);
421 /* read from the draw buffer again (in case of blending) */
422 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
423 ctx
->Color
.DriverDrawBuffer
);
424 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
428 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
430 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
431 CHECKARRAY(rgbaFloat
, return);
433 /* convert chan to float */
434 for (k
= 0; k
< width
; k
++) {
435 rgbaFloat
[k
][RCOMP
] = (GLfloat
) span
.color
.rgba
[k
][RCOMP
] * scale
;
436 rgbaFloat
[k
][GCOMP
] = (GLfloat
) span
.color
.rgba
[k
][GCOMP
] * scale
;
437 rgbaFloat
[k
][BCOMP
] = (GLfloat
) span
.color
.rgba
[k
][BCOMP
] * scale
;
438 rgbaFloat
[k
][ACOMP
] = (GLfloat
) span
.color
.rgba
[k
][ACOMP
] * scale
;
441 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
442 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
443 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
444 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
445 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
446 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
448 /* color map lookup */
449 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
450 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
452 /* GL_COLOR_TABLE lookup */
453 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
454 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
457 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
458 abort(); /* should never get here; caught at top of function */
460 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
461 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
462 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
463 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
464 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
465 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
466 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
467 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
468 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
469 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
470 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
472 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
473 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
474 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
477 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
478 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
480 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
481 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
482 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
484 /* update histogram count */
485 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
486 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
489 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
490 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
492 /* clamp to [0,1] and convert float back to chan */
493 for (k
= 0; k
< width
; k
++) {
494 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
495 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
496 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
497 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
498 span
.color
.rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
499 span
.color
.rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
500 span
.color
.rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
501 span
.color
.rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
503 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
506 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._ReallyEnabled
) {
508 _swrast_pixel_texture(ctx
, &span
);
511 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
512 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
513 (const GLchan (*)[4])span
.color
.rgba
, NULL
);
519 _mesa_write_zoomed_rgba_span(ctx
, &span
,
520 (CONST
GLchan (*)[4]) span
.color
.rgba
,
527 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
531 /* Restore pixel source to be the draw buffer (for blending, etc) */
532 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
533 ctx
->Color
.DriverDrawBuffer
);
540 static void copy_ci_pixels( GLcontext
*ctx
,
541 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
542 GLint destx
, GLint desty
)
544 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
548 GLboolean changeBuffer
;
549 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
550 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
555 span
.arrayMask
|= SPAN_INDEX
;
557 /* Determine if copy should be bottom-to-top or top-to-bottom */
559 /* top-down max-to-min */
560 sy
= srcy
+ height
- 1;
561 dy
= desty
+ height
- 1;
565 /* bottom-up min-to-max */
571 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
572 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
575 _mesa_span_default_z(ctx
, &span
);
576 if (ctx
->Fog
.Enabled
)
577 _mesa_span_default_fog(ctx
, &span
);
579 /* If read and draw buffer are different we must do buffer switching */
580 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
581 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
583 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
584 ctx
->Pixel
.DriverReadBuffer
);
588 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
590 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
595 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
596 ctx
->Pixel
.DriverReadBuffer
);
598 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
599 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
605 tmpImage
= NULL
; /* silence compiler warning */
609 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
611 MEMCPY(span
.color
.index
, p
, width
* sizeof(GLuint
));
616 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
617 ctx
->Pixel
.DriverReadBuffer
);
619 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
624 /* set read buffer back to draw buffer (in case of logicops) */
625 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
626 ctx
->Color
.DriverDrawBuffer
);
629 if (shift_or_offset
) {
630 _mesa_shift_and_offset_ci( ctx
, width
, span
.color
.index
);
632 if (ctx
->Pixel
.MapColorFlag
) {
633 _mesa_map_ci( ctx
, width
, span
.color
.index
);
640 _mesa_write_zoomed_index_span(ctx
, &span
, desty
);
642 _mesa_write_index_span(ctx
, &span
, GL_BITMAP
);
645 /* Restore pixel source to be the draw buffer (for blending, etc) */
646 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
647 ctx
->Color
.DriverDrawBuffer
);
658 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
659 GLint width
, GLint height
,
660 GLint destx
, GLint desty
)
662 GLfloat depth
[MAX_WIDTH
];
663 GLfloat
*p
, *tmpImage
;
666 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
671 span
.arrayMask
|= SPAN_Z
;
673 if (!ctx
->Visual
.depthBits
) {
674 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
678 /* Determine if copy should be bottom-to-top or top-to-bottom */
680 /* top-down max-to-min */
681 sy
= srcy
+ height
- 1;
682 dy
= desty
+ height
- 1;
686 /* bottom-up min-to-max */
692 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
693 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
695 _mesa_span_default_color(ctx
, &span
);
696 if (ctx
->Fog
.Enabled
)
697 _mesa_span_default_fog(ctx
, &span
);
701 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
703 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
707 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
708 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
714 tmpImage
= NULL
; /* silence compiler warning */
718 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
720 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
724 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
727 for (i
= 0; i
< width
; i
++) {
728 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
729 span
.zArray
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
735 if (ctx
->Visual
.rgbMode
) {
737 _mesa_write_zoomed_rgba_span( ctx
, &span
,
738 (const GLchan (*)[4])span
.color
.rgba
,
741 _mesa_write_rgba_span(ctx
, &span
, GL_BITMAP
);
745 _mesa_write_zoomed_index_span( ctx
, &span
, desty
);
747 _mesa_write_index_span(ctx
, &span
, GL_BITMAP
);
757 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
758 GLint width
, GLint height
,
759 GLint destx
, GLint desty
)
763 GLstencil
*p
, *tmpImage
;
764 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
765 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
768 if (!ctx
->Visual
.stencilBits
) {
769 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
773 /* Determine if copy should be bottom-to-top or top-to-bottom */
775 /* top-down max-to-min */
776 sy
= srcy
+ height
- 1;
777 dy
= desty
+ height
- 1;
781 /* bottom-up min-to-max */
787 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
788 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
792 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
794 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
798 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
799 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
805 tmpImage
= NULL
; /* silence compiler warning */
809 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
810 GLstencil stencil
[MAX_WIDTH
];
813 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
817 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
820 if (shift_or_offset
) {
821 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
823 if (ctx
->Pixel
.MapStencilFlag
) {
824 _mesa_map_stencil( ctx
, width
, stencil
);
828 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
831 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
843 _swrast_CopyPixels( GLcontext
*ctx
,
844 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
845 GLint destx
, GLint desty
,
848 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
849 RENDER_START(swrast
,ctx
);
851 if (swrast
->NewState
)
852 _swrast_validate_derived( ctx
);
854 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
855 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
857 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
858 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
860 else if (type
== GL_DEPTH
) {
861 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
863 else if (type
== GL_STENCIL
) {
864 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
867 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
870 RENDER_FINISH(swrast
,ctx
);