d418eb541052e3604eb2bdd1984458c3af354e0c
[mesa.git] / src / mesa / swrast / s_copypix.c
1 /* $Id: s_copypix.c,v 1.29 2002/01/27 18:32:03 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 #include "glheader.h"
29 #include "colormac.h"
30 #include "context.h"
31 #include "convolve.h"
32 #include "feedback.h"
33 #include "macros.h"
34 #include "mem.h"
35 #include "mmath.h"
36 #include "pixel.h"
37
38 #include "s_context.h"
39 #include "s_depth.h"
40 #include "s_fog.h"
41 #include "s_histogram.h"
42 #include "s_pixeltex.h"
43 #include "s_span.h"
44 #include "s_stencil.h"
45 #include "s_texture.h"
46 #include "s_zoom.h"
47
48
49
50 /*
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.
55 */
56 static GLboolean
57 regions_overlap(GLint srcx, GLint srcy,
58 GLint dstx, GLint dsty,
59 GLint width, GLint height,
60 GLfloat zoomX, GLfloat zoomY)
61 {
62 if (zoomX == 1.0 && zoomY == 1.0) {
63 /* no zoom */
64 if (srcx >= dstx + width || (srcx + width <= dstx)) {
65 return GL_FALSE;
66 }
67 else if (srcy < dsty) { /* this is OK */
68 return GL_FALSE;
69 }
70 else if (srcy > dsty + height) {
71 return GL_FALSE;
72 }
73 else {
74 return GL_TRUE;
75 }
76 }
77 else {
78 /* add one pixel of slop when zooming, just to be safe */
79 if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
80 return GL_FALSE;
81 }
82 else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
83 return GL_FALSE;
84 }
85 else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
86 return GL_FALSE;
87 }
88 else {
89 return GL_TRUE;
90 }
91 }
92 }
93
94
95
96 /*
97 * RGBA copypixels with convolution.
98 */
99 static void
100 copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
101 GLint width, GLint height, GLint destx, GLint desty)
102 {
103 SWcontext *swrast = SWRAST_CONTEXT(ctx);
104 GLboolean quick_draw;
105 GLint row;
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;
111 struct sw_span span;
112
113 INIT_SPAN(span);
114 span.arrayMask |= SPAN_RGBA;
115
116 if (ctx->Depth.Test)
117 _mesa_span_default_z(ctx, &span);
118 if (ctx->Fog.Enabled)
119 _mesa_span_default_fog(ctx, &span);
120
121
122 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
123 && !zoom
124 && destx >= 0
125 && destx + width <= ctx->DrawBuffer->Width) {
126 quick_draw = GL_TRUE;
127 }
128 else {
129 quick_draw = GL_FALSE;
130 }
131
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;
136
137
138 /* allocate space for GLfloat image */
139 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
140 if (!tmpImage) {
141 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
142 return;
143 }
144 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
145 if (!convImage) {
146 FREE(tmpImage);
147 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
148 return;
149 }
150
151 dest = tmpImage;
152
153 if (changeBuffer) {
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;
162 else
163 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
164 }
165
166 /* read source image */
167 dest = tmpImage;
168 for (row = 0; row < height; row++) {
169 GLchan rgba[MAX_WIDTH][4];
170 GLint i;
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);
178 }
179 }
180
181 /* read from the draw buffer again (in case of blending) */
182 if (changeBuffer) {
183 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
184 ctx->Color.DriverDrawBuffer );
185 ctx->ReadBuffer->Alpha = saveReadAlpha;
186 }
187
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);
191
192 /* scale & bias */
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);
199 }
200 /* color map lookup */
201 if (transferOps & IMAGE_MAP_COLOR_BIT) {
202 _mesa_map_rgba(ctx, width, rgba);
203 }
204 /* GL_COLOR_TABLE lookup */
205 if (transferOps & IMAGE_COLOR_TABLE_BIT) {
206 _mesa_lookup_rgba(&ctx->ColorTable, width, rgba);
207 }
208 }
209
210 /* do convolution */
211 if (ctx->Pixel.Convolution2DEnabled) {
212 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
213 }
214 else {
215 ASSERT(ctx->Pixel.Separable2DEnabled);
216 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
217 }
218 FREE(tmpImage);
219
220 /* do remaining image transfer ops */
221 for (row = 0; row < height; row++) {
222 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
223
224 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
225 if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
226 _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgba);
227 }
228 /* color matrix */
229 if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
230 _mesa_transform_rgba(ctx, width, rgba);
231 }
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);
235 }
236 /* update histogram count */
237 if (transferOps & IMAGE_HISTOGRAM_BIT) {
238 _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgba);
239 }
240 /* update min/max */
241 if (transferOps & IMAGE_MIN_MAX_BIT) {
242 _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgba);
243 }
244 }
245
246 for (row = 0; row < height; row++) {
247 const GLfloat *src = convImage + row * width * 4;
248 GLint i, dy;
249
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);
260 }
261
262 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
263 span.end = width;
264 _swrast_pixel_texture(ctx, &span);
265 }
266
267 /* write row to framebuffer */
268
269 dy = desty + row;
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 );
273 }
274 else if (zoom) {
275 _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, span.zArray,
276 span.fogArray,
277 (const GLchan (*)[4])span.color.rgba,
278 desty);
279 }
280 else {
281 span.x = destx;
282 span.y = dy;
283 span.end = width;
284 _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
285 }
286 }
287
288 FREE(convImage);
289 }
290
291
292 /*
293 * RGBA copypixels
294 */
295 static void
296 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
297 GLint width, GLint height, GLint destx, GLint desty)
298 {
299 SWcontext *swrast = SWRAST_CONTEXT(ctx);
300 GLchan *tmpImage,*p;
301 GLboolean quick_draw;
302 GLint sy, dy, stepy;
303 GLint i, j;
304 GLboolean changeBuffer;
305 GLchan *saveReadAlpha;
306 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
307 GLint overlapping;
308 const GLuint transferOps = ctx->_ImageTransferState;
309 struct sw_span span;
310
311 INIT_SPAN(span);
312 span.arrayMask |= SPAN_RGBA;
313
314 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
315 copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
316 return;
317 }
318
319 /* Determine if copy should be done bottom-to-top or top-to-bottom */
320 if (srcy < desty) {
321 /* top-down max-to-min */
322 sy = srcy + height - 1;
323 dy = desty + height - 1;
324 stepy = -1;
325 }
326 else {
327 /* bottom-up min-to-max */
328 sy = srcy;
329 dy = desty;
330 stepy = 1;
331 }
332
333 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
334 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
335
336 if (ctx->Depth.Test || ctx->Fog.Enabled) {
337 /* fill in array of z values */
338 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax);
339 GLfloat fog;
340
341 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
342 fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
343 else
344 fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
345
346 for (i=0;i<width;i++) {
347 span.zArray[i] = z;
348 span.fogArray[i] = fog;
349 }
350 span.arrayMask |= (SPAN_Z | SPAN_FOG);
351 }
352
353 if (ctx->Depth.Test)
354 _mesa_span_default_z(ctx, &span);
355 if (ctx->Fog.Enabled)
356 _mesa_span_default_fog(ctx, &span);
357
358 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
359 && !zoom
360 && destx >= 0
361 && destx + width <= ctx->DrawBuffer->Width) {
362 quick_draw = GL_TRUE;
363 }
364 else {
365 quick_draw = GL_FALSE;
366 }
367
368 /* If read and draw buffer are different we must do buffer switching */
369 saveReadAlpha = ctx->ReadBuffer->Alpha;
370 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
371 || ctx->DrawBuffer != ctx->ReadBuffer;
372
373 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
374 ctx->Pixel.DriverReadBuffer );
375
376 if (overlapping) {
377 GLint ssy = sy;
378 tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
379 if (!tmpImage) {
380 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
381 return;
382 }
383 p = tmpImage;
384 if (changeBuffer) {
385 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
386 ctx->Pixel.DriverReadBuffer );
387 if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
388 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
389 else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
390 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
391 else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
392 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
393 else
394 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
395 }
396 for (j = 0; j < height; j++, ssy += stepy) {
397 _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
398 (GLchan (*)[4]) p );
399 p += (width * sizeof(GLchan) * 4);
400 }
401 p = tmpImage;
402 }
403 else {
404 tmpImage = NULL; /* silence compiler warnings */
405 p = NULL;
406 }
407
408 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
409 /* Get source pixels */
410 if (overlapping) {
411 /* get from buffered image */
412 MEMCPY(span.color.rgba, p, width * sizeof(GLchan) * 4);
413 p += (width * sizeof(GLchan) * 4);
414 }
415 else {
416 /* get from framebuffer */
417 if (changeBuffer) {
418 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
419 ctx->Pixel.DriverReadBuffer );
420 if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT) {
421 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
422 }
423 else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT) {
424 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
425 }
426 else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT) {
427 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
428 }
429 else {
430 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
431 }
432 }
433 _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, span.color.rgba );
434 }
435
436 if (changeBuffer) {
437 /* read from the draw buffer again (in case of blending) */
438 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
439 ctx->Color.DriverDrawBuffer );
440 ctx->ReadBuffer->Alpha = saveReadAlpha;
441 }
442
443 if (transferOps) {
444 const GLfloat scale = (1.0F / CHAN_MAXF);
445 GLint k;
446 DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4); /* mac 32k limitation */
447 CHECKARRAY(rgbaFloat, return);
448
449 /* convert chan to float */
450 for (k = 0; k < width; k++) {
451 rgbaFloat[k][RCOMP] = (GLfloat) span.color.rgba[k][RCOMP] * scale;
452 rgbaFloat[k][GCOMP] = (GLfloat) span.color.rgba[k][GCOMP] * scale;
453 rgbaFloat[k][BCOMP] = (GLfloat) span.color.rgba[k][BCOMP] * scale;
454 rgbaFloat[k][ACOMP] = (GLfloat) span.color.rgba[k][ACOMP] * scale;
455 }
456 /* scale & bias */
457 if (transferOps & IMAGE_SCALE_BIAS_BIT) {
458 _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
459 ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
460 ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
461 ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
462 ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
463 }
464 /* color map lookup */
465 if (transferOps & IMAGE_MAP_COLOR_BIT) {
466 _mesa_map_rgba(ctx, width, rgbaFloat);
467 }
468 /* GL_COLOR_TABLE lookup */
469 if (transferOps & IMAGE_COLOR_TABLE_BIT) {
470 _mesa_lookup_rgba(&ctx->ColorTable, width, rgbaFloat);
471 }
472 /* convolution */
473 if (transferOps & IMAGE_CONVOLUTION_BIT) {
474 abort(); /* should never get here; caught at top of function */
475 }
476 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
477 if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
478 _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
479 ctx->Pixel.PostConvolutionScale[RCOMP],
480 ctx->Pixel.PostConvolutionScale[GCOMP],
481 ctx->Pixel.PostConvolutionScale[BCOMP],
482 ctx->Pixel.PostConvolutionScale[ACOMP],
483 ctx->Pixel.PostConvolutionBias[RCOMP],
484 ctx->Pixel.PostConvolutionBias[GCOMP],
485 ctx->Pixel.PostConvolutionBias[BCOMP],
486 ctx->Pixel.PostConvolutionBias[ACOMP]);
487 }
488 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
489 if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
490 _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgbaFloat);
491 }
492 /* color matrix */
493 if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
494 _mesa_transform_rgba(ctx, width, rgbaFloat);
495 }
496 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
497 if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
498 _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgbaFloat);
499 }
500 /* update histogram count */
501 if (transferOps & IMAGE_HISTOGRAM_BIT) {
502 _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
503 }
504 /* update min/max */
505 if (transferOps & IMAGE_MIN_MAX_BIT) {
506 _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
507 }
508 /* clamp to [0,1] and convert float back to chan */
509 for (k = 0; k < width; k++) {
510 GLint r = (GLint) (rgbaFloat[k][RCOMP] * CHAN_MAXF);
511 GLint g = (GLint) (rgbaFloat[k][GCOMP] * CHAN_MAXF);
512 GLint b = (GLint) (rgbaFloat[k][BCOMP] * CHAN_MAXF);
513 GLint a = (GLint) (rgbaFloat[k][ACOMP] * CHAN_MAXF);
514 span.color.rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
515 span.color.rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
516 span.color.rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
517 span.color.rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
518 }
519 UNDEFARRAY(rgbaFloat); /* mac 32k limitation */
520 }
521
522 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
523 span.end = width;
524 _swrast_pixel_texture(ctx, &span);
525 }
526
527 if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
528 (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
529 (const GLchan (*)[4])span.color.rgba, NULL );
530 }
531 else if (zoom) {
532 _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, span.zArray,
533 span.fogArray,
534 (const GLchan (*)[4]) span.color.rgba,
535 desty);
536 }
537 else {
538 span.x = destx;
539 span.y = dy;
540 span.end = width;
541 _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
542 }
543 }
544
545 /* Restore pixel source to be the draw buffer (for blending, etc) */
546 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
547 ctx->Color.DriverDrawBuffer );
548
549 if (overlapping)
550 FREE(tmpImage);
551 }
552
553
554 static void copy_ci_pixels( GLcontext *ctx,
555 GLint srcx, GLint srcy, GLint width, GLint height,
556 GLint destx, GLint desty )
557 {
558 SWcontext *swrast = SWRAST_CONTEXT(ctx);
559 GLuint *tmpImage,*p;
560 GLint sy, dy, stepy;
561 GLint j;
562 GLboolean changeBuffer;
563 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
564 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
565 GLint overlapping;
566 struct sw_span span;
567
568 INIT_SPAN(span);
569 span.arrayMask |= SPAN_INDEX;
570
571 /* Determine if copy should be bottom-to-top or top-to-bottom */
572 if (srcy<desty) {
573 /* top-down max-to-min */
574 sy = srcy + height - 1;
575 dy = desty + height - 1;
576 stepy = -1;
577 }
578 else {
579 /* bottom-up min-to-max */
580 sy = srcy;
581 dy = desty;
582 stepy = 1;
583 }
584
585 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
586 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
587
588 if (ctx->Depth.Test)
589 _mesa_span_default_z(ctx, &span);
590 if (ctx->Fog.Enabled)
591 _mesa_span_default_fog(ctx, &span);
592
593 /* If read and draw buffer are different we must do buffer switching */
594 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
595 || ctx->DrawBuffer != ctx->ReadBuffer;
596
597 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
598 ctx->Pixel.DriverReadBuffer );
599
600 if (overlapping) {
601 GLint ssy = sy;
602 tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
603 if (!tmpImage) {
604 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
605 return;
606 }
607 p = tmpImage;
608 if (changeBuffer) {
609 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
610 ctx->Pixel.DriverReadBuffer );
611 }
612 for (j = 0; j < height; j++, ssy += stepy) {
613 _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
614 p += width;
615 }
616 p = tmpImage;
617 }
618 else {
619 tmpImage = NULL; /* silence compiler warning */
620 p = NULL;
621 }
622
623 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
624 if (overlapping) {
625 MEMCPY(span.color.index, p, width * sizeof(GLuint));
626 p += width;
627 }
628 else {
629 if (changeBuffer) {
630 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
631 ctx->Pixel.DriverReadBuffer );
632 }
633 _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy,
634 span.color.index );
635 }
636
637 if (changeBuffer) {
638 /* set read buffer back to draw buffer (in case of logicops) */
639 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
640 ctx->Color.DriverDrawBuffer );
641 }
642
643 if (shift_or_offset) {
644 _mesa_shift_and_offset_ci( ctx, width, span.color.index );
645 }
646 if (ctx->Pixel.MapColorFlag) {
647 _mesa_map_ci( ctx, width, span.color.index );
648 }
649
650 if (zoom) {
651 _mesa_write_zoomed_index_span(ctx, width, destx, dy,
652 span.zArray, span.fogArray,
653 span.color.index, desty );
654 }
655 else {
656 span.x = destx;
657 span.y = dy;
658 span.end = width;
659 _mesa_write_index_span(ctx, &span, GL_BITMAP);
660 }
661 }
662
663 /* Restore pixel source to be the draw buffer (for blending, etc) */
664 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
665 ctx->Color.DriverDrawBuffer );
666
667 if (overlapping)
668 FREE(tmpImage);
669 }
670
671
672
673 /*
674 * TODO: Optimize!!!!
675 */
676 static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
677 GLint width, GLint height,
678 GLint destx, GLint desty )
679 {
680 GLfloat depth[MAX_WIDTH];
681 GLfloat *p, *tmpImage;
682 GLint sy, dy, stepy;
683 GLint i, j;
684 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
685 GLint overlapping;
686 struct sw_span span;
687
688 INIT_SPAN(span);
689 span.arrayMask |= SPAN_Z;
690
691 if (!ctx->Visual.depthBits) {
692 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
693 return;
694 }
695
696 /* Determine if copy should be bottom-to-top or top-to-bottom */
697 if (srcy<desty) {
698 /* top-down max-to-min */
699 sy = srcy + height - 1;
700 dy = desty + height - 1;
701 stepy = -1;
702 }
703 else {
704 /* bottom-up min-to-max */
705 sy = srcy;
706 dy = desty;
707 stepy = 1;
708 }
709
710 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
711 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
712
713 _mesa_span_default_color(ctx, &span);
714 if (ctx->Fog.Enabled)
715 _mesa_span_default_fog(ctx, &span);
716
717 if (overlapping) {
718 GLint ssy = sy;
719 tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
720 if (!tmpImage) {
721 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
722 return;
723 }
724 p = tmpImage;
725 for (j = 0; j < height; j++, ssy += stepy) {
726 _mesa_read_depth_span_float(ctx, width, srcx, ssy, p);
727 p += width;
728 }
729 p = tmpImage;
730 }
731 else {
732 tmpImage = NULL; /* silence compiler warning */
733 p = NULL;
734 }
735
736 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
737 if (overlapping) {
738 MEMCPY(depth, p, width * sizeof(GLfloat));
739 p += width;
740 }
741 else {
742 _mesa_read_depth_span_float(ctx, width, srcx, sy, depth);
743 }
744
745 for (i = 0; i < width; i++) {
746 GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
747 span.zArray[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
748 }
749
750 if (ctx->Visual.rgbMode) {
751 if (zoom) {
752 _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, span.zArray,
753 span.fogArray,
754 (const GLchan (*)[4])span.color.rgba,
755 desty );
756 }
757 else {
758 span.x = destx;
759 span.y = dy;
760 span.end = width;
761 _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
762 }
763 }
764 else {
765 if (zoom) {
766 _mesa_write_zoomed_index_span( ctx, width, destx, dy,
767 span.zArray, span.fogArray,
768 span.color.index, desty );
769 }
770 else {
771 span.x = destx;
772 span.y = dy;
773 span.end = width;
774 _mesa_write_index_span(ctx, &span, GL_BITMAP);
775 }
776 }
777 }
778
779 if (overlapping)
780 FREE(tmpImage);
781 }
782
783
784
785 static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
786 GLint width, GLint height,
787 GLint destx, GLint desty )
788 {
789 GLint sy, dy, stepy;
790 GLint j;
791 GLstencil *p, *tmpImage;
792 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
793 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
794 GLint overlapping;
795
796 if (!ctx->Visual.stencilBits) {
797 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
798 return;
799 }
800
801 /* Determine if copy should be bottom-to-top or top-to-bottom */
802 if (srcy < desty) {
803 /* top-down max-to-min */
804 sy = srcy + height - 1;
805 dy = desty + height - 1;
806 stepy = -1;
807 }
808 else {
809 /* bottom-up min-to-max */
810 sy = srcy;
811 dy = desty;
812 stepy = 1;
813 }
814
815 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
816 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
817
818 if (overlapping) {
819 GLint ssy = sy;
820 tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
821 if (!tmpImage) {
822 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
823 return;
824 }
825 p = tmpImage;
826 for (j = 0; j < height; j++, ssy += stepy) {
827 _mesa_read_stencil_span( ctx, width, srcx, ssy, p );
828 p += width;
829 }
830 p = tmpImage;
831 }
832 else {
833 tmpImage = NULL; /* silence compiler warning */
834 p = NULL;
835 }
836
837 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
838 GLstencil stencil[MAX_WIDTH];
839
840 if (overlapping) {
841 MEMCPY(stencil, p, width * sizeof(GLstencil));
842 p += width;
843 }
844 else {
845 _mesa_read_stencil_span( ctx, width, srcx, sy, stencil );
846 }
847
848 if (shift_or_offset) {
849 _mesa_shift_and_offset_stencil( ctx, width, stencil );
850 }
851 if (ctx->Pixel.MapStencilFlag) {
852 _mesa_map_stencil( ctx, width, stencil );
853 }
854
855 if (zoom) {
856 _mesa_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
857 }
858 else {
859 _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
860 }
861 }
862
863 if (overlapping)
864 FREE(tmpImage);
865 }
866
867
868
869
870 void
871 _swrast_CopyPixels( GLcontext *ctx,
872 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
873 GLint destx, GLint desty,
874 GLenum type )
875 {
876 SWcontext *swrast = SWRAST_CONTEXT(ctx);
877 RENDER_START(swrast,ctx);
878
879 if (swrast->NewState)
880 _swrast_validate_derived( ctx );
881
882 if (type == GL_COLOR && ctx->Visual.rgbMode) {
883 copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
884 }
885 else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
886 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
887 }
888 else if (type == GL_DEPTH) {
889 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
890 }
891 else if (type == GL_STENCIL) {
892 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
893 }
894 else {
895 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
896 }
897
898 RENDER_FINISH(swrast,ctx);
899 }