1 /* $Id: s_copypix.c,v 1.9 2001/01/05 21:28:31 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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
53 regions_overlap(int srcx
, int srcy
, int dstx
, int dsty
, int width
, int height
,
54 float zoomX
, float zoomY
)
56 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
59 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
62 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
73 * RGBA copypixels with convolution.
76 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
77 GLint width
, GLint height
, GLint destx
, GLint desty
)
79 GLdepth zspan
[MAX_WIDTH
];
82 GLboolean changeBuffer
;
83 GLchan
*saveReadAlpha
;
84 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
85 const GLuint transferOps
= ctx
->_ImageTransferState
;
86 GLfloat
*dest
, *tmpImage
, *convImage
;
88 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
89 /* fill in array of z values */
91 (ctx
->Current
.RasterPos
[2] * ctx
->Visual
.DepthMax
);
93 for (i
= 0; i
< width
; i
++) {
98 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
101 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
102 quick_draw
= GL_TRUE
;
105 quick_draw
= GL_FALSE
;
108 /* If read and draw buffer are different we must do buffer switching */
109 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
110 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
111 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
114 /* allocate space for GLfloat image */
115 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
117 gl_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
120 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
123 gl_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
130 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
131 ctx
->Pixel
.DriverReadBuffer
);
132 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
133 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
134 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
135 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
136 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
137 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
139 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
142 /* read source image */
144 for (row
= 0; row
< height
; row
++) {
145 GLchan rgba
[MAX_WIDTH
][4];
147 gl_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
148 /* convert GLchan to GLfloat */
149 for (i
= 0; i
< width
; i
++) {
150 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
151 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
152 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
153 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
157 /* read from the draw buffer again (in case of blending) */
159 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
160 ctx
->Color
.DriverDrawBuffer
);
161 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
164 /* do image transfer ops up until convolution */
165 for (row
= 0; row
< height
; row
++) {
166 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) tmpImage
+ row
* width
* 4;
169 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
170 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
171 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
172 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
173 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
174 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
176 /* color map lookup */
177 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
178 _mesa_map_rgba(ctx
, width
, rgba
);
180 /* GL_COLOR_TABLE lookup */
181 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
182 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
187 if (ctx
->Pixel
.Convolution2DEnabled
) {
188 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
191 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
192 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
196 /* do remaining image transfer ops */
197 for (row
= 0; row
< height
; row
++) {
198 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) convImage
+ row
* width
* 4;
200 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
201 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
202 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
205 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
206 _mesa_transform_rgba(ctx
, width
, rgba
);
208 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
209 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
210 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
212 /* update histogram count */
213 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
214 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
217 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
218 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
222 for (row
= 0; row
< height
; row
++) {
223 const GLfloat
*src
= convImage
+ row
* width
* 4;
224 GLchan rgba
[MAX_WIDTH
][4];
227 /* clamp to [0,1] and convert float back to chan */
228 for (i
= 0; i
< width
; i
++) {
229 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
230 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
231 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
232 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
233 rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
234 rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
235 rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
236 rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
239 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
240 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
241 GLchan primary_rgba
[MAX_WIDTH
][4];
243 /* XXX not sure how multitexture is supposed to work here */
245 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
247 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
248 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
250 _swrast_texture_fragments(ctx
, unit
, width
, s
, t
, r
, NULL
,
255 /* write row to framebuffer */
258 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
259 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
260 (const GLchan (*)[4])rgba
, NULL
);
263 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
264 (const GLchan (*)[4])rgba
, desty
);
267 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
, GL_BITMAP
);
279 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
280 GLint width
, GLint height
, GLint destx
, GLint desty
)
282 GLdepth zspan
[MAX_WIDTH
];
283 GLchan rgba
[MAX_WIDTH
][4];
285 GLboolean quick_draw
;
288 GLboolean changeBuffer
;
289 GLchan
*saveReadAlpha
;
290 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
292 const GLuint transferOps
= ctx
->_ImageTransferState
;
294 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
295 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
299 /* Determine if copy should be done bottom-to-top or top-to-bottom */
301 /* top-down max-to-min */
302 sy
= srcy
+ height
- 1;
303 dy
= desty
+ height
- 1;
307 /* bottom-up min-to-max */
313 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
314 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
316 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
317 /* fill in array of z values */
318 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->Visual
.DepthMax
);
319 for (i
=0;i
<width
;i
++) {
324 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
327 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
328 quick_draw
= GL_TRUE
;
331 quick_draw
= GL_FALSE
;
334 /* If read and draw buffer are different we must do buffer switching */
335 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
336 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
337 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
339 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
340 ctx
->Pixel
.DriverReadBuffer
);
344 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
346 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
351 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
352 ctx
->Pixel
.DriverReadBuffer
);
353 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
354 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
355 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
356 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
357 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
358 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
360 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
362 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
363 gl_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
365 p
+= (width
* sizeof(GLchan
) * 4);
370 tmpImage
= NULL
; /* silence compiler warnings */
374 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
375 /* Get source pixels */
377 /* get from buffered image */
378 MEMCPY(rgba
, p
, width
* sizeof(GLchan
) * 4);
379 p
+= (width
* sizeof(GLchan
) * 4);
382 /* get from framebuffer */
384 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
385 ctx
->Pixel
.DriverReadBuffer
);
386 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
387 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
389 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
390 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
392 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
393 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
396 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
399 gl_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, rgba
);
403 /* read from the draw buffer again (in case of blending) */
404 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
405 ctx
->Color
.DriverDrawBuffer
);
406 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
410 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
411 GLfloat rgbaFloat
[MAX_WIDTH
][4];
413 /* convert chan to float */
414 for (k
= 0; k
< width
; k
++) {
415 rgbaFloat
[k
][RCOMP
] = (GLfloat
) rgba
[k
][RCOMP
] * scale
;
416 rgbaFloat
[k
][GCOMP
] = (GLfloat
) rgba
[k
][GCOMP
] * scale
;
417 rgbaFloat
[k
][BCOMP
] = (GLfloat
) rgba
[k
][BCOMP
] * scale
;
418 rgbaFloat
[k
][ACOMP
] = (GLfloat
) rgba
[k
][ACOMP
] * scale
;
421 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
422 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
423 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
424 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
425 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
426 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
428 /* color map lookup */
429 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
430 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
432 /* GL_COLOR_TABLE lookup */
433 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
434 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
437 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
440 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
441 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
442 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
443 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
444 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
445 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
446 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
447 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
448 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
449 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
450 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
452 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
453 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
454 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
457 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
458 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
460 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
461 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
462 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
464 /* update histogram count */
465 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
466 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
469 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
470 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
472 /* clamp to [0,1] and convert float back to chan */
473 for (k
= 0; k
< width
; k
++) {
474 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
475 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
476 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
477 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
478 rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
479 rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
480 rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
481 rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
485 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
486 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
487 GLchan primary_rgba
[MAX_WIDTH
][4];
489 /* XXX not sure how multitexture is supposed to work here */
491 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
493 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
494 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
496 _swrast_texture_fragments(ctx
, unit
, width
, s
, t
, r
, NULL
,
501 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
502 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
503 (const GLchan (*)[4])rgba
, NULL
);
506 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
507 (const GLchan (*)[4])rgba
, desty
);
510 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
, GL_BITMAP
);
514 /* Restore pixel source to be the draw buffer (for blending, etc) */
515 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
516 ctx
->Color
.DriverDrawBuffer
);
523 static void copy_ci_pixels( GLcontext
*ctx
,
524 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
525 GLint destx
, GLint desty
)
527 GLdepth zspan
[MAX_WIDTH
];
531 GLboolean changeBuffer
;
532 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
533 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
536 /* Determine if copy should be bottom-to-top or top-to-bottom */
538 /* top-down max-to-min */
539 sy
= srcy
+ height
- 1;
540 dy
= desty
+ height
- 1;
544 /* bottom-up min-to-max */
550 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
551 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
553 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
554 /* fill in array of z values */
555 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->Visual
.DepthMax
);
556 for (i
=0;i
<width
;i
++) {
561 /* If read and draw buffer are different we must do buffer switching */
562 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
563 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
565 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
566 ctx
->Pixel
.DriverReadBuffer
);
570 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
572 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
577 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
578 ctx
->Pixel
.DriverReadBuffer
);
580 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
581 gl_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
587 tmpImage
= NULL
; /* silence compiler warning */
591 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
592 GLuint indexes
[MAX_WIDTH
];
594 MEMCPY(indexes
, p
, width
* sizeof(GLuint
));
599 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
600 ctx
->Pixel
.DriverReadBuffer
);
602 gl_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, indexes
);
606 /* set read buffer back to draw buffer (in case of logicops) */
607 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
608 ctx
->Color
.DriverDrawBuffer
);
611 if (shift_or_offset
) {
612 _mesa_shift_and_offset_ci( ctx
, width
, indexes
);
614 if (ctx
->Pixel
.MapColorFlag
) {
615 _mesa_map_ci( ctx
, width
, indexes
);
619 gl_write_zoomed_index_span( ctx
, width
, destx
, dy
, zspan
, 0, indexes
, desty
);
622 gl_write_index_span(ctx
, width
, destx
, dy
, zspan
, 0, indexes
, GL_BITMAP
);
626 /* Restore pixel source to be the draw buffer (for blending, etc) */
627 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
628 ctx
->Color
.DriverDrawBuffer
);
639 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
640 GLint width
, GLint height
,
641 GLint destx
, GLint desty
)
643 GLfloat depth
[MAX_WIDTH
];
644 GLdepth zspan
[MAX_WIDTH
];
645 GLfloat
*p
, *tmpImage
;
646 GLuint indexes
[MAX_WIDTH
];
647 GLchan rgba
[MAX_WIDTH
][4];
650 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
653 if (!ctx
->Visual
.DepthBits
) {
654 gl_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
658 /* Determine if copy should be bottom-to-top or top-to-bottom */
660 /* top-down max-to-min */
661 sy
= srcy
+ height
- 1;
662 dy
= desty
+ height
- 1;
666 /* bottom-up min-to-max */
672 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
673 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
675 /* setup colors or indexes */
676 if (ctx
->Visual
.RGBAflag
) {
677 GLuint
*rgba32
= (GLuint
*) rgba
;
678 GLuint color
= *(GLuint
*)( ctx
->Current
.Color
);
679 for (i
= 0; i
< width
; i
++) {
684 for (i
= 0; i
< width
; i
++) {
685 indexes
[i
] = ctx
->Current
.Index
;
691 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
693 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
697 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
698 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
704 tmpImage
= NULL
; /* silence compiler warning */
708 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
710 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
714 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
717 for (i
= 0; i
< width
; i
++) {
718 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
719 zspan
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->Visual
.DepthMax
);
722 if (ctx
->Visual
.RGBAflag
) {
724 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
725 (const GLchan (*)[4])rgba
, desty
);
728 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
734 gl_write_zoomed_index_span( ctx
, width
, destx
, dy
,
735 zspan
, 0, indexes
, desty
);
738 gl_write_index_span( ctx
, width
, destx
, dy
,
739 zspan
, 0, indexes
, GL_BITMAP
);
750 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
751 GLint width
, GLint height
,
752 GLint destx
, GLint desty
)
756 GLstencil
*p
, *tmpImage
;
757 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
758 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
761 if (!ctx
->Visual
.StencilBits
) {
762 gl_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
766 /* Determine if copy should be bottom-to-top or top-to-bottom */
768 /* top-down max-to-min */
769 sy
= srcy
+ height
- 1;
770 dy
= desty
+ height
- 1;
774 /* bottom-up min-to-max */
780 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
781 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
785 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
787 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
791 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
792 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
798 tmpImage
= NULL
; /* silence compiler warning */
802 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
803 GLstencil stencil
[MAX_WIDTH
];
806 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
810 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
813 if (shift_or_offset
) {
814 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
816 if (ctx
->Pixel
.MapStencilFlag
) {
817 _mesa_map_stencil( ctx
, width
, stencil
);
821 gl_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
824 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
836 _swrast_CopyPixels( GLcontext
*ctx
,
837 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
838 GLint destx
, GLint desty
,
841 if (SWRAST_CONTEXT(ctx
)->NewState
)
842 _swrast_validate_derived( ctx
);
844 if (type
== GL_COLOR
&& ctx
->Visual
.RGBAflag
) {
845 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
847 else if (type
== GL_COLOR
&& !ctx
->Visual
.RGBAflag
) {
848 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
850 else if (type
== GL_DEPTH
) {
851 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
853 else if (type
== GL_STENCIL
) {
854 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
857 gl_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );