7d2bca38f478796cd0deb4d92f61ab87309273ac
[mesa.git] / src / mesa / swrast / s_copypix.c
1 /* $Id: s_copypix.c,v 1.31 2002/01/28 04:25:56 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, j;
303 GLboolean changeBuffer;
304 GLchan *saveReadAlpha;
305 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
306 GLint overlapping;
307 const GLuint transferOps = ctx->_ImageTransferState;
308 struct sw_span span;
309
310 INIT_SPAN(span);
311 span.arrayMask |= SPAN_RGBA;
312
313 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
314 copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
315 return;
316 }
317
318 /* Determine if copy should be done bottom-to-top or top-to-bottom */
319 if (srcy < desty) {
320 /* top-down max-to-min */
321 sy = srcy + height - 1;
322 dy = desty + height - 1;
323 stepy = -1;
324 }
325 else {
326 /* bottom-up min-to-max */
327 sy = srcy;
328 dy = desty;
329 stepy = 1;
330 }
331
332 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
333 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
334
335 if (ctx->Depth.Test)
336 _mesa_span_default_z(ctx, &span);
337 if (ctx->Fog.Enabled)
338 _mesa_span_default_fog(ctx, &span);
339
340 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
341 && !zoom
342 && destx >= 0
343 && destx + width <= ctx->DrawBuffer->Width) {
344 quick_draw = GL_TRUE;
345 }
346 else {
347 quick_draw = GL_FALSE;
348 }
349
350 /* If read and draw buffer are different we must do buffer switching */
351 saveReadAlpha = ctx->ReadBuffer->Alpha;
352 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
353 || ctx->DrawBuffer != ctx->ReadBuffer;
354
355 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
356 ctx->Pixel.DriverReadBuffer );
357
358 if (overlapping) {
359 GLint ssy = sy;
360 tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
361 if (!tmpImage) {
362 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
363 return;
364 }
365 p = tmpImage;
366 if (changeBuffer) {
367 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
368 ctx->Pixel.DriverReadBuffer );
369 if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
370 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
371 else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
372 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
373 else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
374 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
375 else
376 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
377 }
378 for (j = 0; j < height; j++, ssy += stepy) {
379 _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
380 (GLchan (*)[4]) p );
381 p += (width * sizeof(GLchan) * 4);
382 }
383 p = tmpImage;
384 }
385 else {
386 tmpImage = NULL; /* silence compiler warnings */
387 p = NULL;
388 }
389
390 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
391 /* Get source pixels */
392 if (overlapping) {
393 /* get from buffered image */
394 MEMCPY(span.color.rgba, p, width * sizeof(GLchan) * 4);
395 p += (width * sizeof(GLchan) * 4);
396 }
397 else {
398 /* get from framebuffer */
399 if (changeBuffer) {
400 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
401 ctx->Pixel.DriverReadBuffer );
402 if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT) {
403 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
404 }
405 else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT) {
406 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
407 }
408 else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT) {
409 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
410 }
411 else {
412 ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
413 }
414 }
415 _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, span.color.rgba );
416 }
417
418 if (changeBuffer) {
419 /* read from the draw buffer again (in case of blending) */
420 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
421 ctx->Color.DriverDrawBuffer );
422 ctx->ReadBuffer->Alpha = saveReadAlpha;
423 }
424
425 if (transferOps) {
426 const GLfloat scale = (1.0F / CHAN_MAXF);
427 GLint k;
428 DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4); /* mac 32k limitation */
429 CHECKARRAY(rgbaFloat, return);
430
431 /* convert chan to float */
432 for (k = 0; k < width; k++) {
433 rgbaFloat[k][RCOMP] = (GLfloat) span.color.rgba[k][RCOMP] * scale;
434 rgbaFloat[k][GCOMP] = (GLfloat) span.color.rgba[k][GCOMP] * scale;
435 rgbaFloat[k][BCOMP] = (GLfloat) span.color.rgba[k][BCOMP] * scale;
436 rgbaFloat[k][ACOMP] = (GLfloat) span.color.rgba[k][ACOMP] * scale;
437 }
438 /* scale & bias */
439 if (transferOps & IMAGE_SCALE_BIAS_BIT) {
440 _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
441 ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
442 ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
443 ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
444 ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
445 }
446 /* color map lookup */
447 if (transferOps & IMAGE_MAP_COLOR_BIT) {
448 _mesa_map_rgba(ctx, width, rgbaFloat);
449 }
450 /* GL_COLOR_TABLE lookup */
451 if (transferOps & IMAGE_COLOR_TABLE_BIT) {
452 _mesa_lookup_rgba(&ctx->ColorTable, width, rgbaFloat);
453 }
454 /* convolution */
455 if (transferOps & IMAGE_CONVOLUTION_BIT) {
456 abort(); /* should never get here; caught at top of function */
457 }
458 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
459 if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
460 _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
461 ctx->Pixel.PostConvolutionScale[RCOMP],
462 ctx->Pixel.PostConvolutionScale[GCOMP],
463 ctx->Pixel.PostConvolutionScale[BCOMP],
464 ctx->Pixel.PostConvolutionScale[ACOMP],
465 ctx->Pixel.PostConvolutionBias[RCOMP],
466 ctx->Pixel.PostConvolutionBias[GCOMP],
467 ctx->Pixel.PostConvolutionBias[BCOMP],
468 ctx->Pixel.PostConvolutionBias[ACOMP]);
469 }
470 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
471 if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
472 _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgbaFloat);
473 }
474 /* color matrix */
475 if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
476 _mesa_transform_rgba(ctx, width, rgbaFloat);
477 }
478 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
479 if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
480 _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgbaFloat);
481 }
482 /* update histogram count */
483 if (transferOps & IMAGE_HISTOGRAM_BIT) {
484 _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
485 }
486 /* update min/max */
487 if (transferOps & IMAGE_MIN_MAX_BIT) {
488 _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
489 }
490 /* clamp to [0,1] and convert float back to chan */
491 for (k = 0; k < width; k++) {
492 GLint r = (GLint) (rgbaFloat[k][RCOMP] * CHAN_MAXF);
493 GLint g = (GLint) (rgbaFloat[k][GCOMP] * CHAN_MAXF);
494 GLint b = (GLint) (rgbaFloat[k][BCOMP] * CHAN_MAXF);
495 GLint a = (GLint) (rgbaFloat[k][ACOMP] * CHAN_MAXF);
496 span.color.rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
497 span.color.rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
498 span.color.rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
499 span.color.rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
500 }
501 UNDEFARRAY(rgbaFloat); /* mac 32k limitation */
502 }
503
504 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._ReallyEnabled) {
505 span.end = width;
506 _swrast_pixel_texture(ctx, &span);
507 }
508
509 if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
510 (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
511 (const GLchan (*)[4])span.color.rgba, NULL );
512 }
513 else if (zoom) {
514 _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, span.zArray,
515 span.fogArray,
516 (const GLchan (*)[4]) span.color.rgba,
517 desty);
518 }
519 else {
520 span.x = destx;
521 span.y = dy;
522 span.end = width;
523 _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
524 }
525 }
526
527 /* Restore pixel source to be the draw buffer (for blending, etc) */
528 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
529 ctx->Color.DriverDrawBuffer );
530
531 if (overlapping)
532 FREE(tmpImage);
533 }
534
535
536 static void copy_ci_pixels( GLcontext *ctx,
537 GLint srcx, GLint srcy, GLint width, GLint height,
538 GLint destx, GLint desty )
539 {
540 SWcontext *swrast = SWRAST_CONTEXT(ctx);
541 GLuint *tmpImage,*p;
542 GLint sy, dy, stepy;
543 GLint j;
544 GLboolean changeBuffer;
545 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
546 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
547 GLint overlapping;
548 struct sw_span span;
549
550 INIT_SPAN(span);
551 span.arrayMask |= SPAN_INDEX;
552
553 /* Determine if copy should be bottom-to-top or top-to-bottom */
554 if (srcy<desty) {
555 /* top-down max-to-min */
556 sy = srcy + height - 1;
557 dy = desty + height - 1;
558 stepy = -1;
559 }
560 else {
561 /* bottom-up min-to-max */
562 sy = srcy;
563 dy = desty;
564 stepy = 1;
565 }
566
567 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
568 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
569
570 if (ctx->Depth.Test)
571 _mesa_span_default_z(ctx, &span);
572 if (ctx->Fog.Enabled)
573 _mesa_span_default_fog(ctx, &span);
574
575 /* If read and draw buffer are different we must do buffer switching */
576 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
577 || ctx->DrawBuffer != ctx->ReadBuffer;
578
579 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
580 ctx->Pixel.DriverReadBuffer );
581
582 if (overlapping) {
583 GLint ssy = sy;
584 tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
585 if (!tmpImage) {
586 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
587 return;
588 }
589 p = tmpImage;
590 if (changeBuffer) {
591 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
592 ctx->Pixel.DriverReadBuffer );
593 }
594 for (j = 0; j < height; j++, ssy += stepy) {
595 _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
596 p += width;
597 }
598 p = tmpImage;
599 }
600 else {
601 tmpImage = NULL; /* silence compiler warning */
602 p = NULL;
603 }
604
605 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
606 if (overlapping) {
607 MEMCPY(span.color.index, p, width * sizeof(GLuint));
608 p += width;
609 }
610 else {
611 if (changeBuffer) {
612 (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
613 ctx->Pixel.DriverReadBuffer );
614 }
615 _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy,
616 span.color.index );
617 }
618
619 if (changeBuffer) {
620 /* set read buffer back to draw buffer (in case of logicops) */
621 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
622 ctx->Color.DriverDrawBuffer );
623 }
624
625 if (shift_or_offset) {
626 _mesa_shift_and_offset_ci( ctx, width, span.color.index );
627 }
628 if (ctx->Pixel.MapColorFlag) {
629 _mesa_map_ci( ctx, width, span.color.index );
630 }
631
632 if (zoom) {
633 _mesa_write_zoomed_index_span(ctx, width, destx, dy,
634 span.zArray, span.fogArray,
635 span.color.index, desty );
636 }
637 else {
638 span.x = destx;
639 span.y = dy;
640 span.end = width;
641 _mesa_write_index_span(ctx, &span, GL_BITMAP);
642 }
643 }
644
645 /* Restore pixel source to be the draw buffer (for blending, etc) */
646 (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
647 ctx->Color.DriverDrawBuffer );
648
649 if (overlapping)
650 FREE(tmpImage);
651 }
652
653
654
655 /*
656 * TODO: Optimize!!!!
657 */
658 static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
659 GLint width, GLint height,
660 GLint destx, GLint desty )
661 {
662 GLfloat depth[MAX_WIDTH];
663 GLfloat *p, *tmpImage;
664 GLint sy, dy, stepy;
665 GLint i, j;
666 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
667 GLint overlapping;
668 struct sw_span span;
669
670 INIT_SPAN(span);
671 span.arrayMask |= SPAN_Z;
672
673 if (!ctx->Visual.depthBits) {
674 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
675 return;
676 }
677
678 /* Determine if copy should be bottom-to-top or top-to-bottom */
679 if (srcy<desty) {
680 /* top-down max-to-min */
681 sy = srcy + height - 1;
682 dy = desty + height - 1;
683 stepy = -1;
684 }
685 else {
686 /* bottom-up min-to-max */
687 sy = srcy;
688 dy = desty;
689 stepy = 1;
690 }
691
692 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
693 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
694
695 _mesa_span_default_color(ctx, &span);
696 if (ctx->Fog.Enabled)
697 _mesa_span_default_fog(ctx, &span);
698
699 if (overlapping) {
700 GLint ssy = sy;
701 tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
702 if (!tmpImage) {
703 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
704 return;
705 }
706 p = tmpImage;
707 for (j = 0; j < height; j++, ssy += stepy) {
708 _mesa_read_depth_span_float(ctx, width, srcx, ssy, p);
709 p += width;
710 }
711 p = tmpImage;
712 }
713 else {
714 tmpImage = NULL; /* silence compiler warning */
715 p = NULL;
716 }
717
718 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
719 if (overlapping) {
720 MEMCPY(depth, p, width * sizeof(GLfloat));
721 p += width;
722 }
723 else {
724 _mesa_read_depth_span_float(ctx, width, srcx, sy, depth);
725 }
726
727 for (i = 0; i < width; i++) {
728 GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
729 span.zArray[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
730 }
731
732 if (ctx->Visual.rgbMode) {
733 if (zoom) {
734 _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, span.zArray,
735 span.fogArray,
736 (const GLchan (*)[4])span.color.rgba,
737 desty );
738 }
739 else {
740 span.x = destx;
741 span.y = dy;
742 span.end = width;
743 _mesa_write_rgba_span(ctx, &span, GL_BITMAP);
744 }
745 }
746 else {
747 if (zoom) {
748 _mesa_write_zoomed_index_span( ctx, width, destx, dy,
749 span.zArray, span.fogArray,
750 span.color.index, desty );
751 }
752 else {
753 span.x = destx;
754 span.y = dy;
755 span.end = width;
756 _mesa_write_index_span(ctx, &span, GL_BITMAP);
757 }
758 }
759 }
760
761 if (overlapping)
762 FREE(tmpImage);
763 }
764
765
766
767 static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
768 GLint width, GLint height,
769 GLint destx, GLint desty )
770 {
771 GLint sy, dy, stepy;
772 GLint j;
773 GLstencil *p, *tmpImage;
774 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
775 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
776 GLint overlapping;
777
778 if (!ctx->Visual.stencilBits) {
779 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
780 return;
781 }
782
783 /* Determine if copy should be bottom-to-top or top-to-bottom */
784 if (srcy < desty) {
785 /* top-down max-to-min */
786 sy = srcy + height - 1;
787 dy = desty + height - 1;
788 stepy = -1;
789 }
790 else {
791 /* bottom-up min-to-max */
792 sy = srcy;
793 dy = desty;
794 stepy = 1;
795 }
796
797 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
798 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
799
800 if (overlapping) {
801 GLint ssy = sy;
802 tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
803 if (!tmpImage) {
804 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
805 return;
806 }
807 p = tmpImage;
808 for (j = 0; j < height; j++, ssy += stepy) {
809 _mesa_read_stencil_span( ctx, width, srcx, ssy, p );
810 p += width;
811 }
812 p = tmpImage;
813 }
814 else {
815 tmpImage = NULL; /* silence compiler warning */
816 p = NULL;
817 }
818
819 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
820 GLstencil stencil[MAX_WIDTH];
821
822 if (overlapping) {
823 MEMCPY(stencil, p, width * sizeof(GLstencil));
824 p += width;
825 }
826 else {
827 _mesa_read_stencil_span( ctx, width, srcx, sy, stencil );
828 }
829
830 if (shift_or_offset) {
831 _mesa_shift_and_offset_stencil( ctx, width, stencil );
832 }
833 if (ctx->Pixel.MapStencilFlag) {
834 _mesa_map_stencil( ctx, width, stencil );
835 }
836
837 if (zoom) {
838 _mesa_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
839 }
840 else {
841 _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
842 }
843 }
844
845 if (overlapping)
846 FREE(tmpImage);
847 }
848
849
850
851
852 void
853 _swrast_CopyPixels( GLcontext *ctx,
854 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
855 GLint destx, GLint desty,
856 GLenum type )
857 {
858 SWcontext *swrast = SWRAST_CONTEXT(ctx);
859 RENDER_START(swrast,ctx);
860
861 if (swrast->NewState)
862 _swrast_validate_derived( ctx );
863
864 if (type == GL_COLOR && ctx->Visual.rgbMode) {
865 copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
866 }
867 else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
868 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
869 }
870 else if (type == GL_DEPTH) {
871 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
872 }
873 else if (type == GL_STENCIL) {
874 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
875 }
876 else {
877 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
878 }
879
880 RENDER_FINISH(swrast,ctx);
881 }