1 /* $Id: s_copypix.c,v 1.3 2000/11/05 18:24:40 keithw 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_imaging.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
);
172 /* color map lookup */
173 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
174 _mesa_map_rgba(ctx
, width
, rgba
);
176 /* GL_COLOR_TABLE lookup */
177 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
178 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgba
);
183 if (ctx
->Pixel
.Convolution2DEnabled
) {
184 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
187 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
188 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
192 /* do remaining image transfer ops */
193 for (row
= 0; row
< height
; row
++) {
194 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) convImage
+ row
* width
* 4;
196 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
197 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
198 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgba
);
201 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
202 _mesa_transform_rgba(ctx
, width
, rgba
);
204 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
205 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
206 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgba
);
208 /* update histogram count */
209 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
210 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
213 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
214 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgba
);
218 for (row
= 0; row
< height
; row
++) {
219 const GLfloat
*src
= convImage
+ row
* width
* 4;
220 GLchan rgba
[MAX_WIDTH
][4];
223 /* clamp to [0,1] and convert float back to chan */
224 for (i
= 0; i
< width
; i
++) {
225 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
226 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
227 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
228 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
229 rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
230 rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
231 rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
232 rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
235 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
236 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
237 GLchan primary_rgba
[MAX_WIDTH
][4];
239 /* XXX not sure how multitexture is supposed to work here */
241 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
243 for (unit
= 0; unit
< MAX_TEXTURE_UNITS
; unit
++) {
244 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
246 gl_texture_pixels(ctx
, unit
, width
, s
, t
, r
, NULL
,
251 /* write row to framebuffer */
254 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
255 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
256 (const GLchan (*)[4])rgba
, NULL
);
259 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
260 (const GLchan (*)[4])rgba
, desty
);
263 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
, GL_BITMAP
);
275 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
276 GLint width
, GLint height
, GLint destx
, GLint desty
)
278 GLdepth zspan
[MAX_WIDTH
];
279 GLchan rgba
[MAX_WIDTH
][4];
281 GLboolean quick_draw
;
284 GLboolean changeBuffer
;
285 GLchan
*saveReadAlpha
;
286 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
288 const GLuint transferOps
= ctx
->_ImageTransferState
;
290 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
291 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
295 /* Determine if copy should be done bottom-to-top or top-to-bottom */
297 /* top-down max-to-min */
298 sy
= srcy
+ height
- 1;
299 dy
= desty
+ height
- 1;
303 /* bottom-up min-to-max */
309 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
310 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
312 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
313 /* fill in array of z values */
314 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->Visual
.DepthMax
);
315 for (i
=0;i
<width
;i
++) {
320 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
323 && destx
+ width
<= ctx
->DrawBuffer
->Width
) {
324 quick_draw
= GL_TRUE
;
327 quick_draw
= GL_FALSE
;
330 /* If read and draw buffer are different we must do buffer switching */
331 saveReadAlpha
= ctx
->ReadBuffer
->Alpha
;
332 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
333 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
335 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
336 ctx
->Pixel
.DriverReadBuffer
);
340 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
342 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
347 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
348 ctx
->Pixel
.DriverReadBuffer
);
349 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
)
350 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
351 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
)
352 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
353 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
)
354 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
356 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
358 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
359 gl_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
361 p
+= (width
* sizeof(GLchan
) * 4);
366 tmpImage
= NULL
; /* silence compiler warnings */
370 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
371 /* Get source pixels */
373 /* get from buffered image */
374 MEMCPY(rgba
, p
, width
* sizeof(GLchan
) * 4);
375 p
+= (width
* sizeof(GLchan
) * 4);
378 /* get from framebuffer */
380 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
381 ctx
->Pixel
.DriverReadBuffer
);
382 if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_LEFT
) {
383 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontLeftAlpha
;
385 else if (ctx
->Pixel
.DriverReadBuffer
== GL_BACK_LEFT
) {
386 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackLeftAlpha
;
388 else if (ctx
->Pixel
.DriverReadBuffer
== GL_FRONT_RIGHT
) {
389 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->FrontRightAlpha
;
392 ctx
->ReadBuffer
->Alpha
= ctx
->ReadBuffer
->BackRightAlpha
;
395 gl_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, rgba
);
399 /* read from the draw buffer again (in case of blending) */
400 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
401 ctx
->Color
.DriverDrawBuffer
);
402 ctx
->ReadBuffer
->Alpha
= saveReadAlpha
;
406 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
407 GLfloat rgbaFloat
[MAX_WIDTH
][4];
409 /* convert chan to float */
410 for (k
= 0; k
< width
; k
++) {
411 rgbaFloat
[k
][RCOMP
] = (GLfloat
) rgba
[k
][RCOMP
] * scale
;
412 rgbaFloat
[k
][GCOMP
] = (GLfloat
) rgba
[k
][GCOMP
] * scale
;
413 rgbaFloat
[k
][BCOMP
] = (GLfloat
) rgba
[k
][BCOMP
] * scale
;
414 rgbaFloat
[k
][ACOMP
] = (GLfloat
) rgba
[k
][ACOMP
] * scale
;
417 if (transferOps
& IMAGE_SCALE_BIAS_BIT
) {
418 _mesa_scale_and_bias_rgba(ctx
, width
, rgbaFloat
);
420 /* color map lookup */
421 if (transferOps
& IMAGE_MAP_COLOR_BIT
) {
422 _mesa_map_rgba(ctx
, width
, rgbaFloat
);
424 /* GL_COLOR_TABLE lookup */
425 if (transferOps
& IMAGE_COLOR_TABLE_BIT
) {
426 _mesa_lookup_rgba(&ctx
->ColorTable
, width
, rgbaFloat
);
429 if (transferOps
& IMAGE_CONVOLUTION_BIT
) {
432 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
433 if (transferOps
& IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT
) {
434 _mesa_lookup_rgba(&ctx
->PostConvolutionColorTable
, width
, rgbaFloat
);
437 if (transferOps
& IMAGE_COLOR_MATRIX_BIT
) {
438 _mesa_transform_rgba(ctx
, width
, rgbaFloat
);
440 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
441 if (transferOps
& IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT
) {
442 _mesa_lookup_rgba(&ctx
->PostColorMatrixColorTable
, width
, rgbaFloat
);
444 /* update histogram count */
445 if (transferOps
& IMAGE_HISTOGRAM_BIT
) {
446 _mesa_update_histogram(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
449 if (transferOps
& IMAGE_MIN_MAX_BIT
) {
450 _mesa_update_minmax(ctx
, width
, (CONST
GLfloat (*)[4]) rgbaFloat
);
452 /* clamp to [0,1] and convert float back to chan */
453 for (k
= 0; k
< width
; k
++) {
454 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
455 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
456 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
457 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
458 rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
459 rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
460 rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
461 rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
465 if (ctx
->Texture
._ReallyEnabled
&& ctx
->Pixel
.PixelTextureEnabled
) {
466 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], r
[MAX_WIDTH
], q
[MAX_WIDTH
];
467 GLchan primary_rgba
[MAX_WIDTH
][4];
469 /* XXX not sure how multitexture is supposed to work here */
471 MEMCPY(primary_rgba
, rgba
, 4 * width
* sizeof(GLchan
));
473 for (unit
= 0; unit
< MAX_TEXTURE_UNITS
; unit
++) {
474 _mesa_pixeltexgen(ctx
, width
, (const GLchan (*)[4]) rgba
,
476 gl_texture_pixels(ctx
, unit
, width
, s
, t
, r
, NULL
,
481 if (quick_draw
&& dy
>= 0 && dy
< ctx
->DrawBuffer
->Height
) {
482 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
483 (const GLchan (*)[4])rgba
, NULL
);
486 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
487 (const GLchan (*)[4])rgba
, desty
);
490 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0, rgba
, GL_BITMAP
);
494 /* Restore pixel source to be the draw buffer (for blending, etc) */
495 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
496 ctx
->Color
.DriverDrawBuffer
);
503 static void copy_ci_pixels( GLcontext
*ctx
,
504 GLint srcx
, GLint srcy
, GLint width
, GLint height
,
505 GLint destx
, GLint desty
)
507 GLdepth zspan
[MAX_WIDTH
];
511 GLboolean changeBuffer
;
512 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
513 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
516 /* Determine if copy should be bottom-to-top or top-to-bottom */
518 /* top-down max-to-min */
519 sy
= srcy
+ height
- 1;
520 dy
= desty
+ height
- 1;
524 /* bottom-up min-to-max */
530 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
531 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
533 if (ctx
->Depth
.Test
|| ctx
->Fog
.Enabled
) {
534 /* fill in array of z values */
535 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->Visual
.DepthMax
);
536 for (i
=0;i
<width
;i
++) {
541 /* If read and draw buffer are different we must do buffer switching */
542 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
543 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
545 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
546 ctx
->Pixel
.DriverReadBuffer
);
550 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
552 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
557 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
558 ctx
->Pixel
.DriverReadBuffer
);
560 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
561 gl_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
567 tmpImage
= NULL
; /* silence compiler warning */
571 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
572 GLuint indexes
[MAX_WIDTH
];
574 MEMCPY(indexes
, p
, width
* sizeof(GLuint
));
579 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
,
580 ctx
->Pixel
.DriverReadBuffer
);
582 gl_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
, indexes
);
586 /* set read buffer back to draw buffer (in case of logicops) */
587 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
588 ctx
->Color
.DriverDrawBuffer
);
591 if (shift_or_offset
) {
592 _mesa_shift_and_offset_ci( ctx
, width
, indexes
);
594 if (ctx
->Pixel
.MapColorFlag
) {
595 _mesa_map_ci( ctx
, width
, indexes
);
599 gl_write_zoomed_index_span( ctx
, width
, destx
, dy
, zspan
, 0, indexes
, desty
);
602 gl_write_index_span(ctx
, width
, destx
, dy
, zspan
, 0, indexes
, GL_BITMAP
);
606 /* Restore pixel source to be the draw buffer (for blending, etc) */
607 (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
,
608 ctx
->Color
.DriverDrawBuffer
);
619 static void copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
620 GLint width
, GLint height
,
621 GLint destx
, GLint desty
)
623 GLfloat depth
[MAX_WIDTH
];
624 GLdepth zspan
[MAX_WIDTH
];
625 GLfloat
*p
, *tmpImage
;
626 GLuint indexes
[MAX_WIDTH
];
627 GLchan rgba
[MAX_WIDTH
][4];
630 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
633 if (!ctx
->ReadBuffer
->DepthBuffer
|| !ctx
->DrawBuffer
->DepthBuffer
) {
634 gl_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
638 /* Determine if copy should be bottom-to-top or top-to-bottom */
640 /* top-down max-to-min */
641 sy
= srcy
+ height
- 1;
642 dy
= desty
+ height
- 1;
646 /* bottom-up min-to-max */
652 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
653 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
655 /* setup colors or indexes */
656 if (ctx
->Visual
.RGBAflag
) {
657 GLuint
*rgba32
= (GLuint
*) rgba
;
658 GLuint color
= *(GLuint
*)( ctx
->Current
.Color
);
659 for (i
= 0; i
< width
; i
++) {
664 for (i
= 0; i
< width
; i
++) {
665 indexes
[i
] = ctx
->Current
.Index
;
671 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
673 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
677 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
678 _mesa_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
684 tmpImage
= NULL
; /* silence compiler warning */
688 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
690 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
694 _mesa_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
697 for (i
= 0; i
< width
; i
++) {
698 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
699 zspan
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->Visual
.DepthMax
);
702 if (ctx
->Visual
.RGBAflag
) {
704 gl_write_zoomed_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
705 (const GLchan (*)[4])rgba
, desty
);
708 gl_write_rgba_span( ctx
, width
, destx
, dy
, zspan
, 0,
714 gl_write_zoomed_index_span( ctx
, width
, destx
, dy
,
715 zspan
, 0, indexes
, desty
);
718 gl_write_index_span( ctx
, width
, destx
, dy
,
719 zspan
, 0, indexes
, GL_BITMAP
);
730 static void copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
731 GLint width
, GLint height
,
732 GLint destx
, GLint desty
)
736 GLstencil
*p
, *tmpImage
;
737 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
738 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
741 if (!ctx
->DrawBuffer
->Stencil
|| !ctx
->ReadBuffer
->Stencil
) {
742 gl_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
746 /* Determine if copy should be bottom-to-top or top-to-bottom */
748 /* top-down max-to-min */
749 sy
= srcy
+ height
- 1;
750 dy
= desty
+ height
- 1;
754 /* bottom-up min-to-max */
760 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
761 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
765 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
767 gl_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
771 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
772 _mesa_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
778 tmpImage
= NULL
; /* silence compiler warning */
782 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
783 GLstencil stencil
[MAX_WIDTH
];
786 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
790 _mesa_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
793 if (shift_or_offset
) {
794 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
796 if (ctx
->Pixel
.MapStencilFlag
) {
797 _mesa_map_stencil( ctx
, width
, stencil
);
801 gl_write_zoomed_stencil_span( ctx
, width
, destx
, dy
, stencil
, desty
);
804 _mesa_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
816 _swrast_CopyPixels( GLcontext
*ctx
,
817 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
818 GLint destx
, GLint desty
,
821 if (SWRAST_CONTEXT(ctx
)->NewState
)
822 _swrast_validate_derived( ctx
);
824 if (type
== GL_COLOR
&& ctx
->Visual
.RGBAflag
) {
825 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
827 else if (type
== GL_COLOR
&& !ctx
->Visual
.RGBAflag
) {
828 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
830 else if (type
== GL_DEPTH
) {
831 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
833 else if (type
== GL_STENCIL
) {
834 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
837 gl_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );