1 /* $Id: s_copypix.c,v 1.26 2001/12/17 04:54:35 brianp 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"
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 */
75 /* add one pixel of slop when zooming, just to be safe */
76 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
79 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
82 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
94 * RGBA copypixels with convolution.
97 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
98 GLint width
, GLint height
, GLint destx
, GLint desty
)
100 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
101 GLdepth zspan
[MAX_WIDTH
];
102 GLfloat fogSpan
[MAX_WIDTH
];
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
;
111 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
112 /* fill in array of z values */
113 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
117 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
118 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterFogCoord
);
120 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
122 for (i
= 0; i
< width
; i
++) {
128 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
131 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
132 quick_draw
= GL_TRUE
;
135 quick_draw
= GL_FALSE
;
138 /* If read and draw buffer are different we must do buffer switching */
139 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
140 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
141 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
144 /* allocate space for GLfloat image */
145 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
147 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
150 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
153 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
160 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
161 ctx
->Pixel
.DriverReadBuffer
);
162 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
163 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
164 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
165 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
166 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
167 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
169 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
172 /* read source image */
174 for (row
= 0; row
< height
; row
++) {
175 GLchan rgba
[MAX_WIDTH
][4];
177 _mesa_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
178 /* convert GLchan to GLfloat */
179 for (i
= 0; i
< width
; i
++) {
180 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
181 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
182 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
183 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
187 /* read from the draw buffer again (in case of blending) */
189 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
190 ctx
->Color
.DriverDrawBuffer
);
191 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
194 /* do image transfer ops up until convolution */
195 for (row
= 0; row
< height
; row
++) {
196 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
199 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
200 _mesa_scale_and_bias_rgba(ctx
, width
, rgba
,
201 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
202 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
203 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
204 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
206 /* color map lookup */
207 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
208 _mesa_map_rgba(ctx
, width
, rgba
);
210 /* GL_COLOR_TABLE lookup */
211 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
212 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
217 if (ctx
->Pixel
.Convolution2DEnabled
) {
218 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
221 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
222 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
226 /* do remaining image transfer ops */
227 for (row
= 0; row
< height
; row
++) {
228 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
230 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
231 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
232 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
235 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
236 _mesa_transform_rgba(ctx
, width
, rgba
);
238 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
239 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
240 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
242 /* update histogram count */
243 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
244 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
247 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
248 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
252 for (row
= 0; row
< height
; row
++) {
253 const GLfloat
*src
= convImage
+ row
* width
* 4;
254 GLchan rgba
[MAX_WIDTH
][4];
257 /* clamp to [0,1] and convert float back to chan */
258 for (i
= 0; i
< width
; i
++) {
259 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
260 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
261 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
262 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
263 rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
264 rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
265 rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
266 rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
269 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
270 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
271 GLfloat texcoord
[MAX_WIDTH
][3];
272 GLchan primary_rgba
[MAX_WIDTH
][4];
274 /* XXX not sure how multitexture is supposed to work here */
276 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
278 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
280 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
282 /* this is an ugly work-around. s,t,r has to be copied to
283 texcoords, because the functions need different
285 for (i
=0; i
<width
; i
++) {
286 texcoord
[i
][0] = s
[i
],
287 texcoord
[i
][1] = t
[i
],
288 texcoord
[i
][2] = r
[i
];
290 _old_swrast_texture_fragments( ctx
, unit
, width
, texcoord
, NULL
,
291 (CONST
GLchan (*)[4]) primary_rgba
,
296 /* write row to framebuffer */
299 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
300 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
301 (const GLchan (*)[4])rgba
, NULL
);
304 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, fogSpan
,
305 (const GLchan (*)[4])rgba
, desty
);
308 _old_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, fogSpan
, rgba
,
321 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
322 GLint width
, GLint height
, GLint destx
, GLint desty
)
324 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
325 GLdepth zspan
[MAX_WIDTH
];
326 GLfloat fogSpan
[MAX_WIDTH
];
327 GLchan rgba
[MAX_WIDTH
][4];
329 GLboolean quick_draw
;
332 GLboolean changeBuffer
;
333 GLchan
*saveReadAlpha
;
334 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
336 const GLuint transferOps
= ctx
->_ImageTransferState
;
338 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
339 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
343 /* Determine if copy should be done bottom-to-top or top-to-bottom */
345 /* top-down max-to-min */
346 sy
= srcy
+ height
- 1;
347 dy
= desty
+ height
- 1;
351 /* bottom-up min-to-max */
357 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
358 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
360 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
361 /* fill in array of z values */
362 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
365 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
366 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterFogCoord
);
368 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
370 for (i
=0;i
<width
;i
++) {
376 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
379 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
380 quick_draw
= GL_TRUE
;
383 quick_draw
= GL_FALSE
;
386 /* If read and draw buffer are different we must do buffer switching */
387 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
388 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
389 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
391 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
392 ctx
->Pixel
.DriverReadBuffer
);
396 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
398 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
403 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
404 ctx
->Pixel
.DriverReadBuffer
);
405 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
406 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
407 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
408 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
409 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
410 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
412 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
414 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
415 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
417 p
+= (width
* sizeof(GLchan
) * 4);
422 tmpImage
= NULL
; /* silence compiler warnings */
426 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
427 /* Get source pixels */
429 /* get from buffered image */
430 MEMCPY(rgba
, p
, width
* sizeof(GLchan
) * 4);
431 p
+= (width
* sizeof(GLchan
) * 4);
434 /* get from framebuffer */
436 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
437 ctx
->Pixel
.DriverReadBuffer
);
438 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
439 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
441 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
442 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
444 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
445 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
448 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
451 _mesa_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, rgba
);
455 /* read from the draw buffer again (in case of blending) */
456 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
457 ctx
->Color
.DriverDrawBuffer
);
458 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
462 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
464 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
465 CHECKARRAY(rgbaFloat
, return);
467 /* convert chan to float */
468 for (k
= 0; k
< width
; k
++) {
469 rgbaFloat
[k
][RCOMP
] = (GLfloat
) rgba
[k
][RCOMP
] * scale
;
470 rgbaFloat
[k
][GCOMP
] = (GLfloat
) rgba
[k
][GCOMP
] * scale
;
471 rgbaFloat
[k
][BCOMP
] = (GLfloat
) rgba
[k
][BCOMP
] * scale
;
472 rgbaFloat
[k
][ACOMP
] = (GLfloat
) rgba
[k
][ACOMP
] * scale
;
475 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
476 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
477 ctx
->Pixel
.RedScale
, ctx
->Pixel
.GreenScale
,
478 ctx
->Pixel
.BlueScale
, ctx
->Pixel
.AlphaScale
,
479 ctx
->Pixel
.RedBias
, ctx
->Pixel
.GreenBias
,
480 ctx
->Pixel
.BlueBias
, ctx
->Pixel
.AlphaBias
);
482 /* color map lookup */
483 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
484 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
486 /* GL_COLOR_TABLE lookup */
487 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
488 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
491 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
494 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
495 if (transferOps
& IMAGE_POST_CONVOLUTION_SCALE_BIAS
) {
496 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
,
497 ctx
->Pixel
.PostConvolutionScale
[RCOMP
],
498 ctx
->Pixel
.PostConvolutionScale
[GCOMP
],
499 ctx
->Pixel
.PostConvolutionScale
[BCOMP
],
500 ctx
->Pixel
.PostConvolutionScale
[ACOMP
],
501 ctx
->Pixel
.PostConvolutionBias
[RCOMP
],
502 ctx
->Pixel
.PostConvolutionBias
[GCOMP
],
503 ctx
->Pixel
.PostConvolutionBias
[BCOMP
],
504 ctx
->Pixel
.PostConvolutionBias
[ACOMP
]);
506 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
507 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
508 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
511 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
512 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
514 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
515 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
516 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
518 /* update histogram count */
519 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
520 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
523 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
524 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
526 /* clamp to [0,1] and convert float back to chan */
527 for (k
= 0; k
< width
; k
++) {
528 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
529 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
530 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
531 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
532 rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
533 rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
534 rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
535 rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
537 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
540 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
542 GLchan primary_rgba
[MAX_WIDTH
][4];
543 GLfloat texcoord
[MAX_WIDTH
][3];
544 DEFARRAY(GLfloat
, s
, MAX_WIDTH
); /* mac 32k limitation */
545 DEFARRAY(GLfloat
, t
, MAX_WIDTH
); /* mac 32k limitation */
546 DEFARRAY(GLfloat
, r
, MAX_WIDTH
); /* mac 32k limitation */
547 DEFARRAY(GLfloat
, q
, MAX_WIDTH
); /* mac 32k limitation */
548 CHECKARRAY(s
, return); /* mac 32k limitation */
549 CHECKARRAY(t
, return);
550 CHECKARRAY(r
, return);
551 CHECKARRAY(q
, return);
553 /* XXX not sure how multitexture is supposed to work here */
554 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
556 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
557 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
559 /* this is an ugly work-around. s,t,r has to be copied to
560 texcoords, because the functions need different
562 for (i
=0; i
<width
; i
++) {
563 texcoord
[i
][0] = s
[i
],
564 texcoord
[i
][1] = t
[i
],
565 texcoord
[i
][2] = r
[i
];
567 _old_swrast_texture_fragments( ctx
, unit
, width
, texcoord
, NULL
,
568 (CONST
GLchan (*)[4]) primary_rgba
,
572 UNDEFARRAY(s
); /* mac 32k limitation */
578 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
579 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
580 (const GLchan (*)[4])rgba
, NULL
);
583 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, fogSpan
,
584 (const GLchan (*)[4])rgba
, desty
);
587 _old_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, fogSpan
, rgba
,
592 /* Restore pixel source to be the draw buffer (for blending, etc) */
593 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
594 ctx
->Color
.DriverDrawBuffer
);
601 static void copy_ci_pixels( GLcontext
*ctx
,
602 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
603 GLint destx
, GLint desty
)
605 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
606 GLdepth zspan
[MAX_WIDTH
];
607 GLfloat fogSpan
[MAX_WIDTH
];
611 GLboolean changeBuffer
;
612 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
613 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
616 /* Determine if copy should be bottom-to-top or top-to-bottom */
618 /* top-down max-to-min */
619 sy
= srcy
+ height
- 1;
620 dy
= desty
+ height
- 1;
624 /* bottom-up min-to-max */
630 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
631 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
633 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
634 /* fill in array of z values */
635 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMax
);
638 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
639 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterFogCoord
);
641 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
643 for (i
=0;i
<width
;i
++) {
649 /* If read and draw buffer are different we must do buffer switching */
650 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
651 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
653 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
654 ctx
->Pixel
.DriverReadBuffer
);
658 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
660 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
665 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
666 ctx
->Pixel
.DriverReadBuffer
);
668 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
669 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
675 tmpImage
= NULL
; /* silence compiler warning */
679 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
680 GLuint indexes
[MAX_WIDTH
];
682 MEMCPY(indexes
, p
, width
* sizeof(GLuint
));
687 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
688 ctx
->Pixel
.DriverReadBuffer
);
690 _mesa_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, indexes
);
694 /* set read buffer back to draw buffer (in case of logicops) */
695 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
696 ctx
->Color
.DriverDrawBuffer
);
699 if (shift_or_offset
) {
700 _mesa_shift_and_offset_ci( ctx
, width
, indexes
);
702 if (ctx
->Pixel
.MapColorFlag
) {
703 _mesa_map_ci( ctx
, width
, indexes
);
707 _mesa_write_zoomed_index_span(ctx
, width
, destx
, dy
, zspan
, fogSpan
,
711 _old_write_index_span(ctx
, width
, destx
, dy
, zspan
, fogSpan
, indexes
,
716 /* Restore pixel source to be the draw buffer (for blending, etc) */
717 (*swrast
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
718 ctx
->Color
.DriverDrawBuffer
);
729 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
730 GLint width
, GLint height
,
731 GLint destx
, GLint desty
)
733 GLfloat depth
[MAX_WIDTH
];
734 GLdepth zspan
[MAX_WIDTH
];
735 GLfloat fogSpan
[MAX_WIDTH
];
736 GLfloat
*p
, *tmpImage
;
737 GLuint indexes
[MAX_WIDTH
];
740 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
742 DEFMARRAY(GLchan
, rgba
, MAX_WIDTH
, 4); /* mac 32k limitation */
743 CHECKARRAY(rgba
, return); /* mac 32k limitation */
745 if (!ctx
->Visual
.depthBits
) {
746 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
747 UNDEFARRAY(rgba
); /* mac 32k limitation */
751 /* Determine if copy should be bottom-to-top or top-to-bottom */
753 /* top-down max-to-min */
754 sy
= srcy
+ height
- 1;
755 dy
= desty
+ height
- 1;
759 /* bottom-up min-to-max */
765 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
766 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
768 /* setup colors or indexes */
769 if (ctx
->Visual
.rgbMode
) {
771 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
][0]);
772 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
][1]);
773 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
][2]);
774 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
][3]);
775 for (i
= 0; i
< width
; i
++) {
783 for (i
= 0; i
< width
; i
++) {
784 indexes
[i
] = ctx
->Current
.Index
;
788 if (ctx
->Fog
.Enabled
) {
791 if (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORDINATE_EXT
)
792 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterFogCoord
);
794 fog
= _mesa_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
796 for (i
= 0; i
< width
; i
++) {
803 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
805 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
806 UNDEFARRAY(rgba
); /* mac 32k limitation */
810 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
811 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
817 tmpImage
= NULL
; /* silence compiler warning */
821 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
823 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
827 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
830 for (i
= 0; i
< width
; i
++) {
831 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
832 zspan
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
835 if (ctx
->Visual
.rgbMode
) {
837 _mesa_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
,
838 fogSpan
, (const GLchan (*)[4])rgba
, desty
);
841 _old_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, fogSpan
,
842 (GLchan (*)[4])rgba
, NULL
, GL_BITMAP
);
847 _mesa_write_zoomed_index_span( ctx
, width
, destx
, dy
,
848 zspan
, fogSpan
, indexes
, desty
);
851 _old_write_index_span( ctx
, width
, destx
, dy
,
852 zspan
, fogSpan
, indexes
, NULL
, GL_BITMAP
);
857 UNDEFARRAY(rgba
); /* mac 32k limitation */
865 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
866 GLint width
, GLint height
,
867 GLint destx
, GLint desty
)
871 GLstencil
*p
, *tmpImage
;
872 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
873 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
876 if (!ctx
->Visual
.stencilBits
) {
877 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
881 /* Determine if copy should be bottom-to-top or top-to-bottom */
883 /* top-down max-to-min */
884 sy
= srcy
+ height
- 1;
885 dy
= desty
+ height
- 1;
889 /* bottom-up min-to-max */
895 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
896 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
900 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
902 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
906 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
907 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
913 tmpImage
= NULL
; /* silence compiler warning */
917 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
918 GLstencil stencil
[MAX_WIDTH
];
921 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
925 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
928 if (shift_or_offset
) {
929 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
931 if (ctx
->Pixel
.MapStencilFlag
) {
932 _mesa_map_stencil( ctx
, width
, stencil
);
936 _mesa_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
939 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
951 _swrast_CopyPixels( GLcontext
*ctx
,
952 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
953 GLint destx
, GLint desty
,
956 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
957 RENDER_START(swrast
,ctx
);
959 if (swrast
->NewState
)
960 _swrast_validate_derived( ctx
);
962 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
963 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
965 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
966 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
968 else if (type
== GL_DEPTH
) {
969 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
971 else if (type
== GL_STENCIL
) {
972 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
975 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
978 RENDER_FINISH(swrast
,ctx
);